@@ -3,11 +3,12 @@ package cluster
33import (
44 "k8s.io/api/core/v1"
55
6+ "testing"
7+
68 acidv1 "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1"
79 "github.com/zalando/postgres-operator/pkg/util/config"
810 "github.com/zalando/postgres-operator/pkg/util/constants"
911 "github.com/zalando/postgres-operator/pkg/util/k8sutil"
10- "testing"
1112)
1213
1314func True () * bool {
@@ -20,6 +21,69 @@ func False() *bool {
2021 return & b
2122}
2223
24+ func TestGenerateSpiloJSONConfiguration (t * testing.T ) {
25+ var cluster = New (
26+ Config {
27+ OpConfig : config.Config {
28+ ProtectedRoles : []string {"admin" },
29+ Auth : config.Auth {
30+ SuperUsername : superUserName ,
31+ ReplicationUsername : replicationUserName ,
32+ },
33+ },
34+ }, k8sutil.KubernetesClient {}, acidv1.Postgresql {}, logger )
35+
36+ testName := "TestGenerateSpiloConfig"
37+ tests := []struct {
38+ subtest string
39+ pgParam * acidv1.PostgresqlParam
40+ patroni * acidv1.Patroni
41+ role string
42+ opConfig config.Config
43+ result string
44+ }{
45+ {
46+ subtest : "Patroni default configuration" ,
47+ pgParam : & acidv1.PostgresqlParam {PgVersion : "9.6" },
48+ patroni : & acidv1.Patroni {},
49+ role : "zalandos" ,
50+ opConfig : config.Config {},
51+ result : `{"postgresql":{"bin_dir":"/usr/lib/postgresql/9.6/bin"},"bootstrap":{"initdb":[{"auth-host":"md5"},{"auth-local":"trust"}],"users":{"zalandos":{"password":"","options":["CREATEDB","NOLOGIN"]}},"dcs":{}}}` ,
52+ },
53+ {
54+ subtest : "Patroni configured" ,
55+ pgParam : & acidv1.PostgresqlParam {PgVersion : "11" },
56+ patroni : & acidv1.Patroni {
57+ InitDB : map [string ]string {
58+ "encoding" : "UTF8" ,
59+ "locale" : "en_US.UTF-8" ,
60+ "data-checksums" : "true" ,
61+ },
62+ PgHba : []string {"hostssl all all 0.0.0.0/0 md5" , "host all all 0.0.0.0/0 md5" },
63+ TTL : 30 ,
64+ LoopWait : 10 ,
65+ RetryTimeout : 10 ,
66+ MaximumLagOnFailover : 33554432 ,
67+ Slots : map [string ]map [string ]string {"permanent_logical_1" : {"type" : "logical" , "database" : "foo" , "plugin" : "pgoutput" }},
68+ },
69+ role : "zalandos" ,
70+ opConfig : config.Config {},
71+ result : `{"postgresql":{"bin_dir":"/usr/lib/postgresql/11/bin","pg_hba":["hostssl all all 0.0.0.0/0 md5","host all all 0.0.0.0/0 md5"]},"bootstrap":{"initdb":[{"auth-host":"md5"},{"auth-local":"trust"},"data-checksums",{"encoding":"UTF8"},{"locale":"en_US.UTF-8"}],"users":{"zalandos":{"password":"","options":["CREATEDB","NOLOGIN"]}},"dcs":{"ttl":30,"loop_wait":10,"retry_timeout":10,"maximum_lag_on_failover":33554432,"slots":{"permanent_logical_1":{"database":"foo","plugin":"pgoutput","type":"logical"}}}}}` ,
72+ },
73+ }
74+ for _ , tt := range tests {
75+ cluster .OpConfig = tt .opConfig
76+ result , err := generateSpiloJSONConfiguration (tt .pgParam , tt .patroni , tt .role , logger )
77+ if err != nil {
78+ t .Errorf ("Unexpected error: %v" , err )
79+ }
80+ if tt .result != result {
81+ t .Errorf ("%s %s: Spilo Config is %v, expected %v for role %#v and param %#v" ,
82+ testName , tt .subtest , result , tt .result , tt .role , tt .pgParam )
83+ }
84+ }
85+ }
86+
2387func TestCreateLoadBalancerLogic (t * testing.T ) {
2488 var cluster = New (
2589 Config {
0 commit comments