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

     1  //go:build api || functional || catalog || org || extnetwork || vm || vdc || system || user || nsxv || network || vapp || vm || affinity || ALL
     2  
     3  /*
     4   * Copyright 2019 VMware, Inc.  All rights reserved.  Licensed under the Apache v2 License.
     5   */
     6  
     7  package govcd
     8  
     9  import (
    10  	"fmt"
    11  	"reflect"
    12  	"strings"
    13  
    14  	. "gopkg.in/check.v1"
    15  )
    16  
    17  // An interface used to test the result of Get* methods
    18  type genericEntity interface {
    19  	name() string // returns the entity name
    20  	id() string   // returns the entity ID
    21  }
    22  
    23  // Defines a generic getter to test all Get* methods
    24  type getterTestDefinition struct {
    25  	parentName    string                                    // Name of the parent entity
    26  	parentType    string                                    // Type of the parent entity
    27  	entityType    string                                    // Type of the entity to retrieve (Must match the type name)
    28  	entityName    string                                    // Name of the entity to retrieve
    29  	getterPrefix  string                                    // Base name for getter functions
    30  	getByName     func(string, bool) (genericEntity, error) // A function that retrieves the entity by name
    31  	getById       func(string, bool) (genericEntity, error) // A function that retrieves the entity by ID
    32  	getByNameOrId func(string, bool) (genericEntity, error) // A function that retrieves the entity by name or ID
    33  }
    34  
    35  // Satisfy interface genericEntity
    36  func (adminCat *AdminCatalog) name() string { return adminCat.AdminCatalog.Name }
    37  func (adminCat *AdminCatalog) id() string   { return adminCat.AdminCatalog.ID }
    38  
    39  func (adminOrg *AdminOrg) name() string { return adminOrg.AdminOrg.Name }
    40  func (adminOrg *AdminOrg) id() string   { return adminOrg.AdminOrg.ID }
    41  
    42  func (vdc *AdminVdc) name() string { return vdc.AdminVdc.Name }
    43  func (vdc *AdminVdc) id() string   { return vdc.AdminVdc.ID }
    44  
    45  func (cat *Catalog) name() string { return cat.Catalog.Name }
    46  func (cat *Catalog) id() string   { return cat.Catalog.ID }
    47  
    48  func (catItem *CatalogItem) name() string { return catItem.CatalogItem.Name }
    49  func (catItem *CatalogItem) id() string   { return catItem.CatalogItem.ID }
    50  
    51  func (extNet *ExternalNetwork) name() string { return extNet.ExternalNetwork.Name }
    52  func (extNet *ExternalNetwork) id() string   { return extNet.ExternalNetwork.ID }
    53  
    54  func (org *Org) name() string { return org.Org.Name }
    55  func (org *Org) id() string   { return org.Org.ID }
    56  
    57  func (orgUser *OrgUser) name() string { return orgUser.User.Name }
    58  func (orgUser *OrgUser) id() string   { return orgUser.User.ID }
    59  
    60  func (orgGroup *OrgGroup) name() string { return orgGroup.Group.Name }
    61  func (orgGroup *OrgGroup) id() string   { return orgGroup.Group.ID }
    62  
    63  func (vdc *Vdc) name() string { return vdc.Vdc.Name }
    64  func (vdc *Vdc) id() string   { return vdc.Vdc.ID }
    65  
    66  func (network *OrgVDCNetwork) name() string { return network.OrgVDCNetwork.Name }
    67  func (network *OrgVDCNetwork) id() string   { return network.OrgVDCNetwork.ID }
    68  
    69  func (egw *EdgeGateway) name() string { return egw.EdgeGateway.Name }
    70  func (egw *EdgeGateway) id() string   { return egw.EdgeGateway.ID }
    71  
    72  func (vapp *VApp) name() string { return vapp.VApp.Name }
    73  func (vapp *VApp) id() string   { return vapp.VApp.ID }
    74  
    75  func (vm *VM) name() string { return vm.VM.Name }
    76  func (vm *VM) id() string   { return vm.VM.ID }
    77  
    78  func (media *Media) name() string { return media.Media.Name }
    79  func (media *Media) id() string   { return media.Media.ID }
    80  
    81  func (vmar *VmAffinityRule) name() string { return vmar.VmAffinityRule.Name }
    82  func (vmar *VmAffinityRule) id() string   { return vmar.VmAffinityRule.ID }
    83  
    84  // Semi-generic tests that check the complete set of Get methods for an entity
    85  // GetEntityByName
    86  // GetEntityById
    87  // getEntityByNameOrId (using name or Id)
    88  // Get invalid name or ID
    89  // To use this function, the entity must satisfy the interface genericEntity
    90  // and within the caller it must define the getter functions
    91  // Example usage:
    92  //
    93  //	func (vcd *TestVCD) Test_OrgGetVdc(check *C) {
    94  //		if vcd.config.VCD.Org == "" {
    95  //			check.Skip("Test_OrgGetVdc: Org name not given.")
    96  //			return
    97  //		}
    98  //		org, err := vcd.client.GetOrgByName(vcd.config.VCD.Org)
    99  //		check.Assert(err, IsNil)
   100  //		check.Assert(org, NotNil)
   101  //
   102  //		getByName := func(name string, refresh bool) (genericEntity, error) { return org.GetVDCByName(name, refresh) }
   103  //		getById := func(id string, refresh bool) (genericEntity, error) { return org.GetVDCById(id, refresh) }
   104  //		getByNameOrId := func(id string, refresh bool) (genericEntity, error) { return org.GetVDCByNameOrId(id, refresh) }
   105  //
   106  //		var def = getterTestDefinition{
   107  //			parentType:       "Org",
   108  //			parentName:       vcd.config.VCD.Org,
   109  //			entityType:       "Vdc",
   110  //			getterPrefix:     "VDC",
   111  //			entityName:       vcd.config.VCD.Vdc,
   112  //			getByName:        getByName,
   113  //			getById:          getById,
   114  //			getByNameOrId:    getByNameOrId,
   115  //		}
   116  //		vcd.testFinderGetGenericEntity(def, check)
   117  //	}
   118  func (vcd *TestVCD) testFinderGetGenericEntity(def getterTestDefinition, check *C) {
   119  	entityName := def.entityName
   120  	if entityName == "" {
   121  		check.Skip(fmt.Sprintf("testFinderGetGenericEntity: %s name not given.", def.entityType))
   122  		return
   123  	}
   124  
   125  	if def.getterPrefix == "" {
   126  		def.getterPrefix = def.entityType
   127  	}
   128  	if def.parentType == "" {
   129  		check.Skip("Field parentType was not filled.")
   130  	}
   131  	if testVerbose {
   132  		fmt.Printf("testFinderGetGenericEntity: %s %s getting %s \n", def.parentType, def.parentName, def.entityType)
   133  	}
   134  
   135  	// 1. Get the entity by name
   136  
   137  	if testVerbose {
   138  		fmt.Printf("#Testing %s.Get%sByName\n", def.parentType, def.getterPrefix)
   139  	}
   140  	ge, err := def.getByName(entityName, false)
   141  	if err != nil {
   142  		check.Skip(fmt.Sprintf("testFinderGetGenericEntity: %s %s not found.", def.entityType, def.entityName))
   143  		return
   144  	}
   145  	entity1 := ge
   146  
   147  	wantedType := fmt.Sprintf("*govcd.%s", def.entityType)
   148  	if testVerbose {
   149  		fmt.Printf("# Detected entity type %s\n", reflect.TypeOf(entity1))
   150  	}
   151  
   152  	check.Assert(strings.ToLower(reflect.TypeOf(entity1).String()), Equals, strings.ToLower(wantedType))
   153  
   154  	check.Assert(entity1, NotNil)
   155  	check.Assert(entity1.name(), Equals, entityName)
   156  	entityId := entity1.id()
   157  
   158  	// 2. Get the entity by ID
   159  	if testVerbose {
   160  		fmt.Printf("#Testing %s.Get%sById (using ID '%s')\n", def.parentType, def.getterPrefix, entityId)
   161  	}
   162  	ge, err = def.getById(entityId, false)
   163  	check.Assert(err, IsNil)
   164  	check.Assert(ge, NotNil)
   165  	entity2 := ge
   166  	check.Assert(entity2, NotNil)
   167  	check.Assert(entity2.name(), Equals, entityName)
   168  	check.Assert(entity2.id(), Equals, entityId)
   169  	check.Assert(strings.ToLower(reflect.TypeOf(entity2).String()), Equals, strings.ToLower(wantedType))
   170  
   171  	// 3. Get the entity by Name or ID, using a known ID
   172  	if testVerbose {
   173  		fmt.Printf("#Testing %s.Get%sByNameOrId\n", def.parentType, def.getterPrefix)
   174  	}
   175  	ge, err = def.getByNameOrId(entityId, false)
   176  	check.Assert(err, IsNil)
   177  	check.Assert(ge, NotNil)
   178  	entity3 := ge
   179  	check.Assert(entity3, NotNil)
   180  	check.Assert(entity3.name(), Equals, entityName)
   181  	check.Assert(entity3.id(), Equals, entityId)
   182  	check.Assert(strings.ToLower(reflect.TypeOf(entity3).String()), Equals, strings.ToLower(wantedType))
   183  
   184  	// 4. Get the entity by Name or ID, using the entity name
   185  	if testVerbose {
   186  		fmt.Printf("#Testing %s.Get%sByNameOrId (name '%s', ID '%s')\n",
   187  			def.parentType, def.getterPrefix, entityName, entityId)
   188  	}
   189  	ge, err = def.getByNameOrId(entityName, false)
   190  	check.Assert(err, IsNil)
   191  	check.Assert(ge, NotNil)
   192  	entity4 := ge
   193  	check.Assert(entity4, NotNil)
   194  	check.Assert(entity4.name(), Equals, entityName)
   195  	check.Assert(entity4.id(), Equals, entityId)
   196  	check.Assert(strings.ToLower(reflect.TypeOf(entity4).String()), Equals, strings.ToLower(wantedType))
   197  
   198  	// 5. Attempting a search by name with an invalid name
   199  	if testVerbose {
   200  		fmt.Printf("#Testing %s.Get%sByName (invalid name)\n", def.parentType, def.getterPrefix)
   201  	}
   202  	ge, err = def.getByName(INVALID_NAME, false)
   203  	check.Assert(err, NotNil)
   204  	entity5 := ge // this is expected to be nil
   205  	check.Assert(IsNotFound(err), Equals, true)
   206  	check.Assert(entity5, IsNil)
   207  
   208  	// 6. Attempting a search by name or ID with an invalid name
   209  	if testVerbose {
   210  		fmt.Printf("#Testing %s.Get%sByNameOrId (invalid name)\n", def.parentType, def.getterPrefix)
   211  	}
   212  	ge, err = def.getByNameOrId(INVALID_NAME, false)
   213  	check.Assert(err, NotNil)
   214  	entity6 := ge // this is expected to be nil
   215  	check.Assert(IsNotFound(err), Equals, true)
   216  	check.Assert(entity6, IsNil)
   217  
   218  	// 7. Attempting a search by ID with an invalid ID
   219  	if testVerbose {
   220  		fmt.Printf("#Testing %s.Get%sById (invalid ID)\n", def.parentType, def.getterPrefix)
   221  	}
   222  	ge, err = def.getById(invalidEntityId, false)
   223  	check.Assert(err, NotNil)
   224  	entity7 := ge // this is expected to be nil
   225  	check.Assert(IsNotFound(err), Equals, true)
   226  	check.Assert(entity7, IsNil)
   227  
   228  	// 8. Attempting a search by name or ID with an invalid ID
   229  	if testVerbose {
   230  		fmt.Printf("#Testing %s.Get%sByNameOrId (invalid ID)\n", def.parentType, def.getterPrefix)
   231  	}
   232  	ge, err = def.getByNameOrId(invalidEntityId, false)
   233  	check.Assert(err, NotNil)
   234  	entity8 := ge // this is expected to be nil
   235  	check.Assert(IsNotFound(err), Equals, true)
   236  	check.Assert(entity8, IsNil)
   237  }