This repository has been archived by the owner on Sep 4, 2024. It is now read-only.
generated from kyma-project/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 5
/
cp_mod_migration_test.go
144 lines (127 loc) · 4.83 KB
/
cp_mod_migration_test.go
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main_test
import (
"context"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
migration "github.tools.sap/framefrog/cp-mod-migrator/pkg"
v211 "github.tools.sap/framefrog/cp-mod-migrator/pkg/cproxy/api/v211"
"github.tools.sap/framefrog/cp-mod-migrator/pkg/extract"
"sigs.k8s.io/controller-runtime/pkg/client"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
)
func deleteObjs(ctx context.Context, obj client.Object, objs ...client.Object) {
for _, obj := range append(objs, obj) {
err := k8sClient.Delete(ctx, obj)
if !errors.IsNotFound(err) {
Expect(err).ShouldNot(HaveOccurred())
}
}
}
var _ = Describe("cp-mod-migrator", Ordered, func() {
var (
ns corev1.Namespace
cm corev1.ConfigMap
cmInfo corev1.ConfigMap
sSet appsv1.StatefulSet
defaultCR v211.ConnectivityProxy
)
BeforeAll(func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// create kyma-system namespace
ns = namespace("kyma-system")
Expect(k8sClient.Create(ctx, &ns)).ShouldNot(HaveOccurred())
// read data
readYaml("hack/testdata/cp_cm.yaml", &cm)
readYaml("hack/testdata/cp_cm_info.yaml", &cmInfo)
readYaml("hack/testdata/cp_stateful_set.yaml", &sSet)
readYaml("hack/testdata/cp_default_cr.yaml", &defaultCR)
})
It("should have types compatible with connectivity-proxy schema", func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cr := cp("connectivity-proxy", ns.Name, true)
Expect(k8sClient.Create(ctx, &cr)).ShouldNot(HaveOccurred())
Expect(k8sClient.Delete(ctx, &cr)).ShouldNot(HaveOccurred())
})
It("should cover all existing cases", func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
for range cms {
cr := defaultCR.DeepCopy()
err := extract.GetCPConfiguration(ctx, cr, mockedClient)
Expect(err).ShouldNot(HaveOccurred())
cr.Namespace = ns.Name
Expect(k8sClient.Create(ctx, cr)).ShouldNot(HaveOccurred())
deleteObjs(ctx, cr)
}
})
It("should migrate data", func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// create config-map with configuration
cmCopy := cm.DeepCopy()
Expect(k8sClient.Create(ctx, cmCopy)).ShouldNot(HaveOccurred())
// create config-map with proxy information
cmInfoCopy := cmInfo.DeepCopy()
Expect(k8sClient.Create(ctx, cmInfoCopy)).ShouldNot(HaveOccurred())
// create stateful-set
sSetCopy := sSet.DeepCopy()
Expect(k8sClient.Create(ctx, sSetCopy)).ShouldNot(HaveOccurred())
// create CR to be migrated
cr := cp("connectivity-proxy", ns.Name, false)
Expect(k8sClient.Create(ctx, cr.DeepCopy())).ShouldNot(HaveOccurred())
// start migration
Expect(migration.Run(ctx, getK8sClient, []string{})).ShouldNot(HaveOccurred())
// fetch created CR
key := client.ObjectKey{Name: "connectivity-proxy", Namespace: "kyma-system"}
Expect(k8sClient.Get(ctx, key, &cr)).ShouldNot(HaveOccurred())
Expect(cr.Annotations).Should(HaveKeyWithValue(v211.CProxyMigratedAnnotation, "true"))
// clean up
deleteObjs(ctx, cmCopy, cmInfoCopy, sSetCopy, &cr)
})
It("should skip migration if the cluster is migrated already", func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// create CR
cr := cp(v211.CProxyDefaultCRName, ns.Name, true)
Expect(k8sClient.Create(ctx, &cr)).ShouldNot(HaveOccurred())
// start migration
Expect(migration.Run(ctx, getK8sClient, []string{})).ShouldNot(HaveOccurred())
deleteObjs(ctx, &cr)
})
It("should skip migration if CR is available but CP is not installed", func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// create config-map with configuration
cmCopy := cm.DeepCopy()
Expect(k8sClient.Create(ctx, cmCopy)).ShouldNot(HaveOccurred())
// create CR
cr := cp(v211.CProxyDefaultCRName, ns.Name, false)
Expect(k8sClient.Create(ctx, &cr)).ShouldNot(HaveOccurred())
// start migration
Expect(migration.Run(ctx, getK8sClient, []string{})).ShouldNot(HaveOccurred())
deleteObjs(ctx, cmCopy, &cr)
})
It("should not migrate when configuration is missing", func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// create CR
cr := cp(v211.CProxyDefaultCRName, ns.Name, false)
Expect(k8sClient.Create(ctx, &cr)).ShouldNot(HaveOccurred())
// create statefu-set
sSetCopy := sSet.DeepCopy()
Expect(k8sClient.Create(ctx, sSetCopy)).ShouldNot(HaveOccurred())
// start migration
Expect(migration.Run(ctx, getK8sClient, []string{})).Should(MatchError(`configmaps "connectivity-proxy" not found`))
deleteObjs(ctx, sSetCopy)
})
It("should not migrate if CR is not available", func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// start migration
Expect(migration.Run(ctx, getK8sClient, []string{})).ShouldNot(HaveOccurred())
})
})