github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/service/unexpose_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  	"github.com/juju/errors"
     8  	jc "github.com/juju/testing/checkers"
     9  	gc "gopkg.in/check.v1"
    10  	"gopkg.in/juju/charm.v6-unstable"
    11  
    12  	"github.com/juju/juju/cmd/juju/common"
    13  	jujutesting "github.com/juju/juju/juju/testing"
    14  	"github.com/juju/juju/rpc"
    15  	"github.com/juju/juju/testcharms"
    16  	"github.com/juju/juju/testing"
    17  )
    18  
    19  type UnexposeSuite struct {
    20  	jujutesting.RepoSuite
    21  	common.CmdBlockHelper
    22  }
    23  
    24  func (s *UnexposeSuite) SetUpTest(c *gc.C) {
    25  	s.RepoSuite.SetUpTest(c)
    26  	s.CmdBlockHelper = common.NewCmdBlockHelper(s.APIState)
    27  	c.Assert(s.CmdBlockHelper, gc.NotNil)
    28  	s.AddCleanup(func(*gc.C) { s.CmdBlockHelper.Close() })
    29  }
    30  
    31  var _ = gc.Suite(&UnexposeSuite{})
    32  
    33  func runUnexpose(c *gc.C, args ...string) error {
    34  	_, err := testing.RunCommand(c, NewUnexposeCommand(), args...)
    35  	return err
    36  }
    37  
    38  func (s *UnexposeSuite) assertExposed(c *gc.C, service string, expected bool) {
    39  	svc, err := s.State.Service(service)
    40  	c.Assert(err, jc.ErrorIsNil)
    41  	actual := svc.IsExposed()
    42  	c.Assert(actual, gc.Equals, expected)
    43  }
    44  
    45  func (s *UnexposeSuite) TestUnexpose(c *gc.C) {
    46  	ch := testcharms.Repo.CharmArchivePath(s.CharmsPath, "dummy")
    47  	err := runDeploy(c, ch, "some-service-name", "--series", "trusty")
    48  	c.Assert(err, jc.ErrorIsNil)
    49  	curl := charm.MustParseURL("local:trusty/dummy-1")
    50  	s.AssertService(c, "some-service-name", curl, 1, 0)
    51  
    52  	err = runExpose(c, "some-service-name")
    53  	c.Assert(err, jc.ErrorIsNil)
    54  	s.assertExposed(c, "some-service-name", true)
    55  
    56  	err = runUnexpose(c, "some-service-name")
    57  	c.Assert(err, jc.ErrorIsNil)
    58  	s.assertExposed(c, "some-service-name", false)
    59  
    60  	err = runUnexpose(c, "nonexistent-service")
    61  	c.Assert(errors.Cause(err), gc.DeepEquals, &rpc.RequestError{
    62  		Message: `service "nonexistent-service" not found`,
    63  		Code:    "not found",
    64  	})
    65  }
    66  
    67  func (s *UnexposeSuite) TestBlockUnexpose(c *gc.C) {
    68  	ch := testcharms.Repo.CharmArchivePath(s.CharmsPath, "dummy")
    69  	err := runDeploy(c, ch, "some-service-name", "--series", "trusty")
    70  	c.Assert(err, jc.ErrorIsNil)
    71  	curl := charm.MustParseURL("local:trusty/dummy-1")
    72  	s.AssertService(c, "some-service-name", curl, 1, 0)
    73  
    74  	// Block operation
    75  	s.BlockAllChanges(c, "TestBlockUnexpose")
    76  	err = runExpose(c, "some-service-name")
    77  	s.AssertBlocked(c, err, ".*TestBlockUnexpose.*")
    78  }