Debian Codegraph @

photon/tenants_test.go

raw symbols SCIP
1 // Copyright (c) 2017 VMware, Inc. All Rights Reserved.
2 //
3 // This product is licensed to you under the Apache License, Version 2.0 (the "License").
4 // You may not use this product except in compliance with the License.
5 //
6 // This product may include a number of subcomponents with separate copyright notices and
7 // license terms. Your use of these subcomponents is subject to the terms and conditions
8 // of the subcomponent's license, as noted in the LICENSE file.
9
10 package photon
11
12 import (
13 "fmt"
14 "reflect"
15
16 . "github.com/onsi/ginkgo"
17 . "github.com/onsi/gomega"
18 "github.com/vmware/photon-controller-go-sdk/photon/internal/mocks"
19 )
20
21 var _ = Describe("Tenant", func() {
22 var (
23 server *mocks.Server
24 client *Client
25 tenantID string
26 )
27
28 BeforeEach(func() {
29 server, client = testSetup()
30 })
31
32 AfterEach(func() {
33 cleanTenants(client)
34 server.Close()
35 })
36
37 Describe("CreateAndDeleteTenant", func() {
38 It("Tenant create and delete succeeds", func() {
39 mockTask := createMockTask("CREATE_TENANT", "COMPLETED")
40 server.SetResponseJson(200, mockTask)
41 tenantSpec := &TenantCreateSpec{Name: randomString(10, "go-sdk-tenant-")}
42 task, err := client.Tenants.Create(tenantSpec)
43 task, err = client.Tasks.Wait(task.ID)
44
45 GinkgoT().Log(err)
46 Expect(err).Should(BeNil())
47 Expect(task).ShouldNot(BeNil())
48 Expect(task.Operation).Should(Equal("CREATE_TENANT"))
49 Expect(task.State).Should(Equal("COMPLETED"))
50
51 mockTask = createMockTask("DELETE_TENANT", "COMPLETED")
52 server.SetResponseJson(200, mockTask)
53 task, err = client.Tenants.Delete(task.Entity.ID)
54
55 GinkgoT().Log(err)
56 Expect(err).Should(BeNil())
57 Expect(task).ShouldNot(BeNil())
58 Expect(task.Operation).Should(Equal("DELETE_TENANT"))
59 Expect(task.State).Should(Equal("COMPLETED"))
60 })
61
62 It("Tenant create fails", func() {
63 tenantSpec := &TenantCreateSpec{}
64 task, err := client.Tenants.Create(tenantSpec)
65
66 server.SetResponseJson(200, createMockTenantsPage())
67
68 Expect(err).ShouldNot(BeNil())
69 Expect(task).Should(BeNil())
70 })
71
72 It("creates and deletes a tenant with security groups specified", func() {
73 // Create a tenant with security groups.
74 mockTask := createMockTask("CREATE_TENANT", "COMPLETED")
75 server.SetResponseJson(200, mockTask)
76 securityGroups := []string{randomString(10), randomString(10)}
77 expected := &Tenant{
78 SecurityGroups: []SecurityGroup{
79 {securityGroups[0], false},
80 {securityGroups[1], false},
81 },
82 }
83 tenantSpec := &TenantCreateSpec{
84 Name: randomString(10, "go-sdk-tenant-"),
85 SecurityGroups: securityGroups,
86 }
87 task, err := client.Tenants.Create(tenantSpec)
88 Expect(task).ShouldNot(BeNil())
89 Expect(err).Should(BeNil())
90 task, err = client.Tasks.Wait(task.ID)
91 Expect(task).ShouldNot(BeNil())
92 Expect(err).Should(BeNil())
93 Expect(task.Operation).Should(Equal("CREATE_TENANT"))
94 Expect(task.State).Should(Equal("COMPLETED"))
95 tenantId := task.Entity.ID
96
97 // Verify that the tenant has the security groups set properly.
98 server.SetResponseJson(200, expected)
99 tenant, err := client.Tenants.Get(tenantId)
100 Expect(err).Should(BeNil())
101 Expect(tenant.SecurityGroups).Should(Equal(expected.SecurityGroups))
102
103 // Delete the tenant.
104 mockTask = createMockTask("DELETE_TENANT", "COMPLETED")
105 server.SetResponseJson(200, mockTask)
106 task, err = client.Tenants.Delete(tenantId)
107 Expect(task).ShouldNot(BeNil())
108 Expect(err).Should(BeNil())
109 Expect(task.Operation).Should(Equal("DELETE_TENANT"))
110 Expect(task.State).Should(Equal("COMPLETED"))
111 })
112 })
113
114 Describe("GetTenant", func() {
115 It("Get returns a tenant ID", func() {
116 mockTask := createMockTask("CREATE_TENANT", "COMPLETED")
117 server.SetResponseJson(200, mockTask)
118 tenantName := randomString(10, "go-sdk-tenant-")
119 tenantSpec := &TenantCreateSpec{Name: tenantName}
120 task, err := client.Tenants.Create(tenantSpec)
121
122 GinkgoT().Log(err)
123 Expect(err).Should(BeNil())
124 Expect(task).ShouldNot(BeNil())
125 Expect(task.Operation).Should(Equal("CREATE_TENANT"))
126 Expect(task.State).Should(Equal("COMPLETED"))
127
128 mockTenantsPage := createMockTenantsPage(Tenant{Name: tenantName})
129 server.SetResponseJson(200, mockTenantsPage)
130 tenants, err := client.Tenants.GetAll()
131
132 mockTenantPage := createMockTenantPage()
133 server.SetResponseJson(200, mockTenantPage)
134 mockTenant, err := client.Tenants.Get("TestTenant")
135 Expect(mockTenant.Name).Should(Equal("TestTenant"))
136 Expect(mockTenant.ID).Should(Equal("12345"))
137
138 GinkgoT().Log(err)
139 Expect(err).Should(BeNil())
140 Expect(tenants).ShouldNot(BeNil())
141
142 var found bool
143 for _, tenant := range tenants.Items {
144 if tenant.Name == tenantName && tenant.ID == task.Entity.ID {
145 found = true
146 break
147 }
148 }
149 Expect(found).Should(BeTrue())
150
151 mockTask = createMockTask("DELETE_TENANT", "COMPLETED")
152 server.SetResponseJson(200, mockTask)
153 _, err = client.Tenants.Delete(task.Entity.ID)
154
155 GinkgoT().Log(err)
156 Expect(err).Should(BeNil())
157 })
158 })
159
160 Describe("GetTenantTasks", func() {
161 var (
162 option string
163 )
164
165 Context("no extra options for GetTask", func() {
166 BeforeEach(func() {
167 option = ""
168 })
169
170 It("GetTasks returns a completed task", func() {
171 mockTask := createMockTask("CREATE_TENANT", "COMPLETED")
172 mockTask.Entity.ID = "mock-task-id"
173 server.SetResponseJson(200, mockTask)
174 tenantSpec := &TenantCreateSpec{Name: randomString(10, "go-sdk-tenant-")}
175 task, err := client.Tenants.Create(tenantSpec)
176
177 GinkgoT().Log(err)
178 Expect(err).Should(BeNil())
179 Expect(task).ShouldNot(BeNil())
180 Expect(task.Operation).Should(Equal("CREATE_TENANT"))
181 Expect(task.State).Should(Equal("COMPLETED"))
182
183 mockTasksPage := createMockTasksPage(*mockTask)
184 server.SetResponseJson(200, mockTasksPage)
185 taskList, err := client.Tenants.GetTasks(task.Entity.ID, &TaskGetOptions{State: option})
186 GinkgoT().Log(err)
187 Expect(err).Should(BeNil())
188 Expect(taskList).ShouldNot(BeNil())
189 Expect(taskList.Items).Should(ContainElement(*task))
190
191 mockTask = createMockTask("DELETE_TENANT", "COMPLETED")
192 server.SetResponseJson(200, mockTask)
193 _, err = client.Tenants.Delete(task.Entity.ID)
194
195 GinkgoT().Log(err)
196 Expect(err).Should(BeNil())
197 })
198 })
199
200 Context("Searching COMPLETED state for GetTask", func() {
201 BeforeEach(func() {
202 option = "COMPLETED"
203 })
204
205 It("GetTasks returns a completed task", func() {
206 mockTask := createMockTask("CREATE_TENANT", "COMPLETED")
207 mockTask.Entity.ID = "mock-task-id"
208 server.SetResponseJson(200, mockTask)
209 tenantSpec := &TenantCreateSpec{Name: randomString(10, "go-sdk-tenant-")}
210 task, err := client.Tenants.Create(tenantSpec)
211
212 GinkgoT().Log(err)
213 Expect(err).Should(BeNil())
214 Expect(task).ShouldNot(BeNil())
215 Expect(task.Operation).Should(Equal("CREATE_TENANT"))
216 Expect(task.State).Should(Equal("COMPLETED"))
217
218 mockTasksPage := createMockTasksPage(*mockTask)
219 server.SetResponseJson(200, mockTasksPage)
220 taskList, err := client.Tenants.GetTasks(task.Entity.ID, &TaskGetOptions{State: option})
221 GinkgoT().Log(err)
222 Expect(err).Should(BeNil())
223 Expect(taskList).ShouldNot(BeNil())
224 Expect(taskList.Items).Should(ContainElement(*task))
225
226 mockTask = createMockTask("DELETE_TENANT", "COMPLETED")
227 server.SetResponseJson(200, mockTask)
228 _, err = client.Tenants.Delete(task.Entity.ID)
229
230 GinkgoT().Log(err)
231 Expect(err).Should(BeNil())
232 })
233 })
234
235 })
236
237 Describe("TenantQuota", func() {
238
239 It("Get Tenant Quota succeeds", func() {
240 mockQuota := createMockQuota()
241
242 // Create Tenant
243 tenantID = createTenant(server, client)
244
245 // Get current Quota
246 server.SetResponseJson(200, mockQuota)
247 quota, err := client.Tenants.GetQuota(tenantID)
248
249 GinkgoT().Log(err)
250 eq := reflect.DeepEqual(quota.QuotaLineItems, mockQuota.QuotaLineItems)
251 Expect(eq).Should(Equal(true))
252 })
253
254 It("Set Tenant Quota succeeds", func() {
255 mockQuotaSpec := &QuotaSpec{
256 "vmCpu": {Unit: "COUNT", Limit: 10, Usage: 0},
257 "vmMemory": {Unit: "GB", Limit: 18, Usage: 0},
258 "diskCapacity": {Unit: "GB", Limit: 100, Usage: 0},
259 }
260
261 // Create Tenant
262 tenantID = createTenant(server, client)
263
264 mockTask := createMockTask("SET_QUOTA", "COMPLETED")
265 server.SetResponseJson(200, mockTask)
266
267 task, err := client.Tenants.SetQuota(tenantID, mockQuotaSpec)
268 task, err = client.Tasks.Wait(task.ID)
269 GinkgoT().Log(err)
270 Expect(err).Should(BeNil())
271 Expect(task).ShouldNot(BeNil())
272 Expect(task.Operation).Should(Equal("SET_QUOTA"))
273 Expect(task.State).Should(Equal("COMPLETED"))
274 })
275
276 It("Update Tenant Quota succeeds", func() {
277 mockQuotaSpec := &QuotaSpec{
278 "vmCpu": {Unit: "COUNT", Limit: 30, Usage: 0},
279 "vmMemory": {Unit: "GB", Limit: 40, Usage: 0},
280 "diskCapacity": {Unit: "GB", Limit: 150, Usage: 0},
281 }
282
283 // Create Tenant
284 tenantID = createTenant(server, client)
285
286 mockTask := createMockTask("UPDATE_QUOTA", "COMPLETED")
287 server.SetResponseJson(200, mockTask)
288
289 task, err := client.Tenants.UpdateQuota(tenantID, mockQuotaSpec)
290 task, err = client.Tasks.Wait(task.ID)
291 GinkgoT().Log(err)
292 Expect(err).Should(BeNil())
293 Expect(task).ShouldNot(BeNil())
294 Expect(task.Operation).Should(Equal("UPDATE_QUOTA"))
295 Expect(task.State).Should(Equal("COMPLETED"))
296 })
297
298 It("Exclude Tenant Quota Items succeeds", func() {
299 mockQuotaSpec := &QuotaSpec{
300 "vmCpu2": {Unit: "COUNT", Limit: 10, Usage: 0},
301 "vmMemory3": {Unit: "GB", Limit: 18, Usage: 0},
302 }
303
304 // Create Tenant
305 tenantID = createTenant(server, client)
306
307 mockTask := createMockTask("DELETE_QUOTA", "COMPLETED")
308 server.SetResponseJson(200, mockTask)
309
310 task, err := client.Tenants.ExcludeQuota(tenantID, mockQuotaSpec)
311 task, err = client.Tasks.Wait(task.ID)
312 GinkgoT().Log(err)
313 Expect(err).Should(BeNil())
314 Expect(task).ShouldNot(BeNil())
315 Expect(task.Operation).Should(Equal("DELETE_QUOTA"))
316 Expect(task.State).Should(Equal("COMPLETED"))
317 })
318 })
319 })
320
321 var _ = Describe("Project", func() {
322 var (
323 server *mocks.Server
324 client *Client
325 tenantID string
326 )
327
328 BeforeEach(func() {
329 server, client = testSetup()
330 tenantID = createTenant(server, client)
331 })
332
333 AfterEach(func() {
334 cleanTenants(client)
335 server.Close()
336 })
337
338 Describe("CreateGetAndDeleteProject", func() {
339 It("Project create and delete succeeds", func() {
340 mockTask := createMockTask("CREATE_PROJECT", "COMPLETED")
341 server.SetResponseJson(200, mockTask)
342 projSpec := &ProjectCreateSpec{
343 Name: randomString(10, "go-sdk-project-"),
344 }
345 task, err := client.Tenants.CreateProject(tenantID, projSpec)
346
347 GinkgoT().Log(err)
348 Expect(err).Should(BeNil())
349 Expect(task).ShouldNot(BeNil())
350 Expect(task.Operation).Should(Equal("CREATE_PROJECT"))
351 Expect(task.State).Should(Equal("COMPLETED"))
352
353 mockProjects := createMockProjectsPage(ProjectCompact{Name: projSpec.Name})
354 server.SetResponseJson(200, mockProjects)
355 projList, err := client.Tenants.GetProjects(tenantID, &ProjectGetOptions{})
356 GinkgoT().Log(err)
357 Expect(err).Should(BeNil())
358 Expect(task).ShouldNot(BeNil())
359 Expect(projList).ShouldNot(BeNil())
360
361 var found bool
362 for _, proj := range projList.Items {
363 if proj.Name == projSpec.Name && proj.ID == task.Entity.ID {
364 found = true
365 break
366 }
367 }
368 Expect(found).Should(BeTrue())
369
370 mockTask = createMockTask("DELETE_PROJECT", "COMPLETED")
371 server.SetResponseJson(200, mockTask)
372 task, err = client.Projects.Delete(task.Entity.ID)
373
374 GinkgoT().Log(err)
375 Expect(err).Should(BeNil())
376 Expect(task).ShouldNot(BeNil())
377 Expect(task.Operation).Should(Equal("DELETE_PROJECT"))
378 Expect(task.State).Should(Equal("COMPLETED"))
379 })
380 })
381
382 Describe("SecurityGroups", func() {
383 It("sets security groups for a tenant", func() {
384 // Create a tenant
385 mockTask := createMockTask("CREATE_TENANT", "COMPLETED")
386 server.SetResponseJson(200, mockTask)
387 tenantSpec := &TenantCreateSpec{Name: randomString(10, "go-sdk-tenant-")}
388 task, err := client.Tenants.Create(tenantSpec)
389 task, err = client.Tasks.Wait(task.ID)
390 Expect(err).Should(BeNil())
391 Expect(task).ShouldNot(BeNil())
392 Expect(task.Operation).Should(Equal("CREATE_TENANT"))
393 Expect(task.State).Should(Equal("COMPLETED"))
394
395 // Set security groups for the tenant
396 expected := &Tenant{
397 SecurityGroups: []SecurityGroup{
398 {randomString(10), false},
399 {randomString(10), false},
400 },
401 }
402 mockTask = createMockTask("SET_TENANT_SECURITY_GROUPS", "COMPLETED")
403 server.SetResponseJson(200, mockTask)
404 securityGroups := &SecurityGroupsSpec{
405 []string{expected.SecurityGroups[0].Name, expected.SecurityGroups[1].Name},
406 }
407 createTask, err := client.Tenants.SetSecurityGroups(task.Entity.ID, securityGroups)
408 createTask, err = client.Tasks.Wait(createTask.ID)
409 Expect(err).Should(BeNil())
410
411 // Get the security groups for the tenant
412 server.SetResponseJson(200, expected)
413 tenant, err := client.Tenants.Get(task.Entity.ID)
414 Expect(err).Should(BeNil())
415 fmt.Fprintf(GinkgoWriter, "Got tenant: %+v", tenant)
416 Expect(expected.SecurityGroups).Should(Equal(tenant.SecurityGroups))
417
418 // Delete the tenant
419 mockTask = createMockTask("DELETE_TENANT", "COMPLETED")
420 server.SetResponseJson(200, mockTask)
421 task, err = client.Tenants.Delete(task.Entity.ID)
422 task, err = client.Tasks.Wait(task.ID)
423 Expect(err).Should(BeNil())
424 Expect(task.Operation).Should(Equal("DELETE_TENANT"))
425 Expect(task.State).Should(Equal("COMPLETED"))
426 })
427 })
428 })
429
430 var _ = Describe("IAM", func() {
431 var (
432 server *mocks.Server
433 client *Client
434 tenantID string
435 )
436
437 BeforeEach(func() {
438 server, client = testSetup()
439 tenantID = createTenant(server, client)
440 })
441
442 AfterEach(func() {
443 cleanTenants(client)
444 server.Close()
445 })
446
447 Describe("ManageTenantIamPolicy", func() {
448 It("Set IAM Policy succeeds", func() {
449 mockTask := createMockTask("SET_IAM_POLICY", "COMPLETED")
450 server.SetResponseJson(200, mockTask)
451 var policy []*RoleBinding
452 policy = []*RoleBinding{{Role: "owner", Subjects: []string{"[email protected]"}}}
453 task, err := client.Tenants.SetIam(tenantID, policy)
454 task, err = client.Tasks.Wait(task.ID)
455 GinkgoT().Log(err)
456 Expect(err).Should(BeNil())
457 Expect(task).ShouldNot(BeNil())
458 Expect(task.Operation).Should(Equal("SET_IAM_POLICY"))
459 Expect(task.State).Should(Equal("COMPLETED"))
460 })
461
462 It("Modify IAM Policy succeeds", func() {
463 mockTask := createMockTask("MODIFY_IAM_POLICY", "COMPLETED")
464 server.SetResponseJson(200, mockTask)
465 var delta []*RoleBindingDelta
466 delta = []*RoleBindingDelta{{Subject: "[email protected]", Action: "ADD", Role: "owner"}}
467 task, err := client.Tenants.ModifyIam(tenantID, delta)
468 task, err = client.Tasks.Wait(task.ID)
469 GinkgoT().Log(err)
470 Expect(err).Should(BeNil())
471 Expect(task).ShouldNot(BeNil())
472 Expect(task.Operation).Should(Equal("MODIFY_IAM_POLICY"))
473 Expect(task.State).Should(Equal("COMPLETED"))
474 })
475
476 It("Get IAM Policy succeeds", func() {
477 var policy []*RoleBinding
478 policy = []*RoleBinding{{Role: "owner", Subjects: []string{"[email protected]"}}}
479 server.SetResponseJson(200, policy)
480 response, err := client.Tenants.GetIam(tenantID)
481 GinkgoT().Log(err)
482 Expect(err).Should(BeNil())
483 Expect(response[0].Subjects).Should(Equal(policy[0].Subjects))
484 Expect(response[0].Role).Should(Equal(policy[0].Role))
485 })
486 })
487 })