Skip to content

Commit ed41f9e

Browse files
authored
fix(cli): adding validation when trying to run resource that does not exist (#3895)
* fix(cli): adding validation when trying to run resource that does not exist * fix tests
1 parent 573877e commit ed41f9e

File tree

4 files changed

+100
-3
lines changed

4 files changed

+100
-3
lines changed

cli/cmd/resource_run_cmd.go

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/kubeshop/tracetest/cli/cmdutil"
99
"github.com/kubeshop/tracetest/cli/config"
1010
"github.com/kubeshop/tracetest/cli/openapi"
11+
"github.com/kubeshop/tracetest/cli/pkg/fileutil"
1112
"github.com/kubeshop/tracetest/cli/runner"
1213
"github.com/spf13/cobra"
1314

@@ -19,6 +20,8 @@ var (
1920
runCmd *cobra.Command
2021
)
2122

23+
const ExitCodeResourceNotFound = 3
24+
2225
func init() {
2326
runCmd = &cobra.Command{
2427
GroupID: cmdGroupResources.ID,
@@ -29,9 +32,7 @@ func init() {
2932
Run: WithResourceMiddleware(func(ctx context.Context, _ *cobra.Command, args []string) (string, error) {
3033
runParams.ResourceName = resourceParams.ResourceName
3134
if cliConfig.Jwt != "" {
32-
exitCode, err := cloudCmd.RunMultipleFiles(ctx, httpClient, runParams, &cliConfig, runnerRegistry, output)
33-
ExitCLI(exitCode)
34-
return "", err
35+
return runMultipleFiles(ctx)
3536
}
3637

3738
return runSingleFile(ctx)
@@ -71,6 +72,12 @@ func runSingleFile(ctx context.Context) (string, error) {
7172
var definitionFile string
7273
if len(runParams.DefinitionFiles) > 0 {
7374
definitionFile = runParams.DefinitionFiles[0]
75+
76+
if !hasValidResourceFiles() {
77+
cliLogger.Warn("Invalid definition file found, stopping execution")
78+
ExitCLI(ExitCodeResourceNotFound)
79+
return "", nil
80+
}
7481
}
7582

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

104+
func runMultipleFiles(ctx context.Context) (string, error) {
105+
if !hasValidResourceFiles() {
106+
cliLogger.Warn("Invalid definition files found, stopping execution")
107+
ExitCLI(ExitCodeResourceNotFound)
108+
return "", nil
109+
}
110+
111+
exitCode, err := cloudCmd.RunMultipleFiles(ctx, httpClient, runParams, &cliConfig, runnerRegistry, output)
112+
ExitCLI(exitCode)
113+
return "", err
114+
}
115+
97116
func validRequiredGatesMsg() string {
98117
opts := make([]string, 0, len(openapi.AllowedSupportedGatesEnumValues))
99118
for _, v := range openapi.AllowedSupportedGatesEnumValues {
@@ -102,3 +121,20 @@ func validRequiredGatesMsg() string {
102121

103122
return "valid options: " + strings.Join(opts, ", ")
104123
}
124+
125+
func hasValidResourceFiles() bool {
126+
if len(runParams.DefinitionFiles) == 0 {
127+
return true
128+
}
129+
130+
validResourceFiles := true
131+
132+
for _, file := range runParams.DefinitionFiles {
133+
if !fileutil.IsExistingFile(file) {
134+
cliLogger.Warn("Definition file does not exist: " + file)
135+
validResourceFiles = false
136+
}
137+
}
138+
139+
return validResourceFiles
140+
}

cli/pkg/fileutil/path.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,8 @@ func IsFilePathToRelativeDir(path, dir string) bool {
5858

5959
return IsFilePath(fullPath)
6060
}
61+
62+
func IsExistingFile(path string) bool {
63+
_, err := os.Stat(path)
64+
return err == nil
65+
}

testing/cli-e2etest/testscenarios/test/run_test_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,31 @@ func TestRunTestWithKafkaTrigger(t *testing.T) {
221221
require.Contains(result.StdOut, "✔ A message was received from Kafka stream")
222222
})
223223
}
224+
225+
func TestRunTestWithInvalidArgs(t *testing.T) {
226+
// setup isolated e2e environment
227+
env := environment.CreateAndStart(t, environment.WithDataStoreEnabled())
228+
defer env.Close(t)
229+
230+
cliConfig := env.GetCLIConfigPath(t)
231+
232+
t.Run("should return an error message", func(t *testing.T) {
233+
// instantiate require with testing helper
234+
require := require.New(t)
235+
236+
// Given I am a Tracetest CLI user
237+
// And I have my server recently created
238+
// And the datasource is already set
239+
240+
// When I try to run a test with an invalid file
241+
// Then it should give an error message
242+
testFile := "./invalid-file.yaml"
243+
244+
command := fmt.Sprintf("run test -f %s", testFile)
245+
result := tracetestcli.Exec(t, command, tracetestcli.WithCLIConfig(cliConfig))
246+
helpers.RequireExitCodeEqual(t, result, 3)
247+
248+
// checks if the error message is valid
249+
require.Contains(result.StdOut, "Invalid definition file found, stopping execution")
250+
})
251+
}

testing/cli-e2etest/testscenarios/testsuite/run_testsuite_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,31 @@ func TestRunTestSuite(t *testing.T) {
8383
require.Contains(result.StdOut, "✔ It should Get Pokemons correctly")
8484
})
8585
}
86+
87+
func TestSuiteRunTestWithInvalidArgs(t *testing.T) {
88+
// setup isolated e2e environment
89+
env := environment.CreateAndStart(t, environment.WithDataStoreEnabled())
90+
defer env.Close(t)
91+
92+
cliConfig := env.GetCLIConfigPath(t)
93+
94+
t.Run("should return an error message", func(t *testing.T) {
95+
// instantiate require with testing helper
96+
require := require.New(t)
97+
98+
// Given I am a Tracetest CLI user
99+
// And I have my server recently created
100+
// And the datasource is already set
101+
102+
// When I try to run a test with an invalid file
103+
// Then it should give an error message
104+
testFile := "./invalid-file.yaml"
105+
106+
command := fmt.Sprintf("run testsuite -f %s", testFile)
107+
result := tracetestcli.Exec(t, command, tracetestcli.WithCLIConfig(cliConfig))
108+
helpers.RequireExitCodeEqual(t, result, 3)
109+
110+
// checks if the error message is valid
111+
require.Contains(result.StdOut, "Invalid definition file found, stopping execution")
112+
})
113+
}

0 commit comments

Comments
 (0)