Skip to content

Commit

Permalink
ci: once the demo app is deployed can it scale up and down when under…
Browse files Browse the repository at this point in the history
… load? (#192)

Co-authored-by: Pedro Silva <[email protected]>
  • Loading branch information
spchortis and Pehesi97 authored Oct 26, 2023
1 parent e39a921 commit d0a2da5
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 12 deletions.
7 changes: 7 additions & 0 deletions examples/sample-app/sample-app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
69 changes: 57 additions & 12 deletions examples/sample-app/sample_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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")
Expand Down

0 comments on commit d0a2da5

Please sign in to comment.