github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/cmd/juju/space/space_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  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  
    10  	"github.com/juju/juju/cmd/juju/space"
    11  	"github.com/juju/juju/feature"
    12  	coretesting "github.com/juju/juju/testing"
    13  )
    14  
    15  var mvpSubcommandNames = []string{
    16  	"create",
    17  	"list",
    18  	"help",
    19  }
    20  
    21  var postMVPSubcommandNames = []string{
    22  	"remove",
    23  	"update",
    24  	"rename",
    25  }
    26  
    27  type SpaceCommandSuite struct {
    28  	BaseSpaceSuite
    29  }
    30  
    31  var _ = gc.Suite(&SpaceCommandSuite{})
    32  
    33  func (s *SpaceCommandSuite) TestHelpSubcommandsMVP(c *gc.C) {
    34  	s.BaseSuite.SetFeatureFlags()
    35  	s.BaseSpaceSuite.SetUpTest(c) // looks evil, but works fine
    36  
    37  	ctx, err := coretesting.RunCommand(c, s.superCmd, "--help")
    38  	c.Assert(err, jc.ErrorIsNil)
    39  
    40  	namesFound := coretesting.ExtractCommandsFromHelpOutput(ctx)
    41  	c.Assert(namesFound, jc.SameContents, mvpSubcommandNames)
    42  }
    43  
    44  func (s *SpaceCommandSuite) TestHelpSubcommandsPostMVP(c *gc.C) {
    45  	s.BaseSuite.SetFeatureFlags(feature.PostNetCLIMVP)
    46  	s.BaseSpaceSuite.SetUpTest(c) // looks evil, but works fine
    47  
    48  	ctx, err := coretesting.RunCommand(c, s.superCmd, "--help")
    49  	c.Assert(err, jc.ErrorIsNil)
    50  
    51  	namesFound := coretesting.ExtractCommandsFromHelpOutput(ctx)
    52  	allSubcommandNames := append(mvpSubcommandNames, postMVPSubcommandNames...)
    53  	c.Assert(namesFound, jc.SameContents, allSubcommandNames)
    54  }
    55  
    56  func (s *SpaceCommandSuite) TestInit(c *gc.C) {
    57  	for i, test := range []struct {
    58  		about         string
    59  		args          []string
    60  		cidrsOptional bool
    61  
    62  		expectName  string
    63  		expectCIDRs []string
    64  		expectErr   string
    65  	}{{
    66  		about:     "no arguments",
    67  		expectErr: "space name is required",
    68  	}, {
    69  		about:     "invalid space name - with invalid characters",
    70  		args:      s.Strings("%inv#alid"),
    71  		expectErr: `"%inv#alid" is not a valid space name`,
    72  	}, {
    73  		about:      "valid space name with invalid CIDR",
    74  		args:       s.Strings("space-name", "noCIDR"),
    75  		expectName: "space-name",
    76  		expectErr:  `"noCIDR" is not a valid CIDR`,
    77  	}, {
    78  		about:         "valid space with one valid and one invalid CIDR (CIDRs required)",
    79  		args:          s.Strings("space-name", "10.1.0.0/16", "nonsense"),
    80  		cidrsOptional: false,
    81  		expectName:    "space-name",
    82  		expectCIDRs:   s.Strings("10.1.0.0/16"),
    83  		expectErr:     `"nonsense" is not a valid CIDR`,
    84  	}, {
    85  		about:         "valid space with one valid and one invalid CIDR (CIDRs optional)",
    86  		args:          s.Strings("space-name", "10.1.0.0/16", "nonsense"),
    87  		expectName:    "space-name",
    88  		cidrsOptional: true,
    89  		expectCIDRs:   s.Strings("10.1.0.0/16"),
    90  		expectErr:     `"nonsense" is not a valid CIDR`,
    91  	}, {
    92  		about:       "valid space with valid but overlapping CIDRs",
    93  		args:        s.Strings("space-name", "10.1.0.0/16", "10.1.0.1/16"),
    94  		expectName:  "space-name",
    95  		expectCIDRs: s.Strings("10.1.0.0/16"),
    96  		expectErr:   `subnet "10.1.0.1/16" overlaps with "10.1.0.0/16"`,
    97  	}, {
    98  		about:       "valid space with valid but duplicated CIDRs",
    99  		args:        s.Strings("space-name", "10.10.0.0/24", "10.10.0.0/24"),
   100  		expectName:  "space-name",
   101  		expectCIDRs: s.Strings("10.10.0.0/24"),
   102  		expectErr:   `duplicate subnet "10.10.0.0/24" specified`,
   103  	}, {
   104  		about:         "valid space name with no other arguments (CIDRs required)",
   105  		args:          s.Strings("space-name"),
   106  		cidrsOptional: false,
   107  		expectName:    "space-name",
   108  		expectErr:     "CIDRs required but not provided",
   109  		expectCIDRs:   s.Strings(),
   110  	}, {
   111  		about:         "valid space name with no other arguments (CIDRs optional)",
   112  		args:          s.Strings("space-name"),
   113  		cidrsOptional: true,
   114  		expectName:    "space-name",
   115  		expectCIDRs:   s.Strings(),
   116  	}, {
   117  		about:       "all ok - CIDRs updated",
   118  		args:        s.Strings("space-name", "10.10.0.0/24", "2001:db8::1/32"),
   119  		expectName:  "space-name",
   120  		expectCIDRs: s.Strings("10.10.0.0/24", "2001:db8::/32"),
   121  	}} {
   122  		c.Logf("test #%d: %s", i, test.about)
   123  		// Create a new instance of the subcommand for each test, but
   124  		// since we're not running the command no need to use
   125  		// envcmd.Wrap().
   126  		name, CIDRs, err := space.ParseNameAndCIDRs(test.args, test.cidrsOptional)
   127  		if test.expectErr != "" {
   128  			prefixedErr := "invalid arguments specified: " + test.expectErr
   129  			c.Check(err, gc.ErrorMatches, prefixedErr)
   130  		} else {
   131  			c.Check(err, jc.ErrorIsNil)
   132  		}
   133  		c.Check(name, gc.Equals, test.expectName)
   134  		c.Check(CIDRs.SortedValues(), jc.DeepEquals, test.expectCIDRs)
   135  	}
   136  }