Skip to content

Commit

Permalink
E2E: E2E Home Dir updates (#593)
Browse files Browse the repository at this point in the history
  • Loading branch information
chandrareddyp authored Nov 4, 2023
1 parent 7a1cc11 commit 60a0903
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 88 deletions.
2 changes: 1 addition & 1 deletion test/e2e/config/config_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ var _ = BeforeSuite(func() {
// AfterSuite deletes the KIND which is created in BeforeSuite
var _ = AfterSuite(func() {
// delete the KIND cluster which was created in the suite setup
_, err := tf.KindCluster.DeleteCluster(clusterInfo.Name)
_, _, err := tf.KindCluster.DeleteCluster(clusterInfo.Name)
Expect(err).To(BeNil(), "kind cluster should be deleted without any error")
})
2 changes: 1 addition & 1 deletion test/e2e/context/k8s/context_k8s_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ var _ = BeforeSuite(func() {
// AfterSuite deletes the KIND cluster created in BeforeSuite
var _ = AfterSuite(func() {
// delete the KIND cluster which was created in the suite setup
_, err := tf.KindCluster.DeleteCluster(clusterInfo.Name)
_, _, err := tf.KindCluster.DeleteCluster(clusterInfo.Name)
Expect(err).To(BeNil(), "kind cluster should be deleted without any error")
})
2 changes: 1 addition & 1 deletion test/e2e/context/tmc/context_tmc_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ var _ = BeforeSuite(func() {
// AfterSuite deletes the KIND cluster created in BeforeSuite
var _ = AfterSuite(func() {
// delete the KIND cluster which was created in the suite setup
_, err := tf.KindCluster.DeleteCluster(k8sClusterInfo.Name)
_, _, err := tf.KindCluster.DeleteCluster(k8sClusterInfo.Name)
Expect(err).To(BeNil(), "kind cluster should be deleted without any error")
})
36 changes: 17 additions & 19 deletions test/e2e/framework/cluster_container_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@

package framework

import "fmt"

// ContainerRuntime has operations to perform on container runtime
type ContainerRuntime interface {
StartContainerRuntime() (output string, err error)
ContainerRuntimeStatus() (status string, err error)
StopContainerRuntime() (output string, err error)
StartContainerRuntime() (stdOut, stdErr string, err error)
ContainerRuntimeStatus() (stdOut, stdErr string, err error)
StopContainerRuntime() (stdOut, stdErr string, err error)
}

// Docker is the container runtime of type docker
Expand All @@ -29,31 +27,31 @@ func NewDocker() Docker {
}

// StartContainerRuntime starts docker daemon if not already running
func (dc *docker) StartContainerRuntime() (output string, err error) {
if status, err := dc.ContainerRuntimeStatus(); err == nil {
return status, nil
func (dc *docker) StartContainerRuntime() (stdOut, stdErr string, err error) {
if so, sb, err := dc.ContainerRuntimeStatus(); err == nil {
return so, sb, nil
}
stdOut, stdErr, err := dc.Exec(StartDockerUbuntu)
stdOutBuff, stdErrBuff, err := dc.Exec(StartDockerUbuntu)
if err != nil {
return stdOut.String(), fmt.Errorf(stdErr.String(), err)
return stdOutBuff.String(), stdErrBuff.String(), err
}
return stdOut.String(), err
return stdOutBuff.String(), stdErrBuff.String(), err
}

// ContainerRuntimeStatus returns docker daemon daemon status
func (dc *docker) ContainerRuntimeStatus() (status string, err error) {
stdOut, stdErr, err := dc.Exec(DockerInfo)
func (dc *docker) ContainerRuntimeStatus() (stdOut, stdErr string, err error) {
stdOutBuff, stdErrBuff, err := dc.Exec(DockerInfo)
if err != nil {
return stdOut.String(), fmt.Errorf(stdErr.String(), err)
return stdOutBuff.String(), stdErrBuff.String(), err
}
return stdOut.String(), err
return stdOutBuff.String(), stdErrBuff.String(), err
}

// StopContainerRuntime returns docker daemon daemon status
func (dc *docker) StopContainerRuntime() (output string, err error) {
stdOut, stdErr, err := dc.Exec(StopDockerUbuntu)
func (dc *docker) StopContainerRuntime() (stdOut, stdErr string, err error) {
stdOutBuff, stdErrBuff, err := dc.Exec(StopDockerUbuntu)
if err != nil {
return stdOut.String(), fmt.Errorf(stdErr.String(), err)
return stdOutBuff.String(), stdErrBuff.String(), err
}
return stdOut.String(), err
return stdOutBuff.String(), stdErrBuff.String(), err
}
10 changes: 5 additions & 5 deletions test/e2e/framework/cluster_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ package framework
// ClusterOps has helper operations to perform on cluster
type ClusterOps interface {
// CreateCluster creates the cluster with given name
CreateCluster(clusterName string) (output string, err error)
CreateCluster(clusterName string) (stdOut, stdErr string, err error)
// DeleteCluster deletes the cluster with given name
DeleteCluster(clusterName string) (output string, err error)
DeleteCluster(clusterName string) (stdOut, stdErr string, err error)
// ClusterStatus checks the status of the cluster for given cluster name
ClusterStatus(clusterName string) (output string, err error)
ClusterStatus(clusterName string) (stdOut, stdErr string, err error)
// GetClusterEndpoint returns the cluster endpoint for the given cluster name
GetClusterEndpoint(clusterName string) (endpoint string, err error)
GetClusterEndpoint(clusterName string) (endpoint, stdOut, stdErr string, err error)
// GetClusterContext returns the given cluster kubeconfig context
GetClusterContext(clusterName string) string
// GetKubeconfigPath returns the default kubeconfig path
GetKubeconfigPath() string
// ApplyConfig applies the given configFilePath on to the given contextName cluster context
ApplyConfig(contextName, configFilePath string) error
ApplyConfig(contextName, configFilePath string) (stdOut, stdErr string, err error)
}

// ClusterInfo holds the general cluster details
Expand Down
73 changes: 28 additions & 45 deletions test/e2e/framework/cluster_kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ package framework
import (
"fmt"
"os"
"path/filepath"

"github.com/pkg/errors"

"github.com/vmware-tanzu/tanzu-plugin-runtime/log"

"gopkg.in/yaml.v3"
configapi "k8s.io/client-go/tools/clientcmd/api/v1"
)
Expand All @@ -33,91 +32,75 @@ func NewKindCluster(docker Docker) KindCluster {
}
}

// CreateCluster creates kind cluster with given name and returns stdout info
// if container runtime not running or any error then returns stdout and error info
func (kc *kindCluster) CreateCluster(kindClusterName string) (output string, err error) {
stdOut, err := kc.ContainerRuntimeStatus()
// CreateCluster creates kind cluster with given cluster name
func (kc *kindCluster) CreateCluster(kindClusterName string) (stdOut, stdErr string, err error) {
stdOut, stdErr, err = kc.ContainerRuntimeStatus()
if err != nil {
return stdOut, err
return stdOut, stdErr, err
}
createCmd := fmt.Sprintf(KindClusterCreate, kindClusterName)
stdOutBuffer, stdErrBuffer, err := kc.Exec(createCmd)
if err != nil {
return stdOutBuffer.String(), fmt.Errorf(stdErrBuffer.String(), err)
}
return stdOutBuffer.String(), err
return stdOutBuffer.String(), stdErrBuffer.String(), err
}

// DeleteCluster creates kind cluster with given name and returns stdout info
// if container runtime not running or any error then returns stdout and error info
func (kc *kindCluster) DeleteCluster(kindClusterName string) (output string, err error) {
stdOut, err := kc.ContainerRuntimeStatus()
// DeleteCluster deletes given kind cluster
func (kc *kindCluster) DeleteCluster(kindClusterName string) (stdOut, stdErr string, err error) {
stdOut, stdErr, err = kc.ContainerRuntimeStatus()
if err != nil {
return stdOut, err
return stdOut, stdErr, err
}
delCmd := fmt.Sprintf(KindClusterDelete, kindClusterName)
stdOutBuffer, stdErrBuffer, err := kc.Exec(delCmd)
if err != nil {
return stdOutBuffer.String(), fmt.Errorf(stdErrBuffer.String(), err)
}
return stdOutBuffer.String(), err
return stdOutBuffer.String(), stdErrBuffer.String(), err
}

// ClusterStatus checks given kind cluster status and returns stdout info
// if container runtime not running or any error then returns stdout and error info
func (kc *kindCluster) ClusterStatus(kindClusterName string) (output string, err error) {
stdOut, err := kc.ContainerRuntimeStatus()
// ClusterStatus checks given kind cluster status
func (kc *kindCluster) ClusterStatus(kindClusterName string) (stdOut, stdErr string, err error) {
stdOut, stdErr, err = kc.ContainerRuntimeStatus()
if err != nil {
return stdOut, err
return stdOut, stdErr, err
}
statusCmd := fmt.Sprintf(KindClusterStatus, kc.GetClusterContext(kindClusterName))
stdOutBuffer, stdErrBuffer, err := kc.Exec(statusCmd)
if err != nil {
return stdOutBuffer.String(), fmt.Errorf(stdErrBuffer.String(), err)
}
return stdOutBuffer.String(), err
return stdOutBuffer.String(), stdErrBuffer.String(), err
}

func (kc *kindCluster) ApplyConfig(contextName, configFilePath string) error {
// ApplyConfig applies given config file on to the given kind cluster context
func (kc *kindCluster) ApplyConfig(contextName, configFilePath string) (stdOut, stdErr string, err error) {
applyCmd := fmt.Sprintf(KubectlApply, contextName, configFilePath)
stdOut, stdErr, err := kc.CmdOps.Exec(applyCmd)
if err != nil {
log.Errorf(ErrorLogForCommandWithErrStdErrAndStdOut, applyCmd, err.Error(), stdErr.String(), stdOut.String())
return err
}
log.Infof("the config:%s applied successfully to context:%s", configFilePath, contextName)
return err
stdOutBuff, stdErrBuff, err := kc.CmdOps.Exec(applyCmd)
return stdOutBuff.String(), stdErrBuff.String(), err
}

// GetClusterEndpoint returns given kind cluster control plane endpoint
func (kc *kindCluster) GetClusterEndpoint(kindClusterName string) (endpoint string, err error) {
stdOut, err := kc.ContainerRuntimeStatus()
func (kc *kindCluster) GetClusterEndpoint(kindClusterName string) (endpoint, stdOut, stdErr string, err error) {
stdOut, stdErr, err = kc.ContainerRuntimeStatus()
if err != nil {
return stdOut, err
return "", stdOut, stdErr, err
}
path := kc.GetKubeconfigPath()
file, err := os.ReadFile(path)
if err != nil {
return "", err
return "", "", "", err
}
var conf *configapi.Config
err = yaml.Unmarshal(file, &conf)
if err != nil {
return "", errors.Wrap(err, "failed to construct yaml node from kubeconfig file")
return "", "", "", errors.Wrap(err, "failed to construct yaml node from kubeconfig file")
}
ctx := kc.GetClusterContext(kindClusterName)
for i := range conf.Clusters {
if conf.Clusters[i].Name == ctx {
return conf.Clusters[i].Cluster.Server, nil
return conf.Clusters[i].Cluster.Server, "", "", nil
}
}
return "", errors.Errorf("the '%s' kubeconfig file does not have context '%s' details", path, ctx)
return "", "", "", errors.Errorf("the '%s' kubeconfig file does not have context '%s' details", path, ctx)
}

func (kc *kindCluster) GetClusterContext(kindClusterName string) string {
return "kind-" + kindClusterName
}

func (kc *kindCluster) GetKubeconfigPath() string {
return GetHomeDir() + "/.kube/config"
return filepath.Join(GetE2EHomeDir(), ".kube", "config")
}
4 changes: 4 additions & 0 deletions test/e2e/framework/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ var (
// OriginalHomeDir is the actual HOME directory of the machine before E2E test overwrites it
OriginalHomeDir string

// TestHomeDir is the HOME directory during E2E test execution
TestHomeDir string

// ConfigFilePath represents config.yaml file path which under $HOME/.tanzu-cli-e2e/.config/tanzu
ConfigFilePath string
// ConfigFilePath represents config-ng.yaml file path which under $HOME/.tanzu-cli-e2e/.config/tanzu
Expand Down Expand Up @@ -91,6 +94,7 @@ func init() {
FullPathForTempDir = filepath.Join(TestDirPath, TempDirInTestDirPath)
// Update $HOME as $HOME/.tanzu-cli-e2e
os.Setenv("HOME", TestDirPath)
TestHomeDir = TestDirPath
TestPluginsDirPath = filepath.Join(TestDirPath, TestPluginsDir)
TanzuFolderPath = filepath.Join(filepath.Join(TestDirPath, ConfigFolder), TanzuFolder)
ConfigFilePath = filepath.Join(TanzuFolderPath, ConfigFile)
Expand Down
20 changes: 17 additions & 3 deletions test/e2e/framework/framework_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@ func RandomNumber(length int) string {
return string(b)
}

func GetE2EHomeDir() string {
if TestHomeDir == "" {
currentHomeDir := GetHomeDir()
if strings.HasSuffix(currentHomeDir, TestDir) {
return currentHomeDir
}
TestHomeDir = filepath.Join(TestHomeDir, TestDir)
return TestHomeDir
}
return TestHomeDir
}

func GetHomeDir() string {
home, err := os.UserHomeDir()
if err != nil {
Expand Down Expand Up @@ -181,12 +193,14 @@ func GetMapKeys[K string, V *PluginInfo](m map[K][]V) []*K {
// CreateKindCluster create the k8s KIND cluster in the local Docker environment
func CreateKindCluster(tf *Framework, name string) (*ClusterInfo, error) {
ci := &ClusterInfo{Name: name}
_, err := tf.KindCluster.CreateCluster(name)
stdOut, stdErr, err := tf.KindCluster.CreateCluster(name)
if err != nil {
log.Errorf("kind cluster creation failed, stdOut:%s, stdErr:%s", stdOut, stdErr)
return nil, errors.Wrapf(err, "error while creating kind cluster: %s", name)
}
endpoint, err := tf.KindCluster.GetClusterEndpoint(name)
endpoint, stdOut, stdErr, err := tf.KindCluster.GetClusterEndpoint(name)
if err != nil {
log.Errorf("error while getting kind cluster status stdOut:%s, stdErr:%s", stdOut, stdErr)
return nil, errors.Wrapf(err, "error while getting kind cluster %s endpoint", name)
}
ci.EndPoint = endpoint
Expand Down Expand Up @@ -505,7 +519,7 @@ func UpdatePluginDiscoverySource(tf *Framework, repoURL string) error {
// ApplyConfigOnKindCluster applies the config files on kind cluster
func ApplyConfigOnKindCluster(tf *Framework, clusterInfo *ClusterInfo, confFilePaths []string) error {
for _, pluginCRFilePaths := range confFilePaths {
err := tf.KindCluster.ApplyConfig(clusterInfo.ClusterKubeContext, pluginCRFilePaths)
_, _, err := tf.KindCluster.ApplyConfig(clusterInfo.ClusterKubeContext, pluginCRFilePaths)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/framework/plugin_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func NewLocalOCIRegistry(registryName, port string) PluginRegistry {
}

func (rep *localOCIRegistry) StartRegistry() (url string, err error) {
if _, err := rep.docker.ContainerRuntimeStatus(); err != nil {
out, err := rep.docker.StartContainerRuntime()
if _, _, err := rep.docker.ContainerRuntimeStatus(); err != nil {
out, _, err := rep.docker.StartContainerRuntime()
if err != nil {
return out, err
}
Expand Down
14 changes: 7 additions & 7 deletions test/e2e/plugin_sync/k8s/plugin_sync_k8s_lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ var _ = f.CLICoreDescribe("[Tests:E2E][Feature:Plugin-sync-lifecycle]", func() {
It("delete current context and the KIND cluster", func() {
_, _, err = tf.ContextCmd.DeleteContext(contextName)
Expect(err).To(BeNil(), "context should be deleted without error")
_, err := tf.KindCluster.DeleteCluster(clusterInfo.Name)
_, _, err := tf.KindCluster.DeleteCluster(clusterInfo.Name)
Expect(err).To(BeNil(), "kind cluster should be deleted without any error")
})
})
Expand Down Expand Up @@ -214,7 +214,7 @@ var _ = f.CLICoreDescribe("[Tests:E2E][Feature:Plugin-sync-lifecycle]", func() {
for i := range installedPluginsList {
Expect(stdOut).To(ContainSubstring(fmt.Sprintf(f.DeactivatingPlugin, installedPluginsList[i].Name, installedPluginsList[i].Version, contextName)))
}
_, err := tf.KindCluster.DeleteCluster(clusterInfo.Name)
_, _, err := tf.KindCluster.DeleteCluster(clusterInfo.Name)
Expect(err).To(BeNil(), "kind cluster should be deleted without any error")
})
})
Expand Down Expand Up @@ -324,7 +324,7 @@ var _ = f.CLICoreDescribe("[Tests:E2E][Feature:Plugin-sync-lifecycle]", func() {
It("delete the KIND cluster", func() {
_, _, err = tf.ContextCmd.DeleteContext(contextName)
Expect(err).To(BeNil(), "context should be deleted without error")
_, err := tf.KindCluster.DeleteCluster(clusterInfo.Name)
_, _, err := tf.KindCluster.DeleteCluster(clusterInfo.Name)
Expect(err).To(BeNil(), "kind cluster should be deleted without any error")
})
})
Expand Down Expand Up @@ -386,7 +386,7 @@ var _ = f.CLICoreDescribe("[Tests:E2E][Feature:Plugin-sync-lifecycle]", func() {
// Test case: e. delete the KIND cluster
It("delete the KIND cluster", func() {
Expect(err).To(BeNil(), "context should be deleted without error")
_, err := tf.KindCluster.DeleteCluster(clusterInfo.Name)
_, _, err := tf.KindCluster.DeleteCluster(clusterInfo.Name)
Expect(err).To(BeNil(), "kind cluster should be deleted without any error")
})
})
Expand Down Expand Up @@ -483,9 +483,9 @@ var _ = f.CLICoreDescribe("[Tests:E2E][Feature:Plugin-sync-lifecycle]", func() {
Expect(err).To(BeNil(), "context should be deleted without error")
_, _, err = tf.ContextCmd.DeleteContext(contextNameClusterTwo)
Expect(err).To(BeNil(), "context should be deleted without error")
_, err = tf.KindCluster.DeleteCluster(clusterOne.Name)
_, _, err = tf.KindCluster.DeleteCluster(clusterOne.Name)
Expect(err).To(BeNil(), "kind cluster should be deleted without any error")
_, err = tf.KindCluster.DeleteCluster(clusterTwo.Name)
_, _, err = tf.KindCluster.DeleteCluster(clusterTwo.Name)
Expect(err).To(BeNil(), "kind cluster should be deleted without any error")
})
})
Expand Down Expand Up @@ -557,7 +557,7 @@ var _ = f.CLICoreDescribe("[Tests:E2E][Feature:Plugin-sync-lifecycle]", func() {
It("delete the KIND cluster", func() {
_, _, err = tf.ContextCmd.DeleteContext(contextName)
Expect(err).To(BeNil(), "context should be deleted without error")
_, err := tf.KindCluster.DeleteCluster(clusterInfo.Name)
_, _, err := tf.KindCluster.DeleteCluster(clusterInfo.Name)
Expect(err).To(BeNil(), "kind cluster should be deleted without any error")
})
})
Expand Down
Loading

0 comments on commit 60a0903

Please sign in to comment.