|
| 1 | +//go:build e2e |
| 2 | +// +build e2e |
| 3 | + |
| 4 | +package default_scaling_strategy_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "encoding/base64" |
| 8 | + "fmt" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/joho/godotenv" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "k8s.io/client-go/kubernetes" |
| 14 | + |
| 15 | + . "github.com/kedacore/keda/v2/tests/helper" // For helper methods |
| 16 | + . "github.com/kedacore/keda/v2/tests/scalers/rabbitmq" |
| 17 | +) |
| 18 | + |
| 19 | +var _ = godotenv.Load("../../.env") // For loading env variables from .env |
| 20 | + |
| 21 | +const ( |
| 22 | + testName = "default-scaling-strategy-test" |
| 23 | +) |
| 24 | + |
| 25 | +var ( |
| 26 | + testNamespace = fmt.Sprintf("%s-ns", testName) |
| 27 | + rmqNamespace = fmt.Sprintf("%s-rmq", testName) |
| 28 | + scaledJobName = fmt.Sprintf("%s-sj", testName) |
| 29 | + queueName = "hello" |
| 30 | + user = fmt.Sprintf("%s-user", testName) |
| 31 | + password = fmt.Sprintf("%s-password", testName) |
| 32 | + vhost = "/" |
| 33 | + connectionString = fmt.Sprintf("amqp://%s:%s@rabbitmq.%s.svc.cluster.local/", user, password, rmqNamespace) |
| 34 | + httpConnectionString = fmt.Sprintf("http://%s:%s@rabbitmq.%s.svc.cluster.local/", user, password, rmqNamespace) |
| 35 | + secretName = fmt.Sprintf("%s-secret", testName) |
| 36 | +) |
| 37 | + |
| 38 | +// YAML templates for your Kubernetes resources |
| 39 | +const ( |
| 40 | + scaledJobTemplate = ` |
| 41 | +apiVersion: v1 |
| 42 | +kind: Secret |
| 43 | +metadata: |
| 44 | + name: {{.SecretName}} |
| 45 | + namespace: {{.TestNamespace}} |
| 46 | +data: |
| 47 | + RabbitApiHost: {{.Base64Connection}} |
| 48 | +--- |
| 49 | +apiVersion: keda.sh/v1alpha1 |
| 50 | +kind: ScaledJob |
| 51 | +metadata: |
| 52 | + name: {{.ScaledJobName}} |
| 53 | + namespace: {{.TestNamespace}} |
| 54 | + labels: |
| 55 | + app: {{.ScaledJobName}} |
| 56 | +spec: |
| 57 | + jobTargetRef: |
| 58 | + template: |
| 59 | + spec: |
| 60 | + containers: |
| 61 | + - name: sleeper |
| 62 | + image: busybox |
| 63 | + command: |
| 64 | + - sleep |
| 65 | + - "300" |
| 66 | + imagePullPolicy: IfNotPresent |
| 67 | + envFrom: |
| 68 | + - secretRef: |
| 69 | + name: {{.SecretName}} |
| 70 | + restartPolicy: Never |
| 71 | + backoffLimit: 1 |
| 72 | + pollingInterval: 5 |
| 73 | + maxReplicaCount: 10 |
| 74 | + scalingStrategy: |
| 75 | + strategy: "default" |
| 76 | + triggers: |
| 77 | + - type: rabbitmq |
| 78 | + metadata: |
| 79 | + queueName: {{.QueueName}} |
| 80 | + hostFromEnv: RabbitApiHost |
| 81 | + mode: QueueLength |
| 82 | + value: '1' |
| 83 | +` |
| 84 | +) |
| 85 | + |
| 86 | +type templateData struct { |
| 87 | + ScaledJobName string |
| 88 | + TestNamespace string |
| 89 | + QueueName string |
| 90 | + SecretName string |
| 91 | + Base64Connection string |
| 92 | +} |
| 93 | + |
| 94 | +func TestScalingStrategy(t *testing.T) { |
| 95 | + kc := GetKubernetesClient(t) |
| 96 | + data, templates := getTemplateData() |
| 97 | + t.Cleanup(func() { |
| 98 | + DeleteKubernetesResources(t, testNamespace, data, templates) |
| 99 | + RMQUninstall(t, rmqNamespace, user, password, vhost, WithoutOAuth()) |
| 100 | + }) |
| 101 | + |
| 102 | + RMQInstall(t, kc, rmqNamespace, user, password, vhost, WithoutOAuth()) |
| 103 | + // Publish 0 messges but create the queue |
| 104 | + RMQPublishMessages(t, rmqNamespace, connectionString, queueName, 0) |
| 105 | + WaitForAllJobsSuccess(t, kc, rmqNamespace, 60, 1) |
| 106 | + |
| 107 | + CreateKubernetesResources(t, kc, testNamespace, data, templates) |
| 108 | + testDefaultScaling(t, kc) |
| 109 | +} |
| 110 | + |
| 111 | +func getTemplateData() (templateData, []Template) { |
| 112 | + return templateData{ |
| 113 | + // Populate fields required in YAML templates |
| 114 | + ScaledJobName: scaledJobName, |
| 115 | + TestNamespace: testNamespace, |
| 116 | + QueueName: queueName, |
| 117 | + Base64Connection: base64.StdEncoding.EncodeToString([]byte(httpConnectionString)), |
| 118 | + SecretName: secretName, |
| 119 | + }, []Template{ |
| 120 | + {Name: "scaledJobTemplate", Config: scaledJobTemplate}, |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +func testDefaultScaling(t *testing.T, kc *kubernetes.Clientset) { |
| 125 | + iterationCount := 20 |
| 126 | + RMQPublishMessages(t, rmqNamespace, connectionString, queueName, 3) |
| 127 | + assert.True(t, WaitForJobCountUntilIteration(t, kc, testNamespace, 3, iterationCount, 1), |
| 128 | + "job count should be %d after %d iterations", 3, iterationCount) |
| 129 | + |
| 130 | + RMQPublishMessages(t, rmqNamespace, connectionString, queueName, 3) |
| 131 | + assert.True(t, WaitForJobCountUntilIteration(t, kc, testNamespace, 6, iterationCount, 1), |
| 132 | + "job count should be %d after %d iterations", 6, iterationCount) |
| 133 | + |
| 134 | + RMQPublishMessages(t, rmqNamespace, connectionString, queueName, 6) |
| 135 | + assert.True(t, WaitForJobCountUntilIteration(t, kc, testNamespace, 10, iterationCount, 1), |
| 136 | + "job count should be %d after %d iterations", 10, iterationCount) |
| 137 | +} |
0 commit comments