forked from terraform-linters/tflint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration_test.go
126 lines (112 loc) · 2.49 KB
/
integration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/terraform-linters/tflint/cmd"
"github.com/terraform-linters/tflint/formatter"
)
func TestMain(m *testing.M) {
log.SetOutput(ioutil.Discard)
os.Exit(m.Run())
}
func TestIntegration(t *testing.T) {
cases := []struct {
Name string
Command string
Env map[string]string
Dir string
}{
{
Name: "basic",
Command: "./tflint --format json",
Dir: "basic",
},
{
Name: "override",
Command: "./tflint --format json",
Dir: "override",
},
{
Name: "variables",
Command: "./tflint --format json --var-file variables.tfvars --var var=var",
Dir: "variables",
},
{
Name: "module",
Command: "./tflint --format json --module --ignore-module ./ignore_module",
Dir: "module",
},
{
Name: "without_module_init",
Command: "./tflint --format json",
Dir: "without_module_init",
},
{
Name: "arguments",
Command: fmt.Sprintf("./tflint --format json %s", filepath.Join("dir", "template.tf")),
Env: map[string]string{
"TF_DATA_DIR": filepath.Join("dir", ".terraform"),
},
Dir: "arguments",
},
{
Name: "plugin",
Command: "./tflint --format json --module",
Dir: "plugin",
},
}
dir, _ := os.Getwd()
defer os.Chdir(dir)
for _, tc := range cases {
testDir := dir + "/integration/" + tc.Dir
os.Chdir(testDir)
if tc.Env != nil {
for k, v := range tc.Env {
os.Setenv(k, v)
}
}
outStream, errStream := new(bytes.Buffer), new(bytes.Buffer)
cli := cmd.NewCLI(outStream, errStream)
args := strings.Split(tc.Command, " ")
cli.Run(args)
var b []byte
var err error
if runtime.GOOS == "windows" && IsWindowsResultExist() {
b, err = ioutil.ReadFile("result_windows.json")
} else {
b, err = ioutil.ReadFile("result.json")
}
if err != nil {
t.Fatal(err)
}
var expected *formatter.JSONOutput
if err := json.Unmarshal(b, &expected); err != nil {
t.Fatal(err)
}
var got *formatter.JSONOutput
if err := json.Unmarshal(outStream.Bytes(), &got); err != nil {
t.Fatal(err)
}
if !cmp.Equal(got, expected) {
t.Fatalf("Failed `%s` test: diff=%s", tc.Name, cmp.Diff(expected, got))
}
if tc.Env != nil {
for k := range tc.Env {
os.Unsetenv(k)
}
}
}
}
func IsWindowsResultExist() bool {
_, err := os.Stat("result_windows.json")
return !os.IsNotExist(err)
}