Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anujc25 committed Oct 4, 2023
1 parent da07394 commit 6c7061f
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 58 deletions.
15 changes: 9 additions & 6 deletions config/config_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,9 @@ func TestGetClientConfigWithLockAndWithoutLock(t *testing.T) {
assert.NoError(t, err)

expectedCtx := &configtypes.Context{
Name: "test-mc",
Target: configtypes.TargetK8s,
Name: "test-mc",
Target: configtypes.TargetK8s,
ContextType: configtypes.ContextTypeK8s,
ClusterOpts: &configtypes.ClusterServer{
Endpoint: "test-endpoint",
Path: "test-path",
Expand Down Expand Up @@ -324,8 +325,9 @@ func TestGetClientConfigWithLockAndMigratedToNewConfig(t *testing.T) {
assert.NoError(t, err)

expectedCtx := &configtypes.Context{
Name: "test-mc",
Target: configtypes.TargetK8s,
Name: "test-mc",
Target: configtypes.TargetK8s,
ContextType: configtypes.ContextTypeK8s,
ClusterOpts: &configtypes.ClusterServer{
Endpoint: "test-endpoint",
Path: "test-path",
Expand Down Expand Up @@ -378,8 +380,9 @@ func TestGetClientConfigWithoutLockAndMigratedToNewConfig(t *testing.T) {
assert.NoError(t, err)

expectedCtx := &configtypes.Context{
Name: "test-mc",
Target: configtypes.TargetK8s,
Name: "test-mc",
Target: configtypes.TargetK8s,
ContextType: configtypes.ContextTypeK8s,
ClusterOpts: &configtypes.ClusterServer{
Endpoint: "test-endpoint",
Path: "test-path",
Expand Down
37 changes: 22 additions & 15 deletions config/contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func RemoveContext(name string) error {
if err != nil {
return err
}
err = removeActiveContext(node, ctx)
err = removeCurrentContext(node, ctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -232,25 +232,19 @@ func SetCurrentContext(name string) error {
}

// RemoveCurrentContext removed the current context of specified context type
// Deprecated:ANUJ
func RemoveCurrentContext(target configtypes.Target) error {
return RemoveActiveContext(configtypes.ConvertTargetToContextType(target))
}

// RemoveActiveContext removed the current context of specified context type
func RemoveActiveContext(contextType configtypes.ContextType) error {
// Retrieve client config node
AcquireTanzuConfigLock()
defer ReleaseTanzuConfigLock()
node, err := getClientConfigNodeNoLock()
if err != nil {
return err
}
c, err := getActiveContext(node, contextType)
c, err := getCurrentContext(node, target)
if err != nil {
return err
}
err = removeActiveContext(node, &configtypes.Context{ContextType: contextType})
err = removeCurrentContext(node, &configtypes.Context{Target: target})
if err != nil {
return err
}
Expand All @@ -261,6 +255,11 @@ func RemoveActiveContext(contextType configtypes.ContextType) error {
return persistConfig(node)
}

// RemoveActiveContext removed the current context of specified context type
func RemoveActiveContext(contextType configtypes.ContextType) error {
return RemoveCurrentContext(configtypes.ConvertContextTypeToTarget(contextType))
}

// EndpointFromContext retrieved the endpoint from the specified context
func EndpointFromContext(s *configtypes.Context) (endpoint string, err error) {
switch s.Target {
Expand Down Expand Up @@ -293,6 +292,14 @@ func getContext(node *yaml.Node, name string) (*configtypes.Context, error) {
return nil, fmt.Errorf("context %v not found", name)
}

func getCurrentContext(node *yaml.Node, target configtypes.Target) (*configtypes.Context, error) {
cfg, err := convertNodeToClientConfig(node)
if err != nil {
return nil, err
}
return cfg.GetCurrentContext(target)
}

func getActiveContext(node *yaml.Node, contextType configtypes.ContextType) (*configtypes.Context, error) {
cfg, err := convertNodeToClientConfig(node)
if err != nil {
Expand Down Expand Up @@ -435,7 +442,7 @@ func setCurrentContext(node *yaml.Node, ctx *configtypes.Context) (persist bool,
return persist, err
}

func removeActiveContext(node *yaml.Node, ctx *configtypes.Context) error {
func removeCurrentContext(node *yaml.Node, ctx *configtypes.Context) error {
// Find current context node in the yaml node
keys := []nodeutils.Key{
{Name: KeyCurrentContext},
Expand All @@ -445,13 +452,13 @@ func removeActiveContext(node *yaml.Node, ctx *configtypes.Context) error {
if currentContextNode == nil {
return nil
}
contextTypeNodeIndex := nodeutils.GetNodeIndex(currentContextNode.Content, string(ctx.ContextType))
if contextTypeNodeIndex == -1 {
targetNodeIndex := nodeutils.GetNodeIndex(currentContextNode.Content, string(ctx.Target))
if targetNodeIndex == -1 {
return nil
}
if currentContextNode.Content[contextTypeNodeIndex].Value == ctx.Name || ctx.Name == "" {
contextTypeNodeIndex--
currentContextNode.Content = append(currentContextNode.Content[:contextTypeNodeIndex], currentContextNode.Content[contextTypeNodeIndex+2:]...)
if currentContextNode.Content[targetNodeIndex].Value == ctx.Name || ctx.Name == "" {
targetNodeIndex--
currentContextNode.Content = append(currentContextNode.Content[:targetNodeIndex], currentContextNode.Content[targetNodeIndex+2:]...)
}
return nil
}
Expand Down
21 changes: 13 additions & 8 deletions config/contexts_it_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ currentContext:
required: true
- name: test-mc2
target: kubernetes
contextType: kubernetes
clusterOpts:
path: test-path-updated
context: test-context-updated
Expand Down Expand Up @@ -286,8 +287,9 @@ func TestContextsIntegration(t *testing.T) {
// Get Context
context, err := GetContext("test-mc")
expected := &configtypes.Context{
Name: "test-mc",
Target: configtypes.TargetK8s,
Name: "test-mc",
Target: configtypes.TargetK8s,
ContextType: configtypes.ContextTypeK8s,
ClusterOpts: &configtypes.ClusterServer{
Endpoint: "test-endpoint",
Path: "test-path",
Expand All @@ -309,8 +311,9 @@ func TestContextsIntegration(t *testing.T) {

// Add new Context
newCtx := &configtypes.Context{
Name: "test-mc2",
Target: configtypes.TargetK8s,
Name: "test-mc2",
Target: configtypes.TargetK8s,
ContextType: configtypes.ContextTypeK8s,
ClusterOpts: &configtypes.ClusterServer{
Path: "test-path",
Context: "test-context",
Expand All @@ -337,8 +340,9 @@ func TestContextsIntegration(t *testing.T) {

// Try to add context with empty name
contextWithEmptyName := &configtypes.Context{
Name: "",
Target: configtypes.TargetK8s,
Name: "",
Target: configtypes.TargetK8s,
ContextType: configtypes.ContextTypeK8s,
ClusterOpts: &configtypes.ClusterServer{
Path: "test-path",
Context: "test-context",
Expand All @@ -362,8 +366,9 @@ func TestContextsIntegration(t *testing.T) {

// Update existing Context
updatedCtx := &configtypes.Context{
Name: "test-mc2",
Target: configtypes.TargetK8s,
Name: "test-mc2",
Target: configtypes.TargetK8s,
ContextType: configtypes.ContextTypeK8s,
ClusterOpts: &configtypes.ClusterServer{
Path: "test-path-updated",
Context: "test-context-updated",
Expand Down
40 changes: 25 additions & 15 deletions config/contexts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package config

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -481,6 +482,7 @@ func setupForGetContext(t *testing.T) {
},
}
func() {
os.Unsetenv(EnvConfigKey)
LocalDirName = TestLocalDirName
err := StoreClientConfig(cfg)
assert.NoError(t, err)
Expand Down Expand Up @@ -854,12 +856,16 @@ func TestSetCurrentContext(t *testing.T) {
assert.Error(t, err)
}
currSrv, err := GetCurrentServer()
assert.NoError(t, err)
if tc.errStr == "" {
if tc.currServer == "" {
assert.Equal(t, prevSrv.Name, currSrv.Name)
} else {
assert.Equal(t, tc.currServer, currSrv.Name)
if tc.currServer == "" && prevSrv == nil {
assert.Error(t, err)
} else {
assert.NoError(t, err)
if tc.errStr == "" {
if tc.currServer == "" {
assert.Equal(t, prevSrv.Name, currSrv.Name)
} else {
assert.Equal(t, tc.currServer, currSrv.Name)
}
}
}
})
Expand Down Expand Up @@ -889,7 +895,7 @@ func TestRemoveCurrentContext(t *testing.T) {
assert.NoError(t, err)

currCtx, err := GetCurrentContext(configtypes.TargetK8s)
assert.Equal(t, "no current context set for target \"kubernetes\"", err.Error())
assert.Equal(t, "no current context set for type \"kubernetes\"", err.Error())
assert.Nil(t, currCtx)

currSrv, err := GetCurrentServer()
Expand Down Expand Up @@ -958,8 +964,9 @@ func TestSetContextMultiFile(t *testing.T) {
}()

ctx := &configtypes.Context{
Name: "test-mc",
Target: configtypes.TargetK8s,
Name: "test-mc",
Target: configtypes.TargetK8s,
ContextType: configtypes.ContextTypeK8s,
ClusterOpts: &configtypes.ClusterServer{
IsManagementCluster: true,
Endpoint: "test-endpoint",
Expand Down Expand Up @@ -1003,8 +1010,9 @@ func TestSetContextMultiFile(t *testing.T) {
}

expectedCtx2 := &configtypes.Context{
Name: "test-mc2",
Target: configtypes.TargetK8s,
Name: "test-mc2",
Target: configtypes.TargetK8s,
ContextType: configtypes.ContextTypeK8s,
ClusterOpts: &configtypes.ClusterServer{
IsManagementCluster: true,
Endpoint: "updated-test-endpoint",
Expand Down Expand Up @@ -1054,8 +1062,9 @@ func TestSetContextMultiFileAndMigrateToNewConfig(t *testing.T) {
}()

ctx := &configtypes.Context{
Name: "test-mc",
Target: configtypes.TargetK8s,
Name: "test-mc",
Target: configtypes.TargetK8s,
ContextType: configtypes.ContextTypeK8s,
ClusterOpts: &configtypes.ClusterServer{
IsManagementCluster: true,
Endpoint: "test-endpoint",
Expand Down Expand Up @@ -1099,8 +1108,9 @@ func TestSetContextMultiFileAndMigrateToNewConfig(t *testing.T) {
}

expectedCtx2 := &configtypes.Context{
Name: "test-mc2",
Target: configtypes.TargetK8s,
Name: "test-mc2",
Target: configtypes.TargetK8s,
ContextType: configtypes.ContextTypeK8s,
ClusterOpts: &configtypes.ClusterServer{
IsManagementCluster: true,
Endpoint: "updated-test-endpoint",
Expand Down
1 change: 1 addition & 0 deletions config/legacy_clientconfig_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ current: test-mc
- local:
name: test
path: test-local-path
contextType: kubernetes
currentContext:
kubernetes: test-mc
`
Expand Down
4 changes: 2 additions & 2 deletions config/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func RemoveCurrentServer(name string) error {
if err != nil {
return err
}
err = removeActiveContext(node, c)
err = removeCurrentContext(node, c)
if err != nil {
return err
}
Expand Down Expand Up @@ -235,7 +235,7 @@ func RemoveServer(name string) error {
if err != nil {
return err
}
err = removeActiveContext(node, c)
err = removeCurrentContext(node, c)
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions config/servers_it_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ currentContext:
contextType: tmc
- name: test-mc2
target: kubernetes
contextType: kubernetes
clusterOpts:
endpoint: test-endpoint-updated
path: test-path
Expand Down Expand Up @@ -275,6 +276,7 @@ func TestServersIntegrationAndMigratedToNewConfig(t *testing.T) {
contextType: tmc
- name: test-mc2
target: kubernetes
contextType: kubernetes
clusterOpts:
endpoint: test-endpoint-updated
path: test-path
Expand Down
4 changes: 2 additions & 2 deletions config/types/clientconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ func ConvertTargetToContextType(target Target) ContextType {
case TargetTAE:
return ContextTypeTAE
}
return ""
return ContextType(target)
}

func ConvertContextTypeToTarget(ctxType ContextType) Target {
Expand All @@ -366,5 +366,5 @@ func ConvertContextTypeToTarget(ctxType ContextType) Target {
case ContextTypeTAE:
return TargetTAE
}
return ""
return Target(ctxType)
}
2 changes: 1 addition & 1 deletion config/types/clientconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (suite *ClientTestSuite) TestGetCurrentContext_K8s() {
func (suite *ClientTestSuite) TestGetCurrentContext_NotFound() {
_, err := suite.ClientConfig.GetCurrentContext("test")
suite.Error(err)
suite.EqualError(err, "no current context set for target \"test\"")
suite.EqualError(err, "no current context set for type \"test\"")
}

func (suite *ClientTestSuite) TestSetCurrentContext_TMC() {
Expand Down
4 changes: 2 additions & 2 deletions plugin/sync_plugins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,14 @@ func TestSyncPlugins(t *testing.T) {
{
test: "with alternate command and sync successfully",
newCommandExitStatus: "0",
expectedOutput: "newcommand sync --target kubernetes succeeded\n",
expectedOutput: "newcommand sync --type kubernetes succeeded\n",
expectedFailure: false,
enableCustomCommand: true,
},
{
test: "with alternate command and sync unsuccessfully",
newCommandExitStatus: "1",
expectedOutput: "newcommand sync --target kubernetes failed\n",
expectedOutput: "newcommand sync --type kubernetes failed\n",
expectedFailure: true,
enableCustomCommand: true,
},
Expand Down
8 changes: 4 additions & 4 deletions tae/tae_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ func setupForGetContext(t *testing.T) {
},
},
},
CurrentContext: map[configtypes.Target]string{
configtypes.TargetK8s: "test-mc-2",
configtypes.TargetTMC: "test-tmc",
configtypes.TargetTAE: "test-tae",
CurrentContext: map[configtypes.ContextType]string{
configtypes.ContextTypeK8s: "test-mc-2",
configtypes.ContextTypeTMC: "test-tmc",
configtypes.ContextTypeTAE: "test-tae",
},
}
func() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ func DefaultGetCurrentContextOutputOptions(version core.RuntimeVersion, contextN
}

// DefaultGetCurrentContextOutputOptionsWithError helper method to construct GetCurrentContext API output options with error
func DefaultGetCurrentContextOutputOptionsWithError(version core.RuntimeVersion) *context.GetCurrentContextOutputOptions {
func DefaultGetCurrentContextOutputOptionsWithError(version core.RuntimeVersion) *context.GetCurrentContextOutputOptions { //nolint:dupl
switch version {
case core.VersionLatest:
return &context.GetCurrentContextOutputOptions{
Expand Down Expand Up @@ -794,7 +794,7 @@ func DefaultRemoveCurrentContextInputOptions(version core.RuntimeVersion) *conte
}

// DefaultRemoveCurrentContextOutputOptionsWithError helper method to construct RemoveCurrentContext API output option
func DefaultRemoveCurrentContextOutputOptionsWithError(version core.RuntimeVersion) *context.RemoveCurrentContextOutputOptions {
func DefaultRemoveCurrentContextOutputOptionsWithError(version core.RuntimeVersion) *context.RemoveCurrentContextOutputOptions { //nolint:dupl
switch version {
case core.VersionLatest:
return &context.RemoveCurrentContextOutputOptions{
Expand Down
Loading

0 comments on commit 6c7061f

Please sign in to comment.