-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathadmin_workspace_integration_test.go
324 lines (263 loc) · 9.82 KB
/
admin_workspace_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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfe
import (
"bytes"
"context"
"encoding/json"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// BEWARE: The admin workspaces API can view all of the workspaces created by
// EVERY test organization in EVERY concurrent test run (or other usage) for the
// current HCP Terraform instance. It's generally not safe to assume that the workspaces
// you create in a given test will be within the first page of list results, so
// you might have to get creative and/or settle for less when testing the
// behavior of these endpoints.
func TestAdminWorkspaces_ListWithFilter(t *testing.T) {
skipUnlessEnterprise(t)
client := testClient(t)
ctx := context.Background()
org, orgCleanup := createOrganization(t, client)
defer orgCleanup()
wTest1, wTest1Cleanup := createWorkspace(t, client, org)
defer wTest1Cleanup()
wTest2, wTest2Cleanup := createWorkspace(t, client, org)
defer wTest2Cleanup()
t.Run("when filtering workspaces on a current run status", func(t *testing.T) {
_, appliedCleanup := createRunApply(t, client, wTest1)
t.Cleanup(appliedCleanup)
_, unAppliedCleanup := createRunUnapplied(t, client, wTest2)
t.Cleanup(unAppliedCleanup)
wl, err := client.Admin.Workspaces.List(ctx, &AdminWorkspaceListOptions{
Filter: string(RunApplied), Include: []AdminWorkspaceIncludeOpt{AdminWorkspaceCurrentRun},
})
require.NoError(t, err)
require.NotEmpty(t, wl.Items)
assert.Equal(t, wl.Items[0].CurrentRun.Status, RunApplied)
assert.NotContains(t, wl.Items, wTest2)
})
}
func TestAdminWorkspaces_ListWithSort(t *testing.T) {
skipUnlessEnterprise(t)
client := testClient(t)
ctx := context.Background()
org, orgCleanup := createOrganization(t, client)
defer orgCleanup()
wTest1, wTest1Cleanup := createWorkspace(t, client, org)
defer wTest1Cleanup()
wTest2, wTest2Cleanup := createWorkspace(t, client, org)
defer wTest2Cleanup()
t.Run("when sorting by workspace names", func(t *testing.T) {
wl, err := client.Admin.Workspaces.List(ctx, &AdminWorkspaceListOptions{
Sort: "name",
})
require.NoError(t, err)
require.NotEmpty(t, wl.Items)
require.GreaterOrEqual(t, len(wl.Items), 2)
assert.Equal(t, wl.Items[0].Name < wl.Items[1].Name, true)
})
t.Run("when sorting workspaces on current-run.created-at", func(t *testing.T) {
_, unappliedCleanup1 := createRunUnapplied(t, client, wTest1)
t.Cleanup(unappliedCleanup1)
_, unappliedCleanup2 := createRunUnapplied(t, client, wTest2)
t.Cleanup(unappliedCleanup2)
wl, err := client.Admin.Workspaces.List(ctx, &AdminWorkspaceListOptions{
Include: []AdminWorkspaceIncludeOpt{AdminWorkspaceCurrentRun},
Sort: "current-run.created-at",
})
require.NoError(t, err)
require.NotEmpty(t, wl.Items)
require.GreaterOrEqual(t, len(wl.Items), 2)
assert.True(t, wl.Items[1].CurrentRun.CreatedAt.After(wl.Items[0].CurrentRun.CreatedAt))
})
}
func TestAdminWorkspaces_List(t *testing.T) {
skipUnlessEnterprise(t)
client := testClient(t)
ctx := context.Background()
org, orgCleanup := createOrganization(t, client)
defer orgCleanup()
wTest1, wTest1Cleanup := createWorkspace(t, client, org)
defer wTest1Cleanup()
wTest2, wTest2Cleanup := createWorkspace(t, client, org)
defer wTest2Cleanup()
t.Run("without list options", func(t *testing.T) {
wl, err := client.Admin.Workspaces.List(ctx, nil)
require.NoError(t, err)
require.GreaterOrEqual(t, len(wl.Items), 2)
})
t.Run("with list options", func(t *testing.T) {
wl, err := client.Admin.Workspaces.List(ctx, &AdminWorkspaceListOptions{
ListOptions: ListOptions{
PageNumber: 999,
PageSize: 100,
},
})
require.NoError(t, err)
// Out of range page number, so the items should be empty
assert.Empty(t, wl.Items)
assert.Equal(t, 999, wl.CurrentPage)
wl, err = client.Admin.Workspaces.List(ctx, &AdminWorkspaceListOptions{
ListOptions: ListOptions{
PageNumber: 1,
PageSize: 100,
},
})
require.NoError(t, err)
assert.Equal(t, 1, wl.CurrentPage)
assert.Equal(t, adminWorkspaceItemsContainsID(wl.Items, wTest1.ID), true)
assert.Equal(t, adminWorkspaceItemsContainsID(wl.Items, wTest2.ID), true)
})
t.Run("when searching a known workspace", func(t *testing.T) {
// Use a known workspace prefix as search attribute. The result
// should be successful and only contain the matching workspace.
wl, err := client.Admin.Workspaces.List(ctx, &AdminWorkspaceListOptions{
Query: wTest1.Name,
})
require.NoError(t, err)
assert.Equal(t, adminWorkspaceItemsContainsID(wl.Items, wTest1.ID), true)
assert.Equal(t, adminWorkspaceItemsContainsID(wl.Items, wTest2.ID), false)
assert.Equal(t, 1, wl.CurrentPage)
assert.Equal(t, true, wl.TotalCount == 1)
})
t.Run("when searching an unknown workspace", func(t *testing.T) {
// Use a nonexisting workspace name as search attribute. The result
// should be successful, but return no results.
wl, err := client.Admin.Workspaces.List(ctx, &AdminWorkspaceListOptions{
Query: "nonexisting",
})
require.NoError(t, err)
assert.Empty(t, wl.Items)
assert.Equal(t, 1, wl.CurrentPage)
assert.Equal(t, 0, wl.TotalCount)
})
t.Run("with organization included", func(t *testing.T) {
wl, err := client.Admin.Workspaces.List(ctx, &AdminWorkspaceListOptions{
Include: []AdminWorkspaceIncludeOpt{AdminWorkspaceOrg},
})
require.NoError(t, err)
require.NotEmpty(t, wl.Items)
require.NotNil(t, wl.Items[0].Organization)
assert.NotEmpty(t, wl.Items[0].Organization.Name)
})
// This sub-test should remain last because it creates a run that does not apply
// Any subsequent runs will be queued until a timeout is triggered
t.Run("with current_run included", func(t *testing.T) {
cvTest, cvCleanup := createUploadedConfigurationVersion(t, client, wTest1)
defer cvCleanup()
runOpts := RunCreateOptions{
ConfigurationVersion: cvTest,
Workspace: wTest1,
}
run, err := client.Runs.Create(ctx, runOpts)
require.NoError(t, err)
wl, err := client.Admin.Workspaces.List(ctx, &AdminWorkspaceListOptions{
Include: []AdminWorkspaceIncludeOpt{AdminWorkspaceCurrentRun},
})
require.NoError(t, err)
require.NotEmpty(t, wl.Items)
require.NotNil(t, wl.Items[0].CurrentRun)
assert.Equal(t, wl.Items[0].CurrentRun.ID, run.ID)
})
}
func TestAdminWorkspaces_Read(t *testing.T) {
skipUnlessEnterprise(t)
client := testClient(t)
ctx := context.Background()
t.Run("it fails to read a workspace with an invalid name", func(t *testing.T) {
workspace, err := client.Admin.Workspaces.Read(ctx, "")
require.Error(t, err)
assert.EqualError(t, err, ErrInvalidWorkspaceValue.Error())
assert.Nil(t, workspace)
})
t.Run("it fails to read a workspace that is non existent", func(t *testing.T) {
workspaceID := fmt.Sprintf("non-existent-%s", randomString(t))
adminWorkspace, err := client.Admin.Workspaces.Read(ctx, workspaceID)
require.Error(t, err)
assert.EqualError(t, err, ErrResourceNotFound.Error())
assert.Nil(t, adminWorkspace)
})
t.Run("it reads a workspace successfully", func(t *testing.T) {
org, orgCleanup := createOrganization(t, client)
defer orgCleanup()
workspace, workspaceCleanup := createWorkspace(t, client, org)
defer workspaceCleanup()
adminWorkspace, err := client.Admin.Workspaces.Read(ctx, workspace.ID)
require.NoError(t, err)
require.NotNilf(t, adminWorkspace, "Admin Workspace is not nil")
assert.Equal(t, adminWorkspace.ID, workspace.ID)
assert.Equal(t, adminWorkspace.Name, workspace.Name)
assert.Equal(t, adminWorkspace.Locked, workspace.Locked)
})
}
func TestAdminWorkspaces_Delete(t *testing.T) {
skipUnlessEnterprise(t)
client := testClient(t)
ctx := context.Background()
t.Run("it fails to delete an organization with an invalid id", func(t *testing.T) {
err := client.Admin.Workspaces.Delete(ctx, "")
require.Error(t, err)
assert.EqualError(t, err, ErrInvalidWorkspaceValue.Error())
})
t.Run("it fails to delete an organization with an bad org name", func(t *testing.T) {
workspaceID := fmt.Sprintf("non-existent-%s", randomString(t))
err := client.Admin.Workspaces.Delete(ctx, workspaceID)
require.Error(t, err)
assert.EqualError(t, err, ErrResourceNotFound.Error())
})
t.Run("it deletes a workspace successfully", func(t *testing.T) {
org, orgCleanup := createOrganization(t, client)
defer orgCleanup()
workspace, _ := createWorkspace(t, client, org)
adminWorkspace, err := client.Admin.Workspaces.Read(ctx, workspace.ID)
require.NoError(t, err)
require.NotNilf(t, adminWorkspace, "Admin Workspace is not nil")
assert.Equal(t, adminWorkspace.ID, workspace.ID)
err = client.Admin.Workspaces.Delete(ctx, adminWorkspace.ID)
require.NoError(t, err)
// Cannot find deleted workspace
_, err = client.Admin.Workspaces.Read(ctx, workspace.ID)
assert.Error(t, err)
assert.EqualError(t, err, ErrResourceNotFound.Error())
})
}
func adminWorkspaceItemsContainsID(items []*AdminWorkspace, id string) bool {
hasID := false
for _, item := range items {
if item.ID == id {
hasID = true
break
}
}
return hasID
}
func TestAdminWorkspace_Unmarshal(t *testing.T) {
data := map[string]interface{}{
"data": map[string]interface{}{
"type": "workspaces",
"id": "workspaces-VCsNJXa59eUza53R",
"attributes": map[string]interface{}{
"name": "workspace-name",
"locked": false,
"vcs-repo": map[string]string{
"identifier": "github",
},
},
},
}
byteData, err := json.Marshal(data)
if err != nil {
t.Fatal(err)
}
adminWorkspace := &AdminWorkspace{}
responseBody := bytes.NewReader(byteData)
err = unmarshalResponse(responseBody, adminWorkspace)
require.NoError(t, err)
assert.Equal(t, adminWorkspace.ID, "workspaces-VCsNJXa59eUza53R")
assert.Equal(t, adminWorkspace.Name, "workspace-name")
assert.Equal(t, adminWorkspace.Locked, false)
assert.Equal(t, adminWorkspace.VCSRepo.Identifier, "github")
}