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