github.com/mwhudson/juju@v0.0.0-20160512215208-90ff01f3497f/featuretests/cmd_juju_space_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package featuretests
     5  
     6  import (
     7  	"fmt"
     8  	"math/rand"
     9  
    10  	"github.com/juju/cmd"
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	jujutesting "github.com/juju/juju/juju/testing"
    15  	"github.com/juju/juju/network"
    16  	"github.com/juju/juju/provider/dummy"
    17  	"github.com/juju/juju/state"
    18  	"github.com/juju/juju/testing"
    19  )
    20  
    21  type cmdSpaceSuite struct {
    22  	jujutesting.JujuConnSuite
    23  }
    24  
    25  func (s *cmdSpaceSuite) AddSubnets(c *gc.C, infos []state.SubnetInfo) []*state.Subnet {
    26  	results := make([]*state.Subnet, len(infos))
    27  	for i, info := range infos {
    28  		subnet, err := s.State.AddSubnet(info)
    29  		c.Assert(err, jc.ErrorIsNil)
    30  		c.Assert(subnet.CIDR(), gc.Equals, info.CIDR)
    31  		results[i] = subnet
    32  	}
    33  	return results
    34  }
    35  
    36  func (s *cmdSpaceSuite) MakeSubnetInfos(c *gc.C, space string, cidrTemplate string, count int) (
    37  	infos []state.SubnetInfo,
    38  	ids []string,
    39  ) {
    40  	infos = make([]state.SubnetInfo, count)
    41  	ids = make([]string, count)
    42  	for i := range infos {
    43  		ids[i] = fmt.Sprintf(cidrTemplate, i)
    44  		infos[i] = state.SubnetInfo{
    45  			// ProviderId it needs to be unique in state.
    46  			ProviderId:       network.Id(fmt.Sprintf("sub-%d", rand.Int())),
    47  			CIDR:             ids[i],
    48  			SpaceName:        space,
    49  			AvailabilityZone: "zone1",
    50  		}
    51  	}
    52  	return infos, ids
    53  }
    54  
    55  func (s *cmdSpaceSuite) AddSpace(c *gc.C, name string, ids []string, public bool) *state.Space {
    56  	space, err := s.State.AddSpace(name, "", ids, public)
    57  	c.Assert(err, jc.ErrorIsNil)
    58  	c.Assert(space.Name(), gc.Equals, name)
    59  	subnets, err := space.Subnets()
    60  	c.Assert(err, jc.ErrorIsNil)
    61  	c.Assert(subnets, gc.HasLen, len(ids))
    62  	return space
    63  }
    64  
    65  const expectedSuccess = ""
    66  
    67  func (s *cmdSpaceSuite) Run(c *gc.C, args ...string) (string, string, error) {
    68  	context, err := runJujuCommand(c, args...)
    69  	stdout, stderr := "", ""
    70  	if context != nil {
    71  		stdout = testing.Stdout(context)
    72  		stderr = testing.Stderr(context)
    73  	}
    74  	return stdout, stderr, err
    75  }
    76  
    77  func (s *cmdSpaceSuite) RunAdd(c *gc.C, expectedError string, args ...string) {
    78  	cmdArgs := append([]string{"add-space"}, args...)
    79  	_, stderr, err := s.Run(c, cmdArgs...)
    80  	if expectedError != "" {
    81  		c.Assert(err, gc.NotNil)
    82  		c.Assert(stderr, jc.Contains, expectedError)
    83  	} else {
    84  		c.Assert(err, jc.ErrorIsNil)
    85  	}
    86  }
    87  
    88  func (s *cmdSpaceSuite) RunList(c *gc.C, expectedError string, args ...string) {
    89  	cmdArgs := append([]string{"list-spaces"}, args...)
    90  	_, stderr, err := s.Run(c, cmdArgs...)
    91  	if expectedError != "" {
    92  		c.Assert(err, gc.NotNil)
    93  		c.Assert(stderr, jc.Contains, expectedError)
    94  	} else {
    95  		c.Assert(err, jc.ErrorIsNil)
    96  	}
    97  }
    98  
    99  func (s *cmdSpaceSuite) AssertOutput(c *gc.C, context *cmd.Context, expectedOut, expectedErr string) {
   100  	c.Assert(testing.Stdout(context), gc.Equals, expectedOut)
   101  	c.Assert(testing.Stderr(context), gc.Equals, expectedErr)
   102  }
   103  
   104  func (s *cmdSpaceSuite) TestSpaceCreateNotSupported(c *gc.C) {
   105  	isEnabled := dummy.SetSupportsSpaces(false)
   106  	defer dummy.SetSupportsSpaces(isEnabled)
   107  
   108  	expectedError := "cannot add space \"foo\": spaces not supported"
   109  	s.RunAdd(c, expectedError, "foo")
   110  }
   111  
   112  func (s *cmdSpaceSuite) TestSpaceCreateNoName(c *gc.C) {
   113  	expectedError := "invalid arguments specified: space name is required"
   114  	s.RunAdd(c, expectedError)
   115  }
   116  
   117  func (s *cmdSpaceSuite) TestSpaceCreateInvalidName(c *gc.C) {
   118  	expectedError := `invalid arguments specified: " f o o " is not a valid space name`
   119  	s.RunAdd(c, expectedError, " f o o ")
   120  }
   121  
   122  func (s *cmdSpaceSuite) TestSpaceCreateWithInvalidSubnets(c *gc.C) {
   123  	expectedError := `invalid arguments specified: "nonsense" is not a valid CIDR`
   124  	s.RunAdd(c, expectedError, "myspace", "nonsense", "10.20.0.0/16")
   125  }
   126  
   127  func (s *cmdSpaceSuite) TestSpaceCreateWithUnknownSubnet(c *gc.C) {
   128  	expectedError := `cannot add space "foo": adding space "foo": subnet "10.10.0.0/16" not found`
   129  	s.RunAdd(c, expectedError, "foo", "10.10.0.0/16")
   130  }
   131  
   132  func (s *cmdSpaceSuite) TestSpaceCreateAlreadyExistingName(c *gc.C) {
   133  	s.AddSpace(c, "foo", nil, true)
   134  
   135  	expectedError := `cannot add space "foo": adding space "foo": space "foo" already exists`
   136  	s.RunAdd(c, expectedError, "foo")
   137  }
   138  
   139  func (s *cmdSpaceSuite) TestSpaceCreateNoSubnets(c *gc.C) {
   140  	stdout, stderr, err := s.Run(c, "add-space", "myspace")
   141  	c.Assert(err, jc.ErrorIsNil)
   142  	c.Assert(stdout, gc.Equals, "")
   143  	c.Assert(stderr, jc.Contains, "added space \"myspace\" with no subnets\n")
   144  
   145  	space, err := s.State.Space("myspace")
   146  	c.Assert(err, jc.ErrorIsNil)
   147  	c.Assert(space.Name(), gc.Equals, "myspace")
   148  	subnets, err := space.Subnets()
   149  	c.Assert(err, jc.ErrorIsNil)
   150  	c.Assert(subnets, gc.HasLen, 0)
   151  }
   152  
   153  func (s *cmdSpaceSuite) TestSpaceCreateWithSubnets(c *gc.C) {
   154  	infos, _ := s.MakeSubnetInfos(c, "", "10.1%d.0.0/16", 2)
   155  	s.AddSubnets(c, infos)
   156  
   157  	_, stderr, err := s.Run(
   158  		c,
   159  		"add-space", "myspace", "10.10.0.0/16", "10.11.0.0/16",
   160  	)
   161  	c.Assert(err, jc.ErrorIsNil)
   162  	c.Assert(stderr, jc.Contains,
   163  		"added space \"myspace\" with subnets 10.10.0.0/16, 10.11.0.0/16\n",
   164  	)
   165  
   166  	space, err := s.State.Space("myspace")
   167  	c.Assert(err, jc.ErrorIsNil)
   168  	c.Assert(space.Name(), gc.Equals, "myspace")
   169  	subnets, err := space.Subnets()
   170  	c.Assert(err, jc.ErrorIsNil)
   171  	c.Assert(subnets, gc.HasLen, 2)
   172  	c.Assert(subnets[0].SpaceName(), gc.Equals, "myspace")
   173  	c.Assert(subnets[1].SpaceName(), gc.Equals, "myspace")
   174  }
   175  
   176  func (s *cmdSpaceSuite) TestSpaceListNoResults(c *gc.C) {
   177  	_, stderr, err := s.Run(c, "spaces")
   178  	c.Assert(err, jc.ErrorIsNil)
   179  	c.Assert(stderr, jc.Contains,
   180  		"no spaces to display\n",
   181  	)
   182  }
   183  
   184  func (s *cmdSpaceSuite) TestSpaceListOneResultNoSubnets(c *gc.C) {
   185  	s.AddSpace(c, "myspace", nil, true)
   186  
   187  	expectedOutput := "{\"spaces\":{\"myspace\":{}}}\n"
   188  	stdout, _, err := s.Run(c, "list-spaces", "--format", "json")
   189  	c.Assert(err, jc.ErrorIsNil)
   190  	c.Assert(stdout, jc.Contains, expectedOutput)
   191  }
   192  
   193  func (s *cmdSpaceSuite) TestSpaceListMoreResults(c *gc.C) {
   194  	infos1, ids1 := s.MakeSubnetInfos(c, "space1", "10.10.%d.0/24", 3)
   195  	s.AddSubnets(c, infos1)
   196  	s.AddSpace(c, "space1", ids1, true)
   197  
   198  	infos2, ids2 := s.MakeSubnetInfos(c, "space2", "10.20.%d.0/24", 1)
   199  	s.AddSubnets(c, infos2)
   200  	s.AddSpace(c, "space2", ids2, false)
   201  
   202  	stdout, stderr, err := s.Run(c, "list-spaces", "--format", "yaml")
   203  	c.Assert(err, jc.ErrorIsNil)
   204  	c.Assert(stderr, gc.Equals, "")
   205  
   206  	// We dont' check the output in detail, just a few things - the
   207  	// rest it tested separately.
   208  	c.Assert(stdout, jc.Contains, "spaces:")
   209  	c.Assert(stdout, jc.Contains, "space1:")
   210  	c.Assert(stdout, jc.Contains, "10.10.2.0/24:")
   211  	c.Assert(stdout, jc.Contains, "space2:")
   212  	c.Assert(stdout, jc.Contains, "10.20.0.0/24:")
   213  	c.Assert(stdout, jc.Contains, "zones:")
   214  	c.Assert(stdout, jc.Contains, "zone1")
   215  }
   216  
   217  func (s *cmdSpaceSuite) TestSpaceListNotSupported(c *gc.C) {
   218  	isEnabled := dummy.SetSupportsSpaces(false)
   219  	defer dummy.SetSupportsSpaces(isEnabled)
   220  
   221  	expectedError := "cannot list spaces: spaces not supported"
   222  	s.RunList(c, expectedError)
   223  }