Skip to content

Commit

Permalink
Adding testcase for scale subresource.
Browse files Browse the repository at this point in the history
  • Loading branch information
tanmayja committed Dec 3, 2024
1 parent a9c67ca commit 76926b5
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 11 deletions.
2 changes: 1 addition & 1 deletion test/backup/backup_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion test/backup_service/backup_service_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})

Expand Down
79 changes: 79 additions & 0 deletions test/cluster/autoscaling_test.go
Original file line number Diff line number Diff line change
@@ -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)))
},
)
})
})
5 changes: 4 additions & 1 deletion test/cluster/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -51,6 +52,8 @@ var cfg *rest.Config

var k8sClientSet *kubernetes.Clientset

var dynamicClient *dynamic.DynamicClient

var projectRoot string

var scheme = k8Runtime.NewScheme()
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion test/restore/restore_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
21 changes: 14 additions & 7 deletions test/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -17,7 +18,7 @@ import (
)

func BootStrapTestEnv(scheme *runtime.Scheme) (testEnv *envtest.Environment, cfg *rest.Config,

Check failure on line 20 in test/utils.go

View workflow job for this annotation

GitHub Actions / lint

tooManyResultsChecker: function has more than 5 results, consider to simplify the function (gocritic)
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,
Expand All @@ -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))
Expand All @@ -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
}

0 comments on commit 76926b5

Please sign in to comment.