@@ -334,3 +334,85 @@ func TestSourceList(t *testing.T) {
334
334
})
335
335
}
336
336
}
337
+
338
+ func TestSourceRemove (t * testing.T ) {
339
+ tests := []struct {
340
+ name string
341
+ migrationManagerdHTTPStatus int
342
+ migrationManagerdResponse string
343
+ args []string
344
+
345
+ assertErr require.ErrorAssertionFunc
346
+ }{
347
+ {
348
+ name : "success" ,
349
+ migrationManagerdHTTPStatus : http .StatusOK ,
350
+ migrationManagerdResponse : `{
351
+ "status_code": 200,
352
+ "status": "Success"
353
+ }` ,
354
+ args : []string {"source 1" },
355
+
356
+ assertErr : require .NoError ,
357
+ },
358
+ {
359
+ name : "error - no name argument" ,
360
+ migrationManagerdHTTPStatus : http .StatusOK ,
361
+ migrationManagerdResponse : `{
362
+ "status_code": 200,
363
+ "status": "Success"
364
+ }` ,
365
+
366
+ assertErr : require .NoError , // handled by root command, show usage
367
+ },
368
+ {
369
+ name : "error - too many arguments" ,
370
+ migrationManagerdHTTPStatus : http .StatusOK ,
371
+ migrationManagerdResponse : `{
372
+ "status_code": 200,
373
+ "status": "Success"
374
+ }` ,
375
+ args : []string {"arg1" , "arg2" },
376
+
377
+ assertErr : require .Error ,
378
+ },
379
+ {
380
+ name : "error - invalid API response" ,
381
+ migrationManagerdHTTPStatus : http .StatusOK ,
382
+ migrationManagerdResponse : `{` , // invalid JSON
383
+ args : []string {"source 1" },
384
+
385
+ assertErr : require .Error ,
386
+ },
387
+ }
388
+
389
+ for _ , tc := range tests {
390
+ t .Run (tc .name , func (t * testing.T ) {
391
+ migrationManagerd := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
392
+ w .WriteHeader (tc .migrationManagerdHTTPStatus )
393
+ _ , _ = w .Write ([]byte (tc .migrationManagerdResponse ))
394
+ }))
395
+ defer migrationManagerd .Close ()
396
+
397
+ remove := cmdSourceRemove {
398
+ global : & CmdGlobal {
399
+ config : & config.Config {
400
+ MMServer : migrationManagerd .URL ,
401
+ },
402
+ },
403
+ }
404
+
405
+ buf := bytes.Buffer {}
406
+
407
+ cmd := & cobra.Command {}
408
+ cmd .SetOutput (& buf )
409
+
410
+ err := remove .Run (cmd , tc .args )
411
+ tc .assertErr (t , err )
412
+
413
+ if testing .Verbose () {
414
+ t .Logf ("\n %s" , buf .String ())
415
+ }
416
+ })
417
+ }
418
+ }
0 commit comments