Skip to content

Commit

Permalink
fix: added flags validation into create test (#1371)
Browse files Browse the repository at this point in the history
* fix: added flags validation into create test

* fix: printf remove
  • Loading branch information
exu committed Apr 22, 2022
1 parent 59e8607 commit 0505461
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion cmd/kubectl-testkube/commands/tests/create.go
Expand Up @@ -48,13 +48,18 @@ func NewCreateTestsCmd() *cobra.Command {
ui.Failf("Test with name '%s' already exists in namespace %s", testName, testNamespace)
}

err := validateCreateOptions(cmd)
ui.ExitOnError("validating passed flags", err)

options, err := NewUpsertTestOptionsFromFlags(cmd, test)
ui.ExitOnError("getting test options", err)

executors, err := client.ListExecutors()
ui.ExitOnError("getting available executors", err)

err = validateExecutorType(options.Type_, executors)
ui.ExitOnError("validating executor type", err)

err = validateSchedule(options.Schedule)
ui.ExitOnError("validating schedule", err)

Expand All @@ -66,11 +71,12 @@ func NewCreateTestsCmd() *cobra.Command {
}

cmd.Flags().StringVarP(&testName, "name", "n", "", "unique test name - mandatory")
cmd.Flags().StringVarP(&file, "file", "f", "", "test file - will be read from stdin if not specified")
cmd.Flags().StringVarP(&testContentType, "test-content-type", "", "", "content type of test one of string|file-uri|git-file|git-dir")

cmd.Flags().StringVarP(&executorType, "type", "t", "", "test type (defaults to postman/collection)")

// create options
cmd.Flags().StringVarP(&file, "file", "f", "", "test file - will be read from stdin if not specified")
cmd.Flags().StringVarP(&uri, "uri", "", "", "URI of resource - will be loaded by http GET")
cmd.Flags().StringVarP(&gitUri, "git-uri", "", "", "Git repository uri")
cmd.Flags().StringVarP(&gitBranch, "git-branch", "", "", "if uri is git repository we can set additional branch parameter")
Expand All @@ -84,6 +90,41 @@ func NewCreateTestsCmd() *cobra.Command {
return cmd
}

func validateCreateOptions(cmd *cobra.Command) error {
gitUri := cmd.Flag("git-uri").Value.String()
gitBranch := cmd.Flag("git-branch").Value.String()
gitPath := cmd.Flag("git-path").Value.String()
gitUsername := cmd.Flag("git-username").Value.String()
gitToken := cmd.Flag("git-token").Value.String()

file := cmd.Flag("file").Value.String()
uri := cmd.Flag("uri").Value.String()

hasGitParams := gitBranch != "" || gitPath != "" || gitUri != "" || gitToken != "" || gitUsername != ""

if hasGitParams && uri != "" {
return fmt.Errorf("found git params and `--uri` flag, please use `--git-uri` for git based repo or `--uri` without git based params")
}
if hasGitParams && file != "" {
return fmt.Errorf("found git params and `--file` flag, please use `--git-uri` for git based repo or `--file` without git based params")
}

if file != "" && uri != "" {
return fmt.Errorf("please pass only one of `--file` and `--uri`")
}

if hasGitParams {
if gitUri == "" {
return fmt.Errorf("please pass valid `--git-uri` flag")
}
if gitBranch == "" {
return fmt.Errorf("please pass valid `--git-branch` flag")
}
}

return nil
}

func validateExecutorType(executorType string, executors testkube.ExecutorsDetails) error {
typeValid := false
executorTypes := []string{}
Expand Down

0 comments on commit 0505461

Please sign in to comment.