forked from hashicorp/go-tfe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin_run_integration_test.go
330 lines (269 loc) · 9.2 KB
/
admin_run_integration_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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfe
import (
"bytes"
"context"
"encoding/json"
"fmt"
"testing"
"time"
retryablehttp "github.com/hashicorp/go-retryablehttp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAdminRuns_List(t *testing.T) {
skipUnlessEnterprise(t)
client := testClient(t)
ctx := context.Background()
org, orgCleanup := createOrganization(t, client)
defer orgCleanup()
wTest, wTestCleanup := createWorkspace(t, client, org)
defer wTestCleanup()
rTest1, rTestCleanup1 := createRun(t, client, wTest)
defer rTestCleanup1()
rTest2, rTestCleanup2 := createRun(t, client, wTest)
defer rTestCleanup2()
t.Run("without list options", func(t *testing.T) {
rl, err := client.Admin.Runs.List(ctx, nil)
require.NoError(t, err)
require.NotEmpty(t, rl.Items)
assert.Equal(t, adminRunItemsContainsID(rl.Items, rTest1.ID), true)
assert.Equal(t, adminRunItemsContainsID(rl.Items, rTest2.ID), true)
})
t.Run("with list options", func(t *testing.T) {
rl, err := client.Admin.Runs.List(ctx, &AdminRunsListOptions{
ListOptions: ListOptions{
PageNumber: 999,
PageSize: 100,
},
})
require.NoError(t, err)
// Out of range page number, so the items should be empty
assert.Empty(t, rl.Items)
assert.Equal(t, 999, rl.CurrentPage)
rl, err = client.Admin.Runs.List(ctx, &AdminRunsListOptions{
ListOptions: ListOptions{
PageNumber: 1,
PageSize: 100,
},
})
require.NoError(t, err)
require.NotEmpty(t, rl.Items)
assert.Equal(t, 1, rl.CurrentPage)
assert.Equal(t, adminRunItemsContainsID(rl.Items, rTest1.ID), true)
assert.Equal(t, adminRunItemsContainsID(rl.Items, rTest2.ID), true)
})
t.Run("with workspace included", func(t *testing.T) {
rl, err := client.Admin.Runs.List(ctx, &AdminRunsListOptions{
Include: []AdminRunIncludeOpt{AdminRunWorkspace},
})
require.NoError(t, err)
require.NotEmpty(t, rl.Items)
require.NotNil(t, rl.Items[0].Workspace)
assert.NotEmpty(t, rl.Items[0].Workspace.Name)
})
t.Run("with workspace.organization included", func(t *testing.T) {
rl, err := client.Admin.Runs.List(ctx, &AdminRunsListOptions{
Include: []AdminRunIncludeOpt{AdminRunWorkspaceOrg},
})
require.NoError(t, err)
require.NotEmpty(t, rl.Items)
require.NotNil(t, rl.Items[0].Workspace)
require.NotNil(t, rl.Items[0].Workspace.Organization)
assert.NotEmpty(t, rl.Items[0].Workspace.Organization.Name)
})
t.Run("with invalid Include option", func(t *testing.T) {
_, err := client.Admin.Runs.List(ctx, &AdminRunsListOptions{
Include: []AdminRunIncludeOpt{"workpsace"},
})
assert.Equal(t, ErrInvalidIncludeValue, err)
})
t.Run("with RunStatus.pending filter", func(t *testing.T) {
r1, err := client.Runs.Read(ctx, rTest1.ID)
require.NoError(t, err)
r2, err := client.Runs.Read(ctx, rTest2.ID)
require.NoError(t, err)
// There should be pending Runs
rl, err := client.Admin.Runs.List(ctx, &AdminRunsListOptions{
RunStatus: string(RunPending),
})
require.NoError(t, err)
require.NotEmpty(t, rl.Items)
assert.Equal(t, r1.Status, RunPlanning)
assert.Equal(t, adminRunItemsContainsID(rl.Items, r1.ID), false)
assert.Equal(t, r2.Status, RunPending)
assert.Equal(t, adminRunItemsContainsID(rl.Items, r2.ID), true)
})
t.Run("with RunStatus.applied filter", func(t *testing.T) {
// There should be no applied Runs
rl, err := client.Admin.Runs.List(ctx, &AdminRunsListOptions{
RunStatus: string(RunApplied),
})
require.NoError(t, err)
assert.Empty(t, rl.Items)
})
t.Run("with query", func(t *testing.T) {
rl, err := client.Admin.Runs.List(ctx, &AdminRunsListOptions{
Query: rTest1.ID,
})
require.NoError(t, err)
require.NotEmpty(t, rl.Items)
assert.Equal(t, adminRunItemsContainsID(rl.Items, rTest1.ID), true)
assert.Equal(t, adminRunItemsContainsID(rl.Items, rTest2.ID), false)
rl, err = client.Admin.Runs.List(ctx, &AdminRunsListOptions{
Query: rTest2.ID,
})
require.NoError(t, err)
require.NotEmpty(t, rl.Items)
assert.Equal(t, adminRunItemsContainsID(rl.Items, rTest1.ID), false)
assert.Equal(t, adminRunItemsContainsID(rl.Items, rTest2.ID), true)
})
}
func TestAdminRuns_ForceCancel(t *testing.T) {
skipUnlessEnterprise(t)
client := testClient(t)
ctx := context.Background()
org, orgCleanup := createOrganization(t, client)
defer orgCleanup()
wTest, wTestCleanup := createWorkspace(t, client, org)
defer wTestCleanup()
// We need to create 2 runs here.
// The first run will automatically be planned
// so that one cannot be cancelled.
rTest1, rCleanup1 := createRun(t, client, wTest)
defer rCleanup1()
// The second one will be pending until the first one is
// confirmed or discarded, so we can cancel that one.
rTest2, rCleanup2 := createRun(t, client, wTest)
defer rCleanup2()
assert.Equal(t, true, rTest1.Actions.IsCancelable)
assert.Equal(t, true, rTest1.Permissions.CanForceCancel)
assert.Equal(t, true, rTest2.Actions.IsCancelable)
assert.Equal(t, true, rTest2.Permissions.CanForceCancel)
t.Run("when the run does not exist", func(t *testing.T) {
err := client.Admin.Runs.ForceCancel(ctx, "nonexisting", AdminRunForceCancelOptions{})
assert.Equal(t, ErrResourceNotFound, err)
})
t.Run("with invalid run ID", func(t *testing.T) {
err := client.Admin.Runs.ForceCancel(ctx, badIdentifier, AdminRunForceCancelOptions{})
assert.EqualError(t, err, ErrInvalidRunID.Error())
})
t.Run("with can force cancel", func(t *testing.T) {
rTestPlanning, err := client.Runs.Read(ctx, rTest1.ID)
require.NoError(t, err)
assert.Equal(t, RunPlanning, rTestPlanning.Status)
require.NotNil(t, rTestPlanning.Actions)
require.NotNil(t, rTestPlanning.Permissions)
assert.Equal(t, true, rTestPlanning.Actions.IsCancelable)
assert.Equal(t, true, rTestPlanning.Permissions.CanForceCancel)
rTestPending, err := client.Runs.Read(ctx, rTest2.ID)
require.NoError(t, err)
assert.Equal(t, RunPending, rTestPending.Status)
require.NotNil(t, rTestPlanning.Actions)
require.NotNil(t, rTestPlanning.Permissions)
assert.Equal(t, true, rTestPending.Actions.IsCancelable)
assert.Equal(t, true, rTestPending.Permissions.CanForceCancel)
comment1 := "Misclick"
err = client.Admin.Runs.ForceCancel(ctx, rTestPending.ID, AdminRunForceCancelOptions{
Comment: String(comment1),
})
require.NoError(t, err)
rTestPendingResult, err := client.Runs.Read(ctx, rTestPending.ID)
require.NoError(t, err)
assert.Equal(t, RunCanceled, rTestPendingResult.Status)
comment2 := "Another misclick"
err = client.Admin.Runs.ForceCancel(ctx, rTestPlanning.ID, AdminRunForceCancelOptions{
Comment: String(comment2),
})
require.NoError(t, err)
rTestPlanningResult, err := client.Runs.Read(ctx, rTestPlanning.ID)
require.NoError(t, err)
assert.Equal(t, RunCanceled, rTestPlanningResult.Status)
})
}
func TestAdminRuns_AdminRunsListOptions_valid(t *testing.T) {
skipUnlessEnterprise(t)
t.Run("has valid status", func(t *testing.T) {
opts := AdminRunsListOptions{
RunStatus: string(RunPending),
}
err := opts.valid()
require.NoError(t, err)
})
t.Run("has invalid status", func(t *testing.T) {
opts := AdminRunsListOptions{
RunStatus: "random_status",
}
err := opts.valid()
assert.Error(t, err)
})
t.Run("has invalid status, even with a valid one", func(t *testing.T) {
statuses := fmt.Sprintf("%s,%s", string(RunPending), "random_status")
opts := AdminRunsListOptions{
RunStatus: statuses,
}
err := opts.valid()
assert.Error(t, err)
})
t.Run("has trailing comma and trailing space", func(t *testing.T) {
opts := AdminRunsListOptions{
RunStatus: "pending, ",
}
err := opts.valid()
require.NoError(t, err)
})
}
func TestAdminRun_ForceCancel_Marshal(t *testing.T) {
opts := AdminRunForceCancelOptions{
Comment: String("cancel comment"),
}
reqBody, err := serializeRequestBody(&opts)
require.NoError(t, err)
req, err := retryablehttp.NewRequest("POST", "url", reqBody)
require.NoError(t, err)
bodyBytes, err := req.BodyBytes()
require.NoError(t, err)
expectedBody := `{"comment":"cancel comment"}`
assert.Equal(t, expectedBody, string(bodyBytes))
}
func TestAdminRun_Unmarshal(t *testing.T) {
data := map[string]interface{}{
"data": map[string]interface{}{
"type": "runs",
"id": "run-VCsNJXa59eUza53R",
"attributes": map[string]interface{}{
"created-at": "2018-03-02T23:42:06.651Z",
"has-changes": true,
"status": RunApplied,
"status-timestamps": map[string]string{
"plan-queued-at": "2020-03-16T23:15:59+00:00",
},
},
},
}
byteData, err := json.Marshal(data)
if err != nil {
t.Fatal(err)
}
planQueuedParsedTime, err := time.Parse(time.RFC3339, "2020-03-16T23:15:59+00:00")
require.NoError(t, err)
adminRun := &AdminRun{}
responseBody := bytes.NewReader(byteData)
err = unmarshalResponse(responseBody, adminRun)
require.NoError(t, err)
assert.Equal(t, adminRun.ID, "run-VCsNJXa59eUza53R")
assert.Equal(t, adminRun.HasChanges, true)
assert.Equal(t, adminRun.Status, RunApplied)
assert.Equal(t, adminRun.StatusTimestamps.PlanQueuedAt, planQueuedParsedTime)
}
func adminRunItemsContainsID(items []*AdminRun, id string) bool {
hasID := false
for _, item := range items {
if item.ID == id {
hasID = true
break
}
}
return hasID
}