-
Notifications
You must be signed in to change notification settings - Fork 117
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
Test for runTerraformCmd leaked go-routine #299
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
400f9d7
Update TestContext_sleepTimeoutExpired to check for canceling within …
lornasong b4e272e
Remove runTerraformCmd check for cmd.ProcessState
lornasong c3ccdc3
Read logs from Stderr/out Pipe to avoid hanging
lornasong dc62a0f
Test for runTerraformCmd leaked go-routine
lornasong 53bfc40
Use CommandContext to kill instead of manually doing it
kylecarbs 11f14d2
Fix EOF error check to use error.Is()
lornasong 594b411
Update tests to use separate string.Builder-s for stdout and stderr
lornasong 5a93b1e
Fixes to runTerraformCmd for race conditions
lornasong 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 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 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 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,39 @@ | ||
//go:build !linux | ||
// +build !linux | ||
|
||
package tfexec | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"log" | ||
"strings" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func Test_runTerraformCmd_default(t *testing.T) { | ||
// Checks runTerraformCmd for race condition when using | ||
// go test -race -run Test_runTerraformCmd_default ./tfexec | ||
var buf bytes.Buffer | ||
|
||
tf := &Terraform{ | ||
logger: log.New(&buf, "", 0), | ||
execPath: "echo", | ||
} | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
|
||
cmd := tf.buildTerraformCmd(ctx, nil, "hello tf-exec!") | ||
err := tf.runTerraformCmd(ctx, cmd) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Cancel stops the leaked go routine which logs an error | ||
cancel() | ||
time.Sleep(time.Second) | ||
if strings.Contains(buf.String(), "error from kill") { | ||
t.Fatal("canceling context should not lead to logging an error") | ||
} | ||
} |
This file contains 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 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,36 @@ | ||
package tfexec | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"log" | ||
"strings" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func Test_runTerraformCmd_linux(t *testing.T) { | ||
// Checks runTerraformCmd for race condition when using | ||
// go test -race -run Test_runTerraformCmd_linux ./tfexec -tags=linux | ||
var buf bytes.Buffer | ||
|
||
tf := &Terraform{ | ||
logger: log.New(&buf, "", 0), | ||
execPath: "echo", | ||
} | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
|
||
cmd := tf.buildTerraformCmd(ctx, nil, "hello tf-exec!") | ||
err := tf.runTerraformCmd(ctx, cmd) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Cancel stops the leaked go routine which logs an error | ||
cancel() | ||
time.Sleep(time.Second) | ||
if strings.Contains(buf.String(), "error from kill") { | ||
t.Fatal("canceling context should not lead to logging an error") | ||
} | ||
} |
This file contains 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 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
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.
I appreciate the comment ❤️