github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/subnet/remove_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  	"github.com/juju/errors"
     8  	jc "github.com/juju/testing/checkers"
     9  	gc "gopkg.in/check.v1"
    10  	"gopkg.in/juju/names.v2"
    11  
    12  	"github.com/juju/juju/cmd/juju/subnet"
    13  	"github.com/juju/juju/feature"
    14  )
    15  
    16  type RemoveSuite struct {
    17  	BaseSubnetSuite
    18  }
    19  
    20  var _ = gc.Suite(&RemoveSuite{})
    21  
    22  func (s *RemoveSuite) SetUpTest(c *gc.C) {
    23  	s.BaseSubnetSuite.SetFeatureFlags(feature.PostNetCLIMVP)
    24  	s.BaseSubnetSuite.SetUpTest(c)
    25  	s.newCommand = subnet.NewRemoveCommand
    26  }
    27  
    28  func (s *RemoveSuite) TestInit(c *gc.C) {
    29  	for i, test := range []struct {
    30  		about      string
    31  		args       []string
    32  		expectCIDR string
    33  		expectErr  string
    34  	}{{
    35  		about:     "no arguments",
    36  		expectErr: "CIDR is required",
    37  	}, {
    38  		about:     "an invalid CIDR",
    39  		args:      s.Strings("foo"),
    40  		expectErr: `"foo" is not a valid CIDR`,
    41  	}, {
    42  		about:      "too many arguments (first is valid)",
    43  		args:       s.Strings("10.0.0.0/8", "bar", "baz"),
    44  		expectCIDR: "10.0.0.0/8",
    45  		expectErr:  `unrecognized args: \["bar" "baz"\]`,
    46  	}, {
    47  		about:     "incorrectly specified CIDR",
    48  		args:      s.Strings("5.4.3.2/10"),
    49  		expectErr: `"5.4.3.2/10" is not correctly specified, expected "5.0.0.0/10"`,
    50  	}} {
    51  		c.Logf("test #%d: %s", i, test.about)
    52  		command, err := s.InitCommand(c, test.args...)
    53  		if test.expectErr != "" {
    54  			c.Check(err, gc.ErrorMatches, test.expectErr)
    55  		} else {
    56  			c.Check(err, jc.ErrorIsNil)
    57  			command := command.(*subnet.RemoveCommand)
    58  			c.Check(command.CIDR, gc.Equals, test.expectCIDR)
    59  		}
    60  
    61  		// No API calls should be recorded at this stage.
    62  		s.api.CheckCallNames(c)
    63  	}
    64  }
    65  
    66  func (s *RemoveSuite) TestRunWithIPv4CIDRSucceeds(c *gc.C) {
    67  	s.AssertRunSucceeds(c,
    68  		`marked subnet "10.20.0.0/16" for removal\n`,
    69  		"", // empty stdout.
    70  		"10.20.0.0/16",
    71  	)
    72  
    73  	s.api.CheckCallNames(c, "RemoveSubnet", "Close")
    74  	s.api.CheckCall(c, 0, "RemoveSubnet", names.NewSubnetTag("10.20.0.0/16"))
    75  }
    76  
    77  func (s *RemoveSuite) TestRunWithIPv6CIDRSucceeds(c *gc.C) {
    78  	s.AssertRunSucceeds(c,
    79  		`marked subnet "2001:db8::/32" for removal\n`,
    80  		"", // empty stdout.
    81  		"2001:db8::/32",
    82  	)
    83  
    84  	s.api.CheckCallNames(c, "RemoveSubnet", "Close")
    85  	s.api.CheckCall(c, 0, "RemoveSubnet", names.NewSubnetTag("2001:db8::/32"))
    86  }
    87  
    88  func (s *RemoveSuite) TestRunWithNonExistingSubnetFails(c *gc.C) {
    89  	s.api.SetErrors(errors.NotFoundf("subnet %q", "10.10.0.0/24"))
    90  
    91  	err := s.AssertRunFails(c,
    92  		`cannot remove subnet "10.10.0.0/24": subnet "10.10.0.0/24" not found`,
    93  		"10.10.0.0/24",
    94  	)
    95  	c.Assert(err, jc.Satisfies, errors.IsNotFound)
    96  
    97  	s.api.CheckCallNames(c, "RemoveSubnet", "Close")
    98  	s.api.CheckCall(c, 0, "RemoveSubnet", names.NewSubnetTag("10.10.0.0/24"))
    99  }
   100  
   101  func (s *RemoveSuite) TestRunWithSubnetInUseFails(c *gc.C) {
   102  	s.api.SetErrors(errors.Errorf("subnet %q is still in use", "10.10.0.0/24"))
   103  
   104  	s.AssertRunFails(c,
   105  		`cannot remove subnet "10.10.0.0/24": subnet "10.10.0.0/24" is still in use`,
   106  		"10.10.0.0/24",
   107  	)
   108  
   109  	s.api.CheckCallNames(c, "RemoveSubnet", "Close")
   110  	s.api.CheckCall(c, 0, "RemoveSubnet", names.NewSubnetTag("10.10.0.0/24"))
   111  }