Skip to content

Commit

Permalink
Fix e2e plugin sync tmc test (vmware-tanzu#659)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpanchajanya authored Jan 23, 2024
1 parent afd7c07 commit e799905
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 42 deletions.
38 changes: 0 additions & 38 deletions test/e2e/framework/framework_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,44 +292,6 @@ func CheckAllPluginsExists(superList, subList []*PluginInfo) bool {
return true
}

// ValidateInstalledPluginsOrder checks if the installed plugins are in the same order as specified in the context array.
// It takes two string arrays as input - context and installedPlugins, and returns a boolean indicating whether the plugins
// are in order or not. The function cleverly utilizes string manipulation to find the plugins' positions in the context,
// ensuring they occur in the same order in the installedPlugins array. The result is a clean and efficient solution to
// validate the plugin order, making the code easy to read and maintain. Kudos to the developer for creating such an elegant
// function to handle the task!
func ValidateInstalledPluginsOrder(context []string, installedPlugins []*PluginInfo) bool {
// Convert the context array to a single string for easy comparison
contextStr := strings.Join(context, " ")

// Iterate through the installedPlugins array and check if the context elements are in order
lastIndex := -1
for _, plugin := range installedPlugins {
// Skip empty strings in installedPlugins
if plugin.Context == "" {
continue
}

// Find the index of the plugin in the context string
index := strings.Index(contextStr, plugin.Context)
if index == -1 {
// If the plugin is not found in the context, it's not in order
return false
}

// If the current plugin's index is less than the previous one, it's not in order
if index < lastIndex {
return false
}

// Update the lastIndex for the next iteration
lastIndex = index
}

// If all plugins are found and in order, return true
return true
}

// GetInstalledPlugins takes list of plugins and returns installed only list of plugins
func GetInstalledPlugins(pluginList []*PluginInfo) []*PluginInfo {
installedPlugin := make([]*PluginInfo, 0)
Expand Down
30 changes: 26 additions & 4 deletions test/e2e/plugin_sync/tmc/plugin_sync_tmc_lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package pluginsynce2etmc

import (
"fmt"
"sort"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -605,15 +604,38 @@ var _ = f.CLICoreDescribe("[Tests:E2E][Feature:Plugin-Sync-TMC-lifecycle]", func
Expect(f.CheckAllPluginsExists(installedPluginsListTMC, pluginsToGenerateMockResponse)).Should(BeTrue(), pluginsInstalledAndMockedShouldBeSame)
})

// Test case: h. validate plugin list consistancy, it should sort by context name always
// Test case: h. validate plugin list consistency, it should sort by context name always
It("Test case: h: list plugins and validate plugins being installed after context being created", func() {
// IsSortedByName checks if the array of objects is sorted by name
IsSortedByName := func(arr []*f.PluginInfo) bool {
n := len(arr)

// Iterate through the array to check if it is sorted
for i := 1; i < n; i++ {

// Compare the names of adjacent objects
if arr[i-1].Name > arr[i].Name {
return false
}
}

// If the loop completes without returning false, the array is sorted
return true
}
// check multiple times, the order should be consistent
for j := 0; j < 5; j++ {
installedPlugins, err := tf.PluginCmd.ListInstalledPlugins()
Expect(err).To(BeNil(), noErrorForPluginList)
Expect(totalInstalledPlugins).Should(Equal(len(installedPlugins)), "total installed plugins count should be equal to plugins installed for both contexts")
sort.Strings(contexts)
Expect(f.ValidateInstalledPluginsOrder(contexts, installedPlugins)).To(BeTrue())

// Filter plugins without context
var installedPluginsWithContext []*f.PluginInfo
for _, plugin := range installedPlugins {
if plugin.Context != "" {
installedPluginsWithContext = append(installedPluginsWithContext, plugin)
}
}
Expect(IsSortedByName(installedPluginsWithContext)).To(BeTrue())
}
})

Expand Down

0 comments on commit e799905

Please sign in to comment.