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