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