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

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package space_test
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/juju/feature"
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/cmd/juju/space"
    13  	coretesting "github.com/juju/juju/testing"
    14  )
    15  
    16  type RemoveSuite struct {
    17  	BaseSpaceSuite
    18  }
    19  
    20  var _ = gc.Suite(&RemoveSuite{})
    21  
    22  func (s *RemoveSuite) SetUpTest(c *gc.C) {
    23  	s.BaseSuite.SetFeatureFlags(feature.PostNetCLIMVP)
    24  	s.BaseSpaceSuite.SetUpTest(c)
    25  	s.command, _ = space.NewRemoveCommandForTest(s.api)
    26  	c.Assert(s.command, gc.NotNil)
    27  }
    28  
    29  func (s *RemoveSuite) TestInit(c *gc.C) {
    30  	for i, test := range []struct {
    31  		about      string
    32  		args       []string
    33  		expectName string
    34  		expectErr  string
    35  	}{{
    36  		about:     "no arguments",
    37  		expectErr: "space name is required",
    38  	}, {
    39  		about:     "invalid space name",
    40  		args:      s.Strings("%inv$alid", "new-name"),
    41  		expectErr: `"%inv\$alid" is not a valid space name`,
    42  	}, {
    43  		about:      "multiple space names aren't allowed",
    44  		args:       s.Strings("a-space", "another-space"),
    45  		expectErr:  `unrecognized args: \["another-space"\]`,
    46  		expectName: "a-space",
    47  	}, {
    48  		about:      "delete a valid space name",
    49  		args:       s.Strings("myspace"),
    50  		expectName: "myspace",
    51  	}} {
    52  		c.Logf("test #%d: %s", i, test.about)
    53  		// Create a new instance of the subcommand for each test, but
    54  		// since we're not running the command no need to use
    55  		// modelcmd.Wrap().
    56  		wrappedCommand, command := space.NewRemoveCommandForTest(s.api)
    57  		err := coretesting.InitCommand(wrappedCommand, test.args)
    58  		if test.expectErr != "" {
    59  			prefixedErr := "invalid arguments specified: " + test.expectErr
    60  			c.Check(err, gc.ErrorMatches, prefixedErr)
    61  		} else {
    62  			c.Check(err, jc.ErrorIsNil)
    63  		}
    64  		c.Check(command.Name(), gc.Equals, test.expectName)
    65  		// No API calls should be recorded at this stage.
    66  		s.api.CheckCallNames(c)
    67  	}
    68  }
    69  
    70  func (s *RemoveSuite) TestRunWithValidSpaceSucceeds(c *gc.C) {
    71  	s.AssertRunSucceeds(c,
    72  		`removed space "myspace"\n`,
    73  		"", // no stdout, just stderr
    74  		"myspace",
    75  	)
    76  
    77  	s.api.CheckCallNames(c, "RemoveSpace", "Close")
    78  	s.api.CheckCall(c, 0, "RemoveSpace", "myspace")
    79  }
    80  
    81  func (s *RemoveSuite) TestRunWhenSpacesAPIFails(c *gc.C) {
    82  	s.api.SetErrors(errors.New("boom"))
    83  
    84  	s.AssertRunFails(c,
    85  		`cannot remove space "myspace": boom`,
    86  		"myspace",
    87  	)
    88  
    89  	s.api.CheckCallNames(c, "RemoveSpace", "Close")
    90  	s.api.CheckCall(c, 0, "RemoveSpace", "myspace")
    91  }