github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/subnet/add_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package subnet_test
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  
    10  	"github.com/juju/errors"
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  	"gopkg.in/juju/names.v2"
    14  
    15  	"github.com/juju/juju/apiserver/params"
    16  	"github.com/juju/juju/cmd/juju/subnet"
    17  	"github.com/juju/juju/network"
    18  )
    19  
    20  type AddSuite struct {
    21  	BaseSubnetSuite
    22  }
    23  
    24  var _ = gc.Suite(&AddSuite{})
    25  
    26  func (s *AddSuite) SetUpTest(c *gc.C) {
    27  	s.BaseSubnetSuite.SetUpTest(c)
    28  	s.newCommand = subnet.NewAddCommand
    29  }
    30  
    31  func (s *AddSuite) TestInit(c *gc.C) {
    32  	for i, test := range []struct {
    33  		about            string
    34  		args             []string
    35  		expectCIDR       string
    36  		expectRawCIDR    string
    37  		expectProviderId string
    38  		expectSpace      string
    39  		expectZones      []string
    40  		expectErr        string
    41  	}{{
    42  		about:     "no arguments",
    43  		expectErr: "either CIDR or provider ID is required",
    44  	}, {
    45  		about:     "single argument - invalid CIDR: space name is required",
    46  		args:      s.Strings("anything"),
    47  		expectErr: "space name is required",
    48  	}, {
    49  		about:     "single argument - valid CIDR: space name is required",
    50  		args:      s.Strings("10.0.0.0/8"),
    51  		expectErr: "space name is required",
    52  	}, {
    53  		about:     "single argument - incorrect CIDR: space name is required",
    54  		args:      s.Strings("10.10.0.0/8"),
    55  		expectErr: "space name is required",
    56  	}, {
    57  		about:            "two arguments: an invalid CIDR is assumed to mean ProviderId",
    58  		args:             s.Strings("foo", "bar"),
    59  		expectProviderId: "foo",
    60  		expectSpace:      "bar",
    61  	}, {
    62  		about:         "two arguments: an incorrectly specified CIDR is fixed",
    63  		args:          s.Strings("10.10.0.0/8", "bar"),
    64  		expectCIDR:    "10.0.0.0/8",
    65  		expectRawCIDR: "10.10.0.0/8",
    66  		expectSpace:   "bar",
    67  	}, {
    68  		about:         "more arguments parsed as zones",
    69  		args:          s.Strings("10.0.0.0/8", "new-space", "zone1", "zone2"),
    70  		expectCIDR:    "10.0.0.0/8",
    71  		expectRawCIDR: "10.0.0.0/8",
    72  		expectSpace:   "new-space",
    73  		expectZones:   s.Strings("zone1", "zone2"),
    74  	}, {
    75  		about:         "CIDR and invalid space name, one zone",
    76  		args:          s.Strings("10.10.0.0/24", "%inv$alid", "zone"),
    77  		expectCIDR:    "10.10.0.0/24",
    78  		expectRawCIDR: "10.10.0.0/24",
    79  		expectErr:     `"%inv\$alid" is not a valid space name`,
    80  	}, {
    81  		about:         "incorrect CIDR and invalid space name, no zones",
    82  		args:          s.Strings("10.10.0.0/8", "%inv$alid"),
    83  		expectCIDR:    "10.0.0.0/8",
    84  		expectRawCIDR: "10.10.0.0/8",
    85  		expectErr:     `"%inv\$alid" is not a valid space name`,
    86  	}, {
    87  		about:            "ProviderId and invalid space name, two zones",
    88  		args:             s.Strings("foo", "%inv$alid", "zone1", "zone2"),
    89  		expectProviderId: "foo",
    90  		expectErr:        `"%inv\$alid" is not a valid space name`,
    91  	}} {
    92  		c.Logf("test #%d: %s", i, test.about)
    93  		command, err := s.InitCommand(c, test.args...)
    94  		if test.expectErr != "" {
    95  			prefixedErr := "invalid arguments specified: " + test.expectErr
    96  			c.Check(err, gc.ErrorMatches, prefixedErr)
    97  		} else {
    98  			c.Check(err, jc.ErrorIsNil)
    99  			command := command.(*subnet.AddCommand)
   100  			c.Check(command.CIDR.Id(), gc.Equals, test.expectCIDR)
   101  			c.Check(command.RawCIDR, gc.Equals, test.expectRawCIDR)
   102  			c.Check(command.ProviderId, gc.Equals, test.expectProviderId)
   103  			c.Check(command.Space.Id(), gc.Equals, test.expectSpace)
   104  			c.Check(command.Zones, jc.DeepEquals, test.expectZones)
   105  		}
   106  		// No API calls should be recorded at this stage.
   107  		s.api.CheckCallNames(c)
   108  	}
   109  }
   110  
   111  func (s *AddSuite) TestRunWithIPv4CIDRSucceeds(c *gc.C) {
   112  	s.AssertRunSucceeds(c,
   113  		`added subnet with CIDR "10.20.0.0/24" in space "myspace"\n`,
   114  		"", // empty stdout.
   115  		"10.20.0.0/24", "myspace",
   116  	)
   117  
   118  	s.api.CheckCallNames(c, "AddSubnet", "Close")
   119  	s.api.CheckCall(c, 0, "AddSubnet",
   120  		names.NewSubnetTag("10.20.0.0/24"),
   121  		network.Id(""),
   122  		names.NewSpaceTag("myspace"),
   123  		[]string(nil),
   124  	)
   125  }
   126  
   127  func (s *AddSuite) TestRunWithIncorrectlyGivenCIDRSucceedsWithWarning(c *gc.C) {
   128  	expectStderr := strings.Join([]string{
   129  		"(.|\n)*",
   130  		"WARNING: using CIDR \"10.0.0.0/8\" instead of ",
   131  		"the incorrectly specified \"10.10.0.0/8\".\n",
   132  		"added subnet with CIDR \"10.0.0.0/8\" in space \"myspace\"\n",
   133  	}, "")
   134  
   135  	s.AssertRunSucceeds(c,
   136  		expectStderr,
   137  		"", // empty stdout.
   138  		"10.10.0.0/8", "myspace",
   139  	)
   140  
   141  	s.api.CheckCallNames(c, "AddSubnet", "Close")
   142  	s.api.CheckCall(c, 0, "AddSubnet",
   143  		names.NewSubnetTag("10.0.0.0/8"),
   144  		network.Id(""),
   145  		names.NewSpaceTag("myspace"),
   146  		[]string(nil),
   147  	)
   148  }
   149  
   150  func (s *AddSuite) TestRunWithProviderIdSucceeds(c *gc.C) {
   151  	s.AssertRunSucceeds(c,
   152  		`added subnet with ProviderId "foo" in space "myspace"\n`,
   153  		"", // empty stdout.
   154  		"foo", "myspace", "zone1", "zone2",
   155  	)
   156  
   157  	s.api.CheckCallNames(c, "AddSubnet", "Close")
   158  	s.api.CheckCall(c, 0, "AddSubnet",
   159  		names.SubnetTag{},
   160  		network.Id("foo"),
   161  		names.NewSpaceTag("myspace"),
   162  		s.Strings("zone1", "zone2"),
   163  	)
   164  }
   165  
   166  func (s *AddSuite) TestRunWithIPv6CIDRSucceeds(c *gc.C) {
   167  	s.AssertRunSucceeds(c,
   168  		`added subnet with CIDR "2001:db8::/32" in space "hyperspace"\n`,
   169  		"", // empty stdout.
   170  		"2001:db8::/32", "hyperspace",
   171  	)
   172  
   173  	s.api.CheckCallNames(c, "AddSubnet", "Close")
   174  	s.api.CheckCall(c, 0, "AddSubnet",
   175  		names.NewSubnetTag("2001:db8::/32"),
   176  		network.Id(""),
   177  		names.NewSpaceTag("hyperspace"),
   178  		[]string(nil),
   179  	)
   180  }
   181  
   182  func (s *AddSuite) TestRunWithExistingSubnetFails(c *gc.C) {
   183  	s.api.SetErrors(errors.AlreadyExistsf("subnet %q", "10.10.0.0/24"))
   184  
   185  	err := s.AssertRunFails(c,
   186  		`cannot add subnet: subnet "10.10.0.0/24" already exists`,
   187  		"10.10.0.0/24", "space",
   188  	)
   189  	c.Assert(err, jc.Satisfies, errors.IsAlreadyExists)
   190  
   191  	s.api.CheckCallNames(c, "AddSubnet", "Close")
   192  	s.api.CheckCall(c, 0, "AddSubnet",
   193  		names.NewSubnetTag("10.10.0.0/24"),
   194  		network.Id(""),
   195  		names.NewSpaceTag("space"),
   196  		[]string(nil),
   197  	)
   198  }
   199  
   200  func (s *AddSuite) TestRunWithNonExistingSpaceFails(c *gc.C) {
   201  	s.api.SetErrors(errors.NotFoundf("space %q", "space"))
   202  
   203  	err := s.AssertRunFails(c,
   204  		`cannot add subnet: space "space" not found`,
   205  		"10.10.0.0/24", "space", "zone1", "zone2",
   206  	)
   207  	c.Assert(err, jc.Satisfies, errors.IsNotFound)
   208  
   209  	s.api.CheckCallNames(c, "AddSubnet", "Close")
   210  	s.api.CheckCall(c, 0, "AddSubnet",
   211  		names.NewSubnetTag("10.10.0.0/24"),
   212  		network.Id(""),
   213  		names.NewSpaceTag("space"),
   214  		s.Strings("zone1", "zone2"),
   215  	)
   216  }
   217  
   218  func (s *AddSuite) TestRunUnauthorizedMentionsJujuGrant(c *gc.C) {
   219  	s.api.SetErrors(&params.Error{
   220  		Message: "permission denied",
   221  		Code:    params.CodeUnauthorized,
   222  	})
   223  	_, stderr, _ := s.RunCommand(c, "10.10.0.0/24", "myspace")
   224  	c.Assert(strings.Replace(stderr, "\n", " ", -1), gc.Matches, `.*juju grant.*`)
   225  }
   226  
   227  func (s *AddSuite) TestRunWithAmbiguousCIDRDisplaysError(c *gc.C) {
   228  	apiError := errors.New(`multiple subnets with CIDR "10.10.0.0/24" <snip>`)
   229  	s.api.SetErrors(apiError)
   230  
   231  	s.AssertRunSucceeds(c,
   232  		fmt.Sprintf("ERROR: %v.\n", apiError),
   233  		"",
   234  		"10.10.0.0/24", "space", "zone1", "zone2",
   235  	)
   236  
   237  	s.api.CheckCallNames(c, "AddSubnet", "Close")
   238  	s.api.CheckCall(c, 0, "AddSubnet",
   239  		names.NewSubnetTag("10.10.0.0/24"),
   240  		network.Id(""),
   241  		names.NewSpaceTag("space"),
   242  		s.Strings("zone1", "zone2"),
   243  	)
   244  }