Skip to content

Commit

Permalink
fix(cli): adding validation when trying to run resource that does not…
Browse files Browse the repository at this point in the history
… exist (kubeshop#3895)

* fix(cli): adding validation when trying to run resource that does not exist

* fix tests
  • Loading branch information
danielbdias authored Jun 10, 2024
1 parent 573877e commit ed41f9e
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 3 deletions.
42 changes: 39 additions & 3 deletions cli/cmd/resource_run_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/kubeshop/tracetest/cli/cmdutil"
"github.com/kubeshop/tracetest/cli/config"
"github.com/kubeshop/tracetest/cli/openapi"
"github.com/kubeshop/tracetest/cli/pkg/fileutil"
"github.com/kubeshop/tracetest/cli/runner"
"github.com/spf13/cobra"

Expand All @@ -19,6 +20,8 @@ var (
runCmd *cobra.Command
)

const ExitCodeResourceNotFound = 3

func init() {
runCmd = &cobra.Command{
GroupID: cmdGroupResources.ID,
Expand All @@ -29,9 +32,7 @@ func init() {
Run: WithResourceMiddleware(func(ctx context.Context, _ *cobra.Command, args []string) (string, error) {
runParams.ResourceName = resourceParams.ResourceName
if cliConfig.Jwt != "" {
exitCode, err := cloudCmd.RunMultipleFiles(ctx, httpClient, runParams, &cliConfig, runnerRegistry, output)
ExitCLI(exitCode)
return "", err
return runMultipleFiles(ctx)
}

return runSingleFile(ctx)
Expand Down Expand Up @@ -71,6 +72,12 @@ func runSingleFile(ctx context.Context) (string, error) {
var definitionFile string
if len(runParams.DefinitionFiles) > 0 {
definitionFile = runParams.DefinitionFiles[0]

if !hasValidResourceFiles() {
cliLogger.Warn("Invalid definition file found, stopping execution")
ExitCLI(ExitCodeResourceNotFound)
return "", nil
}
}

ID := ""
Expand All @@ -94,6 +101,18 @@ func runSingleFile(ctx context.Context) (string, error) {
return "", err
}

func runMultipleFiles(ctx context.Context) (string, error) {
if !hasValidResourceFiles() {
cliLogger.Warn("Invalid definition files found, stopping execution")
ExitCLI(ExitCodeResourceNotFound)
return "", nil
}

exitCode, err := cloudCmd.RunMultipleFiles(ctx, httpClient, runParams, &cliConfig, runnerRegistry, output)
ExitCLI(exitCode)
return "", err
}

func validRequiredGatesMsg() string {
opts := make([]string, 0, len(openapi.AllowedSupportedGatesEnumValues))
for _, v := range openapi.AllowedSupportedGatesEnumValues {
Expand All @@ -102,3 +121,20 @@ func validRequiredGatesMsg() string {

return "valid options: " + strings.Join(opts, ", ")
}

func hasValidResourceFiles() bool {
if len(runParams.DefinitionFiles) == 0 {
return true
}

validResourceFiles := true

for _, file := range runParams.DefinitionFiles {
if !fileutil.IsExistingFile(file) {
cliLogger.Warn("Definition file does not exist: " + file)
validResourceFiles = false
}
}

return validResourceFiles
}
5 changes: 5 additions & 0 deletions cli/pkg/fileutil/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,8 @@ func IsFilePathToRelativeDir(path, dir string) bool {

return IsFilePath(fullPath)
}

func IsExistingFile(path string) bool {
_, err := os.Stat(path)
return err == nil
}
28 changes: 28 additions & 0 deletions testing/cli-e2etest/testscenarios/test/run_test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,31 @@ func TestRunTestWithKafkaTrigger(t *testing.T) {
require.Contains(result.StdOut, "✔ A message was received from Kafka stream")
})
}

func TestRunTestWithInvalidArgs(t *testing.T) {
// setup isolated e2e environment
env := environment.CreateAndStart(t, environment.WithDataStoreEnabled())
defer env.Close(t)

cliConfig := env.GetCLIConfigPath(t)

t.Run("should return an error message", func(t *testing.T) {
// instantiate require with testing helper
require := require.New(t)

// Given I am a Tracetest CLI user
// And I have my server recently created
// And the datasource is already set

// When I try to run a test with an invalid file
// Then it should give an error message
testFile := "./invalid-file.yaml"

command := fmt.Sprintf("run test -f %s", testFile)
result := tracetestcli.Exec(t, command, tracetestcli.WithCLIConfig(cliConfig))
helpers.RequireExitCodeEqual(t, result, 3)

// checks if the error message is valid
require.Contains(result.StdOut, "Invalid definition file found, stopping execution")
})
}
28 changes: 28 additions & 0 deletions testing/cli-e2etest/testscenarios/testsuite/run_testsuite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,31 @@ func TestRunTestSuite(t *testing.T) {
require.Contains(result.StdOut, "✔ It should Get Pokemons correctly")
})
}

func TestSuiteRunTestWithInvalidArgs(t *testing.T) {
// setup isolated e2e environment
env := environment.CreateAndStart(t, environment.WithDataStoreEnabled())
defer env.Close(t)

cliConfig := env.GetCLIConfigPath(t)

t.Run("should return an error message", func(t *testing.T) {
// instantiate require with testing helper
require := require.New(t)

// Given I am a Tracetest CLI user
// And I have my server recently created
// And the datasource is already set

// When I try to run a test with an invalid file
// Then it should give an error message
testFile := "./invalid-file.yaml"

command := fmt.Sprintf("run testsuite -f %s", testFile)
result := tracetestcli.Exec(t, command, tracetestcli.WithCLIConfig(cliConfig))
helpers.RequireExitCodeEqual(t, result, 3)

// checks if the error message is valid
require.Contains(result.StdOut, "Invalid definition file found, stopping execution")
})
}

0 comments on commit ed41f9e

Please sign in to comment.