-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix a race condition when evaluating on the root context (#2096)
* Add an E2E test for race conditions * Fix a race condition when evaluating on the root context The root runner's Evaluator is shared across goroutines, so side effects on the CallStack are not goroutine safe. We solve this problem by moving the CallStack into a Scope for each evaluation.
- Loading branch information
Showing
11 changed files
with
272 additions
and
76 deletions.
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,3 @@ | ||
plugin "testing" { | ||
enabled = true | ||
} |
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,17 @@ | ||
locals { | ||
dns_name = "www.example.com" | ||
} | ||
|
||
resource "aws_route53_record" "www" { | ||
zone_id = aws_route53_zone.primary.zone_id | ||
name = local.dns_name | ||
type = "A" | ||
ttl = 300 | ||
records = [aws_eip.lb.public_ip] | ||
} | ||
|
||
module "route53_records" { | ||
count = 10 | ||
|
||
source = "./module" | ||
} |
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,7 @@ | ||
resource "aws_route53_record" "help" { | ||
zone_id = aws_route53_zone.primary.zone_id | ||
name = "help.example.com" | ||
type = "A" | ||
ttl = 300 | ||
records = [aws_eip.lb.public_ip] | ||
} |
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,25 @@ | ||
{ | ||
"issues": [ | ||
{ | ||
"rule": { | ||
"name": "aws_route53_record_eval_on_root_ctx_example", | ||
"severity": "error", | ||
"link": "" | ||
}, | ||
"message": "record name (root): \"www.example.com\"", | ||
"range": { | ||
"filename": "main.tf", | ||
"start": { | ||
"line": 7, | ||
"column": 13 | ||
}, | ||
"end": { | ||
"line": 7, | ||
"column": 27 | ||
} | ||
}, | ||
"callers": [] | ||
} | ||
], | ||
"errors": [] | ||
} |
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,116 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"strings" | ||
"testing" | ||
"text/template" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/terraform-linters/tflint/cmd" | ||
"github.com/terraform-linters/tflint/formatter" | ||
"github.com/terraform-linters/tflint/tflint" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
log.SetOutput(io.Discard) | ||
os.Exit(m.Run()) | ||
} | ||
|
||
type meta struct { | ||
Version string | ||
} | ||
|
||
func TestIntegration(t *testing.T) { | ||
cases := []struct { | ||
Name string | ||
Command string | ||
Dir string | ||
}{ | ||
{ | ||
// @see https://github.com/terraform-linters/tflint/issues/2094 | ||
Name: "eval locals on the root context in parallel runners", | ||
Command: "tflint --format json", | ||
Dir: "eval_locals_on_root_ctx", | ||
}, | ||
} | ||
|
||
// Disable the bundled plugin because the `os.Executable()` is go(1) in the tests | ||
tflint.DisableBundledPlugin = true | ||
defer func() { | ||
tflint.DisableBundledPlugin = false | ||
}() | ||
|
||
dir, _ := os.Getwd() | ||
for _, tc := range cases { | ||
t.Run(tc.Name, func(t *testing.T) { | ||
testDir := filepath.Join(dir, tc.Dir) | ||
|
||
defer func() { | ||
if err := os.Chdir(dir); err != nil { | ||
t.Fatal(err) | ||
} | ||
}() | ||
if err := os.Chdir(testDir); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
outStream, errStream := new(bytes.Buffer), new(bytes.Buffer) | ||
cli, err := cmd.NewCLI(outStream, errStream) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
args := strings.Split(tc.Command, " ") | ||
|
||
cli.Run(args) | ||
|
||
rawWant, err := readResultFile(testDir) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
var want *formatter.JSONOutput | ||
if err := json.Unmarshal(rawWant, &want); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
var got *formatter.JSONOutput | ||
if err := json.Unmarshal(outStream.Bytes(), &got); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if diff := cmp.Diff(got, want); diff != "" { | ||
t.Fatal(diff) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func readResultFile(dir string) ([]byte, error) { | ||
resultFile := "result.json" | ||
if runtime.GOOS == "windows" { | ||
if _, err := os.Stat(filepath.Join(dir, "result_windows.json")); !os.IsNotExist(err) { | ||
resultFile = "result_windows.json" | ||
} | ||
} | ||
if _, err := os.Stat(fmt.Sprintf("%s.tmpl", resultFile)); !os.IsNotExist(err) { | ||
resultFile = fmt.Sprintf("%s.tmpl", resultFile) | ||
} | ||
|
||
if !strings.HasSuffix(resultFile, ".tmpl") { | ||
return os.ReadFile(filepath.Join(dir, resultFile)) | ||
} | ||
|
||
want := new(bytes.Buffer) | ||
tmpl := template.Must(template.ParseFiles(filepath.Join(dir, resultFile))) | ||
if err := tmpl.Execute(want, meta{Version: tflint.Version.String()}); err != nil { | ||
return nil, err | ||
} | ||
return want.Bytes(), nil | ||
} |
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
Oops, something went wrong.