Skip to content

Commit 5fbdc92

Browse files
authored
Add E2E tests (#341)
1 parent b23cf9f commit 5fbdc92

File tree

23 files changed

+406
-1
lines changed

23 files changed

+406
-1
lines changed

.github/workflows/e2e.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: e2e
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
schedule:
11+
- cron: "0 0 * * *"
12+
13+
jobs:
14+
e2e:
15+
name: ${{ matrix.os }} (${{ matrix.version }})
16+
runs-on: ${{ matrix.os }}
17+
strategy:
18+
matrix:
19+
os: [ubuntu-latest, windows-latest]
20+
version: [v0.35.0, latest]
21+
env:
22+
TFLINT_VERSION: ${{ matrix.version }}
23+
steps:
24+
- name: Checkout
25+
uses: actions/checkout@v3
26+
- name: Set up Go
27+
uses: actions/setup-go@v2
28+
with:
29+
go-version: 1.18
30+
- name: Install TFLint
31+
run: curl -sL https://raw.githubusercontent.com/terraform-linters/tflint/master/install_linux.sh | bash
32+
- name: Install plugin (Linux)
33+
if: runner.os == 'Linux'
34+
run: make install
35+
- name: Install plugin (Windows)
36+
if: runner.os == 'Windows'
37+
run: |
38+
mkdir -p ~/.tflint.d/plugins
39+
go build -o ~/.tflint.d/plugins/tflint-ruleset-aws.exe
40+
shell: bash
41+
- name: Run E2E tests
42+
run: make e2e

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
default: build
22

33
test:
4-
go test ./...
4+
go test $$(go list ./... | grep -v integration)
5+
6+
e2e:
7+
cd integration && go test && cd ../
58

69
build:
710
go build

integration/basic/.tflint.hcl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
plugin "aws" {
2+
enabled = true
3+
}

integration/basic/result.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"issues": [
3+
{
4+
"rule": {
5+
"name": "aws_instance_invalid_type",
6+
"severity": "error",
7+
"link": ""
8+
},
9+
"message": "\"x2.2xlarge\" is an invalid value as instance_type",
10+
"range": {
11+
"filename": "template.tf",
12+
"start": {
13+
"line": 2,
14+
"column": 19
15+
},
16+
"end": {
17+
"line": 2,
18+
"column": 31
19+
}
20+
},
21+
"callers": []
22+
}
23+
],
24+
"errors": []
25+
}

