Skip to content

Commit

Permalink
test(controller): use kedav1alpha1.ScaledObject default values
Browse files Browse the repository at this point in the history
Signed-off-by: Pedro Tôrres <[email protected]>
  • Loading branch information
t0rr3sp3dr0 committed Feb 24, 2023
1 parent 4645292 commit e0200ac
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 56 deletions.
79 changes: 35 additions & 44 deletions operator/controllers/scaled_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand All @@ -94,39 +98,26 @@ 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,
)
}

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
}
34 changes: 22 additions & 12 deletions operator/controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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,
},
},
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e0200ac

Please sign in to comment.