github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/cmd/juju/space/rename_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  	jc "github.com/juju/testing/checkers"
     9  	gc "gopkg.in/check.v1"
    10  
    11  	"github.com/juju/juju/cmd/juju/space"
    12  	"github.com/juju/juju/feature"
    13  	coretesting "github.com/juju/juju/testing"
    14  )
    15  
    16  type RenameSuite struct {
    17  	BaseSpaceSuite
    18  }
    19  
    20  var _ = gc.Suite(&RenameSuite{})
    21  
    22  func (s *RenameSuite) SetUpTest(c *gc.C) {
    23  	s.BaseSuite.SetFeatureFlags(feature.PostNetCLIMVP)
    24  	s.BaseSpaceSuite.SetUpTest(c)
    25  	s.command = space.NewRenameCommand(s.api)
    26  	c.Assert(s.command, gc.NotNil)
    27  }
    28  
    29  func (s *RenameSuite) TestInit(c *gc.C) {
    30  	for i, test := range []struct {
    31  		about         string
    32  		args          []string
    33  		expectName    string
    34  		expectNewName string
    35  		expectErr     string
    36  	}{{
    37  		about:     "no arguments",
    38  		expectErr: "old-name is required",
    39  	}, {
    40  		about:     "no new name",
    41  		args:      s.Strings("a-space"),
    42  		expectErr: "new-name is required",
    43  	}, {
    44  		about:     "invalid space name - with invalid characters",
    45  		args:      s.Strings("%inv$alid", "new-name"),
    46  		expectErr: `"%inv\$alid" is not a valid space name`,
    47  	}, {
    48  		about:     "invalid space name - using underscores",
    49  		args:      s.Strings("42_space", "new-name"),
    50  		expectErr: `"42_space" is not a valid space name`,
    51  	}, {
    52  		about:     "valid space name with invalid new name",
    53  		args:      s.Strings("a-space", "inv#alid"),
    54  		expectErr: `"inv#alid" is not a valid space name`,
    55  	}, {
    56  		about:     "valid space name with CIDR as new name",
    57  		args:      s.Strings("a-space", "1.2.3.4/24"),
    58  		expectErr: `"1.2.3.4/24" is not a valid space name`,
    59  	}, {
    60  		about:         "more than two arguments",
    61  		args:          s.Strings("a-space", "another-space", "rubbish"),
    62  		expectErr:     `unrecognized args: \["rubbish"\]`,
    63  		expectName:    "a-space",
    64  		expectNewName: "another-space",
    65  	}, {
    66  		about:         "old and new names are the same",
    67  		args:          s.Strings("a-space", "a-space"),
    68  		expectName:    "a-space",
    69  		expectNewName: "a-space",
    70  		expectErr:     "old-name and new-name are the same",
    71  	}, {
    72  		about:         "all ok",
    73  		args:          s.Strings("a-space", "another-space"),
    74  		expectName:    "a-space",
    75  		expectNewName: "another-space",
    76  	}} {
    77  		c.Logf("test #%d: %s", i, test.about)
    78  		// Create a new instance of the subcommand for each test, but
    79  		// since we're not running the command no need to use
    80  		// envcmd.Wrap().
    81  		command := space.NewRenameCommand(s.api) // surely can use s.command??
    82  		err := coretesting.InitCommand(command, test.args)
    83  		if test.expectErr != "" {
    84  			prefixedErr := "invalid arguments specified: " + test.expectErr
    85  			c.Check(err, gc.ErrorMatches, prefixedErr)
    86  		} else {
    87  			c.Check(err, jc.ErrorIsNil)
    88  		}
    89  		c.Check(command.Name, gc.Equals, test.expectName)
    90  		c.Check(command.NewName, gc.Equals, test.expectNewName)
    91  		// No API calls should be recorded at this stage.
    92  		s.api.CheckCallNames(c)
    93  	}
    94  }
    95  
    96  func (s *RenameSuite) TestRunWithValidNamesSucceeds(c *gc.C) {
    97  	s.AssertRunSucceeds(c,
    98  		`renamed space "a-space" to "another-space"\n`,
    99  		"", // no stdout, just stderr
   100  		"a-space", "another-space",
   101  	)
   102  
   103  	s.api.CheckCallNames(c, "RenameSpace", "Close")
   104  	s.api.CheckCall(c, 0, "RenameSpace", "a-space", "another-space")
   105  }
   106  
   107  func (s *RenameSuite) TestRunWhenSpacesAPIFails(c *gc.C) {
   108  	s.api.SetErrors(errors.New("boom"))
   109  
   110  	s.AssertRunFails(c,
   111  		`cannot rename space "foo": boom`,
   112  		"foo", "bar",
   113  	)
   114  
   115  	s.api.CheckCallNames(c, "RenameSpace", "Close")
   116  	s.api.CheckCall(c, 0, "RenameSpace", "foo", "bar")
   117  }
   118  
   119  func (s *RenameSuite) TestRunAPIConnectFails(c *gc.C) {
   120  	s.command = space.NewRenameCommand(nil)
   121  	s.AssertRunFails(c,
   122  		"cannot connect to the API server: no environment specified",
   123  		"myname", "newname", // Drop the args once RunWitnAPI is called internally.
   124  	)
   125  	// No API calls recoreded.
   126  	s.api.CheckCallNames(c)
   127  }