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

test: add unit test for GenerateScaledObjectName #3892

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
174 changes: 174 additions & 0 deletions control-plane/pkg/autoscaler/keda/mocks/keda_mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
* Copyright 2024 The Knative Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package mocks

import (
"github.com/google/uuid"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

type ObjectMock struct{}

func (ObjectMock) GetNamespace() string {

Check warning on line 27 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L27

Added line #L27 was not covered by tests
//TODO implement me
panic("implement me")

Check warning on line 29 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L29

Added line #L29 was not covered by tests
}

func (ObjectMock) SetNamespace(namespace string) {

Check warning on line 32 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L32

Added line #L32 was not covered by tests
//TODO implement me
panic("implement me")

Check warning on line 34 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L34

Added line #L34 was not covered by tests
}

func (ObjectMock) GetName() string {

Check warning on line 37 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L37

Added line #L37 was not covered by tests
//TODO implement me
panic("implement me")

Check warning on line 39 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L39

Added line #L39 was not covered by tests
}

func (ObjectMock) SetName(name string) {

Check warning on line 42 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L42

Added line #L42 was not covered by tests
//TODO implement me
panic("implement me")

Check warning on line 44 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L44

Added line #L44 was not covered by tests
}

func (ObjectMock) GetGenerateName() string {

Check warning on line 47 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L47

Added line #L47 was not covered by tests
//TODO implement me
panic("implement me")

Check warning on line 49 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L49

Added line #L49 was not covered by tests
}

func (ObjectMock) SetGenerateName(name string) {

Check warning on line 52 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L52

Added line #L52 was not covered by tests
//TODO implement me
panic("implement me")

Check warning on line 54 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L54

Added line #L54 was not covered by tests
}

func (ObjectMock) GetUID() types.UID {
return types.UID(uuid.New().String())
}

func (ObjectMock) SetUID(uid types.UID) {

Check warning on line 61 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L61

Added line #L61 was not covered by tests
//TODO implement me
panic("implement me")

Check warning on line 63 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L63

Added line #L63 was not covered by tests
}

func (ObjectMock) GetResourceVersion() string {

Check warning on line 66 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L66

Added line #L66 was not covered by tests
//TODO implement me
panic("implement me")

Check warning on line 68 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L68

Added line #L68 was not covered by tests
}

func (ObjectMock) SetResourceVersion(version string) {

Check warning on line 71 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L71

Added line #L71 was not covered by tests
//TODO implement me
panic("implement me")

Check warning on line 73 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L73

Added line #L73 was not covered by tests
}

func (ObjectMock) GetGeneration() int64 {

Check warning on line 76 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L76

Added line #L76 was not covered by tests
//TODO implement me
panic("implement me")

Check warning on line 78 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L78

Added line #L78 was not covered by tests
}

func (ObjectMock) SetGeneration(generation int64) {

Check warning on line 81 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L81

Added line #L81 was not covered by tests
//TODO implement me
panic("implement me")

Check warning on line 83 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L83

Added line #L83 was not covered by tests
}

func (ObjectMock) GetSelfLink() string {

Check warning on line 86 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L86

Added line #L86 was not covered by tests
//TODO implement me
panic("implement me")

Check warning on line 88 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L88

Added line #L88 was not covered by tests
}

func (ObjectMock) SetSelfLink(selfLink string) {

Check warning on line 91 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L91

Added line #L91 was not covered by tests
//TODO implement me
panic("implement me")

Check warning on line 93 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L93

Added line #L93 was not covered by tests
}

func (ObjectMock) GetCreationTimestamp() v1.Time {

Check warning on line 96 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L96

Added line #L96 was not covered by tests
//TODO implement me
panic("implement me")

Check warning on line 98 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L98

Added line #L98 was not covered by tests
}

func (ObjectMock) SetCreationTimestamp(timestamp v1.Time) {

Check warning on line 101 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L101

Added line #L101 was not covered by tests
//TODO implement me
panic("implement me")

Check warning on line 103 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L103

Added line #L103 was not covered by tests
}

func (ObjectMock) GetDeletionTimestamp() *v1.Time {

Check warning on line 106 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L106

Added line #L106 was not covered by tests
//TODO implement me
panic("implement me")

Check warning on line 108 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L108

Added line #L108 was not covered by tests
}

func (ObjectMock) SetDeletionTimestamp(timestamp *v1.Time) {

Check warning on line 111 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L111

Added line #L111 was not covered by tests
//TODO implement me
panic("implement me")

Check warning on line 113 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L113

Added line #L113 was not covered by tests
}

func (ObjectMock) GetDeletionGracePeriodSeconds() *int64 {

Check warning on line 116 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L116

Added line #L116 was not covered by tests
//TODO implement me
panic("implement me")

Check warning on line 118 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L118

Added line #L118 was not covered by tests
}

func (ObjectMock) SetDeletionGracePeriodSeconds(i *int64) {

Check warning on line 121 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L121

Added line #L121 was not covered by tests
//TODO implement me
panic("implement me")

Check warning on line 123 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L123

Added line #L123 was not covered by tests
}

func (ObjectMock) GetLabels() map[string]string {

Check warning on line 126 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L126

Added line #L126 was not covered by tests
//TODO implement me
panic("implement me")

Check warning on line 128 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L128

Added line #L128 was not covered by tests
}

func (ObjectMock) SetLabels(labels map[string]string) {

Check warning on line 131 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L131

Added line #L131 was not covered by tests
//TODO implement me
panic("implement me")

Check warning on line 133 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L133

Added line #L133 was not covered by tests
}

func (ObjectMock) GetAnnotations() map[string]string {

Check warning on line 136 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L136

Added line #L136 was not covered by tests
//TODO implement me
panic("implement me")

Check warning on line 138 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L138

Added line #L138 was not covered by tests
}

func (ObjectMock) SetAnnotations(annotations map[string]string) {

Check warning on line 141 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L141

Added line #L141 was not covered by tests
//TODO implement me
panic("implement me")

Check warning on line 143 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L143

Added line #L143 was not covered by tests
}

func (ObjectMock) GetFinalizers() []string {

Check warning on line 146 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L146

Added line #L146 was not covered by tests
//TODO implement me
panic("implement me")

Check warning on line 148 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L148

Added line #L148 was not covered by tests
}

func (ObjectMock) SetFinalizers(finalizers []string) {

Check warning on line 151 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L151

Added line #L151 was not covered by tests
//TODO implement me
panic("implement me")

Check warning on line 153 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L153

Added line #L153 was not covered by tests
}

func (ObjectMock) GetOwnerReferences() []v1.OwnerReference {

Check warning on line 156 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L156

Added line #L156 was not covered by tests
//TODO implement me
panic("implement me")

Check warning on line 158 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L158

Added line #L158 was not covered by tests
}

func (ObjectMock) SetOwnerReferences(references []v1.OwnerReference) {

Check warning on line 161 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L161

Added line #L161 was not covered by tests
//TODO implement me
panic("implement me")

Check warning on line 163 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L163

Added line #L163 was not covered by tests
}

func (ObjectMock) GetManagedFields() []v1.ManagedFieldsEntry {

Check warning on line 166 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L166

Added line #L166 was not covered by tests
//TODO implement me
panic("implement me")

Check warning on line 168 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L168

Added line #L168 was not covered by tests
}

func (ObjectMock) SetManagedFields(managedFields []v1.ManagedFieldsEntry) {

Check warning on line 171 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L171

Added line #L171 was not covered by tests
//TODO implement me
panic("implement me")

Check warning on line 173 in control-plane/pkg/autoscaler/keda/mocks/keda_mock.go

View check run for this annotation

Codecov / codecov/patch

control-plane/pkg/autoscaler/keda/mocks/keda_mock.go#L173

Added line #L173 was not covered by tests
}
4 changes: 3 additions & 1 deletion control-plane/pkg/autoscaler/keda/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ var (
KedaSchemeGroupVersion = schema.GroupVersion{Group: "keda.sh", Version: "v1alpha1"}
)

const scaledObjectPrefixName = "so-"

func GenerateScaledObject(obj metav1.Object, gvk schema.GroupVersionKind, scaleTarget *kedav1alpha1.ScaleTarget, triggers []kedav1alpha1.ScaleTriggers, aconfig autoscaler.AutoscalerConfig) (*kedav1alpha1.ScaledObject, error) {

cooldownPeriod, err := GetInt32ValueFromMap(obj.GetAnnotations(), autoscaler.AutoscalingCooldownPeriodAnnotation, aconfig.AutoscalerDefaults[autoscaler.AutoscalingCooldownPeriodAnnotation])
Expand Down Expand Up @@ -70,7 +72,7 @@ func GenerateScaledObject(obj metav1.Object, gvk schema.GroupVersionKind, scaleT
}

func GenerateScaledObjectName(obj metav1.Object) string {
return fmt.Sprintf("so-%s", string(obj.GetUID()))
return fmt.Sprintf("%s%s", scaledObjectPrefixName, string(obj.GetUID()))
}

func GetInt32ValueFromMap(dict map[string]string, key string, defaultValue int32) (*int32, error) {
Expand Down
64 changes: 64 additions & 0 deletions control-plane/pkg/autoscaler/keda/resources_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2024 The Knative Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package keda

import (
"strings"
"testing"

v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"knative.dev/eventing-kafka-broker/control-plane/pkg/autoscaler/keda/mocks"
)

func TestGenerateScaledObjectName(t *testing.T) {
type args struct {
obj v1.Object
}
tests := []struct {
name string
args args
wantPrefix string
wantSuffix types.UID
}{
Comment on lines +28 to +37
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice use of the table testing pattern! This is how we normally do unit tests in eventing (as I'm sure you noticed)

{
name: "create scaled object name successfully",
args: args{
obj: mocks.ObjectMock{},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := GenerateScaledObjectName(tt.args.obj)

soNameLength := len(got)
if soNameLength != 39 {
t.Errorf("GenerateScaledObjectName() length = %v, want length %v", len(got), soNameLength)
}
if !strings.HasPrefix(got, scaledObjectPrefixName) {
t.Errorf("GenerateScaledObjectName() prefix = %v, want prefix %v", got, tt.wantPrefix)
}

uid := types.UID(strings.TrimPrefix(got, scaledObjectPrefixName))
uidLength := len(uid)
if uidLength != 36 {
t.Errorf("GenerateScaledObjectName() UID length = %v, want length 36", len(string(uid)))
}
})
}
}