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