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

     1  //go:build catalog || functional || 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  	"io"
    12  	"log"
    13  	"os"
    14  	"strings"
    15  	"time"
    16  
    17  	"github.com/davecgh/go-spew/spew"
    18  	"github.com/vmware/go-vcloud-director/v2/types/v56"
    19  	"github.com/vmware/go-vcloud-director/v2/util"
    20  	. "gopkg.in/check.v1"
    21  )
    22  
    23  // Tests catalog refresh
    24  func (vcd *TestVCD) Test_CatalogRefresh(check *C) {
    25  	fmt.Printf("Running: %s\n", check.TestName())
    26  
    27  	catalogName := vcd.config.VCD.Catalog.Name
    28  	if catalogName == "" {
    29  		check.Skip("Test_CatalogRefresh: Catalog name not given")
    30  		return
    31  	}
    32  	cat, err := vcd.org.GetCatalogByName(catalogName, false)
    33  	if err != nil {
    34  		check.Skip("Test_CatalogRefresh: Catalog not found")
    35  		return
    36  	}
    37  	catalogId := cat.Catalog.ID
    38  	numItems := len(cat.Catalog.CatalogItems)
    39  	dateCreated := cat.Catalog.DateCreated
    40  	check.Assert(cat, NotNil)
    41  	check.Assert(cat.Catalog.Name, Equals, catalogName)
    42  
    43  	// Pollute the catalog structure
    44  	cat.Catalog.Name = INVALID_NAME
    45  	cat.Catalog.ID = invalidEntityId
    46  	cat.Catalog.CatalogItems = nil
    47  	cat.Catalog.DateCreated = ""
    48  
    49  	// Get the catalog again from vCD
    50  	err = cat.Refresh()
    51  	check.Assert(err, IsNil)
    52  	check.Assert(cat, NotNil)
    53  	check.Assert(cat.Catalog.Name, Equals, catalogName)
    54  	check.Assert(cat.Catalog.DateCreated, Equals, dateCreated)
    55  	check.Assert(len(cat.Catalog.CatalogItems), Equals, numItems)
    56  	check.Assert(cat.Catalog.ID, Equals, catalogId)
    57  }
    58  
    59  func (vcd *TestVCD) Test_FindCatalogItem(check *C) {
    60  	fmt.Printf("Running: %s\n", check.TestName())
    61  
    62  	// Fetch Catalog
    63  	cat, err := vcd.org.GetCatalogByName(vcd.config.VCD.Catalog.Name, false)
    64  	if err != nil {
    65  		check.Skip("Test_FindCatalogItem: Catalog not found. Test can't proceed")
    66  		return
    67  	}
    68  
    69  	// Find Catalog Item
    70  	if vcd.config.VCD.Catalog.CatalogItem == "" {
    71  		check.Skip("Test_FindCatalogItem: Catalog Item not given. Test can't proceed")
    72  	}
    73  	catalogItem, err := cat.GetCatalogItemByName(vcd.config.VCD.Catalog.CatalogItem, false)
    74  	check.Assert(err, IsNil)
    75  	check.Assert(catalogItem.CatalogItem.Name, Equals, vcd.config.VCD.Catalog.CatalogItem)
    76  	// If given a description in config file then it checks if the descriptions match
    77  	// Otherwise it skips the assert
    78  	if vcd.config.VCD.Catalog.CatalogItemDescription != "" {
    79  		check.Assert(catalogItem.CatalogItem.Description, Equals, vcd.config.VCD.Catalog.CatalogItemDescription)
    80  	}
    81  	// Test non-existent catalog item
    82  	catalogItem, err = cat.GetCatalogItemByName("INVALID", false)
    83  	check.Assert(err, NotNil)
    84  	check.Assert(catalogItem, IsNil)
    85  }
    86  
    87  func (vcd *TestVCD) Test_FindVAppTemplate(check *C) {
    88  	fmt.Printf("Running: %s\n", check.TestName())
    89  
    90  	// Prepare test
    91  	cat, err := vcd.org.GetCatalogByName(vcd.config.VCD.Catalog.Name, false)
    92  	if err != nil {
    93  		check.Skip(fmt.Sprintf("%s: Catalog not found. Test can't proceed", check.TestName()))
    94  		return
    95  	}
    96  	if vcd.config.VCD.Catalog.CatalogItem == "" {
    97  		check.Skip(fmt.Sprintf("%s: Catalog Item not given. Test can't proceed", check.TestName()))
    98  	}
    99  
   100  	// Test cases
   101  	vAppTemplate, err := cat.GetVAppTemplateByName(vcd.config.VCD.Catalog.CatalogItem)
   102  	check.Assert(err, IsNil)
   103  	check.Assert(vAppTemplate.VAppTemplate.Name, Equals, vcd.config.VCD.Catalog.CatalogItem)
   104  	if vcd.config.VCD.Catalog.CatalogItemDescription != "" {
   105  		check.Assert(vAppTemplate.VAppTemplate.Description, Equals, vcd.config.VCD.Catalog.CatalogItemDescription)
   106  	}
   107  
   108  	vAppTemplate, err = cat.GetVAppTemplateById(vAppTemplate.VAppTemplate.ID)
   109  	check.Assert(err, IsNil)
   110  	check.Assert(vAppTemplate.VAppTemplate.Name, Equals, vcd.config.VCD.Catalog.CatalogItem)
   111  	if vcd.config.VCD.Catalog.CatalogItemDescription != "" {
   112  		check.Assert(vAppTemplate.VAppTemplate.Description, Equals, vcd.config.VCD.Catalog.CatalogItemDescription)
   113  	}
   114  
   115  	vAppTemplate, err = cat.GetVAppTemplateByNameOrId(vAppTemplate.VAppTemplate.ID, false)
   116  	check.Assert(err, IsNil)
   117  	check.Assert(vAppTemplate.VAppTemplate.Name, Equals, vcd.config.VCD.Catalog.CatalogItem)
   118  	if vcd.config.VCD.Catalog.CatalogItemDescription != "" {
   119  		check.Assert(vAppTemplate.VAppTemplate.Description, Equals, vcd.config.VCD.Catalog.CatalogItemDescription)
   120  	}
   121  
   122  	vAppTemplate, err = cat.GetVAppTemplateByNameOrId(vcd.config.VCD.Catalog.CatalogItem, false)
   123  	check.Assert(err, IsNil)
   124  	check.Assert(vAppTemplate.VAppTemplate.Name, Equals, vcd.config.VCD.Catalog.CatalogItem)
   125  	if vcd.config.VCD.Catalog.CatalogItemDescription != "" {
   126  		check.Assert(vAppTemplate.VAppTemplate.Description, Equals, vcd.config.VCD.Catalog.CatalogItemDescription)
   127  	}
   128  
   129  	// Test non-existent vApp Template
   130  	vAppTemplate, err = cat.GetVAppTemplateByName("INVALID")
   131  	check.Assert(err, NotNil)
   132  	check.Assert(vAppTemplate, IsNil)
   133  }
   134  
   135  // Creates a Catalog, updates the description, and checks the changes against the
   136  // newly updated catalog. Then deletes the catalog
   137  func (vcd *TestVCD) Test_UpdateCatalog(check *C) {
   138  	fmt.Printf("Running: %s\n", check.TestName())
   139  
   140  	org, err := vcd.client.GetAdminOrgByName(vcd.config.VCD.Org)
   141  	check.Assert(err, IsNil)
   142  	check.Assert(org, NotNil)
   143  	catalog, _ := org.GetAdminCatalogByName(TestUpdateCatalog, false)
   144  	if catalog != nil {
   145  		err = catalog.Delete(true, true)
   146  		check.Assert(err, IsNil)
   147  	}
   148  	adminCatalog, err := org.CreateCatalog(TestUpdateCatalog, TestUpdateCatalog)
   149  	check.Assert(err, IsNil)
   150  	// After a successful creation, the entity is added to the cleanup list.
   151  	// If something fails after this point, the entity will be removed
   152  	AddToCleanupList(TestUpdateCatalog, "catalog", vcd.config.VCD.Org, "Test_UpdateCatalog")
   153  	check.Assert(adminCatalog.AdminCatalog.Name, Equals, TestUpdateCatalog)
   154  
   155  	adminCatalog.AdminCatalog.Description = TestCreateCatalogDesc
   156  	err = adminCatalog.Update()
   157  	check.Assert(err, IsNil)
   158  	check.Assert(adminCatalog.AdminCatalog.Description, Equals, TestCreateCatalogDesc)
   159  
   160  	err = adminCatalog.Delete(true, true)
   161  	check.Assert(err, IsNil)
   162  }
   163  
   164  // Creates a Catalog, and then deletes the catalog, and checks if
   165  // the catalog still exists. If it does the assertion fails.
   166  func (vcd *TestVCD) Test_DeleteCatalog(check *C) {
   167  	fmt.Printf("Running: %s\n", check.TestName())
   168  
   169  	org, err := vcd.client.GetAdminOrgByName(vcd.config.VCD.Org)
   170  	check.Assert(err, IsNil)
   171  	check.Assert(org, NotNil)
   172  	oldCatalog, _ := org.GetAdminCatalogByName(TestDeleteCatalog, false)
   173  	if oldCatalog != nil {
   174  		err = oldCatalog.Delete(true, true)
   175  		check.Assert(err, IsNil)
   176  	}
   177  	adminCatalog, err := org.CreateCatalog(TestDeleteCatalog, TestDeleteCatalog)
   178  	check.Assert(err, IsNil)
   179  	// After a successful creation, the entity is added to the cleanup list.
   180  	// If something fails after this point, the entity will be removed
   181  	AddToCleanupList(TestDeleteCatalog, "catalog", vcd.config.VCD.Org, check.TestName())
   182  	check.Assert(adminCatalog.AdminCatalog.Name, Equals, TestDeleteCatalog)
   183  
   184  	checkUploadOvf(vcd, check, vcd.config.OVA.OvaPath, TestDeleteCatalog, TestUploadOvf+"_"+check.TestName(), false)
   185  	err = adminCatalog.Delete(false, false)
   186  	check.Assert(err, NotNil)
   187  	// Catalog is not empty. An attempt to delete without recursion will fail
   188  	check.Assert(strings.Contains(err.Error(), "You must remove"), Equals, true)
   189  
   190  	err = adminCatalog.Delete(true, true)
   191  	check.Assert(err, IsNil)
   192  	doesCatalogExist(check, org)
   193  }
   194  
   195  func doesCatalogExist(check *C, org *AdminOrg) {
   196  	var err error
   197  	var catalog *AdminCatalog
   198  	for i := 0; i < 30; i++ {
   199  		catalog, err = org.GetAdminCatalogByName(TestDeleteCatalog, true)
   200  		if catalog == nil {
   201  			break
   202  		} else {
   203  			time.Sleep(time.Second)
   204  		}
   205  	}
   206  	check.Assert(err, NotNil)
   207  }
   208  
   209  // Creates a Catalog, uploads a vApp template to it, renames it, retrieves it
   210  // using the updated name and checks if it has the same vApp template.
   211  // If it doesn't the assertion fails.
   212  func (vcd *TestVCD) Test_RenameCatalog(check *C) {
   213  	fmt.Printf("Running: %s\n", check.TestName())
   214  	testRenameCatalog := check.TestName()
   215  	testUploadOvf := check.TestName() + "_ovf"
   216  	testUploadMedia := check.TestName() + "_media"
   217  
   218  	org, err := vcd.client.GetAdminOrgByName(vcd.config.VCD.Org)
   219  	check.Assert(err, IsNil)
   220  	check.Assert(org, NotNil)
   221  
   222  	adminCatalog, err := org.CreateCatalog(testRenameCatalog, testRenameCatalog)
   223  	check.Assert(err, IsNil)
   224  	check.Assert(adminCatalog, NotNil)
   225  	AddToCleanupList(testRenameCatalog, "catalog", vcd.config.VCD.Org, check.TestName())
   226  
   227  	catalog, err := vcd.client.Client.GetCatalogByName(vcd.config.VCD.Org, testRenameCatalog)
   228  	check.Assert(err, IsNil)
   229  	check.Assert(catalog, NotNil)
   230  
   231  	uploadTask, err := adminCatalog.UploadOvf(vcd.config.OVA.OvaPath, testUploadOvf, testUploadOvf, 1024)
   232  	check.Assert(err, IsNil)
   233  	err = uploadTask.WaitTaskCompletion()
   234  	check.Assert(err, IsNil)
   235  
   236  	uploadTask, err = catalog.UploadMediaImage(testUploadMedia, testUploadMedia, vcd.config.Media.MediaPath, 1024)
   237  	check.Assert(err, IsNil)
   238  	err = uploadTask.WaitTaskCompletion()
   239  	check.Assert(err, IsNil)
   240  
   241  	vAppTemplate1, err := catalog.GetVAppTemplateByName(testUploadOvf)
   242  	check.Assert(err, IsNil)
   243  	check.Assert(vAppTemplate1, NotNil)
   244  
   245  	mediaImage1, err := catalog.GetMediaByName(testUploadMedia, true)
   246  	check.Assert(err, IsNil)
   247  	check.Assert(mediaImage1, NotNil)
   248  
   249  	adminCatalog.AdminCatalog.Name = testRenameCatalog + "_updated"
   250  	err = adminCatalog.Update()
   251  	check.Assert(err, IsNil)
   252  	AddToCleanupList(testRenameCatalog+"_updated", "catalog", vcd.config.VCD.Org, check.TestName())
   253  
   254  	// Get a Catalog using the previously updated name
   255  	updatedCatalog, err := vcd.client.Client.GetCatalogByName(vcd.config.VCD.Org, testRenameCatalog+"_updated")
   256  	check.Assert(err, IsNil)
   257  	check.Assert(updatedCatalog, NotNil)
   258  
   259  	vAppTemplate2, err := updatedCatalog.GetVAppTemplateByName(testUploadOvf)
   260  	check.Assert(err, IsNil)
   261  	check.Assert(vAppTemplate2, NotNil)
   262  
   263  	mediaImage2, err := updatedCatalog.GetMediaByName(testUploadMedia, false)
   264  	check.Assert(err, IsNil)
   265  	check.Assert(mediaImage2, NotNil)
   266  
   267  	// Check the HREFs of the vApp templates and media images that were retrieved from
   268  	// catalog and updatedCatalog
   269  	check.Assert(vAppTemplate1.VAppTemplate.HREF, Equals, vAppTemplate2.VAppTemplate.HREF)
   270  	check.Assert(mediaImage1.Media.HREF, Equals, mediaImage2.Media.HREF)
   271  
   272  	err = updatedCatalog.Delete(true, true)
   273  	check.Assert(err, IsNil)
   274  }
   275  
   276  // Tests System function UploadOvf by creating catalog and
   277  // checking if provided standard ova file uploaded.
   278  func (vcd *TestVCD) Test_UploadOvf(check *C) {
   279  	fmt.Printf("Running: %s\n", check.TestName())
   280  
   281  	skipWhenOvaPathMissing(vcd.config.OVA.OvaPath, check)
   282  	checkUploadOvf(vcd, check, vcd.config.OVA.OvaPath, vcd.config.VCD.Catalog.Name, TestUploadOvf, true)
   283  }
   284  
   285  // Tests System function UploadOvf by creating catalog and
   286  // checking if provided chunked ova file uploaded.
   287  func (vcd *TestVCD) Test_UploadOvf_chunked(check *C) {
   288  	fmt.Printf("Running: %s\n", check.TestName())
   289  
   290  	skipWhenOvaPathMissing(vcd.config.OVA.OvaChunkedPath, check)
   291  	checkUploadOvf(vcd, check, vcd.config.OVA.OvaChunkedPath, vcd.config.VCD.Catalog.Name, TestUploadOvf+"2", true)
   292  }
   293  
   294  // Tests System function UploadOvf by creating catalog and
   295  // checking UploadTask.GetUploadProgress returns values of progress.
   296  func (vcd *TestVCD) Test_UploadOvf_progress_works(check *C) {
   297  	fmt.Printf("Running: %s\n", check.TestName())
   298  
   299  	skipWhenOvaPathMissing(vcd.config.OVA.OvaPath, check)
   300  	itemName := TestUploadOvf + "3"
   301  
   302  	catalog, org := findCatalog(vcd, check, vcd.config.VCD.Catalog.Name)
   303  
   304  	uploadTask, err := catalog.UploadOvf(vcd.config.OVA.OvaPath, itemName, "upload from test", 1024)
   305  	check.Assert(err, IsNil)
   306  	for {
   307  		if value := uploadTask.GetUploadProgress(); value == "100.00" {
   308  			break
   309  		} else {
   310  			check.Assert(value, Not(Equals), "")
   311  		}
   312  	}
   313  	err = uploadTask.WaitTaskCompletion()
   314  	check.Assert(err, IsNil)
   315  
   316  	AddToCleanupList(itemName, "catalogItem", vcd.org.Org.Name+"|"+vcd.config.VCD.Catalog.Name, "Test_UploadOvf")
   317  
   318  	catalog, err = org.GetCatalogByName(vcd.config.VCD.Catalog.Name, true)
   319  	check.Assert(err, IsNil)
   320  	verifyCatalogItemUploaded(check, catalog, itemName)
   321  
   322  	// Delete testing catalog item
   323  	deleteCatalogItem(check, catalog, itemName)
   324  }
   325  
   326  // Tests System function UploadOvf by creating catalog and
   327  // checking UploadTask.ShowUploadProgress writes values of progress to stdin.
   328  func (vcd *TestVCD) Test_UploadOvf_ShowUploadProgress_works(check *C) {
   329  	fmt.Printf("Running: %s\n", check.TestName())
   330  
   331  	skipWhenOvaPathMissing(vcd.config.OVA.OvaPath, check)
   332  	itemName := TestUploadOvf + "4"
   333  
   334  	catalog, org := findCatalog(vcd, check, vcd.config.VCD.Catalog.Name)
   335  
   336  	//execute
   337  	uploadTask, err := catalog.UploadOvf(vcd.config.OVA.OvaPath, itemName, "upload from test", 1024)
   338  	check.Assert(err, IsNil)
   339  
   340  	//take control of stdout
   341  	oldStdout := os.Stdout
   342  	r, w, _ := os.Pipe()
   343  	os.Stdout = w
   344  
   345  	err = uploadTask.ShowUploadProgress()
   346  	check.Assert(err, IsNil)
   347  	err = w.Close()
   348  	check.Assert(err, IsNil)
   349  
   350  	//read stdin
   351  	result, _ := io.ReadAll(r)
   352  	os.Stdout = oldStdout
   353  
   354  	err = uploadTask.WaitTaskCompletion()
   355  	check.Assert(err, IsNil)
   356  	AddToCleanupList(itemName, "catalogItem", vcd.org.Org.Name+"|"+vcd.config.VCD.Catalog.Name, "Test_UploadOvf")
   357  
   358  	check.Assert(string(result), Matches, "(?s).*Upload progress 100.00%.*")
   359  
   360  	catalog, err = org.GetCatalogByName(vcd.config.VCD.Catalog.Name, true)
   361  	check.Assert(err, IsNil)
   362  	check.Assert(catalog, NotNil)
   363  	verifyCatalogItemUploaded(check, catalog, itemName)
   364  
   365  	// Delete testing catalog item
   366  	deleteCatalogItem(check, catalog, itemName)
   367  }
   368  
   369  // Tests System function UploadOvf by creating catalog, creating catalog item
   370  // and expecting specific error then trying to create same catalog item. As vCD returns cryptic error for such case.
   371  func (vcd *TestVCD) Test_UploadOvf_error_withSameItem(check *C) {
   372  	fmt.Printf("Running: %s\n", check.TestName())
   373  
   374  	skipWhenOvaPathMissing(vcd.config.OVA.OvaPath, check)
   375  
   376  	itemName := TestUploadOvf + "5"
   377  
   378  	catalog, _ := findCatalog(vcd, check, vcd.config.VCD.Catalog.Name)
   379  
   380  	//add item
   381  	uploadTask, err2 := catalog.UploadOvf(vcd.config.OVA.OvaPath, itemName, "upload from test", 1024)
   382  	check.Assert(err2, IsNil)
   383  	err2 = uploadTask.WaitTaskCompletion()
   384  	check.Assert(err2, IsNil)
   385  
   386  	AddToCleanupList(itemName, "catalogItem", vcd.org.Org.Name+"|"+vcd.config.VCD.Catalog.Name, "Test_UploadOvf")
   387  
   388  	catalog, _ = findCatalog(vcd, check, vcd.config.VCD.Catalog.Name)
   389  	_, err3 := catalog.UploadOvf(vcd.config.OVA.OvaPath, itemName, "upload from test", 1024)
   390  	check.Assert(err3.Error(), Matches, ".*already exists. Upload with different name.*")
   391  
   392  	// Delete testing catalog item
   393  	deleteCatalogItem(check, catalog, itemName)
   394  }
   395  
   396  // Tests System function UploadOvf by creating catalog, uploading file and verifying
   397  // that extracted files were deleted.s
   398  func (vcd *TestVCD) Test_UploadOvf_cleaned_extracted_files(check *C) {
   399  	fmt.Printf("Running: %s\n", check.TestName())
   400  
   401  	skipWhenOvaPathMissing(vcd.config.OVA.OvaPath, check)
   402  
   403  	itemName := TestUploadOvf + "6"
   404  
   405  	//check existing count of folders
   406  	oldFolderCount := countFolders()
   407  
   408  	catalog, _ := findCatalog(vcd, check, vcd.config.VCD.Catalog.Name)
   409  
   410  	uploadTask, err := catalog.UploadOvf(vcd.config.OVA.OvaPath, itemName, "upload from test", 1024)
   411  	check.Assert(err, IsNil)
   412  	err = uploadTask.WaitTaskCompletion()
   413  	check.Assert(err, IsNil)
   414  
   415  	AddToCleanupList(itemName, "catalogItem", vcd.org.Org.Name+"|"+vcd.config.VCD.Catalog.Name, "Test_UploadOvf")
   416  
   417  	check.Assert(oldFolderCount, Equals, countFolders())
   418  
   419  	// Delete testing catalog item
   420  	deleteCatalogItem(check, catalog, itemName)
   421  }
   422  
   423  // Tests System function UploadOvf by creating catalog and
   424  // checking if provided standard ovf file uploaded.
   425  func (vcd *TestVCD) Test_UploadOvfFile(check *C) {
   426  	fmt.Printf("Running: %s\n", check.TestName())
   427  
   428  	skipWhenOvaPathMissing(vcd.config.OVA.OvfPath, check)
   429  	checkUploadOvf(vcd, check, vcd.config.OVA.OvfPath, vcd.config.VCD.Catalog.Name, TestUploadOvf+"7", true)
   430  }
   431  
   432  // Tests System function UploadOvf by creating catalog and
   433  // checking that ova file without vmdk size specified can be uploaded.
   434  func (vcd *TestVCD) Test_UploadOvf_withoutVMDKSize(check *C) {
   435  	fmt.Printf("Running: %s\n", check.TestName())
   436  
   437  	skipWhenOvaPathMissing(vcd.config.OVA.OvaWithoutSizePath, check)
   438  	checkUploadOvf(vcd, check, vcd.config.OVA.OvaWithoutSizePath, vcd.config.VCD.Catalog.Name, TestUploadOvf+"8", true)
   439  }
   440  
   441  func countFolders() int {
   442  	files, err := os.ReadDir(os.TempDir())
   443  	if err != nil {
   444  		log.Fatal(err)
   445  	}
   446  	count := 0
   447  	for _, f := range files {
   448  		if strings.Contains(f.Name(), util.TmpDirPrefix+".*") {
   449  			count++
   450  		}
   451  	}
   452  	return count
   453  }
   454  
   455  func checkUploadOvf(vcd *TestVCD, check *C, ovaFileName, catalogName, itemName string, deleteItemAtTheEnd bool) {
   456  	catalog, org := findCatalog(vcd, check, catalogName)
   457  
   458  	uploadTask, err := catalog.UploadOvf(ovaFileName, itemName, "upload from test", 1024)
   459  	check.Assert(err, IsNil)
   460  	err = uploadTask.WaitTaskCompletion()
   461  	check.Assert(err, IsNil)
   462  
   463  	AddToCleanupList(itemName, "catalogItem", vcd.org.Org.Name+"|"+catalogName, "checkUploadOvf")
   464  
   465  	catalog, err = org.GetCatalogByName(catalogName, false)
   466  	check.Assert(err, IsNil)
   467  	verifyCatalogItemUploaded(check, catalog, itemName)
   468  
   469  	// Delete testing catalog item
   470  	if deleteItemAtTheEnd {
   471  		deleteCatalogItem(check, catalog, itemName)
   472  	}
   473  }
   474  
   475  func verifyCatalogItemUploaded(check *C, catalog *Catalog, itemName string) {
   476  	entityFound := false
   477  	for _, catalogItems := range catalog.Catalog.CatalogItems {
   478  		for _, catalogItem := range catalogItems.CatalogItem {
   479  			if catalogItem.Name == itemName {
   480  				entityFound = true
   481  			}
   482  		}
   483  	}
   484  	check.Assert(entityFound, Equals, true)
   485  }
   486  
   487  func findCatalog(vcd *TestVCD, check *C, catalogName string) (*Catalog, *AdminOrg) {
   488  	org := getOrg(vcd, check)
   489  	catalog, err := org.GetCatalogByName(catalogName, false)
   490  	check.Assert(err, IsNil)
   491  	return catalog, org
   492  }
   493  
   494  func getOrg(vcd *TestVCD, check *C) *AdminOrg {
   495  	// Fetching organization
   496  	org, err := vcd.client.GetAdminOrgByName(vcd.org.Org.Name)
   497  	check.Assert(err, IsNil)
   498  	check.Assert(org, NotNil)
   499  	return org
   500  }
   501  
   502  func skipWhenOvaPathMissing(ovaPath string, check *C) {
   503  	if ovaPath == "" {
   504  		check.Skip("Skipping test because no OVA/OVF path given")
   505  	}
   506  }
   507  
   508  func deleteCatalogItem(check *C, catalog *Catalog, itemName string) {
   509  	catalogItem, err := catalog.GetCatalogItemByName(itemName, true)
   510  	check.Assert(err, IsNil)
   511  	check.Assert(catalogItem, NotNil)
   512  
   513  	err = catalogItem.Delete()
   514  	check.Assert(err, IsNil)
   515  }
   516  
   517  // Tests System function UploadMediaImage by checking if provided standard iso file uploaded.
   518  func (vcd *TestVCD) Test_CatalogUploadMediaImage(check *C) {
   519  	fmt.Printf("Running: %s\n", check.TestName())
   520  
   521  	skipWhenMediaPathMissing(vcd, check)
   522  
   523  	catalog, org := findCatalog(vcd, check, vcd.config.VCD.Catalog.Name)
   524  
   525  	uploadTask, err := catalog.UploadMediaImage(TestCatalogUploadMedia, "upload from test", vcd.config.Media.MediaPath, 1024)
   526  	check.Assert(err, IsNil)
   527  	err = uploadTask.WaitTaskCompletion()
   528  	check.Assert(err, IsNil)
   529  
   530  	AddToCleanupList(TestCatalogUploadMedia, "mediaCatalogImage", vcd.org.Org.Name+"|"+vcd.config.VCD.Catalog.Name, "Test_CatalogUploadMediaImage")
   531  
   532  	//verifyMediaImageUploaded(vcd.vdc.client, check, TestUploadMedia)
   533  	catalog, err = org.GetCatalogByName(vcd.config.VCD.Catalog.Name, false)
   534  	check.Assert(err, IsNil)
   535  	check.Assert(catalog, NotNil)
   536  	verifyCatalogItemUploaded(check, catalog, TestCatalogUploadMedia)
   537  
   538  	// Delete testing catalog item
   539  	deleteCatalogItem(check, catalog, TestCatalogUploadMedia)
   540  }
   541  
   542  // Tests System function UploadMediaImage by checking UploadTask.GetUploadProgress returns values of progress.
   543  func (vcd *TestVCD) Test_CatalogUploadMediaImage_progress_works(check *C) {
   544  	fmt.Printf("Running: %s\n", check.TestName())
   545  
   546  	skipWhenMediaPathMissing(vcd, check)
   547  	itemName := TestCatalogUploadMedia + "2"
   548  
   549  	catalog, org := findCatalog(vcd, check, vcd.config.VCD.Catalog.Name)
   550  
   551  	uploadTask, err := catalog.UploadMediaImage(itemName, "upload from test", vcd.config.Media.MediaPath, 1024)
   552  	check.Assert(err, IsNil)
   553  	for {
   554  		if value := uploadTask.GetUploadProgress(); value == "100.00" {
   555  			break
   556  		} else {
   557  			check.Assert(value, Not(Equals), "")
   558  		}
   559  	}
   560  	err = uploadTask.WaitTaskCompletion()
   561  	check.Assert(err, IsNil)
   562  
   563  	AddToCleanupList(itemName, "mediaCatalogImage", vcd.org.Org.Name+"|"+vcd.config.VCD.Catalog.Name, "Test_CatalogUploadMediaImage_progress_works")
   564  
   565  	catalog, err = org.GetCatalogByName(vcd.config.VCD.Catalog.Name, false)
   566  	check.Assert(err, IsNil)
   567  	check.Assert(catalog, NotNil)
   568  	verifyCatalogItemUploaded(check, catalog, itemName)
   569  
   570  	// Delete testing catalog item
   571  	deleteCatalogItem(check, catalog, itemName)
   572  }
   573  
   574  // Tests System function UploadMediaImage by checking UploadTask.ShowUploadProgress writes values of progress to stdin.
   575  func (vcd *TestVCD) Test_CatalogUploadMediaImage_ShowUploadProgress_works(check *C) {
   576  	fmt.Printf("Running: %s\n", check.TestName())
   577  
   578  	skipWhenMediaPathMissing(vcd, check)
   579  	itemName := TestCatalogUploadMedia + "3"
   580  
   581  	catalog, org := findCatalog(vcd, check, vcd.config.VCD.Catalog.Name)
   582  
   583  	uploadTask, err := catalog.UploadMediaImage(itemName, "upload from test", vcd.config.Media.MediaPath, 1024)
   584  	check.Assert(err, IsNil)
   585  
   586  	//take control of stdout
   587  	oldStdout := os.Stdout
   588  	r, w, _ := os.Pipe()
   589  	os.Stdout = w
   590  
   591  	err = uploadTask.ShowUploadProgress()
   592  	check.Assert(err, IsNil)
   593  	err = w.Close()
   594  	check.Assert(err, IsNil)
   595  	//read stdin
   596  	result, _ := io.ReadAll(r)
   597  	os.Stdout = oldStdout
   598  
   599  	err = uploadTask.WaitTaskCompletion()
   600  	check.Assert(err, IsNil)
   601  
   602  	AddToCleanupList(itemName, "mediaCatalogImage", vcd.org.Org.Name+"|"+vcd.config.VCD.Catalog.Name, "Test_CatalogUploadMediaImage_ShowUploadProgress_works")
   603  
   604  	check.Assert(string(result), Matches, "(?s).*Upload progress 100.00%.*")
   605  	catalog, err = org.GetCatalogByName(vcd.config.VCD.Catalog.Name, false)
   606  	check.Assert(err, IsNil)
   607  	check.Assert(catalog, NotNil)
   608  	verifyCatalogItemUploaded(check, catalog, itemName)
   609  
   610  	// Delete testing catalog item
   611  	deleteCatalogItem(check, catalog, itemName)
   612  }
   613  
   614  // Tests System function UploadMediaImage by creating media item and expecting specific error
   615  // then trying to create same media item. As vCD returns cryptic error for such case.
   616  func (vcd *TestVCD) Test_CatalogUploadMediaImage_error_withSameItem(check *C) {
   617  	fmt.Printf("Running: %s\n", check.TestName())
   618  
   619  	skipWhenMediaPathMissing(vcd, check)
   620  	itemName := TestCatalogUploadMedia + "4"
   621  
   622  	catalog, _ := findCatalog(vcd, check, vcd.config.VCD.Catalog.Name)
   623  
   624  	uploadTask, err := catalog.UploadMediaImage(itemName, "upload from test", vcd.config.Media.MediaPath, 1024)
   625  	check.Assert(err, IsNil)
   626  	err = uploadTask.WaitTaskCompletion()
   627  	check.Assert(err, IsNil)
   628  
   629  	AddToCleanupList(itemName, "mediaCatalogImage", vcd.org.Org.Name+"|"+vcd.config.VCD.Catalog.Name, "Test_CatalogUploadMediaImage_error_withSameItem")
   630  
   631  	// Delete testing catalog item
   632  	deleteCatalogItem(check, catalog, itemName)
   633  }
   634  
   635  // Tests System function Delete by creating media item and
   636  // deleting it after.
   637  func (vcd *TestVCD) Test_CatalogDeleteMediaRecord(check *C) {
   638  	fmt.Printf("Running: %s\n", check.TestName())
   639  
   640  	skipWhenMediaPathMissing(vcd, check)
   641  	itemName := TestCatalogUploadMedia + "5"
   642  
   643  	catalog, org := findCatalog(vcd, check, vcd.config.VCD.Catalog.Name)
   644  
   645  	uploadTask, err := catalog.UploadMediaImage(itemName, "upload from test", vcd.config.Media.MediaPath, 1024)
   646  	check.Assert(err, IsNil)
   647  	err = uploadTask.WaitTaskCompletion()
   648  	check.Assert(err, IsNil)
   649  
   650  	AddToCleanupList(itemName, "mediaCatalogImage", vcd.org.Org.Name+"|"+vcd.config.VCD.Catalog.Name, "Test_CatalogDeleteMediaImage")
   651  
   652  	mediaRecord, err := catalog.QueryMedia(itemName)
   653  	check.Assert(err, IsNil)
   654  	check.Assert(mediaRecord.MediaRecord.Name, Equals, itemName)
   655  
   656  	task, err := mediaRecord.Delete()
   657  	check.Assert(err, IsNil)
   658  	err = task.WaitTaskCompletion()
   659  	check.Assert(err, IsNil)
   660  
   661  	mediaRecord, err = catalog.QueryMedia(itemName)
   662  	check.Assert(IsNotFound(err), Equals, true)
   663  	check.Assert(mediaRecord, IsNil)
   664  
   665  	//addition check
   666  	// check through existing catalogItems
   667  	catalog, err = org.GetCatalogByName(vcd.config.VCD.Catalog.Name, false)
   668  	check.Assert(err, IsNil)
   669  	check.Assert(catalog, NotNil)
   670  	entityFound := false
   671  	for _, catalogItems := range catalog.Catalog.CatalogItems {
   672  		for _, catalogItem := range catalogItems.CatalogItem {
   673  			if catalogItem.Name == TestDeleteCatalogItem {
   674  				entityFound = true
   675  			}
   676  		}
   677  	}
   678  	check.Assert(entityFound, Equals, false)
   679  }
   680  
   681  func init() {
   682  	testingTags["catalog"] = "catalog_test.go"
   683  }
   684  
   685  // Tests CatalogItem retrieval by name, by ID, and by a combination of name and ID
   686  func (vcd *TestVCD) Test_CatalogGetItem(check *C) {
   687  	fmt.Printf("Running: %s\n", check.TestName())
   688  
   689  	if vcd.config.VCD.Org == "" {
   690  		check.Skip("Test_CatalogGetItem: Org name not given")
   691  		return
   692  	}
   693  	if vcd.config.VCD.Catalog.Name == "" {
   694  		check.Skip("Test_CatalogGetItem: Catalog name not given")
   695  		return
   696  	}
   697  	org, err := vcd.client.GetOrgByName(vcd.config.VCD.Org)
   698  	check.Assert(err, IsNil)
   699  	check.Assert(org, NotNil)
   700  
   701  	catalog, err := org.GetCatalogByName(vcd.config.VCD.Catalog.Name, false)
   702  	check.Assert(err, IsNil)
   703  	check.Assert(catalog, NotNil)
   704  
   705  	getByName := func(name string, refresh bool) (genericEntity, error) {
   706  		return catalog.GetCatalogItemByName(name, refresh)
   707  	}
   708  	getById := func(id string, refresh bool) (genericEntity, error) { return catalog.GetCatalogItemById(id, refresh) }
   709  	getByNameOrId := func(id string, refresh bool) (genericEntity, error) {
   710  		return catalog.GetCatalogItemByNameOrId(id, refresh)
   711  	}
   712  
   713  	var def = getterTestDefinition{
   714  		parentType:    "Catalog",
   715  		parentName:    vcd.config.VCD.Catalog.Name,
   716  		entityType:    "CatalogItem",
   717  		entityName:    vcd.config.VCD.Catalog.CatalogItem,
   718  		getByName:     getByName,
   719  		getById:       getById,
   720  		getByNameOrId: getByNameOrId,
   721  	}
   722  	vcd.testFinderGetGenericEntity(def, check)
   723  }
   724  
   725  // TestGetVappTemplateByHref tests that we can find a vApp template using
   726  // the HREF from the Entity section of a known Catalog Item
   727  func (vcd *TestVCD) TestGetVappTemplateByHref(check *C) {
   728  
   729  	fmt.Printf("Running: %s\n", check.TestName())
   730  	if vcd.config.VCD.Org == "" {
   731  		check.Skip("Test_CatalogGetItem: Org name not given")
   732  		return
   733  	}
   734  	if vcd.config.VCD.Catalog.Name == "" {
   735  		check.Skip("Test_CatalogGetItem: Catalog name not given")
   736  		return
   737  	}
   738  	if vcd.config.VCD.Catalog.CatalogItem == "" {
   739  		check.Skip("Test_CatalogGetItem: Catalog item name not given")
   740  		return
   741  	}
   742  
   743  	org, err := vcd.client.GetOrgByName(vcd.config.VCD.Org)
   744  	check.Assert(err, IsNil)
   745  	check.Assert(org, NotNil)
   746  
   747  	catalog, err := org.GetCatalogByName(vcd.config.VCD.Catalog.Name, false)
   748  	check.Assert(err, IsNil)
   749  	check.Assert(catalog, NotNil)
   750  
   751  	catalogItem, err := catalog.GetCatalogItemByName(vcd.config.VCD.Catalog.CatalogItem, false)
   752  	check.Assert(err, IsNil)
   753  	check.Assert(catalogItem, NotNil)
   754  	check.Assert(catalogItem.CatalogItem.Entity, NotNil)
   755  
   756  	vappTemplate, err := catalog.GetVappTemplateByHref(catalogItem.CatalogItem.Entity.HREF)
   757  	check.Assert(err, IsNil)
   758  	check.Assert(vappTemplate, NotNil)
   759  	check.Assert(vappTemplate.VAppTemplate.ID, Not(Equals), catalogItem.CatalogItem.ID)
   760  	check.Assert(vappTemplate.VAppTemplate.Type, Equals, types.MimeVAppTemplate)
   761  	check.Assert(vappTemplate.VAppTemplate.Name, Equals, catalogItem.CatalogItem.Name)
   762  }
   763  
   764  // Test_GetCatalogByNameSharedCatalog creates a separate Org and VDC just to create Catalog and share it with main Org
   765  // One should be able to find shared catalogs from different Organizations
   766  func (vcd *TestVCD) Test_GetCatalogByNameSharedCatalog(check *C) {
   767  	fmt.Printf("Running: %s\n", check.TestName())
   768  	vcd.skipIfNotSysAdmin(check)
   769  	newOrg1, vdc, sharedCatalog := createSharedCatalogInNewOrg(vcd, check, check.TestName())
   770  
   771  	// Try to find the catalog inside Org which owns it - newOrg1
   772  	catalogByName, err := newOrg1.GetCatalogByName(sharedCatalog.Catalog.Name, true)
   773  	check.Assert(err, IsNil)
   774  	check.Assert(catalogByName.Catalog.Name, Equals, sharedCatalog.Catalog.Name)
   775  
   776  	// Try to find the catalog in another Org with which this catalog is shared (vcd.Org)
   777  	sharedCatalogByName, err := vcd.org.GetCatalogByName(sharedCatalog.Catalog.Name, false)
   778  	check.Assert(err, IsNil)
   779  	check.Assert(sharedCatalogByName.Catalog.Name, Equals, sharedCatalog.Catalog.Name)
   780  
   781  	cleanupCatalogOrgVdc(check, sharedCatalog, vdc, vcd, newOrg1)
   782  }
   783  
   784  // Test_GetCatalogByIdSharedCatalog creates a separate Org and VDC just to create Catalog and share it with main Org
   785  // One should be able to find shared catalogs from different Organizations
   786  func (vcd *TestVCD) Test_GetCatalogByIdSharedCatalog(check *C) {
   787  	fmt.Printf("Running: %s\n", check.TestName())
   788  	vcd.skipIfNotSysAdmin(check)
   789  
   790  	newOrg1, vdc, sharedCatalog := createSharedCatalogInNewOrg(vcd, check, check.TestName())
   791  
   792  	// Try to find the sharedCatalog inside Org which owns it - newOrg1
   793  	catalogById, err := newOrg1.GetCatalogById(sharedCatalog.Catalog.ID, true)
   794  	check.Assert(err, IsNil)
   795  	check.Assert(catalogById.Catalog.Name, Equals, sharedCatalog.Catalog.Name)
   796  
   797  	// Try to find the sharedCatalog in another Org with which this sharedCatalog is shared (vcd.Org)
   798  	sharedCatalogById, err := vcd.org.GetCatalogById(sharedCatalog.Catalog.ID, false)
   799  	check.Assert(err, IsNil)
   800  	check.Assert(sharedCatalogById.Catalog.Name, Equals, sharedCatalog.Catalog.Name)
   801  
   802  	cleanupCatalogOrgVdc(check, sharedCatalog, vdc, vcd, newOrg1)
   803  }
   804  
   805  // Test_GetCatalogByNamePrefersLocal tests that local catalog (in the same Org) is prioritised against shared catalogs
   806  // in other Orgs. It does so by creating another Org with shared Catalog named just like the one in testing catalog
   807  func (vcd *TestVCD) Test_GetCatalogByNamePrefersLocal(check *C) {
   808  	fmt.Printf("Running: %s\n", check.TestName())
   809  	vcd.skipIfNotSysAdmin(check)
   810  	// Create a catalog  in new org with exactly the same name as in vcd.Org
   811  	newOrg1, vdc, sharedCatalog := createSharedCatalogInNewOrg(vcd, check, vcd.config.VCD.Catalog.Name)
   812  
   813  	// Make sure that the Owner Org HREF is the local one for vcd.Org catalog named vcd.config.VCD.Catalog.Name
   814  	catalogByNameInTestOrg, err := vcd.org.GetCatalogByName(vcd.config.VCD.Catalog.Name, true)
   815  	check.Assert(err, IsNil)
   816  	check.Assert(catalogByNameInTestOrg.parent.orgName(), Equals, vcd.org.Org.Name)
   817  
   818  	// Make sure that the Owner Org HREF is the local one for vcd.Org catalog named vcd.config.VCD.Catalog.Name
   819  	catalogByNameInNewOrg, err := newOrg1.GetCatalogByName(vcd.config.VCD.Catalog.Name, true)
   820  	check.Assert(err, IsNil)
   821  	check.Assert(catalogByNameInNewOrg.parent.orgName(), Equals, newOrg1.Org.Name)
   822  
   823  	cleanupCatalogOrgVdc(check, sharedCatalog, vdc, vcd, newOrg1)
   824  }
   825  
   826  // Test_GetCatalogByNameSharedCatalogOrgUser additionally tests GetOrgByName and GetOrgById using a custom created Org
   827  // Admin user. It tests the following cases:
   828  // * System user must be able to retrieve any catalog - shared or unshared from another Org
   829  // * Org Admin user must be able to retrieve catalog in his own Org
   830  // * Org Admin user must be able to retrieve shared catalog from another Org
   831  // * Org admin user must not be able to retrieve unshared catalog from another Org
   832  func (vcd *TestVCD) Test_GetCatalogByXSharedCatalogOrgUser(check *C) {
   833  	fmt.Printf("Running: %s\n", check.TestName())
   834  	vcd.skipIfNotSysAdmin(check)
   835  	newOrg1, vdc, sharedCatalog := createSharedCatalogInNewOrg(vcd, check, check.TestName())
   836  
   837  	// Create one more additional catalog which is not shared
   838  	unsharedCatalog, err := newOrg1.CreateCatalog("unshared-catalog", check.TestName())
   839  	check.Assert(err, IsNil)
   840  	AddToCleanupList(unsharedCatalog.Catalog.Name, "catalog", newOrg1.Org.Name, check.TestName())
   841  
   842  	// Try to find the catalog inside Org which owns it - newOrg1
   843  	catalogByName, err := newOrg1.GetCatalogByName(sharedCatalog.Catalog.Name, true)
   844  	check.Assert(err, IsNil)
   845  	check.Assert(catalogByName.Catalog.Name, Equals, sharedCatalog.Catalog.Name)
   846  
   847  	// Try to find the catalog in another Org with which this catalog is shared (vcd.Org)
   848  	sharedCatalogByName, err := vcd.org.GetCatalogByName(sharedCatalog.Catalog.Name, false)
   849  	check.Assert(err, IsNil)
   850  	check.Assert(sharedCatalogByName.Catalog.Name, Equals, sharedCatalog.Catalog.Name)
   851  
   852  	// Try to find unshared catalog from another Org with System user
   853  	systemUnsharedCatalogByName, err := vcd.org.GetCatalogByName(unsharedCatalog.Catalog.Name, true)
   854  	check.Assert(err, IsNil)
   855  	check.Assert(systemUnsharedCatalogByName.Catalog.ID, Equals, unsharedCatalog.Catalog.ID)
   856  
   857  	// Create an Org Admin user and test that it can find catalog as well
   858  	adminOrg, err := vcd.client.GetAdminOrgByName(vcd.config.VCD.Org)
   859  	check.Assert(err, IsNil)
   860  	orgAdminClient, _, err := newOrgUserConnection(adminOrg, "test-user", "CHANGE-ME", vcd.config.Provider.Url, true)
   861  	check.Assert(err, IsNil)
   862  	orgAsOrgUser, err := orgAdminClient.GetOrgByName(vcd.config.VCD.Org)
   863  	check.Assert(err, IsNil)
   864  
   865  	// Find a catalog in the same Org using Org Admin user
   866  	orgAdminCatalogByNameSameOrg, err := orgAsOrgUser.GetCatalogByName(vcd.config.VCD.Catalog.Name, false)
   867  	check.Assert(err, IsNil)
   868  	check.Assert(orgAdminCatalogByNameSameOrg.Catalog.Name, Equals, vcd.config.VCD.Catalog.Name)
   869  
   870  	orgAdminCatalogByIdSameOrg, err := orgAsOrgUser.GetCatalogById(orgAdminCatalogByNameSameOrg.Catalog.ID, false)
   871  	check.Assert(err, IsNil)
   872  	check.Assert(orgAdminCatalogByIdSameOrg.Catalog.Name, Equals, orgAdminCatalogByNameSameOrg.Catalog.Name)
   873  	check.Assert(orgAdminCatalogByIdSameOrg.Catalog.ID, Equals, orgAdminCatalogByNameSameOrg.Catalog.ID)
   874  
   875  	// Find a shared catalog from another Org using Org Admin user
   876  	orgAdminCatalogByName, err := orgAsOrgUser.GetCatalogByName(sharedCatalog.Catalog.Name, false)
   877  	check.Assert(err, IsNil)
   878  	check.Assert(orgAdminCatalogByName.Catalog.Name, Equals, sharedCatalog.Catalog.Name)
   879  	check.Assert(orgAdminCatalogByName.Catalog.ID, Equals, sharedCatalog.Catalog.ID)
   880  
   881  	orgAdminCatalogById, err := orgAsOrgUser.GetCatalogById(sharedCatalog.Catalog.ID, false)
   882  	check.Assert(err, IsNil)
   883  	check.Assert(orgAdminCatalogById.Catalog.Name, Equals, sharedCatalog.Catalog.Name)
   884  	check.Assert(orgAdminCatalogById.Catalog.ID, Equals, sharedCatalog.Catalog.ID)
   885  
   886  	// Try to find unshared catalog from another Org with Org admin user and expect an ErrorEntityNotFound
   887  	_, err = orgAsOrgUser.GetCatalogByName(unsharedCatalog.Catalog.Name, true)
   888  	check.Assert(ContainsNotFound(err), Equals, true)
   889  
   890  	_, err = orgAsOrgUser.GetCatalogById(unsharedCatalog.Catalog.ID, true)
   891  	check.Assert(ContainsNotFound(err), Equals, true)
   892  
   893  	// Cleanup
   894  	err = unsharedCatalog.Delete(true, true)
   895  	check.Assert(err, IsNil)
   896  
   897  	cleanupCatalogOrgVdc(check, sharedCatalog, vdc, vcd, newOrg1)
   898  }
   899  
   900  func createSharedCatalogInNewOrg(vcd *TestVCD, check *C, newCatalogName string) (*Org, *Vdc, Catalog) {
   901  	newOrgName1 := spawnTestOrg(vcd, check, "org")
   902  
   903  	newOrg1, err := vcd.client.GetOrgByName(newOrgName1)
   904  	check.Assert(err, IsNil)
   905  
   906  	// Spawn a VDC inside newly created Org so that there is storage to create new catalog
   907  	vdc := spawnTestVdc(vcd, check, newOrgName1)
   908  
   909  	catalog, err := newOrg1.CreateCatalog(newCatalogName, "Catalog for testing")
   910  	check.Assert(err, IsNil)
   911  	AddToCleanupList(newCatalogName, "catalog", newOrgName1, check.TestName())
   912  
   913  	// Share new Catalog in newOrgName1 with default test Org vcd.Org
   914  	readOnly := "ReadOnly"
   915  	accessControl := &types.ControlAccessParams{
   916  		IsSharedToEveryone:  false,
   917  		EveryoneAccessLevel: &readOnly,
   918  		AccessSettings: &types.AccessSettingList{
   919  			AccessSetting: []*types.AccessSetting{&types.AccessSetting{
   920  				Subject: &types.LocalSubject{
   921  					HREF: vcd.org.Org.HREF,
   922  					Name: vcd.org.Org.Name,
   923  					Type: types.MimeOrg,
   924  				},
   925  				AccessLevel: "ReadOnly",
   926  			}},
   927  		},
   928  	}
   929  	err = catalog.SetAccessControl(accessControl, false)
   930  	check.Assert(err, IsNil)
   931  
   932  	return newOrg1, vdc, catalog
   933  }
   934  
   935  func cleanupCatalogOrgVdc(check *C, sharedCatalog Catalog, vdc *Vdc, vcd *TestVCD, newOrg1 *Org) {
   936  	// Cleanup catalog, vdc and org
   937  	err := sharedCatalog.Delete(true, true)
   938  	check.Assert(err, IsNil)
   939  
   940  	err = vdc.DeleteWait(true, true)
   941  	check.Assert(err, IsNil)
   942  
   943  	adminOrg, err := vcd.client.GetAdminOrgByName(newOrg1.Org.Name)
   944  	check.Assert(err, IsNil)
   945  	err = adminOrg.Delete(true, true)
   946  	check.Assert(err, IsNil)
   947  }
   948  
   949  // Creates a Catalog. Publishes catalog to external Org and then deletes the catalog.
   950  func (vcd *TestVCD) Test_PublishToExternalOrganizations(check *C) {
   951  	fmt.Printf("Running: %s\n", check.TestName())
   952  
   953  	// test with AdminCatalog
   954  	catalogName := check.TestName()
   955  	catalogDescription := check.TestName() + " description"
   956  
   957  	adminOrg, err := vcd.client.GetAdminOrgByName(vcd.config.VCD.Org)
   958  	check.Assert(err, IsNil)
   959  	check.Assert(adminOrg, NotNil)
   960  
   961  	// TODO - remove once VCD is fixed.
   962  	// Every Org update causes catalog publishing to be removed and therefore this test fails.
   963  	// Turning publishing on right before test to be sure it is tested and passes.
   964  	// VCD 10.2.0 <-> 10.3.3 have a bug that even though catalog publishing is enabled adminOrg.
   965  	fmt.Println("Overcomming VCD 10.2.0 <-> 10.3.3 bug - explicitly setting catalog sharing")
   966  	adminOrg.AdminOrg.OrgSettings.OrgGeneralSettings.CanPublishCatalogs = true
   967  	adminOrg.AdminOrg.OrgSettings.OrgGeneralSettings.CanPublishExternally = true
   968  	updatedAdminOrg, err := adminOrg.Update()
   969  	check.Assert(err, IsNil)
   970  	check.Assert(updatedAdminOrg, NotNil)
   971  
   972  	adminCatalog, err := adminOrg.CreateCatalog(catalogName, catalogDescription)
   973  	check.Assert(err, IsNil)
   974  	check.Assert(adminCatalog.AdminCatalog.Name, Equals, catalogName)
   975  	check.Assert(adminCatalog.AdminCatalog.Description, Equals, catalogDescription)
   976  
   977  	AddToCleanupList(catalogName, "catalog", vcd.config.VCD.Org, check.TestName())
   978  
   979  	err = adminCatalog.PublishToExternalOrganizations(types.PublishExternalCatalogParams{
   980  		IsPublishedExternally:    addrOf(true),
   981  		IsCachedEnabled:          addrOf(true),
   982  		Password:                 "secretOrNot",
   983  		PreserveIdentityInfoFlag: addrOf(true),
   984  	})
   985  	check.Assert(err, IsNil)
   986  	check.Assert(*adminCatalog.AdminCatalog.PublishExternalCatalogParams.IsPublishedExternally, Equals, true)
   987  	check.Assert(*adminCatalog.AdminCatalog.PublishExternalCatalogParams.PreserveIdentityInfoFlag, Equals, true)
   988  	check.Assert(*adminCatalog.AdminCatalog.PublishExternalCatalogParams.IsCachedEnabled, Equals, true)
   989  	check.Assert(adminCatalog.AdminCatalog.PublishExternalCatalogParams.Password, Equals, "******")
   990  
   991  	err = adminCatalog.Delete(true, true)
   992  	check.Assert(err, IsNil)
   993  
   994  	// test with Catalog
   995  	org, err := vcd.client.GetOrgByName(vcd.config.VCD.Org)
   996  	check.Assert(err, IsNil)
   997  	check.Assert(org, NotNil)
   998  
   999  	catalog, err := org.CreateCatalog(catalogName, catalogDescription)
  1000  	check.Assert(err, IsNil)
  1001  	check.Assert(catalog.Catalog.Name, Equals, catalogName)
  1002  	check.Assert(catalog.Catalog.Description, Equals, catalogDescription)
  1003  
  1004  	AddToCleanupList(catalogName, "catalog", vcd.config.VCD.Org, check.TestName())
  1005  
  1006  	err = catalog.PublishToExternalOrganizations(types.PublishExternalCatalogParams{
  1007  		IsPublishedExternally:    addrOf(true),
  1008  		IsCachedEnabled:          addrOf(true),
  1009  		Password:                 "secretOrNot",
  1010  		PreserveIdentityInfoFlag: addrOf(true),
  1011  	})
  1012  	check.Assert(err, IsNil)
  1013  	check.Assert(*catalog.Catalog.PublishExternalCatalogParams.IsPublishedExternally, Equals, true)
  1014  	check.Assert(*catalog.Catalog.PublishExternalCatalogParams.PreserveIdentityInfoFlag, Equals, true)
  1015  	check.Assert(*catalog.Catalog.PublishExternalCatalogParams.IsCachedEnabled, Equals, true)
  1016  	check.Assert(catalog.Catalog.PublishExternalCatalogParams.Password, Equals, "******")
  1017  
  1018  	err = catalog.PublishToExternalOrganizations(types.PublishExternalCatalogParams{
  1019  		IsPublishedExternally:    addrOf(true),
  1020  		IsCachedEnabled:          addrOf(false),
  1021  		Password:                 "secretOrNot2",
  1022  		PreserveIdentityInfoFlag: addrOf(false),
  1023  	})
  1024  	check.Assert(err, IsNil)
  1025  	check.Assert(*catalog.Catalog.PublishExternalCatalogParams.IsPublishedExternally, Equals, true)
  1026  	check.Assert(*catalog.Catalog.PublishExternalCatalogParams.PreserveIdentityInfoFlag, Equals, false)
  1027  	check.Assert(*catalog.Catalog.PublishExternalCatalogParams.IsCachedEnabled, Equals, false)
  1028  	check.Assert(catalog.Catalog.PublishExternalCatalogParams.Password, Equals, "******")
  1029  
  1030  	err = catalog.Delete(true, true)
  1031  	check.Assert(err, IsNil)
  1032  }
  1033  
  1034  // Tests System function UploadOvfByLink and verifies that
  1035  // Task.GetTaskProgress returns values of progress.
  1036  func (vcd *TestVCD) Test_UploadOvfByLink_progress_works(check *C) {
  1037  	fmt.Printf("Running: %s\n", check.TestName())
  1038  
  1039  	if vcd.config.OVA.OvfUrl == "" {
  1040  		check.Skip("Skipping test because no OVF URL given")
  1041  	}
  1042  
  1043  	itemName := TestUploadOvf + "URL"
  1044  
  1045  	catalog, org := findCatalog(vcd, check, vcd.config.VCD.Catalog.Name)
  1046  
  1047  	uploadTask, err := catalog.UploadOvfByLink(vcd.config.OVA.OvfUrl, itemName, "upload from test")
  1048  	check.Assert(err, IsNil)
  1049  	check.Assert(uploadTask, NotNil)
  1050  
  1051  	for {
  1052  		if value, err := uploadTask.GetTaskProgress(); value == "100" || err != nil {
  1053  			check.Assert(err, IsNil)
  1054  			break
  1055  		} else {
  1056  			check.Assert(value, Not(Equals), "")
  1057  		}
  1058  	}
  1059  	err = uploadTask.WaitTaskCompletion()
  1060  	check.Assert(err, IsNil)
  1061  
  1062  	AddToCleanupList(itemName, "catalogItem", vcd.org.Org.Name+"|"+vcd.config.VCD.Catalog.Name, "Test_UploadOvfByLink_progress_works")
  1063  
  1064  	catalog, err = org.GetCatalogByName(vcd.config.VCD.Catalog.Name, true)
  1065  	check.Assert(err, IsNil)
  1066  	verifyCatalogItemUploaded(check, catalog, itemName)
  1067  
  1068  	// Delete testing catalog item
  1069  	deleteCatalogItem(check, catalog, itemName)
  1070  }
  1071  
  1072  func (vcd *TestVCD) Test_CatalogQueryMediaList(check *C) {
  1073  	fmt.Printf("Running: %s\n", check.TestName())
  1074  
  1075  	catalogName := vcd.config.VCD.Catalog.Name
  1076  	if catalogName == "" {
  1077  		check.Skip("Test_CatalogQueryMediaList: Catalog name not given")
  1078  		return
  1079  	}
  1080  
  1081  	cat, err := vcd.org.GetCatalogByName(catalogName, false)
  1082  	if err != nil {
  1083  		check.Skip("Test_CatalogQueryMediaList: Catalog not found")
  1084  		return
  1085  	}
  1086  
  1087  	medias, err := cat.QueryMediaList()
  1088  	check.Assert(err, IsNil)
  1089  	check.Assert(medias, NotNil)
  1090  
  1091  	// Check that number of medias is 1
  1092  	// Dump all media structures to easily identify leftover objects if number is not 1
  1093  	if len(medias) > 1 {
  1094  		spew.Dump(medias)
  1095  	}
  1096  	check.Assert(len(medias), Equals, 1)
  1097  
  1098  	// Check that media name is what it should be
  1099  	check.Assert(medias[0].Name, Equals, vcd.config.Media.Media)
  1100  }
  1101  
  1102  // Tests System function UploadMediaImage by using provided ISO file of UDF type.
  1103  func (vcd *TestVCD) Test_CatalogUploadMediaImageWihUdfTypeIso(check *C) {
  1104  	fmt.Printf("Running: %s\n", check.TestName())
  1105  
  1106  	if vcd.config.Media.MediaUdfTypePath == "" {
  1107  		check.Skip("Skipping test because no UDF type ISO path was given")
  1108  	}
  1109  
  1110  	catalog, org := findCatalog(vcd, check, vcd.config.VCD.Catalog.Name)
  1111  
  1112  	mediaName := check.TestName()
  1113  
  1114  	uploadTask, err := catalog.UploadMediaImage(mediaName, "upload from test", vcd.config.Media.MediaUdfTypePath, 1024)
  1115  	check.Assert(err, IsNil)
  1116  	err = uploadTask.WaitTaskCompletion()
  1117  	check.Assert(err, IsNil)
  1118  
  1119  	AddToCleanupList(mediaName, "mediaCatalogImage", vcd.org.Org.Name+"|"+vcd.config.VCD.Catalog.Name, mediaName)
  1120  
  1121  	catalog, err = org.GetCatalogByName(vcd.config.VCD.Catalog.Name, true)
  1122  	check.Assert(err, IsNil)
  1123  	check.Assert(catalog, NotNil)
  1124  	verifyCatalogItemUploaded(check, catalog, mediaName)
  1125  
  1126  	// Delete testing catalog item
  1127  	deleteCatalogItem(check, catalog, mediaName)
  1128  }
  1129  
  1130  func (vcd *TestVCD) Test_GetAdminCatalogById(check *C) {
  1131  	if vcd.config.VCD.Org == "" || vcd.config.VCD.Catalog.Name == "" {
  1132  		check.Skip("no Org or Catalog found in configuration")
  1133  	}
  1134  
  1135  	// 1. Get a catalog from an organization
  1136  	org, err := vcd.client.GetAdminOrgByName(vcd.config.VCD.Org)
  1137  	check.Assert(err, IsNil)
  1138  
  1139  	adminCatalog, err := org.GetAdminCatalogByName(vcd.config.VCD.Catalog.Name, false)
  1140  	check.Assert(err, IsNil)
  1141  
  1142  	// 2. retrieve that same catalog from the client alone using HREF
  1143  	adminCatalogByHref, err := vcd.client.Client.GetAdminCatalogByHref(adminCatalog.AdminCatalog.HREF)
  1144  	check.Assert(err, IsNil)
  1145  	check.Assert(adminCatalogByHref.AdminCatalog.HREF, Equals, adminCatalog.AdminCatalog.HREF)
  1146  
  1147  	// 3. retrieve the same catalog again, using ID
  1148  	adminCatalogById, err := vcd.client.Client.GetAdminCatalogById(adminCatalog.AdminCatalog.ID)
  1149  	check.Assert(err, IsNil)
  1150  	check.Assert(adminCatalogById.AdminCatalog.HREF, Equals, adminCatalog.AdminCatalog.HREF)
  1151  }
  1152  
  1153  func (vcd *TestVCD) Test_CatalogAccessAsOrgUsers(check *C) {
  1154  	if vcd.config.Tenants == nil || len(vcd.config.Tenants) < 2 {
  1155  		check.Skip("no tenants found in configuration")
  1156  	}
  1157  
  1158  	if vcd.config.OVA.OvaPath == "" || vcd.config.Media.MediaPath == "" {
  1159  		check.Skip("no OVA or Media path found in configuration")
  1160  	}
  1161  
  1162  	org1Name := vcd.config.Tenants[0].SysOrg
  1163  	user1Name := vcd.config.Tenants[0].User
  1164  	password1 := vcd.config.Tenants[0].Password
  1165  	org2Name := vcd.config.Tenants[1].SysOrg
  1166  	user2Name := vcd.config.Tenants[1].User
  1167  	password2 := vcd.config.Tenants[1].Password
  1168  
  1169  	org1AsSystem, err := vcd.client.GetAdminOrgByName(org1Name)
  1170  	check.Assert(err, IsNil)
  1171  	check.Assert(org1AsSystem, NotNil)
  1172  
  1173  	org2AsSystem, err := vcd.client.GetAdminOrgByName(org2Name)
  1174  	if err != nil {
  1175  		if ContainsNotFound(err) {
  1176  			check.Skip(fmt.Sprintf("organization %s not found", org2Name))
  1177  		}
  1178  	}
  1179  	check.Assert(err, IsNil)
  1180  	check.Assert(org2AsSystem, NotNil)
  1181  	vcdClient1 := NewVCDClient(vcd.client.Client.VCDHREF, true)
  1182  	err = vcdClient1.Authenticate(user1Name, password1, org1Name)
  1183  	check.Assert(err, IsNil)
  1184  
  1185  	vcdClient2 := NewVCDClient(vcd.client.Client.VCDHREF, true)
  1186  	err = vcdClient2.Authenticate(user2Name, password2, org2Name)
  1187  	check.Assert(err, IsNil)
  1188  
  1189  	org1, err := vcdClient1.GetOrgByName(org1Name)
  1190  	check.Assert(err, IsNil)
  1191  	org2, err := vcdClient2.GetOrgByName(org2Name)
  1192  	check.Assert(err, IsNil)
  1193  	check.Assert(org2, NotNil)
  1194  	catalogName := check.TestName() + "-cat"
  1195  	fmt.Printf("creating catalog %s in org %s\n", catalogName, org1Name)
  1196  	adminCatalog1AsSystem, err := org1AsSystem.CreateCatalog(catalogName, fmt.Sprintf("catalog %s created in %s", catalogName, org1Name))
  1197  	check.Assert(err, IsNil)
  1198  	AddToCleanupList(catalogName, "catalog", org1Name, check.TestName())
  1199  	catalog1AsSystem, err := org1AsSystem.GetCatalogByName(catalogName, true)
  1200  	check.Assert(err, IsNil)
  1201  	fmt.Printf("sharing catalog %s from org %s\n", catalogName, org1Name)
  1202  	err = adminCatalog1AsSystem.SetAccessControl(&types.ControlAccessParams{
  1203  		IsSharedToEveryone: false,
  1204  		AccessSettings: &types.AccessSettingList{
  1205  			AccessSetting: []*types.AccessSetting{
  1206  				{
  1207  					Subject: &types.LocalSubject{
  1208  						HREF: org2.Org.HREF,
  1209  						Name: org2Name,
  1210  						Type: types.MimeOrg,
  1211  					},
  1212  					AccessLevel: types.ControlAccessReadOnly,
  1213  				},
  1214  			},
  1215  		},
  1216  	}, true)
  1217  	check.Assert(err, IsNil)
  1218  
  1219  	// populate the catalog
  1220  
  1221  	vappTemplateName := check.TestName() + "-template"
  1222  	mediaName := check.TestName() + "-media"
  1223  	fmt.Printf("uploading vApp template into catalog %s\n", catalogName)
  1224  	task, err := catalog1AsSystem.UploadOvf(vcd.config.OVA.OvaPath, vappTemplateName, vappTemplateName, 1024)
  1225  	check.Assert(err, IsNil)
  1226  	err = task.WaitTaskCompletion()
  1227  	check.Assert(err, IsNil)
  1228  
  1229  	fmt.Printf("uploading media image into catalog %s\n", catalogName)
  1230  	uploadTask, err := catalog1AsSystem.UploadMediaImage(mediaName, "upload from test", vcd.config.Media.MediaPath, 1024)
  1231  	check.Assert(err, IsNil)
  1232  	err = uploadTask.WaitTaskCompletion()
  1233  	check.Assert(err, IsNil)
  1234  
  1235  	vAppTemplateAsSystem, err := catalog1AsSystem.GetVAppTemplateByName(vappTemplateName)
  1236  	check.Assert(err, IsNil)
  1237  	check.Assert(vAppTemplateAsSystem, NotNil)
  1238  	mediaRecordAsSystem, err := catalog1AsSystem.GetMediaByName(mediaName, true)
  1239  	check.Assert(err, IsNil)
  1240  	check.Assert(mediaRecordAsSystem, NotNil)
  1241  
  1242  	// Retrieve catalog by ID in its own Org
  1243  	adminCatalog1, err := vcdClient1.Client.GetAdminCatalogById(adminCatalog1AsSystem.AdminCatalog.ID)
  1244  	check.Assert(err, IsNil)
  1245  	check.Assert(adminCatalog1.AdminCatalog.HREF, Equals, adminCatalog1AsSystem.AdminCatalog.HREF)
  1246  
  1247  	catalog1, err := vcdClient1.Client.GetCatalogById(adminCatalog1AsSystem.AdminCatalog.ID)
  1248  	check.Assert(err, IsNil)
  1249  	check.Assert(catalog1.Catalog.HREF, Equals, catalog1AsSystem.Catalog.HREF)
  1250  
  1251  	startTime := time.Now()
  1252  	timeout := 100 * time.Second
  1253  	// Start retrieving catalog in the other org
  1254  	fmt.Printf("retrieving catalog %s in org %s\n", catalogName, org2Name)
  1255  	for time.Since(startTime) < timeout {
  1256  		_, err = vcdClient2.Client.GetAdminCatalogById(adminCatalog1AsSystem.AdminCatalog.ID)
  1257  		if err == nil {
  1258  			fmt.Printf("shared catalog available in %s\n", time.Since(startTime))
  1259  			break
  1260  		}
  1261  		time.Sleep(10 * time.Millisecond)
  1262  	}
  1263  	// Retrieve the shared catalog in the other organization
  1264  	adminCatalog2, err := vcdClient2.Client.GetAdminCatalogById(adminCatalog1AsSystem.AdminCatalog.ID)
  1265  	check.Assert(err, IsNil)
  1266  	check.Assert(adminCatalog2, NotNil)
  1267  
  1268  	// Retrieve the catalog from both tenants, using functions that don't rely on organization internals
  1269  	catalog1FromOrg, err := vcdClient1.Client.GetCatalogByName(org1.Org.Name, catalogName)
  1270  	check.Assert(err, IsNil)
  1271  	adminCatalog1FromOrg, err := vcdClient1.Client.GetAdminCatalogByName(org1.Org.Name, catalogName)
  1272  	check.Assert(err, IsNil)
  1273  	catalog2FromOrg, err := vcdClient2.Client.GetCatalogByName(org1.Org.Name, catalogName)
  1274  	check.Assert(err, IsNil)
  1275  	adminCatalog2FromOrg, err := vcdClient2.Client.GetAdminCatalogByName(org1.Org.Name, catalogName)
  1276  	check.Assert(err, IsNil)
  1277  
  1278  	// Also retrieve the catalog items from both tenants
  1279  	vAppTemplate1, err := catalog1FromOrg.GetVAppTemplateByName(vappTemplateName)
  1280  	check.Assert(err, IsNil)
  1281  	check.Assert(vAppTemplate1.VAppTemplate.HREF, Equals, vAppTemplateAsSystem.VAppTemplate.HREF)
  1282  	mediaRecord1, err := catalog1FromOrg.GetMediaByName(mediaName, false)
  1283  	check.Assert(err, IsNil)
  1284  	check.Assert(mediaRecord1.Media.HREF, Equals, mediaRecordAsSystem.Media.HREF)
  1285  
  1286  	vAppTemplate2, err := catalog2FromOrg.GetVAppTemplateByName(vappTemplateName)
  1287  	check.Assert(err, IsNil)
  1288  	check.Assert(vAppTemplate2.VAppTemplate.HREF, Equals, vAppTemplateAsSystem.VAppTemplate.HREF)
  1289  	mediaRecord2, err := catalog2FromOrg.GetMediaByName(mediaName, false)
  1290  	check.Assert(err, IsNil)
  1291  	check.Assert(mediaRecord2.Media.HREF, Equals, mediaRecordAsSystem.Media.HREF)
  1292  
  1293  	check.Assert(catalog1FromOrg.Catalog.HREF, Equals, catalog1AsSystem.Catalog.HREF)
  1294  	check.Assert(adminCatalog1FromOrg.AdminCatalog.HREF, Equals, adminCatalog1AsSystem.AdminCatalog.HREF)
  1295  	check.Assert(adminCatalog2FromOrg.AdminCatalog.HREF, Equals, adminCatalog1AsSystem.AdminCatalog.HREF)
  1296  	check.Assert(catalog2FromOrg.Catalog.HREF, Equals, catalog1AsSystem.Catalog.HREF)
  1297  	timeout = 30 * time.Second
  1298  	startTime = time.Now()
  1299  	for time.Since(startTime) < timeout {
  1300  		err = adminCatalog1AsSystem.Delete(true, true)
  1301  		if err == nil {
  1302  			fmt.Printf("shared catalog deleted in %s\n", time.Since(startTime))
  1303  			break
  1304  		}
  1305  		time.Sleep(200 * time.Millisecond)
  1306  	}
  1307  	check.Assert(err, IsNil)
  1308  }
  1309  
  1310  func (vcd *TestVCD) Test_CatalogAccessAsOrgUsersReadOnly(check *C) {
  1311  	if vcd.config.Tenants == nil || len(vcd.config.Tenants) < 2 {
  1312  		check.Skip("no tenants found in configuration")
  1313  	}
  1314  
  1315  	if vcd.config.OVA.OvaPath == "" || vcd.config.Media.MediaPath == "" {
  1316  		check.Skip("no OVA or Media path found in configuration")
  1317  	}
  1318  
  1319  	org1Name := vcd.config.Tenants[0].SysOrg
  1320  	user1Name := vcd.config.Tenants[0].User
  1321  	password1 := vcd.config.Tenants[0].Password
  1322  	org2Name := vcd.config.Tenants[1].SysOrg
  1323  	user2Name := vcd.config.Tenants[1].User
  1324  	password2 := vcd.config.Tenants[1].Password
  1325  
  1326  	vcdClient1 := NewVCDClient(vcd.client.Client.VCDHREF, true)
  1327  	err := vcdClient1.Authenticate(user1Name, password1, org1Name)
  1328  	check.Assert(err, IsNil)
  1329  
  1330  	vcdClient2 := NewVCDClient(vcd.client.Client.VCDHREF, true)
  1331  	err = vcdClient2.Authenticate(user2Name, password2, org2Name)
  1332  	check.Assert(err, IsNil)
  1333  
  1334  	org1, err := vcdClient1.GetAdminOrgByName(org1Name)
  1335  	check.Assert(err, IsNil)
  1336  	org2, err := vcdClient2.GetAdminOrgByName(org2Name)
  1337  	check.Assert(err, IsNil)
  1338  	check.Assert(org2, NotNil)
  1339  	catalogName := check.TestName() + "-cat"
  1340  	fmt.Printf("creating catalog %s in org %s\n", catalogName, org1Name)
  1341  	adminCatalog1Created, err := org1.CreateCatalog(catalogName, fmt.Sprintf("catalog %s created in %s", catalogName, org1Name))
  1342  	check.Assert(err, IsNil)
  1343  	AddToCleanupList(catalogName, "catalog", org1Name, check.TestName())
  1344  	catalog1AsOrg1, err := org1.GetCatalogByName(catalogName, true)
  1345  	check.Assert(err, IsNil)
  1346  	fmt.Printf("sharing catalog %s from org %s\n", catalogName, org1Name)
  1347  
  1348  	err = adminCatalog1Created.SetReadOnlyAccessControl(true)
  1349  
  1350  	check.Assert(err, IsNil)
  1351  
  1352  	// populate the catalog
  1353  
  1354  	vappTemplateName := check.TestName() + "-template"
  1355  	mediaName := check.TestName() + "-media"
  1356  	fmt.Printf("uploading vApp template into catalog %s\n", catalogName)
  1357  	task, err := catalog1AsOrg1.UploadOvf(vcd.config.OVA.OvaPath, vappTemplateName, vappTemplateName, 1024)
  1358  	check.Assert(err, IsNil)
  1359  	err = task.WaitTaskCompletion()
  1360  	check.Assert(err, IsNil)
  1361  
  1362  	fmt.Printf("uploading media image into catalog %s\n", catalogName)
  1363  	uploadTask, err := catalog1AsOrg1.UploadMediaImage(mediaName, "upload from test", vcd.config.Media.MediaPath, 1024)
  1364  	check.Assert(err, IsNil)
  1365  	err = uploadTask.WaitTaskCompletion()
  1366  	check.Assert(err, IsNil)
  1367  
  1368  	vAppTemplateAsSystem, err := catalog1AsOrg1.GetVAppTemplateByName(vappTemplateName)
  1369  	check.Assert(err, IsNil)
  1370  	check.Assert(vAppTemplateAsSystem, NotNil)
  1371  	mediaRecordAsSystem, err := catalog1AsOrg1.GetMediaByName(mediaName, true)
  1372  	check.Assert(err, IsNil)
  1373  	check.Assert(mediaRecordAsSystem, NotNil)
  1374  
  1375  	// Retrieve catalog by ID in its own Org
  1376  	adminCatalog1, err := vcdClient1.Client.GetAdminCatalogById(adminCatalog1Created.AdminCatalog.ID)
  1377  	check.Assert(err, IsNil)
  1378  	check.Assert(adminCatalog1.AdminCatalog.HREF, Equals, adminCatalog1Created.AdminCatalog.HREF)
  1379  
  1380  	catalog1, err := vcdClient1.Client.GetCatalogById(adminCatalog1Created.AdminCatalog.ID)
  1381  	check.Assert(err, IsNil)
  1382  	check.Assert(catalog1.Catalog.HREF, Equals, catalog1AsOrg1.Catalog.HREF)
  1383  
  1384  	startTime := time.Now()
  1385  	timeout := 100 * time.Second
  1386  	var timeElapsedToAvailability time.Duration
  1387  	// Start retrieving catalog in the other org
  1388  	fmt.Printf("retrieving catalog %s in org %s\n", catalogName, org2Name)
  1389  	for time.Since(startTime) < timeout {
  1390  		_, err = vcdClient2.Client.GetAdminCatalogById(adminCatalog1Created.AdminCatalog.ID)
  1391  		if err == nil {
  1392  			timeElapsedToAvailability = time.Since(startTime)
  1393  			fmt.Printf("shared catalog available in %s\n", timeElapsedToAvailability)
  1394  			break
  1395  		}
  1396  		time.Sleep(10 * time.Millisecond)
  1397  	}
  1398  	// Retrieve the shared catalog in the other organization
  1399  	adminCatalog2, err := vcdClient2.Client.GetAdminCatalogById(adminCatalog1Created.AdminCatalog.ID)
  1400  	check.Assert(err, IsNil)
  1401  	check.Assert(adminCatalog2, NotNil)
  1402  
  1403  	// Retrieve the catalog from both tenants, using functions that don't rely on organization internals
  1404  	catalog1FromOrg, err := vcdClient1.Client.GetCatalogByName(org1.AdminOrg.Name, catalogName)
  1405  	check.Assert(err, IsNil)
  1406  	adminCatalog1FromOrg, err := vcdClient1.Client.GetAdminCatalogByName(org1.AdminOrg.Name, catalogName)
  1407  	check.Assert(err, IsNil)
  1408  	catalog2FromOrg, err := vcdClient2.Client.GetCatalogByName(org1.AdminOrg.Name, catalogName)
  1409  	check.Assert(err, IsNil)
  1410  	adminCatalog2FromOrg, err := vcdClient2.Client.GetAdminCatalogByName(org1.AdminOrg.Name, catalogName)
  1411  	check.Assert(err, IsNil)
  1412  
  1413  	// Also retrieve the catalog items from both tenants
  1414  	vAppTemplate1, err := catalog1FromOrg.GetVAppTemplateByName(vappTemplateName)
  1415  	check.Assert(err, IsNil)
  1416  	check.Assert(vAppTemplate1.VAppTemplate.HREF, Equals, vAppTemplateAsSystem.VAppTemplate.HREF)
  1417  	mediaRecord1, err := catalog1FromOrg.GetMediaByName(mediaName, false)
  1418  	check.Assert(err, IsNil)
  1419  	check.Assert(mediaRecord1.Media.HREF, Equals, mediaRecordAsSystem.Media.HREF)
  1420  
  1421  	vAppTemplate2, err := catalog2FromOrg.GetVAppTemplateByName(vappTemplateName)
  1422  	check.Assert(err, IsNil)
  1423  	check.Assert(vAppTemplate2.VAppTemplate.HREF, Equals, vAppTemplateAsSystem.VAppTemplate.HREF)
  1424  	mediaRecord2, err := catalog2FromOrg.GetMediaByName(mediaName, false)
  1425  	check.Assert(err, IsNil)
  1426  	check.Assert(mediaRecord2.Media.HREF, Equals, mediaRecordAsSystem.Media.HREF)
  1427  
  1428  	check.Assert(catalog1FromOrg.Catalog.HREF, Equals, catalog1AsOrg1.Catalog.HREF)
  1429  	check.Assert(adminCatalog1FromOrg.AdminCatalog.HREF, Equals, adminCatalog1Created.AdminCatalog.HREF)
  1430  	check.Assert(adminCatalog2FromOrg.AdminCatalog.HREF, Equals, adminCatalog1Created.AdminCatalog.HREF)
  1431  	check.Assert(catalog2FromOrg.Catalog.HREF, Equals, catalog1AsOrg1.Catalog.HREF)
  1432  
  1433  	isSharedReadOnly, err := adminCatalog1.IsSharedReadOnly()
  1434  	check.Assert(err, IsNil)
  1435  	check.Assert(isSharedReadOnly, Equals, true)
  1436  
  1437  	fmt.Println("removing read-only catalog sharing")
  1438  	err = adminCatalog1Created.SetReadOnlyAccessControl(false)
  1439  	check.Assert(err, IsNil)
  1440  	catalog1FromOrg, err = vcdClient1.Client.GetCatalogByName(org1.AdminOrg.Name, catalogName)
  1441  	check.Assert(err, IsNil)
  1442  	check.Assert(catalog1FromOrg, NotNil)
  1443  	fmt.Println("try retrieving read-only catalog from second org")
  1444  	time.Sleep(timeElapsedToAvailability)
  1445  	adminCatalog2FromOrg, err = vcdClient2.Client.GetAdminCatalogByName(org1.AdminOrg.Name, catalogName)
  1446  	check.Assert(err, NotNil)
  1447  	check.Assert(adminCatalog2FromOrg, IsNil)
  1448  
  1449  	isSharedReadOnly, err = adminCatalog1.IsSharedReadOnly()
  1450  	check.Assert(err, IsNil)
  1451  	check.Assert(isSharedReadOnly, Equals, false)
  1452  
  1453  	timeout = 30 * time.Second
  1454  	startTime = time.Now()
  1455  	for time.Since(startTime) < timeout {
  1456  		err = adminCatalog1Created.Delete(true, true)
  1457  		if err == nil {
  1458  			fmt.Printf("shared catalog deleted in %s\n", time.Since(startTime))
  1459  			break
  1460  		}
  1461  		time.Sleep(200 * time.Millisecond)
  1462  	}
  1463  	check.Assert(err, IsNil)
  1464  }
  1465  
  1466  func (vcd *TestVCD) Test_CatalogCreateCompleteness(check *C) {
  1467  	fmt.Printf("Running: %s\n", check.TestName())
  1468  
  1469  	adminOrg, err := vcd.client.GetAdminOrgByName(vcd.config.VCD.Org)
  1470  	check.Assert(err, IsNil)
  1471  	check.Assert(adminOrg, NotNil)
  1472  	catalogName := "TestAdminCatalogCreate"
  1473  	adminCatalog, err := adminOrg.CreateCatalog(catalogName, catalogName)
  1474  	check.Assert(err, IsNil)
  1475  	AddToCleanupList(catalogName, "catalog", vcd.config.VCD.Org, check.TestName())
  1476  	metadataLink := adminCatalog.AdminCatalog.Link.ForType(types.MimeMetaData, "add")
  1477  	check.Assert(metadataLink, NotNil)
  1478  	err = adminCatalog.Delete(true, true)
  1479  	check.Assert(err, IsNil)
  1480  
  1481  	catalogName = "TestCatalogCreate"
  1482  	org, err := vcd.client.GetOrgByName(vcd.config.VCD.Org)
  1483  	check.Assert(err, IsNil)
  1484  	catalog, err := org.CreateCatalog(catalogName, catalogName)
  1485  	check.Assert(err, IsNil)
  1486  	AddToCleanupList(catalogName, "catalog", vcd.config.VCD.Org, check.TestName())
  1487  	metadataLink = nil
  1488  	metadataLink = catalog.Catalog.Link.ForType(types.MimeMetaData, "add")
  1489  	check.Assert(metadataLink, NotNil)
  1490  	err = catalog.Delete(true, true)
  1491  	check.Assert(err, IsNil)
  1492  }
  1493  
  1494  func (vcd *TestVCD) Test_CaptureVapp(check *C) {
  1495  	fmt.Printf("Running: %s\n", check.TestName())
  1496  
  1497  	vapp, vm := createNsxtVAppAndVm(vcd, check)
  1498  	check.Assert(vapp, NotNil)
  1499  	check.Assert(vm, NotNil)
  1500  
  1501  	// retrieve NSX-T Catalog
  1502  	cat, err := vcd.org.GetCatalogByName(vcd.config.VCD.Catalog.NsxtBackedCatalogName, false)
  1503  	check.Assert(err, IsNil)
  1504  	check.Assert(cat, NotNil)
  1505  
  1506  	vAppCaptureParams := &types.CaptureVAppParams{
  1507  		Name: check.TestName() + "vm-template",
  1508  		Source: &types.Reference{
  1509  			HREF: vapp.VApp.HREF,
  1510  		},
  1511  		CustomizationSection: types.CaptureVAppParamsCustomizationSection{
  1512  			Info:                   "CustomizeOnInstantiate Settings",
  1513  			CustomizeOnInstantiate: true,
  1514  		},
  1515  		CopyTpmOnInstantiate: addrOf(false),
  1516  	}
  1517  
  1518  	templ, err := cat.CaptureVappTemplate(vAppCaptureParams)
  1519  	check.Assert(err, IsNil)
  1520  	check.Assert(templ, NotNil)
  1521  
  1522  	err = templ.Delete()
  1523  	check.Assert(err, IsNil)
  1524  
  1525  	AddToCleanupList(templ.VAppTemplate.Name, "catalogItem", vcd.org.Org.Name+"|"+vcd.config.VCD.Catalog.NsxtBackedCatalogName, check.TestName())
  1526  }