diff --git a/federation/pkg/federation-controller/ingress/ingress_controller_test.go b/federation/pkg/federation-controller/ingress/ingress_controller_test.go index 48d95bbbea6..a2be3d68be9 100644 --- a/federation/pkg/federation-controller/ingress/ingress_controller_test.go +++ b/federation/pkg/federation-controller/ingress/ingress_controller_test.go @@ -23,43 +23,40 @@ import ( "time" federation_api "k8s.io/kubernetes/federation/apis/federation/v1beta1" - // federation_release_1_4 "k8s.io/kubernetes/federation/client/clientset_generated/federation_release_1_4" fake_federation_release_1_4 "k8s.io/kubernetes/federation/client/clientset_generated/federation_release_1_4/fake" - "k8s.io/kubernetes/federation/pkg/federation-controller/util" + . "k8s.io/kubernetes/federation/pkg/federation-controller/util/test" api_v1 "k8s.io/kubernetes/pkg/api/v1" extensions_v1beta1 "k8s.io/kubernetes/pkg/apis/extensions/v1beta1" kube_release_1_4 "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_4" fake_kube_release_1_4 "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_4/fake" - "k8s.io/kubernetes/pkg/client/testing/core" "k8s.io/kubernetes/pkg/runtime" - "k8s.io/kubernetes/pkg/watch" "github.com/stretchr/testify/assert" ) func TestIngressController(t *testing.T) { - cluster1 := mkCluster("cluster1", api_v1.ConditionTrue) - cluster2 := mkCluster("cluster2", api_v1.ConditionTrue) + cluster1 := NewCluster("cluster1", api_v1.ConditionTrue) + cluster2 := NewCluster("cluster2", api_v1.ConditionTrue) fakeClient := &fake_federation_release_1_4.Clientset{} - RegisterList("clusters", &fakeClient.Fake, &federation_api.ClusterList{Items: []federation_api.Cluster{*cluster1}}) - RegisterList("ingresses", &fakeClient.Fake, &extensions_v1beta1.IngressList{Items: []extensions_v1beta1.Ingress{}}) - ingressWatch := RegisterWatch("ingresses", &fakeClient.Fake) - clusterWatch := RegisterWatch("clusters", &fakeClient.Fake) + RegisterFakeList("clusters", &fakeClient.Fake, &federation_api.ClusterList{Items: []federation_api.Cluster{*cluster1}}) + RegisterFakeList("ingresses", &fakeClient.Fake, &extensions_v1beta1.IngressList{Items: []extensions_v1beta1.Ingress{}}) + ingressWatch := RegisterFakeWatch("ingresses", &fakeClient.Fake) + clusterWatch := RegisterFakeWatch("clusters", &fakeClient.Fake) cluster1Client := &fake_kube_release_1_4.Clientset{} - cluster1Watch := RegisterWatch("ingresses", &cluster1Client.Fake) - RegisterList("ingresses", &cluster1Client.Fake, &extensions_v1beta1.IngressList{Items: []extensions_v1beta1.Ingress{}}) - cluster1CreateChan := RegisterCopyOnCreate("ingresses", &cluster1Client.Fake, cluster1Watch) - cluster1UpdateChan := RegisterCopyOnUpdate("ingresses", &cluster1Client.Fake, cluster1Watch) + cluster1Watch := RegisterFakeWatch("ingresses", &cluster1Client.Fake) + RegisterFakeList("ingresses", &cluster1Client.Fake, &extensions_v1beta1.IngressList{Items: []extensions_v1beta1.Ingress{}}) + cluster1CreateChan := RegisterFakeCopyOnCreate("ingresses", &cluster1Client.Fake, cluster1Watch) + cluster1UpdateChan := RegisterFakeCopyOnUpdate("ingresses", &cluster1Client.Fake, cluster1Watch) cluster2Client := &fake_kube_release_1_4.Clientset{} - cluster2Watch := RegisterWatch("ingresses", &cluster2Client.Fake) - RegisterList("ingresses", &cluster2Client.Fake, &extensions_v1beta1.IngressList{Items: []extensions_v1beta1.Ingress{}}) - cluster2CreateChan := RegisterCopyOnCreate("ingresses", &cluster2Client.Fake, cluster2Watch) + cluster2Watch := RegisterFakeWatch("ingresses", &cluster2Client.Fake) + RegisterFakeList("ingresses", &cluster2Client.Fake, &extensions_v1beta1.IngressList{Items: []extensions_v1beta1.Ingress{}}) + cluster2CreateChan := RegisterFakeCopyOnCreate("ingresses", &cluster2Client.Fake, cluster2Watch) ingressController := NewIngressController(fakeClient) - informer := toFederatedInformerForTestOnly(ingressController.ingressFederatedInformer) + informer := ToFederatedInformerForTestOnly(ingressController.ingressFederatedInformer) informer.SetClientFactory(func(cluster *federation_api.Cluster) (kube_release_1_4.Interface, error) { switch cluster.Name { case cluster1.Name: @@ -111,70 +108,7 @@ func TestIngressController(t *testing.T) { close(stop) } -func toFederatedInformerForTestOnly(informer util.FederatedInformer) util.FederatedInformerForTestOnly { - inter := informer.(interface{}) - return inter.(util.FederatedInformerForTestOnly) -} - -func mkCluster(name string, readyStatus api_v1.ConditionStatus) *federation_api.Cluster { - return &federation_api.Cluster{ - ObjectMeta: api_v1.ObjectMeta{ - Name: name, - }, - Status: federation_api.ClusterStatus{ - Conditions: []federation_api.ClusterCondition{ - {Type: federation_api.ClusterReady, Status: readyStatus}, - }, - }, - } -} - -func RegisterWatch(resource string, client *core.Fake) *watch.FakeWatcher { - watcher := watch.NewFake() - client.AddWatchReactor(resource, func(action core.Action) (bool, watch.Interface, error) { return true, watcher, nil }) - return watcher -} - -func RegisterList(resource string, client *core.Fake, obj runtime.Object) { - client.AddReactor("list", resource, func(action core.Action) (bool, runtime.Object, error) { - return true, obj, nil - }) -} - -func RegisterCopyOnCreate(resource string, client *core.Fake, watcher *watch.FakeWatcher) chan runtime.Object { - objChan := make(chan runtime.Object, 100) - client.AddReactor("create", resource, func(action core.Action) (bool, runtime.Object, error) { - createAction := action.(core.CreateAction) - obj := createAction.GetObject() - go func() { - watcher.Add(obj) - objChan <- obj - }() - return true, obj, nil - }) - return objChan -} - -func RegisterCopyOnUpdate(resource string, client *core.Fake, watcher *watch.FakeWatcher) chan runtime.Object { - objChan := make(chan runtime.Object, 100) - client.AddReactor("update", resource, func(action core.Action) (bool, runtime.Object, error) { - updateAction := action.(core.UpdateAction) - obj := updateAction.GetObject() - go func() { - watcher.Modify(obj) - objChan <- obj - }() - return true, obj, nil - }) - return objChan -} - func GetIngressFromChan(c chan runtime.Object) *extensions_v1beta1.Ingress { - select { - case obj := <-c: - ingress := obj.(*extensions_v1beta1.Ingress) - return ingress - case <-time.After(time.Minute): - return nil - } + ingress := GetObjectFromChan(c).(*extensions_v1beta1.Ingress) + return ingress } diff --git a/federation/pkg/federation-controller/namespace/namespace_controller_test.go b/federation/pkg/federation-controller/namespace/namespace_controller_test.go index c5d83f3e1da..15ae5736247 100644 --- a/federation/pkg/federation-controller/namespace/namespace_controller_test.go +++ b/federation/pkg/federation-controller/namespace/namespace_controller_test.go @@ -23,40 +23,38 @@ import ( federation_api "k8s.io/kubernetes/federation/apis/federation/v1beta1" fake_federation_release_1_4 "k8s.io/kubernetes/federation/client/clientset_generated/federation_release_1_4/fake" - "k8s.io/kubernetes/federation/pkg/federation-controller/util" + . "k8s.io/kubernetes/federation/pkg/federation-controller/util/test" api_v1 "k8s.io/kubernetes/pkg/api/v1" kube_release_1_4 "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_4" fake_kube_release_1_4 "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_4/fake" - "k8s.io/kubernetes/pkg/client/testing/core" "k8s.io/kubernetes/pkg/runtime" - "k8s.io/kubernetes/pkg/watch" "github.com/stretchr/testify/assert" ) func TestNamespaceController(t *testing.T) { - cluster1 := mkCluster("cluster1", api_v1.ConditionTrue) - cluster2 := mkCluster("cluster2", api_v1.ConditionTrue) + cluster1 := NewCluster("cluster1", api_v1.ConditionTrue) + cluster2 := NewCluster("cluster2", api_v1.ConditionTrue) fakeClient := &fake_federation_release_1_4.Clientset{} - RegisterList("clusters", &fakeClient.Fake, &federation_api.ClusterList{Items: []federation_api.Cluster{*cluster1}}) - RegisterList("namespaces", &fakeClient.Fake, &api_v1.NamespaceList{Items: []api_v1.Namespace{}}) - namespaceWatch := RegisterWatch("namespaces", &fakeClient.Fake) - clusterWatch := RegisterWatch("clusters", &fakeClient.Fake) + RegisterFakeList("clusters", &fakeClient.Fake, &federation_api.ClusterList{Items: []federation_api.Cluster{*cluster1}}) + RegisterFakeList("namespaces", &fakeClient.Fake, &api_v1.NamespaceList{Items: []api_v1.Namespace{}}) + namespaceWatch := RegisterFakeWatch("namespaces", &fakeClient.Fake) + clusterWatch := RegisterFakeWatch("clusters", &fakeClient.Fake) cluster1Client := &fake_kube_release_1_4.Clientset{} - cluster1Watch := RegisterWatch("namespaces", &cluster1Client.Fake) - RegisterList("namespaces", &cluster1Client.Fake, &api_v1.NamespaceList{Items: []api_v1.Namespace{}}) - cluster1CreateChan := RegisterCopyOnCreate("namespaces", &cluster1Client.Fake, cluster1Watch) - cluster1UpdateChan := RegisterCopyOnUpdate("namespaces", &cluster1Client.Fake, cluster1Watch) + cluster1Watch := RegisterFakeWatch("namespaces", &cluster1Client.Fake) + RegisterFakeList("namespaces", &cluster1Client.Fake, &api_v1.NamespaceList{Items: []api_v1.Namespace{}}) + cluster1CreateChan := RegisterFakeCopyOnCreate("namespaces", &cluster1Client.Fake, cluster1Watch) + cluster1UpdateChan := RegisterFakeCopyOnUpdate("namespaces", &cluster1Client.Fake, cluster1Watch) cluster2Client := &fake_kube_release_1_4.Clientset{} - cluster2Watch := RegisterWatch("namespaces", &cluster2Client.Fake) - RegisterList("namespaces", &cluster2Client.Fake, &api_v1.NamespaceList{Items: []api_v1.Namespace{}}) - cluster2CreateChan := RegisterCopyOnCreate("namespaces", &cluster2Client.Fake, cluster2Watch) + cluster2Watch := RegisterFakeWatch("namespaces", &cluster2Client.Fake) + RegisterFakeList("namespaces", &cluster2Client.Fake, &api_v1.NamespaceList{Items: []api_v1.Namespace{}}) + cluster2CreateChan := RegisterFakeCopyOnCreate("namespaces", &cluster2Client.Fake, cluster2Watch) namespaceController := NewNamespaceController(fakeClient) - informer := toFederatedInformerForTestOnly(namespaceController.namespaceFederatedInformer) + informer := ToFederatedInformerForTestOnly(namespaceController.namespaceFederatedInformer) informer.SetClientFactory(func(cluster *federation_api.Cluster) (kube_release_1_4.Interface, error) { switch cluster.Name { case cluster1.Name: @@ -107,70 +105,8 @@ func TestNamespaceController(t *testing.T) { close(stop) } -func toFederatedInformerForTestOnly(informer util.FederatedInformer) util.FederatedInformerForTestOnly { - inter := informer.(interface{}) - return inter.(util.FederatedInformerForTestOnly) -} - -func mkCluster(name string, readyStatus api_v1.ConditionStatus) *federation_api.Cluster { - return &federation_api.Cluster{ - ObjectMeta: api_v1.ObjectMeta{ - Name: name, - }, - Status: federation_api.ClusterStatus{ - Conditions: []federation_api.ClusterCondition{ - {Type: federation_api.ClusterReady, Status: readyStatus}, - }, - }, - } -} - -func RegisterWatch(resource string, client *core.Fake) *watch.FakeWatcher { - watcher := watch.NewFake() - client.AddWatchReactor(resource, func(action core.Action) (bool, watch.Interface, error) { return true, watcher, nil }) - return watcher -} - -func RegisterList(resource string, client *core.Fake, obj runtime.Object) { - client.AddReactor("list", resource, func(action core.Action) (bool, runtime.Object, error) { - return true, obj, nil - }) -} - -func RegisterCopyOnCreate(resource string, client *core.Fake, watcher *watch.FakeWatcher) chan runtime.Object { - objChan := make(chan runtime.Object, 100) - client.AddReactor("create", resource, func(action core.Action) (bool, runtime.Object, error) { - createAction := action.(core.CreateAction) - obj := createAction.GetObject() - go func() { - watcher.Add(obj) - objChan <- obj - }() - return true, obj, nil - }) - return objChan -} - -func RegisterCopyOnUpdate(resource string, client *core.Fake, watcher *watch.FakeWatcher) chan runtime.Object { - objChan := make(chan runtime.Object, 100) - client.AddReactor("update", resource, func(action core.Action) (bool, runtime.Object, error) { - updateAction := action.(core.UpdateAction) - obj := updateAction.GetObject() - go func() { - watcher.Modify(obj) - objChan <- obj - }() - return true, obj, nil - }) - return objChan -} - func GetNamespaceFromChan(c chan runtime.Object) *api_v1.Namespace { - select { - case obj := <-c: - namespace := obj.(*api_v1.Namespace) - return namespace - case <-time.After(time.Minute): - return nil - } + namespace := GetObjectFromChan(c).(*api_v1.Namespace) + return namespace + } diff --git a/federation/pkg/federation-controller/replicaset/replicasetcontroller_test.go b/federation/pkg/federation-controller/replicaset/replicasetcontroller_test.go index 3a194a40d19..79b0b7ce8b9 100644 --- a/federation/pkg/federation-controller/replicaset/replicasetcontroller_test.go +++ b/federation/pkg/federation-controller/replicaset/replicasetcontroller_test.go @@ -19,10 +19,12 @@ package replicaset import ( "flag" "fmt" - "github.com/stretchr/testify/assert" + "testing" + "time" + fedv1 "k8s.io/kubernetes/federation/apis/federation/v1beta1" fedclientfake "k8s.io/kubernetes/federation/client/clientset_generated/federation_release_1_4/fake" - fedutil "k8s.io/kubernetes/federation/pkg/federation-controller/util" + "k8s.io/kubernetes/federation/pkg/federation-controller/util/test" "k8s.io/kubernetes/pkg/api/meta" apiv1 "k8s.io/kubernetes/pkg/api/v1" extensionsv1 "k8s.io/kubernetes/pkg/apis/extensions/v1beta1" @@ -30,8 +32,8 @@ import ( kubeclientfake "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_4/fake" "k8s.io/kubernetes/pkg/client/testing/core" "k8s.io/kubernetes/pkg/watch" - "testing" - "time" + + "github.com/stretchr/testify/assert" ) func TestParseFederationReplicaSetReference(t *testing.T) { @@ -81,8 +83,8 @@ func TestReplicaSetController(t *testing.T) { fedrswatch := watch.NewFake() fedclientset.PrependWatchReactor("replicasets", core.DefaultWatchReactor(fedrswatch, nil)) - fedclientset.Federation().Clusters().Create(newClusterWithReadyStatus("k8s-1", apiv1.ConditionTrue)) - fedclientset.Federation().Clusters().Create(newClusterWithReadyStatus("k8s-2", apiv1.ConditionTrue)) + fedclientset.Federation().Clusters().Create(testutil.NewCluster("k8s-1", apiv1.ConditionTrue)) + fedclientset.Federation().Clusters().Create(testutil.NewCluster("k8s-2", apiv1.ConditionTrue)) kube1clientset := kubeclientfake.NewSimpleClientset() kube1rswatch := watch.NewFake() @@ -106,9 +108,9 @@ func TestReplicaSetController(t *testing.T) { } } replicaSetController := NewReplicaSetController(fedclientset) - rsFedinformer := toFederatedInformerForTestOnly(replicaSetController.fedReplicaSetInformer) + rsFedinformer := testutil.ToFederatedInformerForTestOnly(replicaSetController.fedReplicaSetInformer) rsFedinformer.SetClientFactory(fedInformerClientFactory) - podFedinformer := toFederatedInformerForTestOnly(replicaSetController.fedPodInformer) + podFedinformer := testutil.ToFederatedInformerForTestOnly(replicaSetController.fedPodInformer) podFedinformer.SetClientFactory(fedInformerClientFactory) stopChan := make(chan struct{}) @@ -159,24 +161,6 @@ func TestReplicaSetController(t *testing.T) { assert.Equal(t, rs.Status.Replicas, rs1.Status.Replicas+rs2.Status.Replicas) } -func toFederatedInformerForTestOnly(informer fedutil.FederatedInformer) fedutil.FederatedInformerForTestOnly { - inter := informer.(interface{}) - return inter.(fedutil.FederatedInformerForTestOnly) -} - -func newClusterWithReadyStatus(name string, readyStatus apiv1.ConditionStatus) *fedv1.Cluster { - return &fedv1.Cluster{ - ObjectMeta: apiv1.ObjectMeta{ - Name: name, - }, - Status: fedv1.ClusterStatus{ - Conditions: []fedv1.ClusterCondition{ - {Type: fedv1.ClusterReady, Status: readyStatus}, - }, - }, - } -} - func newReplicaSetWithReplicas(name string, replicas int32) *extensionsv1.ReplicaSet { return &extensionsv1.ReplicaSet{ ObjectMeta: apiv1.ObjectMeta{ diff --git a/federation/pkg/federation-controller/secret/secret_controller_test.go b/federation/pkg/federation-controller/secret/secret_controller_test.go index 22a6cf101a1..4af7e7e33ba 100644 --- a/federation/pkg/federation-controller/secret/secret_controller_test.go +++ b/federation/pkg/federation-controller/secret/secret_controller_test.go @@ -24,40 +24,38 @@ import ( federation_api "k8s.io/kubernetes/federation/apis/federation/v1beta1" fake_federation_release_1_4 "k8s.io/kubernetes/federation/client/clientset_generated/federation_release_1_4/fake" - "k8s.io/kubernetes/federation/pkg/federation-controller/util" + . "k8s.io/kubernetes/federation/pkg/federation-controller/util/test" api_v1 "k8s.io/kubernetes/pkg/api/v1" kube_release_1_4 "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_4" fake_kube_release_1_4 "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_4/fake" - "k8s.io/kubernetes/pkg/client/testing/core" "k8s.io/kubernetes/pkg/runtime" - "k8s.io/kubernetes/pkg/watch" "github.com/stretchr/testify/assert" ) func TestSecretController(t *testing.T) { - cluster1 := mkCluster("cluster1", api_v1.ConditionTrue) - cluster2 := mkCluster("cluster2", api_v1.ConditionTrue) + cluster1 := NewCluster("cluster1", api_v1.ConditionTrue) + cluster2 := NewCluster("cluster2", api_v1.ConditionTrue) fakeClient := &fake_federation_release_1_4.Clientset{} - RegisterList("clusters", &fakeClient.Fake, &federation_api.ClusterList{Items: []federation_api.Cluster{*cluster1}}) - RegisterList("secrets", &fakeClient.Fake, &api_v1.SecretList{Items: []api_v1.Secret{}}) - secretWatch := RegisterWatch("secrets", &fakeClient.Fake) - clusterWatch := RegisterWatch("clusters", &fakeClient.Fake) + RegisterFakeList("clusters", &fakeClient.Fake, &federation_api.ClusterList{Items: []federation_api.Cluster{*cluster1}}) + RegisterFakeList("secrets", &fakeClient.Fake, &api_v1.SecretList{Items: []api_v1.Secret{}}) + secretWatch := RegisterFakeWatch("secrets", &fakeClient.Fake) + clusterWatch := RegisterFakeWatch("clusters", &fakeClient.Fake) cluster1Client := &fake_kube_release_1_4.Clientset{} - cluster1Watch := RegisterWatch("secrets", &cluster1Client.Fake) - RegisterList("secrets", &cluster1Client.Fake, &api_v1.SecretList{Items: []api_v1.Secret{}}) - cluster1CreateChan := RegisterCopyOnCreate("secrets", &cluster1Client.Fake, cluster1Watch) - cluster1UpdateChan := RegisterCopyOnUpdate("secrets", &cluster1Client.Fake, cluster1Watch) + cluster1Watch := RegisterFakeWatch("secrets", &cluster1Client.Fake) + RegisterFakeList("secrets", &cluster1Client.Fake, &api_v1.SecretList{Items: []api_v1.Secret{}}) + cluster1CreateChan := RegisterFakeCopyOnCreate("secrets", &cluster1Client.Fake, cluster1Watch) + cluster1UpdateChan := RegisterFakeCopyOnUpdate("secrets", &cluster1Client.Fake, cluster1Watch) cluster2Client := &fake_kube_release_1_4.Clientset{} - cluster2Watch := RegisterWatch("secrets", &cluster2Client.Fake) - RegisterList("secrets", &cluster2Client.Fake, &api_v1.SecretList{Items: []api_v1.Secret{}}) - cluster2CreateChan := RegisterCopyOnCreate("secrets", &cluster2Client.Fake, cluster2Watch) + cluster2Watch := RegisterFakeWatch("secrets", &cluster2Client.Fake) + RegisterFakeList("secrets", &cluster2Client.Fake, &api_v1.SecretList{Items: []api_v1.Secret{}}) + cluster2CreateChan := RegisterFakeCopyOnCreate("secrets", &cluster2Client.Fake, cluster2Watch) secretController := NewSecretController(fakeClient) - informer := toFederatedInformerForTestOnly(secretController.secretFederatedInformer) + informer := ToFederatedInformerForTestOnly(secretController.secretFederatedInformer) informer.SetClientFactory(func(cluster *federation_api.Cluster) (kube_release_1_4.Interface, error) { switch cluster.Name { case cluster1.Name: @@ -119,70 +117,7 @@ func TestSecretController(t *testing.T) { close(stop) } -func toFederatedInformerForTestOnly(informer util.FederatedInformer) util.FederatedInformerForTestOnly { - inter := informer.(interface{}) - return inter.(util.FederatedInformerForTestOnly) -} - -func mkCluster(name string, readyStatus api_v1.ConditionStatus) *federation_api.Cluster { - return &federation_api.Cluster{ - ObjectMeta: api_v1.ObjectMeta{ - Name: name, - }, - Status: federation_api.ClusterStatus{ - Conditions: []federation_api.ClusterCondition{ - {Type: federation_api.ClusterReady, Status: readyStatus}, - }, - }, - } -} - -func RegisterWatch(resource string, client *core.Fake) *watch.FakeWatcher { - watcher := watch.NewFake() - client.AddWatchReactor(resource, func(action core.Action) (bool, watch.Interface, error) { return true, watcher, nil }) - return watcher -} - -func RegisterList(resource string, client *core.Fake, obj runtime.Object) { - client.AddReactor("list", resource, func(action core.Action) (bool, runtime.Object, error) { - return true, obj, nil - }) -} - -func RegisterCopyOnCreate(resource string, client *core.Fake, watcher *watch.FakeWatcher) chan runtime.Object { - objChan := make(chan runtime.Object, 100) - client.AddReactor("create", resource, func(action core.Action) (bool, runtime.Object, error) { - createAction := action.(core.CreateAction) - obj := createAction.GetObject() - go func() { - watcher.Add(obj) - objChan <- obj - }() - return true, obj, nil - }) - return objChan -} - -func RegisterCopyOnUpdate(resource string, client *core.Fake, watcher *watch.FakeWatcher) chan runtime.Object { - objChan := make(chan runtime.Object, 100) - client.AddReactor("update", resource, func(action core.Action) (bool, runtime.Object, error) { - updateAction := action.(core.UpdateAction) - obj := updateAction.GetObject() - go func() { - watcher.Modify(obj) - objChan <- obj - }() - return true, obj, nil - }) - return objChan -} - func GetSecretFromChan(c chan runtime.Object) *api_v1.Secret { - select { - case obj := <-c: - secret := obj.(*api_v1.Secret) - return secret - case <-time.After(time.Minute): - return nil - } + secret := GetObjectFromChan(c).(*api_v1.Secret) + return secret } diff --git a/federation/pkg/federation-controller/util/test/test_helper.go b/federation/pkg/federation-controller/util/test/test_helper.go new file mode 100644 index 00000000000..c4ce317d8b8 --- /dev/null +++ b/federation/pkg/federation-controller/util/test/test_helper.go @@ -0,0 +1,108 @@ +/* +Copyright 2016 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package testutil + +import ( + "time" + + federation_api "k8s.io/kubernetes/federation/apis/federation/v1beta1" + "k8s.io/kubernetes/federation/pkg/federation-controller/util" + api_v1 "k8s.io/kubernetes/pkg/api/v1" + "k8s.io/kubernetes/pkg/client/testing/core" + "k8s.io/kubernetes/pkg/runtime" + "k8s.io/kubernetes/pkg/watch" +) + +// RegisterFakeWatch adds a new fake watcher for the specified resource in the given fake client. +// All subsequent requrest for watch on the client will result in returning this fake watcher. +func RegisterFakeWatch(resource string, client *core.Fake) *watch.FakeWatcher { + watcher := watch.NewFake() + client.AddWatchReactor(resource, func(action core.Action) (bool, watch.Interface, error) { return true, watcher, nil }) + return watcher +} + +// RegisterFakeList registers a list response for the specified resource inside the given fake client. +// The passed value will be returned with every list call. +func RegisterFakeList(resource string, client *core.Fake, obj runtime.Object) { + client.AddReactor("list", resource, func(action core.Action) (bool, runtime.Object, error) { + return true, obj, nil + }) +} + +// RegisterFakeCopyOnCreate register a reactor in the given fake client that passes +// all created object to the given watcher and also copies them to a channel for +// in-test inspection. +func RegisterFakeCopyOnCreate(resource string, client *core.Fake, watcher *watch.FakeWatcher) chan runtime.Object { + objChan := make(chan runtime.Object, 100) + client.AddReactor("create", resource, func(action core.Action) (bool, runtime.Object, error) { + createAction := action.(core.CreateAction) + obj := createAction.GetObject() + go func() { + watcher.Add(obj) + objChan <- obj + }() + return true, obj, nil + }) + return objChan +} + +// RegisterFakeCopyOnCreate register a reactor in the given fake client that passes +// all updated object to the given watcher and also copies them to a channel for +// in-test inspection. +func RegisterFakeCopyOnUpdate(resource string, client *core.Fake, watcher *watch.FakeWatcher) chan runtime.Object { + objChan := make(chan runtime.Object, 100) + client.AddReactor("update", resource, func(action core.Action) (bool, runtime.Object, error) { + updateAction := action.(core.UpdateAction) + obj := updateAction.GetObject() + go func() { + watcher.Modify(obj) + objChan <- obj + }() + return true, obj, nil + }) + return objChan +} + +// GetObjectFromChan tries to get an api object from the given channel +// within a reasonable time (1 min). +func GetObjectFromChan(c chan runtime.Object) runtime.Object { + select { + case obj := <-c: + return obj + case <-time.After(time.Minute): + return nil + } +} + +func ToFederatedInformerForTestOnly(informer util.FederatedInformer) util.FederatedInformerForTestOnly { + inter := informer.(interface{}) + return inter.(util.FederatedInformerForTestOnly) +} + +// NewCluster build a new cluster object. +func NewCluster(name string, readyStatus api_v1.ConditionStatus) *federation_api.Cluster { + return &federation_api.Cluster{ + ObjectMeta: api_v1.ObjectMeta{ + Name: name, + }, + Status: federation_api.ClusterStatus{ + Conditions: []federation_api.ClusterCondition{ + {Type: federation_api.ClusterReady, Status: readyStatus}, + }, + }, + } +}