put mock methods to k8sutil to avoid cycling imports
This commit is contained in:
parent
2ad951e172
commit
9e7760d80a
|
|
@ -148,6 +148,7 @@ func (c *Cluster) setProcessName(procName string, args ...interface{}) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetStatus of Postgres cluster
|
||||||
func (c *Cluster) SetStatus(status string) {
|
func (c *Cluster) SetStatus(status string) {
|
||||||
// TODO: eventually switch to updateStatus() for kubernetes 1.11 and above
|
// TODO: eventually switch to updateStatus() for kubernetes 1.11 and above
|
||||||
patch, err := json.Marshal(acidv1.PostgresStatus{PostgresClusterStatus: status})
|
patch, err := json.Marshal(acidv1.PostgresStatus{PostgresClusterStatus: status})
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ var cl = New(
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
k8sutil.KubernetesClient{},
|
k8sutil.NewMockKubernetesClient(),
|
||||||
acidv1.Postgresql{},
|
acidv1.Postgresql{},
|
||||||
logger,
|
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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,82 +7,23 @@ import (
|
||||||
|
|
||||||
b64 "encoding/base64"
|
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/spec"
|
||||||
"github.com/zalando/postgres-operator/pkg/util/k8sutil"
|
"github.com/zalando/postgres-operator/pkg/util/k8sutil"
|
||||||
"k8s.io/api/core/v1"
|
"k8s.io/api/core/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
v1core "k8s.io/client-go/kubernetes/typed/core/v1"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
testInfrastructureRolesSecretName = "infrastructureroles-test"
|
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 {
|
func newMockController() *Controller {
|
||||||
controller := NewController(&spec.ControllerConfig{})
|
controller := NewController(&spec.ControllerConfig{})
|
||||||
controller.opConfig.ClusterNameLabel = "cluster-name"
|
controller.opConfig.ClusterNameLabel = "cluster-name"
|
||||||
controller.opConfig.InfrastructureRolesSecretName =
|
controller.opConfig.InfrastructureRolesSecretName =
|
||||||
spec.NamespacedName{Namespace: v1.NamespaceDefault, Name: testInfrastructureRolesSecretName}
|
spec.NamespacedName{Namespace: v1.NamespaceDefault, Name: testInfrastructureRolesSecretName}
|
||||||
controller.opConfig.Workers = 4
|
controller.opConfig.Workers = 4
|
||||||
controller.KubeClient = newMockKubernetesClient()
|
controller.KubeClient = k8sutil.NewMockKubernetesClient()
|
||||||
return controller
|
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())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,10 @@ package k8sutil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
|
b64 "encoding/base64"
|
||||||
|
|
||||||
"github.com/zalando/postgres-operator/pkg/util/constants"
|
"github.com/zalando/postgres-operator/pkg/util/constants"
|
||||||
"k8s.io/api/core/v1"
|
"k8s.io/api/core/v1"
|
||||||
policybeta1 "k8s.io/api/policy/v1beta1"
|
policybeta1 "k8s.io/api/policy/v1beta1"
|
||||||
|
|
@ -15,9 +19,9 @@ import (
|
||||||
rbacv1beta1 "k8s.io/client-go/kubernetes/typed/rbac/v1beta1"
|
rbacv1beta1 "k8s.io/client-go/kubernetes/typed/rbac/v1beta1"
|
||||||
"k8s.io/client-go/rest"
|
"k8s.io/client-go/rest"
|
||||||
"k8s.io/client-go/tools/clientcmd"
|
"k8s.io/client-go/tools/clientcmd"
|
||||||
"reflect"
|
|
||||||
|
|
||||||
acidv1client "github.com/zalando/postgres-operator/pkg/generated/clientset/versioned"
|
acidv1client "github.com/zalando/postgres-operator/pkg/generated/clientset/versioned"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
// KubernetesClient describes getters for Kubernetes objects
|
// KubernetesClient describes getters for Kubernetes objects
|
||||||
|
|
@ -41,6 +45,20 @@ type KubernetesClient struct {
|
||||||
AcidV1ClientSet *acidv1client.Clientset
|
AcidV1ClientSet *acidv1client.Clientset
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type mockSecret struct {
|
||||||
|
v1core.SecretInterface
|
||||||
|
}
|
||||||
|
|
||||||
|
type MockSecretGetter struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
type mockConfigMap struct {
|
||||||
|
v1core.ConfigMapInterface
|
||||||
|
}
|
||||||
|
|
||||||
|
type MockConfigMapsGetter struct {
|
||||||
|
}
|
||||||
|
|
||||||
// RestConfig creates REST config
|
// RestConfig creates REST config
|
||||||
func RestConfig(kubeConfig string, outOfCluster bool) (*rest.Config, error) {
|
func RestConfig(kubeConfig string, outOfCluster bool) (*rest.Config, error) {
|
||||||
if outOfCluster {
|
if outOfCluster {
|
||||||
|
|
@ -140,3 +158,49 @@ func SamePDB(cur, new *policybeta1.PodDisruptionBudget) (match bool, reason stri
|
||||||
|
|
||||||
return
|
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{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue