@@ -51,6 +51,72 @@ func (ts *rootTestSuite) TestConfigNotFound() {
5151 r .True (os .IsNotExist (errors .Cause (readConfigFile (v , rootCmd ))))
5252}
5353
54+ func (ts * rootTestSuite ) TestNodeFlag () {
55+ r := ts .Require ()
56+ fs := afero .NewMemMapFs ()
57+
58+ fs .Create ("/dfget" )
59+
60+ configName := "dfdaemon.yml"
61+ file , err := fs .Create (configName )
62+ r .Nil (err )
63+ file .WriteString ("supernodes:\n - 127.0.0.1:6666" )
64+ file .Close ()
65+
66+ // flag not set, should use config file
67+ {
68+ v := viper .New ()
69+ v .SetFs (fs )
70+ v .Set ("dfpath" , "/" )
71+ rootCmd .Flags ().Set ("config" , configName )
72+ r .Nil (bindRootFlags (v ))
73+ r .Nil (readConfigFile (v , rootCmd ))
74+ cfg , err := getConfigFromViper (rootCmd , v )
75+ r .Nil (err )
76+ r .Equal ([]string {"127.0.0.1:6666" }, cfg .SuperNodes )
77+ }
78+
79+ // flag not set, config file doesn't exist, should use default
80+ {
81+ v := viper .New ()
82+ v .SetFs (fs )
83+ v .Set ("dfpath" , "/" )
84+ rootCmd .Flags ().Set ("config" , "xxx" )
85+ r .Nil (bindRootFlags (v ))
86+ r .NotNil (readConfigFile (v , rootCmd ))
87+ cfg , err := getConfigFromViper (rootCmd , v )
88+ r .Nil (err )
89+ r .Equal ([]string {"127.0.0.1:8002" }, cfg .SuperNodes )
90+ }
91+
92+ // when --node flag is set, should always use the flag
93+ rootCmd .Flags ().Set ("node" , "127.0.0.1:7777" )
94+
95+ {
96+ v := viper .New ()
97+ v .SetFs (fs )
98+ v .Set ("dfpath" , "/" )
99+ rootCmd .Flags ().Set ("config" , "xxx" )
100+ r .Nil (bindRootFlags (v ))
101+ r .NotNil (readConfigFile (v , rootCmd ))
102+ cfg , err := getConfigFromViper (rootCmd , v )
103+ r .Nil (err )
104+ r .Equal ([]string {"127.0.0.1:7777" }, cfg .SuperNodes )
105+ }
106+
107+ {
108+ v := viper .New ()
109+ v .SetFs (fs )
110+ v .Set ("dfpath" , "/" )
111+ rootCmd .Flags ().Set ("config" , configName )
112+ r .Nil (bindRootFlags (v ))
113+ r .Nil (readConfigFile (v , rootCmd ))
114+ cfg , err := getConfigFromViper (rootCmd , v )
115+ r .Nil (err )
116+ r .Equal ([]string {"127.0.0.1:7777" }, cfg .SuperNodes )
117+ }
118+ }
119+
54120func generateFakeFilename (fs afero.Fs ) string {
55121 for i := 0 ; i < 100 ; i ++ {
56122 d := fmt .Sprintf ("/dftest-%d-%d" , time .Now ().UnixNano (), rand .Int ())
@@ -111,7 +177,7 @@ func (ts *rootTestSuite) TestDecodeWithYAML() {
111177 r .Nil (err )
112178 v .Set ("registry_mirror.certs" , []string {f .Name ()})
113179
114- cfg , err := getConfigFromViper (v )
180+ cfg , err := getConfigFromViper (rootCmd , v )
115181 r .Nil (err )
116182 r .NotNil (cfg .RegistryMirror .Remote )
117183 r .Equal (mockURL , cfg .RegistryMirror .Remote .String ())
0 commit comments