Skip to content

Commit

Permalink
Add empty value check for config items
Browse files Browse the repository at this point in the history
  • Loading branch information
mpanchajanya committed Apr 24, 2023
1 parent 9ea858f commit 20e9626
Show file tree
Hide file tree
Showing 16 changed files with 612 additions and 98 deletions.
17 changes: 15 additions & 2 deletions config/cli_discovery_sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,21 @@ func getCLIDiscoverySources(node *yaml.Node) ([]configtypes.PluginDiscovery, err
}

func getCLIDiscoverySource(node *yaml.Node, name string) (*configtypes.PluginDiscovery, error) {
// check if context name is empty
if name == "" {
return nil, errors.New("discovery source name cannot be empty")
}

cfg, err := convertNodeToClientConfig(node)
if err != nil {
return nil, err
}
if cfg.ClientOptions != nil && cfg.ClientOptions.CLI != nil && cfg.ClientOptions.CLI.DiscoverySources != nil {
for _, discoverySource := range cfg.ClientOptions.CLI.DiscoverySources {
_, discoverySourceName := getDiscoverySourceTypeAndName(discoverySource)
_, discoverySourceName, err := getDiscoverySourceTypeAndName(discoverySource)
if err != nil {
return nil, err
}
if discoverySourceName == name {
return &discoverySource, nil
}
Expand Down Expand Up @@ -185,7 +193,12 @@ func deleteCLIDiscoverySource(node *yaml.Node, name string) error {
if err != nil {
return err
}
discoverySourceType, discoverySourceName := getDiscoverySourceTypeAndName(*discoverySource)

discoverySourceType, discoverySourceName, err := getDiscoverySourceTypeAndName(*discoverySource)
if err != nil {
return err
}

var result []*yaml.Node
for _, discoverySourceNode := range cliDiscoverySourcesNode.Content {
// Find discovery source matched by discoverySourceType
Expand Down
73 changes: 62 additions & 11 deletions config/cli_discovery_sources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ func TestGetCLIDiscoverySource(t *testing.T) {
}()

tests := []struct {
name string
in *configtypes.ClientConfig
out *configtypes.PluginDiscovery
name string
discoverySourceName string
in *configtypes.ClientConfig
out *configtypes.PluginDiscovery
errStr string
}{
{
name: "success get",
Expand All @@ -101,15 +103,44 @@ func TestGetCLIDiscoverySource(t *testing.T) {
ManifestPath: "test-manifest-path",
},
},
discoverySourceName: "test",
},
{
name: "failed discovery source with empty name",
in: &configtypes.ClientConfig{
ClientOptions: &configtypes.ClientOptions{
CLI: &configtypes.CLIOptions{
DiscoverySources: []configtypes.PluginDiscovery{
{
GCP: &configtypes.GCPDiscovery{
Name: "",
Bucket: "updated-test-bucket",
ManifestPath: "test-manifest-path",
},
},
},
},
},
},
discoverySourceName: "",
errStr: "discovery source name cannot be empty",
},
}
for _, spec := range tests {
t.Run(spec.name, func(t *testing.T) {
err := StoreClientConfig(spec.in)
assert.NoError(t, err)
c, err := GetCLIDiscoverySource("test")
assert.Equal(t, spec.out, c)
assert.NoError(t, err)
if spec.errStr != "" {
assert.Equal(t, spec.errStr, err.Error())
} else {
assert.NoError(t, err)
}
c, err := GetCLIDiscoverySource(spec.discoverySourceName)
if spec.errStr != "" {
assert.Equal(t, spec.errStr, err.Error())
} else {
assert.Equal(t, spec.out, c)
assert.NoError(t, err)
}
})
}
}
Expand All @@ -123,9 +154,10 @@ func TestSetCLIDiscoverySources(t *testing.T) {
}()

tests := []struct {
name string
input []configtypes.PluginDiscovery
total int
name string
input []configtypes.PluginDiscovery
total int
errStr string
}{
{
name: "success add test",
Expand Down Expand Up @@ -246,14 +278,33 @@ func TestSetCLIDiscoverySources(t *testing.T) {
},
total: 4,
},
{
name: "failed discovery source with empty name",
input: []configtypes.PluginDiscovery{
{
Local: &configtypes.LocalDiscovery{
Name: "",
Path: "test-path",
},
},
},
errStr: "discovery source name cannot be empty",
total: 4,
},
}
for _, spec := range tests {
t.Run(spec.name, func(t *testing.T) {
err := SetCLIDiscoverySources(spec.input)
assert.NoError(t, err)
if spec.errStr != "" {
assert.Equal(t, spec.errStr, err.Error())
} else {
assert.NoError(t, err)
}

sources, err := GetCLIDiscoverySources()
assert.NoError(t, err)
assert.Equal(t, spec.total, len(sources))

})
}
}
Expand Down
4 changes: 4 additions & 0 deletions config/cli_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func GetEdition() (string, error) {

// SetEdition adds or updates edition value
func SetEdition(val string) (err error) {
// Check if val is empty
if val == "" {
return errors.New("value cannot be empty")
}
// Retrieve client config node
AcquireTanzuConfigLock()
defer ReleaseTanzuConfigLock()
Expand Down
66 changes: 22 additions & 44 deletions config/cli_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ import (
"testing"

"github.com/stretchr/testify/assert"

configtypes "github.com/vmware-tanzu/tanzu-plugin-runtime/config/types"
)

func TestGetEdition(t *testing.T) {
func TestSetEdition(t *testing.T) {
// Setup config test data
_, cleanUp := setupTestConfig(t, &CfgTestData{})

Expand All @@ -20,47 +18,17 @@ func TestGetEdition(t *testing.T) {
}()

tests := []struct {
name string
in *configtypes.ClientConfig
out string
errStr string
name string
value string
errStr string
errStrForGet string
}{
{
name: "success k8s",
in: &configtypes.ClientConfig{
ClientOptions: &configtypes.ClientOptions{
Env: map[string]string{
"test": "test",
},
},
},
out: "test",
name: "should return error for empty val",
value: "",
errStr: "value cannot be empty",
errStrForGet: "edition not found",
},
}
for _, spec := range tests {
t.Run(spec.name, func(t *testing.T) {
err := StoreClientConfig(spec.in)
assert.NoError(t, err)
c, err := GetEnv("test")
assert.NoError(t, err)
assert.Equal(t, spec.out, c)
assert.NoError(t, err)
})
}
}

func TestSetEdition(t *testing.T) {
// Setup config test data
_, cleanUp := setupTestConfig(t, &CfgTestData{})

defer func() {
cleanUp()
}()

tests := []struct {
name string
value string
}{
{
name: "should persist tanzu when empty client config",
value: "tanzu",
Expand All @@ -78,10 +46,20 @@ func TestSetEdition(t *testing.T) {
for _, spec := range tests {
t.Run(spec.name, func(t *testing.T) {
err := SetEdition(spec.value)
assert.NoError(t, err)
if spec.errStr != "" {
assert.Equal(t, spec.errStr, err.Error())
} else {
assert.NoError(t, err)
}

c, err := GetEdition()
assert.Equal(t, spec.value, c)
assert.NoError(t, err)
if spec.errStrForGet != "" {
assert.Equal(t, spec.errStrForGet, err.Error())
} else {
assert.Equal(t, spec.value, c)
assert.NoError(t, err)
}

})
}
}
4 changes: 3 additions & 1 deletion config/cli_repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ func deleteCLIRepository(node *yaml.Node, name string) error {
}

repositoryType, repositoryName := getRepositoryTypeAndName(*repository)

if repositoryType == "" || repositoryName == "" {
return errors.New("not found")
}
var result []*yaml.Node
for _, repositoryNode := range cliRepositoriesNode.Content {
if repositoryIndex := nodeutils.GetNodeIndex(repositoryNode.Content, repositoryType); repositoryIndex != -1 {
Expand Down
16 changes: 16 additions & 0 deletions config/contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package config
import (
"fmt"

"github.com/pkg/errors"
"gopkg.in/yaml.v3"

"github.com/vmware-tanzu/tanzu-plugin-runtime/config/nodeutils"
Expand Down Expand Up @@ -246,6 +247,11 @@ func EndpointFromContext(s *configtypes.Context) (endpoint string, err error) {
}

func getContext(node *yaml.Node, name string) (*configtypes.Context, error) {
// check if context name is empty
if name == "" {
return nil, errors.New("context name cannot be empty")
}

cfg, err := convertNodeToClientConfig(node)
if err != nil {
return nil, err
Expand Down Expand Up @@ -285,6 +291,11 @@ func setContexts(node *yaml.Node, contexts []*configtypes.Context) (err error) {
}

func setContext(node *yaml.Node, ctx *configtypes.Context) (persist bool, err error) {
// Check if ctx.Name is empty
if ctx.Name == "" {
return false, errors.New("context name cannot be empty")
}

// Get Patch Strategies from config metadata
patchStrategies, err := GetConfigMetadataPatchStrategy()
if err != nil {
Expand Down Expand Up @@ -394,6 +405,11 @@ func removeCurrentContext(node *yaml.Node, ctx *configtypes.Context) error {

//nolint:dupl
func removeContext(node *yaml.Node, name string) error {
// check if context name is empty
if name == "" {
return errors.New("context name cannot be empty")
}

// Find the contexts node in the yaml node
keys := []nodeutils.Key{
{Name: KeyContexts},
Expand Down
Loading

0 comments on commit 20e9626

Please sign in to comment.