@@ -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"
1113 "github.com/stackrox/roxie/internal/types"
14+ "github.com/stackrox/roxie/internal/xdg"
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 ) {
@@ -205,3 +209,109 @@ central:
205209 })
206210 }
207211}
212+
213+ func TestApplyUserDefaults (t * testing.T ) {
214+ log := logger .New ()
215+
216+ tests := []struct {
217+ name string
218+ config deployer.Config
219+ user deployer.Config
220+ expected deployer.Config
221+ }{
222+ {
223+ name : "empty user config leaves config unchanged" ,
224+ config : deployer.Config {
225+ Roxie : deployer.RoxieConfig {Version : "4.5.0" },
226+ Central : deployer.CentralConfig {
227+ Namespace : "custom-namespace" ,
228+ },
229+ },
230+ expected : deployer.Config {
231+ Roxie : deployer.RoxieConfig {Version : "4.5.0" },
232+ Central : deployer.CentralConfig {
233+ Namespace : "custom-namespace" ,
234+ },
235+ },
236+ },
237+ {
238+ name : "fills empty fields from user defaults" ,
239+ config : deployer.Config {},
240+ user : deployer.Config {
241+ Roxie : deployer.RoxieConfig {Version : "4.5.0" },
242+ Operator : deployer.OperatorConfig {DeployViaOlm : true },
243+ },
244+ expected : deployer.Config {
245+ Roxie : deployer.RoxieConfig {Version : "4.5.0" },
246+ Operator : deployer.OperatorConfig {DeployViaOlm : true },
247+ },
248+ },
249+ {
250+ name : "user config overrides any config fields including config defaults" ,
251+ config : deployer.Config {
252+ Roxie : deployer.RoxieConfig {
253+ Version : "4.9.2" ,
254+ },
255+ },
256+ user : deployer.Config {
257+ Roxie : deployer.RoxieConfig {
258+ Version : "4.5.0" ,
259+ },
260+ Operator : deployer.OperatorConfig {
261+ DeployViaOlm : true ,
262+ },
263+ Central : deployer.CentralConfig {
264+ Namespace : "custom-namespace" ,
265+ },
266+ },
267+ expected : deployer.Config {
268+ Roxie : deployer.RoxieConfig {
269+ Version : "4.5.0" ,
270+ },
271+ Operator : deployer.OperatorConfig {
272+ DeployViaOlm : true ,
273+ },
274+ Central : deployer.CentralConfig {
275+ Namespace : "custom-namespace" ,
276+ },
277+ },
278+ },
279+ }
280+
281+ for _ , tt := range tests {
282+ t .Run (tt .name , func (t * testing.T ) {
283+ tmpDir := t .TempDir ()
284+ t .Setenv ("XDG_CONFIG_HOME" , tmpDir )
285+ t .Setenv ("HOME" , tmpDir ) // For non-Unix systems.
286+
287+ if ! reflect .DeepEqual (tt .user , deployer.Config {}) {
288+ configPath , err := xdg .UserConfigPath ()
289+ require .NoError (t , err )
290+ require .NoError (t , os .MkdirAll (filepath .Dir (configPath ), 0o755 ))
291+ data , err := yaml .Marshal (tt .user )
292+ require .NoError (t , err )
293+ require .NoError (t , os .WriteFile (configPath , data , 0o644 ))
294+ }
295+
296+ cfg := deployer .NewConfig ()
297+ require .NoError (t , mergo .Merge (& cfg , & tt .config , mergo .WithOverride , mergo .WithoutDereference ))
298+ require .NoError (t , tryApplyUserDefaults (log , & cfg ))
299+
300+ expected := deployer .NewConfig ()
301+ require .NoError (t , mergo .Merge (& expected , & tt .expected , mergo .WithOverride , mergo .WithoutDereference ))
302+
303+ assert .True (t , reflect .DeepEqual (expected , cfg ), "expected %+v, got %+v" , expected , cfg )
304+ })
305+ }
306+
307+ t .Run ("returns error on invalid yaml" , func (t * testing.T ) {
308+ t .Setenv ("HOME" , t .TempDir ())
309+ configPath , err := xdg .UserConfigPath ()
310+ require .NoError (t , err )
311+ require .NoError (t , os .MkdirAll (filepath .Dir (configPath ), 0o755 ))
312+ require .NoError (t , os .WriteFile (configPath , []byte (`invalid: [yaml` ), 0o644 ))
313+
314+ cfg := deployer .NewConfig ()
315+ assert .Error (t , tryApplyUserDefaults (log , & cfg ))
316+ })
317+ }
0 commit comments