github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/service/removeunit_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package service
     5  
     6  import (
     7  	"fmt"
     8  
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  	"gopkg.in/juju/charm.v6-unstable"
    12  
    13  	"github.com/juju/juju/cmd/juju/common"
    14  	jujutesting "github.com/juju/juju/juju/testing"
    15  	"github.com/juju/juju/state"
    16  	"github.com/juju/juju/testcharms"
    17  	"github.com/juju/juju/testing"
    18  )
    19  
    20  type RemoveUnitSuite struct {
    21  	jujutesting.RepoSuite
    22  	common.CmdBlockHelper
    23  }
    24  
    25  func (s *RemoveUnitSuite) SetUpTest(c *gc.C) {
    26  	s.RepoSuite.SetUpTest(c)
    27  	s.CmdBlockHelper = common.NewCmdBlockHelper(s.APIState)
    28  	c.Assert(s.CmdBlockHelper, gc.NotNil)
    29  	s.AddCleanup(func(*gc.C) { s.CmdBlockHelper.Close() })
    30  }
    31  
    32  var _ = gc.Suite(&RemoveUnitSuite{})
    33  
    34  func runRemoveUnit(c *gc.C, args ...string) error {
    35  	_, err := testing.RunCommand(c, NewRemoveUnitCommand(), args...)
    36  	return err
    37  }
    38  
    39  func (s *RemoveUnitSuite) setupUnitForRemove(c *gc.C) *state.Service {
    40  	ch := testcharms.Repo.CharmArchivePath(s.CharmsPath, "dummy")
    41  	err := runDeploy(c, "-n", "2", ch, "dummy", "--series", testing.FakeDefaultSeries)
    42  	c.Assert(err, jc.ErrorIsNil)
    43  	curl := charm.MustParseURL(fmt.Sprintf("local:%s/dummy-1", testing.FakeDefaultSeries))
    44  	svc, _ := s.AssertService(c, "dummy", curl, 2, 0)
    45  	return svc
    46  }
    47  
    48  func (s *RemoveUnitSuite) TestRemoveUnit(c *gc.C) {
    49  	svc := s.setupUnitForRemove(c)
    50  
    51  	err := runRemoveUnit(c, "dummy/0", "dummy/1", "dummy/2", "sillybilly/17")
    52  	c.Assert(err, gc.ErrorMatches, `some units were not destroyed: unit "dummy/2" does not exist; unit "sillybilly/17" does not exist`)
    53  	units, err := svc.AllUnits()
    54  	c.Assert(err, jc.ErrorIsNil)
    55  	for _, u := range units {
    56  		c.Assert(u.Life(), gc.Equals, state.Dying)
    57  	}
    58  }
    59  func (s *RemoveUnitSuite) TestBlockRemoveUnit(c *gc.C) {
    60  	svc := s.setupUnitForRemove(c)
    61  
    62  	// block operation
    63  	s.BlockRemoveObject(c, "TestBlockRemoveUnit")
    64  	err := runRemoveUnit(c, "dummy/0", "dummy/1")
    65  	s.AssertBlocked(c, err, ".*TestBlockRemoveUnit.*")
    66  	c.Assert(svc.Life(), gc.Equals, state.Alive)
    67  }