|
| 1 | +package database_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + . "github.com/onsi/ginkgo/v2" |
| 11 | + . "github.com/onsi/gomega" |
| 12 | + appsv1 "k8s.io/api/apps/v1" |
| 13 | + corev1 "k8s.io/api/core/v1" |
| 14 | + "k8s.io/apimachinery/pkg/api/meta" |
| 15 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 16 | + "k8s.io/apimachinery/pkg/types" |
| 17 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 18 | + "sigs.k8s.io/controller-runtime/pkg/manager" |
| 19 | + |
| 20 | + "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1" |
| 21 | + testobjects "github.com/ydb-platform/ydb-kubernetes-operator/e2e/tests/test-objects" |
| 22 | + . "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/constants" |
| 23 | + "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/database" |
| 24 | + "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/storage" |
| 25 | + "github.com/ydb-platform/ydb-kubernetes-operator/internal/test" |
| 26 | +) |
| 27 | + |
| 28 | +var ( |
| 29 | + k8sClient client.Client |
| 30 | + ctx context.Context |
| 31 | +) |
| 32 | + |
| 33 | +func TestAPIs(t *testing.T) { |
| 34 | + RegisterFailHandler(Fail) |
| 35 | + |
| 36 | + test.SetupK8STestManager(&ctx, &k8sClient, func(mgr *manager.Manager) []test.Reconciler { |
| 37 | + return []test.Reconciler{ |
| 38 | + &storage.Reconciler{ |
| 39 | + Client: k8sClient, |
| 40 | + Scheme: (*mgr).GetScheme(), |
| 41 | + }, |
| 42 | + &database.Reconciler{ |
| 43 | + Client: k8sClient, |
| 44 | + Scheme: (*mgr).GetScheme(), |
| 45 | + }, |
| 46 | + } |
| 47 | + }) |
| 48 | + |
| 49 | + RunSpecs(t, "Database controller medium tests suite") |
| 50 | +} |
| 51 | + |
| 52 | +var _ = Describe("Database controller medium tests", func() { |
| 53 | + var namespace corev1.Namespace |
| 54 | + var storageSample v1alpha1.Storage |
| 55 | + |
| 56 | + BeforeEach(func() { |
| 57 | + namespace = corev1.Namespace{ |
| 58 | + ObjectMeta: metav1.ObjectMeta{ |
| 59 | + Name: testobjects.YdbNamespace, |
| 60 | + }, |
| 61 | + } |
| 62 | + Expect(k8sClient.Create(ctx, &namespace)).Should(Succeed()) |
| 63 | + storageSample = *testobjects.DefaultStorage(filepath.Join("..", "..", "..", "e2e", "tests", "data", "storage-block-4-2-config.yaml")) |
| 64 | + Expect(k8sClient.Create(ctx, &storageSample)).Should(Succeed()) |
| 65 | + |
| 66 | + By("checking that Storage created on local cluster...") |
| 67 | + foundStorage := v1alpha1.Storage{} |
| 68 | + Eventually(func() bool { |
| 69 | + Expect(k8sClient.Get(ctx, types.NamespacedName{ |
| 70 | + Name: storageSample.Name, |
| 71 | + Namespace: testobjects.YdbNamespace, |
| 72 | + }, &foundStorage)) |
| 73 | + return foundStorage.Status.State == StorageInitializing |
| 74 | + }, test.Timeout, test.Interval).Should(BeTrue()) |
| 75 | + |
| 76 | + By("set condition Initialized to Storage...") |
| 77 | + Eventually(func() error { |
| 78 | + foundStorage := v1alpha1.Storage{} |
| 79 | + Expect(k8sClient.Get(ctx, types.NamespacedName{ |
| 80 | + Name: storageSample.Name, |
| 81 | + Namespace: testobjects.YdbNamespace, |
| 82 | + }, &foundStorage)) |
| 83 | + meta.SetStatusCondition(&foundStorage.Status.Conditions, metav1.Condition{ |
| 84 | + Type: StorageInitializedCondition, |
| 85 | + Status: metav1.ConditionTrue, |
| 86 | + Reason: ReasonCompleted, |
| 87 | + }) |
| 88 | + return k8sClient.Status().Update(ctx, &foundStorage) |
| 89 | + }, test.Timeout, test.Interval).ShouldNot(HaveOccurred()) |
| 90 | + }) |
| 91 | + |
| 92 | + AfterEach(func() { |
| 93 | + Expect(k8sClient.Delete(ctx, &storageSample)).Should(Succeed()) |
| 94 | + Expect(k8sClient.Delete(ctx, &namespace)).Should(Succeed()) |
| 95 | + }) |
| 96 | + |
| 97 | + It("Checking field propagation to objects", func() { |
| 98 | + By("Check that Shared Database was created...") |
| 99 | + databaseSample := *testobjects.DefaultDatabase() |
| 100 | + databaseSample.Spec.SharedResources = &v1alpha1.DatabaseResources{ |
| 101 | + StorageUnits: []v1alpha1.StorageUnit{ |
| 102 | + { |
| 103 | + UnitKind: "ssd", |
| 104 | + Count: 1, |
| 105 | + }, |
| 106 | + }, |
| 107 | + } |
| 108 | + Expect(k8sClient.Create(ctx, &databaseSample)).Should(Succeed()) |
| 109 | + |
| 110 | + By("Check that StatefulSet was created...") |
| 111 | + databaseStatefulSet := appsv1.StatefulSet{} |
| 112 | + foundStatefulSets := appsv1.StatefulSetList{} |
| 113 | + Eventually(func() error { |
| 114 | + err := k8sClient.List(ctx, &foundStatefulSets, client.InNamespace( |
| 115 | + testobjects.YdbNamespace)) |
| 116 | + if err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + for idx, statefulSet := range foundStatefulSets.Items { |
| 120 | + if statefulSet.Name == testobjects.DatabaseName { |
| 121 | + databaseStatefulSet = foundStatefulSets.Items[idx] |
| 122 | + return nil |
| 123 | + } |
| 124 | + } |
| 125 | + return errors.New("failed to find StatefulSet") |
| 126 | + }, test.Timeout, test.Interval).ShouldNot(HaveOccurred()) |
| 127 | + |
| 128 | + By("Check that args `--label` propagated to pods...", func() { |
| 129 | + podContainerArgs := databaseStatefulSet.Spec.Template.Spec.Containers[0].Args |
| 130 | + var labelArgKey string |
| 131 | + var labelArgValue string |
| 132 | + for idx, arg := range podContainerArgs { |
| 133 | + if arg == "--label" { |
| 134 | + labelArgKey = strings.Split(podContainerArgs[idx+1], "=")[0] |
| 135 | + labelArgValue = strings.Split(podContainerArgs[idx+1], "=")[1] |
| 136 | + if labelArgKey == v1alpha1.LabelDeploymentKey { |
| 137 | + Expect(labelArgValue).Should(BeEquivalentTo(v1alpha1.LabelDeploymentValueKubernetes)) |
| 138 | + } |
| 139 | + if labelArgKey == v1alpha1.LabelSharedDatabaseKey { |
| 140 | + Expect(labelArgValue).Should(BeEquivalentTo(v1alpha1.LabelSharedDatabaseValueTrue)) |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + }) |
| 145 | + }) |
| 146 | +}) |
0 commit comments