-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add e2e test for eagerScalingStrategy
Signed-off-by: June Han <sorrowitsch@gmail.com>
- Loading branch information
Showing
1 changed file
with
116 additions
and
0 deletions.
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
tests/internals/scaling_strategies/eager_scaling_strategy_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
//go:build e2e | ||
// +build e2e | ||
|
||
package eager_scaling_strategy_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/joho/godotenv" | ||
"github.com/stretchr/testify/assert" | ||
"k8s.io/client-go/kubernetes" | ||
|
||
. "github.com/kedacore/keda/v2/tests/helper" // For helper methods | ||
. "github.com/kedacore/keda/v2/tests/scalers/rabbitmq" | ||
) | ||
|
||
var _ = godotenv.Load("../../.env") // For loading env variables from .env | ||
|
||
const ( | ||
testName = "eager-scaling-strategy-test" | ||
) | ||
|
||
var ( | ||
testNamespace = fmt.Sprintf("%s-ns", testName) | ||
rmqNamespace = fmt.Sprintf("%s-rmq", testName) | ||
scaledJobName = fmt.Sprintf("%s-sj", testName) | ||
queueName = "hello" | ||
user = fmt.Sprintf("%s-user", testName) | ||
password = fmt.Sprintf("%s-password", testName) | ||
vhost = "/" | ||
connectionString = fmt.Sprintf("amqp://%s:%s@rabbitmq.%s.svc.cluster.local", user, password, rmqNamespace) | ||
) | ||
|
||
// YAML templates for your Kubernetes resources | ||
const ( | ||
scaledJobTemplate = ` | ||
apiVersion: keda.sh/v1alpha1 | ||
kind: ScaledJob | ||
metadata: | ||
name: {{.ScaledJobName}} | ||
namespace: {{.TestNamespace}} | ||
labels: | ||
app: {{.ScaledJobName}} | ||
spec: | ||
jobTargetRef: | ||
template: | ||
spec: | ||
containers: | ||
- name: sleeper | ||
image: busybox | ||
command: | ||
- sleep | ||
- "300" | ||
imagePullPolicy: IfNotPresent | ||
restartPolicy: Never | ||
backoffLimit: 1 | ||
pollingInterval: 5 | ||
maxReplicaCount: 10 | ||
scalingStrategy: | ||
strategy: "eager" | ||
triggers: | ||
- type: rabbitmq | ||
metadata: | ||
queueName: {{.QueueName}} | ||
hostFromEnv: RabbitApiHost | ||
mode: QueueLength | ||
value: '1' | ||
` | ||
) | ||
|
||
type templateData struct { | ||
ScaledJobName string | ||
TestNamespace string | ||
QueueName string | ||
} | ||
|
||
func TestScalingStrategy(t *testing.T) { | ||
kc := GetKubernetesClient(t) | ||
data, templates := getTemplateData() | ||
t.Cleanup(func() { | ||
DeleteKubernetesResources(t, testNamespace, data, templates) | ||
RMQUninstall(t, rmqNamespace, user, password, vhost, WithoutOAuth()) | ||
}) | ||
|
||
RMQInstall(t, kc, rmqNamespace, user, password, vhost, WithoutOAuth()) | ||
CreateKubernetesResources(t, kc, testNamespace, data, templates) | ||
|
||
testEagerScaling(t, kc) | ||
} | ||
|
||
func getTemplateData() (templateData, []Template) { | ||
return templateData{ | ||
// Populate fields required in YAML templates | ||
ScaledJobName: scaledJobName, | ||
TestNamespace: testNamespace, | ||
QueueName: queueName, | ||
}, []Template{ | ||
{Name: "scaledJobTemplate", Config: scaledJobTemplate}, | ||
} | ||
} | ||
|
||
func testEagerScaling(t *testing.T, kc *kubernetes.Clientset) { | ||
iterationCount := 20 | ||
RMQPublishMessages(t, rmqNamespace, connectionString, queueName, 4) | ||
assert.True(t, WaitForScaledJobCount(t, kc, scaledJobName, testNamespace, 4, iterationCount, 1), | ||
"job count should be %d after %d iterations", 4, iterationCount) | ||
|
||
RMQPublishMessages(t, rmqNamespace, connectionString, queueName, 4) | ||
assert.True(t, WaitForScaledJobCount(t, kc, scaledJobName, testNamespace, 8, iterationCount, 1), | ||
"job count should be %d after %d iterations", 8, iterationCount) | ||
|
||
RMQPublishMessages(t, rmqNamespace, connectionString, queueName, 4) | ||
assert.True(t, WaitForScaledJobCount(t, kc, scaledJobName, testNamespace, 10, iterationCount, 1), | ||
"job count should be %d after %d iterations", 10, iterationCount) | ||
} |