add unit tests
This commit is contained in:
parent
901f4108c4
commit
e8a4495de4
|
|
@ -13,6 +13,7 @@ import (
|
||||||
"github.com/zalando/postgres-operator/pkg/util/k8sutil"
|
"github.com/zalando/postgres-operator/pkg/util/k8sutil"
|
||||||
"github.com/zalando/postgres-operator/pkg/util/teams"
|
"github.com/zalando/postgres-operator/pkg/util/teams"
|
||||||
v1 "k8s.io/api/core/v1"
|
v1 "k8s.io/api/core/v1"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -32,7 +33,7 @@ var cl = New(
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
k8sutil.NewMockKubernetesClient(),
|
k8sutil.NewMockKubernetesClient(),
|
||||||
acidv1.Postgresql{},
|
acidv1.Postgresql{ObjectMeta: metav1.ObjectMeta{Name: "acid-test", Namespace: "test"}},
|
||||||
logger,
|
logger,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -756,3 +757,89 @@ func TestInitSystemUsers(t *testing.T) {
|
||||||
t.Errorf("%s, System users are not allowed to be a connection pool user", testName)
|
t.Errorf("%s, System users are not allowed to be a connection pool user", testName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPreparedDatabases(t *testing.T) {
|
||||||
|
testName := "TestDefaultPreparedDatabase"
|
||||||
|
|
||||||
|
cl.Spec.PreparedDatabases = map[string]acidv1.PreparedDatabase{}
|
||||||
|
cl.initPreparedDatabaseRoles()
|
||||||
|
|
||||||
|
for _, role := range []string{"acid_test_owner", "acid_test_reader", "acid_test_writer",
|
||||||
|
"acid_test_data_owner", "acid_test_data_reader", "acid_test_data_writer"} {
|
||||||
|
if _, exist := cl.pgUsers[role]; !exist {
|
||||||
|
t.Errorf("%s, default role %q for prepared database not present", testName, role)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
testName = "TestPreparedDatabaseWithSchema"
|
||||||
|
|
||||||
|
cl.Spec.PreparedDatabases = map[string]acidv1.PreparedDatabase{
|
||||||
|
"foo": {
|
||||||
|
DefaultUsers: true,
|
||||||
|
PreparedSchemas: map[string]acidv1.PreparedSchema{
|
||||||
|
"bar": {
|
||||||
|
DefaultUsers: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
cl.initPreparedDatabaseRoles()
|
||||||
|
|
||||||
|
for _, role := range []string{
|
||||||
|
"foo_owner", "foo_reader", "foo_writer",
|
||||||
|
"foo_owner_user", "foo_reader_user", "foo_writer_user",
|
||||||
|
"foo_bar_owner", "foo_bar_reader", "foo_bar_writer",
|
||||||
|
"foo_bar_owner_user", "foo_bar_reader_user", "foo_bar_writer_user"} {
|
||||||
|
if _, exist := cl.pgUsers[role]; !exist {
|
||||||
|
t.Errorf("%s, default role %q for prepared database not present", testName, role)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
roleTests := []struct {
|
||||||
|
subTest string
|
||||||
|
role string
|
||||||
|
memberOf string
|
||||||
|
admin string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
subTest: "Test admin role of owner",
|
||||||
|
role: "foo_owner",
|
||||||
|
memberOf: "",
|
||||||
|
admin: "admin",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
subTest: "Test writer is a member of reader",
|
||||||
|
role: "foo_writer",
|
||||||
|
memberOf: "foo_reader",
|
||||||
|
admin: "foo_owner",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
subTest: "Test reader LOGIN role",
|
||||||
|
role: "foo_reader_user",
|
||||||
|
memberOf: "foo_reader",
|
||||||
|
admin: "foo_owner",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
subTest: "Test schema owner",
|
||||||
|
role: "foo_bar_owner",
|
||||||
|
memberOf: "",
|
||||||
|
admin: "foo_owner",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
subTest: "Test schema writer LOGIN role",
|
||||||
|
role: "foo_bar_writer_user",
|
||||||
|
memberOf: "foo_bar_writer",
|
||||||
|
admin: "foo_bar_owner",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range roleTests {
|
||||||
|
user := cl.pgUsers[tt.role]
|
||||||
|
if (tt.memberOf == "" && len(user.MemberOf) > 0) || (tt.memberOf != "" && user.MemberOf[0] != tt.memberOf) {
|
||||||
|
t.Errorf("%s, incorrect membership for default role %q. Expected %q, got %q", tt.subTest, tt.role, tt.memberOf, user.MemberOf[0])
|
||||||
|
}
|
||||||
|
if user.AdminRole != tt.admin {
|
||||||
|
t.Errorf("%s, incorrect admin role for default role %q. Expected %q, got %q", tt.subTest, tt.role, tt.admin, user.AdminRole)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -160,9 +160,9 @@ func (c *Cluster) closeDbConn() (err error) {
|
||||||
c.logger.Debug("closing database connection")
|
c.logger.Debug("closing database connection")
|
||||||
if err = c.pgDb.Close(); err != nil {
|
if err = c.pgDb.Close(); err != nil {
|
||||||
c.logger.Errorf("could not close database connection: %v", err)
|
c.logger.Errorf("could not close database connection: %v", err)
|
||||||
} else {
|
|
||||||
c.pgDb = nil
|
|
||||||
}
|
}
|
||||||
|
c.pgDb = nil
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
c.logger.Warning("attempted to close an empty db connection object")
|
c.logger.Warning("attempted to close an empty db connection object")
|
||||||
|
|
@ -488,7 +488,7 @@ func (c *Cluster) installLookupFunction(poolerSchema, poolerUser string) error {
|
||||||
|
|
||||||
templater := template.Must(template.New("sql").Parse(connectionPoolerLookup))
|
templater := template.Must(template.New("sql").Parse(connectionPoolerLookup))
|
||||||
|
|
||||||
for dbname, _ := range currentDatabases {
|
for dbname := range currentDatabases {
|
||||||
if dbname == "template0" || dbname == "template1" {
|
if dbname == "template0" || dbname == "template1" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -553,10 +553,10 @@ func (c *Cluster) syncDatabases() error {
|
||||||
preparedDatabases = map[string]acidv1.PreparedDatabase{strings.Replace(c.Name, "-", "_", -1): {}}
|
preparedDatabases = map[string]acidv1.PreparedDatabase{strings.Replace(c.Name, "-", "_", -1): {}}
|
||||||
c.Spec.PreparedDatabases = preparedDatabases
|
c.Spec.PreparedDatabases = preparedDatabases
|
||||||
}
|
}
|
||||||
for preparedDatname := range c.Spec.PreparedDatabases {
|
for preparedDatabaseName := range c.Spec.PreparedDatabases {
|
||||||
_, exists := currentDatabases[preparedDatname]
|
_, exists := currentDatabases[preparedDatabaseName]
|
||||||
if !exists {
|
if !exists {
|
||||||
createDatabases[preparedDatname] = preparedDatname + constants.OwnerRoleNameSuffix
|
createDatabases[preparedDatabaseName] = preparedDatabaseName + constants.OwnerRoleNameSuffix
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue