Files
open-cluster-management/pkg/spoke/managedcluster/joining_controller_test.go
2021-11-05 10:00:21 +01:00

75 lines
2.7 KiB
Go

package managedcluster
import (
"context"
"testing"
"time"
clusterfake "open-cluster-management.io/api/client/cluster/clientset/versioned/fake"
clusterinformers "open-cluster-management.io/api/client/cluster/informers/externalversions"
clusterv1 "open-cluster-management.io/api/cluster/v1"
testinghelpers "open-cluster-management.io/registration/pkg/helpers/testing"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
clienttesting "k8s.io/client-go/testing"
)
func TestSyncManagedCluster(t *testing.T) {
cases := []struct {
name string
startingObjects []runtime.Object
validateActions func(t *testing.T, actions []clienttesting.Action)
expectedErr string
}{
{
name: "sync no managed cluster",
startingObjects: []runtime.Object{},
validateActions: testinghelpers.AssertNoActions,
expectedErr: "unable to get managed cluster with name \"testmanagedcluster\" from hub: managedcluster.cluster.open-cluster-management.io \"testmanagedcluster\" not found",
},
{
name: "sync an unaccepted managed cluster",
startingObjects: []runtime.Object{testinghelpers.NewManagedCluster()},
validateActions: testinghelpers.AssertNoActions,
},
{
name: "sync an accepted managed cluster",
startingObjects: []runtime.Object{testinghelpers.NewAcceptedManagedCluster()},
validateActions: func(t *testing.T, actions []clienttesting.Action) {
expectedCondition := metav1.Condition{
Type: clusterv1.ManagedClusterConditionJoined,
Status: metav1.ConditionTrue,
Reason: "ManagedClusterJoined",
Message: "Managed cluster joined",
}
testinghelpers.AssertActions(t, actions, "get", "update")
actual := actions[1].(clienttesting.UpdateActionImpl).Object
testinghelpers.AssertManagedClusterCondition(t, actual.(*clusterv1.ManagedCluster).Status.Conditions, expectedCondition)
},
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
clusterClient := clusterfake.NewSimpleClientset(c.startingObjects...)
clusterInformerFactory := clusterinformers.NewSharedInformerFactory(clusterClient, time.Minute*10)
clusterStore := clusterInformerFactory.Cluster().V1().ManagedClusters().Informer().GetStore()
for _, cluster := range c.startingObjects {
clusterStore.Add(cluster)
}
ctrl := managedClusterJoiningController{
clusterName: testinghelpers.TestManagedClusterName,
hubClusterClient: clusterClient,
hubClusterLister: clusterInformerFactory.Cluster().V1().ManagedClusters().Lister(),
}
syncErr := ctrl.sync(context.TODO(), testinghelpers.NewFakeSyncContext(t, ""))
testinghelpers.AssertError(t, syncErr, c.expectedErr)
c.validateActions(t, clusterClient.Actions())
})
}
}