Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config/contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ func SetContext(c *configtypes.Context, setCurrent bool) error {
}

// Back-fill servers based on contexts
if c.ContextType == configtypes.ContextTypeTanzu {
return nil
}
s := convertContextToServer(c)

// Add or update server
Expand Down
36 changes: 27 additions & 9 deletions config/contexts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package config

import (
"fmt"
"testing"

. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -684,10 +685,11 @@ func TestSetContext(t *testing.T) {
cleanupDir(LocalDirName)
}()
tcs := []struct {
name string
ctx *configtypes.Context
current bool
errStr string
name string
ctx *configtypes.Context
current bool
errStr string
shouldServerExists bool
}{
{
name: "should add new context and set current on empty config",
Expand All @@ -704,7 +706,8 @@ func TestSetContext(t *testing.T) {
"metaToken": "token1",
},
},
current: true,
current: true,
shouldServerExists: true,
},

{
Expand All @@ -722,6 +725,7 @@ func TestSetContext(t *testing.T) {
"metaToken": "token1",
},
},
shouldServerExists: true,
},
{
name: "should add new context and configure missing Target from ContextType",
Expand All @@ -738,6 +742,7 @@ func TestSetContext(t *testing.T) {
"metaToken": "token1",
},
},
shouldServerExists: true,
},
{
name: "success tmc current",
Expand All @@ -748,7 +753,8 @@ func TestSetContext(t *testing.T) {
Endpoint: "test-endpoint",
},
},
current: true,
current: true,
shouldServerExists: true,
},
{
name: "success tmc not_current",
Expand All @@ -760,6 +766,7 @@ func TestSetContext(t *testing.T) {
Endpoint: "test-endpoint",
},
},
shouldServerExists: true,
},
{
name: "success update test-mc",
Expand All @@ -776,6 +783,7 @@ func TestSetContext(t *testing.T) {
"metaToken": "updated-token1",
},
},
shouldServerExists: true,
},
{
name: "success update tmc",
Expand All @@ -786,6 +794,7 @@ func TestSetContext(t *testing.T) {
Endpoint: "updated-test-endpoint",
},
},
shouldServerExists: true,
},
{
name: "success tanzu current",
Expand All @@ -804,7 +813,8 @@ func TestSetContext(t *testing.T) {
"org": "fake-org-1",
},
},
current: true,
current: true,
shouldServerExists: false,
},
{
name: "success tanzu not_current",
Expand All @@ -823,6 +833,7 @@ func TestSetContext(t *testing.T) {
"org": "fake-org-2",
},
},
shouldServerExists: false,
},
{
name: "error target and contexttype does not match",
Expand Down Expand Up @@ -859,9 +870,16 @@ func TestSetContext(t *testing.T) {
// Verify that even though only Target or ContextType was provided when
// setting context, retrieving the Context should have both set
assert.Equal(t, string(ctx.Target), string(ctx.ContextType))

s, err := GetServer(tc.ctx.Name)
assert.NoError(t, err)
assert.Equal(t, tc.ctx.Name, s.Name)
// Verify that for some context types(e.g "tanzu") the server entry in not populated
if !tc.shouldServerExists {
assert.Nil(t, s)
assert.EqualError(t, err, fmt.Sprintf("could not find server %q", tc.ctx.Name))
} else {
assert.NoError(t, err)
assert.Equal(t, tc.ctx.Name, s.Name)
}
}
})
}
Expand Down
8 changes: 4 additions & 4 deletions config/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func PopulateContexts(cfg *configtypes.ClientConfig) bool {
cfg.KnownContexts = make([]*configtypes.Context, 0, len(cfg.KnownServers))
}
for _, s := range cfg.KnownServers {
if cfg.HasContext(s.Name) {
// server already present in known contexts; skip
if s.Type == configtypes.ServerType(configtypes.ContextTypeTanzu) || cfg.HasContext(s.Name) {
// server of type "tanzu" or server already present in known contexts; skip
continue
}

Expand Down Expand Up @@ -100,8 +100,8 @@ func populateServers(cfg *configtypes.ClientConfig) {
fillMissingContextTypeInContext(c)
fillMissingTargetInContext(c)

if cfg.HasServer(c.Name) {
// context already present in known servers; skip
if c.ContextType == configtypes.ContextTypeTanzu || cfg.HasServer(c.Name) {
// "tanzu" context type or context already present in known servers; skip
continue
}

Expand Down
154 changes: 154 additions & 0 deletions config/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,72 @@ func TestPopulateContexts(t *testing.T) {
},
delta: true,
},
{
name: "server of type 'tanzu' should be ignored for context population",
ip: &configtypes.ClientConfig{
KnownServers: []*configtypes.Server{
{
Name: "test-tanzu",
Type: configtypes.ServerType("tanzu"),
GlobalOpts: &configtypes.GlobalServer{
Endpoint: "test-tanzu-endpoint",
},
},
{
Name: "test-tmc",
Type: configtypes.GlobalServerType,
GlobalOpts: &configtypes.GlobalServer{
Endpoint: "test-endpoint",
},
},
},
CurrentServer: "test-tmc",
KnownContexts: []*configtypes.Context{
{
Name: "test-tmc",
Target: configtypes.TargetTMC,
GlobalOpts: &configtypes.GlobalServer{
Endpoint: "test-endpoint",
},
},
},
CurrentContext: map[configtypes.ContextType]string{
configtypes.ContextTypeTMC: "test-tmc",
},
},
op: &configtypes.ClientConfig{
KnownServers: []*configtypes.Server{
{
Name: "test-tanzu",
Type: configtypes.ServerType("tanzu"),
GlobalOpts: &configtypes.GlobalServer{
Endpoint: "test-tanzu-endpoint",
},
},
{
Name: "test-tmc",
Type: configtypes.GlobalServerType,
GlobalOpts: &configtypes.GlobalServer{
Endpoint: "test-endpoint",
},
},
},
CurrentServer: "test-tmc",
KnownContexts: []*configtypes.Context{
{
Name: "test-tmc",
Target: configtypes.TargetTMC,
GlobalOpts: &configtypes.GlobalServer{
Endpoint: "test-endpoint",
},
},
},
CurrentContext: map[configtypes.ContextType]string{
configtypes.ContextTypeTMC: "test-tmc",
},
},
delta: false,
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
Expand Down Expand Up @@ -398,6 +464,94 @@ func TestPopulateServers(t *testing.T) {
},
},
},
{
name: "context type of 'tanzu' should be ignored for server population",
ip: &configtypes.ClientConfig{
KnownServers: []*configtypes.Server{
{
Name: "test-mc",
Type: configtypes.ManagementClusterServerType,
ManagementClusterOpts: &configtypes.ManagementClusterServer{
Endpoint: "test-endpoint",
Path: "test-path",
Context: "test-context",
},
},
},
CurrentServer: "test-mc",
KnownContexts: []*configtypes.Context{
{
Name: "test-tanzu",
Target: configtypes.Target(configtypes.ContextTypeTanzu),
GlobalOpts: &configtypes.GlobalServer{
Endpoint: "test--tanzu-endpoint",
},
ClusterOpts: &configtypes.ClusterServer{
Endpoint: "test-endpoint",
Path: "test-path",
Context: "test-context",
},
},
{
Name: "test-tmc",
Target: configtypes.TargetTMC,
GlobalOpts: &configtypes.GlobalServer{
Endpoint: "test-endpoint",
},
},
},
CurrentContext: map[configtypes.ContextType]string{
configtypes.ContextTypeTanzu: "test-tanzu",
configtypes.ContextTypeTMC: "test-tmc",
},
},
op: &configtypes.ClientConfig{
KnownServers: []*configtypes.Server{
{
Name: "test-mc",
Type: configtypes.ManagementClusterServerType,
ManagementClusterOpts: &configtypes.ManagementClusterServer{
Endpoint: "test-endpoint",
Path: "test-path",
Context: "test-context",
},
},
{
Name: "test-tmc",
Type: configtypes.GlobalServerType,
GlobalOpts: &configtypes.GlobalServer{
Endpoint: "test-endpoint",
},
},
},
CurrentServer: "test-mc",
KnownContexts: []*configtypes.Context{
{
Name: "test-tanzu",
Target: configtypes.Target(configtypes.ContextTypeTanzu),
GlobalOpts: &configtypes.GlobalServer{
Endpoint: "test--tanzu-endpoint",
},
ClusterOpts: &configtypes.ClusterServer{
Endpoint: "test-endpoint",
Path: "test-path",
Context: "test-context",
},
},
{
Name: "test-tmc",
Target: configtypes.TargetTMC,
GlobalOpts: &configtypes.GlobalServer{
Endpoint: "test-endpoint",
},
},
},
CurrentContext: map[configtypes.ContextType]string{
configtypes.ContextTypeTanzu: "test-tanzu",
configtypes.ContextTypeTMC: "test-tmc",
},
},
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
Expand Down