From 9e7760d80ad0532b7450c40f62a48b2d5d2cd7c6 Mon Sep 17 00:00:00 2001 From: Felix Kunde Date: Thu, 18 Apr 2019 18:04:37 +0200 Subject: [PATCH] put mock methods to k8sutil to avoid cycling imports --- pkg/cluster/cluster.go | 1 + pkg/cluster/cluster_test.go | 30 +++++++++++- pkg/controller/util_test.go | 91 +------------------------------------ pkg/util/k8sutil/k8sutil.go | 66 ++++++++++++++++++++++++++- 4 files changed, 96 insertions(+), 92 deletions(-) diff --git a/pkg/cluster/cluster.go b/pkg/cluster/cluster.go index dd363053c..d1f285be6 100644 --- a/pkg/cluster/cluster.go +++ b/pkg/cluster/cluster.go @@ -148,6 +148,7 @@ func (c *Cluster) setProcessName(procName string, args ...interface{}) { } } +// SetStatus of Postgres cluster func (c *Cluster) SetStatus(status string) { // TODO: eventually switch to updateStatus() for kubernetes 1.11 and above patch, err := json.Marshal(acidv1.PostgresStatus{PostgresClusterStatus: status}) diff --git a/pkg/cluster/cluster_test.go b/pkg/cluster/cluster_test.go index 0940dd034..547ec19c7 100644 --- a/pkg/cluster/cluster_test.go +++ b/pkg/cluster/cluster_test.go @@ -30,7 +30,7 @@ var cl = New( }, }, }, - k8sutil.KubernetesClient{}, + k8sutil.NewMockKubernetesClient(), acidv1.Postgresql{}, logger, ) @@ -328,3 +328,31 @@ func TestShouldDeleteSecret(t *testing.T) { } } } + +func TestSetStatus(t *testing.T) { + + tests := []struct { + status acidv1.PostgresStatus + outcome bool + }{ + { + status: acidv1.PostgresStatus{PostgresClusterStatus: acidv1.ClusterStatusCreating}, + outcome: cl.Status.Creating(), + }, + { + status: acidv1.PostgresStatus{PostgresClusterStatus: acidv1.ClusterStatusRunning}, + outcome: cl.Status.Running(), + }, + { + status: acidv1.PostgresStatus{PostgresClusterStatus: acidv1.ClusterStatusInvalid}, + outcome: !cl.Status.Success(), + }, + } + + for _, tt := range tests { + cl.SetStatus(tt.status.PostgresClusterStatus) + if tt.outcome { + t.Errorf("Wrong status: %s", cl.Status.String()) + } + } +} diff --git a/pkg/controller/util_test.go b/pkg/controller/util_test.go index ae2ab5147..c9e16cbd9 100644 --- a/pkg/controller/util_test.go +++ b/pkg/controller/util_test.go @@ -7,82 +7,23 @@ import ( b64 "encoding/base64" - acidv1 "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1" "github.com/zalando/postgres-operator/pkg/spec" "github.com/zalando/postgres-operator/pkg/util/k8sutil" "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - v1core "k8s.io/client-go/kubernetes/typed/core/v1" ) const ( testInfrastructureRolesSecretName = "infrastructureroles-test" ) -type mockSecret struct { - v1core.SecretInterface -} - -type mockConfigMap struct { - v1core.ConfigMapInterface -} - -func (c *mockSecret) Get(name string, options metav1.GetOptions) (*v1.Secret, error) { - if name != testInfrastructureRolesSecretName { - return nil, fmt.Errorf("NotFound") - } - secret := &v1.Secret{} - secret.Name = mockController.opConfig.ClusterNameLabel - secret.Data = map[string][]byte{ - "user1": []byte("testrole"), - "password1": []byte("testpassword"), - "inrole1": []byte("testinrole"), - "foobar": []byte(b64.StdEncoding.EncodeToString([]byte("password"))), - } - return secret, nil - -} - -func (c *mockConfigMap) Get(name string, options metav1.GetOptions) (*v1.ConfigMap, error) { - if name != testInfrastructureRolesSecretName { - return nil, fmt.Errorf("NotFound") - } - configmap := &v1.ConfigMap{} - configmap.Name = mockController.opConfig.ClusterNameLabel - configmap.Data = map[string]string{ - "foobar": "{}", - } - return configmap, nil -} - -type MockSecretGetter struct { -} - -type MockConfigMapsGetter struct { -} - -func (c *MockSecretGetter) Secrets(namespace string) v1core.SecretInterface { - return &mockSecret{} -} - -func (c *MockConfigMapsGetter) ConfigMaps(namespace string) v1core.ConfigMapInterface { - return &mockConfigMap{} -} - -func newMockKubernetesClient() k8sutil.KubernetesClient { - return k8sutil.KubernetesClient{ - SecretsGetter: &MockSecretGetter{}, - ConfigMapsGetter: &MockConfigMapsGetter{}, - } -} - func newMockController() *Controller { controller := NewController(&spec.ControllerConfig{}) controller.opConfig.ClusterNameLabel = "cluster-name" controller.opConfig.InfrastructureRolesSecretName = spec.NamespacedName{Namespace: v1.NamespaceDefault, Name: testInfrastructureRolesSecretName} controller.opConfig.Workers = 4 - controller.KubeClient = newMockKubernetesClient() + controller.KubeClient = k8sutil.NewMockKubernetesClient() return controller } @@ -187,33 +128,3 @@ func TestGetInfrastructureRoles(t *testing.T) { } } } - -func TestSetStatus(t *testing.T) { - - for _, cl := range c.clusters { - tests := []struct { - status acidv1.PostgresStatus - outcome bool - }{ - { - status: acidv1.PostgresStatus{PostgresClusterStatus: acidv1.ClusterStatusCreating}, - outcome: cl.Status.Creating(), - }, - { - status: acidv1.PostgresStatus{PostgresClusterStatus: acidv1.ClusterStatusRunning}, - outcome: cl.Status.Running(), - }, - { - status: acidv1.PostgresStatus{PostgresClusterStatus: acidv1.ClusterStatusInvalid}, - outcome: !cl.Status.Success(), - }, - } - - for _, tt := range tests { - cl.SetStatus(tt.status.PostgresClusterStatus) - if tt.outcome { - t.Errorf("Wrong status: %s", cl.Status.String()) - } - } - } -} diff --git a/pkg/util/k8sutil/k8sutil.go b/pkg/util/k8sutil/k8sutil.go index 6f5b15085..dad6a7b8e 100644 --- a/pkg/util/k8sutil/k8sutil.go +++ b/pkg/util/k8sutil/k8sutil.go @@ -2,6 +2,10 @@ package k8sutil import ( "fmt" + "reflect" + + b64 "encoding/base64" + "github.com/zalando/postgres-operator/pkg/util/constants" "k8s.io/api/core/v1" policybeta1 "k8s.io/api/policy/v1beta1" @@ -15,9 +19,9 @@ import ( rbacv1beta1 "k8s.io/client-go/kubernetes/typed/rbac/v1beta1" "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" - "reflect" acidv1client "github.com/zalando/postgres-operator/pkg/generated/clientset/versioned" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) // KubernetesClient describes getters for Kubernetes objects @@ -41,6 +45,20 @@ type KubernetesClient struct { AcidV1ClientSet *acidv1client.Clientset } +type mockSecret struct { + v1core.SecretInterface +} + +type MockSecretGetter struct { +} + +type mockConfigMap struct { + v1core.ConfigMapInterface +} + +type MockConfigMapsGetter struct { +} + // RestConfig creates REST config func RestConfig(kubeConfig string, outOfCluster bool) (*rest.Config, error) { if outOfCluster { @@ -140,3 +158,49 @@ func SamePDB(cur, new *policybeta1.PodDisruptionBudget) (match bool, reason stri return } + +func (c *mockSecret) Get(name string, options metav1.GetOptions) (*v1.Secret, error) { + if name != "infrastructureroles-test" { + return nil, fmt.Errorf("NotFound") + } + secret := &v1.Secret{} + secret.Name = "testcluster" + secret.Data = map[string][]byte{ + "user1": []byte("testrole"), + "password1": []byte("testpassword"), + "inrole1": []byte("testinrole"), + "foobar": []byte(b64.StdEncoding.EncodeToString([]byte("password"))), + } + return secret, nil + +} + +func (c *mockConfigMap) Get(name string, options metav1.GetOptions) (*v1.ConfigMap, error) { + if name != "infrastructureroles-test" { + return nil, fmt.Errorf("NotFound") + } + configmap := &v1.ConfigMap{} + configmap.Name = "testcluster" + configmap.Data = map[string]string{ + "foobar": "{}", + } + return configmap, nil +} + +// Secrets to be mocked +func (c *MockSecretGetter) Secrets(namespace string) v1core.SecretInterface { + return &mockSecret{} +} + +// ConfigMaps to be mocked +func (c *MockConfigMapsGetter) ConfigMaps(namespace string) v1core.ConfigMapInterface { + return &mockConfigMap{} +} + +// NewMockKubernetesClient for other tests +func NewMockKubernetesClient() KubernetesClient { + return KubernetesClient{ + SecretsGetter: &MockSecretGetter{}, + ConfigMapsGetter: &MockConfigMapsGetter{}, + } +}