moved multiple duplicate strings to constants, updated test that when flacking paniced instead of just failing.

updated tests to avoid panic if channel returns nil.

updated tests based on PR recs.
This commit is contained in:
Alejandro Escobar
2017-02-16 07:09:36 -08:00
parent 17375fc59f
commit a56a694be9
7 changed files with 162 additions and 107 deletions

View File

@@ -38,27 +38,33 @@ import (
"github.com/stretchr/testify/assert" "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) { func TestConfigMapController(t *testing.T) {
cluster1 := NewCluster("cluster1", apiv1.ConditionTrue) cluster1 := NewCluster("cluster1", apiv1.ConditionTrue)
cluster2 := NewCluster("cluster2", apiv1.ConditionTrue) cluster2 := NewCluster("cluster2", apiv1.ConditionTrue)
fakeClient := &fakefedclientset.Clientset{} fakeClient := &fakefedclientset.Clientset{}
RegisterFakeList("clusters", &fakeClient.Fake, &federationapi.ClusterList{Items: []federationapi.Cluster{*cluster1}}) RegisterFakeList(clusters, &fakeClient.Fake, &federationapi.ClusterList{Items: []federationapi.Cluster{*cluster1}})
RegisterFakeList("configmaps", &fakeClient.Fake, &apiv1.ConfigMapList{Items: []apiv1.ConfigMap{}}) RegisterFakeList(configmaps, &fakeClient.Fake, &apiv1.ConfigMapList{Items: []apiv1.ConfigMap{}})
configmapWatch := RegisterFakeWatch("configmaps", &fakeClient.Fake) configmapWatch := RegisterFakeWatch(configmaps, &fakeClient.Fake)
configmapUpdateChan := RegisterFakeCopyOnUpdate("configmaps", &fakeClient.Fake, configmapWatch) configmapUpdateChan := RegisterFakeCopyOnUpdate(configmaps, &fakeClient.Fake, configmapWatch)
clusterWatch := RegisterFakeWatch("clusters", &fakeClient.Fake) clusterWatch := RegisterFakeWatch(clusters, &fakeClient.Fake)
cluster1Client := &fakekubeclientset.Clientset{} cluster1Client := &fakekubeclientset.Clientset{}
cluster1Watch := RegisterFakeWatch("configmaps", &cluster1Client.Fake) cluster1Watch := RegisterFakeWatch(configmaps, &cluster1Client.Fake)
RegisterFakeList("configmaps", &cluster1Client.Fake, &apiv1.ConfigMapList{Items: []apiv1.ConfigMap{}}) RegisterFakeList(configmaps, &cluster1Client.Fake, &apiv1.ConfigMapList{Items: []apiv1.ConfigMap{}})
cluster1CreateChan := RegisterFakeCopyOnCreate("configmaps", &cluster1Client.Fake, cluster1Watch) cluster1CreateChan := RegisterFakeCopyOnCreate(configmaps, &cluster1Client.Fake, cluster1Watch)
cluster1UpdateChan := RegisterFakeCopyOnUpdate("configmaps", &cluster1Client.Fake, cluster1Watch) cluster1UpdateChan := RegisterFakeCopyOnUpdate(configmaps, &cluster1Client.Fake, cluster1Watch)
cluster2Client := &fakekubeclientset.Clientset{} cluster2Client := &fakekubeclientset.Clientset{}
cluster2Watch := RegisterFakeWatch("configmaps", &cluster2Client.Fake) cluster2Watch := RegisterFakeWatch(configmaps, &cluster2Client.Fake)
RegisterFakeList("configmaps", &cluster2Client.Fake, &apiv1.ConfigMapList{Items: []apiv1.ConfigMap{}}) RegisterFakeList(configmaps, &cluster2Client.Fake, &apiv1.ConfigMapList{Items: []apiv1.ConfigMap{}})
cluster2CreateChan := RegisterFakeCopyOnCreate("configmaps", &cluster2Client.Fake, cluster2Watch) cluster2CreateChan := RegisterFakeCopyOnCreate(configmaps, &cluster2Client.Fake, cluster2Watch)
configmapController := NewConfigMapController(fakeClient) configmapController := NewConfigMapController(fakeClient)
informer := ToFederatedInformerForTestOnly(configmapController.configmapFederatedInformer) informer := ToFederatedInformerForTestOnly(configmapController.configmapFederatedInformer)
@@ -111,7 +117,7 @@ func TestConfigMapController(t *testing.T) {
err := WaitForStoreUpdate( err := WaitForStoreUpdate(
configmapController.configmapFederatedInformer.GetTargetStore(), configmapController.configmapFederatedInformer.GetTargetStore(),
cluster1.Name, types.NamespacedName{Namespace: configmap1.Namespace, Name: configmap1.Name}.String(), wait.ForeverTestTimeout) 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. // Test update federated configmap.
configmap1.Annotations = map[string]string{ configmap1.Annotations = map[string]string{
@@ -129,7 +135,7 @@ func TestConfigMapController(t *testing.T) {
configmapController.configmapFederatedInformer.GetTargetStore(), configmapController.configmapFederatedInformer.GetTargetStore(),
cluster1.Name, types.NamespacedName{Namespace: configmap1.Namespace, Name: configmap1.Name}.String(), cluster1.Name, types.NamespacedName{Namespace: configmap1.Namespace, Name: configmap1.Name}.String(),
configmap1, wait.ForeverTestTimeout) configmap1, wait.ForeverTestTimeout)
assert.Nil(t, err, "configmap should have appeared in the informer store") assert.Nil(t, err, informerStoreErr)
// Test update federated configmap. // Test update federated configmap.
configmap1.Data = map[string]string{ configmap1.Data = map[string]string{
@@ -155,8 +161,11 @@ func TestConfigMapController(t *testing.T) {
} }
func GetConfigMapFromChan(c chan runtime.Object) *apiv1.ConfigMap { func GetConfigMapFromChan(c chan runtime.Object) *apiv1.ConfigMap {
configmap := GetObjectFromChan(c).(*apiv1.ConfigMap) if configmap := GetObjectFromChan(c); configmap == nil {
return configmap return nil
} else {
return configmap.(*apiv1.ConfigMap)
}
} }
// Wait till the store is updated with latest configmap. // Wait till the store is updated with latest configmap.

View File

@@ -38,27 +38,32 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
const (
daemonsets string = "daemonsets"
clusters string = "clusters"
)
func TestDaemonSetController(t *testing.T) { func TestDaemonSetController(t *testing.T) {
cluster1 := NewCluster("cluster1", apiv1.ConditionTrue) cluster1 := NewCluster("cluster1", apiv1.ConditionTrue)
cluster2 := NewCluster("cluster2", apiv1.ConditionTrue) cluster2 := NewCluster("cluster2", apiv1.ConditionTrue)
fakeClient := &fakefedclientset.Clientset{} fakeClient := &fakefedclientset.Clientset{}
RegisterFakeList("clusters", &fakeClient.Fake, &federationapi.ClusterList{Items: []federationapi.Cluster{*cluster1}}) RegisterFakeList(clusters, &fakeClient.Fake, &federationapi.ClusterList{Items: []federationapi.Cluster{*cluster1}})
RegisterFakeList("daemonsets", &fakeClient.Fake, &extensionsv1.DaemonSetList{Items: []extensionsv1.DaemonSet{}}) RegisterFakeList(daemonsets, &fakeClient.Fake, &extensionsv1.DaemonSetList{Items: []extensionsv1.DaemonSet{}})
daemonsetWatch := RegisterFakeWatch("daemonsets", &fakeClient.Fake) daemonsetWatch := RegisterFakeWatch(daemonsets, &fakeClient.Fake)
daemonsetUpdateChan := RegisterFakeCopyOnUpdate("daemonsets", &fakeClient.Fake, daemonsetWatch) daemonsetUpdateChan := RegisterFakeCopyOnUpdate(daemonsets, &fakeClient.Fake, daemonsetWatch)
clusterWatch := RegisterFakeWatch("clusters", &fakeClient.Fake) clusterWatch := RegisterFakeWatch("clusters", &fakeClient.Fake)
cluster1Client := &fakekubeclientset.Clientset{} cluster1Client := &fakekubeclientset.Clientset{}
cluster1Watch := RegisterFakeWatch("daemonsets", &cluster1Client.Fake) cluster1Watch := RegisterFakeWatch(daemonsets, &cluster1Client.Fake)
RegisterFakeList("daemonsets", &cluster1Client.Fake, &extensionsv1.DaemonSetList{Items: []extensionsv1.DaemonSet{}}) RegisterFakeList(daemonsets, &cluster1Client.Fake, &extensionsv1.DaemonSetList{Items: []extensionsv1.DaemonSet{}})
cluster1CreateChan := RegisterFakeCopyOnCreate("daemonsets", &cluster1Client.Fake, cluster1Watch) cluster1CreateChan := RegisterFakeCopyOnCreate(daemonsets, &cluster1Client.Fake, cluster1Watch)
cluster1UpdateChan := RegisterFakeCopyOnUpdate("daemonsets", &cluster1Client.Fake, cluster1Watch) cluster1UpdateChan := RegisterFakeCopyOnUpdate(daemonsets, &cluster1Client.Fake, cluster1Watch)
cluster2Client := &fakekubeclientset.Clientset{} cluster2Client := &fakekubeclientset.Clientset{}
cluster2Watch := RegisterFakeWatch("daemonsets", &cluster2Client.Fake) cluster2Watch := RegisterFakeWatch(daemonsets, &cluster2Client.Fake)
RegisterFakeList("daemonsets", &cluster2Client.Fake, &extensionsv1.DaemonSetList{Items: []extensionsv1.DaemonSet{}}) RegisterFakeList(daemonsets, &cluster2Client.Fake, &extensionsv1.DaemonSetList{Items: []extensionsv1.DaemonSet{}})
cluster2CreateChan := RegisterFakeCopyOnCreate("daemonsets", &cluster2Client.Fake, cluster2Watch) cluster2CreateChan := RegisterFakeCopyOnCreate(daemonsets, &cluster2Client.Fake, cluster2Watch)
daemonsetController := NewDaemonSetController(fakeClient) daemonsetController := NewDaemonSetController(fakeClient)
informer := ToFederatedInformerForTestOnly(daemonsetController.daemonsetFederatedInformer) informer := ToFederatedInformerForTestOnly(daemonsetController.daemonsetFederatedInformer)
@@ -152,6 +157,10 @@ func daemonsetsEqual(a, b extensionsv1.DaemonSet) bool {
} }
func GetDaemonSetFromChan(c chan runtime.Object) *extensionsv1.DaemonSet { func GetDaemonSetFromChan(c chan runtime.Object) *extensionsv1.DaemonSet {
daemonset := GetObjectFromChan(c).(*extensionsv1.DaemonSet) if daemonset := GetObjectFromChan(c); daemonset == nil {
return daemonset return nil
} else {
return daemonset.(*extensionsv1.DaemonSet)
}
} }

View File

@@ -38,6 +38,11 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
const (
deployments = "deployments"
pods = "pods"
)
func TestParseFederationDeploymentPreference(t *testing.T) { func TestParseFederationDeploymentPreference(t *testing.T) {
successPrefs := []string{ successPrefs := []string{
`{"rebalance": true, `{"rebalance": true,
@@ -87,23 +92,23 @@ func TestDeploymentController(t *testing.T) {
fakeClient := &fakefedclientset.Clientset{} fakeClient := &fakefedclientset.Clientset{}
// Add an update reactor on fake client to return the desired updated object. // 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. // 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}}) 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) clusterWatch := RegisterFakeWatch("clusters", &fakeClient.Fake)
cluster1Client := &fakekubeclientset.Clientset{} cluster1Client := &fakekubeclientset.Clientset{}
cluster1Watch := RegisterFakeWatch("deployments", &cluster1Client.Fake) cluster1Watch := RegisterFakeWatch(deployments, &cluster1Client.Fake)
_ = RegisterFakeWatch("pods", &cluster1Client.Fake) _ = RegisterFakeWatch(pods, &cluster1Client.Fake)
RegisterFakeList("deployments", &cluster1Client.Fake, &extensionsv1.DeploymentList{Items: []extensionsv1.Deployment{}}) RegisterFakeList(deployments, &cluster1Client.Fake, &extensionsv1.DeploymentList{Items: []extensionsv1.Deployment{}})
cluster1CreateChan := RegisterFakeCopyOnCreate("deployments", &cluster1Client.Fake, cluster1Watch) cluster1CreateChan := RegisterFakeCopyOnCreate(deployments, &cluster1Client.Fake, cluster1Watch)
cluster1UpdateChan := RegisterFakeCopyOnUpdate("deployments", &cluster1Client.Fake, cluster1Watch) cluster1UpdateChan := RegisterFakeCopyOnUpdate(deployments, &cluster1Client.Fake, cluster1Watch)
cluster2Client := &fakekubeclientset.Clientset{} cluster2Client := &fakekubeclientset.Clientset{}
cluster2Watch := RegisterFakeWatch("deployments", &cluster2Client.Fake) cluster2Watch := RegisterFakeWatch(deployments, &cluster2Client.Fake)
_ = RegisterFakeWatch("pods", &cluster2Client.Fake) _ = RegisterFakeWatch(pods, &cluster2Client.Fake)
RegisterFakeList("deployments", &cluster2Client.Fake, &extensionsv1.DeploymentList{Items: []extensionsv1.Deployment{}}) RegisterFakeList(deployments, &cluster2Client.Fake, &extensionsv1.DeploymentList{Items: []extensionsv1.Deployment{}})
cluster2CreateChan := RegisterFakeCopyOnCreate("deployments", &cluster2Client.Fake, cluster2Watch) cluster2CreateChan := RegisterFakeCopyOnCreate(deployments, &cluster2Client.Fake, cluster2Watch)
deploymentController := NewDeploymentController(fakeClient) deploymentController := NewDeploymentController(fakeClient)
clientFactory := func(cluster *fedv1.Cluster) (kubeclientset.Interface, error) { 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))) 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 { func newDeploymentWithReplicas(name string, replicas int32) *extensionsv1.Deployment {
return &extensionsv1.Deployment{ return &extensionsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{

View File

@@ -43,7 +43,10 @@ import (
) )
const ( const (
maxTrials = 20 maxTrials = 20
clusters string = "clusters"
ingresses string = "ingresses"
configmaps string = "configmaps"
) )
func TestIngressController(t *testing.T) { func TestIngressController(t *testing.T) {
@@ -57,28 +60,28 @@ func TestIngressController(t *testing.T) {
t.Log("Creating fake infrastructure") t.Log("Creating fake infrastructure")
fedClient := &fakefedclientset.Clientset{} fedClient := &fakefedclientset.Clientset{}
RegisterFakeList("clusters", &fedClient.Fake, &fakeClusterList) RegisterFakeList(clusters, &fedClient.Fake, &fakeClusterList)
RegisterFakeList("ingresses", &fedClient.Fake, &extensionsv1beta1.IngressList{Items: []extensionsv1beta1.Ingress{}}) RegisterFakeList(ingresses, &fedClient.Fake, &extensionsv1beta1.IngressList{Items: []extensionsv1beta1.Ingress{}})
fedIngressWatch := RegisterFakeWatch("ingresses", &fedClient.Fake) fedIngressWatch := RegisterFakeWatch(ingresses, &fedClient.Fake)
clusterWatch := RegisterFakeWatch("clusters", &fedClient.Fake) clusterWatch := RegisterFakeWatch(clusters, &fedClient.Fake)
fedClusterUpdateChan := RegisterFakeCopyOnUpdate("clusters", &fedClient.Fake, clusterWatch) fedClusterUpdateChan := RegisterFakeCopyOnUpdate(clusters, &fedClient.Fake, clusterWatch)
fedIngressUpdateChan := RegisterFakeCopyOnUpdate("ingresses", &fedClient.Fake, fedIngressWatch) fedIngressUpdateChan := RegisterFakeCopyOnUpdate(ingresses, &fedClient.Fake, fedIngressWatch)
cluster1Client := &fakekubeclientset.Clientset{} cluster1Client := &fakekubeclientset.Clientset{}
RegisterFakeList("ingresses", &cluster1Client.Fake, &extensionsv1beta1.IngressList{Items: []extensionsv1beta1.Ingress{}}) RegisterFakeList(ingresses, &cluster1Client.Fake, &extensionsv1beta1.IngressList{Items: []extensionsv1beta1.Ingress{}})
RegisterFakeList("configmaps", &cluster1Client.Fake, &fakeConfigMapList1) RegisterFakeList(configmaps, &cluster1Client.Fake, &fakeConfigMapList1)
cluster1IngressWatch := RegisterFakeWatch("ingresses", &cluster1Client.Fake) cluster1IngressWatch := RegisterFakeWatch(ingresses, &cluster1Client.Fake)
cluster1ConfigMapWatch := RegisterFakeWatch("configmaps", &cluster1Client.Fake) cluster1ConfigMapWatch := RegisterFakeWatch(configmaps, &cluster1Client.Fake)
cluster1IngressCreateChan := RegisterFakeCopyOnCreate("ingresses", &cluster1Client.Fake, cluster1IngressWatch) cluster1IngressCreateChan := RegisterFakeCopyOnCreate(ingresses, &cluster1Client.Fake, cluster1IngressWatch)
cluster1IngressUpdateChan := RegisterFakeCopyOnUpdate("ingresses", &cluster1Client.Fake, cluster1IngressWatch) cluster1IngressUpdateChan := RegisterFakeCopyOnUpdate(ingresses, &cluster1Client.Fake, cluster1IngressWatch)
cluster2Client := &fakekubeclientset.Clientset{} cluster2Client := &fakekubeclientset.Clientset{}
RegisterFakeList("ingresses", &cluster2Client.Fake, &extensionsv1beta1.IngressList{Items: []extensionsv1beta1.Ingress{}}) RegisterFakeList(ingresses, &cluster2Client.Fake, &extensionsv1beta1.IngressList{Items: []extensionsv1beta1.Ingress{}})
RegisterFakeList("configmaps", &cluster2Client.Fake, &fakeConfigMapList2) RegisterFakeList(configmaps, &cluster2Client.Fake, &fakeConfigMapList2)
cluster2IngressWatch := RegisterFakeWatch("ingresses", &cluster2Client.Fake) cluster2IngressWatch := RegisterFakeWatch(ingresses, &cluster2Client.Fake)
cluster2ConfigMapWatch := RegisterFakeWatch("configmaps", &cluster2Client.Fake) cluster2ConfigMapWatch := RegisterFakeWatch(configmaps, &cluster2Client.Fake)
cluster2IngressCreateChan := RegisterFakeCopyOnCreate("ingresses", &cluster2Client.Fake, cluster2IngressWatch) cluster2IngressCreateChan := RegisterFakeCopyOnCreate(ingresses, &cluster2Client.Fake, cluster2IngressWatch)
cluster2ConfigMapUpdateChan := RegisterFakeCopyOnUpdate("configmaps", &cluster2Client.Fake, cluster2ConfigMapWatch) cluster2ConfigMapUpdateChan := RegisterFakeCopyOnUpdate(configmaps, &cluster2Client.Fake, cluster2ConfigMapWatch)
clientFactoryFunc := func(cluster *federationapi.Cluster) (kubeclientset.Interface, error) { clientFactoryFunc := func(cluster *federationapi.Cluster) (kubeclientset.Interface, error) {
switch cluster.Name { switch cluster.Name {
@@ -263,6 +266,11 @@ func TestIngressController(t *testing.T) {
func GetIngressFromChan(t *testing.T, c chan runtime.Object) *extensionsv1beta1.Ingress { func GetIngressFromChan(t *testing.T, c chan runtime.Object) *extensionsv1beta1.Ingress {
obj := GetObjectFromChan(c) obj := GetObjectFromChan(c)
if obj == nil {
return nil
}
ingress, ok := obj.(*extensionsv1beta1.Ingress) ingress, ok := obj.(*extensionsv1beta1.Ingress)
if !ok { if !ok {
t.Logf("Object on channel was not of type *extensionsv1beta1.Ingress: %v", obj) 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 { func GetConfigMapFromChan(c chan runtime.Object) *apiv1.ConfigMap {
configMap, _ := GetObjectFromChan(c).(*apiv1.ConfigMap) if configMap := GetObjectFromChan(c); configMap == nil {
return configMap return nil
} else {
return configMap.(*apiv1.ConfigMap)
}
} }
func GetClusterFromChan(c chan runtime.Object) *federationapi.Cluster { func GetClusterFromChan(c chan runtime.Object) *federationapi.Cluster {
cluster, _ := GetObjectFromChan(c).(*federationapi.Cluster) if cluster := GetObjectFromChan(c); cluster == nil {
return cluster return nil
} else {
return cluster.(*federationapi.Cluster)
}
} }
func NewConfigMap(uid string) *apiv1.ConfigMap { func NewConfigMap(uid string) *apiv1.ConfigMap {

View File

@@ -39,6 +39,11 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
const (
namespaces string = "namespaces"
clusters string = "clusters"
)
func TestNamespaceController(t *testing.T) { func TestNamespaceController(t *testing.T) {
cluster1 := NewCluster("cluster1", apiv1.ConditionTrue) cluster1 := NewCluster("cluster1", apiv1.ConditionTrue)
cluster2 := NewCluster("cluster2", apiv1.ConditionTrue) cluster2 := NewCluster("cluster2", apiv1.ConditionTrue)
@@ -53,24 +58,24 @@ func TestNamespaceController(t *testing.T) {
} }
fakeClient := &fakefedclientset.Clientset{} fakeClient := &fakefedclientset.Clientset{}
RegisterFakeList("clusters", &fakeClient.Fake, &federationapi.ClusterList{Items: []federationapi.Cluster{*cluster1}}) RegisterFakeList(clusters, &fakeClient.Fake, &federationapi.ClusterList{Items: []federationapi.Cluster{*cluster1}})
RegisterFakeList("namespaces", &fakeClient.Fake, &apiv1.NamespaceList{Items: []apiv1.Namespace{}}) RegisterFakeList(namespaces, &fakeClient.Fake, &apiv1.NamespaceList{Items: []apiv1.Namespace{}})
namespaceWatch := RegisterFakeWatch("namespaces", &fakeClient.Fake) namespaceWatch := RegisterFakeWatch(namespaces, &fakeClient.Fake)
namespaceCreateChan := RegisterFakeCopyOnCreate("namespaces", &fakeClient.Fake, namespaceWatch) namespaceCreateChan := RegisterFakeCopyOnCreate(namespaces, &fakeClient.Fake, namespaceWatch)
clusterWatch := RegisterFakeWatch("clusters", &fakeClient.Fake) clusterWatch := RegisterFakeWatch(clusters, &fakeClient.Fake)
cluster1Client := &fakekubeclientset.Clientset{} cluster1Client := &fakekubeclientset.Clientset{}
cluster1Watch := RegisterFakeWatch("namespaces", &cluster1Client.Fake) cluster1Watch := RegisterFakeWatch(namespaces, &cluster1Client.Fake)
RegisterFakeList("namespaces", &cluster1Client.Fake, &apiv1.NamespaceList{Items: []apiv1.Namespace{}}) RegisterFakeList(namespaces, &cluster1Client.Fake, &apiv1.NamespaceList{Items: []apiv1.Namespace{}})
cluster1CreateChan := RegisterFakeCopyOnCreate("namespaces", &cluster1Client.Fake, cluster1Watch) cluster1CreateChan := RegisterFakeCopyOnCreate(namespaces, &cluster1Client.Fake, cluster1Watch)
cluster1UpdateChan := RegisterFakeCopyOnUpdate("namespaces", &cluster1Client.Fake, cluster1Watch) cluster1UpdateChan := RegisterFakeCopyOnUpdate(namespaces, &cluster1Client.Fake, cluster1Watch)
cluster2Client := &fakekubeclientset.Clientset{} cluster2Client := &fakekubeclientset.Clientset{}
cluster2Watch := RegisterFakeWatch("namespaces", &cluster2Client.Fake) cluster2Watch := RegisterFakeWatch(namespaces, &cluster2Client.Fake)
RegisterFakeList("namespaces", &cluster2Client.Fake, &apiv1.NamespaceList{Items: []apiv1.Namespace{}}) RegisterFakeList(namespaces, &cluster2Client.Fake, &apiv1.NamespaceList{Items: []apiv1.Namespace{}})
cluster2CreateChan := RegisterFakeCopyOnCreate("namespaces", &cluster2Client.Fake, cluster2Watch) cluster2CreateChan := RegisterFakeCopyOnCreate(namespaces, &cluster2Client.Fake, cluster2Watch)
nsDeleteChan := RegisterDelete(&fakeClient.Fake, "namespaces") nsDeleteChan := RegisterDelete(&fakeClient.Fake, namespaces)
namespaceController := NewNamespaceController(fakeClient, dynamic.NewDynamicClientPool(&restclient.Config{})) namespaceController := NewNamespaceController(fakeClient, dynamic.NewDynamicClientPool(&restclient.Config{}))
informerClientFactory := func(cluster *federationapi.Cluster) (kubeclientset.Interface, error) { informerClientFactory := func(cluster *federationapi.Cluster) (kubeclientset.Interface, error) {
switch cluster.Name { switch cluster.Name {
@@ -175,7 +180,9 @@ func GetStringFromChan(c chan string) string {
} }
func GetNamespaceFromChan(c chan runtime.Object) *apiv1.Namespace { func GetNamespaceFromChan(c chan runtime.Object) *apiv1.Namespace {
namespace := GetObjectFromChan(c).(*apiv1.Namespace) if namespace := GetObjectFromChan(c); namespace == nil {
return namespace return nil
} else {
return namespace.(*apiv1.Namespace)
}
} }

View File

@@ -37,6 +37,13 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
const (
pods = "pods"
replicasets = "replicasets"
k8s1 = "k8s-1"
k8s2 = "k8s-2"
)
func TestParseFederationReplicaSetReference(t *testing.T) { func TestParseFederationReplicaSetReference(t *testing.T) {
successPrefs := []string{ successPrefs := []string{
`{"rebalance": true, `{"rebalance": true,
@@ -82,27 +89,27 @@ func TestReplicaSetController(t *testing.T) {
fedclientset := fedclientfake.NewSimpleClientset() fedclientset := fedclientfake.NewSimpleClientset()
fedrswatch := watch.NewFake() 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(k8s1, apiv1.ConditionTrue))
fedclientset.Federation().Clusters().Create(testutil.NewCluster("k8s-2", apiv1.ConditionTrue)) fedclientset.Federation().Clusters().Create(testutil.NewCluster(k8s2, apiv1.ConditionTrue))
kube1clientset := kubeclientfake.NewSimpleClientset() kube1clientset := kubeclientfake.NewSimpleClientset()
kube1rswatch := watch.NewFake() kube1rswatch := watch.NewFake()
kube1clientset.PrependWatchReactor("replicasets", core.DefaultWatchReactor(kube1rswatch, nil)) kube1clientset.PrependWatchReactor(replicasets, core.DefaultWatchReactor(kube1rswatch, nil))
kube1Podwatch := watch.NewFake() kube1Podwatch := watch.NewFake()
kube1clientset.PrependWatchReactor("pods", core.DefaultWatchReactor(kube1Podwatch, nil)) kube1clientset.PrependWatchReactor(pods, core.DefaultWatchReactor(kube1Podwatch, nil))
kube2clientset := kubeclientfake.NewSimpleClientset() kube2clientset := kubeclientfake.NewSimpleClientset()
kube2rswatch := watch.NewFake() kube2rswatch := watch.NewFake()
kube2clientset.PrependWatchReactor("replicasets", core.DefaultWatchReactor(kube2rswatch, nil)) kube2clientset.PrependWatchReactor(replicasets, core.DefaultWatchReactor(kube2rswatch, nil))
kube2Podwatch := watch.NewFake() 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) { fedInformerClientFactory := func(cluster *fedv1.Cluster) (kubeclientset.Interface, error) {
switch cluster.Name { switch cluster.Name {
case "k8s-1": case k8s1:
return kube1clientset, nil return kube1clientset, nil
case "k8s-2": case k8s2:
return kube2clientset, nil return kube2clientset, nil
default: default:
return nil, fmt.Errorf("Unknown cluster: %v", cluster.Name) return nil, fmt.Errorf("Unknown cluster: %v", cluster.Name)

View File

@@ -39,27 +39,32 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
const (
clusters string = "clusters"
secrets string = "secrets"
)
func TestSecretController(t *testing.T) { func TestSecretController(t *testing.T) {
cluster1 := NewCluster("cluster1", apiv1.ConditionTrue) cluster1 := NewCluster("cluster1", apiv1.ConditionTrue)
cluster2 := NewCluster("cluster2", apiv1.ConditionTrue) cluster2 := NewCluster("cluster2", apiv1.ConditionTrue)
fakeClient := &fakefedclientset.Clientset{} fakeClient := &fakefedclientset.Clientset{}
RegisterFakeList("clusters", &fakeClient.Fake, &federationapi.ClusterList{Items: []federationapi.Cluster{*cluster1}}) RegisterFakeList(clusters, &fakeClient.Fake, &federationapi.ClusterList{Items: []federationapi.Cluster{*cluster1}})
RegisterFakeList("secrets", &fakeClient.Fake, &apiv1.SecretList{Items: []apiv1.Secret{}}) RegisterFakeList(secrets, &fakeClient.Fake, &apiv1.SecretList{Items: []apiv1.Secret{}})
secretWatch := RegisterFakeWatch("secrets", &fakeClient.Fake) secretWatch := RegisterFakeWatch(secrets, &fakeClient.Fake)
secretUpdateChan := RegisterFakeCopyOnUpdate("secrets", &fakeClient.Fake, secretWatch) secretUpdateChan := RegisterFakeCopyOnUpdate(secrets, &fakeClient.Fake, secretWatch)
clusterWatch := RegisterFakeWatch("clusters", &fakeClient.Fake) clusterWatch := RegisterFakeWatch(clusters, &fakeClient.Fake)
cluster1Client := &fakekubeclientset.Clientset{} cluster1Client := &fakekubeclientset.Clientset{}
cluster1Watch := RegisterFakeWatch("secrets", &cluster1Client.Fake) cluster1Watch := RegisterFakeWatch(secrets, &cluster1Client.Fake)
RegisterFakeList("secrets", &cluster1Client.Fake, &apiv1.SecretList{Items: []apiv1.Secret{}}) RegisterFakeList(secrets, &cluster1Client.Fake, &apiv1.SecretList{Items: []apiv1.Secret{}})
cluster1CreateChan := RegisterFakeCopyOnCreate("secrets", &cluster1Client.Fake, cluster1Watch) cluster1CreateChan := RegisterFakeCopyOnCreate(secrets, &cluster1Client.Fake, cluster1Watch)
cluster1UpdateChan := RegisterFakeCopyOnUpdate("secrets", &cluster1Client.Fake, cluster1Watch) cluster1UpdateChan := RegisterFakeCopyOnUpdate(secrets, &cluster1Client.Fake, cluster1Watch)
cluster2Client := &fakekubeclientset.Clientset{} cluster2Client := &fakekubeclientset.Clientset{}
cluster2Watch := RegisterFakeWatch("secrets", &cluster2Client.Fake) cluster2Watch := RegisterFakeWatch(secrets, &cluster2Client.Fake)
RegisterFakeList("secrets", &cluster2Client.Fake, &apiv1.SecretList{Items: []apiv1.Secret{}}) RegisterFakeList(secrets, &cluster2Client.Fake, &apiv1.SecretList{Items: []apiv1.Secret{}})
cluster2CreateChan := RegisterFakeCopyOnCreate("secrets", &cluster2Client.Fake, cluster2Watch) cluster2CreateChan := RegisterFakeCopyOnCreate(secrets, &cluster2Client.Fake, cluster2Watch)
secretController := NewSecretController(fakeClient) secretController := NewSecretController(fakeClient)
informerClientFactory := func(cluster *federationapi.Cluster) (kubeclientset.Interface, error) { 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 { func GetSecretFromChan(c chan runtime.Object) *apiv1.Secret {
secret := GetObjectFromChan(c).(*apiv1.Secret) if secret := GetObjectFromChan(c); secret == nil {
return secret return nil
} else {
return secret.(*apiv1.Secret)
}
} }
// Wait till the store is updated with latest secret. // Wait till the store is updated with latest secret.