github.com/mwhudson/juju@v0.0.0-20160512215208-90ff01f3497f/cmd/juju/service/removeservice_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package service
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	"github.com/juju/errors"
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  	"gopkg.in/macaroon-bakery.v1/httpbakery"
    12  
    13  	"github.com/juju/juju/cmd/juju/common"
    14  	"github.com/juju/juju/cmd/modelcmd"
    15  	jujutesting "github.com/juju/juju/juju/testing"
    16  	"github.com/juju/juju/rpc"
    17  	"github.com/juju/juju/state"
    18  	"github.com/juju/juju/testcharms"
    19  	"github.com/juju/juju/testing"
    20  	jutesting "github.com/juju/testing"
    21  )
    22  
    23  type RemoveServiceSuite struct {
    24  	jujutesting.RepoSuite
    25  	common.CmdBlockHelper
    26  	stub            *jutesting.Stub
    27  	budgetAPIClient budgetAPIClient
    28  }
    29  
    30  var _ = gc.Suite(&RemoveServiceSuite{})
    31  
    32  func (s *RemoveServiceSuite) SetUpTest(c *gc.C) {
    33  	s.RepoSuite.SetUpTest(c)
    34  	s.CmdBlockHelper = common.NewCmdBlockHelper(s.APIState)
    35  	c.Assert(s.CmdBlockHelper, gc.NotNil)
    36  	s.AddCleanup(func(*gc.C) { s.CmdBlockHelper.Close() })
    37  	s.stub = &jutesting.Stub{}
    38  	s.budgetAPIClient = &mockBudgetAPIClient{Stub: s.stub}
    39  	s.PatchValue(&getBudgetAPIClient, func(*httpbakery.Client) budgetAPIClient { return s.budgetAPIClient })
    40  }
    41  
    42  func runRemoveService(c *gc.C, args ...string) error {
    43  	_, err := testing.RunCommand(c, NewRemoveServiceCommand(), args...)
    44  	return err
    45  }
    46  
    47  func (s *RemoveServiceSuite) setupTestService(c *gc.C) {
    48  	// Destroy a service that exists.
    49  	ch := testcharms.Repo.CharmArchivePath(s.CharmsPath, "riak")
    50  	err := runDeploy(c, ch, "riak", "--series", "quantal")
    51  	c.Assert(err, jc.ErrorIsNil)
    52  }
    53  
    54  func (s *RemoveServiceSuite) TestSuccess(c *gc.C) {
    55  	s.setupTestService(c)
    56  	err := runRemoveService(c, "riak")
    57  	c.Assert(err, jc.ErrorIsNil)
    58  	riak, err := s.State.Service("riak")
    59  	c.Assert(err, jc.ErrorIsNil)
    60  	c.Assert(riak.Life(), gc.Equals, state.Dying)
    61  	s.stub.CheckNoCalls(c)
    62  }
    63  
    64  func (s *RemoveServiceSuite) TestRemoveLocalMetered(c *gc.C) {
    65  	ch := testcharms.Repo.CharmArchivePath(s.CharmsPath, "metered")
    66  	deploy := &DeployCommand{}
    67  	_, err := testing.RunCommand(c, modelcmd.Wrap(deploy), ch, "--series", "quantal")
    68  	c.Assert(err, jc.ErrorIsNil)
    69  	err = runRemoveService(c, "metered")
    70  	c.Assert(err, jc.ErrorIsNil)
    71  	riak, err := s.State.Service("metered")
    72  	c.Assert(err, jc.ErrorIsNil)
    73  	c.Assert(riak.Life(), gc.Equals, state.Dying)
    74  	s.stub.CheckNoCalls(c)
    75  }
    76  
    77  func (s *RemoveServiceSuite) TestBlockRemoveService(c *gc.C) {
    78  	s.setupTestService(c)
    79  
    80  	// block operation
    81  	s.BlockRemoveObject(c, "TestBlockRemoveService")
    82  	err := runRemoveService(c, "riak")
    83  	s.AssertBlocked(c, err, ".*TestBlockRemoveService.*")
    84  	riak, err := s.State.Service("riak")
    85  	c.Assert(err, jc.ErrorIsNil)
    86  	c.Assert(riak.Life(), gc.Equals, state.Alive)
    87  	s.stub.CheckNoCalls(c)
    88  }
    89  
    90  func (s *RemoveServiceSuite) TestFailure(c *gc.C) {
    91  	// Destroy a service that does not exist.
    92  	err := runRemoveService(c, "gargleblaster")
    93  	c.Assert(errors.Cause(err), gc.DeepEquals, &rpc.RequestError{
    94  		Message: `service "gargleblaster" not found`,
    95  		Code:    "not found",
    96  	})
    97  	s.stub.CheckNoCalls(c)
    98  }
    99  
   100  func (s *RemoveServiceSuite) TestInvalidArgs(c *gc.C) {
   101  	err := runRemoveService(c)
   102  	c.Assert(err, gc.ErrorMatches, `no service specified`)
   103  	err = runRemoveService(c, "ping", "pong")
   104  	c.Assert(err, gc.ErrorMatches, `unrecognized args: \["pong"\]`)
   105  	err = runRemoveService(c, "invalid:name")
   106  	c.Assert(err, gc.ErrorMatches, `invalid service name "invalid:name"`)
   107  	s.stub.CheckNoCalls(c)
   108  }
   109  
   110  type RemoveCharmStoreCharmsSuite struct {
   111  	charmStoreSuite
   112  	stub            *jutesting.Stub
   113  	ctx             *cmd.Context
   114  	budgetAPIClient budgetAPIClient
   115  }
   116  
   117  var _ = gc.Suite(&RemoveCharmStoreCharmsSuite{})
   118  
   119  func (s *RemoveCharmStoreCharmsSuite) SetUpTest(c *gc.C) {
   120  	s.charmStoreSuite.SetUpTest(c)
   121  
   122  	s.ctx = testing.Context(c)
   123  	s.stub = &jutesting.Stub{}
   124  	s.budgetAPIClient = &mockBudgetAPIClient{Stub: s.stub}
   125  	s.PatchValue(&getBudgetAPIClient, func(*httpbakery.Client) budgetAPIClient { return s.budgetAPIClient })
   126  
   127  	testcharms.UploadCharm(c, s.client, "cs:quantal/metered-1", "metered")
   128  	deploy := &DeployCommand{}
   129  	_, err := testing.RunCommand(c, modelcmd.Wrap(deploy), "cs:quantal/metered-1")
   130  	c.Assert(err, jc.ErrorIsNil)
   131  
   132  }
   133  
   134  func (s *RemoveCharmStoreCharmsSuite) TestRemoveAllocation(c *gc.C) {
   135  	err := runRemoveService(c, "metered")
   136  	c.Assert(err, jc.ErrorIsNil)
   137  	s.stub.CheckCalls(c, []jutesting.StubCall{{
   138  		"DeleteAllocation", []interface{}{testing.ModelTag.Id(), "metered"}}})
   139  }
   140  
   141  type mockBudgetAPIClient struct {
   142  	*jutesting.Stub
   143  }
   144  
   145  // CreateAllocation implements apiClient.
   146  func (c *mockBudgetAPIClient) CreateAllocation(budget, limit, model string, services []string) (string, error) {
   147  	c.MethodCall(c, "CreateAllocation", budget, limit, model, services)
   148  	return "Allocation created.", c.NextErr()
   149  }
   150  
   151  // DeleteAllocation implements apiClient.
   152  func (c *mockBudgetAPIClient) DeleteAllocation(model, service string) (string, error) {
   153  	c.MethodCall(c, "DeleteAllocation", model, service)
   154  	return "Allocation removed.", c.NextErr()
   155  }