forked from hashicorp/go-tfe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_task_integration_test.go
323 lines (254 loc) · 9.26 KB
/
run_task_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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfe
import (
"context"
"errors"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func hasGlobalRunTasks(client *Client, organizationName string) (bool, error) {
ctx := context.Background()
if orgEntitlements, err := client.Organizations.ReadEntitlements(ctx, organizationName); err != nil {
return false, err
} else if orgEntitlements == nil {
return false, errors.New("The organization entitlements are empty.")
} else {
return orgEntitlements.GlobalRunTasks, nil
}
}
func TestRunTasksCreate(t *testing.T) {
client := testClient(t)
ctx := context.Background()
orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
newSubscriptionUpdater(orgTest).WithBusinessPlan().Update(t)
if v, err := hasGlobalRunTasks(client, orgTest.Name); err != nil {
t.Fatalf("Could not retrieve the entitlements for the test organization.: %s", err)
} else if !v {
t.Fatal("The test organization requires the global-run-tasks entitlement but is not entitled.")
return
}
runTaskServerURL := os.Getenv("TFC_RUN_TASK_URL")
if runTaskServerURL == "" {
t.Error("Cannot create a run task with an empty URL. You must set TFC_RUN_TASK_URL for run task related tests.")
}
runTaskName := "tst-runtask-" + randomString(t)
runTaskDescription := "A Run Task Description"
globalEnabled := true
globalStages := []Stage{
PostPlan,
PrePlan,
}
globalEnforce := Mandatory
t.Run("add run task to organization", func(t *testing.T) {
r, err := client.RunTasks.Create(ctx, orgTest.Name, RunTaskCreateOptions{
Name: runTaskName,
URL: runTaskServerURL,
Description: &runTaskDescription,
Category: "task",
Enabled: Bool(true),
Global: &GlobalRunTaskOptions{
Enabled: &globalEnabled,
Stages: &globalStages,
EnforcementLevel: &globalEnforce,
},
})
require.NoError(t, err)
assert.NotEmpty(t, r.ID)
assert.Equal(t, r.Name, runTaskName)
assert.Equal(t, r.URL, runTaskServerURL)
assert.Equal(t, r.Category, "task")
assert.Equal(t, r.Description, runTaskDescription)
assert.NotNil(t, r.Global)
assert.Equal(t, globalEnabled, r.Global.Enabled)
assert.Equal(t, globalEnforce, r.Global.EnforcementLevel)
assert.Equal(t, globalStages, r.Global.Stages)
t.Run("ensure org is deserialized properly", func(t *testing.T) {
assert.Equal(t, r.Organization.Name, orgTest.Name)
})
})
}
func TestRunTasksCreateWithoutGlobalEntitlement(t *testing.T) {
client := testClient(t)
ctx := context.Background()
orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
newSubscriptionUpdater(orgTest).WithTrialPlan().Update(t)
if v, err := hasGlobalRunTasks(client, orgTest.Name); err != nil {
t.Fatalf("Could not retrieve the entitlements for the test organization.: %s", err)
} else if v {
t.Fatal("The test organization should not have the global-run-tasks entitlement but it does.")
return
}
runTaskServerURL := os.Getenv("TFC_RUN_TASK_URL")
if runTaskServerURL == "" {
t.Error("Cannot create a run task with an empty URL. You must set TFC_RUN_TASK_URL for run task related tests.")
}
runTaskName := "tst-runtask-" + randomString(t)
runTaskDescription := "A Run Task Description"
globalStages := []Stage{
PostPlan,
PrePlan,
}
globalEnforce := Mandatory
t.Run("add run task to organization", func(t *testing.T) {
r, err := client.RunTasks.Create(ctx, orgTest.Name, RunTaskCreateOptions{
Name: runTaskName,
URL: runTaskServerURL,
Description: &runTaskDescription,
Category: "task",
// Even though we pass in these global parameters,
// they should be ignored and not throw an API error
Global: &GlobalRunTaskOptions{
Enabled: Bool(true),
Stages: &globalStages,
EnforcementLevel: &globalEnforce,
},
})
require.NoError(t, err)
assert.NotEmpty(t, r.ID)
assert.Nil(t, r.Global)
})
}
func TestRunTasksList(t *testing.T) {
client := testClient(t)
ctx := context.Background()
orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
upgradeOrganizationSubscription(t, client, orgTest)
_, runTaskTest1Cleanup := createRunTask(t, client, orgTest)
defer runTaskTest1Cleanup()
_, runTaskTest2Cleanup := createRunTask(t, client, orgTest)
defer runTaskTest2Cleanup()
t.Run("with no params", func(t *testing.T) {
runTaskList, err := client.RunTasks.List(ctx, orgTest.Name, nil)
require.NoError(t, err)
assert.NotNil(t, runTaskList.Items)
assert.NotEmpty(t, runTaskList.Items[0].ID)
assert.NotEmpty(t, runTaskList.Items[0].URL)
assert.NotEmpty(t, runTaskList.Items[1].ID)
assert.NotEmpty(t, runTaskList.Items[1].URL)
})
}
func TestRunTasksRead(t *testing.T) {
client := testClient(t)
ctx := context.Background()
orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
upgradeOrganizationSubscription(t, client, orgTest)
runTaskTest, runTaskTestCleanup := createRunTask(t, client, orgTest)
defer runTaskTestCleanup()
t.Run("by ID", func(t *testing.T) {
r, err := client.RunTasks.Read(ctx, runTaskTest.ID)
require.NoError(t, err)
assert.Equal(t, runTaskTest.ID, r.ID)
assert.Equal(t, runTaskTest.URL, r.URL)
assert.Equal(t, runTaskTest.Category, r.Category)
assert.Equal(t, runTaskTest.Description, r.Description)
assert.Equal(t, runTaskTest.HMACKey, r.HMACKey)
assert.Equal(t, runTaskTest.Enabled, r.Enabled)
})
t.Run("with options", func(t *testing.T) {
wkTest1, wkTest1Cleanup := createWorkspace(t, client, orgTest)
defer wkTest1Cleanup()
wkTest2, wkTest2Cleanup := createWorkspace(t, client, orgTest)
defer wkTest2Cleanup()
_, wrTest1Cleanup := createWorkspaceRunTask(t, client, wkTest1, runTaskTest)
defer wrTest1Cleanup()
_, wrTest2Cleanup := createWorkspaceRunTask(t, client, wkTest2, runTaskTest)
defer wrTest2Cleanup()
r, err := client.RunTasks.ReadWithOptions(ctx, runTaskTest.ID, &RunTaskReadOptions{
Include: []RunTaskIncludeOpt{RunTaskWorkspaceTasks},
})
require.NoError(t, err)
require.NotEmpty(t, r.WorkspaceRunTasks)
assert.NotEmpty(t, r.WorkspaceRunTasks[0].ID)
assert.NotEmpty(t, r.WorkspaceRunTasks[0].EnforcementLevel)
assert.NotEmpty(t, r.WorkspaceRunTasks[1].ID)
assert.NotEmpty(t, r.WorkspaceRunTasks[1].EnforcementLevel)
})
}
func TestRunTasksUpdate(t *testing.T) {
client := testClient(t)
ctx := context.Background()
orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
upgradeOrganizationSubscription(t, client, orgTest)
runTaskTest, runTaskTestCleanup := createRunTask(t, client, orgTest)
defer runTaskTestCleanup()
t.Run("rename task", func(t *testing.T) {
rename := runTaskTest.Name + "-UPDATED"
r, err := client.RunTasks.Update(ctx, runTaskTest.ID, RunTaskUpdateOptions{
Name: &rename,
})
require.NoError(t, err)
r, err = client.RunTasks.Read(ctx, r.ID)
require.NoError(t, err)
assert.Equal(t, rename, r.Name)
})
t.Run("toggle enabled", func(t *testing.T) {
runTaskTest.Enabled = !runTaskTest.Enabled
r, err := client.RunTasks.Update(ctx, runTaskTest.ID, RunTaskUpdateOptions{
Enabled: &runTaskTest.Enabled,
})
require.NoError(t, err)
r, err = client.RunTasks.Read(ctx, r.ID)
require.NoError(t, err)
assert.Equal(t, runTaskTest.Enabled, r.Enabled)
})
t.Run("update description", func(t *testing.T) {
newDescription := "An updated task description"
r, err := client.RunTasks.Update(ctx, runTaskTest.ID, RunTaskUpdateOptions{
Description: &newDescription,
})
require.NoError(t, err)
r, err = client.RunTasks.Read(ctx, r.ID)
require.NoError(t, err)
assert.Equal(t, newDescription, r.Description)
})
}
func TestRunTasksDelete(t *testing.T) {
client := testClient(t)
ctx := context.Background()
orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
upgradeOrganizationSubscription(t, client, orgTest)
runTaskTest, _ := createRunTask(t, client, orgTest)
t.Run("with valid options", func(t *testing.T) {
err := client.RunTasks.Delete(ctx, runTaskTest.ID)
require.NoError(t, err)
_, err = client.RunTasks.Read(ctx, runTaskTest.ID)
assert.Equal(t, err, ErrResourceNotFound)
})
t.Run("when the run task does not exist", func(t *testing.T) {
err := client.RunTasks.Delete(ctx, runTaskTest.ID)
assert.Equal(t, err, ErrResourceNotFound)
})
t.Run("when the run task ID is invalid", func(t *testing.T) {
err := client.RunTasks.Delete(ctx, badIdentifier)
assert.EqualError(t, err, ErrInvalidRunTaskID.Error())
})
}
func TestRunTasksAttachToWorkspace(t *testing.T) {
client := testClient(t)
ctx := context.Background()
orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
upgradeOrganizationSubscription(t, client, orgTest)
runTaskTest, runTaskTestCleanup := createRunTask(t, client, orgTest)
defer runTaskTestCleanup()
wkspaceTest, wkspaceTestCleanup := createWorkspace(t, client, orgTest)
defer wkspaceTestCleanup()
t.Run("to a valid workspace", func(t *testing.T) {
wr, err := client.RunTasks.AttachToWorkspace(ctx, wkspaceTest.ID, runTaskTest.ID, Advisory)
defer func() {
err = client.WorkspaceRunTasks.Delete(ctx, wkspaceTest.ID, wr.ID)
require.NoError(t, err)
}()
require.NoError(t, err)
require.NotNil(t, wr.ID)
})
}