@@ -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.
@@ -56,6 +62,11 @@ func TestMissingVar(t *testing.T) {
56
62
t .Fatalf ("expected missing no_default, got %q" , e .VariableName )
57
63
}
58
64
65
+ var ee * exec.ExitError
66
+ if ! errors .As (err , & ee ) {
67
+ t .Fatalf ("expected exec.ExitError, got %T, %s" , err , err )
68
+ }
69
+
59
70
_ , err = tf .Plan (context .Background (), tfexec .Var ("no_default=foo" ))
60
71
if err != nil {
61
72
t .Fatalf ("expected no error, got %s" , err )
@@ -89,5 +100,95 @@ func TestTFVersionMismatch(t *testing.T) {
89
100
if e .TFVersion != tfv .String () {
90
101
t .Fatalf ("expected %q, got %q" , tfv .String (), e .TFVersion )
91
102
}
103
+
104
+ var ee * exec.ExitError
105
+ if ! errors .As (err , & ee ) {
106
+ t .Fatalf ("expected exec.ExitError, got %T, %s" , err , err )
107
+ }
108
+ })
109
+ }
110
+
111
+ func TestContext_alreadyPastDeadline (t * testing.T ) {
112
+ runTest (t , "" , func (t * testing.T , tfv * version.Version , tf * tfexec.Terraform ) {
113
+ ctx , cancel := context .WithDeadline (context .Background (), time .Now ().Add (- 1 * time .Second ))
114
+ defer cancel ()
115
+
116
+ _ , _ , err := tf .Version (ctx , true )
117
+ if err == nil {
118
+ t .Fatal ("expected error from version command" )
119
+ }
120
+
121
+ if ! errors .Is (err , context .DeadlineExceeded ) {
122
+ t .Fatalf ("expected context.DeadlineExceeded, got %T %s" , err , err )
123
+ }
124
+ })
125
+ }
126
+
127
+ func TestContext_sleepNoCancellation (t * testing.T ) {
128
+ // this test is just to ensure that time_sleep works properly without cancellation
129
+ runTest (t , "sleep" , func (t * testing.T , tfv * version.Version , tf * tfexec.Terraform ) {
130
+ // only testing versions that can cancel mid apply
131
+ if ! tfv .GreaterThanOrEqual (protocol5MinVersion ) {
132
+ t .Skip ("the ability to interrupt an apply was added in protocol 5.0 in Terraform 0.12, so test is not valid" )
133
+ }
134
+
135
+ err := tf .Init (context .Background ())
136
+ if err != nil {
137
+ t .Fatalf ("err during init: %s" , err )
138
+ }
139
+
140
+ ctx := context .Background ()
141
+ start := time .Now ()
142
+ err = tf .Apply (ctx , tfexec .Var (`create_duration=5s` ))
143
+ if err != nil {
144
+ t .Fatalf ("error during apply: %s" , err )
145
+ }
146
+ elapsed := time .Now ().Sub (start )
147
+ if elapsed < 5 * time .Second {
148
+ t .Fatalf ("expected runtime of at least 5s, got %s" , elapsed )
149
+ }
150
+ })
151
+ }
152
+
153
+ func TestContext_sleepTimeoutExpired (t * testing.T ) {
154
+ runTest (t , "sleep" , func (t * testing.T , tfv * version.Version , tf * tfexec.Terraform ) {
155
+ // only testing versions that can cancel mid apply
156
+ if ! tfv .GreaterThanOrEqual (protocol5MinVersion ) {
157
+ t .Skip ("the ability to interrupt an apply was added in protocol 5.0 in Terraform 0.12, so test is not valid" )
158
+ }
159
+
160
+ err := tf .Init (context .Background ())
161
+ if err != nil {
162
+ t .Fatalf ("err during init: %s" , err )
163
+ }
164
+
165
+ ctx := context .Background ()
166
+ ctx , cancel := context .WithTimeout (ctx , 5 * time .Second )
167
+ defer cancel ()
168
+
169
+ err = tf .Apply (ctx )
170
+ if err == nil {
171
+ t .Fatal ("expected error, but didn't find one" )
172
+ }
173
+
174
+ if ! errors .Is (err , context .DeadlineExceeded ) {
175
+ t .Fatalf ("expected context.DeadlineExceeded, got %T %s" , err , err )
176
+ }
177
+ })
178
+ }
179
+
180
+ func TestContext_alreadyCancelled (t * testing.T ) {
181
+ runTest (t , "" , func (t * testing.T , tfv * version.Version , tf * tfexec.Terraform ) {
182
+ ctx , cancel := context .WithCancel (context .Background ())
183
+ cancel ()
184
+
185
+ _ , _ , err := tf .Version (ctx , true )
186
+ if err == nil {
187
+ t .Fatal ("expected error from version command" )
188
+ }
189
+
190
+ if ! errors .Is (err , context .Canceled ) {
191
+ t .Fatalf ("expected context.Canceled, got %T %s" , err , err )
192
+ }
92
193
})
93
194
}
0 commit comments