fix unit tests and return type for getInfrastructureRoles

This commit is contained in:
Felix Kunde 2021-11-24 13:41:38 +01:00
parent 4bbf1e9d24
commit d580684acb
2 changed files with 16 additions and 50 deletions

View File

@ -200,7 +200,7 @@ func (c *Controller) getInfrastructureRoles(
errors := make([]string, 0) errors := make([]string, 0)
noRolesProvided := true noRolesProvided := true
roles := []spec.PgUser{} roles := []spec.PgUser{}
uniqRoles := map[string]spec.PgUser{} uniqRoles := make(map[string]spec.PgUser)
// To be compatible with the legacy implementation we need to return nil if // To be compatible with the legacy implementation we need to return nil if
// the provided secret name is empty. The equivalent situation in the // the provided secret name is empty. The equivalent situation in the
@ -213,7 +213,7 @@ func (c *Controller) getInfrastructureRoles(
} }
if noRolesProvided { if noRolesProvided {
return nil, nil return uniqRoles, nil
} }
for _, secret := range rolesSecrets { for _, secret := range rolesSecrets {
@ -242,7 +242,7 @@ func (c *Controller) getInfrastructureRoles(
} }
if len(errors) > 0 { if len(errors) > 0 {
return nil, fmt.Errorf(strings.Join(errors, `', '`)) return uniqRoles, fmt.Errorf(strings.Join(errors, `', '`))
} }
return uniqRoles, nil return uniqRoles, nil

View File

@ -7,6 +7,7 @@ import (
b64 "encoding/base64" b64 "encoding/base64"
"github.com/stretchr/testify/assert"
"github.com/zalando/postgres-operator/pkg/spec" "github.com/zalando/postgres-operator/pkg/spec"
"github.com/zalando/postgres-operator/pkg/util/config" "github.com/zalando/postgres-operator/pkg/util/config"
"github.com/zalando/postgres-operator/pkg/util/k8sutil" "github.com/zalando/postgres-operator/pkg/util/k8sutil"
@ -90,21 +91,21 @@ func TestClusterWorkerID(t *testing.T) {
// not exist, or empty) and the old format. // not exist, or empty) and the old format.
func TestOldInfrastructureRoleFormat(t *testing.T) { func TestOldInfrastructureRoleFormat(t *testing.T) {
var testTable = []struct { var testTable = []struct {
secretName spec.NamespacedName secretName spec.NamespacedName
expectedRoles map[string]spec.PgUser expectedRoles map[string]spec.PgUser
expectedErrors []error expectedError error
}{ }{
{ {
// empty secret name // empty secret name
spec.NamespacedName{}, spec.NamespacedName{},
nil, map[string]spec.PgUser{},
nil, nil,
}, },
{ {
// secret does not exist // secret does not exist
spec.NamespacedName{Namespace: v1.NamespaceDefault, Name: "null"}, spec.NamespacedName{Namespace: v1.NamespaceDefault, Name: "null"},
map[string]spec.PgUser{}, map[string]spec.PgUser{},
[]error{fmt.Errorf(`could not get infrastructure roles secret default/null: NotFound`)}, fmt.Errorf(`could not get infrastructure roles secret default/null: NotFound`),
}, },
{ {
spec.NamespacedName{ spec.NamespacedName{
@ -129,7 +130,7 @@ func TestOldInfrastructureRoleFormat(t *testing.T) {
}, },
} }
for _, test := range testTable { for _, test := range testTable {
roles, errors := utilTestController.getInfrastructureRoles( roles, err := utilTestController.getInfrastructureRoles(
[]*config.InfrastructureRole{ []*config.InfrastructureRole{
&config.InfrastructureRole{ &config.InfrastructureRole{
SecretName: test.secretName, SecretName: test.secretName,
@ -140,22 +141,9 @@ func TestOldInfrastructureRoleFormat(t *testing.T) {
}, },
}) })
if len(errors) != len(test.expectedErrors) { if err != nil && err.Error() != test.expectedError.Error() {
t.Errorf("expected error '%v' does not match the actual error '%v'", t.Errorf("expected error '%v' does not match the actual error '%v'",
test.expectedErrors, errors) test.expectedError, err)
}
for idx := range errors {
err := errors[idx]
expectedErr := test.expectedErrors[idx]
if err != expectedErr {
if err != nil && expectedErr != nil && err.Error() == expectedErr.Error() {
continue
}
t.Errorf("expected error '%v' does not match the actual error '%v'",
expectedErr, err)
}
} }
if !reflect.DeepEqual(roles, test.expectedRoles) { if !reflect.DeepEqual(roles, test.expectedRoles) {
@ -169,9 +157,8 @@ func TestOldInfrastructureRoleFormat(t *testing.T) {
// corresponding secrets. Here we test the new format. // corresponding secrets. Here we test the new format.
func TestNewInfrastructureRoleFormat(t *testing.T) { func TestNewInfrastructureRoleFormat(t *testing.T) {
var testTable = []struct { var testTable = []struct {
secrets []spec.NamespacedName secrets []spec.NamespacedName
expectedRoles map[string]spec.PgUser expectedRoles map[string]spec.PgUser
expectedErrors []error
}{ }{
// one secret with one configmap // one secret with one configmap
{ {
@ -196,7 +183,6 @@ func TestNewInfrastructureRoleFormat(t *testing.T) {
Flags: []string{"createdb"}, Flags: []string{"createdb"},
}, },
}, },
nil,
}, },
// multiple standalone secrets // multiple standalone secrets
{ {
@ -224,7 +210,6 @@ func TestNewInfrastructureRoleFormat(t *testing.T) {
MemberOf: []string{"new-test-inrole2"}, MemberOf: []string{"new-test-inrole2"},
}, },
}, },
nil,
}, },
} }
for _, test := range testTable { for _, test := range testTable {
@ -239,27 +224,8 @@ func TestNewInfrastructureRoleFormat(t *testing.T) {
}) })
} }
roles, errors := utilTestController.getInfrastructureRoles(definitions) roles, err := utilTestController.getInfrastructureRoles(definitions)
if len(errors) != len(test.expectedErrors) { assert.NoError(t, err)
t.Errorf("expected error does not match the actual error:\n%+v\n%+v",
test.expectedErrors, errors)
// Stop and do not do any further checks
return
}
for idx := range errors {
err := errors[idx]
expectedErr := test.expectedErrors[idx]
if err != expectedErr {
if err != nil && expectedErr != nil && err.Error() == expectedErr.Error() {
continue
}
t.Errorf("expected error '%v' does not match the actual error '%v'",
expectedErr, err)
}
}
if !reflect.DeepEqual(roles, test.expectedRoles) { if !reflect.DeepEqual(roles, test.expectedRoles) {
t.Errorf("expected roles output/the actual:\n%#v\n%#v", t.Errorf("expected roles output/the actual:\n%#v\n%#v",