Skip to content

Commit

Permalink
config and context test cases update (vmware-tanzu#130)
Browse files Browse the repository at this point in the history
Co-authored-by: Chandra Pamuluri <[email protected]>
  • Loading branch information
chandrareddyp and Chandra Pamuluri authored Mar 23, 2023
1 parent b7f962b commit 89ddb6d
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,25 @@ const TRUE = "true"
// tests `tanzu config set` and `tanzu config unset` commands
var _ = framework.CLICoreDescribe("[Tests:E2E][Feature:config]", func() {
var (
tf *framework.Framework
randomFeatureFlag string
)
BeforeEach(func() {
tf = framework.NewFramework()
})
Context("config feature flag operations", func() {
When("new config flag set with value", func() {
It("should set flag and unset flag successfully", func() {
flagName := "e2e-test-" + framework.RandomString(4)
randomFeatureFlagPath := "features.global." + flagName
flagVal := TRUE
// Set random feature flag
err := tf.Config.ConfigSetFeatureFlag(randomFeatureFlagPath, flagVal)
Expect(err).To(BeNil())

// Validate the value of random feature flag set in previous step
val, err := tf.Config.ConfigGetFeatureFlag(randomFeatureFlagPath)
Expect(err).To(BeNil())
Expect(val).Should(Equal(TRUE))

// Unset random feature flag which was set in previous step
err = tf.Config.ConfigUnsetFeature(randomFeatureFlagPath)
Expect(err).To(BeNil())

// Validate the unset random feature flag in previous step
val, err = tf.Config.ConfigGetFeatureFlag(randomFeatureFlagPath)
Expect(err).To(BeNil())
Expect(val).Should(Equal(""))
Expand Down
32 changes: 15 additions & 17 deletions test/e2e/config/config_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,27 @@ const ContextNameConfigPrefix = "config-k8s-"
// As part of this suite, create a KIND cluster, and creates context's
// tests the 'tanzu config server list' and 'tanzu config server delete' commands
var _ = framework.CLICoreDescribe("[Tests:E2E][Feature:Config-Server]", func() {
var (
tf *framework.Framework
clusterInfo *framework.ClusterInfo
contextNames []string
)

BeforeSuite(func() {
tf = framework.NewFramework()
// Create KIND cluster, which is used in test cases to create server's/context's
clusterInfo = context.CreateKindCluster(tf, "config-e2e-"+framework.RandomNumber(4))
contextNames = make([]string, 0)
})
Context("tanzu config server command test cases ", func() {
// Test case: delete servers if any exists, with command 'tanzu config server delete'
It("list and delete servers if any exists before running tests", func() {
By("delete servers if any exists before running tests")
list, err := tf.Config.ConfigServerList()
Expect(err).To(BeNil(), "server list should not return any error")
for _, ctx := range list {
err := tf.Config.ConfigServerDelete(ctx.Name)
Expect(err).To(BeNil(), "delete server should delete server without any error")
}
list, err = tf.Config.ConfigServerList()
Expect(err).To(BeNil(), "server list should not return any error")
Expect(len(list)).To(Equal(0), "all servers should be deleted")
})
// Test case: Create context for k8s target with kubeconfig and its context as input
It("create context with kubeconfig and context", func() {
By("create context with kubeconfig and context")
ctxName := ContextNameConfigPrefix + framework.RandomString(4)
err := tf.ContextCmd.CreateConextWithKubeconfig(ctxName, clusterInfo.KubeConfigPath, clusterInfo.ClusterContext)
Expect(err).To(BeNil(), "context should create without any error")
Expect(context.IsContextExists(tf, ctxName)).To(BeTrue(), "context should be available")
contextNames = append(contextNames, ctxName)
})
// Test case: Create context for k8s target with "default" kubeconfig and its context only as input value
Expand All @@ -45,6 +47,7 @@ var _ = framework.CLICoreDescribe("[Tests:E2E][Feature:Config-Server]", func() {
ctxName := "context-defaultConfig-" + framework.RandomString(4)
err := tf.ContextCmd.CreateContextWithDefaultKubeconfig(ctxName, clusterInfo.ClusterContext)
Expect(err).To(BeNil(), "context should create without any error")
Expect(context.IsContextExists(tf, ctxName)).To(BeTrue(), "context should be available")
contextNames = append(contextNames, ctxName)
})
// Test case: test 'tanzu config server list' command, should list all contexts created as servers
Expand All @@ -71,9 +74,4 @@ var _ = framework.CLICoreDescribe("[Tests:E2E][Feature:Config-Server]", func() {
Expect(err).ToNot(BeNil())
})
})
AfterSuite(func() {
// delete the KIND cluster which was created in the suite setup
_, err := tf.KindCluster.DeleteCluster(clusterInfo.Name)
Expect(err).To(BeNil(), "kind cluster should be deleted without any error")
})
})
25 changes: 25 additions & 0 deletions test/e2e/config/config_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,34 @@ import (

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

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

func TestConfig(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Config Suite")
}

var (
tf *framework.Framework
clusterInfo *framework.ClusterInfo
contextNames []string
)

// BeforeSuite creates KIND cluster needed to test 'tanzu config server' use cases
// initializes the tf
var _ = BeforeSuite(func() {
tf = framework.NewFramework()
// Create KIND cluster, which is used in test cases to create server's/context's
clusterInfo = context.CreateKindCluster(tf, "config-e2e-"+framework.RandomNumber(4))
contextNames = make([]string, 0)
})

// 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)
Expect(err).To(BeNil(), "kind cluster should be deleted without any error")
})
38 changes: 20 additions & 18 deletions test/e2e/context/context_k8s_lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package context

import (
"fmt"
"strings"

. "github.com/onsi/ginkgo"
Expand All @@ -18,25 +19,28 @@ const ContextNameConfigPrefix = "context-config-k8s-"

// Test suite tests the context life cycle use cases for the k8s target
var _ = framework.CLICoreDescribe("[Tests:E2E][Feature:Context-lifecycle-k8s]", func() {
var (
tf *framework.Framework
clusterInfo *framework.ClusterInfo
contextNames []string
)

BeforeSuite(func() {
tf = framework.NewFramework()
// Create KIND cluster, which is used in test cases to create context's
clusterInfo = CreateKindCluster(tf, "context-e2e-"+framework.RandomNumber(4))
contextNames = make([]string, 0)
})
Context("Context lifecycle tests for k8s target", func() {
// Test case: list and delete context's if any exists already, before running test cases.
It("list and delete contexts if any exists already", func() {
By("list and delete contexts if any exists already before running test cases")
list, err := tf.ContextCmd.ListContext()
Expect(err).To(BeNil(), "list context should not return any error")
for _, ctx := range list {
err := tf.ContextCmd.DeleteContext(ctx.Name)
Expect(err).To(BeNil(), "delete context should delete context without any error")
Expect(IsContextExists(tf, ctx.Name)).To(BeFalse(), fmt.Sprintf(ContextShouldNotExists, ctx.Name))
}
list, err = tf.ContextCmd.ListContext()
Expect(err).To(BeNil(), "list context should not return any error")
Expect(len(list)).To(Equal(0), "all contexts should be deleted")
})
// Test case: Create context for k8s target with kubeconfig and its context as input
It("create context with kubeconfig and context", func() {
By("create context with kubeconfig and context")
ctxName := ContextNameConfigPrefix + framework.RandomString(4)
err := tf.ContextCmd.CreateConextWithKubeconfig(ctxName, clusterInfo.KubeConfigPath, clusterInfo.ClusterContext)
Expect(err).To(BeNil(), "context should create without any error")
Expect(IsContextExists(tf, ctxName)).To(BeTrue(), fmt.Sprintf(ContextShouldExistsAsCreated, ctxName))
contextNames = append(contextNames, ctxName)
})
// Test case: (negative test) Create context for k8s target with incorrect kubeconfig file path and its context as input
Expand All @@ -46,6 +50,7 @@ var _ = framework.CLICoreDescribe("[Tests:E2E][Feature:Context-lifecycle-k8s]",
err := tf.ContextCmd.CreateConextWithKubeconfig(ctxName, framework.RandomString(4), clusterInfo.ClusterContext)
Expect(err).ToNot(BeNil())
Expect(strings.Contains(err.Error(), framework.FailedToCreateContext)).To(BeTrue())
Expect(IsContextExists(tf, ctxName)).To(BeFalse(), fmt.Sprintf(ContextShouldNotExists, ctxName))
})
// Test case: (negative test) Create context for k8s target with kubeconfig file path and incorrect context as input
It("create context with kubeconfig and incorrect context", func() {
Expand All @@ -54,13 +59,15 @@ var _ = framework.CLICoreDescribe("[Tests:E2E][Feature:Context-lifecycle-k8s]",
err := tf.ContextCmd.CreateConextWithKubeconfig(ctxName, clusterInfo.KubeConfigPath, framework.RandomString(4))
Expect(err).ToNot(BeNil())
Expect(strings.Contains(err.Error(), framework.FailedToCreateContext)).To(BeTrue())
Expect(IsContextExists(tf, ctxName)).To(BeFalse(), fmt.Sprintf(ContextShouldNotExists, ctxName))
})
// Test case: Create context for k8s target with "default" kubeconfig and its context only as input value
It("create context with kubeconfig and context", func() {
By("create context with kubeconfig and context")
ctxName := "context-defaultConfig-" + framework.RandomString(4)
err := tf.ContextCmd.CreateContextWithDefaultKubeconfig(ctxName, clusterInfo.ClusterContext)
Expect(err).To(BeNil(), "context should create without any error")
Expect(IsContextExists(tf, ctxName)).To(BeTrue(), fmt.Sprintf(ContextShouldExistsAsCreated, ctxName))
contextNames = append(contextNames, ctxName)
active, err := tf.ContextCmd.GetActiveContext(string(types.TargetK8s))
Expect(err).To(BeNil(), "there should be a active context")
Expand Down Expand Up @@ -91,6 +98,7 @@ var _ = framework.CLICoreDescribe("[Tests:E2E][Feature:Context-lifecycle-k8s]",
By("delete all contexts created in previous tests")
for _, ctx := range contextNames {
err := tf.ContextCmd.DeleteContext(ctx)
Expect(IsContextExists(tf, ctx)).To(BeFalse(), fmt.Sprintf(ContextShouldNotExists, ctx))
Expect(err).To(BeNil(), "delete context should delete context without any error")
}
list := GetAvailableContexts(tf, contextNames)
Expand All @@ -103,10 +111,4 @@ var _ = framework.CLICoreDescribe("[Tests:E2E][Feature:Context-lifecycle-k8s]",
Expect(err).ToNot(BeNil())
})
})
AfterSuite(func() {
// delete the KIND cluster which was created in the suite setup
_, err := tf.KindCluster.DeleteCluster(clusterInfo.Name)
Expect(err).To(BeNil(), "kind cluster should be deleted without any error")
})

})
12 changes: 12 additions & 0 deletions test/e2e/context/context_lifecycle_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ func GetAvailableContexts(tf *framework.Framework, contextNames []string) []stri
return available
}

// IsContextExists checks the given context is exists in the config file by listing the existing contexts in the config file
func IsContextExists(tf *framework.Framework, contextName string) bool {
list, err := tf.ContextCmd.ListContext()
gomega.Expect(err).To(gomega.BeNil(), "list context should not return any error")
for _, context := range list {
if context.Name == contextName {
return true
}
}
return false
}

// GetAvailableServers takes list of servers and returns which are available in the 'tanzu config server list' command
func GetAvailableServers(tf *framework.Framework, serverNames []string) []string {
var available []string
Expand Down
26 changes: 26 additions & 0 deletions test/e2e/context/context_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,35 @@ import (

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

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

func TestContext(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Context-K8S Suite")
}

const ContextShouldNotExists = "the context %s should not exists"
const ContextShouldExistsAsCreated = "the context %s should exists as its been created"

var (
tf *framework.Framework
clusterInfo *framework.ClusterInfo
contextNames []string
)

// BeforeSuite created KIND cluster
var _ = BeforeSuite(func() {
tf = framework.NewFramework()
// Create KIND cluster, which is used in test cases to create context's
clusterInfo = CreateKindCluster(tf, "context-e2e-"+framework.RandomNumber(4))
contextNames = make([]string, 0)
})

// 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)
Expect(err).To(BeNil(), "kind cluster should be deleted without any error")
})
24 changes: 23 additions & 1 deletion test/e2e/context/tmc/context_tmc_lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package tmc

import (
"fmt"
"os"
"strings"

Expand All @@ -17,6 +18,8 @@ import (
)

const ContextNamePrefix = "context-endpoint-tmc-"
const ContextShouldNotExists = "the context %s should not exists"
const ContextShouldExistsAsCreated = "the context %s should exists as its been created"

// Test suite tests the context life cycle use cases for the TMC target
var _ = framework.CLICoreDescribe("[Tests:E2E][Feature:Context-lifecycle-tmc]", func() {
Expand All @@ -34,11 +37,25 @@ var _ = framework.CLICoreDescribe("[Tests:E2E][Feature:Context-lifecycle-tmc]",
contextNames = make([]string, 0)
})
Context("Context lifecycle tests for TMC target", func() {
// Test case: list and delete context's if any exists
It("delete context command", func() {
list, err := tf.ContextCmd.ListContext()
Expect(err).To(BeNil(), "list context should not return any error")
for _, ctx := range list {
err := tf.ContextCmd.DeleteContext(ctx.Name)
Expect(context.IsContextExists(tf, ctx.Name)).To(BeFalse(), fmt.Sprintf(ContextShouldNotExists, ctx.Name))
Expect(err).To(BeNil(), "delete context should delete context without any error")
}
list, err = tf.ContextCmd.ListContext()
Expect(err).To(BeNil(), "list context should not return any error")
Expect(len(list)).To(Equal(0), "all contexts should be deleted")
})
// Test case: 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.CreateConextWithEndPointStaging(ctxName, clusterInfo.EndPoint)
Expect(err).To(BeNil(), "context should create without any error")
Expect(context.IsContextExists(tf, ctxName)).To(BeTrue(), fmt.Sprintf(ContextShouldExistsAsCreated, ctxName))
contextNames = append(contextNames, ctxName)
})
// Test case: (negative test) Create context for TMC target with TMC cluster "incorrect" URL as endpoint
Expand All @@ -47,20 +64,24 @@ var _ = framework.CLICoreDescribe("[Tests:E2E][Feature:Context-lifecycle-tmc]",
err := tf.ContextCmd.CreateConextWithEndPointStaging(ctxName, framework.RandomString(4))
Expect(err).ToNot(BeNil())
Expect(strings.Contains(err.Error(), framework.FailedToCreateContext)).To(BeTrue())
Expect(context.IsContextExists(tf, ctxName)).To(BeFalse(), fmt.Sprintf(ContextShouldNotExists, ctxName))
})
// Test case: (negative test) Create context for TMC target with TMC cluster URL as endpoint when api token set as incorrect
It("create tmc context with endpoint and with incorrect api token", func() {
os.Setenv(framework.TanzuAPIToken, framework.RandomString(4))
err := tf.ContextCmd.CreateConextWithEndPointStaging(framework.RandomString(4), clusterInfo.EndPoint)
ctxName := framework.RandomString(4)
err := tf.ContextCmd.CreateConextWithEndPointStaging(ctxName, clusterInfo.EndPoint)
os.Setenv(framework.TanzuAPIToken, clusterInfo.APIKey)
Expect(err).ToNot(BeNil())
Expect(strings.Contains(err.Error(), framework.FailedToCreateContext)).To(BeTrue())
Expect(context.IsContextExists(tf, ctxName)).To(BeFalse(), fmt.Sprintf(ContextShouldNotExists, ctxName))
})
// Test case: 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.CreateConextWithEndPointStaging(ctxName, clusterInfo.EndPoint)
Expect(err).To(BeNil(), "context should create without any error")
Expect(context.IsContextExists(tf, ctxName)).To(BeTrue(), fmt.Sprintf(ContextShouldExistsAsCreated, ctxName))
contextNames = append(contextNames, ctxName)
active, err := tf.ContextCmd.GetActiveContext(string(types.TargetTMC))
Expect(err).To(BeNil(), "there should be a active context")
Expand Down Expand Up @@ -88,6 +109,7 @@ var _ = framework.CLICoreDescribe("[Tests:E2E][Feature:Context-lifecycle-tmc]",
It("delete context command", func() {
for _, ctx := range contextNames {
err := tf.ContextCmd.DeleteContext(ctx)
Expect(context.IsContextExists(tf, ctx)).To(BeFalse(), fmt.Sprintf(ContextShouldNotExists+" as been deleted", ctx))
Expect(err).To(BeNil(), "delete context should delete context without any error")
}
list := context.GetAvailableContexts(tf, contextNames)
Expand Down
11 changes: 7 additions & 4 deletions test/e2e/framework/config_lifecycle_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ func (co *configOps) ConfigServerDelete(serverName string) error {
configDelCmd := fmt.Sprintf(ConfigServerDelete, serverName)
_, _, err := co.cmdExe.Exec(configDelCmd)
if err != nil {
log.Infof("failed to delete config server: %s", serverName)
log.Error(err, "error while running: "+configDelCmd)
} else {
log.Infof(ConfigServerDeleted, serverName)
}
return err
}
Expand All @@ -126,14 +129,14 @@ func (co *configOps) DeleteCLIConfigurationFiles() error {
configFile := filepath.Join(homeDir, ConfigFileDir, ConfigFileName)
_, err := os.Stat(configFile)
if err == nil {
if ferr := os.Remove(configFile); ferr != nil {
return ferr
if fileErr := os.Remove(configFile); fileErr != nil {
return fileErr
}
}
configNGFile := filepath.Join(homeDir, ConfigFileDir, ConfigNGFileName)
if _, err := os.Stat(configNGFile); err == nil {
if ferr := os.Remove(configNGFile); ferr != nil {
return ferr
if fileErr := os.Remove(configNGFile); fileErr != nil {
return fileErr
}
}
return nil
Expand Down
Loading

0 comments on commit 89ddb6d

Please sign in to comment.