github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/provider/oci/common_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  	"fmt"
     9  	"time"
    10  
    11  	"github.com/juju/clock/testclock"
    12  	gitjujutesting "github.com/juju/testing"
    13  	"github.com/juju/utils/arch"
    14  	"github.com/juju/version"
    15  
    16  	gomock "github.com/golang/mock/gomock"
    17  	ocitesting "github.com/juju/juju/provider/oci/testing"
    18  	jujutesting "github.com/juju/juju/testing"
    19  	jc "github.com/juju/testing/checkers"
    20  	gc "gopkg.in/check.v1"
    21  
    22  	ociCore "github.com/oracle/oci-go-sdk/core"
    23  	ociIdentity "github.com/oracle/oci-go-sdk/identity"
    24  
    25  	"github.com/juju/juju/environs"
    26  	"github.com/juju/juju/environs/config"
    27  	"github.com/juju/juju/environs/tags"
    28  	"github.com/juju/juju/provider/oci"
    29  	"github.com/juju/juju/tools"
    30  )
    31  
    32  var clk = testclock.NewClock(time.Time{})
    33  var advancingClock = testclock.AutoAdvancingClock{clk, clk.Advance}
    34  
    35  func makeToolsList(series string) tools.List {
    36  	var toolsVersion version.Binary
    37  	toolsVersion.Number = version.MustParse("2.4.0")
    38  	toolsVersion.Arch = arch.AMD64
    39  	toolsVersion.Series = series
    40  	return tools.List{{
    41  		Version: toolsVersion,
    42  		URL:     fmt.Sprintf("http://example.com/tools/juju-%s.tgz", toolsVersion),
    43  		SHA256:  "1234567890abcdef",
    44  		Size:    1024,
    45  	}}
    46  }
    47  
    48  func makeListSecurityListsRequestResponse(seclistDetails []ociCore.SecurityList) (ociCore.ListSecurityListsRequest, ociCore.ListSecurityListsResponse) {
    49  	if len(seclistDetails) == 0 {
    50  		return ociCore.ListSecurityListsRequest{}, ociCore.ListSecurityListsResponse{}
    51  	}
    52  
    53  	compartment := seclistDetails[0].CompartmentId
    54  	vcnId := seclistDetails[0].VcnId
    55  	request := ociCore.ListSecurityListsRequest{
    56  		CompartmentId: compartment,
    57  		VcnId:         vcnId,
    58  	}
    59  	response := ociCore.ListSecurityListsResponse{
    60  		Items: seclistDetails,
    61  	}
    62  
    63  	return request, response
    64  }
    65  
    66  func makeListInternetGatewaysRequestResponse(gwDetails []ociCore.InternetGateway) (ociCore.ListInternetGatewaysRequest, ociCore.ListInternetGatewaysResponse) {
    67  	if len(gwDetails) == 0 {
    68  		return ociCore.ListInternetGatewaysRequest{}, ociCore.ListInternetGatewaysResponse{}
    69  	}
    70  
    71  	compartment := gwDetails[0].CompartmentId
    72  	vcnId := gwDetails[0].VcnId
    73  	request := ociCore.ListInternetGatewaysRequest{
    74  		CompartmentId: compartment,
    75  		VcnId:         vcnId,
    76  	}
    77  	response := ociCore.ListInternetGatewaysResponse{
    78  		Items: gwDetails,
    79  	}
    80  	return request, response
    81  }
    82  
    83  func makeListRouteTableRequestResponse(routeDetails []ociCore.RouteTable) (ociCore.ListRouteTablesRequest, ociCore.ListRouteTablesResponse) {
    84  	if len(routeDetails) == 0 {
    85  		return ociCore.ListRouteTablesRequest{}, ociCore.ListRouteTablesResponse{}
    86  	}
    87  
    88  	compartment := routeDetails[0].CompartmentId
    89  	vcnId := routeDetails[0].VcnId
    90  
    91  	request := ociCore.ListRouteTablesRequest{
    92  		CompartmentId: compartment,
    93  		VcnId:         vcnId,
    94  	}
    95  	response := ociCore.ListRouteTablesResponse{
    96  		Items: routeDetails,
    97  	}
    98  	return request, response
    99  }
   100  
   101  func makeListInstancesRequestResponse(instances []ociCore.Instance) (ociCore.ListInstancesRequest, ociCore.ListInstancesResponse) {
   102  
   103  	if len(instances) == 0 {
   104  		return ociCore.ListInstancesRequest{}, ociCore.ListInstancesResponse{}
   105  	}
   106  
   107  	compartment := instances[0].CompartmentId
   108  
   109  	request := ociCore.ListInstancesRequest{
   110  		CompartmentId: compartment,
   111  	}
   112  
   113  	response := ociCore.ListInstancesResponse{
   114  		Items: instances,
   115  	}
   116  
   117  	return request, response
   118  }
   119  
   120  func makeListAvailabilityDomainsRequestResponse(azDetails []ociIdentity.AvailabilityDomain) (ociIdentity.ListAvailabilityDomainsRequest, ociIdentity.ListAvailabilityDomainsResponse) {
   121  	if len(azDetails) == 0 {
   122  		return ociIdentity.ListAvailabilityDomainsRequest{}, ociIdentity.ListAvailabilityDomainsResponse{}
   123  	}
   124  
   125  	compartment := azDetails[0].CompartmentId
   126  	request := ociIdentity.ListAvailabilityDomainsRequest{
   127  		CompartmentId: compartment,
   128  	}
   129  
   130  	response := ociIdentity.ListAvailabilityDomainsResponse{
   131  		Items: azDetails,
   132  	}
   133  	return request, response
   134  }
   135  
   136  func makeGetVnicRequestResponse(vnicDetails []ociCore.GetVnicResponse) ([]ociCore.GetVnicRequest, []ociCore.GetVnicResponse) {
   137  	requests := make([]ociCore.GetVnicRequest, len(vnicDetails))
   138  	responses := make([]ociCore.GetVnicResponse, len(vnicDetails))
   139  
   140  	for idx, val := range vnicDetails {
   141  		requests[idx] = ociCore.GetVnicRequest{
   142  			VnicId: val.Id,
   143  		}
   144  
   145  		responses[idx] = val
   146  	}
   147  
   148  	return requests, responses
   149  }
   150  
   151  func makeListVcnRequestResponse(vcnDetails []ociCore.Vcn) (ociCore.ListVcnsRequest, ociCore.ListVcnsResponse) {
   152  	if len(vcnDetails) == 0 {
   153  		return ociCore.ListVcnsRequest{}, ociCore.ListVcnsResponse{}
   154  	}
   155  	compartment := vcnDetails[0].CompartmentId
   156  	request := ociCore.ListVcnsRequest{
   157  		CompartmentId: compartment,
   158  	}
   159  
   160  	response := ociCore.ListVcnsResponse{
   161  		Items: vcnDetails,
   162  	}
   163  	return request, response
   164  }
   165  
   166  func makeListSubnetsRequestResponse(subnetDetails []ociCore.Subnet) (ociCore.ListSubnetsRequest, ociCore.ListSubnetsResponse) {
   167  	if len(subnetDetails) == 0 {
   168  		return ociCore.ListSubnetsRequest{}, ociCore.ListSubnetsResponse{}
   169  	}
   170  	compartment := subnetDetails[0].CompartmentId
   171  	vcnID := subnetDetails[0].VcnId
   172  
   173  	request := ociCore.ListSubnetsRequest{
   174  		CompartmentId: compartment,
   175  		VcnId:         vcnID,
   176  	}
   177  
   178  	response := ociCore.ListSubnetsResponse{
   179  		Items: subnetDetails,
   180  	}
   181  	return request, response
   182  }
   183  
   184  func newFakeOCIInstance(Id, compartment string, state ociCore.InstanceLifecycleStateEnum) *ociCore.Instance {
   185  	return &ociCore.Instance{
   186  		AvailabilityDomain: makeStringPointer("fake-az"),
   187  		Id:                 &Id,
   188  		CompartmentId:      &compartment,
   189  		Region:             makeStringPointer("us-phoenix-1"),
   190  		Shape:              makeStringPointer("VM.Standard1.1"),
   191  		DisplayName:        makeStringPointer("fakeName"),
   192  		FreeformTags:       map[string]string{},
   193  		LifecycleState:     state,
   194  	}
   195  }
   196  
   197  func makeGetInstanceRequestResponse(instanceDetails ociCore.Instance) (ociCore.GetInstanceRequest, ociCore.GetInstanceResponse) {
   198  	request := ociCore.GetInstanceRequest{
   199  		InstanceId: instanceDetails.Id,
   200  	}
   201  
   202  	response := ociCore.GetInstanceResponse{
   203  		Instance: instanceDetails,
   204  	}
   205  
   206  	return request, response
   207  }
   208  
   209  func makeListVnicAttachmentsRequestResponse(vnicAttachDetails []ociCore.VnicAttachment) (ociCore.ListVnicAttachmentsRequest, ociCore.ListVnicAttachmentsResponse) {
   210  	if len(vnicAttachDetails) == 0 {
   211  		return ociCore.ListVnicAttachmentsRequest{}, ociCore.ListVnicAttachmentsResponse{}
   212  	}
   213  	compartment := vnicAttachDetails[0].CompartmentId
   214  	instanceID := vnicAttachDetails[0].InstanceId
   215  
   216  	request := ociCore.ListVnicAttachmentsRequest{
   217  		CompartmentId: compartment,
   218  		InstanceId:    instanceID,
   219  	}
   220  
   221  	response := ociCore.ListVnicAttachmentsResponse{
   222  		Items: vnicAttachDetails,
   223  	}
   224  	return request, response
   225  }
   226  
   227  func makeShapesRequestResponse(compartment, id string, shapeNames []string) (ociCore.ListShapesRequest, ociCore.ListShapesResponse) {
   228  	shapesRequest := ociCore.ListShapesRequest{
   229  		CompartmentId: &compartment,
   230  		ImageId:       &id,
   231  	}
   232  
   233  	ociShapes := []ociCore.Shape{}
   234  	for _, val := range shapeNames {
   235  		shape := ociCore.Shape{
   236  			Shape: makeStringPointer(val),
   237  		}
   238  		ociShapes = append(ociShapes, shape)
   239  	}
   240  
   241  	shapesResponse := ociCore.ListShapesResponse{
   242  		Items: ociShapes,
   243  	}
   244  
   245  	return shapesRequest, shapesResponse
   246  }
   247  
   248  func makeListImageRequestResponse(imgDetails []ociCore.Image) (ociCore.ListImagesRequest, ociCore.ListImagesResponse) {
   249  	if len(imgDetails) == 0 {
   250  		return ociCore.ListImagesRequest{}, ociCore.ListImagesResponse{}
   251  	}
   252  
   253  	compartment := imgDetails[0].CompartmentId
   254  	request := ociCore.ListImagesRequest{
   255  		CompartmentId: compartment,
   256  	}
   257  	response := ociCore.ListImagesResponse{
   258  		Items: imgDetails,
   259  	}
   260  	return request, response
   261  }
   262  
   263  type commonSuite struct {
   264  	gitjujutesting.IsolationSuite
   265  
   266  	testInstanceID  string
   267  	testCompartment string
   268  	controllerUUID  string
   269  
   270  	ident   *ocitesting.MockOCIIdentityClient
   271  	compute *ocitesting.MockOCIComputeClient
   272  	netw    *ocitesting.MockOCINetworkingClient
   273  	fw      *ocitesting.MockOCIFirewallClient
   274  	storage *ocitesting.MockOCIStorageClient
   275  
   276  	env         *oci.Environ
   277  	provider    environs.EnvironProvider
   278  	spec        environs.CloudSpec
   279  	config      *config.Config
   280  	ociInstance *ociCore.Instance
   281  	tags        map[string]string
   282  	ctrlTags    map[string]string
   283  }
   284  
   285  func (s *commonSuite) SetUpTest(c *gc.C) {
   286  	s.IsolationSuite.SetUpTest(c)
   287  	oci.SetImageCache(&oci.ImageCache{})
   288  
   289  	s.controllerUUID = jujutesting.ControllerTag.Id()
   290  	s.testInstanceID = "ocid1.instance.oc1.phx.abyhqljt4bl4i76iforb7wczlc4qmmmrmzvngeyzi2n45bxsee7a11rukjla"
   291  	s.testCompartment = "ocid1.compartment.oc1..aaaaaaaaakr75vvb5yx4nkm7ag7ekvluap7afa2y4zprswuprcnehqecwqga"
   292  
   293  	s.provider = &oci.EnvironProvider{}
   294  	s.spec = fakeCloudSpec()
   295  
   296  	config := newConfig(c, jujutesting.Attrs{"compartment-id": s.testCompartment})
   297  	env, err := environs.Open(s.provider, environs.OpenParams{
   298  		Cloud:  s.spec,
   299  		Config: config,
   300  	})
   301  	c.Assert(err, jc.ErrorIsNil)
   302  	c.Assert(env, gc.NotNil)
   303  
   304  	s.config = config
   305  	s.env = env.(*oci.Environ)
   306  	s.env.SetClock(&advancingClock)
   307  
   308  	s.ociInstance = newFakeOCIInstance(s.testInstanceID, s.testCompartment, ociCore.InstanceLifecycleStateRunning)
   309  	s.tags = map[string]string{
   310  		tags.JujuController: s.controllerUUID,
   311  		tags.JujuModel:      config.UUID(),
   312  	}
   313  	s.ctrlTags = map[string]string{
   314  		tags.JujuController:   s.controllerUUID,
   315  		tags.JujuModel:        s.config.UUID(),
   316  		tags.JujuIsController: "true",
   317  	}
   318  	s.ociInstance.FreeformTags = s.tags
   319  }
   320  
   321  func (e *commonSuite) setupListInstancesExpectations(instanceId string, state ociCore.InstanceLifecycleStateEnum, times int) {
   322  	listInstancesRequest, listInstancesResponse := makeListInstancesRequestResponse(
   323  		[]ociCore.Instance{
   324  			{
   325  				AvailabilityDomain: makeStringPointer("fakeZone1"),
   326  				CompartmentId:      &e.testCompartment,
   327  				Id:                 makeStringPointer(instanceId),
   328  				LifecycleState:     state,
   329  				Region:             makeStringPointer("us-phoenix-1"),
   330  				Shape:              makeStringPointer("VM.Standard1.1"),
   331  				DisplayName:        makeStringPointer("fakeName"),
   332  				FreeformTags:       e.tags,
   333  			},
   334  		},
   335  	)
   336  	expect := e.compute.EXPECT().ListInstances(
   337  		context.Background(), listInstancesRequest).Return(
   338  		listInstancesResponse, nil)
   339  	if times == 0 {
   340  		expect.AnyTimes()
   341  	} else {
   342  		expect.Times(times)
   343  	}
   344  }
   345  
   346  func (s *commonSuite) patchEnv(c *gc.C) *gomock.Controller {
   347  	ctrl := gomock.NewController(c)
   348  	s.compute = ocitesting.NewMockOCIComputeClient(ctrl)
   349  	s.ident = ocitesting.NewMockOCIIdentityClient(ctrl)
   350  	s.netw = ocitesting.NewMockOCINetworkingClient(ctrl)
   351  	s.fw = ocitesting.NewMockOCIFirewallClient(ctrl)
   352  	s.storage = ocitesting.NewMockOCIStorageClient(ctrl)
   353  
   354  	s.env.Compute = s.compute
   355  	s.env.Networking = s.netw
   356  	s.env.Firewall = s.fw
   357  	s.env.Storage = s.storage
   358  	s.env.Identity = s.ident
   359  
   360  	return ctrl
   361  }