diff --git a/federation/pkg/federation-controller/configmap/configmap_controller_test.go b/federation/pkg/federation-controller/configmap/configmap_controller_test.go index 65e667df6ae..465b790d221 100644 --- a/federation/pkg/federation-controller/configmap/configmap_controller_test.go +++ b/federation/pkg/federation-controller/configmap/configmap_controller_test.go @@ -38,27 +38,33 @@ import ( "github.com/stretchr/testify/assert" ) +const ( + configmaps string = "configmaps" + clusters string = "clusters" + informerStoreErr string = "configmap should have appeared in the informer store" +) + func TestConfigMapController(t *testing.T) { cluster1 := NewCluster("cluster1", apiv1.ConditionTrue) cluster2 := NewCluster("cluster2", apiv1.ConditionTrue) fakeClient := &fakefedclientset.Clientset{} - RegisterFakeList("clusters", &fakeClient.Fake, &federationapi.ClusterList{Items: []federationapi.Cluster{*cluster1}}) - RegisterFakeList("configmaps", &fakeClient.Fake, &apiv1.ConfigMapList{Items: []apiv1.ConfigMap{}}) - configmapWatch := RegisterFakeWatch("configmaps", &fakeClient.Fake) - configmapUpdateChan := RegisterFakeCopyOnUpdate("configmaps", &fakeClient.Fake, configmapWatch) - clusterWatch := RegisterFakeWatch("clusters", &fakeClient.Fake) + RegisterFakeList(clusters, &fakeClient.Fake, &federationapi.ClusterList{Items: []federationapi.Cluster{*cluster1}}) + RegisterFakeList(configmaps, &fakeClient.Fake, &apiv1.ConfigMapList{Items: []apiv1.ConfigMap{}}) + configmapWatch := RegisterFakeWatch(configmaps, &fakeClient.Fake) + configmapUpdateChan := RegisterFakeCopyOnUpdate(configmaps, &fakeClient.Fake, configmapWatch) + clusterWatch := RegisterFakeWatch(clusters, &fakeClient.Fake) cluster1Client := &fakekubeclientset.Clientset{} - cluster1Watch := RegisterFakeWatch("configmaps", &cluster1Client.Fake) - RegisterFakeList("configmaps", &cluster1Client.Fake, &apiv1.ConfigMapList{Items: []apiv1.ConfigMap{}}) - cluster1CreateChan := RegisterFakeCopyOnCreate("configmaps", &cluster1Client.Fake, cluster1Watch) - cluster1UpdateChan := RegisterFakeCopyOnUpdate("configmaps", &cluster1Client.Fake, cluster1Watch) + cluster1Watch := RegisterFakeWatch(configmaps, &cluster1Client.Fake) + RegisterFakeList(configmaps, &cluster1Client.Fake, &apiv1.ConfigMapList{Items: []apiv1.ConfigMap{}}) + cluster1CreateChan := RegisterFakeCopyOnCreate(configmaps, &cluster1Client.Fake, cluster1Watch) + cluster1UpdateChan := RegisterFakeCopyOnUpdate(configmaps, &cluster1Client.Fake, cluster1Watch) cluster2Client := &fakekubeclientset.Clientset{} - cluster2Watch := RegisterFakeWatch("configmaps", &cluster2Client.Fake) - RegisterFakeList("configmaps", &cluster2Client.Fake, &apiv1.ConfigMapList{Items: []apiv1.ConfigMap{}}) - cluster2CreateChan := RegisterFakeCopyOnCreate("configmaps", &cluster2Client.Fake, cluster2Watch) + cluster2Watch := RegisterFakeWatch(configmaps, &cluster2Client.Fake) + RegisterFakeList(configmaps, &cluster2Client.Fake, &apiv1.ConfigMapList{Items: []apiv1.ConfigMap{}}) + cluster2CreateChan := RegisterFakeCopyOnCreate(configmaps, &cluster2Client.Fake, cluster2Watch) configmapController := NewConfigMapController(fakeClient) informer := ToFederatedInformerForTestOnly(configmapController.configmapFederatedInformer) @@ -111,7 +117,7 @@ func TestConfigMapController(t *testing.T) { err := WaitForStoreUpdate( configmapController.configmapFederatedInformer.GetTargetStore(), cluster1.Name, types.NamespacedName{Namespace: configmap1.Namespace, Name: configmap1.Name}.String(), wait.ForeverTestTimeout) - assert.Nil(t, err, "configmap should have appeared in the informer store") + assert.Nil(t, err, informerStoreErr) // Test update federated configmap. configmap1.Annotations = map[string]string{ @@ -129,7 +135,7 @@ func TestConfigMapController(t *testing.T) { configmapController.configmapFederatedInformer.GetTargetStore(), cluster1.Name, types.NamespacedName{Namespace: configmap1.Namespace, Name: configmap1.Name}.String(), configmap1, wait.ForeverTestTimeout) - assert.Nil(t, err, "configmap should have appeared in the informer store") + assert.Nil(t, err, informerStoreErr) // Test update federated configmap. configmap1.Data = map[string]string{ @@ -155,8 +161,11 @@ func TestConfigMapController(t *testing.T) { } func GetConfigMapFromChan(c chan runtime.Object) *apiv1.ConfigMap { - configmap := GetObjectFromChan(c).(*apiv1.ConfigMap) - return configmap + if configmap := GetObjectFromChan(c); configmap == nil { + return nil + } else { + return configmap.(*apiv1.ConfigMap) + } } // Wait till the store is updated with latest configmap. diff --git a/federation/pkg/federation-controller/daemonset/daemonset_controller_test.go b/federation/pkg/federation-controller/daemonset/daemonset_controller_test.go index 3ac9052ba89..ef01b8d30a7 100644 --- a/federation/pkg/federation-controller/daemonset/daemonset_controller_test.go +++ b/federation/pkg/federation-controller/daemonset/daemonset_controller_test.go @@ -38,27 +38,32 @@ import ( "github.com/stretchr/testify/assert" ) +const ( + daemonsets string = "daemonsets" + clusters string = "clusters" +) + func TestDaemonSetController(t *testing.T) { cluster1 := NewCluster("cluster1", apiv1.ConditionTrue) cluster2 := NewCluster("cluster2", apiv1.ConditionTrue) fakeClient := &fakefedclientset.Clientset{} - RegisterFakeList("clusters", &fakeClient.Fake, &federationapi.ClusterList{Items: []federationapi.Cluster{*cluster1}}) - RegisterFakeList("daemonsets", &fakeClient.Fake, &extensionsv1.DaemonSetList{Items: []extensionsv1.DaemonSet{}}) - daemonsetWatch := RegisterFakeWatch("daemonsets", &fakeClient.Fake) - daemonsetUpdateChan := RegisterFakeCopyOnUpdate("daemonsets", &fakeClient.Fake, daemonsetWatch) + RegisterFakeList(clusters, &fakeClient.Fake, &federationapi.ClusterList{Items: []federationapi.Cluster{*cluster1}}) + RegisterFakeList(daemonsets, &fakeClient.Fake, &extensionsv1.DaemonSetList{Items: []extensionsv1.DaemonSet{}}) + daemonsetWatch := RegisterFakeWatch(daemonsets, &fakeClient.Fake) + daemonsetUpdateChan := RegisterFakeCopyOnUpdate(daemonsets, &fakeClient.Fake, daemonsetWatch) clusterWatch := RegisterFakeWatch("clusters", &fakeClient.Fake) cluster1Client := &fakekubeclientset.Clientset{} - cluster1Watch := RegisterFakeWatch("daemonsets", &cluster1Client.Fake) - RegisterFakeList("daemonsets", &cluster1Client.Fake, &extensionsv1.DaemonSetList{Items: []extensionsv1.DaemonSet{}}) - cluster1CreateChan := RegisterFakeCopyOnCreate("daemonsets", &cluster1Client.Fake, cluster1Watch) - cluster1UpdateChan := RegisterFakeCopyOnUpdate("daemonsets", &cluster1Client.Fake, cluster1Watch) + cluster1Watch := RegisterFakeWatch(daemonsets, &cluster1Client.Fake) + RegisterFakeList(daemonsets, &cluster1Client.Fake, &extensionsv1.DaemonSetList{Items: []extensionsv1.DaemonSet{}}) + cluster1CreateChan := RegisterFakeCopyOnCreate(daemonsets, &cluster1Client.Fake, cluster1Watch) + cluster1UpdateChan := RegisterFakeCopyOnUpdate(daemonsets, &cluster1Client.Fake, cluster1Watch) cluster2Client := &fakekubeclientset.Clientset{} - cluster2Watch := RegisterFakeWatch("daemonsets", &cluster2Client.Fake) - RegisterFakeList("daemonsets", &cluster2Client.Fake, &extensionsv1.DaemonSetList{Items: []extensionsv1.DaemonSet{}}) - cluster2CreateChan := RegisterFakeCopyOnCreate("daemonsets", &cluster2Client.Fake, cluster2Watch) + cluster2Watch := RegisterFakeWatch(daemonsets, &cluster2Client.Fake) + RegisterFakeList(daemonsets, &cluster2Client.Fake, &extensionsv1.DaemonSetList{Items: []extensionsv1.DaemonSet{}}) + cluster2CreateChan := RegisterFakeCopyOnCreate(daemonsets, &cluster2Client.Fake, cluster2Watch) daemonsetController := NewDaemonSetController(fakeClient) informer := ToFederatedInformerForTestOnly(daemonsetController.daemonsetFederatedInformer) @@ -152,6 +157,10 @@ func daemonsetsEqual(a, b extensionsv1.DaemonSet) bool { } func GetDaemonSetFromChan(c chan runtime.Object) *extensionsv1.DaemonSet { - daemonset := GetObjectFromChan(c).(*extensionsv1.DaemonSet) - return daemonset + if daemonset := GetObjectFromChan(c); daemonset == nil { + return nil + } else { + return daemonset.(*extensionsv1.DaemonSet) + } + } diff --git a/federation/pkg/federation-controller/deployment/deploymentcontroller_test.go b/federation/pkg/federation-controller/deployment/deploymentcontroller_test.go index 7a857fde9aa..1d72fc8e5db 100644 --- a/federation/pkg/federation-controller/deployment/deploymentcontroller_test.go +++ b/federation/pkg/federation-controller/deployment/deploymentcontroller_test.go @@ -38,6 +38,11 @@ import ( "github.com/stretchr/testify/assert" ) +const ( + deployments = "deployments" + pods = "pods" +) + func TestParseFederationDeploymentPreference(t *testing.T) { successPrefs := []string{ `{"rebalance": true, @@ -87,23 +92,23 @@ func TestDeploymentController(t *testing.T) { fakeClient := &fakefedclientset.Clientset{} // Add an update reactor on fake client to return the desired updated object. // This is a hack to workaround https://github.com/kubernetes/kubernetes/issues/40939. - AddFakeUpdateReactor("deployments", &fakeClient.Fake) + AddFakeUpdateReactor(deployments, &fakeClient.Fake) RegisterFakeList("clusters", &fakeClient.Fake, &fedv1.ClusterList{Items: []fedv1.Cluster{*cluster1}}) - deploymentsWatch := RegisterFakeWatch("deployments", &fakeClient.Fake) + deploymentsWatch := RegisterFakeWatch(deployments, &fakeClient.Fake) clusterWatch := RegisterFakeWatch("clusters", &fakeClient.Fake) cluster1Client := &fakekubeclientset.Clientset{} - cluster1Watch := RegisterFakeWatch("deployments", &cluster1Client.Fake) - _ = RegisterFakeWatch("pods", &cluster1Client.Fake) - RegisterFakeList("deployments", &cluster1Client.Fake, &extensionsv1.DeploymentList{Items: []extensionsv1.Deployment{}}) - cluster1CreateChan := RegisterFakeCopyOnCreate("deployments", &cluster1Client.Fake, cluster1Watch) - cluster1UpdateChan := RegisterFakeCopyOnUpdate("deployments", &cluster1Client.Fake, cluster1Watch) + cluster1Watch := RegisterFakeWatch(deployments, &cluster1Client.Fake) + _ = RegisterFakeWatch(pods, &cluster1Client.Fake) + RegisterFakeList(deployments, &cluster1Client.Fake, &extensionsv1.DeploymentList{Items: []extensionsv1.Deployment{}}) + cluster1CreateChan := RegisterFakeCopyOnCreate(deployments, &cluster1Client.Fake, cluster1Watch) + cluster1UpdateChan := RegisterFakeCopyOnUpdate(deployments, &cluster1Client.Fake, cluster1Watch) cluster2Client := &fakekubeclientset.Clientset{} - cluster2Watch := RegisterFakeWatch("deployments", &cluster2Client.Fake) - _ = RegisterFakeWatch("pods", &cluster2Client.Fake) - RegisterFakeList("deployments", &cluster2Client.Fake, &extensionsv1.DeploymentList{Items: []extensionsv1.Deployment{}}) - cluster2CreateChan := RegisterFakeCopyOnCreate("deployments", &cluster2Client.Fake, cluster2Watch) + cluster2Watch := RegisterFakeWatch(deployments, &cluster2Client.Fake) + _ = RegisterFakeWatch(pods, &cluster2Client.Fake) + RegisterFakeList(deployments, &cluster2Client.Fake, &extensionsv1.DeploymentList{Items: []extensionsv1.Deployment{}}) + cluster2CreateChan := RegisterFakeCopyOnCreate(deployments, &cluster2Client.Fake, cluster2Watch) deploymentController := NewDeploymentController(fakeClient) clientFactory := func(cluster *fedv1.Cluster) (kubeclientset.Interface, error) { @@ -171,11 +176,6 @@ func TestDeploymentController(t *testing.T) { assert.NoError(t, CheckObjectFromChan(cluster2CreateChan, checkDeployment(dep2, 3))) } -func GetDeploymentFromChan(c chan runtime.Object) *extensionsv1.Deployment { - secret := GetObjectFromChan(c).(*extensionsv1.Deployment) - return secret -} - func newDeploymentWithReplicas(name string, replicas int32) *extensionsv1.Deployment { return &extensionsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ diff --git a/federation/pkg/federation-controller/ingress/ingress_controller_test.go b/federation/pkg/federation-controller/ingress/ingress_controller_test.go index 132532a447d..df0145c5138 100644 --- a/federation/pkg/federation-controller/ingress/ingress_controller_test.go +++ b/federation/pkg/federation-controller/ingress/ingress_controller_test.go @@ -43,7 +43,10 @@ import ( ) const ( - maxTrials = 20 + maxTrials = 20 + clusters string = "clusters" + ingresses string = "ingresses" + configmaps string = "configmaps" ) func TestIngressController(t *testing.T) { @@ -57,28 +60,28 @@ func TestIngressController(t *testing.T) { t.Log("Creating fake infrastructure") fedClient := &fakefedclientset.Clientset{} - RegisterFakeList("clusters", &fedClient.Fake, &fakeClusterList) - RegisterFakeList("ingresses", &fedClient.Fake, &extensionsv1beta1.IngressList{Items: []extensionsv1beta1.Ingress{}}) - fedIngressWatch := RegisterFakeWatch("ingresses", &fedClient.Fake) - clusterWatch := RegisterFakeWatch("clusters", &fedClient.Fake) - fedClusterUpdateChan := RegisterFakeCopyOnUpdate("clusters", &fedClient.Fake, clusterWatch) - fedIngressUpdateChan := RegisterFakeCopyOnUpdate("ingresses", &fedClient.Fake, fedIngressWatch) + RegisterFakeList(clusters, &fedClient.Fake, &fakeClusterList) + RegisterFakeList(ingresses, &fedClient.Fake, &extensionsv1beta1.IngressList{Items: []extensionsv1beta1.Ingress{}}) + fedIngressWatch := RegisterFakeWatch(ingresses, &fedClient.Fake) + clusterWatch := RegisterFakeWatch(clusters, &fedClient.Fake) + fedClusterUpdateChan := RegisterFakeCopyOnUpdate(clusters, &fedClient.Fake, clusterWatch) + fedIngressUpdateChan := RegisterFakeCopyOnUpdate(ingresses, &fedClient.Fake, fedIngressWatch) cluster1Client := &fakekubeclientset.Clientset{} - RegisterFakeList("ingresses", &cluster1Client.Fake, &extensionsv1beta1.IngressList{Items: []extensionsv1beta1.Ingress{}}) - RegisterFakeList("configmaps", &cluster1Client.Fake, &fakeConfigMapList1) - cluster1IngressWatch := RegisterFakeWatch("ingresses", &cluster1Client.Fake) - cluster1ConfigMapWatch := RegisterFakeWatch("configmaps", &cluster1Client.Fake) - cluster1IngressCreateChan := RegisterFakeCopyOnCreate("ingresses", &cluster1Client.Fake, cluster1IngressWatch) - cluster1IngressUpdateChan := RegisterFakeCopyOnUpdate("ingresses", &cluster1Client.Fake, cluster1IngressWatch) + RegisterFakeList(ingresses, &cluster1Client.Fake, &extensionsv1beta1.IngressList{Items: []extensionsv1beta1.Ingress{}}) + RegisterFakeList(configmaps, &cluster1Client.Fake, &fakeConfigMapList1) + cluster1IngressWatch := RegisterFakeWatch(ingresses, &cluster1Client.Fake) + cluster1ConfigMapWatch := RegisterFakeWatch(configmaps, &cluster1Client.Fake) + cluster1IngressCreateChan := RegisterFakeCopyOnCreate(ingresses, &cluster1Client.Fake, cluster1IngressWatch) + cluster1IngressUpdateChan := RegisterFakeCopyOnUpdate(ingresses, &cluster1Client.Fake, cluster1IngressWatch) cluster2Client := &fakekubeclientset.Clientset{} - RegisterFakeList("ingresses", &cluster2Client.Fake, &extensionsv1beta1.IngressList{Items: []extensionsv1beta1.Ingress{}}) - RegisterFakeList("configmaps", &cluster2Client.Fake, &fakeConfigMapList2) - cluster2IngressWatch := RegisterFakeWatch("ingresses", &cluster2Client.Fake) - cluster2ConfigMapWatch := RegisterFakeWatch("configmaps", &cluster2Client.Fake) - cluster2IngressCreateChan := RegisterFakeCopyOnCreate("ingresses", &cluster2Client.Fake, cluster2IngressWatch) - cluster2ConfigMapUpdateChan := RegisterFakeCopyOnUpdate("configmaps", &cluster2Client.Fake, cluster2ConfigMapWatch) + RegisterFakeList(ingresses, &cluster2Client.Fake, &extensionsv1beta1.IngressList{Items: []extensionsv1beta1.Ingress{}}) + RegisterFakeList(configmaps, &cluster2Client.Fake, &fakeConfigMapList2) + cluster2IngressWatch := RegisterFakeWatch(ingresses, &cluster2Client.Fake) + cluster2ConfigMapWatch := RegisterFakeWatch(configmaps, &cluster2Client.Fake) + cluster2IngressCreateChan := RegisterFakeCopyOnCreate(ingresses, &cluster2Client.Fake, cluster2IngressWatch) + cluster2ConfigMapUpdateChan := RegisterFakeCopyOnUpdate(configmaps, &cluster2Client.Fake, cluster2ConfigMapWatch) clientFactoryFunc := func(cluster *federationapi.Cluster) (kubeclientset.Interface, error) { switch cluster.Name { @@ -263,6 +266,11 @@ func TestIngressController(t *testing.T) { func GetIngressFromChan(t *testing.T, c chan runtime.Object) *extensionsv1beta1.Ingress { obj := GetObjectFromChan(c) + + if obj == nil { + return nil + } + ingress, ok := obj.(*extensionsv1beta1.Ingress) if !ok { t.Logf("Object on channel was not of type *extensionsv1beta1.Ingress: %v", obj) @@ -271,13 +279,20 @@ func GetIngressFromChan(t *testing.T, c chan runtime.Object) *extensionsv1beta1. } func GetConfigMapFromChan(c chan runtime.Object) *apiv1.ConfigMap { - configMap, _ := GetObjectFromChan(c).(*apiv1.ConfigMap) - return configMap + if configMap := GetObjectFromChan(c); configMap == nil { + return nil + } else { + return configMap.(*apiv1.ConfigMap) + } + } func GetClusterFromChan(c chan runtime.Object) *federationapi.Cluster { - cluster, _ := GetObjectFromChan(c).(*federationapi.Cluster) - return cluster + if cluster := GetObjectFromChan(c); cluster == nil { + return nil + } else { + return cluster.(*federationapi.Cluster) + } } func NewConfigMap(uid string) *apiv1.ConfigMap { diff --git a/federation/pkg/federation-controller/namespace/namespace_controller_test.go b/federation/pkg/federation-controller/namespace/namespace_controller_test.go index 7a24a98bf7b..fe022c1a9c6 100644 --- a/federation/pkg/federation-controller/namespace/namespace_controller_test.go +++ b/federation/pkg/federation-controller/namespace/namespace_controller_test.go @@ -39,6 +39,11 @@ import ( "github.com/stretchr/testify/assert" ) +const ( + namespaces string = "namespaces" + clusters string = "clusters" +) + func TestNamespaceController(t *testing.T) { cluster1 := NewCluster("cluster1", apiv1.ConditionTrue) cluster2 := NewCluster("cluster2", apiv1.ConditionTrue) @@ -53,24 +58,24 @@ func TestNamespaceController(t *testing.T) { } fakeClient := &fakefedclientset.Clientset{} - RegisterFakeList("clusters", &fakeClient.Fake, &federationapi.ClusterList{Items: []federationapi.Cluster{*cluster1}}) - RegisterFakeList("namespaces", &fakeClient.Fake, &apiv1.NamespaceList{Items: []apiv1.Namespace{}}) - namespaceWatch := RegisterFakeWatch("namespaces", &fakeClient.Fake) - namespaceCreateChan := RegisterFakeCopyOnCreate("namespaces", &fakeClient.Fake, namespaceWatch) - clusterWatch := RegisterFakeWatch("clusters", &fakeClient.Fake) + RegisterFakeList(clusters, &fakeClient.Fake, &federationapi.ClusterList{Items: []federationapi.Cluster{*cluster1}}) + RegisterFakeList(namespaces, &fakeClient.Fake, &apiv1.NamespaceList{Items: []apiv1.Namespace{}}) + namespaceWatch := RegisterFakeWatch(namespaces, &fakeClient.Fake) + namespaceCreateChan := RegisterFakeCopyOnCreate(namespaces, &fakeClient.Fake, namespaceWatch) + clusterWatch := RegisterFakeWatch(clusters, &fakeClient.Fake) cluster1Client := &fakekubeclientset.Clientset{} - cluster1Watch := RegisterFakeWatch("namespaces", &cluster1Client.Fake) - RegisterFakeList("namespaces", &cluster1Client.Fake, &apiv1.NamespaceList{Items: []apiv1.Namespace{}}) - cluster1CreateChan := RegisterFakeCopyOnCreate("namespaces", &cluster1Client.Fake, cluster1Watch) - cluster1UpdateChan := RegisterFakeCopyOnUpdate("namespaces", &cluster1Client.Fake, cluster1Watch) + cluster1Watch := RegisterFakeWatch(namespaces, &cluster1Client.Fake) + RegisterFakeList(namespaces, &cluster1Client.Fake, &apiv1.NamespaceList{Items: []apiv1.Namespace{}}) + cluster1CreateChan := RegisterFakeCopyOnCreate(namespaces, &cluster1Client.Fake, cluster1Watch) + cluster1UpdateChan := RegisterFakeCopyOnUpdate(namespaces, &cluster1Client.Fake, cluster1Watch) cluster2Client := &fakekubeclientset.Clientset{} - cluster2Watch := RegisterFakeWatch("namespaces", &cluster2Client.Fake) - RegisterFakeList("namespaces", &cluster2Client.Fake, &apiv1.NamespaceList{Items: []apiv1.Namespace{}}) - cluster2CreateChan := RegisterFakeCopyOnCreate("namespaces", &cluster2Client.Fake, cluster2Watch) + cluster2Watch := RegisterFakeWatch(namespaces, &cluster2Client.Fake) + RegisterFakeList(namespaces, &cluster2Client.Fake, &apiv1.NamespaceList{Items: []apiv1.Namespace{}}) + cluster2CreateChan := RegisterFakeCopyOnCreate(namespaces, &cluster2Client.Fake, cluster2Watch) - nsDeleteChan := RegisterDelete(&fakeClient.Fake, "namespaces") + nsDeleteChan := RegisterDelete(&fakeClient.Fake, namespaces) namespaceController := NewNamespaceController(fakeClient, dynamic.NewDynamicClientPool(&restclient.Config{})) informerClientFactory := func(cluster *federationapi.Cluster) (kubeclientset.Interface, error) { switch cluster.Name { @@ -175,7 +180,9 @@ func GetStringFromChan(c chan string) string { } func GetNamespaceFromChan(c chan runtime.Object) *apiv1.Namespace { - namespace := GetObjectFromChan(c).(*apiv1.Namespace) - return namespace - + if namespace := GetObjectFromChan(c); namespace == nil { + return nil + } else { + return namespace.(*apiv1.Namespace) + } } diff --git a/federation/pkg/federation-controller/replicaset/replicasetcontroller_test.go b/federation/pkg/federation-controller/replicaset/replicasetcontroller_test.go index a8539ccaa4b..732b4a8007c 100644 --- a/federation/pkg/federation-controller/replicaset/replicasetcontroller_test.go +++ b/federation/pkg/federation-controller/replicaset/replicasetcontroller_test.go @@ -37,6 +37,13 @@ import ( "github.com/stretchr/testify/assert" ) +const ( + pods = "pods" + replicasets = "replicasets" + k8s1 = "k8s-1" + k8s2 = "k8s-2" +) + func TestParseFederationReplicaSetReference(t *testing.T) { successPrefs := []string{ `{"rebalance": true, @@ -82,27 +89,27 @@ func TestReplicaSetController(t *testing.T) { fedclientset := fedclientfake.NewSimpleClientset() fedrswatch := watch.NewFake() - fedclientset.PrependWatchReactor("replicasets", core.DefaultWatchReactor(fedrswatch, nil)) + fedclientset.PrependWatchReactor(replicasets, core.DefaultWatchReactor(fedrswatch, nil)) - fedclientset.Federation().Clusters().Create(testutil.NewCluster("k8s-1", apiv1.ConditionTrue)) - fedclientset.Federation().Clusters().Create(testutil.NewCluster("k8s-2", apiv1.ConditionTrue)) + fedclientset.Federation().Clusters().Create(testutil.NewCluster(k8s1, apiv1.ConditionTrue)) + fedclientset.Federation().Clusters().Create(testutil.NewCluster(k8s2, apiv1.ConditionTrue)) kube1clientset := kubeclientfake.NewSimpleClientset() kube1rswatch := watch.NewFake() - kube1clientset.PrependWatchReactor("replicasets", core.DefaultWatchReactor(kube1rswatch, nil)) + kube1clientset.PrependWatchReactor(replicasets, core.DefaultWatchReactor(kube1rswatch, nil)) kube1Podwatch := watch.NewFake() - kube1clientset.PrependWatchReactor("pods", core.DefaultWatchReactor(kube1Podwatch, nil)) + kube1clientset.PrependWatchReactor(pods, core.DefaultWatchReactor(kube1Podwatch, nil)) kube2clientset := kubeclientfake.NewSimpleClientset() kube2rswatch := watch.NewFake() - kube2clientset.PrependWatchReactor("replicasets", core.DefaultWatchReactor(kube2rswatch, nil)) + kube2clientset.PrependWatchReactor(replicasets, core.DefaultWatchReactor(kube2rswatch, nil)) kube2Podwatch := watch.NewFake() - kube2clientset.PrependWatchReactor("pods", core.DefaultWatchReactor(kube2Podwatch, nil)) + kube2clientset.PrependWatchReactor(pods, core.DefaultWatchReactor(kube2Podwatch, nil)) fedInformerClientFactory := func(cluster *fedv1.Cluster) (kubeclientset.Interface, error) { switch cluster.Name { - case "k8s-1": + case k8s1: return kube1clientset, nil - case "k8s-2": + case k8s2: return kube2clientset, nil default: return nil, fmt.Errorf("Unknown cluster: %v", cluster.Name) diff --git a/federation/pkg/federation-controller/secret/secret_controller_test.go b/federation/pkg/federation-controller/secret/secret_controller_test.go index 24b790dad2e..5532a8025c8 100644 --- a/federation/pkg/federation-controller/secret/secret_controller_test.go +++ b/federation/pkg/federation-controller/secret/secret_controller_test.go @@ -39,27 +39,32 @@ import ( "github.com/stretchr/testify/assert" ) +const ( + clusters string = "clusters" + secrets string = "secrets" +) + func TestSecretController(t *testing.T) { cluster1 := NewCluster("cluster1", apiv1.ConditionTrue) cluster2 := NewCluster("cluster2", apiv1.ConditionTrue) fakeClient := &fakefedclientset.Clientset{} - RegisterFakeList("clusters", &fakeClient.Fake, &federationapi.ClusterList{Items: []federationapi.Cluster{*cluster1}}) - RegisterFakeList("secrets", &fakeClient.Fake, &apiv1.SecretList{Items: []apiv1.Secret{}}) - secretWatch := RegisterFakeWatch("secrets", &fakeClient.Fake) - secretUpdateChan := RegisterFakeCopyOnUpdate("secrets", &fakeClient.Fake, secretWatch) - clusterWatch := RegisterFakeWatch("clusters", &fakeClient.Fake) + RegisterFakeList(clusters, &fakeClient.Fake, &federationapi.ClusterList{Items: []federationapi.Cluster{*cluster1}}) + RegisterFakeList(secrets, &fakeClient.Fake, &apiv1.SecretList{Items: []apiv1.Secret{}}) + secretWatch := RegisterFakeWatch(secrets, &fakeClient.Fake) + secretUpdateChan := RegisterFakeCopyOnUpdate(secrets, &fakeClient.Fake, secretWatch) + clusterWatch := RegisterFakeWatch(clusters, &fakeClient.Fake) cluster1Client := &fakekubeclientset.Clientset{} - cluster1Watch := RegisterFakeWatch("secrets", &cluster1Client.Fake) - RegisterFakeList("secrets", &cluster1Client.Fake, &apiv1.SecretList{Items: []apiv1.Secret{}}) - cluster1CreateChan := RegisterFakeCopyOnCreate("secrets", &cluster1Client.Fake, cluster1Watch) - cluster1UpdateChan := RegisterFakeCopyOnUpdate("secrets", &cluster1Client.Fake, cluster1Watch) + cluster1Watch := RegisterFakeWatch(secrets, &cluster1Client.Fake) + RegisterFakeList(secrets, &cluster1Client.Fake, &apiv1.SecretList{Items: []apiv1.Secret{}}) + cluster1CreateChan := RegisterFakeCopyOnCreate(secrets, &cluster1Client.Fake, cluster1Watch) + cluster1UpdateChan := RegisterFakeCopyOnUpdate(secrets, &cluster1Client.Fake, cluster1Watch) cluster2Client := &fakekubeclientset.Clientset{} - cluster2Watch := RegisterFakeWatch("secrets", &cluster2Client.Fake) - RegisterFakeList("secrets", &cluster2Client.Fake, &apiv1.SecretList{Items: []apiv1.Secret{}}) - cluster2CreateChan := RegisterFakeCopyOnCreate("secrets", &cluster2Client.Fake, cluster2Watch) + cluster2Watch := RegisterFakeWatch(secrets, &cluster2Client.Fake) + RegisterFakeList(secrets, &cluster2Client.Fake, &apiv1.SecretList{Items: []apiv1.Secret{}}) + cluster2CreateChan := RegisterFakeCopyOnCreate(secrets, &cluster2Client.Fake, cluster2Watch) secretController := NewSecretController(fakeClient) informerClientFactory := func(cluster *federationapi.Cluster) (kubeclientset.Interface, error) { @@ -185,8 +190,11 @@ func secretsEqual(a, b apiv1.Secret) bool { } func GetSecretFromChan(c chan runtime.Object) *apiv1.Secret { - secret := GetObjectFromChan(c).(*apiv1.Secret) - return secret + if secret := GetObjectFromChan(c); secret == nil { + return nil + } else { + return secret.(*apiv1.Secret) + } } // Wait till the store is updated with latest secret.