forked from hashicorp/go-tfe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_task_integration_test.go
201 lines (150 loc) · 5.29 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
//go:build integration
// +build integration
package tfe
import (
"context"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRunTasksCreate(t *testing.T) {
skipIfBeta(t)
client := testClient(t)
ctx := context.Background()
orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
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)
t.Run("add run task to organization", func(t *testing.T) {
r, err := client.RunTasks.Create(ctx, orgTest.Name, RunTaskCreateOptions{
Name: runTaskName,
URL: runTaskServerURL,
Category: "task",
})
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")
t.Run("ensure org is deserialized properly", func(t *testing.T) {
assert.Equal(t, r.Organization.Name, orgTest.Name)
})
})
}
func TestRunTasksList(t *testing.T) {
skipIfBeta(t)
client := testClient(t)
ctx := context.Background()
orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
_, 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) {
skipIfBeta(t)
client := testClient(t)
ctx := context.Background()
orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
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.HMACKey, r.HMACKey)
})
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)
assert.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) {
skipIfBeta(t)
client := testClient(t)
ctx := context.Background()
orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
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)
})
}
func TestRunTasksDelete(t *testing.T) {
skipIfBeta(t)
client := testClient(t)
ctx := context.Background()
orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
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) {
skipIfBeta(t)
client := testClient(t)
ctx := context.Background()
orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
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)
require.NoError(t, err)
require.NotNil(t, wr.ID)
})
}