github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/provider/oci/networking_integration_test.go (about) 1 // Copyright 2018 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package oci_test 5 6 import ( 7 "context" 8 9 ociCore "github.com/oracle/oci-go-sdk/v65/core" 10 "go.uber.org/mock/gomock" 11 gc "gopkg.in/check.v1" 12 13 "github.com/juju/juju/core/instance" 14 "github.com/juju/juju/core/network" 15 ) 16 17 type networkingSuite struct { 18 commonSuite 19 } 20 21 var _ = gc.Suite(&networkingSuite{}) 22 23 func (s *networkingSuite) SetUpTest(c *gc.C) { 24 s.commonSuite.SetUpTest(c) 25 } 26 27 func (s *networkingSuite) setupNetworkInterfacesExpectations(vnicID, vcnID string) { 28 attachResponse := []ociCore.VnicAttachment{ 29 { 30 Id: makeStringPointer("fakeAttachmentId"), 31 AvailabilityDomain: makeStringPointer("fake"), 32 CompartmentId: &s.testCompartment, 33 InstanceId: &s.testInstanceID, 34 LifecycleState: ociCore.VnicAttachmentLifecycleStateAttached, 35 DisplayName: makeStringPointer("fakeAttachmentName"), 36 NicIndex: makeIntPointer(0), 37 VnicId: &vnicID, 38 }, 39 } 40 41 vnicRequest, vnicResponse := makeGetVnicRequestResponse([]ociCore.GetVnicResponse{ 42 { 43 Vnic: ociCore.Vnic{ 44 Id: makeStringPointer(vnicID), 45 PrivateIp: makeStringPointer("1.1.1.1"), 46 DisplayName: makeStringPointer("fakeVnicName"), 47 PublicIp: makeStringPointer("2.2.2.2"), 48 MacAddress: makeStringPointer("aa:aa:aa:aa:aa:aa"), 49 SubnetId: makeStringPointer("fakeSubnetId"), 50 LifecycleState: ociCore.VnicLifecycleStateAvailable, 51 }, 52 }, 53 }) 54 55 vcnResponse := []ociCore.Vcn{ 56 { 57 CompartmentId: &s.testCompartment, 58 CidrBlock: makeStringPointer("1.0.0.0/8"), 59 Id: makeStringPointer(vcnID), 60 LifecycleState: ociCore.VcnLifecycleStateAvailable, 61 DefaultRouteTableId: makeStringPointer("fakeRouteTable"), 62 DefaultSecurityListId: makeStringPointer("fakeSeclist"), 63 DisplayName: makeStringPointer("amazingVcn"), 64 FreeformTags: s.tags, 65 }, 66 } 67 68 subnetResponse := []ociCore.Subnet{ 69 { 70 AvailabilityDomain: makeStringPointer("us-phoenix-1"), 71 CidrBlock: makeStringPointer("1.0.0.0/8"), 72 CompartmentId: &s.testCompartment, 73 Id: makeStringPointer("fakeSubnetId"), 74 VcnId: &vcnID, 75 DisplayName: makeStringPointer("fakeSubnet"), 76 RouteTableId: makeStringPointer("fakeRouteTable"), 77 LifecycleState: ociCore.SubnetLifecycleStateAvailable, 78 }, 79 } 80 81 request, response := makeGetInstanceRequestResponse(ociCore.Instance{ 82 CompartmentId: &s.testCompartment, 83 AvailabilityDomain: makeStringPointer("QXay:PHX-AD-3"), 84 Id: &s.testInstanceID, 85 Region: makeStringPointer("us-phoenix-1"), 86 Shape: makeStringPointer("VM.Standard1.1"), 87 DisplayName: makeStringPointer("fake"), 88 FreeformTags: s.tags, 89 LifecycleState: ociCore.InstanceLifecycleStateRunning, 90 }) 91 92 gomock.InOrder( 93 s.compute.EXPECT().GetInstance(context.Background(), request).Return(response, nil), 94 s.compute.EXPECT().ListVnicAttachments(context.Background(), &s.testCompartment, &s.testInstanceID).Return(attachResponse, nil), 95 s.netw.EXPECT().GetVnic(context.Background(), vnicRequest[0]).Return(vnicResponse[0], nil), 96 s.netw.EXPECT().ListVcns(context.Background(), &s.testCompartment).Return(vcnResponse, nil), 97 s.netw.EXPECT().ListSubnets(context.Background(), &s.testCompartment, &vcnID).Return(subnetResponse, nil), 98 ) 99 } 100 101 func (s *networkingSuite) setupListSubnetsExpectations() { 102 vcnID := "fakeVcn" 103 104 vcnResponse := []ociCore.Vcn{ 105 { 106 CompartmentId: &s.testCompartment, 107 CidrBlock: makeStringPointer("1.0.0.0/8"), 108 Id: makeStringPointer(vcnID), 109 LifecycleState: ociCore.VcnLifecycleStateAvailable, 110 DefaultRouteTableId: makeStringPointer("fakeRouteTable"), 111 DefaultSecurityListId: makeStringPointer("fakeSeclist"), 112 DisplayName: makeStringPointer("amazingVcn"), 113 FreeformTags: s.tags, 114 }, 115 } 116 117 subnetResponse := []ociCore.Subnet{ 118 { 119 AvailabilityDomain: makeStringPointer("us-phoenix-1"), 120 CidrBlock: makeStringPointer("1.0.0.0/8"), 121 CompartmentId: &s.testCompartment, 122 Id: makeStringPointer("fakeSubnetId"), 123 VcnId: makeStringPointer(vcnID), 124 DisplayName: makeStringPointer("fakeSubnet"), 125 RouteTableId: makeStringPointer("fakeRouteTable"), 126 LifecycleState: ociCore.SubnetLifecycleStateAvailable, 127 }, 128 } 129 130 s.netw.EXPECT().ListVcns(context.Background(), &s.testCompartment).Return(vcnResponse, nil).Times(2) 131 s.netw.EXPECT().ListSubnets(context.Background(), &s.testCompartment, &vcnID).Return(subnetResponse, nil).Times(2) 132 } 133 134 func (s *networkingSuite) setupSubnetsKnownInstanceExpectations() { 135 vnicID := "fakeVnicId" 136 vcnID := "fakeVcn" 137 138 attachResponse := []ociCore.VnicAttachment{ 139 { 140 Id: makeStringPointer("fakeAttachmentId"), 141 AvailabilityDomain: makeStringPointer("fake"), 142 CompartmentId: &s.testCompartment, 143 InstanceId: &s.testInstanceID, 144 LifecycleState: ociCore.VnicAttachmentLifecycleStateAttached, 145 DisplayName: makeStringPointer("fakeAttachmentName"), 146 NicIndex: makeIntPointer(0), 147 VnicId: &vnicID, 148 }, 149 } 150 151 vnicRequest, vnicResponse := makeGetVnicRequestResponse([]ociCore.GetVnicResponse{ 152 { 153 Vnic: ociCore.Vnic{ 154 Id: makeStringPointer(vnicID), 155 PrivateIp: makeStringPointer("1.1.1.1"), 156 DisplayName: makeStringPointer("fakeVnicName"), 157 PublicIp: makeStringPointer("2.2.2.2"), 158 MacAddress: makeStringPointer("aa:aa:aa:aa:aa:aa"), 159 SubnetId: makeStringPointer("fakeSubnetId"), 160 LifecycleState: ociCore.VnicLifecycleStateAvailable, 161 }, 162 }, 163 }) 164 165 vcnResponse := []ociCore.Vcn{ 166 { 167 CompartmentId: &s.testCompartment, 168 CidrBlock: makeStringPointer("1.0.0.0/8"), 169 Id: makeStringPointer(vcnID), 170 LifecycleState: ociCore.VcnLifecycleStateAvailable, 171 DefaultRouteTableId: makeStringPointer("fakeRouteTable"), 172 DefaultSecurityListId: makeStringPointer("fakeSeclist"), 173 DisplayName: makeStringPointer("amazingVcn"), 174 FreeformTags: s.tags, 175 }, 176 } 177 178 subnetResponse := []ociCore.Subnet{ 179 { 180 AvailabilityDomain: makeStringPointer("us-phoenix-1"), 181 CidrBlock: makeStringPointer("1.0.0.0/8"), 182 CompartmentId: &s.testCompartment, 183 Id: makeStringPointer("fakeSubnetId"), 184 VcnId: &vcnID, 185 DisplayName: makeStringPointer("fakeSubnet"), 186 RouteTableId: makeStringPointer("fakeRouteTable"), 187 LifecycleState: ociCore.SubnetLifecycleStateAvailable, 188 }, 189 { 190 AvailabilityDomain: makeStringPointer("us-phoenix-1"), 191 CidrBlock: makeStringPointer("1.0.0.0/8"), 192 CompartmentId: &s.testCompartment, 193 Id: makeStringPointer("anotherFakeSubnetId"), 194 VcnId: &vcnID, 195 DisplayName: makeStringPointer("fakeSubnet"), 196 RouteTableId: makeStringPointer("fakeRouteTable"), 197 LifecycleState: ociCore.SubnetLifecycleStateAvailable, 198 }, 199 } 200 201 request, response := makeGetInstanceRequestResponse( 202 ociCore.Instance{ 203 CompartmentId: &s.testCompartment, 204 AvailabilityDomain: makeStringPointer("QXay:PHX-AD-3"), 205 Id: &s.testInstanceID, 206 Region: makeStringPointer("us-phoenix-1"), 207 Shape: makeStringPointer("VM.Standard1.1"), 208 DisplayName: makeStringPointer("fake"), 209 FreeformTags: s.tags, 210 LifecycleState: ociCore.InstanceLifecycleStateRunning, 211 }) 212 213 s.netw.EXPECT().ListVcns(context.Background(), &s.testCompartment).Return(vcnResponse, nil).Times(2) 214 s.netw.EXPECT().ListSubnets(context.Background(), &s.testCompartment, &vcnID).Return(subnetResponse, nil).Times(2) 215 s.compute.EXPECT().GetInstance(context.Background(), request).Return(response, nil).Times(2) 216 s.compute.EXPECT().ListVnicAttachments(context.Background(), &s.testCompartment, &s.testInstanceID).Return(attachResponse, nil).Times(2) 217 s.netw.EXPECT().GetVnic(context.Background(), vnicRequest[0]).Return(vnicResponse[0], nil).Times(2) 218 } 219 220 func (s *networkingSuite) TestNetworkInterfaces(c *gc.C) { 221 ctrl := s.patchEnv(c) 222 defer ctrl.Finish() 223 224 vnicID := "fakeVnicId" 225 vcnID := "fakeVcn" 226 227 s.setupNetworkInterfacesExpectations(vnicID, vcnID) 228 229 infoList, err := s.env.NetworkInterfaces(nil, []instance.Id{instance.Id(s.testInstanceID)}) 230 c.Assert(err, gc.IsNil) 231 c.Assert(infoList, gc.HasLen, 1) 232 info := infoList[0] 233 234 c.Assert(info, gc.HasLen, 1) 235 c.Assert(info[0].Addresses, gc.DeepEquals, network.ProviderAddresses{ 236 network.NewMachineAddress( 237 "1.1.1.1", network.WithScope(network.ScopeCloudLocal), network.WithCIDR("1.0.0.0/8"), 238 ).AsProviderAddress()}) 239 c.Assert(info[0].ShadowAddresses, gc.DeepEquals, network.ProviderAddresses{ 240 network.NewMachineAddress("2.2.2.2", network.WithScope(network.ScopePublic)).AsProviderAddress()}) 241 c.Assert(info[0].DeviceIndex, gc.Equals, 0) 242 c.Assert(info[0].ProviderId, gc.Equals, network.Id(vnicID)) 243 c.Assert(info[0].MACAddress, gc.Equals, "aa:aa:aa:aa:aa:aa") 244 c.Assert(info[0].InterfaceType, gc.Equals, network.EthernetDevice) 245 c.Assert(info[0].ProviderSubnetId, gc.Equals, network.Id("fakeSubnetId")) 246 } 247 248 func (s *networkingSuite) TestSubnets(c *gc.C) { 249 ctrl := s.patchEnv(c) 250 defer ctrl.Finish() 251 252 s.setupListSubnetsExpectations() 253 254 lookFor := []network.Id{ 255 network.Id("fakeSubnetId"), 256 } 257 info, err := s.env.Subnets(nil, instance.UnknownId, lookFor) 258 c.Assert(err, gc.IsNil) 259 c.Assert(info, gc.HasLen, 1) 260 c.Assert(info[0].CIDR, gc.Equals, "1.0.0.0/8") 261 262 lookFor = []network.Id{"IDontExist"} 263 _, err = s.env.Subnets(nil, instance.UnknownId, lookFor) 264 c.Check(err, gc.ErrorMatches, "failed to find the following subnet ids:.*IDontExist.*") 265 } 266 267 func (s *networkingSuite) TestSubnetsKnownInstanceId(c *gc.C) { 268 ctrl := s.patchEnv(c) 269 defer ctrl.Finish() 270 271 s.setupSubnetsKnownInstanceExpectations() 272 273 lookFor := []network.Id{ 274 network.Id("fakeSubnetId"), 275 } 276 info, err := s.env.Subnets(nil, instance.Id(s.testInstanceID), lookFor) 277 c.Assert(err, gc.IsNil) 278 c.Assert(info, gc.HasLen, 1) 279 c.Assert(info[0].CIDR, gc.Equals, "1.0.0.0/8") 280 281 lookFor = []network.Id{ 282 network.Id("notHere"), 283 } 284 _, err = s.env.Subnets(nil, instance.Id(s.testInstanceID), lookFor) 285 c.Check(err, gc.ErrorMatches, "failed to find the following subnet ids:.*notHere.*") 286 }