Skip to content

Commit

Permalink
Merge pull request #140 from rakshitgondwal/test/add-tests-for-args
Browse files Browse the repository at this point in the history
test: add tests for args.go
  • Loading branch information
k8s-ci-robot authored Feb 3, 2024
2 parents c1ee232 + 659df91 commit d94c0a9
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 19 deletions.
4 changes: 3 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ var rootCmd = &cobra.Command{
} else if listImages {
service.PrintListImages(client.ClientSet)
} else {
common.ValidateArgs()
if err := common.ValidateArgs(); err != nil {
log.Fatal(err)
}

service.RunE2E(client.ClientSet)
client.PrintE2ELogs()
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21
require (
github.com/lmittmann/tint v1.0.3
github.com/mattn/go-isatty v0.0.20
github.com/stretchr/testify v1.8.4
k8s.io/api v0.29.1
k8s.io/apimachinery v0.29.1
k8s.io/client-go v0.29.1
Expand Down Expand Up @@ -40,6 +41,7 @@ require (
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
Expand Down
11 changes: 6 additions & 5 deletions pkg/common/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ func PrintInfo(clientSet *kubernetes.Clientset, config *rest.Config) {

// ValidateArgs validates the arguments passed to the program
// and creates the output directory if it doesn't exist
func ValidateArgs() {

func ValidateArgs() error {
if viper.Get("namespace") == "" {
viper.Set("namespace", DefaultNamespace)
}
Expand All @@ -63,11 +64,11 @@ func ValidateArgs() {
for _, kv := range extraArgs {
keyValuePair := strings.SplitN(kv, "=", 2)
if len(keyValuePair) != 2 {
log.Fatalf("Expected [%s] in [%s] to be of --key=value format", keyValuePair, extraArgs)
return fmt.Errorf("expected [%s] in [%s] to be of --key=value format", keyValuePair, extraArgs)
}
key := keyValuePair[0]
if !strings.HasPrefix(key, "--") && strings.Count(key, "--") != 1 {
log.Fatalf("Expected key [%s] in [%s] to start with prefix --", key, extraArgs)
return fmt.Errorf("expected key [%s] in [%s] to start with prefix --", key, extraArgs)
}
}
}
Expand All @@ -81,8 +82,8 @@ func ValidateArgs() {
outputDir := viper.GetString("output-dir")
if _, err := os.Stat(outputDir); os.IsNotExist(err) {
if err = os.MkdirAll(outputDir, 0755); err != nil {
log.Fatalf("Error creating output directory [%s] : %v", outputDir, err)
return fmt.Errorf("error creating output directory [%s] : %v", outputDir, err)
}
}

return nil
}
54 changes: 41 additions & 13 deletions pkg/common/args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ limitations under the License.
package common

import (
"reflect"
"testing"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)

func TestValidateArgs(t *testing.T) {
Expand All @@ -33,24 +33,55 @@ func TestValidateArgs(t *testing.T) {
name string
focus string
expectedFocus string
skip string
expectedSkip string
extraArgs []string
expectedArgs []string
wantErr bool
expectedErr string
}{
{
name: "With focus",
focus: "\\[E2E\\]",
expectedFocus: "\\[E2E\\]",
extraArgs: []string{},
expectedArgs: []string{},
wantErr: false,
expectedErr: "",
},
{
name: "With extra args",
focus: "",
expectedFocus: "\\[Conformance\\]",
extraArgs: []string{"--key1=value1", "--key2=value2"},
expectedArgs: []string{"--key1=value1", "--key2=value2"},
wantErr: false,
expectedErr: "",
},
{
name: "Invalid extra args format",
focus: "",
expectedFocus: "\\[Conformance\\]",
extraArgs: []string{"invalid-arg"},
expectedArgs: []string{},
wantErr: true,
expectedErr: "expected [[invalid-arg]] in [[invalid-arg]] to be of --key=value format",
},
{
name: "Extra args with missing values",
focus: "",
expectedFocus: "\\[Conformance\\]",
extraArgs: []string{"--key1=value1", "--key2"},
expectedArgs: []string{},
wantErr: true,
expectedErr: "expected [[--key2]] in [[--key1=value1 --key2]] to be of --key=value format",
},
{
name: "Extra args with invalid key format",
focus: "",
expectedFocus: "\\[Conformance\\]",
extraArgs: []string{"key1=value1", "--key2=value2"},
expectedArgs: []string{},
wantErr: true,
expectedErr: "expected key [key1] in [[key1=value1 --key2=value2]] to start with prefix --",
},
}

Expand All @@ -59,19 +90,16 @@ func TestValidateArgs(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
// Set up the test environment
viper.Set("focus", tc.focus)
viper.Set("skip", tc.skip)
viper.Set("extra-args", tc.extraArgs)

// Call the function under test
ValidateArgs()
if viper.GetString("skip") != tc.expectedSkip {
t.Errorf("expected skip to be [%s], got [%s]", tc.expectedSkip, viper.GetString("skip"))
}
if viper.GetString("focus") != tc.expectedFocus {
t.Errorf("expected focus to be [%s], got [%s]", tc.expectedFocus, viper.GetString("focus"))
}
if !reflect.DeepEqual(viper.GetStringSlice("extra-args"), tc.expectedArgs) {
t.Errorf("expected extra-args to be [%v], got [%v]", tc.expectedArgs, viper.GetStringSlice("extra-args"))
err := ValidateArgs()
if tc.wantErr {
assert.EqualError(t, err, tc.expectedErr)
} else {
assert.Nil(t, err)
assert.Equal(t, viper.GetString("focus"), tc.expectedFocus)
assert.Equal(t, viper.GetStringSlice("extra-args"), tc.expectedArgs)
}
})
}
Expand Down

0 comments on commit d94c0a9

Please sign in to comment.