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

     1  //go:build unit || 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  	"github.com/vmware/go-vcloud-director/v2/types/v56"
    12  	"testing"
    13  )
    14  
    15  // TestVmGroupFilterWithResourcePools tests that the filter for VM Groups works correctly, as it depends
    16  // heavily on Resource Pool data type handling.
    17  func TestVmGroupFilterWithResourcePools(t *testing.T) {
    18  
    19  	// This function generates a few dummy Resource Pools
    20  	getDummyResourcePools := func(howMany int, generateErrors bool) []*types.QueryResultResourcePoolRecordType {
    21  		var resourcePools []*types.QueryResultResourcePoolRecordType
    22  		clusterMoref := ""
    23  		vCenterHREF := ""
    24  		for i := 0; i < howMany; i++ {
    25  			if !generateErrors {
    26  				clusterMoref = fmt.Sprintf("domain-%d", i%10)
    27  				vCenterHREF = fmt.Sprintf("https://my-company-vcd.com/api/admin/extension/vimServer/f583b76e-9e34-48e7-b90d-930653ee161%d", i%10)
    28  			}
    29  			resourcePools = append(resourcePools, &types.QueryResultResourcePoolRecordType{
    30  				ClusterMoref: clusterMoref,
    31  				VcenterHREF:  vCenterHREF,
    32  			})
    33  		}
    34  		return resourcePools
    35  	}
    36  
    37  	type testData struct {
    38  		name           string
    39  		resourcePools  []*types.QueryResultResourcePoolRecordType
    40  		idKey          string
    41  		idValue        string
    42  		expectedFilter string
    43  		expectedError  string
    44  	}
    45  	var testItems = []testData{
    46  		{
    47  			name:           "create_filter_with_many_resource_pools_and_vm_group_id",
    48  			resourcePools:  getDummyResourcePools(2, false),
    49  			idKey:          "namedVmGroupId",
    50  			idValue:        "12345678-9012-3456-7890-123456789012",
    51  			expectedFilter: "(namedVmGroupId==12345678-9012-3456-7890-123456789012;(clusterMoref==domain-0,clusterMoref==domain-1);(vcId==f583b76e-9e34-48e7-b90d-930653ee1610,vcId==f583b76e-9e34-48e7-b90d-930653ee1611))",
    52  			expectedError:  "",
    53  		},
    54  		{
    55  			name:           "create_filter_with_one_resource_pool_and_vm_group_id",
    56  			resourcePools:  getDummyResourcePools(1, false),
    57  			idKey:          "namedVmGroupId",
    58  			idValue:        "12345678-9012-3456-7890-123456789012",
    59  			expectedFilter: "(namedVmGroupId==12345678-9012-3456-7890-123456789012;(clusterMoref==domain-0);(vcId==f583b76e-9e34-48e7-b90d-930653ee1610))",
    60  			expectedError:  "",
    61  		},
    62  		{
    63  			name:           "create_filter_with_many_resource_pools_and_vm_group_name",
    64  			resourcePools:  getDummyResourcePools(2, false),
    65  			idKey:          "vmGroupName",
    66  			idValue:        "testVmGroup",
    67  			expectedFilter: "(vmGroupName==testVmGroup;(clusterMoref==domain-0,clusterMoref==domain-1);(vcId==f583b76e-9e34-48e7-b90d-930653ee1610,vcId==f583b76e-9e34-48e7-b90d-930653ee1611))",
    68  			expectedError:  "",
    69  		},
    70  		{
    71  			name:           "create_filter_with_one_resource_pool_and_vm_group_name",
    72  			resourcePools:  getDummyResourcePools(1, false),
    73  			idKey:          "vmGroupName",
    74  			idValue:        "testVmGroup",
    75  			expectedFilter: "(vmGroupName==testVmGroup;(clusterMoref==domain-0);(vcId==f583b76e-9e34-48e7-b90d-930653ee1610))",
    76  			expectedError:  "",
    77  		},
    78  		{
    79  			name:           "create_filter_with_one_wrong_resource_pool_should_fail",
    80  			resourcePools:  getDummyResourcePools(1, true),
    81  			idKey:          "someKey",
    82  			idValue:        "someValue",
    83  			expectedFilter: "",
    84  			expectedError:  "could not retrieve Resource pools information to retrieve VM Group with someKey=someValue",
    85  		},
    86  		{
    87  			name:           "create_filter_with_no_identifier_nor_value_should_fail",
    88  			resourcePools:  getDummyResourcePools(1, true),
    89  			idKey:          "",
    90  			idValue:        "",
    91  			expectedFilter: "someFilter",
    92  			expectedError:  "identifier must have a key and value to be able to search",
    93  		},
    94  		{
    95  			name:           "create_filter_with_no_resource_pools_should_fail",
    96  			resourcePools:  getDummyResourcePools(0, false),
    97  			idKey:          "someKey",
    98  			idValue:        "someValue",
    99  			expectedFilter: "",
   100  			expectedError:  "could not retrieve Resource pools information to retrieve VM Group with someKey=someValue",
   101  		},
   102  	}
   103  	for _, test := range testItems {
   104  		t.Run(test.name, func(t *testing.T) {
   105  			filter, err := buildFilterForVmGroups(test.resourcePools, test.idKey, test.idValue)
   106  			if test.expectedError == "" {
   107  				// Successful path
   108  				if filter != test.expectedFilter {
   109  					t.Errorf("Expected this filter: '%s' for %s=%s but got: %s", test.expectedFilter, test.idKey, test.idValue, filter)
   110  				}
   111  			} else {
   112  				// Error path
   113  				if err == nil || err.Error() != test.expectedError {
   114  					t.Errorf("Expected error for %s=%s but got: %s", test.idKey, test.idValue, err)
   115  				}
   116  			}
   117  		})
   118  	}
   119  }