6
6
"strings"
7
7
"testing"
8
8
9
- "github.com/stretchr/testify/assert"
10
-
11
9
"github.com/sirupsen/logrus"
10
+ "github.com/stretchr/testify/assert"
12
11
acidv1 "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1"
13
12
fakeacidv1 "github.com/zalando/postgres-operator/pkg/generated/clientset/versioned/fake"
14
13
"github.com/zalando/postgres-operator/pkg/spec"
@@ -61,7 +60,6 @@ var cl = New(
61
60
)
62
61
63
62
func TestStatefulSetAnnotations (t * testing.T ) {
64
- testName := "CheckStatefulsetAnnotations"
65
63
spec := acidv1.PostgresSpec {
66
64
TeamID : "myapp" , NumberOfInstances : 1 ,
67
65
Resources : & acidv1.Resources {
@@ -74,19 +72,59 @@ func TestStatefulSetAnnotations(t *testing.T) {
74
72
}
75
73
ss , err := cl .generateStatefulSet (& spec )
76
74
if err != nil {
77
- t .Errorf ("in %s no statefulset created %v" , testName , err )
75
+ t .Errorf ("in %s no statefulset created %v" , t . Name () , err )
78
76
}
79
77
if ss != nil {
80
78
annotation := ss .ObjectMeta .GetAnnotations ()
81
79
if _ , ok := annotation ["downscaler/downtime_replicas" ]; ! ok {
82
- t .Errorf ("in %s respective annotation not found on sts" , testName )
80
+ t .Errorf ("in %s respective annotation not found on sts" , t . Name () )
83
81
}
84
82
}
83
+ }
84
+
85
+ func TestStatefulSetUpdateWithEnv (t * testing.T ) {
86
+ oldSpec := & acidv1.PostgresSpec {
87
+ TeamID : "myapp" , NumberOfInstances : 1 ,
88
+ Resources : & acidv1.Resources {
89
+ ResourceRequests : acidv1.ResourceDescription {CPU : "1" , Memory : "10" },
90
+ ResourceLimits : acidv1.ResourceDescription {CPU : "1" , Memory : "10" },
91
+ },
92
+ Volume : acidv1.Volume {
93
+ Size : "1G" ,
94
+ },
95
+ }
96
+ oldSS , err := cl .generateStatefulSet (oldSpec )
97
+ if err != nil {
98
+ t .Errorf ("in %s no StatefulSet created %v" , t .Name (), err )
99
+ }
100
+
101
+ newSpec := oldSpec .DeepCopy ()
102
+ newSS , err := cl .generateStatefulSet (newSpec )
103
+ if err != nil {
104
+ t .Errorf ("in %s no StatefulSet created %v" , t .Name (), err )
105
+ }
85
106
107
+ if ! reflect .DeepEqual (oldSS , newSS ) {
108
+ t .Errorf ("in %s StatefulSet's must be equal" , t .Name ())
109
+ }
110
+
111
+ newSpec .Env = []v1.EnvVar {
112
+ {
113
+ Name : "CUSTOM_ENV_VARIABLE" ,
114
+ Value : "data" ,
115
+ },
116
+ }
117
+ newSS , err = cl .generateStatefulSet (newSpec )
118
+ if err != nil {
119
+ t .Errorf ("in %s no StatefulSet created %v" , t .Name (), err )
120
+ }
121
+
122
+ if reflect .DeepEqual (oldSS , newSS ) {
123
+ t .Errorf ("in %s StatefulSet's must be not equal" , t .Name ())
124
+ }
86
125
}
87
126
88
127
func TestInitRobotUsers (t * testing.T ) {
89
- testName := "TestInitRobotUsers"
90
128
tests := []struct {
91
129
manifestUsers map [string ]acidv1.UserFlags
92
130
infraRoles map [string ]spec.PgUser
@@ -130,22 +168,20 @@ func TestInitRobotUsers(t *testing.T) {
130
168
cl .pgUsers = tt .infraRoles
131
169
if err := cl .initRobotUsers (); err != nil {
132
170
if tt .err == nil {
133
- t .Errorf ("%s got an unexpected error: %v" , testName , err )
171
+ t .Errorf ("%s got an unexpected error: %v" , t . Name () , err )
134
172
}
135
173
if err .Error () != tt .err .Error () {
136
- t .Errorf ("%s expected error %v, got %v" , testName , tt .err , err )
174
+ t .Errorf ("%s expected error %v, got %v" , t . Name () , tt .err , err )
137
175
}
138
176
} else {
139
177
if ! reflect .DeepEqual (cl .pgUsers , tt .result ) {
140
- t .Errorf ("%s expected: %#v, got %#v" , testName , tt .result , cl .pgUsers )
178
+ t .Errorf ("%s expected: %#v, got %#v" , t . Name () , tt .result , cl .pgUsers )
141
179
}
142
180
}
143
181
}
144
182
}
145
183
146
184
func TestInitAdditionalOwnerRoles (t * testing.T ) {
147
- testName := "TestInitAdditionalOwnerRoles"
148
-
149
185
manifestUsers := map [string ]acidv1.UserFlags {"foo_owner" : {}, "bar_owner" : {}, "app_user" : {}}
150
186
expectedUsers := map [string ]spec.PgUser {
151
187
"foo_owner" : {Origin : spec .RoleOriginManifest , Name : "foo_owner" , Namespace : cl .Namespace , Password : "f123" , Flags : []string {"LOGIN" }, IsDbOwner : true , MemberOf : []string {"cron_admin" , "part_man" }},
@@ -158,7 +194,7 @@ func TestInitAdditionalOwnerRoles(t *testing.T) {
158
194
159
195
// this should set IsDbOwner field for manifest users
160
196
if err := cl .initRobotUsers (); err != nil {
161
- t .Errorf ("%s could not init manifest users" , testName )
197
+ t .Errorf ("%s could not init manifest users" , t . Name () )
162
198
}
163
199
164
200
// now assign additional roles to owners
@@ -169,7 +205,7 @@ func TestInitAdditionalOwnerRoles(t *testing.T) {
169
205
expectedPgUser := expectedUsers [username ]
170
206
if ! util .IsEqualIgnoreOrder (expectedPgUser .MemberOf , existingPgUser .MemberOf ) {
171
207
t .Errorf ("%s unexpected membership of user %q: expected member of %#v, got member of %#v" ,
172
- testName , username , expectedPgUser .MemberOf , existingPgUser .MemberOf )
208
+ t . Name () , username , expectedPgUser .MemberOf , existingPgUser .MemberOf )
173
209
}
174
210
}
175
211
}
@@ -195,11 +231,9 @@ func (m *mockTeamsAPIClient) setMembers(members []string) {
195
231
196
232
// Test adding a member of a product team owning a particular DB cluster
197
233
func TestInitHumanUsers (t * testing.T ) {
198
-
199
234
var mockTeamsAPI mockTeamsAPIClient
200
235
cl .oauthTokenGetter = & mockOAuthTokenGetter {}
201
236
cl .teamsAPIClient = & mockTeamsAPI
202
- testName := "TestInitHumanUsers"
203
237
204
238
// members of a product team are granted superuser rights for DBs of their team
205
239
cl .OpConfig .EnableTeamSuperuser = true
@@ -232,11 +266,11 @@ func TestInitHumanUsers(t *testing.T) {
232
266
cl .pgUsers = tt .existingRoles
233
267
mockTeamsAPI .setMembers (tt .teamRoles )
234
268
if err := cl .initHumanUsers (); err != nil {
235
- t .Errorf ("%s got an unexpected error %v" , testName , err )
269
+ t .Errorf ("%s got an unexpected error %v" , t . Name () , err )
236
270
}
237
271
238
272
if ! reflect .DeepEqual (cl .pgUsers , tt .result ) {
239
- t .Errorf ("%s expects %#v, got %#v" , testName , tt .result , cl .pgUsers )
273
+ t .Errorf ("%s expects %#v, got %#v" , t . Name () , tt .result , cl .pgUsers )
240
274
}
241
275
}
242
276
}
@@ -264,12 +298,10 @@ func (m *mockTeamsAPIClientMultipleTeams) TeamInfo(teamID, token string) (tm *te
264
298
265
299
// Test adding members of maintenance teams that get superuser rights for all PG databases
266
300
func TestInitHumanUsersWithSuperuserTeams (t * testing.T ) {
267
-
268
301
var mockTeamsAPI mockTeamsAPIClientMultipleTeams
269
302
cl .oauthTokenGetter = & mockOAuthTokenGetter {}
270
303
cl .teamsAPIClient = & mockTeamsAPI
271
304
cl .OpConfig .EnableTeamSuperuser = false
272
- testName := "TestInitHumanUsersWithSuperuserTeams"
273
305
274
306
cl .OpConfig .EnableTeamsAPI = true
275
307
cl .OpConfig .PamRoleName = "zalandos"
@@ -371,17 +403,16 @@ func TestInitHumanUsersWithSuperuserTeams(t *testing.T) {
371
403
cl .OpConfig .PostgresSuperuserTeams = tt .superuserTeams
372
404
373
405
if err := cl .initHumanUsers (); err != nil {
374
- t .Errorf ("%s got an unexpected error %v" , testName , err )
406
+ t .Errorf ("%s got an unexpected error %v" , t . Name () , err )
375
407
}
376
408
377
409
if ! reflect .DeepEqual (cl .pgUsers , tt .result ) {
378
- t .Errorf ("%s expects %#v, got %#v" , testName , tt .result , cl .pgUsers )
410
+ t .Errorf ("%s expects %#v, got %#v" , t . Name () , tt .result , cl .pgUsers )
379
411
}
380
412
}
381
413
}
382
414
383
415
func TestPodAnnotations (t * testing.T ) {
384
- testName := "TestPodAnnotations"
385
416
tests := []struct {
386
417
subTest string
387
418
operator map [string ]string
@@ -428,13 +459,13 @@ func TestPodAnnotations(t *testing.T) {
428
459
for k , v := range annotations {
429
460
if observed , expected := v , tt .merged [k ]; observed != expected {
430
461
t .Errorf ("%v expects annotation value %v for key %v, but found %v" ,
431
- testName + "/" + tt .subTest , expected , observed , k )
462
+ t . Name () + "/" + tt .subTest , expected , observed , k )
432
463
}
433
464
}
434
465
for k , v := range tt .merged {
435
466
if observed , expected := annotations [k ], v ; observed != expected {
436
467
t .Errorf ("%v expects annotation value %v for key %v, but found %v" ,
437
- testName + "/" + tt .subTest , expected , observed , k )
468
+ t . Name () + "/" + tt .subTest , expected , observed , k )
438
469
}
439
470
}
440
471
}
@@ -780,22 +811,20 @@ func TestServiceAnnotations(t *testing.T) {
780
811
}
781
812
782
813
func TestInitSystemUsers (t * testing.T ) {
783
- testName := "Test system users initialization"
784
-
785
814
// default cluster without connection pooler and event streams
786
815
cl .initSystemUsers ()
787
816
if _ , exist := cl .systemUsers [constants .ConnectionPoolerUserKeyName ]; exist {
788
- t .Errorf ("%s, connection pooler user is present" , testName )
817
+ t .Errorf ("%s, connection pooler user is present" , t . Name () )
789
818
}
790
819
if _ , exist := cl .systemUsers [constants .EventStreamUserKeyName ]; exist {
791
- t .Errorf ("%s, stream user is present" , testName )
820
+ t .Errorf ("%s, stream user is present" , t . Name () )
792
821
}
793
822
794
823
// cluster with connection pooler
795
824
cl .Spec .EnableConnectionPooler = boolToPointer (true )
796
825
cl .initSystemUsers ()
797
826
if _ , exist := cl .systemUsers [constants .ConnectionPoolerUserKeyName ]; ! exist {
798
- t .Errorf ("%s, connection pooler user is not present" , testName )
827
+ t .Errorf ("%s, connection pooler user is not present" , t . Name () )
799
828
}
800
829
801
830
// superuser is not allowed as connection pool user
@@ -807,7 +836,7 @@ func TestInitSystemUsers(t *testing.T) {
807
836
808
837
cl .initSystemUsers ()
809
838
if _ , exist := cl .systemUsers ["pooler" ]; ! exist {
810
- t .Errorf ("%s, Superuser is not allowed to be a connection pool user" , testName )
839
+ t .Errorf ("%s, Superuser is not allowed to be a connection pool user" , t . Name () )
811
840
}
812
841
813
842
// neither protected users are
@@ -819,7 +848,7 @@ func TestInitSystemUsers(t *testing.T) {
819
848
820
849
cl .initSystemUsers ()
821
850
if _ , exist := cl .systemUsers ["pooler" ]; ! exist {
822
- t .Errorf ("%s, Protected user are not allowed to be a connection pool user" , testName )
851
+ t .Errorf ("%s, Protected user are not allowed to be a connection pool user" , t . Name () )
823
852
}
824
853
825
854
delete (cl .systemUsers , "pooler" )
@@ -829,15 +858,15 @@ func TestInitSystemUsers(t *testing.T) {
829
858
830
859
cl .initSystemUsers ()
831
860
if _ , exist := cl .systemUsers ["pooler" ]; ! exist {
832
- t .Errorf ("%s, System users are not allowed to be a connection pool user" , testName )
861
+ t .Errorf ("%s, System users are not allowed to be a connection pool user" , t . Name () )
833
862
}
834
863
835
864
// using stream user in manifest but no streams defined should be treated like normal robot user
836
865
streamUser := fmt .Sprintf ("%s%s" , constants .EventStreamSourceSlotPrefix , constants .UserRoleNameSuffix )
837
866
cl .Spec .Users = map [string ]acidv1.UserFlags {streamUser : []string {}}
838
867
cl .initSystemUsers ()
839
868
if _ , exist := cl .systemUsers [constants .EventStreamUserKeyName ]; exist {
840
- t .Errorf ("%s, stream user is present" , testName )
869
+ t .Errorf ("%s, stream user is present" , t . Name () )
841
870
}
842
871
843
872
// cluster with streams
@@ -846,32 +875,30 @@ func TestInitSystemUsers(t *testing.T) {
846
875
ApplicationId : "test-app" ,
847
876
Database : "test_db" ,
848
877
Tables : map [string ]acidv1.StreamTable {
849
- "data.test_table" : acidv1. StreamTable {
878
+ "data.test_table" : {
850
879
EventType : "test_event" ,
851
880
},
852
881
},
853
882
},
854
883
}
855
884
cl .initSystemUsers ()
856
885
if _ , exist := cl .systemUsers [constants .EventStreamUserKeyName ]; ! exist {
857
- t .Errorf ("%s, stream user is not present" , testName )
886
+ t .Errorf ("%s, stream user is not present" , t . Name () )
858
887
}
859
888
}
860
889
861
890
func TestPreparedDatabases (t * testing.T ) {
862
- testName := "TestDefaultPreparedDatabase"
863
-
864
891
cl .Spec .PreparedDatabases = map [string ]acidv1.PreparedDatabase {}
865
892
cl .initPreparedDatabaseRoles ()
866
893
867
894
for _ , role := range []string {"acid_test_owner" , "acid_test_reader" , "acid_test_writer" ,
868
895
"acid_test_data_owner" , "acid_test_data_reader" , "acid_test_data_writer" } {
869
896
if _ , exist := cl .pgUsers [role ]; ! exist {
870
- t .Errorf ("%s, default role %q for prepared database not present" , testName , role )
897
+ t .Errorf ("%s, default role %q for prepared database not present" , t . Name () , role )
871
898
}
872
899
}
873
900
874
- testName = "TestPreparedDatabaseWithSchema"
901
+ testName : = "TestPreparedDatabaseWithSchema"
875
902
876
903
cl .Spec .PreparedDatabases = map [string ]acidv1.PreparedDatabase {
877
904
"foo" : {
@@ -1109,7 +1136,6 @@ func newService(ann map[string]string, svcT v1.ServiceType, lbSr []string) *v1.S
1109
1136
}
1110
1137
1111
1138
func TestCompareServices (t * testing.T ) {
1112
- testName := "TestCompareServices"
1113
1139
cluster := Cluster {
1114
1140
Config : Config {
1115
1141
OpConfig : config.Config {
@@ -1410,16 +1436,16 @@ func TestCompareServices(t *testing.T) {
1410
1436
match , reason := cluster .compareServices (tt .current , tt .new )
1411
1437
if match && ! tt .match {
1412
1438
t .Logf ("match=%v current=%v, old=%v reason=%s" , match , tt .current .Annotations , tt .new .Annotations , reason )
1413
- t .Errorf ("%s - expected services to do not match: %q and %q" , testName , tt .current , tt .new )
1439
+ t .Errorf ("%s - expected services to do not match: %q and %q" , t . Name () , tt .current , tt .new )
1414
1440
return
1415
1441
}
1416
1442
if ! match && tt .match {
1417
- t .Errorf ("%s - expected services to be the same: %q and %q" , testName , tt .current , tt .new )
1443
+ t .Errorf ("%s - expected services to be the same: %q and %q" , t . Name () , tt .current , tt .new )
1418
1444
return
1419
1445
}
1420
1446
if ! match && ! tt .match {
1421
1447
if ! strings .HasPrefix (reason , tt .reason ) {
1422
- t .Errorf ("%s - expected reason prefix %s, found %s" , testName , tt .reason , reason )
1448
+ t .Errorf ("%s - expected reason prefix %s, found %s" , t . Name () , tt .reason , reason )
1423
1449
return
1424
1450
}
1425
1451
}
0 commit comments