integration/basic/template.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
resource "aws_instance" "foo" {
2+
instance_type = "x2.2xlarge"
3+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
plugin "aws" {
2+
enabled = true
3+
}
4+
5+
rule "aws_resource_missing_tags" {
6+
enabled = true
7+
tags = ["Environment", "Name", "Type"]
8+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"issues": [
3+
{
4+
"rule": {
5+
"name": "aws_resource_missing_tags",
6+
"severity": "info",
7+
"link": "https://github.com/terraform-linters/tflint-ruleset-aws/blob/v0.13.3/docs/rules/aws_resource_missing_tags.md"
8+
},
9+
"message": "The resource is missing the following tags: \"Environment\", \"Name\", \"Type\".",
10+
"range": {
11+
"filename": "template.tf",
12+
"start": {
13+
"line": 5,
14+
"column": 1
15+
},
16+
"end": {
17+
"line": 5,
18+
"column": 41
19+
}
20+
},
21+
"callers": []
22+
}
23+
],
24+
"errors": []
25+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
variable "tags" {
2+
default = []
3+
}
4+
5+
resource "aws_autoscaling_group" "group" {
6+
tags = var.tags
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
plugin "aws" {
2+
enabled = true
3+
}
4+
5+
rule "aws_instance_invalid_ami" {
6+
enabled = false
7+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"issues": [],
3+
"errors": []
4+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
plugin "aws" {
2+
enabled = true
3+
}
4+
5+
rule "aws_instance_invalid_type" {
6+
enabled = true
7+
}
8+
9+
rule "aws_instance_previous_type" {
10+
enabled = false
11+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"issues": [
3+
{
4+
"rule": {
5+
"name": "aws_instance_invalid_type",
6+
"severity": "error",
7+
"link": ""
8+
},
9+
"message": "\"x2.2xlarge\" is an invalid value as instance_type",
10+
"range": {
11+
"filename": "template.tf",
12+
"start": {
13+
"line": 2,
14+
"column": 19
15+
},
16+
"end": {
17+
"line": 2,
18+
"column": 31
19+
}
20+
},
21+
"callers": []
22+
}
23+
],
24+
"errors": []
25+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
resource "aws_instance" "invalid" {
2+
instance_type = "x2.2xlarge"
3+
}
4+
5+
resource "aws_instance" "previous" {
6+
instance_type = "t1.micro"
7+
}

integration/heredoc/.tflint.hcl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
plugin "aws" {
2+
enabled = true
3+
}

integration/heredoc/result.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"issues": [
3+
{
4+
"rule": {
5+
"name": "aws_ecr_lifecycle_policy_invalid_policy",
6+
"severity": "error",
7+
"link": ""
8+
},
9+
"message": "policy must be 100 characters or higher",
10+
"range": {
11+
"filename": "template.tf",
12+
"start": {
13+
"line": 2,
14+
"column": 16
15+
},
16+
"end": {
17+
"line": 4,
18+
"column": 4
19+
}
20+
},
21+
"callers": []
22+
}
23+
],
24+
"errors": []
25+
}

integration/heredoc/template.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
resource "aws_ecr_lifecycle_policy" "test" {
2+
policy = <<EOF
3+
{}
4+
EOF
5+
}

integration/integration_test.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package integration
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"os"
7+
"os/exec"
8+
"path/filepath"
9+
"runtime"
10+
"testing"
11+
12+
"github.com/google/go-cmp/cmp"
13+
)
14+
15+
func TestIntegration(t *testing.T) {
16+
cases := []struct {
17+
Name string
18+
Command *exec.Cmd
19+
Dir string
20+
}{
21+
{
22+
Name: "basic",
23+
Command: exec.Command("tflint", "--format", "json", "--force"),
24+
Dir: "basic",
25+
},
26+
{
27+
// Regression: https://github.com/terraform-linters/tflint-ruleset-aws/issues/41
28+
Name: "rule config",
29+
Command: exec.Command("tflint", "--format", "json", "--force"),
30+
Dir: "rule-config",
31+
},
32+
{
33+
// Regression: https://github.com/terraform-linters/tflint/issues/1028
34+
Name: "deep checking rule config",
35+
Command: exec.Command("tflint", "--format", "json", "--force"),
36+
Dir: "deep-checking-rule-config",
37+
},
38+
{
39+
// Regression: https://github.com/terraform-linters/tflint/issues/1029
40+
Name: "heredoc",
41+
Command: exec.Command("tflint", "--format", "json", "--force"),
42+
Dir: "heredoc",
43+
},
44+
{
45+
// Regression: https://github.com/terraform-linters/tflint/issues/1054
46+
Name: "disabled-rules",
47+
Command: exec.Command("tflint", "--format", "json", "--force"),
48+
Dir: "disabled-rules",
49+
},
50+
{
51+
// Regression: https://github.com/terraform-linters/tflint-ruleset-aws/issues/48
52+
Name: "cty-based-eval",
53+
Command: exec.Command("tflint", "--format", "json", "--force"),
54+
Dir: "cty-based-eval",
55+
},
56+
{
57+
// Regression: https://github.com/terraform-linters/tflint/issues/1102
58+
Name: "map-attribute",
59+
Command: exec.Command("tflint", "--format", "json", "--force"),
60+
Dir: "map-attribute",
61+
},
62+
{
63+
// Regression: https://github.com/terraform-linters/tflint/issues/1103
64+
Name: "rule config with --enable-rule",
65+
Command: exec.Command("tflint", "--enable-rule", "aws_s3_bucket_name", "--format", "json", "--force"),
66+
Dir: "rule-config",
67+
},
68+
{
69+
Name: "rule config with --only",
70+
Command: exec.Command("tflint", "--only", "aws_s3_bucket_name", "--format", "json", "--force"),
71+
Dir: "rule-config",
72+
},
73+
}
74+
75+
dir, _ := os.Getwd()
76+
defer os.Chdir(dir)
77+
78+
for _, tc := range cases {
79+
t.Run(tc.Name, func(t *testing.T) {
80+
testDir := filepath.Join(dir, tc.Dir)
81+
82+
t.Cleanup(func() {
83+
if err := os.Chdir(dir); err != nil {
84+
t.Fatal(err)
85+
}
86+
})
87+
88+
if err := os.Chdir(testDir); err != nil {
89+
t.Fatal(err)
90+
}
91+
92+
var stdout, stderr bytes.Buffer
93+
tc.Command.Stdout = &stdout
94+
tc.Command.Stderr = &stderr
95+
if err := tc.Command.Run(); err != nil {
96+
t.Fatalf("%s, stdout=%s stderr=%s", err, stdout.String(), stderr.String())
97+
}
98+
99+
var b []byte
100+
var err error
101+
if runtime.GOOS == "windows" && IsWindowsResultExist() {
102+
b, err = os.ReadFile(filepath.Join(testDir, "result_windows.json"))
103+
} else {
104+
b, err = os.ReadFile(filepath.Join(testDir, "result.json"))
105+
}
106+
if err != nil {
107+
t.Fatal(err)
108+
}
109+
110+
var expected interface{}
111+
if err := json.Unmarshal(b, &expected); err != nil {
112+
t.Fatal(err)
113+
}
114+
115+
var got interface{}
116+
if err := json.Unmarshal(stdout.Bytes(), &got); err != nil {
117+
t.Fatal(err)
118+
}
119+
120+
if diff := cmp.Diff(got, expected); diff != "" {
121+
t.Fatal(diff)
122+
}
123+
})
124+
}
125+
}
126+
127+
func IsWindowsResultExist() bool {
128+
_, err := os.Stat("result_windows.json")
129+
return !os.IsNotExist(err)
130+
}

integration/map-attribute/.tflint.hcl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
plugin "aws" {
2+
enabled = true
3+
}
4+
5+
rule "aws_resource_missing_tags" {
6+
enabled = true
7+
tags = ["Environment", "Name", "Type"]
8+
}

integration/map-attribute/result.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"issues": [
3+
{
4+
"rule": {
5+
"name": "aws_resource_missing_tags",
6+
"severity": "info",
7+
"link": "https://github.com/terraform-linters/tflint-ruleset-aws/blob/v0.13.3/docs/rules/aws_resource_missing_tags.md"
8+
},
9+
"message": "The resource is missing the following tags: \"Environment\", \"Name\", \"Type\".",
10+
"range": {
11+
"filename": "template.tf",
12+
"start": {
13+
"line": 2,
14+
"column": 10
15+
},
16+
"end": {
17+
"line": 2,
18+
"column": 25
19+
}
20+
},
21+
"callers": []
22+
}
23+
],
24+
"errors": []
25+
}

integration/map-attribute/template.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
resource "aws_instance" "intance" {
2+
tags = { foo = "bar" }
3+
}

0 commit comments

Comments
 (0)