-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcontroller_test.go
200 lines (174 loc) · 6.79 KB
/
controller_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package storage_test
import (
"context"
"fmt"
"path/filepath"
"strings"
"testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"
"github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1"
testobjects "github.com/ydb-platform/ydb-kubernetes-operator/e2e/tests/test-objects"
"github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations"
"github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/storage"
"github.com/ydb-platform/ydb-kubernetes-operator/internal/labels"
"github.com/ydb-platform/ydb-kubernetes-operator/internal/resources"
"github.com/ydb-platform/ydb-kubernetes-operator/internal/test"
)
var (
k8sClient client.Client
ctx context.Context
)
func TestAPIs(t *testing.T) {
RegisterFailHandler(Fail)
test.SetupK8STestManager(&ctx, &k8sClient, func(mgr *manager.Manager) []test.Reconciler {
return []test.Reconciler{
&storage.Reconciler{
Client: k8sClient,
Scheme: (*mgr).GetScheme(),
},
}
})
RunSpecs(t, "Storage controller medium tests suite")
}
var _ = Describe("Storage controller medium tests", func() {
var namespace corev1.Namespace
BeforeEach(func() {
namespace = corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: testobjects.YdbNamespace,
},
}
Expect(k8sClient.Create(ctx, &namespace)).Should(Succeed())
})
AfterEach(func() {
Expect(k8sClient.Delete(ctx, &namespace)).Should(Succeed())
})
It("Checking field propagation to objects", func() {
storageSample := testobjects.DefaultStorage(filepath.Join("..", "..", "..", "e2e", "tests", "data", "storage-block-4-2-config.yaml"))
tmpFilesDir := "/tmp/mounted_volume"
testVolumeName := "sample-volume"
testVolumeMountPath := fmt.Sprintf("%v/volume", tmpFilesDir)
HostPathDirectoryType := corev1.HostPathDirectory
storageSample.Spec.Volumes = append(storageSample.Spec.Volumes, &corev1.Volume{
Name: testVolumeName,
VolumeSource: corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: testVolumeMountPath,
Type: &HostPathDirectoryType,
},
},
})
Expect(k8sClient.Create(ctx, storageSample)).Should(Succeed())
By("Check volume has been propagated to pods...")
storageStatefulSets := appsv1.StatefulSetList{}
Eventually(func() bool {
Expect(k8sClient.List(ctx, &storageStatefulSets, client.InNamespace(
testobjects.YdbNamespace,
))).Should(Succeed())
foundStatefulSet := false
for _, statefulSet := range storageStatefulSets.Items {
if statefulSet.Name == testobjects.StorageName {
foundStatefulSet = true
break
}
}
return foundStatefulSet
}, test.Timeout, test.Interval).Should(BeTrue())
storageSS := storageStatefulSets.Items[0]
volumes := storageSS.Spec.Template.Spec.Volumes
// Pod Template always has `ydb-config` mounted as a volume, plus in
// this test it also has our test volume. So two in total:
Expect(len(volumes)).To(Equal(1 + 1))
foundVolume := false
for _, volume := range volumes {
if volume.Name == testVolumeName {
foundVolume = true
Expect(volume.VolumeSource.HostPath.Path).To(Equal(testVolumeMountPath))
}
}
Expect(foundVolume).To(BeTrue())
By("Check that configuration checksum annotation propagated to pods...", func() {
podAnnotations := storageSS.Spec.Template.Annotations
foundStorage := v1alpha1.Storage{}
Expect(k8sClient.Get(ctx, types.NamespacedName{
Name: testobjects.StorageName,
Namespace: testobjects.YdbNamespace,
}, &foundStorage)).Should(Succeed())
foundConfigurationChecksumAnnotation := false
if podAnnotations[annotations.ConfigurationChecksum] == resources.SHAChecksum(foundStorage.Spec.Configuration) {
foundConfigurationChecksumAnnotation = true
}
Expect(foundConfigurationChecksumAnnotation).To(BeTrue())
})
By("Check that args with --label propagated to pods...", func() {
podContainerArgs := storageSS.Spec.Template.Spec.Containers[0].Args
var labelArgKey string
var labelArgValue string
for idx, arg := range podContainerArgs {
if arg == "--label" {
labelArgKey = strings.Split(podContainerArgs[idx+1], "=")[0]
labelArgValue = strings.Split(podContainerArgs[idx+1], "=")[1]
}
}
Expect(labelArgKey).Should(BeEquivalentTo(v1alpha1.LabelDeploymentKey))
Expect(labelArgValue).Should(BeEquivalentTo(v1alpha1.LabelDeploymentValueKubernetes))
})
By("Check that statefulset podTemplate labels remain immutable...", func() {
testLabelKey := "ydb-label"
testLabelValue := "test"
By("set additional labels to Storage...")
Eventually(func() error {
foundStorage := v1alpha1.Storage{}
Expect(k8sClient.Get(ctx, types.NamespacedName{
Name: storageSample.Name,
Namespace: testobjects.YdbNamespace,
}, &foundStorage))
additionalLabels := resources.CopyDict(foundStorage.Spec.AdditionalLabels)
additionalLabels[testLabelKey] = testLabelValue
foundStorage.Spec.AdditionalLabels = additionalLabels
return k8sClient.Update(ctx, &foundStorage)
}, test.Timeout, test.Interval).ShouldNot(HaveOccurred())
By("check that additional labels was added...")
foundStatefulSets := appsv1.StatefulSetList{}
Eventually(func() error {
err := k8sClient.List(ctx, &foundStatefulSets,
client.InNamespace(testobjects.YdbNamespace),
)
if err != nil {
return err
}
value, exist := foundStatefulSets.Items[0].Labels[testLabelKey]
if !exist {
return fmt.Errorf("label key `ydb-label` does not exist in StatefulSet. Current labels: %s", foundStatefulSets.Items[0].Labels)
}
if value != testLabelValue {
return fmt.Errorf("label value `ydb-label` in StatefulSet does not equal `test`. Current labels: %s", foundStatefulSets.Items[0].Labels)
}
return nil
}, test.Timeout, test.Interval).ShouldNot(HaveOccurred())
By("check that StatefulSet selector was not updated...")
Expect(*foundStatefulSets.Items[0].Spec.Selector).Should(BeEquivalentTo(
metav1.LabelSelector{
MatchLabels: map[string]string{
labels.StatefulsetComponent: storageSample.Name,
},
},
))
By("check that delete StatefulSet event was detected...")
Expect(k8sClient.List(ctx, &foundStatefulSets, client.InNamespace(testobjects.YdbNamespace))).ShouldNot(HaveOccurred())
Expect(len(foundStatefulSets.Items)).Should(Equal(1))
Expect(k8sClient.Delete(ctx, &foundStatefulSets.Items[0])).ShouldNot(HaveOccurred())
Eventually(func() int {
Expect(k8sClient.List(ctx, &foundStatefulSets, client.InNamespace(testobjects.YdbNamespace))).ShouldNot(HaveOccurred())
return len(foundStatefulSets.Items)
}, test.Timeout, test.Interval).Should(Equal(1))
})
})
})