Skip to content

Commit

Permalink
Adding some test cases
Browse files Browse the repository at this point in the history
Signed-off-by: Feny Mehta <[email protected]>
  • Loading branch information
fbm3307 committed Apr 11, 2024
1 parent 84525e0 commit eb16703
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,19 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.
// Error reading the object - requeue the request.
return reconcile.Result{}, err
}

clusterObj := toolchainCluster.DeepCopy()
cachedCluster, ok := cluster.GetCachedToolchainCluster(toolchainCluster.Name)
if !ok {
err := fmt.Errorf("cluster %s not found in cache", toolchainCluster.Name)
reqLogger.Error(err, "failed to retrieve stored data for cluster")
clusterObj.Status.Conditions = []toolchainv1alpha1.ToolchainClusterCondition{clusterOfflineCondition()}
if err := r.client.Status().Update(ctx, clusterObj); err != nil {
reqLogger.Error(err, "failed to update the status of ToolchainCluster")

Check warning on line 73 in controllers/toolchainclusterhealth/toolchaincluster_healthcheck_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/toolchainclusterhealth/toolchaincluster_healthcheck_controller.go#L70-L73

Added lines #L70 - L73 were not covered by tests
}
return reconcile.Result{}, err

Check warning on line 75 in controllers/toolchainclusterhealth/toolchaincluster_healthcheck_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/toolchainclusterhealth/toolchaincluster_healthcheck_controller.go#L75

Added line #L75 was not covered by tests
}

clientSet, err := kubeclientset.NewForConfig(cachedCluster.RestConfig)
if err != nil {
reqLogger.Error(err, "cannot create ClientSet for a ToolchainCluster")
return reconcile.Result{}, err

Check warning on line 80 in controllers/toolchainclusterhealth/toolchaincluster_healthcheck_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/toolchainclusterhealth/toolchaincluster_healthcheck_controller.go#L80

Added line #L80 was not covered by tests
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package toolchainclusterhealth

import (
"context"
"fmt"
"testing"
"time"

Expand All @@ -15,6 +16,7 @@ import (
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
Expand All @@ -40,9 +42,51 @@ func TestClusterHealthChecks(t *testing.T) {

requeAfter := 10 * time.Second
withCA := false
t.Run("ToolchainCluster.status doesn't contain any conditions", func(t *testing.T) {

t.Run("ToolchainCluster not found", func(t *testing.T) {
unstable, sec := newToolchainCluster("unstable", tcNs, "http://unstable.com", toolchainv1alpha1.ToolchainClusterStatus{})

cl := test.NewFakeClient(t, sec)
reset := setupCachedClusters(t, cl, unstable)
defer reset()
service := newToolchainClusterService(t, cl, withCA)
// given
controller, req := prepareReconcile(unstable, cl, service, requeAfter)

// when
_, err := controller.Reconcile(context.TODO(), req)

// then
require.NoError(t, err)

})

t.Run("Error while getting ToolchainCluster", func(t *testing.T) {
unstable, sec := newToolchainCluster("unstable", tcNs, "http://unstable.com", toolchainv1alpha1.ToolchainClusterStatus{})

cl := test.NewFakeClient(t, sec)

cl.MockGet = func(ctx context.Context, key runtimeclient.ObjectKey, obj runtimeclient.Object, opts ...runtimeclient.GetOption) error {
if _, ok := obj.(*toolchainv1alpha1.ToolchainCluster); ok {
return fmt.Errorf("mock error")
}
return cl.Client.Get(ctx, key, obj, opts...)
}

service := newToolchainClusterService(t, cl, withCA)
// given
controller, req := prepareReconcile(unstable, cl, service, requeAfter)

// when
_, err := controller.Reconcile(context.TODO(), req)

// then
require.EqualError(t, err, "mock error")

})

t.Run("ToolchainCluster.status doesn't contain any conditions", func(t *testing.T) {
unstable, sec := newToolchainCluster("unstable", tcNs, "http://unstable.com", toolchainv1alpha1.ToolchainClusterStatus{})
cl := test.NewFakeClient(t, unstable, sec)
reset := setupCachedClusters(t, cl, unstable)
defer reset()
Expand Down

0 comments on commit eb16703

Please sign in to comment.