Skip to content

Commit

Permalink
revise test case
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 d65b867 commit 24c4a42
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 26 deletions.
3 changes: 2 additions & 1 deletion apis/keda/v1alpha1/scaledobject_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package v1alpha1
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/api/meta"
"reflect"
"strconv"

Expand Down Expand Up @@ -260,7 +261,7 @@ func (so *ScaledObject) GetHPAMaxReplicas() int32 {
}

// CheckScaleTargetRefIfExist checks if scaleTargetRef of ScaledObject exists
func (so *ScaledObject) CheckScaleTargetRefIfExist(ctx context.Context) error {
func (so *ScaledObject) CheckScaleTargetRefIfExist(ctx context.Context, restMapper meta.RESTMapper, getFromCacheOrDirect func(ctx context.Context, key client.ObjectKey, obj client.Object) error) error {
soGvkr, err := ParseGVKR(restMapper, so.Spec.ScaleTargetRef.APIVersion, so.Spec.ScaleTargetRef.Kind)
if err != nil {
msg := "Failed to parse Group, Version, Kind, Resource"
Expand Down
2 changes: 1 addition & 1 deletion apis/keda/v1alpha1/scaledobject_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func verifyScaledObjects(incomingSo *ScaledObject, action string, _ bool) error
if err != nil {
return err
}
if err := incomingSo.CheckScaleTargetRefIfExist(context.Background()); err != nil {
if err := incomingSo.CheckScaleTargetRefIfExist(context.Background(), restMapper, getFromCacheOrDirect); err != nil {
return err
}

Expand Down
8 changes: 7 additions & 1 deletion controllers/keda/scaledobject_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ func (r *ScaledObjectReconciler) checkTargetResourceIsScalable(ctx context.Conte
scale, errScale = (r.ScaleClient).Scales(scaledObject.Namespace).Get(ctx, gr, scaledObject.Spec.ScaleTargetRef.Name, metav1.GetOptions{})
if errScale != nil {
// not able to get /scale subresource -> let's check if the resource even exist in the cluster
if err := scaledObject.CheckScaleTargetRefIfExist(ctx); err != nil {
if err := scaledObject.CheckScaleTargetRefIfExist(ctx, r.restMapper, r.getFromCacheOrDirect); err != nil {
r.EventEmitter.Emit(scaledObject, scaledObject.Namespace, corev1.EventTypeWarning, eventingv1alpha1.ScaledObjectFailedType, eventreason.ScaledObjectCheckFailed, message.ScaleTargetNotFoundMsg)
return gvkr, err
}
Expand Down Expand Up @@ -632,3 +632,9 @@ func (r *ScaledObjectReconciler) updateStatusWithTriggersAndAuthsTypes(ctx conte

return kedastatus.UpdateScaledObjectStatus(ctx, r.Client, logger, scaledObject, status)
}

func (r *ScaledObjectReconciler) getFromCacheOrDirect(ctx context.Context, key client.ObjectKey, obj client.Object) error {
// r.Client from controller-runtime here had implemented the logic to choose to perform a cache or live lookup.
err := r.Client.Get(ctx, key, obj, &client.GetOptions{})
return err
}
6 changes: 5 additions & 1 deletion tests/internals/cloudevent_source/cloudevent_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var (
expectedSubject = fmt.Sprintf("/%s/%s/scaledobject/%s", clusterName, namespace, scaledObjectName)
expectedSource = fmt.Sprintf("/%s/keda/keda", clusterName)
lastCloudEventTime = time.Now()
test = "test"
)

type templateData struct {
Expand Down Expand Up @@ -114,7 +115,7 @@ metadata:
namespace: {{.TestNamespace}}
spec:
scaleTargetRef:
name: {{.DeploymentName}}
name: test
triggers:
- type: kubernetes-workload
metadata:
Expand Down Expand Up @@ -377,6 +378,7 @@ func testErrEventSourceEmitValue(t *testing.T, _ *kubernetes.Clientset, data tem

t.Log("--- test emitting eventsource about scaledobject err---")
KubectlApplyWithTemplate(t, data, "cloudEventSourceTemplate", ceTemplate)
data.DeploymentName = test
KubectlApplyWithTemplate(t, data, "deploymentTemplate", deploymentTemplate)
KubectlApplyWithTemplate(t, data, "scaledObjectErrTemplate", scaledObjectErrTemplate)

Expand Down Expand Up @@ -484,6 +486,7 @@ func testErrEventSourceExcludeValue(t *testing.T, _ *kubernetes.Clientset, data
}

KubectlApplyWithTemplate(t, data, "cloudEventSourceWithExcludeTemplate", ceTemplate)
data.DeploymentName = test
KubectlApplyWithTemplate(t, data, "deploymentTemplate", deploymentTemplate)
KubectlApplyWithTemplate(t, data, "scaledObjectErrTemplate", scaledObjectErrTemplate)

Expand Down Expand Up @@ -527,6 +530,7 @@ func testErrEventSourceIncludeValue(t *testing.T, _ *kubernetes.Clientset, data
}

KubectlApplyWithTemplate(t, data, "cloudEventSourceWithIncludeTemplate", ceTemplate)
data.DeploymentName = test
KubectlApplyWithTemplate(t, data, "deploymentTemplate", deploymentTemplate)
KubectlApplyWithTemplate(t, data, "scaledObjectErrTemplate", scaledObjectErrTemplate)

Expand Down
1 change: 0 additions & 1 deletion tests/internals/events/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,6 @@ func TestEvents(t *testing.T) {

// test scaling
testNormalEvent(t, kc, data)
testTargetNotFoundErr(t, kc, data)
testTargetNotSupportEventErr(t, kc, data)

testScaledJobNormalEvent(t, kc, data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (

var (
testNamespace = fmt.Sprintf("%s-ns", testName)
deployment1Name = fmt.Sprintf("%s-deployment", testName)
deployment1Name = fmt.Sprintf("%s-deployment1", testName)
deployment2Name = fmt.Sprintf("%s-deployment2", testName)
scaledObject1Name = fmt.Sprintf("%s-so1", testName)
scaledObject2Name = fmt.Sprintf("%s-so2", testName)
Expand Down Expand Up @@ -219,10 +219,10 @@ func TestScaledObjectValidations(t *testing.T) {
func testWithNotScaledWorkload(t *testing.T, data templateData) {
t.Log("--- scaled workload ---")
err := KubectlApplyWithErrors(t, data, "deploymentTemplate", deploymentTemplate)
assert.Errorf(t, err, "cannot deploy the deployment - %s", err)
assert.NoErrorf(t, err, "cannot deploy the deployment - %s", err)

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

KubectlDeleteWithTemplate(t, data, "scaledObjectTemplate", scaledObjectTemplate)
Expand All @@ -233,10 +233,10 @@ 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)
assert.NoErrorf(t, err, "cannot deploy the deployment - %s", err)

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

data.ScaledObjectName = scaledObject2Name
Expand All @@ -255,17 +255,17 @@ func testManagedHpaByOtherScaledObject(t *testing.T, data templateData) {
data.HpaName = hpaName

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

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

data.ScaledObjectName = scaledObject2Name
data.DeploymentName = deployment2Name

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

err = KubectlApplyWithErrors(t, data, "scaledObjectTemplate", customHpaScaledObjectTemplate)
assert.Errorf(t, err, "can deploy the scaledObject - %s", err)
Expand All @@ -282,10 +282,10 @@ 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)
assert.NoErrorf(t, err, "cannot deploy the deployment - %s", err)

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

data.ScaledObjectName = scaledObject1Name
Expand All @@ -301,10 +301,10 @@ func testScaledWorkloadByOtherHpaWithOwnershipTransfer(t *testing.T, data templa
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)
assert.NoErrorf(t, err, "cannot deploy the deployment - %s", err)

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

data.ScaledObjectName = ownershipTransferScaledObjectName
Expand All @@ -320,10 +320,10 @@ 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)
assert.NoErrorf(t, err, "cannot deploy the deployment - %s", err)

data.ScaledObjectName = scaledObject1Name
err := KubectlApplyWithErrors(t, data, "scaledObjectTemplate", cpuScaledObjectTemplate)
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", deployment1Name))

Expand All @@ -334,10 +334,10 @@ 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)
assert.NoErrorf(t, err, "cannot deploy the deployment - %s", err)

data.ScaledObjectName = scaledObject1Name
err := KubectlApplyWithErrors(t, data, "scaledObjectTemplate", memoryScaledObjectTemplate)
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", deployment1Name))

Expand Down Expand Up @@ -408,13 +408,11 @@ 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)
assert.NoErrorf(t, err, "cannot deploy the deployment - %s", err)

err := KubectlApplyWithErrors(t, data, "emptyTriggersTemplate", emptyTriggersTemplate)
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) {
Expand Down

0 comments on commit 24c4a42

Please sign in to comment.