github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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  )
    12  
    13  type SpaceCommandSuite struct {
    14  	BaseSpaceSuite
    15  }
    16  
    17  var _ = gc.Suite(&SpaceCommandSuite{})
    18  
    19  func (s *SpaceCommandSuite) TestInit(c *gc.C) {
    20  	for i, test := range []struct {
    21  		about         string
    22  		args          []string
    23  		cidrsOptional bool
    24  
    25  		expectName  string
    26  		expectCIDRs []string
    27  		expectErr   string
    28  	}{{
    29  		about:     "no arguments",
    30  		expectErr: "space name is required",
    31  	}, {
    32  		about:     "invalid space name - with invalid characters",
    33  		args:      s.Strings("%inv#alid"),
    34  		expectErr: `"%inv#alid" is not a valid space name`,
    35  	}, {
    36  		about:      "valid space name with invalid CIDR",
    37  		args:       s.Strings("space-name", "noCIDR"),
    38  		expectName: "space-name",
    39  		expectErr:  `"noCIDR" is not a valid CIDR`,
    40  	}, {
    41  		about:         "valid space with one valid and one invalid CIDR (CIDRs required)",
    42  		args:          s.Strings("space-name", "10.1.0.0/16", "nonsense"),
    43  		cidrsOptional: false,
    44  		expectName:    "space-name",
    45  		expectCIDRs:   s.Strings("10.1.0.0/16"),
    46  		expectErr:     `"nonsense" is not a valid CIDR`,
    47  	}, {
    48  		about:         "valid space with one valid and one invalid CIDR (CIDRs optional)",
    49  		args:          s.Strings("space-name", "10.1.0.0/16", "nonsense"),
    50  		expectName:    "space-name",
    51  		cidrsOptional: true,
    52  		expectCIDRs:   s.Strings("10.1.0.0/16"),
    53  		expectErr:     `"nonsense" is not a valid CIDR`,
    54  	}, {
    55  		about:       "valid space with valid but overlapping CIDRs",
    56  		args:        s.Strings("space-name", "10.1.0.0/16", "10.1.0.1/16"),
    57  		expectName:  "space-name",
    58  		expectCIDRs: s.Strings("10.1.0.0/16"),
    59  		expectErr:   `subnet "10.1.0.1/16" overlaps with "10.1.0.0/16"`,
    60  	}, {
    61  		about:       "valid space with valid but duplicated CIDRs",
    62  		args:        s.Strings("space-name", "10.10.0.0/24", "10.10.0.0/24"),
    63  		expectName:  "space-name",
    64  		expectCIDRs: s.Strings("10.10.0.0/24"),
    65  		expectErr:   `duplicate subnet "10.10.0.0/24" specified`,
    66  	}, {
    67  		about:         "valid space name with no other arguments (CIDRs required)",
    68  		args:          s.Strings("space-name"),
    69  		cidrsOptional: false,
    70  		expectName:    "space-name",
    71  		expectErr:     "CIDRs required but not provided",
    72  		expectCIDRs:   s.Strings(),
    73  	}, {
    74  		about:         "valid space name with no other arguments (CIDRs optional)",
    75  		args:          s.Strings("space-name"),
    76  		cidrsOptional: true,
    77  		expectName:    "space-name",
    78  		expectCIDRs:   s.Strings(),
    79  	}, {
    80  		about:       "all ok - CIDRs updated",
    81  		args:        s.Strings("space-name", "10.10.0.0/24", "2001:db8::1/32"),
    82  		expectName:  "space-name",
    83  		expectCIDRs: s.Strings("10.10.0.0/24", "2001:db8::/32"),
    84  	}} {
    85  		c.Logf("test #%d: %s", i, test.about)
    86  		// Create a new instance of the subcommand for each test, but
    87  		// since we're not running the command no need to use
    88  		// modelcmd.Wrap().
    89  		name, CIDRs, err := space.ParseNameAndCIDRs(test.args, test.cidrsOptional)
    90  		if test.expectErr != "" {
    91  			prefixedErr := "invalid arguments specified: " + test.expectErr
    92  			c.Check(err, gc.ErrorMatches, prefixedErr)
    93  		} else {
    94  			c.Check(err, jc.ErrorIsNil)
    95  		}
    96  		c.Check(name, gc.Equals, test.expectName)
    97  		c.Check(CIDRs.SortedValues(), jc.DeepEquals, test.expectCIDRs)
    98  	}
    99  }