@@ -7,13 +7,19 @@ import (
7
7
"context"
8
8
"errors"
9
9
"os"
10
+ "os/exec"
10
11
"testing"
12
+ "time"
11
13
12
14
"github.com/hashicorp/go-version"
13
15
14
16
"github.com/hashicorp/terraform-exec/tfexec"
15
17
)
16
18
19
+ var (
20
+ protocol5MinVersion = version .Must (version .NewVersion ("0.12.0" ))
21
+ )
22
+
17
23
func TestUnparsedError (t * testing.T ) {
18
24
// This simulates an unparsed error from the Cmd.Run method (in this case file not found). This
19
25
// is to ensure we don't miss raising unexpected errors in addition to parsed / well known ones.
@@ -74,6 +80,11 @@ func TestMissingVar(t *testing.T) {
74
80
t .Fatalf ("expected missing %s, got %q" , longVarName , e .VariableName )
75
81
}
76
82
83
+ var ee * exec.ExitError
84
+ if ! errors .As (err , & ee ) {
85
+ t .Fatalf ("expected exec.ExitError, got %T, %s" , err , err )
86
+ }
87
+
77
88
// Test for no error when all variables have a value
78
89
_ , err = tf .Plan (context .Background (), tfexec .Var (shortVarName + "=foo" ), tfexec .Var (longVarName + "=foo" ))
79
90
if err != nil {
@@ -108,5 +119,95 @@ func TestTFVersionMismatch(t *testing.T) {
108
119
if e .TFVersion != tfv .String () {
109
120
t .Fatalf ("expected %q, got %q" , tfv .String (), e .TFVersion )
110
121
}
122
+
123
+ var ee * exec.ExitError
124
+ if ! errors .As (err , & ee ) {
125
+ t .Fatalf ("expected exec.ExitError, got %T, %s" , err , err )
126
+ }
127
+ })
128
+ }
129
+
130
+ func TestContext_alreadyPastDeadline (t * testing.T ) {
131
+ runTest (t , "" , func (t * testing.T , tfv * version.Version , tf * tfexec.Terraform ) {
132
+ ctx , cancel := context .WithDeadline (context .Background (), time .Now ().Add (- 1 * time .Second ))
133
+ defer cancel ()
134
+
135
+ _ , _ , err := tf .Version (ctx , true )
136
+ if err == nil {
137
+ t .Fatal ("expected error from version command" )
138
+ }
139
+
140
+ if ! errors .Is (err , context .DeadlineExceeded ) {
141
+ t .Fatalf ("expected context.DeadlineExceeded, got %T %s" , err , err )
142
+ }
143
+ })
144
+ }
145
+
146
+ func TestContext_sleepNoCancellation (t * testing.T ) {
147
+ // this test is just to ensure that time_sleep works properly without cancellation
148
+ runTest (t , "sleep" , func (t * testing.T , tfv * version.Version , tf * tfexec.Terraform ) {
149
+ // only testing versions that can cancel mid apply
150
+ if ! tfv .GreaterThanOrEqual (protocol5MinVersion ) {
151
+ t .Skip ("the ability to interrupt an apply was added in protocol 5.0 in Terraform 0.12, so test is not valid" )
152
+ }
153
+
154
+ err := tf .Init (context .Background ())
155
+ if err != nil {
156
+ t .Fatalf ("err during init: %s" , err )
157
+ }
158
+
159
+ ctx := context .Background ()
160
+ start := time .Now ()
161
+ err = tf .Apply (ctx , tfexec .Var (`create_duration=5s` ))
162
+ if err != nil {
163
+ t .Fatalf ("error during apply: %s" , err )
164
+ }
165
+ elapsed := time .Now ().Sub (start )
166
+ if elapsed < 5 * time .Second {
167
+ t .Fatalf ("expected runtime of at least 5s, got %s" , elapsed )
168
+ }
169
+ })
170
+ }
171
+
172
+ func TestContext_sleepTimeoutExpired (t * testing.T ) {
173
+ runTest (t , "sleep" , func (t * testing.T , tfv * version.Version , tf * tfexec.Terraform ) {
174
+ // only testing versions that can cancel mid apply
175
+ if ! tfv .GreaterThanOrEqual (protocol5MinVersion ) {
176
+ t .Skip ("the ability to interrupt an apply was added in protocol 5.0 in Terraform 0.12, so test is not valid" )
177
+ }
178
+
179
+ err := tf .Init (context .Background ())
180
+ if err != nil {
181
+ t .Fatalf ("err during init: %s" , err )
182
+ }
183
+
184
+ ctx := context .Background ()
185
+ ctx , cancel := context .WithTimeout (ctx , 5 * time .Second )
186
+ defer cancel ()
187
+
188
+ err = tf .Apply (ctx )
189
+ if err == nil {
190
+ t .Fatal ("expected error, but didn't find one" )
191
+ }
192
+
193
+ if ! errors .Is (err , context .DeadlineExceeded ) {
194
+ t .Fatalf ("expected context.DeadlineExceeded, got %T %s" , err , err )
195
+ }
196
+ })
197
+ }
198
+
199
+ func TestContext_alreadyCancelled (t * testing.T ) {
200
+ runTest (t , "" , func (t * testing.T , tfv * version.Version , tf * tfexec.Terraform ) {
201
+ ctx , cancel := context .WithCancel (context .Background ())
202
+ cancel ()
203
+
204
+ _ , _ , err := tf .Version (ctx , true )
205
+ if err == nil {
206
+ t .Fatal ("expected error from version command" )
207
+ }
208
+
209
+ if ! errors .Is (err , context .Canceled ) {
210
+ t .Fatalf ("expected context.Canceled, got %T %s" , err , err )
211
+ }
111
212
})
112
213
}
0 commit comments