github.com/vmware/go-vcloud-director/v2@v2.24.0/govcd/vdccomputepolicy_test.go (about)

     1  //go:build vdc || functional || openapi || ALL
     2  
     3  /*
     4   * Copyright 2022 VMware, Inc.  All rights reserved.  Licensed under the Apache v2 License.
     5   */
     6  
     7  package govcd
     8  
     9  import (
    10  	"fmt"
    11  	"net/url"
    12  	"strings"
    13  
    14  	"github.com/vmware/go-vcloud-director/v2/types/v56"
    15  	. "gopkg.in/check.v1"
    16  )
    17  
    18  func (vcd *TestVCD) Test_VdcComputePolicies(check *C) {
    19  	if vcd.skipAdminTests {
    20  		check.Skip(fmt.Sprintf(TestRequiresSysAdminPrivileges, check.TestName()))
    21  	}
    22  
    23  	client := &vcd.client.Client
    24  	// Step 1 - Create a new VDC compute policies
    25  	newComputePolicy := &VdcComputePolicy{
    26  		client: client,
    27  		VdcComputePolicy: &types.VdcComputePolicy{
    28  			Name:        check.TestName() + "_empty",
    29  			Description: addrOf("Empty policy created by test"),
    30  		},
    31  	}
    32  
    33  	createdPolicy, err := client.CreateVdcComputePolicy(newComputePolicy.VdcComputePolicy)
    34  	check.Assert(err, IsNil)
    35  
    36  	AddToCleanupList(createdPolicy.VdcComputePolicy.ID, "vdcComputePolicy", "", check.TestName())
    37  
    38  	check.Assert(createdPolicy.VdcComputePolicy.Name, Equals, newComputePolicy.VdcComputePolicy.Name)
    39  	check.Assert(*createdPolicy.VdcComputePolicy.Description, Equals, *newComputePolicy.VdcComputePolicy.Description)
    40  
    41  	newComputePolicy2 := &VdcComputePolicy{
    42  		client: client,
    43  		VdcComputePolicy: &types.VdcComputePolicy{
    44  			Name:                       check.TestName(),
    45  			Description:                addrOf("Not Empty policy created by test"),
    46  			CPUSpeed:                   addrOf(100),
    47  			CPUCount:                   addrOf(2),
    48  			CoresPerSocket:             addrOf(1),
    49  			CPUReservationGuarantee:    takeFloatAddress(0.26),
    50  			CPULimit:                   addrOf(200),
    51  			CPUShares:                  addrOf(5),
    52  			Memory:                     addrOf(1600),
    53  			MemoryReservationGuarantee: takeFloatAddress(0.5),
    54  			MemoryLimit:                addrOf(1200),
    55  			MemoryShares:               addrOf(500),
    56  		},
    57  	}
    58  
    59  	createdPolicy2, err := client.CreateVdcComputePolicy(newComputePolicy2.VdcComputePolicy)
    60  	check.Assert(err, IsNil)
    61  
    62  	AddToCleanupList(createdPolicy2.VdcComputePolicy.ID, "vdcComputePolicy", "", check.TestName())
    63  
    64  	check.Assert(createdPolicy2.VdcComputePolicy.Name, Equals, newComputePolicy2.VdcComputePolicy.Name)
    65  	check.Assert(*createdPolicy2.VdcComputePolicy.CPUSpeed, Equals, 100)
    66  	check.Assert(*createdPolicy2.VdcComputePolicy.CPUCount, Equals, 2)
    67  	check.Assert(*createdPolicy2.VdcComputePolicy.CoresPerSocket, Equals, 1)
    68  	check.Assert(*createdPolicy2.VdcComputePolicy.CPUReservationGuarantee, Equals, 0.26)
    69  	check.Assert(*createdPolicy2.VdcComputePolicy.CPULimit, Equals, 200)
    70  	check.Assert(*createdPolicy2.VdcComputePolicy.CPUShares, Equals, 5)
    71  	check.Assert(*createdPolicy2.VdcComputePolicy.Memory, Equals, 1600)
    72  	check.Assert(*createdPolicy2.VdcComputePolicy.MemoryReservationGuarantee, Equals, 0.5)
    73  	check.Assert(*createdPolicy2.VdcComputePolicy.MemoryLimit, Equals, 1200)
    74  	check.Assert(*createdPolicy2.VdcComputePolicy.MemoryShares, Equals, 500)
    75  
    76  	// Step 2 - update
    77  	createdPolicy2.VdcComputePolicy.Description = addrOf("Updated description")
    78  	updatedPolicy, err := createdPolicy2.Update()
    79  	check.Assert(err, IsNil)
    80  	check.Assert(updatedPolicy.VdcComputePolicy, DeepEquals, createdPolicy2.VdcComputePolicy)
    81  
    82  	// Step 3 - Get all VDC compute policies
    83  	allExistingPolicies, err := client.GetAllVdcComputePolicies(nil)
    84  	check.Assert(err, IsNil)
    85  	check.Assert(allExistingPolicies, NotNil)
    86  
    87  	// Step 4 - Get all VDC compute policies using query filters
    88  	for _, onePolicy := range allExistingPolicies {
    89  
    90  		// Step 3.1 - retrieve  using FIQL filter
    91  		queryParams := url.Values{}
    92  		queryParams.Add("filter", "id=="+onePolicy.VdcComputePolicy.ID)
    93  
    94  		expectOnePolicyResultById, err := client.GetAllVdcComputePolicies(queryParams)
    95  		check.Assert(err, IsNil)
    96  		check.Assert(len(expectOnePolicyResultById) == 1, Equals, true)
    97  
    98  		// Step 2.2 - retrieve
    99  		exactItem, err := client.GetVdcComputePolicyById(onePolicy.VdcComputePolicy.ID)
   100  		check.Assert(err, IsNil)
   101  
   102  		check.Assert(err, IsNil)
   103  		check.Assert(exactItem, NotNil)
   104  
   105  		// Step 2.3 - compare struct retrieved by using filter and the one retrieved by exact ID
   106  		check.Assert(onePolicy, DeepEquals, expectOnePolicyResultById[0])
   107  
   108  	}
   109  
   110  	// Step 5 - delete
   111  	err = createdPolicy.Delete()
   112  	check.Assert(err, IsNil)
   113  	// Step 5 - try to read deleted VDC computed policy should end up with error 'ErrorEntityNotFound'
   114  	deletedPolicy, err := client.GetVdcComputePolicyById(createdPolicy.VdcComputePolicy.ID)
   115  	check.Assert(ContainsNotFound(err), Equals, true)
   116  	check.Assert(deletedPolicy, IsNil)
   117  
   118  	err = createdPolicy2.Delete()
   119  	check.Assert(err, IsNil)
   120  	deletedPolicy2, err := client.GetVdcComputePolicyById(createdPolicy2.VdcComputePolicy.ID)
   121  	check.Assert(ContainsNotFound(err), Equals, true)
   122  	check.Assert(deletedPolicy2, IsNil)
   123  }
   124  
   125  func (vcd *TestVCD) Test_SetAssignedComputePolicies(check *C) {
   126  	if vcd.skipAdminTests {
   127  		check.Skip(fmt.Sprintf(TestRequiresSysAdminPrivileges, check.TestName()))
   128  	}
   129  
   130  	client := &vcd.client.Client
   131  	org, err := vcd.client.GetAdminOrgByName(vcd.org.Org.Name)
   132  	check.Assert(err, IsNil)
   133  	check.Assert(org, NotNil)
   134  
   135  	adminVdc, err := org.GetAdminVDCByName(vcd.vdc.Vdc.Name, false)
   136  	if adminVdc == nil || err != nil {
   137  		vcd.infoCleanup(notFoundMsg, "vdc", vcd.vdc.Vdc.Name)
   138  	}
   139  
   140  	// Step 1 - Create a new VDC compute policies
   141  	newComputePolicy := &VdcComputePolicy{
   142  		client: org.client,
   143  		VdcComputePolicy: &types.VdcComputePolicy{
   144  			Name:                    check.TestName() + "1",
   145  			Description:             addrOf("Policy created by Test_SetAssignedComputePolicies"),
   146  			CoresPerSocket:          addrOf(1),
   147  			CPUReservationGuarantee: takeFloatAddress(0.26),
   148  			CPULimit:                addrOf(200),
   149  		},
   150  	}
   151  	createdPolicy, err := client.CreateVdcComputePolicy(newComputePolicy.VdcComputePolicy)
   152  	check.Assert(err, IsNil)
   153  	AddToCleanupList(createdPolicy.VdcComputePolicy.ID, "vdcComputePolicy", "", check.TestName())
   154  
   155  	newComputePolicy2 := &VdcComputePolicy{
   156  		client: org.client,
   157  		VdcComputePolicy: &types.VdcComputePolicy{
   158  			Name:                    check.TestName() + "2",
   159  			Description:             addrOf("Policy created by Test_SetAssignedComputePolicies"),
   160  			CoresPerSocket:          addrOf(2),
   161  			CPUReservationGuarantee: takeFloatAddress(0.52),
   162  			CPULimit:                addrOf(400),
   163  		},
   164  	}
   165  	createdPolicy2, err := client.CreateVdcComputePolicy(newComputePolicy2.VdcComputePolicy)
   166  	check.Assert(err, IsNil)
   167  	AddToCleanupList(createdPolicy2.VdcComputePolicy.ID, "vdcComputePolicy", "", check.TestName())
   168  
   169  	// Get default compute policy
   170  	allAssignedComputePolicies, err := adminVdc.GetAllAssignedVdcComputePolicies(nil)
   171  	check.Assert(err, IsNil)
   172  	var defaultPolicyId string
   173  	for _, assignedPolicy := range allAssignedComputePolicies {
   174  		if assignedPolicy.VdcComputePolicy.ID == vcd.vdc.Vdc.DefaultComputePolicy.ID {
   175  			defaultPolicyId = assignedPolicy.VdcComputePolicy.ID
   176  		}
   177  	}
   178  
   179  	vdcComputePolicyHref, err := org.client.OpenApiBuildEndpoint(types.OpenApiPathVersion1_0_0, types.OpenApiEndpointVdcComputePolicies)
   180  	check.Assert(err, IsNil)
   181  
   182  	// Assign compute policies to VDC
   183  	policyReferences := types.VdcComputePolicyReferences{VdcComputePolicyReference: []*types.Reference{&types.Reference{HREF: vdcComputePolicyHref.String() + createdPolicy.VdcComputePolicy.ID},
   184  		&types.Reference{HREF: vdcComputePolicyHref.String() + createdPolicy2.VdcComputePolicy.ID},
   185  		{HREF: vdcComputePolicyHref.String() + defaultPolicyId}}}
   186  
   187  	assignedVdcComputePolicies, err := adminVdc.SetAssignedComputePolicies(policyReferences)
   188  	check.Assert(err, IsNil)
   189  	check.Assert(strings.SplitAfter(policyReferences.VdcComputePolicyReference[0].HREF, "vdcComputePolicy:")[1], Equals,
   190  		strings.SplitAfter(assignedVdcComputePolicies.VdcComputePolicyReference[0].HREF, "vdcComputePolicy:")[1])
   191  	check.Assert(strings.SplitAfter(policyReferences.VdcComputePolicyReference[1].HREF, "vdcComputePolicy:")[1], Equals,
   192  		strings.SplitAfter(assignedVdcComputePolicies.VdcComputePolicyReference[1].HREF, "vdcComputePolicy:")[1])
   193  
   194  	// cleanup assigned compute policies
   195  	policyReferences = types.VdcComputePolicyReferences{VdcComputePolicyReference: []*types.Reference{
   196  		{HREF: vdcComputePolicyHref.String() + defaultPolicyId}}}
   197  
   198  	_, err = adminVdc.SetAssignedComputePolicies(policyReferences)
   199  	check.Assert(err, IsNil)
   200  
   201  	err = createdPolicy.Delete()
   202  	check.Assert(err, IsNil)
   203  	err = createdPolicy2.Delete()
   204  	check.Assert(err, IsNil)
   205  }