Skip to content

Commit

Permalink
inprogress changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Chandra Pamuluri committed Apr 25, 2023
1 parent 67cffd7 commit 4a79f01
Show file tree
Hide file tree
Showing 9 changed files with 544 additions and 13 deletions.
8 changes: 4 additions & 4 deletions test/e2e/context/tmc/context_tmc_lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ var _ = framework.CLICoreDescribe("[Tests:E2E][Feature:Context-lifecycle-tmc]",
// Test case: b. Create context for TMC target with TMC cluster URL as endpoint
It("create tmc context with endpoint", func() {
ctxName := ContextNamePrefix + framework.RandomString(4)
err := tf.ContextCmd.CreateContextWithEndPointStaging(ctxName, clusterInfo.EndPoint)
_, err := tf.ContextCmd.CreateContextWithEndPointStaging(ctxName, clusterInfo.EndPoint)
Expect(err).To(BeNil(), "context should create without any error")
Expect(framework.IsContextExists(tf, ctxName)).To(BeTrue(), fmt.Sprintf(ContextShouldExistsAsCreated, ctxName))
contextNames = append(contextNames, ctxName)
})
// Test case: c. (negative test) Create context for TMC target with TMC cluster "incorrect" URL as endpoint
It("create tmc context with incorrect endpoint", func() {
ctxName := ContextNamePrefix + framework.RandomString(4)
err := tf.ContextCmd.CreateContextWithEndPointStaging(ctxName, framework.RandomString(4))
_, err := tf.ContextCmd.CreateContextWithEndPointStaging(ctxName, framework.RandomString(4))
Expect(err).ToNot(BeNil())
Expect(strings.Contains(err.Error(), framework.FailedToCreateContext)).To(BeTrue())
Expect(framework.IsContextExists(tf, ctxName)).To(BeFalse(), fmt.Sprintf(ContextShouldNotExists, ctxName))
Expand All @@ -80,7 +80,7 @@ var _ = framework.CLICoreDescribe("[Tests:E2E][Feature:Context-lifecycle-tmc]",
It("create tmc context with endpoint and with incorrect api token", func() {
os.Setenv(framework.TanzuAPIToken, framework.RandomString(4))
ctxName := framework.RandomString(4)
err := tf.ContextCmd.CreateContextWithEndPointStaging(ctxName, clusterInfo.EndPoint)
_, err := tf.ContextCmd.CreateContextWithEndPointStaging(ctxName, clusterInfo.EndPoint)
os.Setenv(framework.TanzuAPIToken, clusterInfo.APIKey)
Expect(err).ToNot(BeNil())
Expect(strings.Contains(err.Error(), framework.FailedToCreateContext)).To(BeTrue())
Expand All @@ -89,7 +89,7 @@ var _ = framework.CLICoreDescribe("[Tests:E2E][Feature:Context-lifecycle-tmc]",
// Test case: e. Create context for TMC target with TMC cluster URL as endpoint, and validate the active context, should be recently create context
It("create tmc context with endpoint and check active context", func() {
ctxName := ContextNamePrefix + framework.RandomString(4)
err := tf.ContextCmd.CreateContextWithEndPointStaging(ctxName, clusterInfo.EndPoint)
_, err := tf.ContextCmd.CreateContextWithEndPointStaging(ctxName, clusterInfo.EndPoint)
Expect(err).To(BeNil(), "context should create without any error")
Expect(framework.IsContextExists(tf, ctxName)).To(BeTrue(), fmt.Sprintf(ContextShouldExistsAsCreated, ctxName))
contextNames = append(contextNames, ctxName)
Expand Down
9 changes: 5 additions & 4 deletions test/e2e/framework/context_create_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ type ContextCreateOps interface {
// CreateContextWithEndPoint creates a context with a given endpoint URL
CreateContextWithEndPoint(contextName, endpoint string) error
// CreateContextWithEndPointStaging creates a context with a given endpoint URL for staging
CreateContextWithEndPointStaging(contextName, endpoint string) error
// returns stdout and error
CreateContextWithEndPointStaging(contextName, endpoint string) (string, error)
// CreateContextWithKubeconfig creates a context with the given kubeconfig file path and a context from the kubeconfig file
CreateContextWithKubeconfig(contextName, kubeconfigPath, kubeContext string) error
// CreateContextWithDefaultKubeconfig creates a context with the default kubeconfig file and a given input context name if it exists in the default kubeconfig file
Expand Down Expand Up @@ -45,15 +46,15 @@ func (cc *contextCreateOps) CreateContextWithEndPoint(contextName, endpoint stri
return err
}

func (cc *contextCreateOps) CreateContextWithEndPointStaging(contextName, endpoint string) error {
func (cc *contextCreateOps) CreateContextWithEndPointStaging(contextName, endpoint string) (string, error) {
createContextCmd := fmt.Sprintf(CreateContextWithEndPointStaging, endpoint, contextName)
out, _, err := cc.cmdExe.Exec(createContextCmd)
if err != nil {
log.Info(fmt.Sprintf(FailedToCreateContextWithStdout, out.String()))
return errors.Wrap(err, fmt.Sprintf(FailedToCreateContextWithStdout, out.String()))
return out.String(), errors.Wrap(err, fmt.Sprintf(FailedToCreateContextWithStdout, out.String()))
}
log.Infof(ContextCreated, contextName)
return err
return out.String(), err
}

func (cc *contextCreateOps) CreateContextWithKubeconfig(contextName, kubeconfigPath, kubeContext string) error {
Expand Down
1 change: 1 addition & 0 deletions test/e2e/framework/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ func CLICoreDescribe(text string, body func()) bool {

// Framework has all helper functions to write CLI e2e test cases
type Framework struct {
Exec CmdOps
CliOps
Config ConfigLifecycleOps
KindCluster ClusterOps
Expand Down
10 changes: 10 additions & 0 deletions test/e2e/framework/output_handling.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,13 @@ type Server struct {
Path string `json:"path"`
Type string `json:"type"`
}

type TMCPluginsCRD struct {
PluginsInfo struct {
Plugins []struct {
Name string `yaml:"name"`
Description string `yaml:"description"`
RecommendedVersion string `yaml:"recommendedVersion"`
} `yaml:"plugins"`
} `yaml:"pluginsInfo"`
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright 2023 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

// pluginsynce2e provides plugin sync command specific E2E test cases
package pluginsynce2e
// pluginsynce2ek8s provides plugin sync command specific E2E test cases
package pluginsynce2ek8s

import (
"fmt"
Expand All @@ -19,7 +19,7 @@ import (

func TestPluginSyncLifecycle(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Plugin-Sync-Lifecycle E2E Test Suite")
RunSpecs(t, "Plugin-Sync-k8s-Lifecycle E2E Test Suite")
}

var (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright 2023 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

// pluginsynce2e provides plugin sync command specific E2E test cases
package pluginsynce2e
// pluginsynce2ek8s provides plugin sync command specific E2E test cases
package pluginsynce2ek8s

import (
"fmt"
Expand Down
3 changes: 3 additions & 0 deletions test/e2e/plugin_sync/tmc/config/routes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"/v1alpha1/system/binaries/plugins": "/plugins"
}
137 changes: 137 additions & 0 deletions test/e2e/plugin_sync/tmc/plugin_sync_tmc_lifecycle_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Copyright 2023 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

// pluginsynce2etmc provides plugin sync command specific E2E test cases
package pluginsynce2etmc

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/vmware-tanzu/tanzu-cli/test/e2e/framework"
helper "github.com/vmware-tanzu/tanzu-cli/test/e2e/plugin_lifecycle"
)

func TestPluginSyncLifecycle(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Plugin-Sync-TMC-Lifecycle E2E Test Suite")
}

var (
tf *framework.Framework
e2eTestLocalCentralRepoURL string
pluginsSearchList []*framework.PluginInfo
pluginGroups []*framework.PluginGroup
pluginGroupToPluginListMap map[string][]*framework.PluginInfo
routesFilePath, serverFilePath, tmcConfigDir string
)

const routesMapping = "{\"/v1alpha1/system/binaries/plugins\": \"/pluginsInfo\"}"
const routesFile = "routes.json"
const serverFile = "server.json"
const CRDFilePath = "../framework/config/cli.tanzu.vmware.com_cliplugins.yaml"
const numberOfPluginsToInstall = 3
const startRestAPIMockServerCMD = "docker run --init --rm -d -p 80:80 --name %s -v %s:/data/ clue/json-server %s --routes %s"
const stopRestAPIMockServerCMD = "docker container stop %s"
const restAPIMockContainerName = "restAPIMockServer"
const ContextNamePrefix = "tmc-sync-"
const mockServerEndpoint = "http://localhost:80"

// BeforeSuite initializes and set up the environment to execute the plugin life cycle and plugin group life cycle end-to-end test cases
var _ = BeforeSuite(func() {
tf = framework.NewFramework()

// remove this
os.Setenv(framework.TanzuAPIToken, "8bczHt0X4G5rPoVzFnkgM8p5s7YE5FngQ9sqCqc6UrQWBTGMyPP_Jsu6k7xATffV")
os.Setenv(framework.TanzuCliE2ETestLocalCentralRepositoryURL, "localhost:9876/tanzu-cli/plugins/central:small")
os.Setenv("TANZU_CLI_PLUGIN_DISCOVERY_IMAGE_SIGNATURE_VERIFICATION_SKIP_LIST", "localhost:9876/tanzu-cli/plugins/central:small")

// check E2E test central repo URL (TANZU_CLI_E2E_TEST_LOCAL_CENTRAL_REPO_URL)
e2eTestLocalCentralRepoURL = os.Getenv(framework.TanzuCliE2ETestLocalCentralRepositoryURL)
Expect(e2eTestLocalCentralRepoURL).NotTo(BeEmpty(), fmt.Sprintf("environment variable %s should set with local central repository URL", framework.TanzuCliE2ETestLocalCentralRepositoryURL))
// set E2E test central repo URL to TANZU_CLI_PRE_RELEASE_REPO_IMAGE
os.Setenv(framework.CentralRepositoryPreReleaseRepoImage, e2eTestLocalCentralRepoURL)
// check
Expect(os.Getenv(framework.TanzuAPIToken)).NotTo(BeEmpty(), fmt.Sprintf("environment variable %s should set with TMC API Token", framework.TanzuAPIToken))

pwd, err := os.Getwd()
Expect(err).To(BeNil(), "should not get any error for getting working directory")
tmcConfigDir = filepath.Join(pwd, "config")
routesFilePath = filepath.Join(tmcConfigDir, routesFile)
serverFilePath = filepath.Join(tmcConfigDir, serverFile)

/*
tmcConfigDir = filepath.Join(framework.FullPathForTempDir, "tmc")
_ = framework.CreateDir(tmcConfigDir)
*/

// search plugin groups and make sure there plugin groups available
pluginGroups = helper.SearchAllPluginGroups(tf)

// check all required plugin groups (framework.PluginGroupsForLifeCycleTests) need for life cycle test are available in plugin group search output
Expect(framework.IsAllPluginGroupsExists(pluginGroups, framework.PluginGroupsForLifeCycleTests)).Should(BeTrue(), "all required plugin groups for life cycle tests should exists in plugin group search output")

// search plugins and make sure there are plugins available
pluginsSearchList = helper.SearchAllPlugins(tf)
Expect(len(pluginsSearchList)).Should(BeNumerically(">", 0))

// check all required plugins (framework.PluginsForLifeCycleTests) for plugin life cycle e2e are available in plugin search output
framework.CheckAllPluginsExists(pluginsSearchList, framework.PluginsForLifeCycleTests)

pluginGroupToPluginListMap = framework.MapPluginsToPluginGroups(pluginsSearchList, framework.PluginGroupsForLifeCycleTests)
for pluginGroupLatest := range framework.PluginGroupsLatestToOldVersions {
framework.CopyPluginsBetweenPluginGroupsAndUpdatePluginsVersion(pluginGroupToPluginListMap, pluginGroupLatest, framework.PluginGroupsLatestToOldVersions[pluginGroupLatest], strings.Split(framework.PluginGroupsLatestToOldVersions[pluginGroupLatest], "/")[1])
}

// check for every plugin group (in framework.PluginGroupsForLifeCycleTests) there should be plugins available
for pg := range pluginGroupToPluginListMap {
Expect(len(pluginGroupToPluginListMap[pg])).Should(BeNumerically(">", 0), "there should be at least one plugin available for each plugin group in plugin group life cycle list")
}
})

// AfterSuite deletes the temp directory created during test cases execution
var _ = AfterSuite(func() {
err := os.RemoveAll(framework.FullPathForTempDir) // delete an entire directory
Expect(err).To(BeNil(), "should not get any error while deleting temp directory")
})

func ApplyConfigOnKindCluster(tf *framework.Framework, clusterInfo *framework.ClusterInfo, confFilePaths []string) {
for _, pluginCRFilePaths := range confFilePaths {
err := tf.KindCluster.ApplyConfig(clusterInfo.ClusterKubeContext, pluginCRFilePaths)
Expect(err).To(BeNil(), "should not get any error for config apply")
}
}

func WriteToFileInJSONFormat(crd framework.TMCPluginsCRD, filePath string) error {
content, err := json.Marshal(crd)
if err != nil {
return err
}
err = ioutil.WriteFile(filePath, content, 0644)
if err != nil {
return err
}
return nil
}

func StartRESTAPIMockServer(tf *framework.Framework, configPath, serverFileName, routesFileName, containerName string) error {
err := StopRestAPIMockServer(tf, containerName)
//const startRestAPIMockServerCMD = "docker run --init --rm -d -p 80:80 --name %s -v %s:/data/ clue/json-server %s --routes %s"
cmd := fmt.Sprintf(startRestAPIMockServerCMD, containerName, configPath, serverFileName, routesFileName)
_, _, err = tf.Exec.Exec(cmd)
return err
}

func StopRestAPIMockServer(tf *framework.Framework, containerName string) error {
cmd := fmt.Sprintf(stopRestAPIMockServerCMD, containerName)
_, _, err := tf.Exec.Exec(cmd)
return err
}
Loading

0 comments on commit 4a79f01

Please sign in to comment.