Skip to content

Commit

Permalink
change end-to-end test cases
Browse files Browse the repository at this point in the history
Signed-off-by: Shane <[email protected]>
  • Loading branch information
ctccxxd committed Dec 10, 2024
1 parent d1728a4 commit d65b867
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 21 deletions.
6 changes: 4 additions & 2 deletions tests/internals/cloudevent_source/cloudevent_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"github.com/joho/godotenv"
"github.com/stretchr/testify/assert"
"k8s.io/client-go/kubernetes"

. "github.com/kedacore/keda/v2/tests/helper"
)

const (
Expand Down Expand Up @@ -426,6 +424,7 @@ func testErrEventSourceEmitValue(t *testing.T, _ *kubernetes.Clientset, data tem
}
assert.NotEmpty(t, foundEvents)
KubectlDeleteWithTemplate(t, data, "cloudEventSourceTemplate", ceTemplate)
KubectlDeleteWithTemplate(t, data, "deploymentTemplate", deploymentTemplate)
t.Log("--- testErrEventSourceEmitValuetestErrEventSourceEmitValuer---", "cloud event time", lastCloudEventTime)
}

Expand Down Expand Up @@ -470,6 +469,7 @@ func testEventSourceEmitValue(t *testing.T, _ *kubernetes.Clientset, data templa
}
}
assert.NotEmpty(t, foundEvents)
KubectlDeleteWithTemplate(t, data, "deploymentTemplate", deploymentTemplate)
}

// tests error events not emitted by
Expand Down Expand Up @@ -512,6 +512,7 @@ func testErrEventSourceExcludeValue(t *testing.T, _ *kubernetes.Clientset, data
}

KubectlDeleteWithTemplate(t, data, "cloudEventSourceWithExcludeTemplate", ceTemplate)
KubectlDeleteWithTemplate(t, data, "deploymentTemplate", deploymentTemplate)
}

// tests error events in include filter
Expand Down Expand Up @@ -552,6 +553,7 @@ func testErrEventSourceIncludeValue(t *testing.T, _ *kubernetes.Clientset, data
}
assert.NotEmpty(t, foundEvents)
KubectlDeleteWithTemplate(t, data, "cloudEventSourceWithIncludeTemplate", ceTemplate)
KubectlDeleteWithTemplate(t, data, "deploymentTemplate", deploymentTemplate)
}

// tests error event type when creation
Expand Down
9 changes: 0 additions & 9 deletions tests/internals/events/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

"github.com/kedacore/keda/v2/pkg/common/message"
"github.com/kedacore/keda/v2/pkg/eventreason"
. "github.com/kedacore/keda/v2/tests/helper"
)

const (
Expand Down Expand Up @@ -352,14 +351,6 @@ func testNormalEvent(t *testing.T, kc *kubernetes.Clientset, data templateData)
KubectlDeleteWithTemplate(t, data, "scaledObjectTemplate", scaledObjectTemplate)
}

func testTargetNotFoundErr(t *testing.T, _ *kubernetes.Clientset, data templateData) {
t.Log("--- testing target not found error event ---")

KubectlApplyWithTemplate(t, data, "scaledObjectTargetErrTemplate", scaledObjectTargetErrTemplate)
checkingEvent(t, testNamespace, scaledObjectTargetNotFoundName, -2, eventreason.ScaledObjectCheckFailed, message.ScaleTargetNotFoundMsg)
checkingEvent(t, testNamespace, scaledObjectTargetNotFoundName, -1, eventreason.ScaledObjectCheckFailed, message.ScaleTargetErrMsg)
}

func testTargetNotSupportEventErr(t *testing.T, _ *kubernetes.Clientset, data templateData) {
t.Log("--- testing target not support error event ---")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"testing"

"github.com/stretchr/testify/assert"

. "github.com/kedacore/keda/v2/tests/helper"
)

