github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/provider/oci/instance_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 "strings" 9 10 ociCore "github.com/oracle/oci-go-sdk/v65/core" 11 "go.uber.org/mock/gomock" 12 gc "gopkg.in/check.v1" 13 14 "github.com/juju/juju/core/instance" 15 corenetwork "github.com/juju/juju/core/network" 16 "github.com/juju/juju/core/network/firewall" 17 "github.com/juju/juju/core/status" 18 "github.com/juju/juju/provider/common" 19 "github.com/juju/juju/provider/common/mocks" 20 "github.com/juju/juju/provider/oci" 21 ) 22 23 type instanceSuite struct { 24 commonSuite 25 } 26 27 var _ = gc.Suite(&instanceSuite{}) 28 29 func (s *instanceSuite) SetUpTest(c *gc.C) { 30 s.commonSuite.SetUpTest(c) 31 } 32 33 func (s *instanceSuite) TestNewInstance(c *gc.C) { 34 _, err := oci.NewInstance(ociCore.Instance{}, s.env) 35 c.Assert(err, gc.ErrorMatches, "Instance response does not contain an ID") 36 } 37 38 func (s *instanceSuite) TestId(c *gc.C) { 39 inst, err := oci.NewInstance(*s.ociInstance, s.env) 40 c.Assert(err, gc.IsNil) 41 id := inst.Id() 42 c.Assert(id, gc.Equals, instance.Id(s.testInstanceID)) 43 } 44 45 func (s *instanceSuite) TestStatus(c *gc.C) { 46 ctrl := s.patchEnv(c) 47 defer ctrl.Finish() 48 49 s.compute.EXPECT().GetInstance(gomock.Any(), gomock.Any()).Return(ociCore.GetInstanceResponse{Instance: *s.ociInstance}, nil) 50 inst, err := oci.NewInstance(*s.ociInstance, s.env) 51 c.Assert(err, gc.IsNil) 52 53 instStatus := inst.Status(nil) 54 expectedStatus := instance.Status{ 55 Status: status.Running, 56 Message: strings.ToLower(string(ociCore.InstanceLifecycleStateRunning)), 57 } 58 c.Assert(instStatus, gc.DeepEquals, expectedStatus) 59 60 // Change lifecycle and check again 61 s.ociInstance.LifecycleState = ociCore.InstanceLifecycleStateTerminating 62 63 s.compute.EXPECT().GetInstance(gomock.Any(), gomock.Any()).Return(ociCore.GetInstanceResponse{Instance: *s.ociInstance}, nil) 64 inst, err = oci.NewInstance(*s.ociInstance, s.env) 65 c.Assert(err, gc.IsNil) 66 67 instStatus = inst.Status(nil) 68 expectedStatus = instance.Status{ 69 Status: status.Running, 70 Message: strings.ToLower(string(ociCore.InstanceLifecycleStateTerminating)), 71 } 72 c.Assert(instStatus, gc.DeepEquals, expectedStatus) 73 } 74 75 func (s *instanceSuite) TestStatusNilRawInstanceResponse(c *gc.C) { 76 ctrl := s.patchEnv(c) 77 defer ctrl.Finish() 78 79 request, response := makeGetInstanceRequestResponse( 80 ociCore.Instance{ 81 CompartmentId: &s.testCompartment, 82 AvailabilityDomain: makeStringPointer("QXay:PHX-AD-3"), 83 Id: &s.testInstanceID, 84 Region: makeStringPointer("us-phoenix-1"), 85 Shape: makeStringPointer("VM.Standard1.1"), 86 DisplayName: makeStringPointer("fake"), 87 FreeformTags: s.tags, 88 LifecycleState: ociCore.InstanceLifecycleStateRunning, 89 }) 90 91 s.compute.EXPECT().GetInstance(context.Background(), request).Return(response, nil) 92 93 inst, err := oci.NewInstance(*s.ociInstance, s.env) 94 c.Assert(err, gc.IsNil) 95 96 instStatus := inst.Status(nil) 97 expectedStatus := instance.Status{ 98 Status: status.Running, 99 Message: strings.ToLower(string(ociCore.InstanceLifecycleStateRunning)), 100 } 101 c.Assert(instStatus, gc.DeepEquals, expectedStatus) 102 } 103 104 func (s *instanceSuite) setupListVnicsExpectations(instanceId, vnicID string) { 105 attachResponse := []ociCore.VnicAttachment{ 106 { 107 Id: makeStringPointer("fakeAttachmentId"), 108 AvailabilityDomain: makeStringPointer("fake"), 109 CompartmentId: makeStringPointer(s.testCompartment), 110 InstanceId: makeStringPointer(s.testInstanceID), 111 LifecycleState: ociCore.VnicAttachmentLifecycleStateAttached, 112 DisplayName: makeStringPointer("fakeAttachmentName"), 113 NicIndex: makeIntPointer(0), 114 VnicId: makeStringPointer(vnicID), 115 }, 116 } 117 118 // I am really sorry for this 119 trueBoolean := true 120 121 vnicRequest, vnicResponse := makeGetVnicRequestResponse([]ociCore.GetVnicResponse{ 122 { 123 Vnic: ociCore.Vnic{ 124 Id: makeStringPointer(vnicID), 125 PrivateIp: makeStringPointer("1.1.1.1"), 126 SubnetId: makeStringPointer("fakeSubnetId"), 127 DisplayName: makeStringPointer("fakeVnicName"), 128 MacAddress: makeStringPointer("11:11:11:11:11:11"), 129 PublicIp: makeStringPointer("2.2.2.2"), 130 IsPrimary: &trueBoolean, 131 LifecycleState: ociCore.VnicLifecycleStateAvailable, 132 }, 133 }, 134 }) 135 136 gomock.InOrder( 137 s.compute.EXPECT().ListVnicAttachments(context.Background(), &s.testCompartment, &s.testInstanceID).Return(attachResponse, nil), 138 s.netw.EXPECT().GetVnic(context.Background(), vnicRequest[0]).Return(vnicResponse[0], nil), 139 ) 140 } 141 142 func (s *instanceSuite) TestAddresses(c *gc.C) { 143 ctrl := s.patchEnv(c) 144 defer ctrl.Finish() 145 146 vnicID := "fakeVnicId" 147 s.setupListVnicsExpectations(s.testInstanceID, vnicID) 148 149 inst, err := oci.NewInstance(*s.ociInstance, s.env) 150 c.Assert(err, gc.IsNil) 151 152 addresses, err := inst.Addresses(nil) 153 c.Assert(err, gc.IsNil) 154 c.Check(addresses, gc.HasLen, 2) 155 c.Check(addresses[0].Scope, gc.Equals, corenetwork.ScopeCloudLocal) 156 c.Check(addresses[1].Scope, gc.Equals, corenetwork.ScopePublic) 157 } 158 159 func (s *instanceSuite) TestAddressesNoPublicIP(c *gc.C) { 160 ctrl := s.patchEnv(c) 161 defer ctrl.Finish() 162 163 vnicID := "fakeVnicId" 164 s.setupListVnicsExpectations(s.testInstanceID, vnicID) 165 166 inst, err := oci.NewInstance(*s.ociInstance, s.env) 167 c.Assert(err, gc.IsNil) 168 169 addresses, err := inst.Addresses(nil) 170 c.Assert(err, gc.IsNil) 171 c.Check(addresses, gc.HasLen, 2) 172 c.Check(addresses[0].Scope, gc.Equals, corenetwork.ScopeCloudLocal) 173 c.Check(addresses[1].Scope, gc.Equals, corenetwork.ScopePublic) 174 } 175 176 func (s *instanceSuite) TestInstanceConfiguratorUsesPublicAddress(c *gc.C) { 177 ctrl := s.patchEnv(c) 178 defer ctrl.Finish() 179 180 vnicID := "fakeVnicId" 181 s.setupListVnicsExpectations(s.testInstanceID, vnicID) 182 183 rules := firewall.IngressRules{{ 184 PortRange: corenetwork.PortRange{ 185 FromPort: 1234, 186 ToPort: 1234, 187 Protocol: "tcp", 188 }, 189 }} 190 191 ic := mocks.NewMockInstanceConfigurator(ctrl) 192 ic.EXPECT().ChangeIngressRules("", true, rules).Return(nil) 193 194 factory := func(addr string) common.InstanceConfigurator { 195 c.Assert(addr, gc.Equals, "2.2.2.2") 196 return ic 197 } 198 199 inst, err := oci.NewInstanceWithConfigurator(*s.ociInstance, s.env, factory) 200 c.Assert(err, gc.IsNil) 201 c.Assert(inst.OpenPorts(nil, "", rules), gc.IsNil) 202 }