mirror of https://github.com/docker/docs.git
				
				
				
			Fixes #2595 `dm rm` fails when config.json is not found
This issue occurs when `api.Load()` fails while loading the `config.json` file, in case the config file is not present. Refactored the code to handled `-y` and `-f` as documented. Now added UT for remove options. Signed-off-by: Anil Belur <askb23@gmail.com>
This commit is contained in:
		
							parent
							
								
									32795c9d1f
								
							
						
					
					
						commit
						403b57437f
					
				|  | @ -17,9 +17,13 @@ func cmdRm(c CommandLine, api libmachine.API) error { | ||||||
| 	confirm := c.Bool("y") | 	confirm := c.Bool("y") | ||||||
| 
 | 
 | ||||||
| 	for _, hostName := range c.Args() { | 	for _, hostName := range c.Args() { | ||||||
| 		h, err := api.Load(hostName) | 		h, loaderr := api.Load(hostName) | ||||||
| 		if err != nil { | 		if loaderr != nil { | ||||||
| 			return fmt.Errorf("Error removing host %q: %s", hostName, err) | 			// On --force, continue to remove on-disk files/dir
 | ||||||
|  | 			if !force { | ||||||
|  | 				return fmt.Errorf("Error removing host %q: %s", hostName, loaderr) | ||||||
|  | 			} | ||||||
|  | 			log.Errorf("Error removing host %q: %s. Continuing on `-f`, host instance may by running", hostName, loaderr) | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		if !confirm && !force { | 		if !confirm && !force { | ||||||
|  | @ -29,10 +33,12 @@ func cmdRm(c CommandLine, api libmachine.API) error { | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		if err := h.Driver.Remove(); err != nil { | 		if loaderr == nil { | ||||||
| 			if !force { | 			if err := h.Driver.Remove(); err != nil { | ||||||
| 				log.Errorf("Provider error removing machine %q: %s", hostName, err) | 				if !force { | ||||||
| 				continue | 					log.Errorf("Provider error removing machine %q: %s", hostName, err) | ||||||
|  | 					continue | ||||||
|  | 				} | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -53,3 +53,87 @@ func TestCmdRm(t *testing.T) { | ||||||
| 	assert.False(t, libmachinetest.Exists(api, "machineToRemove2")) | 	assert.False(t, libmachinetest.Exists(api, "machineToRemove2")) | ||||||
| 	assert.True(t, libmachinetest.Exists(api, "machine")) | 	assert.True(t, libmachinetest.Exists(api, "machine")) | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | func TestCmdRmforcefully(t *testing.T) { | ||||||
|  | 	commandLine := &commandstest.FakeCommandLine{ | ||||||
|  | 		CliArgs: []string{"machineToRemove1", "machineToRemove2"}, | ||||||
|  | 		LocalFlags: &commandstest.FakeFlagger{ | ||||||
|  | 			Data: map[string]interface{}{ | ||||||
|  | 				"force": true, | ||||||
|  | 			}, | ||||||
|  | 		}, | ||||||
|  | 	} | ||||||
|  | 	api := &libmachinetest.FakeAPI{ | ||||||
|  | 		Hosts: []*host.Host{ | ||||||
|  | 			{ | ||||||
|  | 				Name:   "machineToRemove1", | ||||||
|  | 				Driver: &fakedriver.Driver{}, | ||||||
|  | 			}, | ||||||
|  | 			{ | ||||||
|  | 				Name:   "machineToRemove2", | ||||||
|  | 				Driver: &fakedriver.Driver{}, | ||||||
|  | 			}, | ||||||
|  | 		}, | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	err := cmdRm(commandLine, api) | ||||||
|  | 	assert.NoError(t, err) | ||||||
|  | 
 | ||||||
|  | 	assert.False(t, libmachinetest.Exists(api, "machineToRemove1")) | ||||||
|  | 	assert.False(t, libmachinetest.Exists(api, "machineToRemove2")) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func TestCmdRmforceDoesAutoConfirm(t *testing.T) { | ||||||
|  | 	commandLine := &commandstest.FakeCommandLine{ | ||||||
|  | 		CliArgs: []string{"machineToRemove1", "machineToRemove2"}, | ||||||
|  | 		LocalFlags: &commandstest.FakeFlagger{ | ||||||
|  | 			Data: map[string]interface{}{ | ||||||
|  | 				"y":     false, | ||||||
|  | 				"force": true, | ||||||
|  | 			}, | ||||||
|  | 		}, | ||||||
|  | 	} | ||||||
|  | 	api := &libmachinetest.FakeAPI{ | ||||||
|  | 		Hosts: []*host.Host{ | ||||||
|  | 			{ | ||||||
|  | 				Name:   "machineToRemove1", | ||||||
|  | 				Driver: &fakedriver.Driver{}, | ||||||
|  | 			}, | ||||||
|  | 			{ | ||||||
|  | 				Name:   "machineToRemove2", | ||||||
|  | 				Driver: &fakedriver.Driver{}, | ||||||
|  | 			}, | ||||||
|  | 		}, | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	err := cmdRm(commandLine, api) | ||||||
|  | 	assert.NoError(t, err) | ||||||
|  | 
 | ||||||
|  | 	assert.False(t, libmachinetest.Exists(api, "machineToRemove1")) | ||||||
|  | 	assert.False(t, libmachinetest.Exists(api, "machineToRemove2")) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func TestCmdRmforceConfirmUnset(t *testing.T) { | ||||||
|  | 	commandLine := &commandstest.FakeCommandLine{ | ||||||
|  | 		CliArgs: []string{"machineToRemove1"}, | ||||||
|  | 		LocalFlags: &commandstest.FakeFlagger{ | ||||||
|  | 			Data: map[string]interface{}{ | ||||||
|  | 				"y":     false, | ||||||
|  | 				"force": false, | ||||||
|  | 			}, | ||||||
|  | 		}, | ||||||
|  | 	} | ||||||
|  | 	api := &libmachinetest.FakeAPI{ | ||||||
|  | 		Hosts: []*host.Host{ | ||||||
|  | 			{ | ||||||
|  | 				Name:   "machineToRemove1", | ||||||
|  | 				Driver: &fakedriver.Driver{}, | ||||||
|  | 			}, | ||||||
|  | 		}, | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	err := cmdRm(commandLine, api) | ||||||
|  | 	assert.EqualError(t, err, "EOF") | ||||||
|  | 
 | ||||||
|  | 	assert.True(t, libmachinetest.Exists(api, "machineToRemove1")) | ||||||
|  | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue