-
Notifications
You must be signed in to change notification settings - Fork 2
dispatch run integration tests
#62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
392ae3d
Add dispatch run tests
chicoxyzzy 02d622d
Do not silence dispatch run usage
chicoxyzzy befce27
add env variables priority tests
chicoxyzzy 3ce8f2a
fix dispatch run tests asserts
chicoxyzzy c08211b
improve dispatch run tests
chicoxyzzy d2bebe3
improve env file creation
chicoxyzzy cf37e41
update GH Workflow to build dispatch binary to be able to run integra…
chicoxyzzy 757e095
make dispatch run work without dispatch login
chicoxyzzy dd51616
improve a RegExp for matching non-existent dotenv file
chicoxyzzy 24bd9a5
improve error handling in dispatch run tests
chicoxyzzy 712c631
Add more comments on how errors are handled in execRunCommand
chicoxyzzy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "runtime" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| var dispatchBinary = filepath.Join("../build", runtime.GOOS, runtime.GOARCH, "dispatch") | ||
|
|
||
| func TestRunCommand(t *testing.T) { | ||
| t.Run("Run with non-existent env file", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| buff, err := execRunCommand(&[]string{}, "run", "--env-file", "non-existent.env", "--", "echo", "hello") | ||
| if err != nil { | ||
| t.Fatal(err.Error()) | ||
| } | ||
|
|
||
| assert.Regexp(t, "Error: failed to load env file from .+/dispatch/cli/non-existent.env: open non-existent.env: no such file or directory\n", buff.String()) | ||
| }) | ||
|
|
||
| t.Run("Run with env file", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| envFile, err := createEnvFile(t.TempDir(), []byte("CHARACTER=rick_sanchez")) | ||
| defer os.Remove(envFile) | ||
| if err != nil { | ||
| t.Fatalf("Failed to write env file: %v", err) | ||
| } | ||
|
|
||
| buff, err := execRunCommand(&[]string{}, "run", "--env-file", envFile, "--", "printenv", "CHARACTER") | ||
| if err != nil { | ||
| t.Fatal(err.Error()) | ||
| } | ||
|
|
||
| result, found := findEnvVariableInLogs(&buff) | ||
| if !found { | ||
| t.Fatalf("Expected printenv in the output: %s", buff.String()) | ||
| } | ||
| assert.Equal(t, "rick_sanchez", result, fmt.Sprintf("Expected 'printenv | rick_sanchez' in the output, got 'printenv | %s'", result)) | ||
| }) | ||
|
|
||
| t.Run("Run with env variable", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| // Set environment variables | ||
| envVars := []string{"CHARACTER=morty_smith"} | ||
|
|
||
| buff, err := execRunCommand(&envVars, "run", "--", "printenv", "CHARACTER") | ||
| if err != nil { | ||
| t.Fatal(err.Error()) | ||
| } | ||
|
|
||
| result, found := findEnvVariableInLogs(&buff) | ||
| if !found { | ||
| t.Fatalf("Expected printenv in the output: %s", buff.String()) | ||
| } | ||
| assert.Equal(t, "morty_smith", result, fmt.Sprintf("Expected 'printenv | morty_smith' in the output, got 'printenv | %s'", result)) | ||
| }) | ||
|
|
||
| t.Run("Run with env variable in command line has priority over the one in the env file", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| envFile, err := createEnvFile(t.TempDir(), []byte("CHARACTER=rick_sanchez")) | ||
| defer os.Remove(envFile) | ||
| if err != nil { | ||
| t.Fatalf("Failed to write env file: %v", err) | ||
| } | ||
|
|
||
| // Set environment variables | ||
| envVars := []string{"CHARACTER=morty_smith"} | ||
| buff, err := execRunCommand(&envVars, "run", "--env-file", envFile, "--", "printenv", "CHARACTER") | ||
| if err != nil { | ||
| t.Fatal(err.Error()) | ||
| } | ||
|
|
||
| result, found := findEnvVariableInLogs(&buff) | ||
| if !found { | ||
| t.Fatalf("Expected printenv in the output: %s", buff.String()) | ||
| } | ||
| assert.Equal(t, "morty_smith", result, fmt.Sprintf("Expected 'printenv | morty_smith' in the output, got 'printenv | %s'", result)) | ||
| }) | ||
|
|
||
| t.Run("Run with env variable in local env vars has priority over the one in the env file", func(t *testing.T) { | ||
| // Do not use t.Parallel() here as we are manipulating the environment! | ||
|
|
||
| // Set environment variables | ||
| os.Setenv("CHARACTER", "morty_smith") | ||
| defer os.Unsetenv("CHARACTER") | ||
|
|
||
| envFile, err := createEnvFile(t.TempDir(), []byte("CHARACTER=rick_sanchez")) | ||
| defer os.Remove(envFile) | ||
|
|
||
| if err != nil { | ||
| t.Fatalf("Failed to write env file: %v", err) | ||
| } | ||
|
|
||
| buff, err := execRunCommand(&[]string{}, "run", "--env-file", envFile, "--", "printenv", "CHARACTER") | ||
| if err != nil { | ||
| t.Fatal(err.Error()) | ||
| } | ||
|
|
||
| result, found := findEnvVariableInLogs(&buff) | ||
| if !found { | ||
| t.Fatalf("Expected printenv in the output: %s\n\n", buff.String()) | ||
| } | ||
| assert.Equal(t, "morty_smith", result, fmt.Sprintf("Expected 'printenv | morty_smith' in the output, got 'printenv | %s'", result)) | ||
| }) | ||
| } | ||
|
|
||
| func execRunCommand(envVars *[]string, arg ...string) (bytes.Buffer, error) { | ||
| // Create a context with a timeout to ensure the process doesn't run indefinitely | ||
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
| defer cancel() | ||
|
|
||
| // add the api key to the arguments so the command can run without `dispatch login` being run first | ||
| arg = append(arg[:1], append([]string{"--api-key", "00000000"}, arg[1:]...)...) | ||
|
|
||
| // Set up the command | ||
| cmd := exec.CommandContext(ctx, dispatchBinary, arg...) | ||
|
|
||
| if len(*envVars) != 0 { | ||
| // Set environment variables | ||
| cmd.Env = append(os.Environ(), *envVars...) | ||
| } | ||
|
|
||
| // Capture the standard error | ||
| var errBuf bytes.Buffer | ||
| cmd.Stderr = &errBuf | ||
|
|
||
| // Start the command | ||
| if err := cmd.Start(); err != nil { | ||
| return errBuf, fmt.Errorf("Failed to start command: %w", err) | ||
| } | ||
|
|
||
| // Wait for the command to finish or for the context to timeout | ||
| // We use Wait() instead of Run() so that we can capture the error | ||
| // For example: | ||
| // FOO=bar ./build/darwin/amd64/dispatch run -- printenv FOO | ||
| // This will exit with | ||
| // Error: command 'printenv FOO' exited unexpectedly | ||
| // but also it will print... | ||
| // printenv | bar | ||
| // to the logs and that is exactly what we want to test | ||
| // If context timeout occurs, than something went wrong | ||
| // and `dispatch run` is running indefinitely. | ||
| if err := cmd.Wait(); err != nil { | ||
| // Check if the error is due to context timeout (command running too long) | ||
| if ctx.Err() == context.DeadlineExceeded { | ||
| return errBuf, fmt.Errorf("Command timed out: %w", err) | ||
| } | ||
| } | ||
|
|
||
| return errBuf, nil | ||
| } | ||
|
|
||
| func createEnvFile(path string, content []byte) (string, error) { | ||
| envFile := filepath.Join(path, "test.env") | ||
| err := os.WriteFile(envFile, content, 0600) | ||
| return envFile, err | ||
| } | ||
|
|
||
| func findEnvVariableInLogs(buf *bytes.Buffer) (string, bool) { | ||
| var result string | ||
| found := false | ||
|
|
||
| // Split the log into lines | ||
| lines := strings.Split(buf.String(), "\n") | ||
|
|
||
| // Iterate over each line and check for the condition | ||
| for _, line := range lines { | ||
| if strings.Contains(line, "printenv | ") { | ||
| result = strings.Split(line, "printenv | ")[1] | ||
| found = true | ||
| break | ||
| } | ||
| } | ||
| return result, found | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The context error will always carry information about this being a timeout, could we simplify the code by removing this special case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will do
exit 1onprintenv ENV_VARIABLE, so I've added this check to be sure that this command exited properly and we can readstderrnow to make sureENV_VARIABLEhas correct value (whenerrisnil).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 I'm not sure I understand the condition in which we would get a timeout, but if you know it's important here it's fine (maybe worth a comment to record that context).