forked from hashicorp/terraform-exec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_test.go
241 lines (202 loc) · 6.76 KB
/
errors_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
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
// This file contains tests that only compile/work in Go 1.13 and forward
//go:build go1.13
// +build go1.13
package e2etest
import (
"context"
"errors"
"os"
"os/exec"
"testing"
"time"
"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-exec/tfexec"
)
var (
protocol5MinVersion = version.Must(version.NewVersion("0.12.0"))
)
func TestUnparsedError(t *testing.T) {
// This simulates an unparsed error from the Cmd.Run method (in this case file not found). This
// is to ensure we don't miss raising unexpected errors in addition to parsed / well known ones.
runTest(t, "", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) {
// force delete the working dir to cause an os.PathError
err := os.RemoveAll(tf.WorkingDir())
if err != nil {
t.Fatal(err)
}
err = tf.Init(context.Background())
if err == nil {
t.Fatalf("expected error running Init, none returned")
}
var e *os.PathError
if !errors.As(err, &e) {
t.Fatalf("expected os.PathError, got %T, %s", err, err)
}
})
}
func TestMissingVar(t *testing.T) {
runTest(t, "var", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) {
err := tf.Init(context.Background())
if err != nil {
t.Fatalf("err during init: %s", err)
}
// Variable names from testdata/var/main.tf
shortVarName := "no_default"
longVarName := "no_default_really_long_variable_name_that_will_line_wrap_tf_output"
// Test for ErrMissingVar and properly formatted error message on shorter variable names
_, err = tf.Plan(context.Background(), tfexec.Var(longVarName+"=foo"))
if err == nil {
t.Fatalf("expected error running Plan, none returned")
}
var e *tfexec.ErrMissingVar
if !errors.As(err, &e) {
t.Fatalf("expected ErrMissingVar, got %T, %s", err, err)
}
if e.VariableName != shortVarName {
t.Fatalf("expected missing %s, got %q", shortVarName, e.VariableName)
}
// Test for ErrMissingVar and properly formatted error message on long variable names
_, err = tf.Plan(context.Background(), tfexec.Var(shortVarName+"=foo"))
if err == nil {
t.Fatalf("expected error running Plan, none returned")
}
if !errors.As(err, &e) {
t.Fatalf("expected ErrMissingVar, got %T, %s", err, err)
}
if e.VariableName != longVarName {
t.Fatalf("expected missing %s, got %q", longVarName, e.VariableName)
}
var ee *exec.ExitError
if !errors.As(err, &ee) {
t.Fatalf("expected exec.ExitError, got %T, %s", err, err)
}
// Test for no error when all variables have a value
_, err = tf.Plan(context.Background(), tfexec.Var(shortVarName+"=foo"), tfexec.Var(longVarName+"=foo"))
if err != nil {
t.Fatalf("expected no error, got %s", err)
}
})
}
func TestTFVersionMismatch(t *testing.T) {
runTest(t, "tf99", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) {
// force cache version for error messaging
_, _, err := tf.Version(context.Background(), true)
if err != nil {
t.Fatal(err)
}
err = tf.Init(context.Background())
if err == nil {
t.Fatal("expected error, but didn't find one")
}
var e *tfexec.ErrTFVersionMismatch
if !errors.As(err, &e) {
t.Fatalf("expected ErrTFVersionMismatch, got %T, %s", err, err)
}
// in 0.12, we just return "unknown" as the specifics are not included in the error messaging
if e.Constraint != "unknown" && e.Constraint != ">99.0.0" {
t.Fatalf("unexpected constraint %q", e.Constraint)
}
if e.TFVersion != tfv.String() {
t.Fatalf("expected %q, got %q", tfv.String(), e.TFVersion)
}
var ee *exec.ExitError
if !errors.As(err, &ee) {
t.Fatalf("expected exec.ExitError, got %T, %s", err, err)
}
})
}
func TestLockedState(t *testing.T) {
runTest(t, "inmem_backend_locked", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) {
err := tf.Init(context.Background())
if err != nil {
t.Fatalf("err during init: %s", err)
}
err = tf.Apply(context.Background())
if err == nil {
t.Fatal("expected error, but didn't find one")
}
var stateLockedErr *tfexec.ErrStateLocked
if !errors.As(err, &stateLockedErr) {
t.Fatalf("expected ErrTFVersionMismatch, got %T, %s", err, err)
}
})
}
func TestContext_alreadyPastDeadline(t *testing.T) {
runTest(t, "", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) {
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(-1*time.Second))
defer cancel()
_, _, err := tf.Version(ctx, true)
if err == nil {
t.Fatal("expected error from version command")
}
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("expected context.DeadlineExceeded, got %T %s", err, err)
}
})
}
func TestContext_sleepNoCancellation(t *testing.T) {
// this test is just to ensure that time_sleep works properly without cancellation
runTest(t, "sleep", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) {
// only testing versions that can cancel mid apply
if !tfv.GreaterThanOrEqual(protocol5MinVersion) {
t.Skip("the ability to interrupt an apply was added in protocol 5.0 in Terraform 0.12, so test is not valid")
}
err := tf.Init(context.Background())
if err != nil {
t.Fatalf("err during init: %s", err)
}
ctx := context.Background()
start := time.Now()
err = tf.Apply(ctx, tfexec.Var(`create_duration=5s`))
if err != nil {
t.Fatalf("error during apply: %s", err)
}
elapsed := time.Now().Sub(start)
if elapsed < 5*time.Second {
t.Fatalf("expected runtime of at least 5s, got %s", elapsed)
}
})
}
func TestContext_sleepTimeoutExpired(t *testing.T) {
runTest(t, "sleep", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) {
// only testing versions that can cancel mid apply
if !tfv.GreaterThanOrEqual(protocol5MinVersion) {
t.Skip("the ability to interrupt an apply was added in protocol 5.0 in Terraform 0.12, so test is not valid")
}
err := tf.Init(context.Background())
if err != nil {
t.Fatalf("err during init: %s", err)
}
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
errCh := make(chan error)
go func() {
err = tf.Apply(ctx)
if err != nil {
errCh <- err
}
}()
select {
case err := <-errCh:
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("expected context.DeadlineExceeded, got %T %s", err, err)
}
case <-time.After(time.Second * 10):
t.Fatal("terraform apply should have canceled and returned in ~5s")
}
})
}
func TestContext_alreadyCancelled(t *testing.T) {
runTest(t, "", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, _, err := tf.Version(ctx, true)
if err == nil {
t.Fatal("expected error from version command")
}
if !errors.Is(err, context.Canceled) {
t.Fatalf("expected context.Canceled, got %T %s", err, err)
}
})
}