-
Notifications
You must be signed in to change notification settings - Fork 151
/
kubernetes_cluster_resource_spec.rb
363 lines (319 loc) · 14.6 KB
/
kubernetes_cluster_resource_spec.rb
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
require 'spec_helper'
RSpec.describe DropletKit::KubernetesClusterResource do
subject(:resource) { described_class.new(connection: connection) }
let(:kubernetes_node_pool_attributes) { DropletKit::KubernetesNodePool.new.attributes }
let(:kubernetes_maintenance_policy_attributes) { DropletKit::KubernetesMaintenancePolicy.new.attributes }
let(:cluster_id) { "c28bf806-eba8-4a6d-a98f-8fd388740bd0" }
include_context 'resources'
describe '#find' do
it 'returns a singular cluster' do
stub_do_api('/v2/kubernetes/clusters/20', :get).to_return(body: api_fixture('kubernetes/clusters/find'))
cluster = resource.find(id: 20)
expect(cluster).to be_kind_of(DropletKit::KubernetesCluster)
expect(cluster.id).to eq("cluster-1-id")
expect(cluster.name).to eq("test-cluster")
expect(cluster.region).to eq("nyc1")
expect(cluster.version).to eq("1.12.1-do.2")
expect(cluster.auto_upgrade).to eq(true)
expect(cluster.ha).to eq(true)
expect(cluster.cluster_subnet).to eq("10.244.0.0/16")
expect(cluster.ipv4).to eq("0.0.0.0")
expect(cluster.tags).to match_array(["test-k8", "k8s", "k8s:cluster-1-id"])
expect(cluster.node_pools.count).to eq(1)
expect(cluster.maintenance_policy).to eq(
"start_time" => "15:00",
"day" => "any"
)
node_pool = cluster.node_pools.first
expect(node_pool["auto_scale"]).to eq(true)
expect(node_pool["min_nodes"]).to eq(1)
expect(node_pool["max_nodes"]).to eq(10)
end
it_behaves_like 'resource that handles common errors' do
let(:path) { '/v2/kubernetes/clusters/123' }
let(:method) { :get }
let(:action) { :find }
let(:arguments) { { id: 123 } }
end
end
describe '#update' do
let(:path) { '/v2/kubernetes/clusters' }
let(:new_attrs) do
{
"name" => "new-test-name",
"tags" => ["new-test"],
"auto_upgrade" => true,
"maintenance_policy" => {
"start_time" => "12:00",
"day" => "Tuesday"
}
}
end
context 'for a successful update' do
it 'returns the created cluster' do
cluster = DropletKit::KubernetesCluster.new(new_attrs)
as_hash = DropletKit::KubernetesClusterMapping.hash_for(:update, cluster)
expect(as_hash['name']).to eq(cluster.name)
expect(as_hash['tags']).to eq(cluster.tags)
as_string = DropletKit::KubernetesClusterMapping.representation_for(:update, cluster)
stub_do_api(path, :put).with(body: as_string).to_return(body: api_fixture('kubernetes/clusters/update'), status: 202)
updated_cluster = resource.update(cluster)
expect(updated_cluster.name).to eq("new-test-name")
expect(updated_cluster.tags).to match_array(["new-test"])
expect(updated_cluster.auto_upgrade).to eq(true)
expect(updated_cluster.maintenance_policy).to eq(
"start_time" => "12:00",
"day" => "Tuesday"
)
end
end
end
describe '#all' do
it 'returns all of the clusters' do
stub_do_api('/v2/kubernetes/clusters', :get).to_return(body: api_fixture('kubernetes/all'))
clusters = resource.all
expect(clusters).to all(be_kind_of(DropletKit::KubernetesCluster))
cluster = clusters.first
expect(cluster.id).to eq("cluster-1-id")
expect(cluster.name).to eq("test-cluster")
expect(cluster.region).to eq("nyc1")
expect(cluster.version).to eq("1.12.1-do.2")
expect(cluster.cluster_subnet).to eq("10.244.0.0/16")
expect(cluster.ipv4).to eq("0.0.0.0")
expect(cluster.tags).to match_array(["test-k8", "k8s", "k8s:cluster-1-id"])
expect(cluster.node_pools.count).to eq(1)
end
it 'returns an empty array of droplets' do
stub_do_api('/v2/kubernetes/clusters', :get).to_return(body: api_fixture('kubernetes/all_empty'))
clusters = resource.all.map(&:id)
expect(clusters).to be_empty
end
it_behaves_like 'a paginated index' do
let(:fixture_path) { 'kubernetes/all' }
let(:api_path) { '/v2/kubernetes/clusters' }
end
end
describe '#create' do
let(:path) { '/v2/kubernetes/clusters' }
let(:new_attrs) do
{
"name" => "test-cluster-01",
"region" => "nyc1",
"version" => "1.12.1-do.2",
"tags" => ["test"],
"auto_upgrade" => true,
"maintenance_policy" => {
"start_time" => "15:00",
"day" => "any"
},
"node_pools" => [
{
"size" => "s-1vcpu-1gb",
"count" => 3,
"name" => "frontend-pool",
"tags" => ["frontend"]
},
{
"size" => "c-4",
"count" => 2,
"name" => "backend-pool"
}
]
}
end
context 'for a successful create' do
it 'returns the created cluster' do
cluster = DropletKit::KubernetesCluster.new(new_attrs)
as_hash = DropletKit::KubernetesClusterMapping.hash_for(:create, cluster)
expect(as_hash['name']).to eq(cluster.name)
expect(as_hash['region']).to eq(cluster.region)
expect(as_hash['version']).to eq(cluster.version)
expect(as_hash['tags']).to eq(cluster.tags)
expect(as_hash['node_pools']).to eq(cluster.node_pools)
as_string = DropletKit::KubernetesClusterMapping.representation_for(:create, cluster)
stub_do_api(path, :post).with(body: as_string).to_return(body: api_fixture('kubernetes/clusters/create'), status: 201)
created_cluster = resource.create(cluster)
expect(cluster.id).to eq("cluster-1-id")
expect(cluster.name).to eq("test-cluster")
expect(cluster.region).to eq("nyc1")
expect(cluster.version).to eq("1.12.1-do.2")
expect(cluster.maintenance_policy).to eq(
"start_time" => "15:00",
"day" => "any"
)
expect(cluster.cluster_subnet).to eq("10.244.0.0/16")
expect(cluster.ipv4).to eq("0.0.0.0")
expect(cluster.tags).to match_array(["test-k8", "k8s", "k8s:cluster-1-id"])
expect(cluster.node_pools.count).to eq(1)
node_pool = cluster.node_pools.first
expect(node_pool["auto_scale"]).to eq(true)
expect(node_pool["min_nodes"]).to eq(1)
expect(node_pool["max_nodes"]).to eq(10)
end
it 'reuses the same object' do
cluster = DropletKit::KubernetesCluster.new(new_attrs)
json = DropletKit::KubernetesClusterMapping.representation_for(:create, cluster)
stub_do_api(path, :post).with(body: json).to_return(body: api_fixture('kubernetes/clusters/create'), status: 201)
created_cluster = resource.create(cluster)
expect(created_cluster).to be cluster
end
end
it_behaves_like 'an action that handles invalid parameters' do
let(:action) { 'create' }
let(:arguments) { DropletKit::KubernetesCluster.new }
end
end
describe '#delete' do
it 'sends a delete request for a cluster' do
request = stub_do_api('/v2/kubernetes/clusters/23', :delete).to_return(status: 202)
response = resource.delete(id: 23)
expect(request).to have_been_made
expect(response).to eq(true)
end
end
describe '#kubeconfig' do
it 'returns a yaml string kubeconfig' do
response = Pathname.new('./spec/fixtures/kubernetes/clusters/kubeconfig.txt').read
stub_do_api('/v2/kubernetes/clusters/1/kubeconfig', :get).to_return(body: response)
kubeconfig = resource.kubeconfig(id: '1')
expect(kubeconfig).to be_kind_of(String)
parsed_config = YAML.load(kubeconfig)
expect(parsed_config.keys).to match_array(["apiVersion", "clusters", "contexts", "current-context", "kind", "preferences", "users"])
end
end
describe "node_pools" do
it 'returns the node_pools for a cluster' do
stub_do_api("/v2/kubernetes/clusters/#{cluster_id}/node_pools", :get).to_return(body: api_fixture('kubernetes/cluster_node_pools'))
node_pools= resource.node_pools(id: cluster_id)
node_pools.each do |pool|
expect(pool).to be_kind_of(DropletKit::KubernetesNodePool)
expect(pool.attributes.keys).to eq kubernetes_node_pool_attributes.keys
end
expect(node_pools.length).to eq 1
expect(node_pools.first["id"]).to eq "0a209365-2fac-465e-a959-bb91f232923a"
expect(node_pools.first["name"]).to eq "k8s-1-12-1-do-1-nyc1-1540837045848-1"
expect(node_pools.first["size"]).to eq "s-4vcpu-8gb"
expect(node_pools.first["count"]).to eq 2
expect(node_pools.first["tags"]).to eq [ "omar-left-his-mark" ]
expect(node_pools.first["nodes"].length).to eq 2
expect(node_pools.first["auto_scale"]).to eq(true)
expect(node_pools.first["min_nodes"]).to eq(1)
expect(node_pools.first["max_nodes"]).to eq(10)
end
end
describe "find_node_pool" do
it "should return a single node pool" do
node_pool_id = "f9f16e5a-83b8-4c9b-acf1-4f91492a6652"
stub_do_api("/v2/kubernetes/clusters/#{cluster_id}/node_pools/#{node_pool_id}", :get).to_return(body: api_fixture('kubernetes/cluster_node_pool'))
node_pool = resource.find_node_pool(id: cluster_id, pool_id: node_pool_id)
expect(node_pool.id).to eq node_pool_id
expect(node_pool.name).to eq "k8s-1-12-1-do-2-nyc1-1542638764614-1"
expect(node_pool.size).to eq "s-1vcpu-1gb"
expect(node_pool.count).to eq 1
expect(node_pool.tags).to eq ["k8s", "k8s:c28bf806-eba8-4a6d-a98f-8fd388740bd0", "k8s:worker"]
expect(node_pool.auto_scale).to eq(true)
expect(node_pool.min_nodes).to eq(1)
expect(node_pool.max_nodes).to eq(10)
expect(node_pool.nodes.length).to eq 1
expect(node_pool.nodes.first.name).to eq "blissful-antonelli-3u87"
expect(node_pool.nodes.first.status['state']).to eq "running"
end
end
describe "create_node_pool" do
it 'should create a node_pool in a cluster' do
node_pool = DropletKit::KubernetesNodePool.new(
name: 'frontend',
size: 's-1vcpu-1gb',
count: 3,
tags: ['k8-tag'],
labels: { foo: 'bar' },
auto_scale: true,
min_nodes: 1,
max_nodes: 10
)
as_hash = DropletKit::KubernetesNodePoolMapping.hash_for(:create, node_pool)
expect(as_hash['name']).to eq(node_pool.name)
expect(as_hash['size']).to eq(node_pool.size)
expect(as_hash['count']).to eq(node_pool.count)
expect(as_hash['tags']).to eq(node_pool.tags)
as_string = DropletKit::KubernetesNodePoolMapping.representation_for(:create, node_pool)
stub_do_api("/v2/kubernetes/clusters/#{cluster_id}/node_pools", :post).with(body: as_string).to_return(body: api_fixture('kubernetes/cluster_node_pool_create'), status: 201)
new_node_pool = resource.create_node_pool(node_pool, id: cluster_id)
expect(new_node_pool).to be_kind_of(DropletKit::KubernetesNodePool)
expect(new_node_pool.name).to eq 'frontend'
expect(new_node_pool.size).to eq 's-1vcpu-1gb'
expect(new_node_pool.count).to eq 3
expect(new_node_pool.tags).to eq ['k8-tag']
expect(new_node_pool.labels).to eq('foo' => 'bar')
expect(node_pool.auto_scale).to eq(true)
expect(node_pool.min_nodes).to eq(1)
expect(node_pool.max_nodes).to eq(10)
expect(new_node_pool.nodes.length).to eq 3
new_node_pool.nodes.each do |node|
expect(node['name']).to eq ""
expect(node['status']['state']).to eq 'provisioning'
end
end
end
describe "update_node_pool" do
it "should update an existing node_pool" do
stub_do_api("/v2/kubernetes/clusters/#{cluster_id}/node_pools", :get).to_return(body: api_fixture('kubernetes/cluster_node_pools'))
node_pools = resource.node_pools(id: cluster_id)
node_pools.each do |pool|
expect(pool).to be_kind_of(DropletKit::KubernetesNodePool)
end
node_pool_id = "0a209365-2fac-465e-a959-bb91f232923a"
expect(node_pools.length).to eq 1
expect(node_pools.first["id"]).to eq node_pool_id
expect(node_pools.first["name"]).to eq "k8s-1-12-1-do-1-nyc1-1540837045848-1"
expect(node_pools.first["size"]).to eq "s-4vcpu-8gb"
expect(node_pools.first["count"]).to eq 2
expect(node_pools.first["tags"]).to eq [ "omar-left-his-mark" ]
node_pool = node_pools.first
node_pool.name = 'backend'
node_pool.size = 's-1vcpu-1gb'
node_pool.count = 2
node_pool.tags = ['updated-k8-tag']
node_pool.labels = { foo: 'bar' }
as_string = DropletKit::KubernetesNodePoolMapping.representation_for(:update, node_pool)
stub_do_api("/v2/kubernetes/clusters/#{cluster_id}/node_pools/#{node_pool_id}", :put).with(body: as_string).to_return(body: api_fixture('kubernetes/cluster_node_pool_update'), status: 202)
updated_node_pool = resource.update_node_pool(node_pool, id: cluster_id, pool_id: node_pool_id)
expect(updated_node_pool.id).to eq node_pool_id
expect(updated_node_pool.name).to eq 'backend'
expect(updated_node_pool.size).to eq 's-1vcpu-1gb'
expect(updated_node_pool.count).to eq 2
expect(updated_node_pool.tags).to eq ['backend']
expect(updated_node_pool.labels).to eq('foo' => 'bar')
expect(updated_node_pool.nodes.length).to eq 2
end
end
describe 'delete_node_pool' do
it 'should delete a clusters node_pool' do
node_pool_id = "f9f16e5a-83b8-4c9b-acf1-4f91492a6652"
stub_do_api("/v2/kubernetes/clusters/#{cluster_id}/node_pools/#{node_pool_id}", :delete).to_return(status: 202)
deleted_node_pool = resource.delete_node_pool(id: cluster_id, pool_id: node_pool_id)
expect(deleted_node_pool).to eq true
end
end
describe 'recycle_node_pool' do
it 'should recycle the clusters node_pool' do
stub_do_api("/v2/kubernetes/clusters/#{cluster_id}/node_pools", :get).to_return(body: api_fixture('kubernetes/cluster_node_pools'))
node_pools = resource.node_pools(id: cluster_id)
node_pools.each do |pool|
expect(pool).to be_kind_of(DropletKit::KubernetesNodePool)
end
node_pool_id = "0a209365-2fac-465e-a959-bb91f232923a"
expect(node_pools.length).to eq 1
expect(node_pools.first["id"]).to eq node_pool_id
expect(node_pools.first["name"]).to eq "k8s-1-12-1-do-1-nyc1-1540837045848-1"
expect(node_pools.first["count"]).to eq 2
nodes = node_pools.first.nodes
expect(nodes.length).to eq 2
node_ids = nodes.map(&:id)
recycle_json = { nodes: node_ids}.to_json
stub_do_api("/v2/kubernetes/clusters/#{cluster_id}/node_pools/#{node_pool_id}/recycle", :post).with(body: recycle_json).to_return(status: 202)
response = resource.recycle_node_pool(node_ids, id: cluster_id, pool_id: node_pool_id)
expect(response).to eq true
end
end
end