Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Review e2e tests #5610

Merged
merged 12 commits into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 71 additions & 27 deletions tests/internals/pause_scaledjob/pause_scaledjob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ var (
scalerName = fmt.Sprintf("%s-scaler", testName)
scaledJobName = fmt.Sprintf("%s-sj", testName)
minReplicaCount = 0
maxReplicaCount = 3
iterationCountInitial = 15
iterationCountLatter = 30
maxReplicaCount = 1
iterationCountInitial = 30
iterationCountLatter = 60
)

type templateData struct {
Expand Down Expand Up @@ -105,7 +105,7 @@ spec:
image: busybox
command:
- sleep
- "15"
- "30"
imagePullPolicy: IfNotPresent
restartPolicy: Never
backoffLimit: 1
Expand All @@ -119,10 +119,32 @@ spec:
)

// Util function
func WaitForJobByFilterCountUntilIteration(t *testing.T, kc *kubernetes.Clientset, namespace string,
target, iterations, intervalSeconds int, listOptions metav1.ListOptions) bool {
var isTargetAchieved = false
func WaitUntilJobIsRunning(t *testing.T, kc *kubernetes.Clientset, namespace string,
target, iterations, intervalSeconds int) bool {
listOptions := metav1.ListOptions{
FieldSelector: "status.successful=0",
}
for i := 0; i < iterations; i++ {
jobList, _ := kc.BatchV1().Jobs(namespace).List(context.Background(), listOptions)
count := len(jobList.Items)

t.Logf("Waiting for job count to hit target. Namespace - %s, Current - %d, Target - %d",
namespace, count, target)

if count == target {
return true
}
time.Sleep(time.Duration(intervalSeconds) * time.Second)
}

return false
}

func WaitUntilJobIsSucceeded(t *testing.T, kc *kubernetes.Clientset, namespace string,
target, iterations, intervalSeconds int) bool {
listOptions := metav1.ListOptions{
FieldSelector: "status.successful=1",
}
for i := 0; i < iterations; i++ {
jobList, _ := kc.BatchV1().Jobs(namespace).List(context.Background(), listOptions)
count := len(jobList.Items)
Expand All @@ -131,15 +153,33 @@ func WaitForJobByFilterCountUntilIteration(t *testing.T, kc *kubernetes.Clientse
namespace, count, target)

if count == target {
isTargetAchieved = true
} else {
isTargetAchieved = false
return true
}
time.Sleep(time.Duration(intervalSeconds) * time.Second)
}

return false
}

func AssertJobNotChangeKeepingIsSucceeded(t *testing.T, kc *kubernetes.Clientset, namespace string,
target, iterations, intervalSeconds int) bool {
listOptions := metav1.ListOptions{
FieldSelector: "status.successful=1",
}
for i := 0; i < iterations; i++ {
jobList, _ := kc.BatchV1().Jobs(namespace).List(context.Background(), listOptions)
count := len(jobList.Items)

t.Logf("Asserting the job count doesn't change. Namespace - %s, Current - %d, Target - %d",
namespace, count, target)

if count != target {
return false
}
time.Sleep(time.Duration(intervalSeconds) * time.Second)
}

return isTargetAchieved
return true
}

func TestScaler(t *testing.T) {
Expand All @@ -152,21 +192,22 @@ func TestScaler(t *testing.T) {

data, templates := getTemplateData(metricValue)

listOptions := metav1.ListOptions{
FieldSelector: "status.successful=0",
}

CreateKubernetesResources(t, kc, testNamespace, data, templates)

assert.True(t, WaitForJobByFilterCountUntilIteration(t, kc, testNamespace, data.MetricThreshold, iterationCountInitial, 1, listOptions),
// we ensure that the gRPC server is up and ready
assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, scalerName, testNamespace, 1, 60, 1),
"replica count should be 0 after 1 minute")

// we ensure that there is a job running
assert.True(t, WaitUntilJobIsRunning(t, kc, testNamespace, data.MetricThreshold, iterationCountInitial, 1),
"job count should be %d after %d iterations", data.MetricThreshold, iterationCountInitial)

// test scaling
testPause(t, kc, listOptions)
testUnpause(t, kc, data, listOptions)
testPause(t, kc)
testUnpause(t, kc, data)

testPause(t, kc, listOptions)
testUnpauseWithBool(t, kc, data, listOptions)
testPause(t, kc)
testUnpauseWithBool(t, kc, data)

// cleanup
DeleteKubernetesResources(t, testNamespace, data, templates)
Expand All @@ -189,20 +230,23 @@ func getTemplateData(metricValue int) (templateData, []Template) {
}
}

func testPause(t *testing.T, kc *kubernetes.Clientset, listOptions metav1.ListOptions) {
func testPause(t *testing.T, kc *kubernetes.Clientset) {
t.Log("--- testing Paused annotation ---")

_, err := ExecuteCommand(fmt.Sprintf("kubectl annotate scaledjob %s autoscaling.keda.sh/paused=true --namespace %s", scaledJobName, testNamespace))
assert.NoErrorf(t, err, "cannot execute command - %s", err)

t.Log("job count does not change as job is paused")

expectedTarget := 0
assert.True(t, WaitForJobByFilterCountUntilIteration(t, kc, testNamespace, expectedTarget, iterationCountLatter, 1, listOptions),
expectedTarget := 1
assert.True(t, WaitUntilJobIsSucceeded(t, kc, testNamespace, expectedTarget, iterationCountLatter, 1),
"job count should be %d after %d iterations", expectedTarget, iterationCountLatter)

assert.True(t, AssertJobNotChangeKeepingIsSucceeded(t, kc, testNamespace, expectedTarget, iterationCountLatter, 1),
"job count should be %d during %d iterations", expectedTarget, iterationCountLatter)
}

func testUnpause(t *testing.T, kc *kubernetes.Clientset, data templateData, listOptions metav1.ListOptions) {
func testUnpause(t *testing.T, kc *kubernetes.Clientset, data templateData) {
t.Log("--- testing removing Paused annotation ---")

_, err := ExecuteCommand(fmt.Sprintf("kubectl annotate scaledjob %s autoscaling.keda.sh/paused- --namespace %s", scaledJobName, testNamespace))
Expand All @@ -211,11 +255,11 @@ func testUnpause(t *testing.T, kc *kubernetes.Clientset, data templateData, list
t.Log("job count increases from zero as job is no longer paused")

expectedTarget := data.MetricThreshold
assert.True(t, WaitForJobByFilterCountUntilIteration(t, kc, testNamespace, expectedTarget, iterationCountLatter, 1, listOptions),
assert.True(t, WaitUntilJobIsRunning(t, kc, testNamespace, expectedTarget, iterationCountLatter, 1),
"job count should be %d after %d iterations", expectedTarget, iterationCountLatter)
}

func testUnpauseWithBool(t *testing.T, kc *kubernetes.Clientset, data templateData, listOptions metav1.ListOptions) {
func testUnpauseWithBool(t *testing.T, kc *kubernetes.Clientset, data templateData) {
t.Log("--- test setting Paused annotation to false ---")

_, err := ExecuteCommand(fmt.Sprintf("kubectl annotate scaledjob %s autoscaling.keda.sh/paused=false --namespace %s --overwrite=true", scaledJobName, testNamespace))
Expand All @@ -224,6 +268,6 @@ func testUnpauseWithBool(t *testing.T, kc *kubernetes.Clientset, data templateDa
t.Log("job count increases from zero as job is no longer paused")

expectedTarget := data.MetricThreshold
assert.True(t, WaitForJobByFilterCountUntilIteration(t, kc, testNamespace, expectedTarget, iterationCountLatter, 1, listOptions),
assert.True(t, WaitUntilJobIsRunning(t, kc, testNamespace, expectedTarget, iterationCountLatter, 1),
"job count should be %d after %d iterations", expectedTarget, iterationCountLatter)
}
12 changes: 12 additions & 0 deletions tests/internals/scaling_modifiers/scaling_modifiers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ spec:
- secretRef:
name: {{.SecretName}}
imagePullPolicy: Always
readinessProbe:
httpGet:
path: /api/value
port: 8080
`
soFallbackTemplate = `
apiVersion: keda.sh/v1alpha1
Expand Down Expand Up @@ -271,6 +275,10 @@ func TestScalingModifiers(t *testing.T) {
data, templates := getTemplateData()
CreateKubernetesResources(t, kc, namespace, data, templates)

// we ensure that the metrics api server is up and ready
assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, metricsServerDeploymentName, namespace, 1, 60, 2),
"replica count should be 1 after 1 minute")

testFormula(t, kc, data)

templates = append(templates, Template{Name: "soComplexFormula", Config: soComplexFormula})
Expand Down Expand Up @@ -309,6 +317,10 @@ func testFormula(t *testing.T, kc *kubernetes.Clientset, data templateData) {
_, err = ExecuteCommand(fmt.Sprintf("kubectl scale deployment/%s --replicas=1 -n %s", metricsServerDeploymentName, namespace))
assert.NoErrorf(t, err, "cannot scale metricsServer deployment - %s", err)

// we ensure that the metrics api server is up and ready
assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, metricsServerDeploymentName, namespace, 1, 60, 2),
"replica count should be 1 after 1 minute")

data.MetricValue = 2
KubectlReplaceWithTemplate(t, data, "updateMetricsTemplate", updateMetricsTemplate)
// 2+2=4; target = 2 -> 4/2 replicas should be 2
Expand Down
23 changes: 14 additions & 9 deletions tests/scalers/rabbitmq/rabbitmq_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/client-go/kubernetes"

"github.com/kedacore/keda/v2/tests/helper"
Expand Down Expand Up @@ -77,7 +78,7 @@ kind: Deployment
metadata:
labels:
app: rabbitmq
name: rabbitmq
name: {{.RabbitServerName}}
namespace: {{.Namespace}}
spec:
replicas: 1
Expand Down Expand Up @@ -200,22 +201,26 @@ type templateData struct {
OAuthClientID string
OAuthScopesKey string
OAuthJwksURI string
RabbitServerName string
}

func RMQInstall(t *testing.T, kc *kubernetes.Clientset, namespace, user, password, vhost string, oauth RabbitOAuthConfig) {
helper.CreateNamespace(t, kc, namespace)
data := templateData{
Namespace: namespace,
VHostName: vhost,
Username: user,
Password: password,
EnableOAuth: oauth.Enable,
OAuthClientID: oauth.ClientID,
OAuthScopesKey: oauth.ScopesKey,
OAuthJwksURI: oauth.JwksURI,
Namespace: namespace,
VHostName: vhost,
Username: user,
Password: password,
EnableOAuth: oauth.Enable,
OAuthClientID: oauth.ClientID,
OAuthScopesKey: oauth.ScopesKey,
OAuthJwksURI: oauth.JwksURI,
RabbitServerName: "rabbitmq",
}

helper.KubectlApplyWithTemplate(t, data, "rmqDeploymentTemplate", deploymentTemplate)
assert.True(t, helper.WaitForDeploymentReplicaReadyCount(t, kc, data.RabbitServerName, namespace, 1, 180, 1),
"replica count should be 1 after 3 minute")
}

func RMQUninstall(t *testing.T, namespace, user, password, vhost string, oauth RabbitOAuthConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,6 @@ func testScalerMetricLatency(t *testing.T) {
for _, label := range labels {
if (*label.Name == labelScaledObject && *label.Value == scaledObjectName) ||
(*label.Name == labelScaledJob && *label.Value == scaledJobName) {
assert.Equal(t, float64(0), *metric.Gauge.Value)
found = true
}
}
Expand Down
Loading