@@ -29,24 +29,24 @@ func TestDoConfigureSet(t *testing.T) {
2929 ctx := cli .NewCommandContext (stdout , stderr )
3030 AddFlags (ctx .Flags ())
3131
32- originhook := hookLoadConfigurationWithContext
32+ originhook := hookLoadOrCreateConfiguration
3333 originhookSave := hookSaveConfigurationWithContext
3434 defer func () {
35- hookLoadConfigurationWithContext = originhook
35+ hookLoadOrCreateConfiguration = originhook
3636 hookSaveConfigurationWithContext = originhookSave
3737 }()
38- hookLoadConfigurationWithContext = func (fn func (ctx * cli. Context ) (* Configuration , error )) func (ctx * cli. Context ) (* Configuration , error ) {
39- return func (ctx * cli. Context ) (* Configuration , error ) {
38+ hookLoadOrCreateConfiguration = func (fn func (path string ) (* Configuration , error )) func (path string ) (* Configuration , error ) {
39+ return func (path string ) (* Configuration , error ) {
4040 return & Configuration {}, errors .New ("error" )
4141 }
4242 }
4343 err := doConfigureSet (ctx )
4444 assert .NotNil (t , err )
45- assert .Equal (t , "fail to set configuration: region can't be empty " , err .Error ())
45+ assert .Equal (t , "load configuration failed error " , err .Error ())
4646
4747 //testcase2
48- hookLoadConfigurationWithContext = func (fn func (ctx * cli. Context ) (* Configuration , error )) func (ctx * cli. Context ) (* Configuration , error ) {
49- return func (ctx * cli. Context ) (* Configuration , error ) {
48+ hookLoadOrCreateConfiguration = func (fn func (path string ) (* Configuration , error )) func (path string ) (* Configuration , error ) {
49+ return func (path string ) (* Configuration , error ) {
5050 return & Configuration {CurrentProfile : "default" , Profiles : []Profile {
5151 {Name : "default" , Mode : AK , AccessKeyId : "default_aliyun_access_key_id" , AccessKeySecret : "default_aliyun_access_key_secret" , OutputFormat : "json" },
5252 {Name : "aaa" , Mode : AK , AccessKeyId : "sdf" , AccessKeySecret : "ddf" , OutputFormat : "json" }}}, nil
@@ -67,8 +67,8 @@ func TestDoConfigureSet(t *testing.T) {
6767 assert .Equal (t , "fail to set configuration: region can't be empty" , err .Error ())
6868
6969 //AK
70- hookLoadConfigurationWithContext = func (fn func (ctx * cli. Context ) (* Configuration , error )) func (ctx * cli. Context ) (* Configuration , error ) {
71- return func (ctx * cli. Context ) (* Configuration , error ) {
70+ hookLoadOrCreateConfiguration = func (fn func (path string ) (* Configuration , error )) func (path string ) (* Configuration , error ) {
71+ return func (path string ) (* Configuration , error ) {
7272 return & Configuration {
7373 CurrentProfile : "default" ,
7474 Profiles : []Profile {
@@ -84,8 +84,8 @@ func TestDoConfigureSet(t *testing.T) {
8484 assert .Empty (t , stdout .String ())
8585
8686 //StsToken
87- hookLoadConfigurationWithContext = func (fn func (ctx * cli. Context ) (* Configuration , error )) func (ctx * cli. Context ) (* Configuration , error ) {
88- return func (ctx * cli. Context ) (* Configuration , error ) {
87+ hookLoadOrCreateConfiguration = func (fn func (path string ) (* Configuration , error )) func (path string ) (* Configuration , error ) {
88+ return func (path string ) (* Configuration , error ) {
8989 return & Configuration {
9090 CurrentProfile : "default" ,
9191 Profiles : []Profile {
@@ -99,8 +99,8 @@ func TestDoConfigureSet(t *testing.T) {
9999 assert .Empty (t , stdout .String ())
100100
101101 //RamRoleArn
102- hookLoadConfigurationWithContext = func (fn func (ctx * cli. Context ) (* Configuration , error )) func (ctx * cli. Context ) (* Configuration , error ) {
103- return func (ctx * cli. Context ) (* Configuration , error ) {
102+ hookLoadOrCreateConfiguration = func (fn func (path string ) (* Configuration , error )) func (path string ) (* Configuration , error ) {
103+ return func (path string ) (* Configuration , error ) {
104104 return & Configuration {
105105 CurrentProfile : "default" ,
106106 Profiles : []Profile {
@@ -114,8 +114,8 @@ func TestDoConfigureSet(t *testing.T) {
114114 assert .Empty (t , stdout .String ())
115115
116116 //EcsRamRole
117- hookLoadConfigurationWithContext = func (fn func (ctx * cli. Context ) (* Configuration , error )) func (ctx * cli. Context ) (* Configuration , error ) {
118- return func (ctx * cli. Context ) (* Configuration , error ) {
117+ hookLoadOrCreateConfiguration = func (fn func (path string ) (* Configuration , error )) func (path string ) (* Configuration , error ) {
118+ return func (path string ) (* Configuration , error ) {
119119 return & Configuration {CurrentProfile : "default" , Profiles : []Profile {{Name : "default" , Mode : EcsRamRole , RamRoleName : "RamRoleName" , AccessKeyId : "default_aliyun_access_key_id" , AccessKeySecret : "default_aliyun_access_key_secret" , OutputFormat : "json" , RegionId : "cn-hangzhou" }, {Name : "aaa" , Mode : AK , AccessKeyId : "sdf" , AccessKeySecret : "ddf" , OutputFormat : "json" }}}, nil
120120 }
121121 }
@@ -125,8 +125,8 @@ func TestDoConfigureSet(t *testing.T) {
125125 assert .Empty (t , stdout .String ())
126126
127127 // RamRoleArnWithEcs
128- hookLoadConfigurationWithContext = func (fn func (ctx * cli. Context ) (* Configuration , error )) func (ctx * cli. Context ) (* Configuration , error ) {
129- return func (ctx * cli. Context ) (* Configuration , error ) {
128+ hookLoadOrCreateConfiguration = func (fn func (path string ) (* Configuration , error )) func (path string ) (* Configuration , error ) {
129+ return func (path string ) (* Configuration , error ) {
130130 return & Configuration {
131131 CurrentProfile : "default" ,
132132 Profiles : []Profile {
@@ -140,8 +140,8 @@ func TestDoConfigureSet(t *testing.T) {
140140 assert .Empty (t , stdout .String ())
141141
142142 // RsaKeyPair
143- hookLoadConfigurationWithContext = func (fn func (ctx * cli. Context ) (* Configuration , error )) func (ctx * cli. Context ) (* Configuration , error ) {
144- return func (ctx * cli. Context ) (* Configuration , error ) {
143+ hookLoadOrCreateConfiguration = func (fn func (path string ) (* Configuration , error )) func (path string ) (* Configuration , error ) {
144+ return func (path string ) (* Configuration , error ) {
145145 return & Configuration {
146146 CurrentProfile : "default" , Profiles : []Profile {
147147 {Name : "default" , Mode : RsaKeyPair , KeyPairName : "KeyPairName" , PrivateKey : "PrivateKey" , AccessKeyId : "default_aliyun_access_key_id" , AccessKeySecret : "default_aliyun_access_key_secret" , OutputFormat : "json" , RegionId : "cn-hangzhou" },
@@ -154,8 +154,8 @@ func TestDoConfigureSet(t *testing.T) {
154154 assert .Empty (t , stdout .String ())
155155
156156 // External
157- hookLoadConfigurationWithContext = func (fn func (ctx * cli. Context ) (* Configuration , error )) func (ctx * cli. Context ) (* Configuration , error ) {
158- return func (ctx * cli. Context ) (* Configuration , error ) {
157+ hookLoadOrCreateConfiguration = func (fn func (path string ) (* Configuration , error )) func (path string ) (* Configuration , error ) {
158+ return func (path string ) (* Configuration , error ) {
159159 return & Configuration {
160160 CurrentProfile : "default" ,
161161 Profiles : []Profile {
@@ -168,8 +168,8 @@ func TestDoConfigureSet(t *testing.T) {
168168 doConfigureSet (ctx )
169169 assert .Empty (t , stdout .String ())
170170 // OIDC
171- hookLoadConfigurationWithContext = func (fn func (ctx * cli. Context ) (* Configuration , error )) func (ctx * cli. Context ) (* Configuration , error ) {
172- return func (ctx * cli. Context ) (* Configuration , error ) {
171+ hookLoadOrCreateConfiguration = func (fn func (path string ) (* Configuration , error )) func (path string ) (* Configuration , error ) {
172+ return func (path string ) (* Configuration , error ) {
173173 return & Configuration {
174174 CurrentProfile : "default" ,
175175 Profiles : []Profile {
@@ -185,8 +185,8 @@ func TestDoConfigureSet(t *testing.T) {
185185 assert .Empty (t , stdout .String ())
186186
187187 // CloudSSO
188- hookLoadConfigurationWithContext = func (fn func (ctx * cli. Context ) (* Configuration , error )) func (ctx * cli. Context ) (* Configuration , error ) {
189- return func (ctx * cli. Context ) (* Configuration , error ) {
188+ hookLoadOrCreateConfiguration = func (fn func (path string ) (* Configuration , error )) func (path string ) (* Configuration , error ) {
189+ return func (path string ) (* Configuration , error ) {
190190 return & Configuration {
191191 CurrentProfile : "default" ,
192192 Profiles : []Profile {
@@ -203,3 +203,42 @@ func TestDoConfigureSet(t *testing.T) {
203203 doConfigureSet (ctx )
204204 assert .Empty (t , stdout .String ())
205205}
206+
207+ func TestDoConfigureSetWithMock (t * testing.T ) {
208+ stdout := new (bytes.Buffer )
209+ stderr := new (bytes.Buffer )
210+ ctx := cli .NewCommandContext (stdout , stderr )
211+ AddFlags (ctx .Flags ())
212+
213+ originhook := hookLoadOrCreateConfiguration
214+ originhookSave := hookSaveConfigurationWithContext
215+ defer func () {
216+ hookLoadOrCreateConfiguration = originhook
217+ hookSaveConfigurationWithContext = originhookSave
218+ }()
219+
220+ // EndpointType
221+ hookLoadOrCreateConfiguration = func (fn func (path string ) (* Configuration , error )) func (path string ) (* Configuration , error ) {
222+ return func (path string ) (* Configuration , error ) {
223+ return & Configuration {
224+ CurrentProfile : "default" ,
225+ Profiles : []Profile {
226+ {Name : "default" , Mode : AK , EndpointType : "vpc" , AccessKeyId : "default_aliyun_access_key_id" , AccessKeySecret : "default_aliyun_access_key_secret" , OutputFormat : "json" , RegionId : "cn-hangzhou" },
227+ {Name : "aaa" , Mode : AK , AccessKeyId : "sdf" , AccessKeySecret : "ddf" , OutputFormat : "json" }}}, nil
228+ }
229+ }
230+ hookSaveConfigurationWithContext = func (fn func (ctx * cli.Context , config * Configuration ) error ) func (ctx * cli.Context , config * Configuration ) error {
231+ return func (ctx * cli.Context , config * Configuration ) error {
232+ return nil
233+ }
234+ }
235+ endpointTypeFlag := EndpointTypeFlag (ctx .Flags ())
236+ if endpointTypeFlag != nil {
237+ endpointTypeFlag .SetAssigned (true )
238+ endpointTypeFlag .SetValue ("testabc" )
239+ }
240+ stdout .Reset ()
241+ stderr .Reset ()
242+ doConfigureSet (ctx )
243+ assert .Empty (t , stdout .String ())
244+ }
0 commit comments