const (
Expand All @@ -18,7 +16,8 @@ const (

var (
testNamespace = fmt.Sprintf("%s-ns", testName)
deploymentName = fmt.Sprintf("%s-deployment", testName)
deployment1Name = fmt.Sprintf("%s-deployment", testName)
deployment2Name = fmt.Sprintf("%s-deployment2", testName)
scaledObject1Name = fmt.Sprintf("%s-so1", testName)
scaledObject2Name = fmt.Sprintf("%s-so2", testName)
emptyTriggersSoName = fmt.Sprintf("%s-so-empty-triggers", testName)
Expand Down Expand Up @@ -218,68 +217,92 @@ func TestScaledObjectValidations(t *testing.T) {
}

func testWithNotScaledWorkload(t *testing.T, data templateData) {
t.Log("--- unscaled workload ---")
t.Log("--- scaled workload ---")
err := KubectlApplyWithErrors(t, data, "deploymentTemplate", deploymentTemplate)
assert.Errorf(t, err, "cannot deploy the deployment - %s", err)

data.ScaledObjectName = scaledObject1Name
err := KubectlApplyWithErrors(t, data, "scaledObjectTemplate", scaledObjectTemplate)
assert.NoErrorf(t, err, "cannot deploy the scaledObject - %s", err)

KubectlDeleteWithTemplate(t, data, "scaledObjectTemplate", scaledObjectTemplate)
KubectlDeleteWithTemplate(t, data, "deploymentTemplate", deploymentTemplate)
}

func testScaledWorkloadByOtherScaledObject(t *testing.T, data templateData) {
t.Log("--- already scaled workload by other scaledobject---")

err := KubectlApplyWithErrors(t, data, "deploymentTemplate", deploymentTemplate)
assert.Errorf(t, err, "cannot deploy the deployment - %s", err)

data.ScaledObjectName = scaledObject1Name
err := KubectlApplyWithErrors(t, data, "scaledObjectTemplate", scaledObjectTemplate)
assert.NoErrorf(t, err, "cannot deploy the scaledObject - %s", err)

data.ScaledObjectName = scaledObject2Name
err = KubectlApplyWithErrors(t, data, "scaledObjectTemplate", scaledObjectTemplate)
assert.Errorf(t, err, "can deploy the scaledObject - %s", err)
assert.Contains(t, err.Error(), fmt.Sprintf("the workload '%s' of type 'apps/v1.Deployment' is already managed by the ScaledObject '%s", deploymentName, scaledObject1Name))
assert.Contains(t, err.Error(), fmt.Sprintf("the workload '%s' of type 'apps/v1.Deployment' is already managed by the ScaledObject '%s", deployment1Name, scaledObject1Name))

data.ScaledObjectName = scaledObject1Name
KubectlDeleteWithTemplate(t, data, "scaledObjectTemplate", scaledObjectTemplate)
KubectlDeleteWithTemplate(t, data, "deploymentTemplate", deploymentTemplate)
}

func testManagedHpaByOtherScaledObject(t *testing.T, data templateData) {
t.Log("--- already managed hpa by other scaledobject---")

data.HpaName = hpaName

err := KubectlApplyWithErrors(t, data, "deploymentTemplate", deploymentTemplate)
assert.Errorf(t, err, "cannot deploy the deployment - %s", err)

data.ScaledObjectName = scaledObject1Name
err := KubectlApplyWithErrors(t, data, "scaledObjectTemplate", customHpaScaledObjectTemplate)
assert.NoErrorf(t, err, "cannot deploy the scaledObject - %s", err)

data.ScaledObjectName = scaledObject2Name
data.DeploymentName = fmt.Sprintf("%s-other-deployment", testName)
data.DeploymentName = deployment2Name

err := KubectlApplyWithErrors(t, data, "deploymentTemplate", deploymentTemplate)
assert.Errorf(t, err, "cannot deploy the deployment - %s", err)

err = KubectlApplyWithErrors(t, data, "scaledObjectTemplate", customHpaScaledObjectTemplate)
assert.Errorf(t, err, "can deploy the scaledObject - %s", err)
assert.Contains(t, err.Error(), fmt.Sprintf("the HPA '%s' is already managed by the ScaledObject '%s", hpaName, scaledObject1Name))

data.ScaledObjectName = scaledObject1Name
KubectlDeleteWithTemplate(t, data, "scaledObjectTemplate", scaledObjectTemplate)
KubectlDeleteWithTemplate(t, data, "deploymentTemplate", deploymentTemplate)
data.DeploymentName = deployment1Name
KubectlDeleteWithTemplate(t, data, "deploymentTemplate", deploymentTemplate)
}

func testScaledWorkloadByOtherHpa(t *testing.T, data templateData) {
t.Log("--- already scaled workload by other hpa---")

err := KubectlApplyWithErrors(t, data, "deploymentTemplate", deploymentTemplate)
assert.Errorf(t, err, "cannot deploy the deployment - %s", err)

data.HpaName = hpaName
err := KubectlApplyWithErrors(t, data, "hpaTemplate", hpaTemplate)
assert.NoErrorf(t, err, "cannot deploy the hpa - %s", err)

data.ScaledObjectName = scaledObject1Name
err = KubectlApplyWithErrors(t, data, "scaledObjectTemplate", scaledObjectTemplate)
assert.Errorf(t, err, "can deploy the scaledObject - %s", err)
assert.Contains(t, err.Error(), fmt.Sprintf("the workload '%s' of type 'apps/v1.Deployment' is already managed by the hpa '%s", deploymentName, hpaName))
assert.Contains(t, err.Error(), fmt.Sprintf("the workload '%s' of type 'apps/v1.Deployment' is already managed by the hpa '%s", deployment1Name, hpaName))

KubectlDeleteWithTemplate(t, data, "hpaTemplate", hpaTemplate)
KubectlDeleteWithTemplate(t, data, "deploymentTemplate", deploymentTemplate)
}

func testScaledWorkloadByOtherHpaWithOwnershipTransfer(t *testing.T, data templateData) {
t.Log("--- already scaled workload by other hpa ownership transfer ---")

err := KubectlApplyWithErrors(t, data, "deploymentTemplate", deploymentTemplate)
assert.Errorf(t, err, "cannot deploy the deployment - %s", err)

data.HpaName = ownershipTransferHpaName
err := KubectlApplyWithErrors(t, data, "hpaTemplate", hpaTemplate)
assert.NoErrorf(t, err, "cannot deploy the hpa - %s", err)
Expand All @@ -290,24 +313,35 @@ func testScaledWorkloadByOtherHpaWithOwnershipTransfer(t *testing.T, data templa

KubectlDeleteWithTemplate(t, data, "hpaTemplate", hpaTemplate)
KubectlDeleteWithTemplate(t, data, "ownershipTransferScaledObjectTemplate", ownershipTransferScaledObjectTemplate)
KubectlDeleteWithTemplate(t, data, "deploymentTemplate", deploymentTemplate)
}

func testMissingCPU(t *testing.T, data templateData) {
t.Log("--- missing cpu resource ---")

err := KubectlApplyWithErrors(t, data, "deploymentTemplate", deploymentTemplate)
assert.Errorf(t, err, "cannot deploy the deployment - %s", err)

data.ScaledObjectName = scaledObject1Name
err := KubectlApplyWithErrors(t, data, "scaledObjectTemplate", cpuScaledObjectTemplate)
assert.Errorf(t, err, "can deploy the scaledObject - %s", err)
assert.Contains(t, err.Error(), fmt.Sprintf("the scaledobject has a cpu trigger but the container %s doesn't have the cpu request defined", deploymentName))
assert.Contains(t, err.Error(), fmt.Sprintf("the scaledobject has a cpu trigger but the container %s doesn't have the cpu request defined", deployment1Name))

KubectlDeleteWithTemplate(t, data, "deploymentTemplate", deploymentTemplate)
}

func testMissingMemory(t *testing.T, data templateData) {
t.Log("--- missing memory resource ---")

err := KubectlApplyWithErrors(t, data, "deploymentTemplate", deploymentTemplate)
assert.Errorf(t, err, "cannot deploy the deployment - %s", err)

data.ScaledObjectName = scaledObject1Name
err := KubectlApplyWithErrors(t, data, "scaledObjectTemplate", memoryScaledObjectTemplate)
assert.Errorf(t, err, "can deploy the scaledObject - %s", err)
assert.Contains(t, err.Error(), fmt.Sprintf("the scaledobject has a memory trigger but the container %s doesn't have the memory request defined", deploymentName))
assert.Contains(t, err.Error(), fmt.Sprintf("the scaledobject has a memory trigger but the container %s doesn't have the memory request defined", deployment1Name))

KubectlDeleteWithTemplate(t, data, "deploymentTemplate", deploymentTemplate)
}

func testWorkloadWithOnlyLimits(t *testing.T, data templateData) {
Expand Down Expand Up @@ -373,15 +407,20 @@ spec:
func testTriggersWithEmptyArray(t *testing.T, data templateData) {
t.Log("--- triggers with empty array ---")

err := KubectlApplyWithErrors(t, data, "deploymentTemplate", deploymentTemplate)
assert.Errorf(t, err, "cannot deploy the deployment - %s", err)

err := KubectlApplyWithErrors(t, data, "emptyTriggersTemplate", emptyTriggersTemplate)
assert.Errorf(t, err, "can deploy the scaledObject - %s", err)
assert.Contains(t, err.Error(), "no triggers defined in the ScaledObject/ScaledJob")

KubectlDeleteWithTemplate(t, data, "deploymentTemplate", deploymentTemplate)
}

func getTemplateData() (templateData, []Template) {
return templateData{
TestNamespace: testNamespace,
DeploymentName: deploymentName,
DeploymentName: deployment1Name,
EmptyTriggersSoName: emptyTriggersSoName,
}, []Template{
{Name: "deploymentTemplate", Config: deploymentTemplate},
Expand Down

0 comments on commit d65b867

Please sign in to comment.