From d0a2da5d17a98779d8ef5e7a910fc210fd550e9d Mon Sep 17 00:00:00 2001 From: spyridon chortis <86228064+spchortis@users.noreply.github.com> Date: Thu, 26 Oct 2023 15:45:13 +0100 Subject: [PATCH] ci: once the demo app is deployed can it scale up and down when under load? (#192) Co-authored-by: Pedro Silva --- examples/sample-app/sample-app.yaml | 7 +++ examples/sample-app/sample_app_test.go | 69 +++++++++++++++++++++----- 2 files changed, 64 insertions(+), 12 deletions(-) diff --git a/examples/sample-app/sample-app.yaml b/examples/sample-app/sample-app.yaml index e3ed9f821..40d7f5da1 100644 --- a/examples/sample-app/sample-app.yaml +++ b/examples/sample-app/sample-app.yaml @@ -4,6 +4,13 @@ metadata: name: sample-app spec: template: + metadata: + annotations: + # For testing purposes set request-per-second to 1 to force scaling + autoscaling.knative.dev/target: "1" + autoscaling.knative.dev/metric: "rps" + labels: + service: "ksample" spec: containers: - image: nginx:stable diff --git a/examples/sample-app/sample_app_test.go b/examples/sample-app/sample_app_test.go index 4293601cc..4943fbed0 100644 --- a/examples/sample-app/sample_app_test.go +++ b/examples/sample-app/sample_app_test.go @@ -3,10 +3,12 @@ package test import ( "fmt" "os" + "strings" "testing" "time" - _ "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/assert" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "github.com/gruntwork-io/terratest/modules/k8s" @@ -30,17 +32,60 @@ func TestExampleApp(t *testing.T) { k8s.KubectlApply(t, kubectlOptions, "sample-app.yaml") // ============================================================= - http_helper.HTTPDoWithRetry( - t, - "GET", - fmt.Sprintf("http://%s", os.Getenv("INITIUM_LB_ENDPOINT")), - []byte("Hello world from initium-platform!"), - map[string]string{"Host": "sample-app.default.example.com"}, - 200, - 30, - 3*time.Second, - nil, - ) + + numRequests := 5 + + for i := 0; i < numRequests; i++ { + + http_helper.HTTPDoWithRetry( + t, + "GET", + fmt.Sprintf("http://%s", os.Getenv("INITIUM_LB_ENDPOINT")), + []byte("Hello world from initium-platform!"), + map[string]string{"Host": "sample-app.default.example.com"}, + 200, + 30, + 3*time.Second, + nil, + ) + } + + sampleAppLabel := "ksample" + prefix := "sample-app" + + //Wait for the pods to be created + time.Sleep(5 * time.Second) + + podOptions := metav1.ListOptions{ + LabelSelector: fmt.Sprintf("service=%s", sampleAppLabel), + } + + pods := k8s.ListPods(t, kubectlOptions, podOptions) + + assert.NotNil(t, pods) + // Validate knative scale-out process was effective + assert.Greater(t, len(pods), 1) + + // Wait for knative to scale-in + time.Sleep(70 * time.Second) + + replicaSets, err := k8s.RunKubectlAndGetOutputE(t, kubectlOptions, "get", "rs") + if err != nil { + t.Fatal(err) + } + + replicaSetNames := strings.Fields(replicaSets) + + var replicaSet string + for _, rs := range replicaSetNames { + if strings.HasPrefix(rs, prefix) { + replicaSet = rs + } + } + + rs := k8s.GetReplicaSet(t, kubectlOptions, replicaSet) + // Validate knative scale-in process was effective + assert.Equal(t, int(rs.Status.Replicas), 0) // ============================================================= k8s.KubectlDelete(t, kubectlOptions, "sample-app.yaml")