forked from hashicorp/go-tfe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_test.go
279 lines (223 loc) · 7.88 KB
/
run_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
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
package tfe
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRunsList(t *testing.T) {
client := testClient(t)
ctx := context.Background()
wTest, wTestCleanup := createWorkspace(t, client, nil)
defer wTestCleanup()
rTest1, _ := createRun(t, client, wTest)
rTest2, _ := createRun(t, client, wTest)
t.Run("without list options", func(t *testing.T) {
rl, err := client.Runs.List(ctx, wTest.ID, RunListOptions{})
require.NoError(t, err)
found := []string{}
for _, r := range rl.Items {
found = append(found, r.ID)
}
assert.Contains(t, found, rTest1.ID)
assert.Contains(t, found, rTest2.ID)
assert.Equal(t, 1, rl.CurrentPage)
assert.Equal(t, 2, rl.TotalCount)
})
t.Run("with list options", func(t *testing.T) {
t.Skip("paging not supported yet in API")
// Request a page number which is out of range. The result should
// be successful, but return no results if the paging options are
// properly passed along.
rl, err := client.Runs.List(ctx, wTest.ID, RunListOptions{
ListOptions: ListOptions{
PageNumber: 999,
PageSize: 100,
},
})
require.NoError(t, err)
assert.Empty(t, rl.Items)
assert.Equal(t, 999, rl.CurrentPage)
assert.Equal(t, 2, rl.TotalCount)
})
t.Run("without a valid workspace ID", func(t *testing.T) {
rl, err := client.Runs.List(ctx, badIdentifier, RunListOptions{})
assert.Nil(t, rl)
assert.EqualError(t, err, "invalid value for workspace ID")
})
}
func TestRunsCreate(t *testing.T) {
client := testClient(t)
ctx := context.Background()
wTest, wTestCleanup := createWorkspace(t, client, nil)
defer wTestCleanup()
cvTest, _ := createUploadedConfigurationVersion(t, client, wTest)
t.Run("without a configuration version", func(t *testing.T) {
options := RunCreateOptions{
Workspace: wTest,
}
_, err := client.Runs.Create(ctx, options)
assert.NoError(t, err)
})
t.Run("with a configuration version", func(t *testing.T) {
options := RunCreateOptions{
ConfigurationVersion: cvTest,
Workspace: wTest,
}
r, err := client.Runs.Create(ctx, options)
require.NoError(t, err)
assert.Equal(t, cvTest.ID, r.ConfigurationVersion.ID)
})
t.Run("without a workspace", func(t *testing.T) {
r, err := client.Runs.Create(ctx, RunCreateOptions{})
assert.Nil(t, r)
assert.EqualError(t, err, "workspace is required")
})
t.Run("with additional attributes", func(t *testing.T) {
options := RunCreateOptions{
Message: String("yo"),
Workspace: wTest,
TargetAddrs: []string{"null_resource.example"},
}
r, err := client.Runs.Create(ctx, options)
require.NoError(t, err)
assert.Equal(t, *options.Message, r.Message)
assert.Equal(t, options.TargetAddrs, r.TargetAddrs)
})
}
func TestRunsRead(t *testing.T) {
client := testClient(t)
ctx := context.Background()
rTest, rTestCleanup := createPlannedRun(t, client, nil)
defer rTestCleanup()
t.Run("when the run exists", func(t *testing.T) {
r, err := client.Runs.Read(ctx, rTest.ID)
assert.NoError(t, err)
assert.Equal(t, rTest, r)
})
t.Run("when the run does not exist", func(t *testing.T) {
r, err := client.Runs.Read(ctx, "nonexisting")
assert.Nil(t, r)
assert.Equal(t, err, ErrResourceNotFound)
})
t.Run("with invalid run ID", func(t *testing.T) {
r, err := client.Runs.Read(ctx, badIdentifier)
assert.Nil(t, r)
assert.EqualError(t, err, "invalid value for run ID")
})
}
func TestRunsApply(t *testing.T) {
client := testClient(t)
ctx := context.Background()
rTest, rTestCleanup := createPlannedRun(t, client, nil)
defer rTestCleanup()
t.Run("when the run exists", func(t *testing.T) {
err := client.Runs.Apply(ctx, rTest.ID, RunApplyOptions{})
assert.NoError(t, err)
})
t.Run("when the run does not exist", func(t *testing.T) {
err := client.Runs.Apply(ctx, "nonexisting", RunApplyOptions{})
assert.Equal(t, err, ErrResourceNotFound)
})
t.Run("with invalid run ID", func(t *testing.T) {
err := client.Runs.Apply(ctx, badIdentifier, RunApplyOptions{})
assert.EqualError(t, err, "invalid value for run ID")
})
}
func TestRunsCancel(t *testing.T) {
client := testClient(t)
ctx := context.Background()
wTest, wTestCleanup := createWorkspace(t, client, nil)
defer wTestCleanup()
// We need to create 2 runs here. The first run will automatically
// be planned so that one cannot be cancelled. The second one will
// be pending until the first one is confirmed or discarded, so we
// can cancel that one.
_, _ = createRun(t, client, wTest)
rTest2, _ := createRun(t, client, wTest)
t.Run("when the run exists", func(t *testing.T) {
err := client.Runs.Cancel(ctx, rTest2.ID, RunCancelOptions{})
assert.NoError(t, err)
})
t.Run("when the run does not exist", func(t *testing.T) {
err := client.Runs.Cancel(ctx, "nonexisting", RunCancelOptions{})
assert.Equal(t, err, ErrResourceNotFound)
})
t.Run("with invalid run ID", func(t *testing.T) {
err := client.Runs.Cancel(ctx, badIdentifier, RunCancelOptions{})
assert.EqualError(t, err, "invalid value for run ID")
})
}
func TestRunsForceCancel(t *testing.T) {
client := testClient(t)
ctx := context.Background()
wTest, wTestCleanup := createWorkspace(t, client, nil)
defer wTestCleanup()
// We need to create 2 runs here. The first run will automatically
// be planned so that one cannot be cancelled. The second one will
// be pending until the first one is confirmed or discarded, so we
// can cancel that one.
_, _ = createRun(t, client, wTest)
rTest, _ := createRun(t, client, wTest)
t.Run("run is not force-cancelable", func(t *testing.T) {
assert.False(t, rTest.Actions.IsForceCancelable)
})
t.Run("user is allowed to force-cancel", func(t *testing.T) {
assert.True(t, rTest.Permissions.CanForceCancel)
})
t.Run("after a normal cancel", func(t *testing.T) {
// Request the normal cancel
err := client.Runs.Cancel(ctx, rTest.ID, RunCancelOptions{})
require.NoError(t, err)
for i := 1; ; i++ {
// Refresh the view of the run
rTest, err = client.Runs.Read(ctx, rTest.ID)
require.NoError(t, err)
// Check if the timestamp is present.
if !rTest.ForceCancelAvailableAt.IsZero() {
break
}
if i > 30 {
t.Fatal("Timeout waiting for run to be canceled")
}
time.Sleep(time.Second)
}
t.Run("force-cancel-available-at timestamp is present", func(t *testing.T) {
assert.True(t, rTest.ForceCancelAvailableAt.After(time.Now()))
})
// This test case is minimal because a force-cancel is not needed in
// any normal circumstance. Only if Terraform encounters unexpected
// errors or behaves abnormally should this functionality be required.
// Force-cancel only becomes available if a normal cancel is performed
// first, and the desired canceled state is not reached within a pre-
// determined amount of time (see
// https://www.terraform.io/docs/enterprise/api/run.html#forcefully-cancel-a-run).
})
t.Run("when the run does not exist", func(t *testing.T) {
err := client.Runs.ForceCancel(ctx, "nonexisting", RunForceCancelOptions{})
assert.Equal(t, err, ErrResourceNotFound)
})
t.Run("with invalid run ID", func(t *testing.T) {
err := client.Runs.ForceCancel(ctx, badIdentifier, RunForceCancelOptions{})
assert.EqualError(t, err, "invalid value for run ID")
})
}
func TestRunsDiscard(t *testing.T) {
client := testClient(t)
ctx := context.Background()
rTest, rTestCleanup := createPlannedRun(t, client, nil)
defer rTestCleanup()
t.Run("when the run exists", func(t *testing.T) {
err := client.Runs.Discard(ctx, rTest.ID, RunDiscardOptions{})
assert.NoError(t, err)
})
t.Run("when the run does not exist", func(t *testing.T) {
err := client.Runs.Discard(ctx, "nonexisting", RunDiscardOptions{})
assert.Equal(t, err, ErrResourceNotFound)
})
t.Run("with invalid run ID", func(t *testing.T) {
err := client.Runs.Discard(ctx, badIdentifier, RunDiscardOptions{})
assert.EqualError(t, err, "invalid value for run ID")
})
}