|
| 1 | +//go:build e2e |
| 2 | +// +build e2e |
| 3 | + |
| 4 | +// go test -v -tags e2e ./internals/pause_scaledjob/pause_scaledjob_test.go |
| 5 | + |
| 6 | +package pause_scaledjob_test |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "fmt" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 16 | + "k8s.io/client-go/kubernetes" |
| 17 | + |
| 18 | + . "github.com/kedacore/keda/v2/tests/helper" |
| 19 | +) |
| 20 | + |
| 21 | +// Load environment variables from .env file |
| 22 | + |
| 23 | +const ( |
| 24 | + testName = "pause-scaledjob-test" |
| 25 | +) |
| 26 | + |
| 27 | +var ( |
| 28 | + testNamespace = fmt.Sprintf("%s-ns", testName) |
| 29 | + serviceName = fmt.Sprintf("%s-service", testName) |
| 30 | + scalerName = fmt.Sprintf("%s-scaler", testName) |
| 31 | + scaledJobName = fmt.Sprintf("%s-sj", testName) |
| 32 | + minReplicaCount = 0 |
| 33 | + maxReplicaCount = 3 |
| 34 | + iterationCountInitial = 15 |
| 35 | + iterationCountLatter = 30 |
| 36 | +) |
| 37 | + |
| 38 | +type templateData struct { |
| 39 | + TestNamespace string |
| 40 | + ServiceName string |
| 41 | + ScalerName string |
| 42 | + ScaledJobName string |
| 43 | + MinReplicaCount, MaxReplicaCount int |
| 44 | + MetricThreshold, MetricValue int |
| 45 | +} |
| 46 | + |
| 47 | +const ( |
| 48 | + serviceTemplate = ` |
| 49 | +apiVersion: v1 |
| 50 | +kind: Service |
| 51 | +metadata: |
| 52 | + name: {{.ServiceName}} |
| 53 | + namespace: {{.TestNamespace}} |
| 54 | +spec: |
| 55 | + ports: |
| 56 | + - port: 6000 |
| 57 | + targetPort: 6000 |
| 58 | + selector: |
| 59 | + app: {{.ScalerName}} |
| 60 | +` |
| 61 | + |
| 62 | + scalerTemplate = ` |
| 63 | +apiVersion: apps/v1 |
| 64 | +kind: Deployment |
| 65 | +metadata: |
| 66 | + name: {{.ScalerName}} |
| 67 | + namespace: {{.TestNamespace}} |
| 68 | + labels: |
| 69 | + app: {{.ScalerName}} |
| 70 | +spec: |
| 71 | + replicas: 1 |
| 72 | + selector: |
| 73 | + matchLabels: |
| 74 | + app: {{.ScalerName}} |
| 75 | + template: |
| 76 | + metadata: |
| 77 | + labels: |
| 78 | + app: {{.ScalerName}} |
| 79 | + spec: |
| 80 | + containers: |
| 81 | + - name: scaler |
| 82 | + image: ghcr.io/kedacore/tests-external-scaler-e2e:latest |
| 83 | + imagePullPolicy: Always |
| 84 | + ports: |
| 85 | + - containerPort: 6000 |
| 86 | +` |
| 87 | + |
| 88 | + scaledJobTemplate = ` |
| 89 | +apiVersion: keda.sh/v1alpha1 |
| 90 | +kind: ScaledJob |
| 91 | +metadata: |
| 92 | + name: {{.ScaledJobName}} |
| 93 | + namespace: {{.TestNamespace}} |
| 94 | +spec: |
| 95 | + pollingInterval: 5 |
| 96 | + maxReplicaCount: {{.MaxReplicaCount}} |
| 97 | + minReplicaCount: {{.MinReplicaCount}} |
| 98 | + successfulJobsHistoryLimit: 0 |
| 99 | + failedJobsHistoryLimit: 0 |
| 100 | + jobTargetRef: |
| 101 | + template: |
| 102 | + spec: |
| 103 | + containers: |
| 104 | + - name: external-executor |
| 105 | + image: busybox |
| 106 | + command: |
| 107 | + - sleep |
| 108 | + - "15" |
| 109 | + imagePullPolicy: IfNotPresent |
| 110 | + restartPolicy: Never |
| 111 | + backoffLimit: 1 |
| 112 | + triggers: |
| 113 | + - type: external |
| 114 | + metadata: |
| 115 | + scalerAddress: {{.ServiceName}}.{{.TestNamespace}}:6000 |
| 116 | + metricThreshold: "{{.MetricThreshold}}" |
| 117 | + metricValue: "{{.MetricValue}}" |
| 118 | +` |
| 119 | +) |
| 120 | + |
| 121 | +// Util function |
| 122 | +func WaitForJobByFilterCountUntilIteration(t *testing.T, kc *kubernetes.Clientset, namespace string, |
| 123 | + target, iterations, intervalSeconds int, listOptions metav1.ListOptions) bool { |
| 124 | + var isTargetAchieved = false |
| 125 | + |
| 126 | + for i := 0; i < iterations; i++ { |
| 127 | + jobList, _ := kc.BatchV1().Jobs(namespace).List(context.Background(), listOptions) |
| 128 | + count := len(jobList.Items) |
| 129 | + |
| 130 | + t.Logf("Waiting for job count to hit target. Namespace - %s, Current - %d, Target - %d", |
| 131 | + namespace, count, target) |
| 132 | + |
| 133 | + if count == target { |
| 134 | + isTargetAchieved = true |
| 135 | + } else { |
| 136 | + isTargetAchieved = false |
| 137 | + } |
| 138 | + |
| 139 | + time.Sleep(time.Duration(intervalSeconds) * time.Second) |
| 140 | + } |
| 141 | + |
| 142 | + return isTargetAchieved |
| 143 | +} |
| 144 | + |
| 145 | +func TestScaler(t *testing.T) { |
| 146 | + // setup |
| 147 | + t.Log("--- setting up ---") |
| 148 | + |
| 149 | + // Create kubernetes resources |
| 150 | + kc := GetKubernetesClient(t) |
| 151 | + metricValue := 1 |
| 152 | + |
| 153 | + data, templates := getTemplateData(metricValue) |
| 154 | + |
| 155 | + listOptions := metav1.ListOptions{ |
| 156 | + FieldSelector: "status.successful=0", |
| 157 | + } |
| 158 | + |
| 159 | + CreateKubernetesResources(t, kc, testNamespace, data, templates) |
| 160 | + |
| 161 | + assert.True(t, WaitForJobByFilterCountUntilIteration(t, kc, testNamespace, data.MetricThreshold, iterationCountInitial, 1, listOptions), |
| 162 | + "job count should be %d after %d iterations", data.MetricThreshold, iterationCountInitial) |
| 163 | + |
| 164 | + // test scaling |
| 165 | + testPause(t, kc, listOptions) |
| 166 | + testUnpause(t, kc, data, listOptions) |
| 167 | + |
| 168 | + // cleanup |
| 169 | + DeleteKubernetesResources(t, testNamespace, data, templates) |
| 170 | +} |
| 171 | + |
| 172 | +func getTemplateData(metricValue int) (templateData, []Template) { |
| 173 | + return templateData{ |
| 174 | + TestNamespace: testNamespace, |
| 175 | + ScaledJobName: scaledJobName, |
| 176 | + ScalerName: scalerName, |
| 177 | + ServiceName: serviceName, |
| 178 | + MinReplicaCount: minReplicaCount, |
| 179 | + MaxReplicaCount: maxReplicaCount, |
| 180 | + MetricThreshold: 1, |
| 181 | + MetricValue: metricValue, |
| 182 | + }, []Template{ |
| 183 | + {Name: "scalerTemplate", Config: scalerTemplate}, |
| 184 | + {Name: "serviceTemplate", Config: serviceTemplate}, |
| 185 | + {Name: "scaledJobTemplate", Config: scaledJobTemplate}, |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +func testPause(t *testing.T, kc *kubernetes.Clientset, listOptions metav1.ListOptions) { |
| 190 | + t.Log("--- testing Paused annotation ---") |
| 191 | + |
| 192 | + _, err := ExecuteCommand(fmt.Sprintf("kubectl annotate scaledjob %s autoscaling.keda.sh/paused=true --namespace %s", scaledJobName, testNamespace)) |
| 193 | + assert.NoErrorf(t, err, "cannot execute command - %s", err) |
| 194 | + |
| 195 | + t.Log("job count does not change as job is paused") |
| 196 | + |
| 197 | + expectedTarget := 0 |
| 198 | + assert.True(t, WaitForJobByFilterCountUntilIteration(t, kc, testNamespace, expectedTarget, iterationCountLatter, 1, listOptions), |
| 199 | + "job count should be %d after %d iterations", expectedTarget, iterationCountLatter) |
| 200 | +} |
| 201 | + |
| 202 | +func testUnpause(t *testing.T, kc *kubernetes.Clientset, data templateData, listOptions metav1.ListOptions) { |
| 203 | + t.Log("--- testing removing Paused annotation ---") |
| 204 | + |
| 205 | + _, err := ExecuteCommand(fmt.Sprintf("kubectl annotate scaledjob %s autoscaling.keda.sh/paused- --namespace %s", scaledJobName, testNamespace)) |
| 206 | + assert.NoErrorf(t, err, "cannot execute command - %s", err) |
| 207 | + |
| 208 | + t.Log("job count increases from zero as job is no longer paused") |
| 209 | + |
| 210 | + expectedTarget := data.MetricThreshold |
| 211 | + assert.True(t, WaitForJobByFilterCountUntilIteration(t, kc, testNamespace, expectedTarget, iterationCountLatter, 1, listOptions), |
| 212 | + "job count should be %d after %d iterations", expectedTarget, iterationCountLatter) |
| 213 | +} |
0 commit comments