github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/machine/remove_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package machine_test
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	jc "github.com/juju/testing/checkers"
     9  	gc "gopkg.in/check.v1"
    10  
    11  	"github.com/juju/juju/apiserver/common"
    12  	"github.com/juju/juju/cmd/juju/machine"
    13  	"github.com/juju/juju/testing"
    14  )
    15  
    16  type RemoveMachineSuite struct {
    17  	testing.FakeJujuXDGDataHomeSuite
    18  	fake *fakeRemoveMachineAPI
    19  }
    20  
    21  var _ = gc.Suite(&RemoveMachineSuite{})
    22  
    23  func (s *RemoveMachineSuite) SetUpTest(c *gc.C) {
    24  	s.FakeJujuXDGDataHomeSuite.SetUpTest(c)
    25  	s.fake = &fakeRemoveMachineAPI{}
    26  }
    27  
    28  func (s *RemoveMachineSuite) run(c *gc.C, args ...string) (*cmd.Context, error) {
    29  	remove, _ := machine.NewRemoveCommandForTest(s.fake)
    30  	return testing.RunCommand(c, remove, args...)
    31  }
    32  
    33  func (s *RemoveMachineSuite) TestInit(c *gc.C) {
    34  	for i, test := range []struct {
    35  		args        []string
    36  		machines    []string
    37  		force       bool
    38  		errorString string
    39  	}{
    40  		{
    41  			errorString: "no machines specified",
    42  		}, {
    43  			args:     []string{"1"},
    44  			machines: []string{"1"},
    45  		}, {
    46  			args:     []string{"1", "2"},
    47  			machines: []string{"1", "2"},
    48  		}, {
    49  			args:     []string{"1", "--force"},
    50  			machines: []string{"1"},
    51  			force:    true,
    52  		}, {
    53  			args:     []string{"--force", "1", "2"},
    54  			machines: []string{"1", "2"},
    55  			force:    true,
    56  		}, {
    57  			args:        []string{"lxd"},
    58  			errorString: `invalid machine id "lxd"`,
    59  		}, {
    60  			args:     []string{"1/lxd/2"},
    61  			machines: []string{"1/lxd/2"},
    62  		},
    63  	} {
    64  		c.Logf("test %d", i)
    65  		wrappedCommand, removeCmd := machine.NewRemoveCommandForTest(s.fake)
    66  		err := testing.InitCommand(wrappedCommand, test.args)
    67  		if test.errorString == "" {
    68  			c.Check(err, jc.ErrorIsNil)
    69  			c.Check(removeCmd.Force, gc.Equals, test.force)
    70  			c.Check(removeCmd.MachineIds, jc.DeepEquals, test.machines)
    71  		} else {
    72  			c.Check(err, gc.ErrorMatches, test.errorString)
    73  		}
    74  	}
    75  }
    76  
    77  func (s *RemoveMachineSuite) TestRemove(c *gc.C) {
    78  	_, err := s.run(c, "1", "2/lxd/1")
    79  	c.Assert(err, jc.ErrorIsNil)
    80  	c.Assert(s.fake.forced, jc.IsFalse)
    81  	c.Assert(s.fake.machines, jc.DeepEquals, []string{"1", "2/lxd/1"})
    82  }
    83  
    84  func (s *RemoveMachineSuite) TestRemoveForce(c *gc.C) {
    85  	_, err := s.run(c, "--force", "1", "2/lxd/1")
    86  	c.Assert(err, jc.ErrorIsNil)
    87  	c.Assert(s.fake.forced, jc.IsTrue)
    88  	c.Assert(s.fake.machines, jc.DeepEquals, []string{"1", "2/lxd/1"})
    89  }
    90  
    91  func (s *RemoveMachineSuite) TestBlockedError(c *gc.C) {
    92  	s.fake.removeError = common.OperationBlockedError("TestBlockedError")
    93  	_, err := s.run(c, "1")
    94  	c.Assert(s.fake.forced, jc.IsFalse)
    95  	testing.AssertOperationWasBlocked(c, err, ".*TestBlockedError.*")
    96  }
    97  
    98  func (s *RemoveMachineSuite) TestForceBlockedError(c *gc.C) {
    99  	s.fake.removeError = common.OperationBlockedError("TestForceBlockedError")
   100  	_, err := s.run(c, "--force", "1")
   101  	c.Assert(s.fake.forced, jc.IsTrue)
   102  	testing.AssertOperationWasBlocked(c, err, ".*TestForceBlockedError.*")
   103  }
   104  
   105  type fakeRemoveMachineAPI struct {
   106  	forced      bool
   107  	machines    []string
   108  	removeError error
   109  }
   110  
   111  func (f *fakeRemoveMachineAPI) Close() error {
   112  	return nil
   113  }
   114  
   115  func (f *fakeRemoveMachineAPI) DestroyMachines(machines ...string) error {
   116  	f.forced = false
   117  	f.machines = machines
   118  	return f.removeError
   119  }
   120  
   121  func (f *fakeRemoveMachineAPI) ForceDestroyMachines(machines ...string) error {
   122  	f.forced = true
   123  	f.machines = machines
   124  	return f.removeError
   125  }