-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Expand file tree
/
Copy pathstate_test.go
More file actions
285 lines (243 loc) · 10.2 KB
/
state_test.go
File metadata and controls
285 lines (243 loc) · 10.2 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package checks
import (
"context"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/configs/configload"
"github.com/hashicorp/terraform/internal/initwd"
)
func TestChecksHappyPath(t *testing.T) {
const fixtureDir = "testdata/happypath"
loader, close := configload.NewLoaderForTests(t)
defer close()
inst := initwd.NewModuleInstaller(loader.ModulesDir(), loader, nil)
_, instDiags := inst.InstallModules(context.Background(), fixtureDir, "tests", true, false, initwd.ModuleInstallHooksImpl{})
if instDiags.HasErrors() {
t.Fatal(instDiags.Err())
}
if err := loader.RefreshModules(); err != nil {
t.Fatalf("failed to refresh modules after installation: %s", err)
}
/////////////////////////////////////////////////////////////////////////
cfg, hclDiags := loader.LoadConfig(fixtureDir)
if hclDiags.HasErrors() {
t.Fatalf("invalid configuration: %s", hclDiags.Error())
}
resourceA := addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "null_resource",
Name: "a",
}.InModule(addrs.RootModule)
resourceNoChecks := addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "null_resource",
Name: "no_checks",
}.InModule(addrs.RootModule)
resourceNonExist := addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "null_resource",
Name: "nonexist",
}.InModule(addrs.RootModule)
rootOutput := addrs.OutputValue{
Name: "a",
}.InModule(addrs.RootModule)
moduleChild := addrs.RootModule.Child("child")
resourceB := addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "null_resource",
Name: "b",
}.InModule(moduleChild)
resourceC := addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "null_resource",
Name: "c",
}.InModule(moduleChild)
childOutput := addrs.OutputValue{
Name: "b",
}.InModule(moduleChild)
checkBlock := addrs.Check{
Name: "check",
}.InModule(addrs.RootModule)
// First some consistency checks to make sure our configuration is the
// shape we are relying on it to be.
if addr := resourceA; cfg.Module.ResourceByAddr(addr.Resource) == nil {
t.Fatalf("configuration does not include %s", addr)
}
if addr := resourceB; cfg.Children["child"].Module.ResourceByAddr(addr.Resource) == nil {
t.Fatalf("configuration does not include %s", addr)
}
if addr := resourceNoChecks; cfg.Module.ResourceByAddr(addr.Resource) == nil {
t.Fatalf("configuration does not include %s", addr)
}
if addr := resourceNonExist; cfg.Module.ResourceByAddr(addr.Resource) != nil {
t.Fatalf("configuration includes %s, which is not supposed to exist", addr)
}
if addr := checkBlock; cfg.Module.Checks[addr.Check.Name] == nil {
t.Fatalf("configuration does not include %s", addr)
}
/////////////////////////////////////////////////////////////////////////
checks := NewState(cfg)
missing := 0
if addr := resourceA; !checks.ConfigHasChecks(addr) {
t.Errorf("checks not detected for %s", addr)
missing++
}
if addr := resourceB; !checks.ConfigHasChecks(addr) {
t.Errorf("checks not detected for %s", addr)
missing++
}
if addr := resourceC; !checks.ConfigHasChecks(addr) {
t.Errorf("checks not detected for %s", addr)
missing++
}
if addr := rootOutput; !checks.ConfigHasChecks(addr) {
t.Errorf("checks not detected for %s", addr)
missing++
}
if addr := childOutput; !checks.ConfigHasChecks(addr) {
t.Errorf("checks not detected for %s", addr)
missing++
}
if addr := resourceNoChecks; checks.ConfigHasChecks(addr) {
t.Errorf("checks detected for %s, even though it has none", addr)
}
if addr := resourceNonExist; checks.ConfigHasChecks(addr) {
t.Errorf("checks detected for %s, even though it doesn't exist", addr)
}
if addr := checkBlock; !checks.ConfigHasChecks(addr) {
t.Errorf("checks not detected for %s", addr)
missing++
}
if missing > 0 {
t.Fatalf("missing some configuration objects we'd need for subsequent testing")
}
/////////////////////////////////////////////////////////////////////////
// Everything should start with status unknown.
{
wantConfigAddrs := addrs.MakeSet[addrs.ConfigCheckable](
resourceA,
resourceB,
resourceC,
rootOutput,
childOutput,
checkBlock,
)
gotConfigAddrs := checks.AllConfigAddrs()
if diff := cmp.Diff(wantConfigAddrs, gotConfigAddrs); diff != "" {
t.Errorf("wrong detected config addresses\n%s", diff)
}
for _, configAddr := range gotConfigAddrs {
if got, want := checks.AggregateCheckStatus(configAddr), StatusUnknown; got != want {
t.Errorf("incorrect initial aggregate check status for %s: %s, but want %s", configAddr, got, want)
}
}
}
/////////////////////////////////////////////////////////////////////////
// The following are steps that would normally be done by Terraform Core
// as part of visiting checkable objects during the graph walk. We're
// simulating a likely sequence of calls here for testing purposes, but
// Terraform Core won't necessarily visit all of these in exactly the
// same order every time and so this is just one possible valid ordering
// of calls.
resourceInstA := resourceA.Resource.Absolute(addrs.RootModuleInstance).Instance(addrs.NoKey)
rootOutputInst := rootOutput.OutputValue.Absolute(addrs.RootModuleInstance)
moduleChildInst := addrs.RootModuleInstance.Child("child", addrs.NoKey)
resourceInstB := resourceB.Resource.Absolute(moduleChildInst).Instance(addrs.NoKey)
resourceInstC0 := resourceC.Resource.Absolute(moduleChildInst).Instance(addrs.IntKey(0))
resourceInstC1 := resourceC.Resource.Absolute(moduleChildInst).Instance(addrs.IntKey(1))
childOutputInst := childOutput.OutputValue.Absolute(moduleChildInst)
checkBlockInst := checkBlock.Check.Absolute(addrs.RootModuleInstance)
checks.ReportCheckableObjects(resourceA, addrs.MakeSet[addrs.Checkable](resourceInstA))
checks.ReportCheckResult(resourceInstA, addrs.ResourcePrecondition, 0, StatusPass)
checks.ReportCheckResult(resourceInstA, addrs.ResourcePrecondition, 1, StatusPass)
checks.ReportCheckResult(resourceInstA, addrs.ResourcePostcondition, 0, StatusPass)
checks.ReportCheckableObjects(resourceB, addrs.MakeSet[addrs.Checkable](resourceInstB))
checks.ReportCheckResult(resourceInstB, addrs.ResourcePrecondition, 0, StatusPass)
checks.ReportCheckableObjects(resourceC, addrs.MakeSet[addrs.Checkable](resourceInstC0, resourceInstC1))
checks.ReportCheckResult(resourceInstC0, addrs.ResourcePostcondition, 0, StatusPass)
checks.ReportCheckResult(resourceInstC1, addrs.ResourcePostcondition, 0, StatusPass)
checks.ReportCheckableObjects(childOutput, addrs.MakeSet[addrs.Checkable](childOutputInst))
checks.ReportCheckResult(childOutputInst, addrs.OutputPrecondition, 0, StatusPass)
checks.ReportCheckableObjects(rootOutput, addrs.MakeSet[addrs.Checkable](rootOutputInst))
checks.ReportCheckResult(rootOutputInst, addrs.OutputPrecondition, 0, StatusPass)
checks.ReportCheckableObjects(checkBlock, addrs.MakeSet[addrs.Checkable](checkBlockInst))
checks.ReportCheckResult(checkBlockInst, addrs.CheckAssertion, 0, StatusPass)
/////////////////////////////////////////////////////////////////////////
// This "section" is simulating what we might do to report the results
// of the checks after a run completes.
{
configCount := 0
for _, configAddr := range checks.AllConfigAddrs() {
configCount++
if got, want := checks.AggregateCheckStatus(configAddr), StatusPass; got != want {
t.Errorf("incorrect final aggregate check status for %s: %s, but want %s", configAddr, got, want)
}
}
if got, want := configCount, 6; got != want {
t.Errorf("incorrect number of known config addresses %d; want %d", got, want)
}
}
{
objAddrs := addrs.MakeSet[addrs.Checkable](
resourceInstA,
rootOutputInst,
resourceInstB,
resourceInstC0,
resourceInstC1,
childOutputInst,
checkBlockInst,
)
for _, addr := range objAddrs {
if got, want := checks.ObjectCheckStatus(addr), StatusPass; got != want {
t.Errorf("incorrect final check status for object %s: %s, but want %s", addr, got, want)
}
}
}
}
func TestChecksDuplicateReport(t *testing.T) {
const fixtureDir = "testdata/happypath"
loader, close := configload.NewLoaderForTests(t)
defer close()
inst := initwd.NewModuleInstaller(loader.ModulesDir(), loader, nil)
_, instDiags := inst.InstallModules(context.Background(), fixtureDir, "tests", true, false, initwd.ModuleInstallHooksImpl{})
if instDiags.HasErrors() {
t.Fatal(instDiags.Err())
}
if err := loader.RefreshModules(); err != nil {
t.Fatalf("failed to refresh modules after installation: %s", err)
}
/////////////////////////////////////////////////////////////////////////
cfg, hclDiags := loader.LoadConfig(fixtureDir)
if hclDiags.HasErrors() {
t.Fatalf("invalid configuration: %s", hclDiags.Error())
}
resourceA := addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "null_resource",
Name: "a",
}.InModule(addrs.RootModule)
checks := NewState(cfg)
resourceInstA := resourceA.Resource.Absolute(addrs.RootModuleInstance).Instance(addrs.NoKey)
checks.ReportCheckableObjects(resourceA, addrs.MakeSet[addrs.Checkable](resourceInstA))
// Report initial check results for resource A (2 preconditions, 1 postcondition)
checks.ReportCheckResult(resourceInstA, addrs.ResourcePrecondition, 0, StatusPass)
checks.ReportCheckResult(resourceInstA, addrs.ResourcePrecondition, 1, StatusPass)
checks.ReportCheckResult(resourceInstA, addrs.ResourcePostcondition, 0, StatusPass)
if got, want := checks.ObjectCheckStatus(resourceInstA), StatusPass; got != want {
t.Fatalf("incorrect check status after first report: got %s, want %s", got, want)
}
// Reporting the same status again should not panic or change the result
checks.ReportCheckResult(resourceInstA, addrs.ResourcePrecondition, 0, StatusPass)
if got, want := checks.ObjectCheckStatus(resourceInstA), StatusPass; got != want {
t.Errorf("incorrect check status after duplicate report: got %s, want %s", got, want)
}
// Reporting a conflicting status should not panic, and should preserve
// the original status (first write wins)
checks.ReportCheckResult(resourceInstA, addrs.ResourcePrecondition, 0, StatusFail)
if got, want := checks.ObjectCheckStatus(resourceInstA), StatusPass; got != want {
t.Errorf("check status should be preserved after conflicting report: got %s, want %s", got, want)
}
}