@@ -7,10 +7,14 @@ import (
77 "testing"
88 "time"
99
10+ "dario.cat/mergo"
1011 "github.com/stackrox/roxie/internal/deployer"
12+ "github.com/stackrox/roxie/internal/logger"
13+ "github.com/stackrox/roxie/internal/paths"
1114 "github.com/stackrox/roxie/internal/types"
1215 "github.com/stretchr/testify/assert"
1316 "github.com/stretchr/testify/require"
17+ "gopkg.in/yaml.v3"
1418)
1519
1620func TestNewDeployCmd_Flags (t * testing.T ) {
@@ -110,22 +114,22 @@ func TestNewDeployCmd_Flags(t *testing.T) {
110114 name : "pause-reconciliation" ,
111115 args : []string {"--pause-reconciliation" },
112116 assert : func (t * testing.T , cfg deployer.Config ) {
113- assert .True (t , cfg .Central .PauseReconciliation , "Central.PauseReconciliation mismatch" )
114- assert .True (t , cfg .SecuredCluster .PauseReconciliation , "SecuredCluster.PauseReconciliation mismatch" )
117+ assert .True (t , cfg .Central .PauseReconciliationEnabled () , "Central.PauseReconciliation mismatch" )
118+ assert .True (t , cfg .SecuredCluster .PauseReconciliationEnabled () , "SecuredCluster.PauseReconciliation mismatch" )
115119 },
116120 },
117121 {
118122 name : "olm" ,
119123 args : []string {"--olm" },
120124 assert : func (t * testing.T , cfg deployer.Config ) {
121- assert .True (t , cfg .Operator .DeployViaOlm , "Operator.DeployViaOlm mismatch" )
125+ assert .True (t , cfg .Operator .DeployViaOlmEnabled () , "Operator.DeployViaOlm mismatch" )
122126 },
123127 },
124128 {
125129 name : "disable deploy-operator" ,
126130 args : []string {"--deploy-operator=false" },
127131 assert : func (t * testing.T , cfg deployer.Config ) {
128- assert .True (t , cfg .Operator .SkipDeployment , "Operator.SkipDeployment mismatch" )
132+ assert .True (t , cfg .Operator .SkipDeploymentEnabled () , "Operator.SkipDeployment mismatch" )
129133 },
130134 },
131135 {
@@ -218,3 +222,117 @@ central:
218222 })
219223 }
220224}
225+
226+ func TestApplyUserDefaults (t * testing.T ) {
227+ log := logger .New ()
228+
229+ tests := []struct {
230+ name string
231+ config deployer.Config
232+ user deployer.Config
233+ expected deployer.Config
234+ }{
235+ {
236+ name : "empty user config leaves config unchanged" ,
237+ config : deployer.Config {
238+ Roxie : deployer.RoxieConfig {Version : "4.5.0" },
239+ Central : deployer.CentralConfig {
240+ Namespace : "custom-namespace" ,
241+ },
242+ },
243+ expected : deployer.Config {
244+ Roxie : deployer.RoxieConfig {Version : "4.5.0" },
245+ Central : deployer.CentralConfig {
246+ Namespace : "custom-namespace" ,
247+ },
248+ },
249+ },
250+ {
251+ name : "fills empty fields from user defaults" ,
252+ config : deployer.Config {},
253+ user : deployer.Config {
254+ Roxie : deployer.RoxieConfig {Version : "4.5.0" },
255+ Operator : deployer.OperatorConfig {DeployViaOlm : new (true )},
256+ },
257+ expected : deployer.Config {
258+ Roxie : deployer.RoxieConfig {Version : "4.5.0" },
259+ Operator : deployer.OperatorConfig {DeployViaOlm : new (true )},
260+ },
261+ },
262+ {
263+ name : "user config overrides any config fields including config defaults" ,
264+ config : deployer.Config {
265+ Roxie : deployer.RoxieConfig {
266+ Version : "4.9.2" ,
267+ },
268+ Central : deployer.CentralConfig {
269+ EarlyReadiness : new (true ),
270+ },
271+ },
272+ user : deployer.Config {
273+ Roxie : deployer.RoxieConfig {
274+ Version : "4.5.0" ,
275+ },
276+ Operator : deployer.OperatorConfig {
277+ DeployViaOlm : new (true ),
278+ },
279+ Central : deployer.CentralConfig {
280+ Namespace : "custom-namespace" ,
281+ EarlyReadiness : new (false ),
282+ },
283+ },
284+ expected : deployer.Config {
285+ Roxie : deployer.RoxieConfig {
286+ Version : "4.5.0" ,
287+ },
288+ Operator : deployer.OperatorConfig {
289+ DeployViaOlm : new (true ),
290+ },
291+ Central : deployer.CentralConfig {
292+ Namespace : "custom-namespace" ,
293+ EarlyReadiness : new (false ),
294+ },
295+ },
296+ },
297+ }
298+
299+ for _ , tt := range tests {
300+ t .Run (tt .name , func (t * testing.T ) {
301+ tmpDir := t .TempDir ()
302+ t .Setenv ("XDG_CONFIG_HOME" , tmpDir )
303+ t .Setenv ("HOME" , tmpDir ) // For non-Unix systems.
304+
305+ if ! reflect .DeepEqual (tt .user , deployer.Config {}) {
306+ configPath , err := paths .UserConfigPath ()
307+ require .NoError (t , err )
308+ require .NoError (t , os .MkdirAll (filepath .Dir (configPath ), 0o755 ))
309+ data , err := yaml .Marshal (tt .user )
310+ require .NoError (t , err )
311+ require .NoError (t , os .WriteFile (configPath , data , 0o644 ))
312+ }
313+
314+ cfg := deployer .NewConfig ()
315+ require .NoError (t , mergo .Merge (& cfg , & tt .config , mergo .WithOverride , mergo .WithoutDereference ))
316+ require .NoError (t , tryApplyUserDefaults (log , & cfg ))
317+
318+ expected := deployer .NewConfig ()
319+ require .NoError (t , mergo .Merge (& expected , & tt .expected , mergo .WithOverride , mergo .WithoutDereference ))
320+
321+ assert .True (t , reflect .DeepEqual (expected , cfg ), "expected %+v, got %+v" , expected , cfg )
322+ })
323+ }
324+
325+ t .Run ("returns error on invalid yaml" , func (t * testing.T ) {
326+ tmpDir := t .TempDir ()
327+ t .Setenv ("XDG_CONFIG_HOME" , tmpDir )
328+ t .Setenv ("HOME" , tmpDir ) // For non-Unix systems.
329+
330+ configPath , err := paths .UserConfigPath ()
331+ require .NoError (t , err )
332+ require .NoError (t , os .MkdirAll (filepath .Dir (configPath ), 0o755 ))
333+ require .NoError (t , os .WriteFile (configPath , []byte (`invalid: [yaml` ), 0o644 ))
334+
335+ cfg := deployer .NewConfig ()
336+ assert .Error (t , tryApplyUserDefaults (log , & cfg ))
337+ })
338+ }
0 commit comments