diff --git a/test/backup/backup_suite_test.go b/test/backup/backup_suite_test.go index e53be0e8..c665daab 100644 --- a/test/backup/backup_suite_test.go +++ b/test/backup/backup_suite_test.go @@ -40,7 +40,7 @@ var _ = BeforeSuite( By("Bootstrapping test environment") var err error - testEnv, _, k8sClient, _, err = test.BootStrapTestEnv(scheme) + testEnv, _, k8sClient, _, _, err = test.BootStrapTestEnv(scheme) Expect(err).NotTo(HaveOccurred()) By("Deploy Backup Service") diff --git a/test/backup_service/backup_service_suite_test.go b/test/backup_service/backup_service_suite_test.go index 3d8ff23c..1979d2e8 100644 --- a/test/backup_service/backup_service_suite_test.go +++ b/test/backup_service/backup_service_suite_test.go @@ -34,7 +34,7 @@ var _ = BeforeSuite( By("Bootstrapping test environment") var err error - testEnv, _, k8sClient, _, err = test.BootStrapTestEnv(scheme) + testEnv, _, k8sClient, _, _, err = test.BootStrapTestEnv(scheme) Expect(err).NotTo(HaveOccurred()) }) diff --git a/test/cluster/autoscaling_test.go b/test/cluster/autoscaling_test.go new file mode 100644 index 00000000..5dcecc59 --- /dev/null +++ b/test/cluster/autoscaling_test.go @@ -0,0 +1,79 @@ +package cluster + +import ( + goctx "context" + "time" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "golang.org/x/net/context" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime/schema" +) + +var _ = FDescribe("AutoScaler", func() { + ctx := goctx.TODO() + + Context("When doing scale operations", func() { + clusterName := "autoscale" + clusterNamespacedName := getNamespacedName( + clusterName, namespace, + ) + + BeforeEach( + func() { + aeroCluster := createDummyAerospikeCluster(clusterNamespacedName, 2) + err := deployCluster(k8sClient, ctx, aeroCluster) + Expect(err).ToNot(HaveOccurred()) + }, + ) + + AfterEach( + func() { + aeroCluster, err := getCluster( + k8sClient, ctx, clusterNamespacedName, + ) + Expect(err).ToNot(HaveOccurred()) + + _ = deleteCluster(k8sClient, ctx, aeroCluster) + }, + ) + + It( + "Should trigger scale up/down via scale subresource", func() { + // Testing over upgrade as it is a long-running operation + By("Scale up the cluster") + + aeroCluster, err := getCluster(k8sClient, ctx, clusterNamespacedName) + Expect(err).ToNot(HaveOccurred()) + + gvr := schema.GroupVersionResource{ + Group: "asdb.aerospike.com", // Replace with your CRD group + Version: "v1", // API version + Resource: "aerospikeclusters", // Replace with your resource + } + + scale, err := dynamicClient.Resource(gvr).Namespace(aeroCluster.Namespace).Get(context.TODO(), + aeroCluster.GetName(), metav1.GetOptions{}, "scale") + Expect(err).ToNot(HaveOccurred()) + + Expect(scale.Object["spec"].(map[string]interface{})["replicas"]).To(Equal(int64(2))) + + scale.Object["spec"].(map[string]interface{})["replicas"] = 3 + + _, err = dynamicClient.Resource(gvr).Namespace(aeroCluster.Namespace).Update(context.TODO(), + scale, metav1.UpdateOptions{}, "scale") + Expect(err).ToNot(HaveOccurred()) + + Eventually( + func() int32 { + aeroCluster, err = getCluster(k8sClient, ctx, clusterNamespacedName) + Expect(err).ToNot(HaveOccurred()) + + return aeroCluster.Spec.Size + }, 1*time.Minute, + ).Should(Equal(int32(3))) + }, + ) + }) +}) diff --git a/test/cluster/suite_test.go b/test/cluster/suite_test.go index d119bc6b..23091cfb 100644 --- a/test/cluster/suite_test.go +++ b/test/cluster/suite_test.go @@ -26,6 +26,7 @@ import ( . "github.com/onsi/gomega" "github.com/onsi/gomega/gexec" k8Runtime "k8s.io/apimachinery/pkg/runtime" + "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" _ "k8s.io/client-go/plugin/pkg/client/auth" "k8s.io/client-go/rest" @@ -51,6 +52,8 @@ var cfg *rest.Config var k8sClientSet *kubernetes.Clientset +var dynamicClient *dynamic.DynamicClient + var projectRoot string var scheme = k8Runtime.NewScheme() @@ -96,7 +99,7 @@ var _ = BeforeSuite( *defaultNetworkType)) var err error - testEnv, cfg, k8sClient, k8sClientSet, err = test.BootStrapTestEnv(scheme) + testEnv, cfg, k8sClient, k8sClientSet, dynamicClient, err = test.BootStrapTestEnv(scheme) Expect(err).NotTo(HaveOccurred()) projectRoot, err = getGitRepoRootPath() diff --git a/test/restore/restore_suite_test.go b/test/restore/restore_suite_test.go index e6b52cca..3b949abc 100644 --- a/test/restore/restore_suite_test.go +++ b/test/restore/restore_suite_test.go @@ -42,7 +42,7 @@ var _ = BeforeSuite( By("Bootstrapping test environment") var err error - testEnv, _, k8sClient, _, err = test.BootStrapTestEnv(scheme) + testEnv, _, k8sClient, _, _, err = test.BootStrapTestEnv(scheme) Expect(err).NotTo(HaveOccurred()) By("Deploy Backup Service") diff --git a/test/utils.go b/test/utils.go index 8db02085..b77497c4 100644 --- a/test/utils.go +++ b/test/utils.go @@ -6,6 +6,7 @@ import ( admissionv1 "k8s.io/api/admission/v1" "k8s.io/apimachinery/pkg/runtime" utilRuntime "k8s.io/apimachinery/pkg/util/runtime" + "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" clientgoscheme "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" @@ -17,7 +18,7 @@ import ( ) func BootStrapTestEnv(scheme *runtime.Scheme) (testEnv *envtest.Environment, cfg *rest.Config, - k8sClient client.Client, k8sClientSet *kubernetes.Clientset, err error) { + k8sClient client.Client, k8sClientSet *kubernetes.Clientset, dynamicClient *dynamic.DynamicClient, err error) { t := true testEnv = &envtest.Environment{ UseExistingCluster: &t, @@ -26,12 +27,12 @@ func BootStrapTestEnv(scheme *runtime.Scheme) (testEnv *envtest.Environment, cfg cfg, err = testEnv.Start() if err != nil { - return testEnv, cfg, k8sClient, k8sClientSet, err + return testEnv, cfg, k8sClient, k8sClientSet, dynamicClient, err } if cfg == nil { err = fmt.Errorf("cfg is nil") - return testEnv, cfg, k8sClient, k8sClientSet, err + return testEnv, cfg, k8sClient, k8sClientSet, dynamicClient, err } utilRuntime.Must(clientgoscheme.AddToScheme(scheme)) @@ -46,20 +47,26 @@ func BootStrapTestEnv(scheme *runtime.Scheme) (testEnv *envtest.Environment, cfg ) if err != nil { - return testEnv, cfg, k8sClient, k8sClientSet, err + return testEnv, cfg, k8sClient, k8sClientSet, dynamicClient, err } if k8sClient == nil { err = fmt.Errorf("k8sClient is nil") - return testEnv, cfg, k8sClient, k8sClientSet, err + return testEnv, cfg, k8sClient, k8sClientSet, dynamicClient, err } k8sClientSet = kubernetes.NewForConfigOrDie(cfg) if k8sClientSet == nil { err = fmt.Errorf("k8sClientSet is nil") - return testEnv, cfg, k8sClient, k8sClientSet, err + return testEnv, cfg, k8sClient, k8sClientSet, dynamicClient, err } - return testEnv, cfg, k8sClient, k8sClientSet, nil + dynamicClient = dynamic.NewForConfigOrDie(cfg) + if dynamicClient == nil { + err = fmt.Errorf("dynamicClient is nil") + return testEnv, cfg, k8sClient, k8sClientSet, dynamicClient, err + } + + return testEnv, cfg, k8sClient, k8sClientSet, dynamicClient, nil }