@@ -34,6 +34,123 @@ import (
3434 "github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
3535)
3636
37+ func TestGeneratePostgresParameters (t * testing.T ) {
38+ ctx := context .Background ()
39+ reconciler := & Reconciler {}
40+
41+ builtin := reconciler .generatePostgresParameters (ctx , v1beta1 .NewPostgresCluster (), false )
42+ assert .Assert (t , len (builtin .AsMap ()) > 0 ,
43+ "expected an empty cluster to have some builtin parameters" )
44+
45+ assert .Equal (t , builtin .Value ("jit" ), "off" ,
46+ "BUG IN TEST: expected JIT to be disabled" )
47+
48+ assert .Equal (t , builtin .Value ("shared_preload_libraries" ), "pgaudit" ,
49+ "BUG IN TEST: expected pgAudit to be mandatory" )
50+
51+ t .Run ("Config" , func (t * testing.T ) {
52+ cluster := v1beta1 .NewPostgresCluster ()
53+ require .UnmarshalInto (t , & cluster .Spec .Config , `{
54+ parameters: {
55+ something: str,
56+ another: 5,
57+ },
58+ }` )
59+
60+ result := reconciler .generatePostgresParameters (ctx , cluster , false )
61+ assert .Assert (t , cmp .LenMap (result .AsMap (), len (builtin .AsMap ())+ 2 ),
62+ "expected two parameters from the Config section" )
63+
64+ assert .Equal (t , result .Value ("another" ), "5" )
65+ assert .Equal (t , result .Value ("something" ), "str" )
66+ })
67+
68+ t .Run ("Patroni" , func (t * testing.T ) {
69+ cluster := v1beta1 .NewPostgresCluster ()
70+ require .UnmarshalInto (t , & cluster .Spec .Patroni , `{
71+ dynamicConfiguration: {
72+ postgresql: { parameters: {
73+ something: str,
74+ another: 5.1,
75+ } },
76+ },
77+ }` )
78+
79+ result := reconciler .generatePostgresParameters (ctx , cluster , false )
80+ assert .Assert (t , cmp .LenMap (result .AsMap (), len (builtin .AsMap ())+ 2 ),
81+ "expected two parameters from the Patroni section" )
82+
83+ assert .Equal (t , result .Value ("another" ), "5.1" )
84+ assert .Equal (t , result .Value ("something" ), "str" )
85+ })
86+
87+ t .Run ("Precedence" , func (t * testing.T ) {
88+ cluster := v1beta1 .NewPostgresCluster ()
89+ require .UnmarshalInto (t , & cluster .Spec .Config , `{
90+ parameters: {
91+ something: replaced,
92+ unrelated: used,
93+ jit: "on",
94+ },
95+ }` )
96+ require .UnmarshalInto (t , & cluster .Spec .Patroni , `{
97+ dynamicConfiguration: {
98+ postgresql: { parameters: {
99+ something: str,
100+ another: 5.1,
101+ } },
102+ },
103+ }` )
104+
105+ result := reconciler .generatePostgresParameters (ctx , cluster , false )
106+ assert .Assert (t , cmp .LenMap (result .AsMap (), len (builtin .AsMap ())+ 3 + 1 - 1 ),
107+ "expected three parameters from the Config section," +
108+ "plus one from the Patroni section, minus one default" )
109+
110+ assert .Equal (t , result .Value ("another" ), "5.1" ) // Patroni
111+ assert .Equal (t , result .Value ("something" ), "replaced" ) // Config
112+ assert .Equal (t , result .Value ("unrelated" ), "used" ) // Config
113+ assert .Equal (t , result .Value ("jit" ), "on" ) // Config
114+ })
115+
116+ t .Run ("shared_preload_libraries" , func (t * testing.T ) {
117+ t .Run ("NumericIncluded" , func (t * testing.T ) {
118+ cluster := v1beta1 .NewPostgresCluster ()
119+ require .UnmarshalInto (t , & cluster .Spec .Config , `{
120+ parameters: {
121+ shared_preload_libraries: 123,
122+ },
123+ }` )
124+
125+ result := reconciler .generatePostgresParameters (ctx , cluster , false )
126+ assert .Assert (t , cmp .Contains (result .Value ("shared_preload_libraries" ), "123" ))
127+ })
128+
129+ t .Run ("Precedence" , func (t * testing.T ) {
130+ cluster := v1beta1 .NewPostgresCluster ()
131+ require .UnmarshalInto (t , & cluster .Spec .Config , `{
132+ parameters: {
133+ shared_preload_libraries: given,
134+ },
135+ }` )
136+
137+ result := reconciler .generatePostgresParameters (ctx , cluster , false )
138+ assert .Equal (t , result .Value ("shared_preload_libraries" ), "pgaudit,given" ,
139+ "expected mandatory ahead of specified" )
140+
141+ require .UnmarshalInto (t , & cluster .Spec .Config , `{
142+ parameters: {
143+ shared_preload_libraries: 'given, citus,other'
144+ },
145+ }` )
146+
147+ result = reconciler .generatePostgresParameters (ctx , cluster , false )
148+ assert .Equal (t , result .Value ("shared_preload_libraries" ), "citus,pgaudit,given, citus,other" ,
149+ "expected citus in front" )
150+ })
151+ })
152+ }
153+
37154func TestGeneratePostgresUserSecret (t * testing.T ) {
38155 _ , tClient := setupKubernetes (t )
39156 require .ParallelCapacity (t , 0 )
0 commit comments