diff --git a/operator/controllers/scaled_object_test.go b/operator/controllers/scaled_object_test.go index a3b6fcba8..401c4c4c0 100644 --- a/operator/controllers/scaled_object_test.go +++ b/operator/controllers/scaled_object_test.go @@ -2,18 +2,16 @@ package controllers import ( "context" - "fmt" "testing" "time" + kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1" "github.com/stretchr/testify/require" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "sigs.k8s.io/controller-runtime/pkg/client" "github.com/kedacore/http-add-on/operator/api/v1alpha1" "github.com/kedacore/http-add-on/operator/controllers/config" - "github.com/kedacore/http-add-on/pkg/k8s" ) func TestCreateOrUpdateScaledObject(t *testing.T) { @@ -49,35 +47,41 @@ func TestCreateOrUpdateScaledObject(t *testing.T) { ) r.NoError(err) - metadata, err := getKeyAsMap(retSO.Object, "metadata") - r.NoError(err) - spec, err := getKeyAsMap(retSO.Object, "spec") - r.NoError(err) + metadata := retSO.ObjectMeta + spec := retSO.Spec - r.Equal(testInfra.ns, metadata["namespace"]) + r.Equal(testInfra.ns, metadata.Namespace) r.Equal( config.AppScaledObjectName(&testInfra.httpso), - metadata["name"], + metadata.Name, ) - // HTTPScaledObject min/max replicas are int32s, - // but the ScaledObject's spec is decoded into - // an *unsructured.Unstructured (basically a map[string]interface{}) - // which is an int64. we need to convert the - // HTTPScaledObject's values into int64s before we compare - r.Equal( - int64(testInfra.httpso.Spec.Replicas.Min), - spec["minReplicaCount"], + var minReplicaCount *int32 + var maxReplicaCount *int32 + if replicas := testInfra.httpso.Spec.Replicas; replicas != nil { + minReplicaCount = replicas.Min + maxReplicaCount = replicas.Max + } + + r.EqualValues( + minReplicaCount, + spec.MinReplicaCount, ) - r.Equal( - int64(testInfra.httpso.Spec.Replicas.Max), - spec["maxReplicaCount"], + r.EqualValues( + maxReplicaCount, + spec.MaxReplicaCount, ) // now update the min and max replicas on the httpso // and call createOrUpdateScaledObject again - testInfra.httpso.Spec.Replicas.Min++ - testInfra.httpso.Spec.Replicas.Max++ + if spec := &testInfra.httpso.Spec; spec.Replicas == nil { + spec.Replicas = &v1alpha1.ReplicaStruct{ + Min: new(int32), + Max: new(int32), + } + } + *testInfra.httpso.Spec.Replicas.Min++ + *testInfra.httpso.Spec.Replicas.Max++ r.NoError(createOrUpdateScaledObject( testInfra.ctx, testInfra.cl, @@ -94,15 +98,14 @@ func TestCreateOrUpdateScaledObject(t *testing.T) { testInfra.httpso, ) r.NoError(err) - spec, err = getKeyAsMap(retSO.Object, "spec") - r.NoError(err) + spec = retSO.Spec r.Equal( - int64(testInfra.httpso.Spec.Replicas.Min), - spec["minReplicaCount"], + *testInfra.httpso.Spec.Replicas.Min, + *spec.MinReplicaCount, ) r.Equal( - int64(testInfra.httpso.Spec.Replicas.Max), - spec["maxReplicaCount"], + *testInfra.httpso.Spec.Replicas.Max, + *spec.MaxReplicaCount, ) } @@ -110,23 +113,11 @@ func getSO( ctx context.Context, cl client.Client, httpso v1alpha1.HTTPScaledObject, -) (*unstructured.Unstructured, error) { - retSO := k8s.NewEmptyScaledObject() +) (*kedav1alpha1.ScaledObject, error) { + var retSO kedav1alpha1.ScaledObject err := cl.Get(ctx, client.ObjectKey{ Namespace: httpso.GetNamespace(), Name: config.AppScaledObjectName(&httpso), - }, retSO) - return retSO, err -} - -func getKeyAsMap(m map[string]interface{}, key string) (map[string]interface{}, error) { - iface, ok := m[key] - if !ok { - return nil, fmt.Errorf("key %s not found in map", key) - } - val, ok := iface.(map[string]interface{}) - if !ok { - return nil, fmt.Errorf("key %s was not a map[string]interface{}", key) - } - return val, nil + }, &retSO) + return &retSO, err } diff --git a/operator/controllers/suite_test.go b/operator/controllers/suite_test.go index deded4b74..f9af2c58c 100644 --- a/operator/controllers/suite_test.go +++ b/operator/controllers/suite_test.go @@ -20,19 +20,33 @@ import ( "testing" "github.com/go-logr/logr" + kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/client-go/kubernetes/scheme" + "k8s.io/apimachinery/pkg/runtime" + clientgoscheme "k8s.io/client-go/kubernetes/scheme" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/log/zap" - "github.com/kedacore/http-add-on/operator/api/v1alpha1" + httpv1alpha1 "github.com/kedacore/http-add-on/operator/api/v1alpha1" // +kubebuilder:scaffold:imports ) +var ( + scheme = runtime.NewScheme() +) + +func init() { + _ = clientgoscheme.AddToScheme(scheme) + + _ = httpv1alpha1.AddToScheme(scheme) + _ = kedav1alpha1.AddToScheme(scheme) + // +kubebuilder:scaffold:scheme +} + // These tests use Ginkgo (BDD-style Go testing framework). Refer to // http://onsi.github.io/ginkgo/ to learn more about Ginkgo. @@ -52,29 +66,25 @@ type commonTestInfra struct { ctx context.Context cl client.Client logger logr.Logger - httpso v1alpha1.HTTPScaledObject + httpso httpv1alpha1.HTTPScaledObject } func newCommonTestInfra(namespace, appName string) *commonTestInfra { ctx := context.Background() - cl := fake.NewClientBuilder().Build() + cl := fake.NewClientBuilder().WithScheme(scheme).Build() logger := logr.Discard() - httpso := v1alpha1.HTTPScaledObject{ + httpso := httpv1alpha1.HTTPScaledObject{ ObjectMeta: metav1.ObjectMeta{ Namespace: namespace, Name: appName, }, - Spec: v1alpha1.HTTPScaledObjectSpec{ - ScaleTargetRef: &v1alpha1.ScaleTargetRef{ + Spec: httpv1alpha1.HTTPScaledObjectSpec{ + ScaleTargetRef: &httpv1alpha1.ScaleTargetRef{ Deployment: appName, Service: appName, Port: 8081, }, - Replicas: v1alpha1.ReplicaStruct{ - Min: 0, - Max: 20, - }, }, } @@ -107,7 +117,7 @@ var _ = BeforeSuite(func(done Done) { // Expect(err).ToNot(HaveOccurred()) // Expect(cfg).ToNot(BeNil()) - err := v1alpha1.AddToScheme(scheme.Scheme) + err := httpv1alpha1.AddToScheme(clientgoscheme.Scheme) Expect(err).NotTo(HaveOccurred()) // +kubebuilder:scaffold:scheme