-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathconfigmap_test.go
More file actions
125 lines (109 loc) · 3.6 KB
/
configmap_test.go
File metadata and controls
125 lines (109 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
// Copyright (c) 2019-2023 Red Hat, Inc.
// This program and the accompanying materials are made
// available under the terms of the Eclipse Public License 2.0
// which is available at https://www.eclipse.org/legal/epl-2.0/
//
// SPDX-License-Identifier: EPL-2.0
//
// Contributors:
// Red Hat, Inc. - initial API and implementation
//
package deploy
import (
"context"
"testing"
chev2 "github.com/eclipse-che/che-operator/api/v2"
"github.com/eclipse-che/che-operator/pkg/common/chetypes"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)
func TestSyncConfigMapDataToCluster(t *testing.T) {
chev2.SchemeBuilder.AddToScheme(scheme.Scheme)
corev1.SchemeBuilder.AddToScheme(scheme.Scheme)
cli := fake.NewFakeClientWithScheme(scheme.Scheme)
deployContext := &chetypes.DeployContext{
CheCluster: &chev2.CheCluster{
ObjectMeta: metav1.ObjectMeta{
Namespace: "eclipse-che",
Name: "eclipse-che",
},
},
ClusterAPI: chetypes.ClusterAPI{
Client: cli,
NonCachingClient: cli,
Scheme: scheme.Scheme,
},
}
done, err := SyncConfigMapDataToCluster(deployContext, "test", map[string]string{"a": "b"}, "che")
if !done || err != nil {
t.Fatalf("Failed to sync config map: %v", err)
}
// sync a new config map
_, err = SyncConfigMapDataToCluster(deployContext, "test", map[string]string{"c": "d"}, "che")
if err != nil {
t.Fatalf("Failed to sync config map: %v", err)
}
// sync twice to be sure update done correctly
done, err = SyncConfigMapDataToCluster(deployContext, "test", map[string]string{"c": "d"}, "che")
if !done || err != nil {
t.Fatalf("Failed to sync config map: %v", err)
}
actual := &corev1.ConfigMap{}
err = cli.Get(context.TODO(), types.NamespacedName{Name: "test", Namespace: "eclipse-che"}, actual)
if err != nil {
t.Fatalf("Failed to get config map: %v", err)
}
if actual.Data["c"] != "d" {
t.Fatalf("Failed to sync config map: %v", err)
}
if actual.Data["a"] == "b" {
t.Fatalf("Failed to sync config map: %v", err)
}
}
func TestSyncConfigMapSpecDataToCluster(t *testing.T) {
chev2.SchemeBuilder.AddToScheme(scheme.Scheme)
corev1.SchemeBuilder.AddToScheme(scheme.Scheme)
cli := fake.NewFakeClientWithScheme(scheme.Scheme)
deployContext := &chetypes.DeployContext{
CheCluster: &chev2.CheCluster{
ObjectMeta: metav1.ObjectMeta{
Namespace: "eclipse-che",
Name: "eclipse-che",
},
},
ClusterAPI: chetypes.ClusterAPI{
Client: cli,
NonCachingClient: cli,
Scheme: scheme.Scheme,
},
}
spec := InitConfigMap(deployContext, "test", map[string]string{"a": "b"}, "che")
done, err := SyncConfigMapSpecToCluster(deployContext, spec)
if !done || err != nil {
t.Fatalf("Failed to sync config map: %v", err)
}
// check if labels
spec = InitConfigMap(deployContext, "test", map[string]string{"a": "b"}, "che")
spec.ObjectMeta.Labels = map[string]string{"l": "v"}
_, err = SyncConfigMapSpecToCluster(deployContext, spec)
if err != nil {
t.Fatalf("Failed to sync config map: %v", err)
}
// sync twice to be sure update done correctly
done, err = SyncConfigMapSpecToCluster(deployContext, spec)
if !done || err != nil {
t.Fatalf("Failed to sync config map: %v", err)
}
actual := &corev1.ConfigMap{}
err = cli.Get(context.TODO(), types.NamespacedName{Name: "test", Namespace: "eclipse-che"}, actual)
if err != nil {
t.Fatalf("Failed to get config map: %v", err)
}
if actual.ObjectMeta.Labels["l"] != "v" {
t.Fatalf("Failed to sync config map")
}
}