github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/cmd/juju/subnet/subnet_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  	"errors"
     8  
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/cmd/juju/subnet"
    13  	"github.com/juju/juju/feature"
    14  	coretesting "github.com/juju/juju/testing"
    15  )
    16  
    17  var mvpSubcommandNames = []string{
    18  	"add",
    19  	"list",
    20  	"help",
    21  }
    22  
    23  var postMVPSubcommandNames = []string{
    24  	"create",
    25  	"remove",
    26  }
    27  
    28  type SubnetCommandSuite struct {
    29  	BaseSubnetSuite
    30  }
    31  
    32  var _ = gc.Suite(&SubnetCommandSuite{})
    33  
    34  func (s *SubnetCommandSuite) TestHelpSubcommandsMVP(c *gc.C) {
    35  	s.BaseSuite.SetFeatureFlags()
    36  	s.BaseSubnetSuite.SetUpTest(c) // looks evil, but works fine
    37  
    38  	ctx, err := coretesting.RunCommand(c, s.superCmd, "--help")
    39  	c.Assert(err, jc.ErrorIsNil)
    40  
    41  	namesFound := coretesting.ExtractCommandsFromHelpOutput(ctx)
    42  	c.Assert(namesFound, jc.SameContents, mvpSubcommandNames)
    43  }
    44  
    45  func (s *SubnetCommandSuite) TestHelpSubcommandsPostMVP(c *gc.C) {
    46  	s.BaseSuite.SetFeatureFlags(feature.PostNetCLIMVP)
    47  	s.BaseSubnetSuite.SetUpTest(c) // looks evil, but works fine
    48  
    49  	ctx, err := coretesting.RunCommand(c, s.superCmd, "--help")
    50  	c.Assert(err, jc.ErrorIsNil)
    51  
    52  	namesFound := coretesting.ExtractCommandsFromHelpOutput(ctx)
    53  	allSubcommandNames := append(mvpSubcommandNames, postMVPSubcommandNames...)
    54  	c.Assert(namesFound, jc.SameContents, allSubcommandNames)
    55  }
    56  
    57  type SubnetCommandBaseSuite struct {
    58  	coretesting.BaseSuite
    59  
    60  	baseCmd *subnet.SubnetCommandBase
    61  }
    62  
    63  func (s *SubnetCommandBaseSuite) SetUpTest(c *gc.C) {
    64  	s.BaseSuite.SetUpTest(c)
    65  	s.baseCmd = &subnet.SubnetCommandBase{}
    66  }
    67  
    68  var _ = gc.Suite(&SubnetCommandBaseSuite{})
    69  
    70  func (s *SubnetCommandBaseSuite) TestCheckNumArgs(c *gc.C) {
    71  	threeErrors := []error{
    72  		errors.New("first"),
    73  		errors.New("second"),
    74  		errors.New("third"),
    75  	}
    76  	twoErrors := threeErrors[:2]
    77  	oneError := threeErrors[:1]
    78  	threeArgs := []string{"foo", "bar", "baz"}
    79  	twoArgs := threeArgs[:2]
    80  	oneArg := threeArgs[:1]
    81  
    82  	for i, errs := range [][]error{nil, oneError, twoErrors, threeErrors} {
    83  		for j, args := range [][]string{nil, oneArg, twoArgs, threeArgs} {
    84  			expectErr := ""
    85  			if i > j {
    86  				// Returned error is always the one with index equal
    87  				// to len(args), if it exists.
    88  				expectErr = errs[j].Error()
    89  			}
    90  
    91  			c.Logf("test #%d: args: %v, errors: %v -> %q", i*4+j, args, errs, expectErr)
    92  			err := s.baseCmd.CheckNumArgs(args, errs)
    93  			if expectErr != "" {
    94  				c.Check(err, gc.ErrorMatches, expectErr)
    95  			} else {
    96  				c.Check(err, jc.ErrorIsNil)
    97  			}
    98  		}
    99  	}
   100  }
   101  
   102  func (s *SubnetCommandBaseSuite) TestValidateCIDR(c *gc.C) {
   103  	// We only validate the subset of accepted CIDR formats which we
   104  	// need to support.
   105  	for i, test := range []struct {
   106  		about     string
   107  		input     string
   108  		strict    bool
   109  		output    string
   110  		expectErr string
   111  	}{{
   112  		about:  "valid IPv4 CIDR, strict=false",
   113  		input:  "10.0.5.0/24",
   114  		strict: false,
   115  		output: "10.0.5.0/24",
   116  	}, {
   117  		about:  "valid IPv4 CIDR, struct=true",
   118  		input:  "10.0.5.0/24",
   119  		strict: true,
   120  		output: "10.0.5.0/24",
   121  	}, {
   122  		about:  "valid IPv6 CIDR, strict=false",
   123  		input:  "2001:db8::/32",
   124  		strict: false,
   125  		output: "2001:db8::/32",
   126  	}, {
   127  		about:  "valid IPv6 CIDR, strict=true",
   128  		input:  "2001:db8::/32",
   129  		strict: true,
   130  		output: "2001:db8::/32",
   131  	}, {
   132  		about:  "incorrectly specified IPv4 CIDR, strict=false",
   133  		input:  "192.168.10.20/16",
   134  		strict: false,
   135  		output: "192.168.0.0/16",
   136  	}, {
   137  		about:     "incorrectly specified IPv4 CIDR, strict=true",
   138  		input:     "192.168.10.20/16",
   139  		strict:    true,
   140  		expectErr: `"192.168.10.20/16" is not correctly specified, expected "192.168.0.0/16"`,
   141  	}, {
   142  		about:  "incorrectly specified IPv6 CIDR, strict=false",
   143  		input:  "2001:db8::2/48",
   144  		strict: false,
   145  		output: "2001:db8::/48",
   146  	}, {
   147  		about:     "incorrectly specified IPv6 CIDR, strict=true",
   148  		input:     "2001:db8::2/48",
   149  		strict:    true,
   150  		expectErr: `"2001:db8::2/48" is not correctly specified, expected "2001:db8::/48"`,
   151  	}, {
   152  		about:     "empty CIDR, strict=false",
   153  		input:     "",
   154  		strict:    false,
   155  		expectErr: `"" is not a valid CIDR`,
   156  	}, {
   157  		about:     "empty CIDR, strict=true",
   158  		input:     "",
   159  		strict:    true,
   160  		expectErr: `"" is not a valid CIDR`,
   161  	}} {
   162  		c.Logf("test #%d: %s -> %s", i, test.about, test.expectErr)
   163  		validated, err := s.baseCmd.ValidateCIDR(test.input, test.strict)
   164  		if test.expectErr != "" {
   165  			c.Check(err, gc.ErrorMatches, test.expectErr)
   166  		} else {
   167  			c.Check(err, jc.ErrorIsNil)
   168  		}
   169  		c.Check(validated.Id(), gc.Equals, test.output)
   170  	}
   171  }
   172  
   173  func (s *SubnetCommandBaseSuite) TestValidateSpace(c *gc.C) {
   174  	// We only validate a few more common invalid cases as
   175  	// names.IsValidSpace() is separately and more extensively tested.
   176  	for i, test := range []struct {
   177  		about     string
   178  		input     string
   179  		expectErr string
   180  	}{{
   181  		about: "valid space - only lowercase letters",
   182  		input: "space",
   183  	}, {
   184  		about: "valid space - only numbers",
   185  		input: "42",
   186  	}, {
   187  		about: "valid space - only lowercase letters and numbers",
   188  		input: "over9000",
   189  	}, {
   190  		about: "valid space - with dashes",
   191  		input: "my-new-99space",
   192  	}, {
   193  		about:     "invalid space - with symbols",
   194  		input:     "%in$valid",
   195  		expectErr: `"%in\$valid" is not a valid space name`,
   196  	}, {
   197  		about:     "invalid space - with underscores",
   198  		input:     "42_foo",
   199  		expectErr: `"42_foo" is not a valid space name`,
   200  	}, {
   201  		about:     "invalid space - with uppercase letters",
   202  		input:     "Not-Good",
   203  		expectErr: `"Not-Good" is not a valid space name`,
   204  	}, {
   205  		about:     "empty space name",
   206  		input:     "",
   207  		expectErr: `"" is not a valid space name`,
   208  	}} {
   209  		c.Logf("test #%d: %s -> %s", i, test.about, test.expectErr)
   210  		validated, err := s.baseCmd.ValidateSpace(test.input)
   211  		if test.expectErr != "" {
   212  			c.Check(err, gc.ErrorMatches, test.expectErr)
   213  			c.Check(validated.Id(), gc.Equals, "")
   214  		} else {
   215  			c.Check(err, jc.ErrorIsNil)
   216  			// When the input is valid it should stay the same.
   217  			c.Check(validated.Id(), gc.Equals, test.input)
   218  		}
   219  	}
   220  }