-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathplan_test.go
178 lines (144 loc) · 3.59 KB
/
plan_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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfjson
import (
"encoding/json"
"os"
"reflect"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestPlanValidate(t *testing.T) {
f, err := os.Open("testdata/basic/plan.json")
if err != nil {
t.Fatal(err)
}
defer f.Close()
var plan *Plan
if err := json.NewDecoder(f).Decode(&plan); err != nil {
t.Fatal(err)
}
if err := plan.Validate(); err != nil {
t.Fatal(err)
}
}
func TestPlan_015(t *testing.T) {
f, err := os.Open("testdata/basic/plan-0.15.json")
if err != nil {
t.Fatal(err)
}
defer f.Close()
var plan *Plan
if err := json.NewDecoder(f).Decode(&plan); err != nil {
t.Fatal(err)
}
if err := plan.Validate(); err != nil {
t.Fatal(err)
}
expectedChange := &Change{
Actions: Actions{"create"},
After: map[string]interface{}{"ami": "boop"},
AfterUnknown: map[string]interface{}{"id": true},
BeforeSensitive: false,
AfterSensitive: map[string]interface{}{"ami": true},
}
if diff := cmp.Diff(expectedChange, plan.ResourceChanges[0].Change); diff != "" {
t.Fatalf("unexpected change: %s", diff)
}
expectedVariable := map[string]*ConfigVariable{
"test_var": {
Default: "boop",
Sensitive: true,
},
}
if diff := cmp.Diff(expectedVariable, plan.Config.RootModule.Variables); diff != "" {
t.Fatalf("unexpected variables: %s", diff)
}
}
func TestPlan_withChecks(t *testing.T) {
f, err := os.Open("testdata/has_checks/plan.json")
if err != nil {
t.Fatal(err)
}
defer f.Close()
var plan *Plan
if err := json.NewDecoder(f).Decode(&plan); err != nil {
t.Fatal(err)
}
if err := plan.Validate(); err != nil {
t.Fatal(err)
}
if len(plan.Checks) == 0 {
t.Fatal("expected checks to not be empty")
}
for _, c := range plan.Checks {
for _, instance := range c.Instances {
k := reflect.TypeOf(instance.Address.InstanceKey).Kind()
switch k {
case reflect.Int, reflect.Float32, reflect.Float64, reflect.String:
t.Log("instance key is a valid type")
default:
t.Fatalf("unexpected type %s, expected string or int", k.String())
}
}
}
}
func TestPlan_movedBlock(t *testing.T) {
f, err := os.Open("testdata/moved_block/plan.json")
if err != nil {
t.Fatal(err)
}
defer f.Close()
var plan *Plan
if err := json.NewDecoder(f).Decode(&plan); err != nil {
t.Fatal(err)
}
if err := plan.Validate(); err != nil {
t.Fatal(err)
}
if plan.ResourceChanges[0].PreviousAddress != "random_id.test" {
t.Fatalf("unexpected previous address %s, expected is random_id.test", plan.ResourceChanges[0].PreviousAddress)
}
}
func TestPlan_UnmarshalJSON(t *testing.T) {
t.Parallel()
b, err := os.ReadFile("testdata/numerics/plan.json")
if err != nil {
t.Fatal(err)
}
testCases := map[string]struct {
useJSONNumber bool
expected any
}{
"float64": {
expected: 1.23,
},
"json-number": {
useJSONNumber: true,
expected: json.Number("1.23"),
},
}
for name, testCase := range testCases {
name, testCase := name, testCase
t.Run(name, func(t *testing.T) {
t.Parallel()
plan := &Plan{}
plan.UseJSONNumber(testCase.useJSONNumber)
err = plan.UnmarshalJSON(b)
if err != nil {
t.Fatal(err)
}
after, ok := plan.ResourceChanges[0].Change.After.(map[string]any)
if !ok {
t.Fatal("plan.ResourceChanges[0].Change.After cannot be asserted as map[string]any")
}
attr, ok := after["configurable_attribute"]
if !ok {
t.Fatal("configurable attribute not found")
}
if diff := cmp.Diff(attr, testCase.expected); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
})
}
}