File: subnets_test.go

package info (click to toggle)
golang-github-vmware-photon-controller-go-sdk 0.0~PROMOTED-738%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 556 kB
  • sloc: sh: 33; makefile: 4
file content (225 lines) | stat: -rw-r--r-- 7,616 bytes parent folder | download
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
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
//
// This product is licensed to you under the Apache License, Version 2.0 (the "License").
// You may not use this product except in compliance with the License.
//
// This product may include a number of subcomponents with separate copyright notices and
// license terms. Your use of these subcomponents is subject to the terms and conditions
// of the subcomponent's license, as noted in the LICENSE file.

package photon

import (
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/vmware/photon-controller-go-sdk/photon/internal/mocks"
)

var _ = Describe("Subnet", func() {
	var (
		server                   *mocks.Server
		client                   *Client
		subnetCreateSpec         *SubnetCreateSpec
		subnetSpecWithPortGroups *SubnetCreateSpec
		portGroups               *PortGroups
		tenantID                 string
		projID                   string
		routerID                 string
	)

	BeforeEach(func() {
		server, client = testSetup()
		tenantID = createTenant(server, client)
		projID = createProject(server, client, tenantID)
		routerID = createRouter(server, client, projID)
		portGroups = &PortGroups{Names: []string{"portGroup"}}
		subnetCreateSpec = &SubnetCreateSpec{Name: "subnet-1", Description: "Test subnet", PrivateIpCidr: "cidr1"}
		subnetSpecWithPortGroups = &SubnetCreateSpec{Name: "subnet-1", Description: "Test subnet", PrivateIpCidr: "cidr1"}
	})

	AfterEach(func() {
		cleanSubnets(client)
		cleanTenants(client)
		server.Close()
	})

	Describe("CreateDeleteSubnet", func() {
		It("Subnet create and delete succeeds", func() {
			mockTask := createMockTask("CREATE_SUBNET", "COMPLETED")
			server.SetResponseJson(200, mockTask)

			task, err := client.Routers.CreateSubnet(routerID, subnetCreateSpec)
			task, err = client.Tasks.Wait(task.ID)
			GinkgoT().Log(err)
			Expect(err).Should(BeNil())
			Expect(task).ShouldNot(BeNil())
			Expect(task.Operation).Should(Equal("CREATE_SUBNET"))
			Expect(task.State).Should(Equal("COMPLETED"))

			mockTask = createMockTask("DELETE_SUBNET", "COMPLETED")
			server.SetResponseJson(200, mockTask)
			task, err = client.Subnets.Delete("subnet-Id")
			task, err = client.Tasks.Wait(task.ID)
			GinkgoT().Log(err)
			Expect(err).Should(BeNil())
			Expect(task).ShouldNot(BeNil())
			Expect(task.Operation).Should(Equal("DELETE_SUBNET"))
			Expect(task.State).Should(Equal("COMPLETED"))
		})
	})

	Describe("CreateDeletePortGroup", func() {
		It("PortGroup create and delete succeeds", func() {
			mockTask := createMockTask("CREATE_PORT_GROUP", "COMPLETED")
			server.SetResponseJson(200, mockTask)

			task, err := client.Subnets.Create(subnetSpecWithPortGroups)
			task, err = client.Tasks.Wait(task.ID)
			GinkgoT().Log(err)
			Expect(err).Should(BeNil())
			Expect(task).ShouldNot(BeNil())
			Expect(task.Operation).Should(Equal("CREATE_PORT_GROUP"))
			Expect(task.State).Should(Equal("COMPLETED"))

			mockTask = createMockTask("DELETE_PORT_GROUP", "COMPLETED")
			server.SetResponseJson(200, mockTask)
			task, err = client.Subnets.Delete(task.Entity.ID)
			task, err = client.Tasks.Wait(task.ID)
			GinkgoT().Log(err)
			Expect(err).Should(BeNil())
			Expect(task).ShouldNot(BeNil())
			Expect(task.Operation).Should(Equal("DELETE_PORT_GROUP"))
			Expect(task.State).Should(Equal("COMPLETED"))
		})
	})

	Describe("GetSubnet", func() {
		It("Get returns subnet", func() {
			mockTask := createMockTask("CREATE_SUBNET", "COMPLETED")
			server.SetResponseJson(200, mockTask)

			task, err := client.Routers.CreateSubnet(routerID, subnetCreateSpec)
			task, err = client.Tasks.Wait(task.ID)
			GinkgoT().Log(err)
			Expect(err).Should(BeNil())
			Expect(task).ShouldNot(BeNil())
			Expect(task.Operation).Should(Equal("CREATE_SUBNET"))
			Expect(task.State).Should(Equal("COMPLETED"))

			server.SetResponseJson(200, Subnet{Name: "subnet-1", Description: "Test subnet", PrivateIpCidr: "cidr1"})
			subnet, err := client.Subnets.Get(task.Entity.ID)

			GinkgoT().Log(err)
			Expect(err).Should(BeNil())
			Expect(subnet).ShouldNot(BeNil())

			var found bool
			if subnet.Name == "subnet-1" && subnet.ID == task.Entity.ID {
				found = true
			}
			Expect(found).Should(BeTrue())

			mockTask = createMockTask("DELETE_SUBNET", "COMPLETED")
			server.SetResponseJson(200, mockTask)
			_, err = client.Subnets.Delete(task.Entity.ID)

			GinkgoT().Log(err)
			Expect(err).Should(BeNil())
		})

		It("GetAll Subnet succeeds", func() {
			mockTask := createMockTask("CREATE_SUBNET", "COMPLETED")
			server.SetResponseJson(200, mockTask)
			task, err := client.Subnets.Create(subnetSpecWithPortGroups)
			task, err = client.Tasks.Wait(task.ID)
			GinkgoT().Log(err)
			Expect(err).Should(BeNil())

			server.SetResponseJson(200, createMockSubnetsPage(Subnet{Name: subnetSpecWithPortGroups.Name}))
			subnets, err := client.Subnets.GetAll(&SubnetGetOptions{})

			GinkgoT().Log(err)
			Expect(err).Should(BeNil())
			Expect(subnets).ShouldNot(BeNil())

			var found bool
			for _, subnet := range subnets.Items {
				if subnet.Name == subnetSpecWithPortGroups.Name && subnet.ID == task.Entity.ID {
					found = true
					break
				}
			}
			Expect(found).Should(BeTrue())

			mockTask = createMockTask("DELETE_SUBNET", "COMPLETED")
			server.SetResponseJson(200, mockTask)
			task, err = client.Subnets.Delete(task.Entity.ID)
			task, err = client.Tasks.Wait(task.ID)
			GinkgoT().Log(err)
			Expect(err).Should(BeNil())
		})

		It("GetAll PortGroups with optional name succeeds", func() {
			mockTask := createMockTask("CREATE_PORT_GROUP", "COMPLETED")
			server.SetResponseJson(200, mockTask)
			task, err := client.Subnets.Create(subnetSpecWithPortGroups)
			task, err = client.Tasks.Wait(task.ID)
			GinkgoT().Log(err)
			Expect(err).Should(BeNil())

			server.SetResponseJson(200, createMockSubnetsPage(Subnet{Name: subnetSpecWithPortGroups.Name}))
			subnets, err := client.Subnets.GetAll(&SubnetGetOptions{Name: subnetSpecWithPortGroups.Name})

			GinkgoT().Log(err)
			Expect(err).Should(BeNil())
			Expect(subnets).ShouldNot(BeNil())

			var found bool
			for _, subnet := range subnets.Items {
				if subnet.Name == subnetSpecWithPortGroups.Name && subnet.ID == task.Entity.ID {
					found = true
					break
				}
			}
			Expect(len(subnets.Items)).Should(Equal(1))
			Expect(found).Should(BeTrue())

			mockTask = createMockTask("DELETE_PORT_GROUP", "COMPLETED")
			server.SetResponseJson(200, mockTask)
			task, err = client.Subnets.Delete(task.Entity.ID)
			task, err = client.Tasks.Wait(task.ID)
			GinkgoT().Log(err)
			Expect(err).Should(BeNil())
		})
	})

	Describe("UpdateSubnet", func() {
		It("update subnet's name", func() {
			mockTask := createMockTask("UPDATE_SUBNET", "COMPLETED")
			server.SetResponseJson(200, mockTask)

			subnetSpec := &SubnetUpdateSpec{SubnetName: "subnet-1"}
			task, err := client.Subnets.Update("subnet-Id", subnetSpec)
			task, err = client.Tasks.Wait(task.ID)
			GinkgoT().Log(err)
			Expect(err).Should(BeNil())
			Expect(task).ShouldNot(BeNil())
			Expect(task.Operation).Should(Equal("UPDATE_SUBNET"))
			Expect(task.State).Should(Equal("COMPLETED"))
		})
	})

	Describe("SetDefaultSubnet", func() {
		It("Set default subnet succeeds", func() {
			mockTask := createMockTask("SET_DEFAULT_SUBNET", "COMPLETED")
			server.SetResponseJson(200, mockTask)

			task, err := client.Subnets.SetDefault("subnetId")
			GinkgoT().Log(err)
			Expect(err).Should(BeNil())
			Expect(task).ShouldNot(BeNil())
			Expect(task.Operation).Should(Equal("SET_DEFAULT_SUBNET"))
			Expect(task.State).Should(Equal("COMPLETED"))
		})
	})
})