-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
160 lines (143 loc) · 4.55 KB
/
parser_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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package tfparser
import (
"os"
"testing"
)
var testTFCode = `
# use1.genhtcc.com <-> aps2.genhtcc.com
module "legacy_use1_legacy_aps2_routing" {
source = "../../modules/vpc-routing"
alice_vpc_name = "Development VPC"
//commented_param = value
bob_vpc_name = "aps2.g1.genhtcc.com"
providers = {
aws.alice = aws.us-east-1
aws.bob = aws.ap-southeast-2
}
}
/*
multiline comment
*/
# use1.genhtcc.com <-> aps2.g1.genhtcc.com
module "legacy_use1_g1_aps2_routing" {
source = "../../modules/vpc-routing"
alice_region = "us-east-1"
alice_vpc_name = "Development VPC"
bob_vpc_name = "aps2.g1.genhtcc.com"
providers = {
aws.alice = aws.us-east-1
aws.bob = aws.ap-southeast-2
}
}
`
var config, parseErr = ParseString(testTFCode)
func TestParser1(t *testing.T) {
if parseErr != nil {
t.Fatalf("parser returned error %v", parseErr)
}
}
func TestModulesLength(t *testing.T) {
if len(config.Modules) != 2 {
t.Fatalf("Unexpceted number of modules parsed: %v", len(config.Modules))
}
}
func TestModule1ParamsNo(t *testing.T) {
m, exists := config.Modules["legacy_use1_legacy_aps2_routing"]
// test case when we check that it must exist is next tst
if exists {
if l := len(m.Parameters); l != 2 {
t.Fatalf("Unexpected number of parameters found for Module 'legacy_use1_legacy_aps2_routing', %v instead of 2", l)
}
}
}
func TestModule1Params(t *testing.T) {
m, exists := config.Modules["legacy_use1_legacy_aps2_routing"]
if !exists {
t.Fatal("Module 'legacy_use1_legacy_aps2_routing' was not found")
}
al, exists := m.Providers["aws.alice"]
if !exists {
t.Fatal("Module 'legacy_use1_legacy_aps2_routing' does not have provider alias 'aws.alice'")
}
if al != "aws.us-east-1" {
t.Fatalf("Module 'legacy_use1_legacy_aps2_routing' provider alias 'aws.alice' unexpected value %#q, expected 'aws.us-east-1'", al)
}
}
func TestModule2Params(t *testing.T) {
m, exists := config.Modules["legacy_use1_g1_aps2_routing"]
if !exists {
t.Fatal("Module 'legacy_use1_g1_aps2_routing' was not found")
}
al, exists := m.Providers["aws.bob"]
if !exists {
t.Fatal("Module 'legacy_use1_g1_aps2_routing' does not have provider alias 'aws.bob'")
}
if al != "aws.ap-southeast-2" {
t.Fatalf("Module 'legacy_use1_g1_aps2_routing' provider alias 'aws.alice' unexpected value %#q, expected 'aws.ap-southeast-2'", al)
}
}
func TestModule2ParamsNo(t *testing.T) {
m, exists := config.Modules["legacy_use1_g1_aps2_routing"]
// test case when we check that it must exist is next tst
if exists {
if l := len(m.Parameters); l != 3 {
t.Fatalf("Unexpected number of parameters found for Module 'legacy_use1_g1_aps2_routing', %v instead of 3", l)
}
}
}
func TestModule1Source(t *testing.T) {
m, exists := config.Modules["legacy_use1_legacy_aps2_routing"]
if exists {
expSourcePath := "../../modules/vpc-routing"
if m.SourcePath != expSourcePath {
t.Fatalf("Unexpected source path for module 'legacy_use1_legacy_aps2_routing', found %#q, expected %#q", m.SourcePath, expSourcePath)
}
}
}
func TestParseFile(t *testing.T) {
testFileName := "testdata/tf/main.tf"
_, err := os.Stat(testFileName)
if os.IsNotExist(err) {
t.Skipf("testing terraform file is not found. Not testing ParseFile")
}
config, err := ParseFile(testFileName)
if err != nil {
t.Fatalf("ParseFile returned an error, %v", err)
}
assertTestingConfig1(config, t)
}
func TestParseDir(t *testing.T) {
testDirName := "testdata/tf/"
_, err := os.Stat(testDirName)
if os.IsNotExist(err) {
t.Skipf("testing terraform dir %#q is not found. Not testing ParseDir", testDirName)
}
config, err := ParseDir(testDirName)
if err != nil {
t.Fatalf("ParseDir returned an error, %v", err)
}
assertTestingConfig1(config, t)
}
func assertTestingConfig1(config *TFconfig, t *testing.T) {
if len(config.Modules) != 2 {
t.Fatalf("Unexpected number of moduels fetched. Expected 2 got %v", len(config.Modules))
}
m1, exists := config.Modules["module1"]
if !exists {
t.Fatalf("Module 'module1' is not found, but expected")
}
m1sp := "../../modules/module_name"
if m1.SourcePath != m1sp {
t.Fatalf("Unexpceted 'module1' source: %#q, expected %#q", m1.SourcePath, m1sp)
}
if len(m1.Providers) != 2 {
t.Fatalf("Unexpected number of providers for 'module1' %v, expected 2", len(m1.Providers))
}
p1, exists := m1.Providers["aws.alice"]
if !exists {
t.Fatal("provider 'aws.alice' was not found in 'module1")
}
if p1 != "aws.us-east-1" {
t.Fatalf("provider 'aws.alice' alias is %#q, expected 'aws.us-east-1'", p1)
}
}