github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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.ProviderCall("Open", apiservertesting.BackingInstance.EnvConfig), 85 apiservertesting.ZonedNetworkingEnvironCall("SupportsSpaces"), 86 } 87 88 addSpaceCalls := append(baseCalls, apiservertesting.BackingCall("AddSpace", p.Name, network.Id(p.ProviderId), p.Subnets, p.Public)) 89 90 if p.Error == "" { 91 apiservertesting.CheckMethodCalls(c, apiservertesting.SharedStub, addSpaceCalls...) 92 } else { 93 apiservertesting.CheckMethodCalls(c, apiservertesting.SharedStub, baseCalls...) 94 } 95 } 96 97 func (s *SpacesSuite) TestCreateInvalidSpace(c *gc.C) { 98 p := checkCreateSpacesParams{ 99 Name: "-", 100 Subnets: []string{"10.0.0.0/24"}, 101 Error: `"space--" is not a valid space tag`, 102 } 103 s.checkCreateSpaces(c, p) 104 } 105 106 func (s *SpacesSuite) TestCreateInvalidSubnet(c *gc.C) { 107 p := checkCreateSpacesParams{ 108 Name: "foo", 109 Subnets: []string{"bar"}, 110 Error: `"subnet-bar" is not a valid subnet tag`, 111 } 112 s.checkCreateSpaces(c, p) 113 } 114 115 func (s *SpacesSuite) TestPublic(c *gc.C) { 116 p := checkCreateSpacesParams{ 117 Name: "foo", 118 Subnets: []string{"10.0.0.0/24"}, 119 Public: true, 120 } 121 s.checkCreateSpaces(c, p) 122 } 123 124 func (s *SpacesSuite) TestProviderId(c *gc.C) { 125 p := checkCreateSpacesParams{ 126 Name: "foo", 127 Subnets: []string{"10.0.0.0/24"}, 128 ProviderId: "foobar", 129 } 130 s.checkCreateSpaces(c, p) 131 } 132 133 func (s *SpacesSuite) TestEmptySpaceName(c *gc.C) { 134 p := checkCreateSpacesParams{ 135 Subnets: []string{"10.0.0.0/24"}, 136 Error: `"" is not a valid tag`, 137 } 138 s.checkCreateSpaces(c, p) 139 } 140 141 func (s *SpacesSuite) TestNoSubnets(c *gc.C) { 142 p := checkCreateSpacesParams{ 143 Name: "foo", 144 Subnets: nil, 145 } 146 s.checkCreateSpaces(c, p) 147 } 148 149 func (s *SpacesSuite) TestCreateSpacesModelConfigError(c *gc.C) { 150 apiservertesting.SharedStub.SetErrors( 151 errors.New("boom"), // Backing.ModelConfig() 152 ) 153 154 spaces := params.CreateSpacesParams{} 155 _, err := networkingcommon.CreateSpaces(apiservertesting.BackingInstance, spaces) 156 c.Assert(err, gc.ErrorMatches, "getting model config: boom") 157 } 158 159 func (s *SpacesSuite) TestCreateSpacesProviderOpenError(c *gc.C) { 160 apiservertesting.SharedStub.SetErrors( 161 nil, // Backing.ModelConfig() 162 errors.New("boom"), // Provider.Open() 163 ) 164 165 spaces := params.CreateSpacesParams{} 166 _, err := networkingcommon.CreateSpaces(apiservertesting.BackingInstance, spaces) 167 c.Assert(err, gc.ErrorMatches, "validating model config: boom") 168 } 169 170 func (s *SpacesSuite) TestCreateSpacesNotSupportedError(c *gc.C) { 171 apiservertesting.SharedStub.SetErrors( 172 nil, // Backing.ModelConfig() 173 nil, // Provider.Open() 174 errors.NotSupportedf("spaces"), // ZonedNetworkingEnviron.SupportsSpaces() 175 ) 176 177 spaces := params.CreateSpacesParams{} 178 _, err := networkingcommon.CreateSpaces(apiservertesting.BackingInstance, spaces) 179 c.Assert(err, gc.ErrorMatches, "spaces not supported") 180 } 181 182 func (s *SpacesSuite) TestSuppportsSpacesModelConfigError(c *gc.C) { 183 apiservertesting.SharedStub.SetErrors( 184 errors.New("boom"), // Backing.ModelConfig() 185 ) 186 187 err := networkingcommon.SupportsSpaces(apiservertesting.BackingInstance) 188 c.Assert(err, gc.ErrorMatches, "getting model config: boom") 189 } 190 191 func (s *SpacesSuite) TestSuppportsSpacesEnvironNewError(c *gc.C) { 192 apiservertesting.SharedStub.SetErrors( 193 nil, // Backing.ModelConfig() 194 errors.New("boom"), // environs.New() 195 ) 196 197 err := networkingcommon.SupportsSpaces(apiservertesting.BackingInstance) 198 c.Assert(err, gc.ErrorMatches, "validating model config: boom") 199 } 200 201 func (s *SpacesSuite) TestSuppportsSpacesWithoutNetworking(c *gc.C) { 202 apiservertesting.BackingInstance.SetUp( 203 c, 204 apiservertesting.StubEnvironName, 205 apiservertesting.WithoutZones, 206 apiservertesting.WithoutSpaces, 207 apiservertesting.WithoutSubnets) 208 209 err := networkingcommon.SupportsSpaces(apiservertesting.BackingInstance) 210 c.Assert(err, jc.Satisfies, errors.IsNotSupported) 211 } 212 213 func (s *SpacesSuite) TestSuppportsSpacesWithoutSpaces(c *gc.C) { 214 apiservertesting.BackingInstance.SetUp( 215 c, 216 apiservertesting.StubNetworkingEnvironName, 217 apiservertesting.WithoutZones, 218 apiservertesting.WithoutSpaces, 219 apiservertesting.WithoutSubnets) 220 221 apiservertesting.SharedStub.SetErrors( 222 nil, // Backing.ModelConfig() 223 nil, // environs.New() 224 errors.New("boom"), // Backing.SupportsSpaces() 225 ) 226 227 err := networkingcommon.SupportsSpaces(apiservertesting.BackingInstance) 228 c.Assert(err, jc.Satisfies, errors.IsNotSupported) 229 } 230 231 func (s *SpacesSuite) TestSuppportsSpaces(c *gc.C) { 232 err := networkingcommon.SupportsSpaces(apiservertesting.BackingInstance) 233 c.Assert(err, jc.ErrorIsNil) 234 }