@@ -51,6 +51,73 @@ 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+ fmt .Println (cfg )
105+ r .Equal ([]string {"127.0.0.1:7777" }, cfg .SuperNodes )
106+ }
107+
108+ {
109+ v := viper .New ()
110+ v .SetFs (fs )
111+ v .Set ("dfpath" , "/" )
112+ rootCmd .Flags ().Set ("config" , configName )
113+ r .Nil (bindRootFlags (v ))
114+ r .Nil (readConfigFile (v , rootCmd ))
115+ cfg , err := getConfigFromViper (rootCmd , v )
116+ r .Nil (err )
117+ r .Equal ([]string {"127.0.0.1:7777" }, cfg .SuperNodes )
118+ }
119+ }
120+
54121func generateFakeFilename (fs afero.Fs ) string {
55122 for i := 0 ; i < 100 ; i ++ {
56123 d := fmt .Sprintf ("/dftest-%d-%d" , time .Now ().UnixNano (), rand .Int ())
@@ -109,7 +176,7 @@ func (ts *rootTestSuite) TestDecodeWithYAML() {
109176 f .Close ()
110177 v .Set ("registry_mirror.certs" , []string {f .Name ()})
111178
112- cfg , err := getConfigFromViper (v )
179+ cfg , err := getConfigFromViper (rootCmd , v )
113180 r .Nil (err )
114181 r .NotNil (cfg .RegistryMirror .Remote )
115182 r .Equal (mockURL , cfg .RegistryMirror .Remote .String ())
0 commit comments