Debian Codegraph @

photon/flavors_test.go

raw symbols SCIP
1 // Copyright (c) 2016 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 . "github.com/onsi/ginkgo"
14 . "github.com/onsi/gomega"
15 "github.com/vmware/photon-controller-go-sdk/photon/internal/mocks"
16 )
17
18 var _ = Describe("Flavor", func() {
19 var (
20 server *mocks.Server
21 client *Client
22 flavorSpec *FlavorCreateSpec
23 )
24
25 BeforeEach(func() {
26 server, client = testSetup()
27 flavorSpec = &FlavorCreateSpec{
28 Name: randomString(10, "go-sdk-flavor-"),
29 Kind: "vm",
30 Cost: []QuotaLineItem{QuotaLineItem{"GB", 16, "vm.memory"}},
31 }
32 })
33
34 AfterEach(func() {
35 cleanFlavors(client)
36 server.Close()
37 })
38
39 Describe("CreateGetAndDeleteFlavor", func() {
40 It("Flavor create and delete succeeds", func() {
41 mockTask := createMockTask("CREATE_FLAVOR", "COMPLETED")
42 server.SetResponseJson(200, mockTask)
43
44 flavorSpec := &FlavorCreateSpec{
45 Name: randomString(10, "go-sdk-flavor-"),
46 Kind: "vm",
47 Cost: []QuotaLineItem{QuotaLineItem{"GB", 16, "vm.memory"}},
48 }
49 task, err := client.Flavors.Create(flavorSpec)
50
51 GinkgoT().Log(err)
52 Expect(err).Should(BeNil())
53 Expect(task).ShouldNot(BeNil())
54 Expect(task.Operation).Should(Equal("CREATE_FLAVOR"))
55 Expect(task.State).Should(Equal("COMPLETED"))
56
57 mockTask = createMockTask("DELETE_FLAVOR", "COMPLETED")
58 server.SetResponseJson(200, mockTask)
59 task, err = client.Flavors.Delete(task.Entity.ID)
60
61 GinkgoT().Log(err)
62 Expect(err).Should(BeNil())
63 Expect(task).ShouldNot(BeNil())
64 Expect(task.Operation).Should(Equal("DELETE_FLAVOR"))
65 Expect(task.State).Should(Equal("COMPLETED"))
66 })
67 })
68
69 Describe("GetFlavor", func() {
70 var (
71 flavorName string
72 flavorID string
73 )
74
75 BeforeEach(func() {
76 flavorName, flavorID = createFlavor(server, client)
77 })
78
79 It("Get flavor succeeds", func() {
80 server.SetResponseJson(200, Flavor{Name: flavorName})
81 flavor, err := client.Flavors.Get(flavorID)
82
83 GinkgoT().Log(err)
84 Expect(err).Should(BeNil())
85 Expect(flavor).ShouldNot(BeNil())
86 Expect(flavor.ID).Should(Equal(flavorID))
87 Expect(flavor.Name).Should(Equal(flavorName))
88
89 mockTask := createMockTask("DELETE_FLAVOR", "COMPLETED")
90 server.SetResponseJson(200, mockTask)
91 _, err = client.Flavors.Delete(flavorID)
92
93 GinkgoT().Log(err)
94 Expect(err).Should(BeNil())
95 })
96
97 It("Get all flavor succeeds", func() {
98 mockFlavorsPage := createMockFlavorsPage(Flavor{Name: flavorName})
99 server.SetResponseJson(200, mockFlavorsPage)
100 flavorList, err := client.Flavors.GetAll(&FlavorGetOptions{})
101 GinkgoT().Log(err)
102 Expect(err).Should(BeNil())
103 Expect(flavorList).ShouldNot(BeNil())
104
105 var found bool
106 for _, flavor := range flavorList.Items {
107 if flavor.Name == flavorName && flavor.ID == flavorID {
108 found = true
109 break
110 }
111 }
112 Expect(found).Should(BeTrue())
113
114 mockTask := createMockTask("DELETE_FLAVOR", "COMPLETED")
115 server.SetResponseJson(200, mockTask)
116 _, err = client.Flavors.Delete(flavorID)
117
118 GinkgoT().Log(err)
119 Expect(err).Should(BeNil())
120 })
121 })
122
123 Describe("GetTasks", func() {
124 It("GetTasks returns a completed task", func() {
125 mockTask := createMockTask("CREATE_FLAVOR", "COMPLETED")
126 mockTask.Entity.ID = "mock-task-id"
127 server.SetResponseJson(200, mockTask)
128
129 task, err := client.Flavors.Create(flavorSpec)
130
131 GinkgoT().Log(err)
132 Expect(err).Should(BeNil())
133
134 mockTasksPage := createMockTasksPage(*mockTask)
135 server.SetResponseJson(200, mockTasksPage)
136 taskList, err := client.Flavors.GetTasks(task.Entity.ID, &TaskGetOptions{})
137
138 GinkgoT().Log(err)
139 Expect(err).Should(BeNil())
140 Expect(taskList).ShouldNot(BeNil())
141 Expect(taskList.Items).Should(ContainElement(*task))
142
143 mockTask = createMockTask("DELETE_FLAVOR", "COMPLETED")
144 server.SetResponseJson(200, mockTask)
145 _, err = client.Flavors.Delete(task.Entity.ID)
146
147 GinkgoT().Log(err)
148 Expect(err).Should(BeNil())
149 })
150 })
151 })