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

     1  //go:build functional || openapi || uiPlugin || ALL
     2  
     3  package govcd
     4  
     5  import (
     6  	"fmt"
     7  	"github.com/vmware/go-vcloud-director/v2/types/v56"
     8  	. "gopkg.in/check.v1"
     9  	"strings"
    10  )
    11  
    12  func init() {
    13  	testingTags["uiPlugin"] = "ui_plugin_test.go"
    14  }
    15  
    16  // Test_UIPlugin tests all the possible operations that can be done with a UIPlugin object in VCD.
    17  func (vcd *TestVCD) Test_UIPlugin(check *C) {
    18  	if vcd.skipAdminTests {
    19  		check.Skip(fmt.Sprintf(TestRequiresSysAdminPrivileges, check.TestName()))
    20  	}
    21  
    22  	if vcd.config.Media.UiPluginPath == "" {
    23  		check.Skip("The testing configuration property 'media.uiPluginPath' is empty")
    24  	}
    25  
    26  	testUIPluginMetadata, err := getPluginMetadata(vcd.config.Media.UiPluginPath)
    27  	check.Assert(err, IsNil)
    28  
    29  	// Add a plugin present on disk
    30  	newUIPlugin, err := vcd.client.AddUIPlugin(vcd.config.Media.UiPluginPath, true)
    31  	check.Assert(err, IsNil)
    32  	AddToCleanupListOpenApi(newUIPlugin.UIPluginMetadata.ID, check.TestName(), types.OpenApiEndpointExtensionsUi+newUIPlugin.UIPluginMetadata.ID)
    33  
    34  	// Assert that the returned metadata from VCD corresponds to the one present inside the ZIP file.
    35  	check.Assert(newUIPlugin.UIPluginMetadata.ID, Not(Equals), "")
    36  	check.Assert(newUIPlugin.UIPluginMetadata.Vendor, Equals, testUIPluginMetadata.Vendor)
    37  	check.Assert(newUIPlugin.UIPluginMetadata.License, Equals, testUIPluginMetadata.License)
    38  	check.Assert(newUIPlugin.UIPluginMetadata.Link, Equals, testUIPluginMetadata.Link)
    39  	check.Assert(newUIPlugin.UIPluginMetadata.PluginName, Equals, testUIPluginMetadata.PluginName)
    40  	check.Assert(newUIPlugin.UIPluginMetadata.Version, Equals, testUIPluginMetadata.Version)
    41  	check.Assert(newUIPlugin.UIPluginMetadata.Description, Equals, testUIPluginMetadata.Description)
    42  	check.Assert(newUIPlugin.UIPluginMetadata.ProviderScoped, Equals, testUIPluginMetadata.ProviderScoped)
    43  	check.Assert(newUIPlugin.UIPluginMetadata.TenantScoped, Equals, testUIPluginMetadata.TenantScoped)
    44  	check.Assert(newUIPlugin.UIPluginMetadata.Enabled, Equals, true)
    45  
    46  	// Try to add the same plugin twice, it should fail
    47  	_, err = vcd.client.AddUIPlugin(vcd.config.Media.UiPluginPath, true)
    48  	check.Assert(err, NotNil)
    49  	check.Assert(true, Equals, strings.Contains(err.Error(), "same pluginName-version-vendor"))
    50  
    51  	// We refresh it to have the latest status
    52  	newUIPlugin, err = vcd.client.GetUIPluginById(newUIPlugin.UIPluginMetadata.ID)
    53  	check.Assert(err, IsNil)
    54  	check.Assert(true, Equals, newUIPlugin.UIPluginMetadata.PluginStatus == "ready" || newUIPlugin.UIPluginMetadata.PluginStatus == "unavailable")
    55  
    56  	// We check that the error returned by a non-existent ID is correct:
    57  	_, err = vcd.client.GetUIPluginById("urn:vcloud:uiPlugin:00000000-0000-0000-0000-000000000000")
    58  	check.Assert(err, NotNil)
    59  	check.Assert(true, Equals, strings.Contains(err.Error(), "could not find any UI plugin with ID"))
    60  
    61  	// Retrieve the created plugin using different getters
    62  	allUIPlugins, err := vcd.client.GetAllUIPlugins()
    63  	check.Assert(err, IsNil)
    64  	for _, plugin := range allUIPlugins {
    65  		if plugin.IsTheSameAs(newUIPlugin) {
    66  			plugin.UIPluginMetadata.PluginStatus = newUIPlugin.UIPluginMetadata.PluginStatus // We ignore status as it can be quite arbitrary
    67  			check.Assert(plugin.UIPluginMetadata, DeepEquals, newUIPlugin.UIPluginMetadata)
    68  		}
    69  	}
    70  	retrievedUIPlugin, err := vcd.client.GetUIPlugin(newUIPlugin.UIPluginMetadata.Vendor, newUIPlugin.UIPluginMetadata.PluginName, newUIPlugin.UIPluginMetadata.Version)
    71  	check.Assert(err, IsNil)
    72  	retrievedUIPlugin.UIPluginMetadata.PluginStatus = newUIPlugin.UIPluginMetadata.PluginStatus // We ignore status as it can be quite arbitrary
    73  	check.Assert(retrievedUIPlugin.UIPluginMetadata, DeepEquals, newUIPlugin.UIPluginMetadata)
    74  
    75  	// Publishing the plugin to all tenants
    76  	err = newUIPlugin.PublishAll()
    77  	check.Assert(err, IsNil)
    78  
    79  	// Retrieving the published tenants, it should be at least the number of tenants provided in the test configuration + 1 (the System one)
    80  	orgRefs, err := newUIPlugin.GetPublishedTenants()
    81  	check.Assert(err, IsNil)
    82  	check.Assert(orgRefs, NotNil)
    83  	check.Assert(len(orgRefs) >= len(vcd.config.Tenants)+1, Equals, true)
    84  
    85  	// Unpublishing the plugin from all the tenants
    86  	err = newUIPlugin.UnpublishAll()
    87  	check.Assert(err, IsNil)
    88  
    89  	// Retrieving the published tenants, it should equal to 0
    90  	orgRefs, err = newUIPlugin.GetPublishedTenants()
    91  	check.Assert(err, IsNil)
    92  	check.Assert(orgRefs, NotNil)
    93  	check.Assert(len(orgRefs), Equals, 0)
    94  
    95  	// Publishing/Unpublishing to/from a specific tenant, if available
    96  	if len(vcd.config.Tenants) > 0 {
    97  		existingOrg, err := vcd.client.GetOrgByName(vcd.config.Tenants[0].SysOrg)
    98  		check.Assert(err, IsNil)
    99  		existingOrgRefs := types.OpenApiReferences{{Name: existingOrg.Org.Name, ID: existingOrg.Org.ID}}
   100  
   101  		// Publish to the retrieved tenant
   102  		err = newUIPlugin.Publish(existingOrgRefs)
   103  		check.Assert(err, IsNil)
   104  
   105  		// Retrieving the published tenants, it should equal to the tenant above
   106  		orgRefs, err = newUIPlugin.GetPublishedTenants()
   107  		check.Assert(err, IsNil)
   108  		check.Assert(orgRefs, NotNil)
   109  		check.Assert(len(orgRefs), Equals, 1)
   110  		check.Assert(orgRefs[0].Name, Equals, existingOrg.Org.Name)
   111  
   112  		// Unpublishing from the same specific tenant
   113  		err = newUIPlugin.Unpublish(existingOrgRefs)
   114  		check.Assert(err, IsNil)
   115  
   116  		// Retrieving the published tenants, it should equal to 0 as we just unpublished it
   117  		orgRefs, err = newUIPlugin.GetPublishedTenants()
   118  		check.Assert(err, IsNil)
   119  		check.Assert(orgRefs, NotNil)
   120  		check.Assert(len(orgRefs), Equals, 0)
   121  	}
   122  
   123  	// Check that the plugin can be disabled and its scope changed
   124  	err = newUIPlugin.Update(false, false, false)
   125  	check.Assert(err, IsNil)
   126  	check.Assert(newUIPlugin.UIPluginMetadata.Enabled, Equals, false)
   127  	check.Assert(newUIPlugin.UIPluginMetadata.ProviderScoped, Equals, false)
   128  	check.Assert(newUIPlugin.UIPluginMetadata.TenantScoped, Equals, false)
   129  
   130  	// Check that the plugin can be enabled again and its scope changed
   131  	err = newUIPlugin.Update(true, true, true)
   132  	check.Assert(err, IsNil)
   133  	check.Assert(newUIPlugin.UIPluginMetadata.Enabled, Equals, true)
   134  	check.Assert(newUIPlugin.UIPluginMetadata.ProviderScoped, Equals, true)
   135  	check.Assert(newUIPlugin.UIPluginMetadata.TenantScoped, Equals, true)
   136  
   137  	check.Assert(newUIPlugin.IsTheSameAs(retrievedUIPlugin), Equals, true)
   138  	check.Assert(newUIPlugin.IsTheSameAs(&UIPlugin{UIPluginMetadata: &types.UIPluginMetadata{Vendor: "foo", Version: "1.2.3", PluginName: "bar"}}), Equals, false)
   139  
   140  	// Delete the created plugin
   141  	err = newUIPlugin.Delete()
   142  	check.Assert(err, IsNil)
   143  	check.Assert(*newUIPlugin.UIPluginMetadata, DeepEquals, types.UIPluginMetadata{})
   144  
   145  	// Check that the plugin was correctly deleted
   146  	_, err = vcd.client.GetUIPlugin(retrievedUIPlugin.UIPluginMetadata.Vendor, retrievedUIPlugin.UIPluginMetadata.PluginName, retrievedUIPlugin.UIPluginMetadata.Version)
   147  	check.Assert(err, NotNil)
   148  	check.Assert(true, Equals, strings.Contains(err.Error(), ErrorEntityNotFound.Error()))
   149  	_, err = vcd.client.GetUIPluginById(retrievedUIPlugin.UIPluginMetadata.ID)
   150  	check.Assert(err, NotNil)
   151  	check.Assert(true, Equals, strings.Contains(err.Error(), ErrorEntityNotFound.Error()))
   152  }