github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/subnet/add_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 "fmt" 8 "strings" 9 10 "github.com/juju/errors" 11 "github.com/juju/names" 12 jc "github.com/juju/testing/checkers" 13 gc "gopkg.in/check.v1" 14 15 "github.com/juju/juju/cmd/juju/subnet" 16 "github.com/juju/juju/network" 17 coretesting "github.com/juju/juju/testing" 18 ) 19 20 type AddSuite struct { 21 BaseSubnetSuite 22 } 23 24 var _ = gc.Suite(&AddSuite{}) 25 26 func (s *AddSuite) SetUpTest(c *gc.C) { 27 s.BaseSubnetSuite.SetUpTest(c) 28 s.command, _ = subnet.NewAddCommandForTest(s.api) 29 c.Assert(s.command, gc.NotNil) 30 } 31 32 func (s *AddSuite) TestInit(c *gc.C) { 33 for i, test := range []struct { 34 about string 35 args []string 36 expectCIDR string 37 expectRawCIDR string 38 expectProviderId string 39 expectSpace string 40 expectZones []string 41 expectErr string 42 }{{ 43 about: "no arguments", 44 expectErr: "either CIDR or provider ID is required", 45 }, { 46 about: "single argument - invalid CIDR: space name is required", 47 args: s.Strings("anything"), 48 expectErr: "space name is required", 49 }, { 50 about: "single argument - valid CIDR: space name is required", 51 args: s.Strings("10.0.0.0/8"), 52 expectErr: "space name is required", 53 }, { 54 about: "single argument - incorrect CIDR: space name is required", 55 args: s.Strings("10.10.0.0/8"), 56 expectErr: "space name is required", 57 }, { 58 about: "two arguments: an invalid CIDR is assumed to mean ProviderId", 59 args: s.Strings("foo", "bar"), 60 expectProviderId: "foo", 61 expectSpace: "bar", 62 }, { 63 about: "two arguments: an incorrectly specified CIDR is fixed", 64 args: s.Strings("10.10.0.0/8", "bar"), 65 expectCIDR: "10.0.0.0/8", 66 expectRawCIDR: "10.10.0.0/8", 67 expectSpace: "bar", 68 }, { 69 about: "more arguments parsed as zones", 70 args: s.Strings("10.0.0.0/8", "new-space", "zone1", "zone2"), 71 expectCIDR: "10.0.0.0/8", 72 expectRawCIDR: "10.0.0.0/8", 73 expectSpace: "new-space", 74 expectZones: s.Strings("zone1", "zone2"), 75 }, { 76 about: "CIDR and invalid space name, one zone", 77 args: s.Strings("10.10.0.0/24", "%inv$alid", "zone"), 78 expectCIDR: "10.10.0.0/24", 79 expectRawCIDR: "10.10.0.0/24", 80 expectErr: `"%inv\$alid" is not a valid space name`, 81 }, { 82 about: "incorrect CIDR and invalid space name, no zones", 83 args: s.Strings("10.10.0.0/8", "%inv$alid"), 84 expectCIDR: "10.0.0.0/8", 85 expectRawCIDR: "10.10.0.0/8", 86 expectErr: `"%inv\$alid" is not a valid space name`, 87 }, { 88 about: "ProviderId and invalid space name, two zones", 89 args: s.Strings("foo", "%inv$alid", "zone1", "zone2"), 90 expectProviderId: "foo", 91 expectErr: `"%inv\$alid" is not a valid space name`, 92 }} { 93 c.Logf("test #%d: %s", i, test.about) 94 // Create a new instance of the subcommand for each test, but 95 // since we're not running the command no need to use 96 // modelcmd.Wrap(). 97 wrappedCommand, command := subnet.NewAddCommandForTest(s.api) 98 err := coretesting.InitCommand(wrappedCommand, test.args) 99 if test.expectErr != "" { 100 prefixedErr := "invalid arguments specified: " + test.expectErr 101 c.Check(err, gc.ErrorMatches, prefixedErr) 102 } else { 103 c.Check(err, jc.ErrorIsNil) 104 c.Check(command.CIDR.Id(), gc.Equals, test.expectCIDR) 105 c.Check(command.RawCIDR, gc.Equals, test.expectRawCIDR) 106 c.Check(command.ProviderId, gc.Equals, test.expectProviderId) 107 c.Check(command.Space.Id(), gc.Equals, test.expectSpace) 108 c.Check(command.Zones, jc.DeepEquals, test.expectZones) 109 } 110 // No API calls should be recorded at this stage. 111 s.api.CheckCallNames(c) 112 } 113 } 114 115 func (s *AddSuite) TestRunWithIPv4CIDRSucceeds(c *gc.C) { 116 s.AssertRunSucceeds(c, 117 `added subnet with CIDR "10.20.0.0/24" in space "myspace"\n`, 118 "", // empty stdout. 119 "10.20.0.0/24", "myspace", 120 ) 121 122 s.api.CheckCallNames(c, "AddSubnet", "Close") 123 s.api.CheckCall(c, 0, "AddSubnet", 124 names.NewSubnetTag("10.20.0.0/24"), 125 network.Id(""), 126 names.NewSpaceTag("myspace"), 127 []string(nil), 128 ) 129 } 130 131 func (s *AddSuite) TestRunWithIncorrectlyGivenCIDRSucceedsWithWarning(c *gc.C) { 132 expectStderr := strings.Join([]string{ 133 "(.|\n)*", 134 "WARNING: using CIDR \"10.0.0.0/8\" instead of ", 135 "the incorrectly specified \"10.10.0.0/8\".\n", 136 "added subnet with CIDR \"10.0.0.0/8\" in space \"myspace\"\n", 137 }, "") 138 139 s.AssertRunSucceeds(c, 140 expectStderr, 141 "", // empty stdout. 142 "10.10.0.0/8", "myspace", 143 ) 144 145 s.api.CheckCallNames(c, "AddSubnet", "Close") 146 s.api.CheckCall(c, 0, "AddSubnet", 147 names.NewSubnetTag("10.0.0.0/8"), 148 network.Id(""), 149 names.NewSpaceTag("myspace"), 150 []string(nil), 151 ) 152 } 153 154 func (s *AddSuite) TestRunWithProviderIdSucceeds(c *gc.C) { 155 s.AssertRunSucceeds(c, 156 `added subnet with ProviderId "foo" in space "myspace"\n`, 157 "", // empty stdout. 158 "foo", "myspace", "zone1", "zone2", 159 ) 160 161 s.api.CheckCallNames(c, "AddSubnet", "Close") 162 s.api.CheckCall(c, 0, "AddSubnet", 163 names.SubnetTag{}, 164 network.Id("foo"), 165 names.NewSpaceTag("myspace"), 166 s.Strings("zone1", "zone2"), 167 ) 168 } 169 170 func (s *AddSuite) TestRunWithIPv6CIDRSucceeds(c *gc.C) { 171 s.AssertRunSucceeds(c, 172 `added subnet with CIDR "2001:db8::/32" in space "hyperspace"\n`, 173 "", // empty stdout. 174 "2001:db8::/32", "hyperspace", 175 ) 176 177 s.api.CheckCallNames(c, "AddSubnet", "Close") 178 s.api.CheckCall(c, 0, "AddSubnet", 179 names.NewSubnetTag("2001:db8::/32"), 180 network.Id(""), 181 names.NewSpaceTag("hyperspace"), 182 []string(nil), 183 ) 184 } 185 186 func (s *AddSuite) TestRunWithExistingSubnetFails(c *gc.C) { 187 s.api.SetErrors(errors.AlreadyExistsf("subnet %q", "10.10.0.0/24")) 188 189 err := s.AssertRunFails(c, 190 `cannot add subnet: subnet "10.10.0.0/24" already exists`, 191 "10.10.0.0/24", "space", 192 ) 193 c.Assert(err, jc.Satisfies, errors.IsAlreadyExists) 194 195 s.api.CheckCallNames(c, "AddSubnet", "Close") 196 s.api.CheckCall(c, 0, "AddSubnet", 197 names.NewSubnetTag("10.10.0.0/24"), 198 network.Id(""), 199 names.NewSpaceTag("space"), 200 []string(nil), 201 ) 202 } 203 204 func (s *AddSuite) TestRunWithNonExistingSpaceFails(c *gc.C) { 205 s.api.SetErrors(errors.NotFoundf("space %q", "space")) 206 207 err := s.AssertRunFails(c, 208 `cannot add subnet: space "space" not found`, 209 "10.10.0.0/24", "space", "zone1", "zone2", 210 ) 211 c.Assert(err, jc.Satisfies, errors.IsNotFound) 212 213 s.api.CheckCallNames(c, "AddSubnet", "Close") 214 s.api.CheckCall(c, 0, "AddSubnet", 215 names.NewSubnetTag("10.10.0.0/24"), 216 network.Id(""), 217 names.NewSpaceTag("space"), 218 s.Strings("zone1", "zone2"), 219 ) 220 } 221 222 func (s *AddSuite) TestRunWithAmbiguousCIDRDisplaysError(c *gc.C) { 223 apiError := errors.New(`multiple subnets with CIDR "10.10.0.0/24" <snip>`) 224 s.api.SetErrors(apiError) 225 226 s.AssertRunSucceeds(c, 227 fmt.Sprintf("ERROR: %v.\n", apiError), 228 "", 229 "10.10.0.0/24", "space", "zone1", "zone2", 230 ) 231 232 s.api.CheckCallNames(c, "AddSubnet", "Close") 233 s.api.CheckCall(c, 0, "AddSubnet", 234 names.NewSubnetTag("10.10.0.0/24"), 235 network.Id(""), 236 names.NewSpaceTag("space"), 237 s.Strings("zone1", "zone2"), 238 ) 239 }