-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcontroller_test.go
139 lines (117 loc) · 4.25 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
package storage_test
import (
"context"
"fmt"
"path/filepath"
"strconv"
"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 label and annotation propagated to pods...", func() {
podLabels := storageSS.Spec.Template.Labels
podAnnotations := storageSS.Spec.Template.Annotations
foundStorage := v1alpha1.Storage{}
Expect(k8sClient.Get(ctx, types.NamespacedName{
Name: testobjects.StorageName,
Namespace: testobjects.YdbNamespace,
}, &foundStorage)).Should(Succeed())
foundStorageGenerationLabel := false
if podLabels[labels.StorageGeneration] == strconv.FormatInt(foundStorage.ObjectMeta.Generation, 10) {
foundStorageGenerationLabel = true
}
Expect(foundStorageGenerationLabel).To(BeTrue())
foundConfigurationChecksumAnnotation := false
if podAnnotations[annotations.ConfigurationChecksum] == resources.GetConfigurationChecksum(foundStorage.Spec.Configuration) {
foundConfigurationChecksumAnnotation = true
}
Expect(foundConfigurationChecksumAnnotation).To(BeTrue())
})
})
})