Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Chandra Pamuluri committed Oct 13, 2023
1 parent 55ed6f5 commit 5c17e63
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 29 deletions.
8 changes: 4 additions & 4 deletions pkg/command/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,13 @@ func createCtx(_ *cobra.Command, args []string) (err error) {
}

// Sync all required plugins
_ = syncContextPlugins(ctx.Target)
_ = syncContextPlugins(ctx.ContextType)

return nil
}

func syncContextPlugins(target configtypes.Target) error {
err := pluginmanager.SyncPluginsForTarget(target)
func syncContextPlugins(contextType configtypes.ContextType) error {
err := pluginmanager.SyncPluginsForContextType(contextType)
if err != nil {
log.Warningf("unable to automatically sync the plugins from target context. Please run 'tanzu plugin sync' command to sync plugins manually, error: '%v'", err.Error())
}
Expand Down Expand Up @@ -995,7 +995,7 @@ func useCtx(_ *cobra.Command, args []string) error {
}

// Sync all required plugins
_ = syncContextPlugins(ctx.Target)
_ = syncContextPlugins(ctx.ContextType)

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/command/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ Plugins installed with this command will only be available while the context rem
if !configtypes.IsValidContextType(contextTypeStr) {
return errors.New(invalidContextTypeMsg)
}
err = pluginmanager.SyncPluginsForTarget(configtypes.ConvertContextTypeToTarget(getContextType()))
err = pluginmanager.SyncPluginsForContextType(getContextType())
} else {
err = pluginmanager.SyncPlugins()
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/pluginmanager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -1003,15 +1003,15 @@ func SyncPlugins() error {
return kerrors.NewAggregate(errList)
}

// SyncPluginsForTarget installs the plugins for given target
func SyncPluginsForTarget(target configtypes.Target) error {
currentContextMap, err := configlib.GetAllCurrentContextsMap()
// SyncPluginsForContextType installs the plugins for given ContextType
func SyncPluginsForContextType(contextType configtypes.ContextType) error {
currentContextMap, err := configlib.GetAllActiveContextsMap()
if err != nil {
return err
}
ctx, ok := currentContextMap[target]
ctx, ok := currentContextMap[contextType]
if !ok {
return fmt.Errorf("there is no active context for the target %v ", target)
return fmt.Errorf("there is no active context for the contextType %v ", contextType)
}
log.Infof("Checking for required plugins for context %s...", ctx.Name)
errList := make([]error, 0)
Expand Down
18 changes: 9 additions & 9 deletions pkg/pluginmanager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -673,8 +673,8 @@ func Test_SyncPlugins(t *testing.T) {
}
}

// Test_SyncPlugins_ForK8SSpecificTarget tests to sync plugins for k8s specific target only
func Test_SyncPlugins_ForK8SSpecificTarget(t *testing.T) {
// Test_SyncPlugins_ForK8SSpecificTarget tests to sync plugins for k8s specific ContextType only
func Test_SyncPlugins_ForK8SSpecificContextType(t *testing.T) {
assertions := assert.New(t)

defer setupPluginSourceForTesting()()
Expand All @@ -688,35 +688,35 @@ func Test_SyncPlugins_ForK8SSpecificTarget(t *testing.T) {
// but other server plugins will be found, so we use those
assertions.Contains(err.Error(), `Failed to load Kubeconfig file from "config"`)
assertions.Equal(len(expectedDiscoveredContextPlugins), len(serverPlugins))
var k8sTargetPlugins []*discovery.Discovered
var k8sContextPlugins []*discovery.Discovered
for _, edp := range expectedDiscoveredContextPlugins {
p := findDiscoveredPlugin(serverPlugins, edp.Name, edp.Target)
assertions.NotNil(p)
assertions.Equal(common.PluginStatusNotInstalled, p.Status)
if p.Target == configtypes.TargetK8s {
k8sTargetPlugins = append(k8sTargetPlugins, p)
k8sContextPlugins = append(k8sContextPlugins, p)
}
}

// Sync all available plugins
err = SyncPluginsForTarget(configtypes.TargetK8s)
err = SyncPluginsForContextType(configtypes.ContextTypeK8s)
assertions.NotNil(err)
// There is an error for the kubernetes discovery since we don't have a cluster
// but other server plugins will be found, so we use those
assertions.Contains(err.Error(), `Failed to load Kubeconfig file from "config"`)

installedServerPlugins, err := pluginsupplier.GetInstalledServerPlugins()
assertions.Nil(err)
assertions.Equal(len(installedServerPlugins), len(k8sTargetPlugins))
assertions.Equal(len(installedServerPlugins), len(k8sContextPlugins))

for _, isp := range installedServerPlugins {
p := findDiscoveredPlugin(serverPlugins, isp.Name, isp.Target)
assertions.NotNil(p)
}
}

// Test_SyncPlugins_ForTMCSpecificTarget tests to sync plugins for tmc specific target only
func Test_SyncPlugins_ForTMCSpecificTarget(t *testing.T) {
// Test_SyncPlugins_ForTMCSpecificContextType tests to sync plugins for tmc specific ContextType only
func Test_SyncPlugins_ForTMCSpecificContextType(t *testing.T) {
assertions := assert.New(t)

defer setupPluginSourceForTesting()()
Expand All @@ -741,7 +741,7 @@ func Test_SyncPlugins_ForTMCSpecificTarget(t *testing.T) {
}

// Sync all available plugins
err = SyncPluginsForTarget(configtypes.TargetTMC)
err = SyncPluginsForContextType(configtypes.ContextTypeTMC)
assertions.Nil(err)

installedServerPlugins, err := pluginsupplier.GetInstalledServerPlugins()
Expand Down
20 changes: 10 additions & 10 deletions test/e2e/plugin_sync/tmc/plugin_sync_tmc_lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ var _ = f.CLICoreDescribe("[Tests:E2E][Feature:Plugin-Sync-TMC-lifecycle]", func

// Test case: i. set both k8s and tmc context as active
// clean plugins
// perform target specific sync (k8s specific)
// perform contextType specific sync (k8s specific)
It("use first context, check plugin list", func() {
err = tf.ContextCmd.UseContext(contextNameK8s)
Expect(err).To(BeNil(), "use context should not return any error")
Expand All @@ -786,23 +786,23 @@ var _ = f.CLICoreDescribe("[Tests:E2E][Feature:Plugin-Sync-TMC-lifecycle]", func
err = tf.PluginCmd.CleanPlugins()
Expect(err).To(BeNil(), "plugin clean should not return any error")

// run target specific sync
_, _, err = tf.PluginCmd.Sync(f.AddAdditionalFlagAndValue("--target k8s"))
// run contextType specific sync
_, _, err = tf.PluginCmd.Sync(f.AddAdditionalFlagAndValue("--type k8s"))
Expect(err).To(BeNil(), "there should be an error for plugin sync for k8s context")
// k8s target specific plugins only should be installed
// k8s contextType specific plugins only should be installed
installedPluginsListK8s, err = tf.PluginCmd.ListPluginsForGivenContext(contextNameK8s, true)
Expect(err).To(BeNil(), "should not get any error for plugin list")
Expect(f.CheckAllPluginsExists(installedPluginsListK8s, pluginsInfoForCRsApplied)).Should(BeTrue(), " plugins being installed and plugins info for which CRs applied should be same")

// Sync should not happen for the tmc context specific plugins, as its target specific sync
// Sync should not happen for the tmc context specific plugins, as its contextType specific sync
installedPluginsListTMC, err = tf.PluginCmd.ListPluginsForGivenContext(contextNameTMC, true)
Expect(len(installedPluginsListTMC)).Should(Equal(0))
Expect(err).To(BeNil(), noErrorForPluginList)
})

// Test case: set both k8s and tmc context as active
// clean plugins
// perform target specific sync (tmc target)
// perform contextType specific sync (tmc type)
It("use first context, check plugin list", func() {
err = tf.ContextCmd.UseContext(contextNameK8s)
Expect(err).To(BeNil(), "use context should not return any error")
Expand All @@ -813,16 +813,16 @@ var _ = f.CLICoreDescribe("[Tests:E2E][Feature:Plugin-Sync-TMC-lifecycle]", func
err = tf.PluginCmd.CleanPlugins()
Expect(err).To(BeNil(), "plugin clean should not return any error")

// run target specific sync
_, _, err = tf.PluginCmd.Sync(f.AddAdditionalFlagAndValue("--target tmc"))
// run contextType specific sync
_, _, err = tf.PluginCmd.Sync(f.AddAdditionalFlagAndValue("--type tmc"))
Expect(err).To(BeNil(), "there should be an error for plugin sync for k8s context")
// tmc target specific plugins only should be installed
// tmc contextType specific plugins only should be installed
installedPluginsListTMC, err = tf.PluginCmd.ListPluginsForGivenContext(contextNameTMC, true)
Expect(err).To(BeNil(), noErrorForPluginList)
Expect(len(installedPluginsListTMC)).Should(Equal(len(pluginsToGenerateMockResponseTMC)), numberOfPluginsSameAsNoOfPluginsInfoMocked)
Expect(f.CheckAllPluginsExists(installedPluginsListTMC, pluginsToGenerateMockResponseTMC)).Should(BeTrue(), pluginsInstalledAndMockedShouldBeSame)

// Sync should not happen for the k8s context specific plugins, as its target specific sync
// Sync should not happen for the k8s context specific plugins, as its contextType specific sync
installedPluginsListK8s, err := tf.PluginCmd.ListPluginsForGivenContext(contextNameK8s, true)
Expect(len(installedPluginsListK8s)).Should(Equal(0))
Expect(err).To(BeNil(), noErrorForPluginList)
Expand Down

0 comments on commit 5c17e63

Please sign in to comment.