github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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  )
    14  
    15  type RenameSuite struct {
    16  	BaseSpaceSuite
    17  }
    18  
    19  var _ = gc.Suite(&RenameSuite{})
    20  
    21  func (s *RenameSuite) SetUpTest(c *gc.C) {
    22  	s.BaseSuite.SetFeatureFlags(feature.PostNetCLIMVP)
    23  	s.BaseSpaceSuite.SetUpTest(c)
    24  	s.newCommand = space.NewRenameCommand
    25  }
    26  
    27  func (s *RenameSuite) TestInit(c *gc.C) {
    28  	for i, test := range []struct {
    29  		about         string
    30  		args          []string
    31  		expectName    string
    32  		expectNewName string
    33  		expectErr     string
    34  	}{{
    35  		about:     "no arguments",
    36  		expectErr: "old-name is required",
    37  	}, {
    38  		about:     "no new name",
    39  		args:      s.Strings("a-space"),
    40  		expectErr: "new-name is required",
    41  	}, {
    42  		about:     "invalid space name - with invalid characters",
    43  		args:      s.Strings("%inv$alid", "new-name"),
    44  		expectErr: `"%inv\$alid" is not a valid space name`,
    45  	}, {
    46  		about:     "invalid space name - using underscores",
    47  		args:      s.Strings("42_space", "new-name"),
    48  		expectErr: `"42_space" is not a valid space name`,
    49  	}, {
    50  		about:     "valid space name with invalid new name",
    51  		args:      s.Strings("a-space", "inv#alid"),
    52  		expectErr: `"inv#alid" is not a valid space name`,
    53  	}, {
    54  		about:     "valid space name with CIDR as new name",
    55  		args:      s.Strings("a-space", "1.2.3.4/24"),
    56  		expectErr: `"1.2.3.4/24" is not a valid space name`,
    57  	}, {
    58  		about:         "more than two arguments",
    59  		args:          s.Strings("a-space", "another-space", "rubbish"),
    60  		expectErr:     `unrecognized args: \["rubbish"\]`,
    61  		expectName:    "a-space",
    62  		expectNewName: "another-space",
    63  	}, {
    64  		about:         "old and new names are the same",
    65  		args:          s.Strings("a-space", "a-space"),
    66  		expectName:    "a-space",
    67  		expectNewName: "a-space",
    68  		expectErr:     "old-name and new-name are the same",
    69  	}, {
    70  		about:         "all ok",
    71  		args:          s.Strings("a-space", "another-space"),
    72  		expectName:    "a-space",
    73  		expectNewName: "another-space",
    74  	}} {
    75  		c.Logf("test #%d: %s", i, test.about)
    76  		command, err := s.InitCommand(c, test.args...)
    77  		if test.expectErr != "" {
    78  			prefixedErr := "invalid arguments specified: " + test.expectErr
    79  			c.Check(err, gc.ErrorMatches, prefixedErr)
    80  		} else {
    81  			c.Check(err, jc.ErrorIsNil)
    82  			command := command.(*space.RenameCommand)
    83  			c.Check(command.Name, gc.Equals, test.expectName)
    84  			c.Check(command.NewName, gc.Equals, test.expectNewName)
    85  		}
    86  		// No API calls should be recorded at this stage.
    87  		s.api.CheckCallNames(c)
    88  	}
    89  }
    90  
    91  func (s *RenameSuite) TestRunWithValidNamesSucceeds(c *gc.C) {
    92  	s.AssertRunSucceeds(c,
    93  		`renamed space "a-space" to "another-space"\n`,
    94  		"", // no stdout, just stderr
    95  		"a-space", "another-space",
    96  	)
    97  
    98  	s.api.CheckCallNames(c, "RenameSpace", "Close")
    99  	s.api.CheckCall(c, 0, "RenameSpace", "a-space", "another-space")
   100  }
   101  
   102  func (s *RenameSuite) TestRunWhenSpacesAPIFails(c *gc.C) {
   103  	s.api.SetErrors(errors.New("boom"))
   104  
   105  	s.AssertRunFails(c,
   106  		`cannot rename space "foo": boom`,
   107  		"foo", "bar",
   108  	)
   109  
   110  	s.api.CheckCallNames(c, "RenameSpace", "Close")
   111  	s.api.CheckCall(c, 0, "RenameSpace", "foo", "bar")
   112  }