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