Skip to content

Commit

Permalink
Fix e2e tests for TMC context creation
Browse files Browse the repository at this point in the history
Updated e2e tests to specify mission-control context type in tests
involving such contexts.

Also fixed context type detection logic so that forceCSP does not take
precedence over explicit context type provided.

Signed-off-by: Vui Lam <[email protected]>
  • Loading branch information
vuil committed Sep 7, 2024
1 parent 0bd4dc6 commit 3191e65
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
6 changes: 4 additions & 2 deletions pkg/command/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,11 @@ func createNewContext() (context *configtypes.Context, err error) {
var ctxCreationType ContextCreationType
contextType := getContextType()

if (contextType == configtypes.ContextTypeTanzu) || (endpoint != "" && isTanzuPlatformSaaSEndpoint(endpoint)) {
if contextType == configtypes.ContextTypeTMC {
ctxCreationType = contextMissionControl
} else if (contextType == configtypes.ContextTypeTanzu) || (endpoint != "" && isTanzuPlatformSaaSEndpoint(endpoint)) {
ctxCreationType = contextTanzu
} else if (contextType == configtypes.ContextTypeTMC) || (endpoint != "" && isGlobalContext(endpoint)) {
} else if endpoint != "" && isGlobalContext(endpoint) {
ctxCreationType = contextMissionControl
} else if endpoint != "" {
// user provided command line option endpoint is provided that is not globalTanzu or GlobalContext=> it is Kubernetes(Cluster Endpoint) type
Expand Down
14 changes: 14 additions & 0 deletions pkg/command/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,19 @@ var _ = Describe("create new context", func() {
AfterEach(func() {
resetContextCommandFlags()
})
Context("with only endpoint and context name provided", func() {
It("should create context with given endpoint and context name", func() {
endpoint = fakeTMCEndpoint
forceCSP = true
contextTypeStr = contextTypeMissionControl
ctxName = testContextName
ctx, err = createNewContext()
Expect(err).To(BeNil())
Expect(ctx.Name).To(ContainSubstring("fake-context-name"))
Expect(string(ctx.ContextType)).To(ContainSubstring(contextTypeMissionControl))
Expect(ctx.GlobalOpts.Endpoint).To(ContainSubstring(endpoint))
})
})
Context("with only endpoint and context name provided", func() {
It("should create context with given endpoint and context name", func() {
endpoint = fakeTMCEndpoint
Expand Down Expand Up @@ -1888,6 +1901,7 @@ func resetContextCommandFlags() {
contextTypeStr = ""
outputFormat = ""
shortCtx = false
forceCSP = false
}

func TestCreateContextWithTanzuTypeAndKubeconfigFlags(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const tmcPluginsMockFile = "tmcPluginsMock.json"
const numberOfPluginsToInstall = 3

const forceCSPFlag = "--force-csp=true"
const typeTMCFlag = "--type=tmc"
const tmcConfigFolderName = "tmc"
const numberOfPluginsSameAsNoOfPluginsInfoMocked = "number of plugins should be same as number of plugins mocked in tmc endpoint response"
const pluginsInstalledAndMockedShouldBeSame = "plugins being installed and plugins being mocked in tmc endpoint response should be same"
Expand Down
18 changes: 9 additions & 9 deletions test/e2e/plugin_sync/tmc/plugin_sync_tmc_lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ var _ = f.CLICoreDescribe("[Tests:E2E][Feature:Plugin-Sync-TMC-lifecycle]", func
// Test case: b. create context and validate current active context
It("create context for TMC target with http mock server URL as endpoint", func() {
contextName = f.ContextPrefixTMC + f.RandomString(4)
_, _, err = tf.ContextCmd.CreateContextWithEndPointStaging(contextName, f.TMCMockServerEndpoint, f.AddAdditionalFlagAndValue(forceCSPFlag))
_, _, err = tf.ContextCmd.CreateContextWithEndPointStaging(contextName, f.TMCMockServerEndpoint, f.AddAdditionalFlagAndValue(forceCSPFlag), f.AddAdditionalFlagAndValue(typeTMCFlag))
Expect(err).To(BeNil(), noErrorWhileCreatingContext)
active, err := tf.ContextCmd.GetActiveContext(string(types.ContextTypeTMC))
Expect(err).To(BeNil(), activeContextShouldExists)
Expand Down Expand Up @@ -143,7 +143,7 @@ var _ = f.CLICoreDescribe("[Tests:E2E][Feature:Plugin-Sync-TMC-lifecycle]", func
// Test case: b. create context and make sure context has created
It("create context for TMC target with http mock server URL as endpoint", func() {
contextName = f.ContextPrefixTMC + f.RandomString(4)
_, _, err = tf.ContextCmd.CreateContextWithEndPointStaging(contextName, f.TMCMockServerEndpoint, f.AddAdditionalFlagAndValue(forceCSPFlag))
_, _, err = tf.ContextCmd.CreateContextWithEndPointStaging(contextName, f.TMCMockServerEndpoint, f.AddAdditionalFlagAndValue(forceCSPFlag), f.AddAdditionalFlagAndValue(typeTMCFlag))
Expect(err).To(BeNil(), noErrorWhileCreatingContext)
active, err := tf.ContextCmd.GetActiveContext(string(types.ContextTypeTMC))
Expect(err).To(BeNil(), activeContextShouldExists)
Expand Down Expand Up @@ -272,7 +272,7 @@ var _ = f.CLICoreDescribe("[Tests:E2E][Feature:Plugin-Sync-TMC-lifecycle]", func
// Test case: b. create context and make sure context has created
It("create context for TMC target with http mock server URL as endpoint", func() {
contextName = f.ContextPrefixTMC + f.RandomString(4)
_, stdErr, err = tf.ContextCmd.CreateContextWithEndPointStaging(contextName, f.TMCMockServerEndpoint, f.AddAdditionalFlagAndValue(forceCSPFlag))
_, stdErr, err = tf.ContextCmd.CreateContextWithEndPointStaging(contextName, f.TMCMockServerEndpoint, f.AddAdditionalFlagAndValue(forceCSPFlag), f.AddAdditionalFlagAndValue(typeTMCFlag))
Expect(err).To(BeNil(), noErrorWhileCreatingContext)
Expect(stdErr).NotTo(BeNil(), "there should be stderr")
Expect(stdErr).To(ContainSubstring(f.UnableToSync), "there should be sync error as all plugins not available in repo")
Expand Down Expand Up @@ -380,7 +380,7 @@ var _ = f.CLICoreDescribe("[Tests:E2E][Feature:Plugin-Sync-TMC-lifecycle]", func
Expect(len(mockResPluginsInfo.Plugins)).Should(Equal(len(pluginsToGenerateMockResponseOne)), "the number of plugins in endpoint response and initially mocked should be same")

contextNameOne = f.ContextPrefixTMC + f.RandomString(4)
_, _, err = tf.ContextCmd.CreateContextWithEndPointStaging(contextNameOne, f.TMCMockServerEndpoint, f.AddAdditionalFlagAndValue(forceCSPFlag))
_, _, err = tf.ContextCmd.CreateContextWithEndPointStaging(contextNameOne, f.TMCMockServerEndpoint, f.AddAdditionalFlagAndValue(forceCSPFlag), f.AddAdditionalFlagAndValue(typeTMCFlag))
Expect(err).To(BeNil(), noErrorWhileCreatingContext)
active, err := tf.ContextCmd.GetActiveContext(string(types.ContextTypeTMC))
Expect(err).To(BeNil(), activeContextShouldExists)
Expand Down Expand Up @@ -411,7 +411,7 @@ var _ = f.CLICoreDescribe("[Tests:E2E][Feature:Plugin-Sync-TMC-lifecycle]", func
Expect(len(mockResPluginsInfo.Plugins)).Should(Equal(len(pluginsToGenerateMockResponseTwo)), "the number of plugins in endpoint response and initially mocked should be same")

contextNameTwo = f.ContextPrefixTMC + f.RandomString(4)
_, _, err = tf.ContextCmd.CreateContextWithEndPointStaging(contextNameTwo, f.TMCMockServerEndpoint, f.AddAdditionalFlagAndValue(forceCSPFlag))
_, _, err = tf.ContextCmd.CreateContextWithEndPointStaging(contextNameTwo, f.TMCMockServerEndpoint, f.AddAdditionalFlagAndValue(forceCSPFlag), f.AddAdditionalFlagAndValue(typeTMCFlag))
Expect(err).To(BeNil(), noErrorWhileCreatingContext)
active, err := tf.ContextCmd.GetActiveContext(string(types.ContextTypeTMC))
Expect(err).To(BeNil(), activeContextShouldExists)
Expand Down Expand Up @@ -482,7 +482,7 @@ var _ = f.CLICoreDescribe("[Tests:E2E][Feature:Plugin-Sync-TMC-lifecycle]", func
Expect(len(mockResPluginsInfo.Plugins)).Should(Equal(len(pluginsToGenerateMockResponseOne)), "the number of plugins in endpoint response and initially mocked should be same")

contextNameOne = f.ContextPrefixTMC + f.RandomString(4)
_, _, err = tf.ContextCmd.CreateContextWithEndPointStaging(contextNameOne, f.TMCMockServerEndpoint, f.AddAdditionalFlagAndValue(forceCSPFlag))
_, _, err = tf.ContextCmd.CreateContextWithEndPointStaging(contextNameOne, f.TMCMockServerEndpoint, f.AddAdditionalFlagAndValue(forceCSPFlag), f.AddAdditionalFlagAndValue(typeTMCFlag))
Expect(err).To(BeNil(), noErrorWhileCreatingContext)
active, err := tf.ContextCmd.GetActiveContext(string(types.ContextTypeTMC))
Expect(err).To(BeNil(), activeContextShouldExists)
Expand Down Expand Up @@ -626,7 +626,7 @@ var _ = f.CLICoreDescribe("[Tests:E2E][Feature:Plugin-Sync-TMC-lifecycle]", func
// Test case: f. TMC: create context and make sure context has created
It("create context for TMC target with http mock server URL as endpoint", func() {
contextNameTMC = f.ContextPrefixTMC + f.RandomString(4)
_, _, err = tf.ContextCmd.CreateContextWithEndPointStaging(contextNameTMC, f.TMCMockServerEndpoint, f.AddAdditionalFlagAndValue(forceCSPFlag))
_, _, err = tf.ContextCmd.CreateContextWithEndPointStaging(contextNameTMC, f.TMCMockServerEndpoint, f.AddAdditionalFlagAndValue(forceCSPFlag), f.AddAdditionalFlagAndValue(typeTMCFlag))
Expect(err).To(BeNil(), noErrorWhileCreatingContext)
active, err := tf.ContextCmd.GetActiveContext(string(types.ContextTypeTMC))
Expect(err).To(BeNil(), activeContextShouldExists)
Expand Down Expand Up @@ -814,7 +814,7 @@ var _ = f.CLICoreDescribe("[Tests:E2E][Feature:Plugin-Sync-TMC-lifecycle]", func
Expect(err).To(BeNil(), thereShouldNotBeError+" while cleaning plugins")

contextNameTMC = f.ContextPrefixTMC + f.RandomString(4)
_, _, err = tf.ContextCmd.CreateContextWithEndPointStaging(contextNameTMC, f.TMCMockServerEndpoint, f.AddAdditionalFlagAndValue(forceCSPFlag))
_, _, err = tf.ContextCmd.CreateContextWithEndPointStaging(contextNameTMC, f.TMCMockServerEndpoint, f.AddAdditionalFlagAndValue(forceCSPFlag), f.AddAdditionalFlagAndValue(typeTMCFlag))
Expect(err).To(BeNil(), noErrorWhileCreatingContext)
active, err := tf.ContextCmd.GetActiveContext(string(types.ContextTypeTMC))
Expect(err).To(BeNil(), activeContextShouldExists)
Expand Down Expand Up @@ -1111,7 +1111,7 @@ var _ = f.CLICoreDescribe("[Tests:E2E][Feature:Plugin-Sync-TMC-lifecycle]", func
// Test case: g. TMC: create context and make sure context has created
It("create context for TMC target with http mock server URL as endpoint", func() {
contextNameTMC = f.ContextPrefixTMC + f.RandomString(4)
_, _, err := tf.ContextCmd.CreateContextWithEndPointStaging(contextNameTMC, f.TMCMockServerEndpoint, f.AddAdditionalFlagAndValue(forceCSPFlag))
_, _, err := tf.ContextCmd.CreateContextWithEndPointStaging(contextNameTMC, f.TMCMockServerEndpoint, f.AddAdditionalFlagAndValue(forceCSPFlag), f.AddAdditionalFlagAndValue(typeTMCFlag))
Expect(err).To(BeNil(), noErrorWhileCreatingContext)
active, err := tf.ContextCmd.GetActiveContext(string(types.ContextTypeTMC))
Expect(err).To(BeNil(), activeContextShouldExists)
Expand Down

0 comments on commit 3191e65

Please sign in to comment.