forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport_tree_test.go
81 lines (70 loc) · 2.3 KB
/
import_tree_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
package config
import (
"testing"
)
func TestImportTreeHCL2Experiment(t *testing.T) {
// Can only run this test if we're built with the experiment enabled.
// Enable this test by passing the following option to "go test":
// -ldflags="-X github.com/hashicorp/terraform/config.enableHCL2Experiment=true"
// See the comment associated with this flag variable for more information.
if enableHCL2Experiment == "" {
t.Skip("HCL2 experiment is not enabled")
}
t.Run("HCL not opted in", func(t *testing.T) {
// .tf file without opt-in should use the old HCL parser
imp, err := loadTree("test-fixtures/hcl2-experiment-switch/not-opted-in.tf")
if err != nil {
t.Fatal(err)
}
tree, err := imp.ConfigTree()
if err != nil {
t.Fatalf("unexpected error loading not-opted-in.tf: %s", err)
}
cfg := tree.Config
if got, want := len(cfg.Locals), 1; got != want {
t.Fatalf("wrong number of locals %#v; want %#v", got, want)
}
if cfg.Locals[0].RawConfig.Raw == nil {
// Having RawConfig.Raw indicates the old loader
t.Fatal("local has no RawConfig.Raw")
}
})
t.Run("HCL opted in", func(t *testing.T) {
// .tf file with opt-in should use the new HCL2 parser
imp, err := loadTree("test-fixtures/hcl2-experiment-switch/opted-in.tf")
if err != nil {
t.Fatal(err)
}
tree, err := imp.ConfigTree()
if err != nil {
t.Fatalf("unexpected error loading opted-in.tf: %s", err)
}
cfg := tree.Config
if got, want := len(cfg.Locals), 1; got != want {
t.Fatalf("wrong number of locals %#v; want %#v", got, want)
}
if cfg.Locals[0].RawConfig.Body == nil {
// Having RawConfig.Body indicates the new loader
t.Fatal("local has no RawConfig.Body")
}
})
t.Run("JSON ineligible", func(t *testing.T) {
// .tf.json file should always use the old HCL parser
imp, err := loadTree("test-fixtures/hcl2-experiment-switch/not-eligible.tf.json")
if err != nil {
t.Fatal(err)
}
tree, err := imp.ConfigTree()
if err != nil {
t.Fatalf("unexpected error loading not-eligible.tf.json: %s", err)
}
cfg := tree.Config
if got, want := len(cfg.Locals), 1; got != want {
t.Fatalf("wrong number of locals %#v; want %#v", got, want)
}
if cfg.Locals[0].RawConfig.Raw == nil {
// Having RawConfig.Raw indicates the old loader
t.Fatal("local has no RawConfig.Raw")
}
})
}