55package validation
66
77import (
8+ "fmt"
89 "testing"
910
1011 "gotest.tools/v3/assert"
@@ -118,8 +119,6 @@ func testPostgresConfigParametersCommon(t *testing.T, cc client.Client, base uns
118119 {key : "hot_standby" , value : "off" },
119120 {key : "ident_file" , value : "two" },
120121 {key : "listen_addresses" , value : "" },
121- {key : "log_file_mode" , value : "" },
122- {key : "logging_collector" , value : "off" },
123122 {key : "port" , value : 5 },
124123 {key : "wal_log_hints" , value : "off" },
125124 } {
@@ -143,6 +142,57 @@ func testPostgresConfigParametersCommon(t *testing.T, cc client.Client, base uns
143142 }
144143 })
145144
145+ t .Run ("Logging" , func (t * testing.T ) {
146+ for _ , tt := range []struct {
147+ valid bool
148+ key string
149+ value any
150+ message string
151+ }{
152+ {valid : false , key : "log_file_mode" , value : "" , message : "cannot be changed" },
153+ {valid : false , key : "log_file_mode" , value : "any" , message : "cannot be changed" },
154+ {valid : false , key : "logging_collector" , value : "" , message : "unsafe" },
155+ {valid : false , key : "logging_collector" , value : "off" , message : "unsafe" },
156+ {valid : false , key : "logging_collector" , value : "on" , message : "unsafe" },
157+
158+ {valid : true , key : "log_destination" , value : "anything" },
159+ {valid : true , key : "log_directory" , value : "anything" },
160+ {valid : true , key : "log_filename" , value : "anything" },
161+ {valid : true , key : "log_filename" , value : "percent-%s-too" },
162+ {valid : true , key : "log_rotation_age" , value : "7d" },
163+ {valid : true , key : "log_rotation_age" , value : 5 },
164+ {valid : true , key : "log_rotation_size" , value : "100MB" },
165+ {valid : true , key : "log_rotation_size" , value : 13 },
166+ {valid : true , key : "log_timezone" , value : "" },
167+ {valid : true , key : "log_timezone" , value : "nonsense" },
168+ } {
169+ t .Run (fmt .Sprint (tt ), func (t * testing.T ) {
170+ cluster := base .DeepCopy ()
171+ require .UnmarshalIntoField (t , cluster ,
172+ require .Value (yaml .Marshal (tt .value )),
173+ "spec" , "config" , "parameters" , tt .key )
174+
175+ err := cc .Create (ctx , cluster , client .DryRunAll )
176+
177+ if tt .valid {
178+ assert .NilError (t , err )
179+ assert .Equal (t , "" , tt .message , "BUG IN TEST: no message expected when valid" )
180+ } else {
181+ assert .Assert (t , apierrors .IsInvalid (err ))
182+
183+ status := require .StatusError (t , err )
184+ assert .Assert (t , status .Details != nil )
185+ assert .Assert (t , cmp .Len (status .Details .Causes , 1 ))
186+
187+ // TODO(k8s-1.30) TODO(validation): Move the parameter name from the message to the field path.
188+ assert .Equal (t , status .Details .Causes [0 ].Field , "spec.config.parameters" )
189+ assert .Assert (t , cmp .Contains (status .Details .Causes [0 ].Message , tt .key ))
190+ assert .Assert (t , cmp .Contains (status .Details .Causes [0 ].Message , tt .message ))
191+ }
192+ })
193+ }
194+ })
195+
146196 t .Run ("NoConnections" , func (t * testing.T ) {
147197 for _ , tt := range []struct {
148198 key string
0 commit comments