github.com/vmware/go-vcloud-director/v2@v2.24.0/govcd/catalogitem_test.go (about) 1 //go:build catalog || functional || ALL 2 3 /* 4 * Copyright 2020 VMware, Inc. All rights reserved. Licensed under the Apache v2 License. 5 */ 6 7 package govcd 8 9 import ( 10 "fmt" 11 //"strings" 12 13 . "gopkg.in/check.v1" 14 15 "github.com/davecgh/go-spew/spew" 16 "github.com/vmware/go-vcloud-director/v2/types/v56" 17 ) 18 19 func (vcd *TestVCD) Test_GetVAppTemplate(check *C) { 20 21 fmt.Printf("Running: %s\n", check.TestName()) 22 cat, err := vcd.org.GetCatalogByName(vcd.config.VCD.Catalog.Name, false) 23 if err != nil { 24 check.Skip("Test_GetVAppTemplate: Catalog not found. Test can't proceed") 25 return 26 } 27 check.Assert(cat, NotNil) 28 29 if vcd.config.VCD.Catalog.CatalogItem == "" { 30 check.Skip("Test_GetVAppTemplate: Catalog Item not given. Test can't proceed") 31 } 32 33 catitem, err := cat.GetCatalogItemByName(vcd.config.VCD.Catalog.CatalogItem, false) 34 check.Assert(err, IsNil) 35 36 // Get VAppTemplate 37 vapptemplate, err := catitem.GetVAppTemplate() 38 39 check.Assert(err, IsNil) 40 check.Assert(vapptemplate.VAppTemplate.Name, Equals, vcd.config.VCD.Catalog.CatalogItem) 41 if vcd.config.VCD.Catalog.CatalogItemDescription != "" { 42 check.Assert(vapptemplate.VAppTemplate.Description, Equals, vcd.config.VCD.Catalog.CatalogItemDescription) 43 } 44 } 45 46 // Tests System function Delete by creating catalog item and 47 // deleting it after. 48 func (vcd *TestVCD) Test_Delete(check *C) { 49 skipWhenOvaPathMissing(vcd.config.OVA.OvaPath, check) 50 AddToCleanupList(TestDeleteCatalogItem, "catalogItem", vcd.org.Org.Name+"|"+vcd.config.VCD.Catalog.Name, "Test_Delete") 51 52 // Fetching organization 53 org, err := vcd.client.GetAdminOrgByName(vcd.org.Org.Name) 54 check.Assert(err, IsNil) 55 check.Assert(org, NotNil) 56 57 catalog, err := org.GetCatalogByName(vcd.config.VCD.Catalog.Name, false) 58 check.Assert(err, IsNil) 59 check.Assert(catalog, NotNil) 60 61 // add catalogItem 62 uploadTask, err := catalog.UploadOvf(vcd.config.OVA.OvaPath, TestDeleteCatalogItem, "upload from delete catalog item test", 1024) 63 check.Assert(err, IsNil) 64 err = uploadTask.WaitTaskCompletion() 65 check.Assert(err, IsNil) 66 67 catalog, err = org.GetCatalogByName(vcd.config.VCD.Catalog.Name, true) 68 check.Assert(err, IsNil) 69 catalogItem, err := catalog.GetCatalogItemByName(TestDeleteCatalogItem, false) 70 check.Assert(err, IsNil) 71 72 err = catalogItem.Delete() 73 check.Assert(err, IsNil) 74 75 // check through existing catalogItems 76 catalog, err = org.GetCatalogByName(vcd.config.VCD.Catalog.Name, false) 77 check.Assert(err, IsNil) 78 entityFound := false 79 for _, catalogItems := range catalog.Catalog.CatalogItems { 80 for _, catalogItem := range catalogItems.CatalogItem { 81 if catalogItem.Name == TestDeleteCatalogItem { 82 entityFound = true 83 } 84 } 85 } 86 check.Assert(entityFound, Equals, false) 87 } 88 89 func (vcd *TestVCD) TestQueryCatalogItemAndVAppTemplateList(check *C) { 90 if vcd.config.VCD.Catalog.Name == "" { 91 check.Skip("no catalog provided. Skipping test") 92 } 93 if vcd.config.VCD.Vdc == "" { 94 check.Skip("no VDC provided. Skipping test") 95 } 96 // Fetching organization and catalog 97 org, err := vcd.client.GetAdminOrgByName(vcd.org.Org.Name) 98 check.Assert(err, IsNil) 99 check.Assert(org, NotNil) 100 catalog, err := org.GetCatalogByName(vcd.config.VCD.Catalog.Name, true) 101 check.Assert(err, IsNil) 102 103 // Fetching VDC 104 vdc, err := org.GetAdminVDCByName(vcd.config.VCD.Vdc, false) 105 check.Assert(err, IsNil) 106 check.Assert(vdc, NotNil) 107 108 // Get the list of catalog items using a query from catalog 109 queryCatalogItemsByCatalog, err := catalog.QueryCatalogItemList() 110 check.Assert(err, IsNil) 111 check.Assert(queryCatalogItemsByCatalog, NotNil) 112 113 // Get the list of catalog items using a query from VDC 114 queryCatalogItemsByVdc, err := vdc.QueryCatalogItemList() 115 check.Assert(err, IsNil) 116 check.Assert(queryCatalogItemsByVdc, NotNil) 117 118 // Make sure the lists have at least one item 119 hasItemsFromCatalog := len(queryCatalogItemsByCatalog) > 0 120 check.Assert(hasItemsFromCatalog, Equals, true) 121 hasItemsFromVdc := len(queryCatalogItemsByVdc) > 0 122 check.Assert(hasItemsFromVdc, Equals, true) 123 124 // Building up a map of catalog items as they are recorded in the catalog 125 var itemMapInCatalog = make(map[string]string) 126 for _, item := range catalog.Catalog.CatalogItems { 127 for _, catalogItem := range item.CatalogItem { 128 itemMapInCatalog[catalogItem.Name] = catalogItem.HREF 129 } 130 } 131 132 var itemMapInVdc = make(map[string]string) 133 for _, resource := range vdc.AdminVdc.ResourceEntities { 134 for _, item := range resource.ResourceEntity { 135 if item.Type == types.MimeVAppTemplate { 136 itemMapInVdc[item.Name] = item.HREF 137 } 138 } 139 } 140 141 // Compare the items in the query with the catalog list 142 for _, qCatalogItem := range queryCatalogItemsByCatalog { 143 itemHref, foundItem := itemMapInCatalog[qCatalogItem.Name] 144 check.Assert(foundItem, Equals, true) 145 if qCatalogItem.EntityType == types.QtVappTemplate { 146 // If the item is not "media", compare the HREF 147 check.Assert(itemHref, Equals, qCatalogItem.HREF) 148 } 149 } 150 151 // Get the list of vApp templates using a query from catalog 152 queryVappTemplatesByCatalog, err := catalog.QueryVappTemplateList() 153 check.Assert(err, IsNil) 154 check.Assert(queryVappTemplatesByCatalog, NotNil) 155 156 // Get the list of vApp templates using a query from VDC 157 queryVappTemplatesByVdc, err := vdc.QueryVappTemplateList() 158 check.Assert(err, IsNil) 159 check.Assert(queryVappTemplatesByVdc, NotNil) 160 161 // Make sure the lists have at least one item 162 hasItemsFromCatalog = len(queryVappTemplatesByCatalog) > 0 163 check.Assert(hasItemsFromCatalog, Equals, true) 164 hasItemsFromVdc = len(queryVappTemplatesByVdc) > 0 165 check.Assert(hasItemsFromVdc, Equals, true) 166 167 // Compare vApp templates with the list of catalog items 168 for _, qvAppTemplate := range queryVappTemplatesByCatalog { 169 // Check that catalog item name and vApp template names match 170 itemHref, foundItem := itemMapInCatalog[qvAppTemplate.Name] 171 check.Assert(foundItem, Equals, true) 172 173 // Retrieve the catalog item, and check the internal Entity HREF 174 // against the vApp template HREF 175 catalogItem, err := catalog.GetCatalogItemByHref(itemHref) 176 check.Assert(err, IsNil) 177 check.Assert(catalogItem, NotNil) 178 179 check.Assert(catalogItem.CatalogItem.Entity, NotNil) 180 check.Assert(catalogItem.CatalogItem.Entity.HREF, Equals, qvAppTemplate.HREF) 181 } 182 183 // Compare vApp templates from query with the list of vappTemplates from VDC 184 for _, qvAppTemplate := range queryVappTemplatesByVdc { 185 // Check that catalog item name and vApp template names match 186 itemHref, foundItem := itemMapInVdc[qvAppTemplate.Name] 187 check.Assert(foundItem, Equals, true) 188 189 // Retrieve the vApp template 190 vappTemplate, err := catalog.GetVappTemplateByHref(itemHref) 191 check.Assert(err, IsNil) 192 check.Assert(vappTemplate, NotNil) 193 } 194 195 // Compare vApp templates from query with one retrieved by name 196 vAppTemplateQueryResult, err := catalog.QueryVappTemplateWithName(queryVappTemplatesByCatalog[0].Name) 197 check.Assert(err, IsNil) 198 check.Assert(vAppTemplateQueryResult, NotNil) 199 check.Assert(vAppTemplateQueryResult, DeepEquals, queryVappTemplatesByCatalog[0]) 200 } 201 202 func (vcd *TestVCD) Test_DeleteNonEmptyCatalog(check *C) { 203 skipWhenOvaPathMissing(vcd.config.OVA.OvaPath, check) 204 205 catalogName := check.TestName() 206 catalogItemName := check.TestName() + "_item" 207 // Fetching organization 208 org, err := vcd.client.GetAdminOrgByName(vcd.org.Org.Name) 209 check.Assert(err, IsNil) 210 check.Assert(org, NotNil) 211 catalog, err := org.CreateCatalog(catalogName, catalogName) 212 check.Assert(err, IsNil) 213 AddToCleanupList(catalogName, "catalog", vcd.org.Org.Name, check.TestName()) 214 215 check.Assert(catalog, NotNil) 216 217 // add catalogItem 218 uploadTask, err := catalog.UploadOvf(vcd.config.OVA.OvaPath, catalogItemName, "upload from delete catalog item test", 1024) 219 check.Assert(err, IsNil) 220 err = uploadTask.WaitTaskCompletion() 221 check.Assert(err, IsNil) 222 AddToCleanupList(catalogItemName, "catalogItem", vcd.org.Org.Name+"|"+catalogName, check.TestName()) 223 224 retrievedCatalog, err := org.GetCatalogByName(catalogName, true) 225 check.Assert(err, IsNil) 226 catalogItem, err := retrievedCatalog.GetCatalogItemByName(catalogItemName, true) 227 check.Assert(err, IsNil) 228 check.Assert(catalogItem, NotNil) 229 230 err = retrievedCatalog.Delete(true, true) 231 check.Assert(err, IsNil) 232 233 retrievedCatalog, err = org.GetCatalogByName(catalogName, true) 234 check.Assert(err, NotNil) 235 check.Assert(retrievedCatalog, IsNil) 236 } 237 238 func (vcd *TestVCD) Test_QueryVappTemplateList(check *C) { 239 fmt.Printf("Running: %s\n", check.TestName()) 240 241 catalogName := vcd.config.VCD.Catalog.Name 242 if catalogName == "" { 243 check.Skip("Test_QueryVappTemplateList: Catalog name not given") 244 return 245 } 246 247 cat, err := vcd.org.GetCatalogByName(catalogName, false) 248 if err != nil { 249 check.Skip("Test_QueryVappTemplateList: Catalog not found") 250 return 251 } 252 253 vAppTemplates, err := cat.QueryVappTemplateList() 254 check.Assert(err, IsNil) 255 check.Assert(vAppTemplates, NotNil) 256 257 // Check the number of vApp templates is one 258 // Dump all vApp template structures to easily identify leftover objects if number is not 1 259 if len(vAppTemplates) > 1 { 260 spew.Dump(vAppTemplates) 261 } 262 check.Assert(len(vAppTemplates), Equals, 1) 263 264 // Check the name of the vApp template is what it should be 265 check.Assert(vAppTemplates[0].Name, Equals, vcd.config.VCD.Catalog.CatalogItem) 266 267 // Check the vApp Template retrieved before is the same as the one retrieved by name 268 vAppTemplate, err := cat.QueryVappTemplateWithName(vAppTemplates[0].Name) 269 check.Assert(err, IsNil) 270 check.Assert(vAppTemplates, NotNil) 271 check.Assert(vAppTemplate, DeepEquals, vAppTemplates[0]) 272 }