github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/apiserver/common/networkingcommon/spaces_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package networkingcommon_test 5 6 import ( 7 "github.com/juju/errors" 8 jc "github.com/juju/testing/checkers" 9 gc "gopkg.in/check.v1" 10 11 "github.com/juju/juju/apiserver/common/networkingcommon" 12 "github.com/juju/juju/apiserver/params" 13 apiservertesting "github.com/juju/juju/apiserver/testing" 14 "github.com/juju/juju/network" 15 coretesting "github.com/juju/juju/testing" 16 ) 17 18 type SpacesSuite struct { 19 coretesting.BaseSuite 20 apiservertesting.StubNetwork 21 } 22 23 var _ = gc.Suite(&SpacesSuite{}) 24 25 func (s *SpacesSuite) SetUpSuite(c *gc.C) { 26 s.StubNetwork.SetUpSuite(c) 27 s.BaseSuite.SetUpSuite(c) 28 } 29 30 func (s *SpacesSuite) TearDownSuite(c *gc.C) { 31 s.BaseSuite.TearDownSuite(c) 32 } 33 34 func (s *SpacesSuite) SetUpTest(c *gc.C) { 35 s.BaseSuite.SetUpTest(c) 36 apiservertesting.BackingInstance.SetUp( 37 c, 38 apiservertesting.StubZonedNetworkingEnvironName, 39 apiservertesting.WithZones, 40 apiservertesting.WithSpaces, 41 apiservertesting.WithSubnets) 42 } 43 44 func (s *SpacesSuite) TearDownTest(c *gc.C) { 45 s.BaseSuite.TearDownTest(c) 46 } 47 48 type checkCreateSpacesParams struct { 49 Name string 50 Subnets []string 51 Error string 52 Public bool 53 ProviderId string 54 } 55 56 func (s *SpacesSuite) checkCreateSpaces(c *gc.C, p checkCreateSpacesParams) { 57 args := params.CreateSpaceParams{} 58 if p.Name != "" { 59 args.SpaceTag = "space-" + p.Name 60 } 61 if len(p.Subnets) > 0 { 62 for _, cidr := range p.Subnets { 63 args.SubnetTags = append(args.SubnetTags, "subnet-"+cidr) 64 } 65 } 66 args.Public = p.Public 67 args.ProviderId = p.ProviderId 68 69 spaces := params.CreateSpacesParams{} 70 spaces.Spaces = append(spaces.Spaces, args) 71 results, err := networkingcommon.CreateSpaces(apiservertesting.BackingInstance, spaces) 72 73 c.Assert(len(results.Results), gc.Equals, 1) 74 c.Assert(err, gc.IsNil) 75 if p.Error == "" { 76 c.Assert(results.Results[0].Error, gc.IsNil) 77 } else { 78 c.Assert(results.Results[0].Error, gc.NotNil) 79 c.Assert(results.Results[0].Error, gc.ErrorMatches, p.Error) 80 } 81 82 baseCalls := []apiservertesting.StubMethodCall{ 83 apiservertesting.BackingCall("ModelConfig"), 84 apiservertesting.BackingCall("CloudSpec"), 85 apiservertesting.ProviderCall("Open", apiservertesting.BackingInstance.EnvConfig), 86 apiservertesting.ZonedNetworkingEnvironCall("SupportsSpaces"), 87 } 88 89 addSpaceCalls := append(baseCalls, apiservertesting.BackingCall("AddSpace", p.Name, network.Id(p.ProviderId), p.Subnets, p.Public)) 90 91 if p.Error == "" { 92 apiservertesting.CheckMethodCalls(c, apiservertesting.SharedStub, addSpaceCalls...) 93 } else { 94 apiservertesting.CheckMethodCalls(c, apiservertesting.SharedStub, baseCalls...) 95 } 96 } 97 98 func (s *SpacesSuite) TestCreateInvalidSpace(c *gc.C) { 99 p := checkCreateSpacesParams{ 100 Name: "-", 101 Subnets: []string{"10.0.0.0/24"}, 102 Error: `"space--" is not a valid space tag`, 103 } 104 s.checkCreateSpaces(c, p) 105 } 106 107 func (s *SpacesSuite) TestCreateInvalidSubnet(c *gc.C) { 108 p := checkCreateSpacesParams{ 109 Name: "foo", 110 Subnets: []string{"bar"}, 111 Error: `"subnet-bar" is not a valid subnet tag`, 112 } 113 s.checkCreateSpaces(c, p) 114 } 115 116 func (s *SpacesSuite) TestPublic(c *gc.C) { 117 p := checkCreateSpacesParams{ 118 Name: "foo", 119 Subnets: []string{"10.0.0.0/24"}, 120 Public: true, 121 } 122 s.checkCreateSpaces(c, p) 123 } 124 125 func (s *SpacesSuite) TestProviderId(c *gc.C) { 126 p := checkCreateSpacesParams{ 127 Name: "foo", 128 Subnets: []string{"10.0.0.0/24"}, 129 ProviderId: "foobar", 130 } 131 s.checkCreateSpaces(c, p) 132 } 133 134 func (s *SpacesSuite) TestEmptySpaceName(c *gc.C) { 135 p := checkCreateSpacesParams{ 136 Subnets: []string{"10.0.0.0/24"}, 137 Error: `"" is not a valid tag`, 138 } 139 s.checkCreateSpaces(c, p) 140 } 141 142 func (s *SpacesSuite) TestNoSubnets(c *gc.C) { 143 p := checkCreateSpacesParams{ 144 Name: "foo", 145 Subnets: nil, 146 } 147 s.checkCreateSpaces(c, p) 148 } 149 150 func (s *SpacesSuite) TestCreateSpacesModelConfigError(c *gc.C) { 151 apiservertesting.SharedStub.SetErrors( 152 errors.New("boom"), // Backing.ModelConfig() 153 ) 154 155 spaces := params.CreateSpacesParams{} 156 _, err := networkingcommon.CreateSpaces(apiservertesting.BackingInstance, spaces) 157 c.Assert(err, gc.ErrorMatches, "getting environ: boom") 158 } 159 160 func (s *SpacesSuite) TestCreateSpacesProviderOpenError(c *gc.C) { 161 apiservertesting.SharedStub.SetErrors( 162 nil, // Backing.ModelConfig() 163 nil, // Backing.CloudSpec() 164 errors.New("boom"), // Provider.Open() 165 ) 166 167 spaces := params.CreateSpacesParams{} 168 _, err := networkingcommon.CreateSpaces(apiservertesting.BackingInstance, spaces) 169 c.Assert(err, gc.ErrorMatches, "getting environ: boom") 170 } 171 172 func (s *SpacesSuite) TestCreateSpacesNotSupportedError(c *gc.C) { 173 apiservertesting.SharedStub.SetErrors( 174 nil, // Backing.ModelConfig() 175 nil, // Backing.CloudSpec() 176 nil, // Provider.Open() 177 errors.NotSupportedf("spaces"), // ZonedNetworkingEnviron.SupportsSpaces() 178 ) 179 180 spaces := params.CreateSpacesParams{} 181 _, err := networkingcommon.CreateSpaces(apiservertesting.BackingInstance, spaces) 182 c.Assert(err, gc.ErrorMatches, "spaces not supported") 183 } 184 185 func (s *SpacesSuite) TestSuppportsSpacesModelConfigError(c *gc.C) { 186 apiservertesting.SharedStub.SetErrors( 187 errors.New("boom"), // Backing.ModelConfig() 188 ) 189 190 err := networkingcommon.SupportsSpaces(apiservertesting.BackingInstance) 191 c.Assert(err, gc.ErrorMatches, "getting environ: boom") 192 } 193 194 func (s *SpacesSuite) TestSuppportsSpacesEnvironNewError(c *gc.C) { 195 apiservertesting.SharedStub.SetErrors( 196 nil, // Backing.ModelConfig() 197 nil, // Backing.CloudSpec() 198 errors.New("boom"), // environs.New() 199 ) 200 201 err := networkingcommon.SupportsSpaces(apiservertesting.BackingInstance) 202 c.Assert(err, gc.ErrorMatches, "getting environ: boom") 203 } 204 205 func (s *SpacesSuite) TestSuppportsSpacesWithoutNetworking(c *gc.C) { 206 apiservertesting.BackingInstance.SetUp( 207 c, 208 apiservertesting.StubEnvironName, 209 apiservertesting.WithoutZones, 210 apiservertesting.WithoutSpaces, 211 apiservertesting.WithoutSubnets) 212 213 err := networkingcommon.SupportsSpaces(apiservertesting.BackingInstance) 214 c.Assert(err, jc.Satisfies, errors.IsNotSupported) 215 } 216 217 func (s *SpacesSuite) TestSuppportsSpacesWithoutSpaces(c *gc.C) { 218 apiservertesting.BackingInstance.SetUp( 219 c, 220 apiservertesting.StubNetworkingEnvironName, 221 apiservertesting.WithoutZones, 222 apiservertesting.WithoutSpaces, 223 apiservertesting.WithoutSubnets) 224 225 apiservertesting.SharedStub.SetErrors( 226 nil, // Backing.ModelConfig() 227 nil, // Backing.CloudSpec() 228 nil, // environs.New() 229 errors.New("boom"), // Backing.SupportsSpaces() 230 ) 231 232 err := networkingcommon.SupportsSpaces(apiservertesting.BackingInstance) 233 c.Assert(err, jc.Satisfies, errors.IsNotSupported) 234 } 235 236 func (s *SpacesSuite) TestSuppportsSpaces(c *gc.C) { 237 err := networkingcommon.SupportsSpaces(apiservertesting.BackingInstance) 238 c.Assert(err, jc.ErrorIsNil) 239 }