Skip to content

Commit

Permalink
highavailability_reconciler unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
eguzki committed Oct 6, 2020
1 parent 4ecb6ad commit 4a11bf8
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions pkg/3scale/amp/operator/highavailability_reconciler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package operator

import (
"context"
"fmt"
"testing"

"github.com/3scale/3scale-operator/pkg/3scale/amp/component"
appsv1alpha1 "github.com/3scale/3scale-operator/pkg/apis/apps/v1alpha1"
"github.com/3scale/3scale-operator/pkg/reconcilers"

appsv1 "github.com/openshift/api/apps/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
fakeclientset "k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)

func TestHighAvailabilityReconciler(t *testing.T) {
var (
log = logf.Log.WithName("operator_test")
systemMysqlRootPassword = "rootPassw1"
systemMysqlDatabaseName = "myDatabaseName"
databaseURL = fmt.Sprintf("mysql2://root:%s@system-mysql/%s", systemMysqlRootPassword, systemMysqlDatabaseName)
)

ctx := context.TODO()

apimanager := basicApimanagerTestHA()
backendRedisSecret := testBackendRedisSecret()
systemRedisSecret := testSystemRedisSecret()
systemDBSecret := getSystemDBSecret(databaseURL, systemMysqlUsername, systemMysqlPassword)
// Objects to track in the fake client.
objs := []runtime.Object{apimanager, backendRedisSecret, systemRedisSecret, systemDBSecret}
s := scheme.Scheme
s.AddKnownTypes(appsv1alpha1.SchemeGroupVersion, apimanager)
err := appsv1.AddToScheme(s)
if err != nil {
t.Fatal(err)
}

// Create a fake client to mock API calls.
cl := fake.NewFakeClient(objs...)
clientAPIReader := fake.NewFakeClient(objs...)
clientset := fakeclientset.NewSimpleClientset()
recorder := record.NewFakeRecorder(10000)

baseReconciler := reconcilers.NewBaseReconciler(cl, s, clientAPIReader, ctx, log, clientset.Discovery(), recorder)
BaseAPIManagerLogicReconciler := NewBaseAPIManagerLogicReconciler(baseReconciler, apimanager)

reconciler := NewHighAvailabilityReconciler(BaseAPIManagerLogicReconciler)
_, err = reconciler.Reconcile()
if err != nil {
t.Fatal(err)
}

cases := []struct {
testName string
secretName string
}{
{"backendRedisTest", component.BackendSecretBackendRedisSecretName},
}
for _, tc := range cases {
t.Run(tc.testName, func(subT *testing.T) {
secret := &v1.Secret{}
namespacedName := types.NamespacedName{
Name: tc.secretName,
Namespace: namespace,
}
err = cl.Get(context.TODO(), namespacedName, secret)
// object must exist, that is all required to be tested
if err != nil {
subT.Errorf("error fetching object %s: %v", tc.secretName, err)
}

// Assert owner ref
if len(secret.GetOwnerReferences()) == 0 {
subT.Errorf("secret %s: owner ref not set", tc.secretName)
}

ownerRef := secret.GetOwnerReferences()[0]

// apimanager.Kind is empty
if ownerRef.Kind != "APIManager" {
subT.Errorf("secret %s: owner ref kind not matched. Expected: %s, found: %s", tc.secretName, "APIManager", ownerRef.Kind)
}

if ownerRef.Name != apimanager.Name {
subT.Errorf("secret %s: owner ref name not matched. Expected: %s, found: %s", tc.secretName, apimanager.Name, ownerRef.Name)
}
})
}
}

0 comments on commit 4a11bf8

Please sign in to comment.