kubeclient 4.9.0 → 4.9.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (112) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/actions.yml +42 -0
  3. data/CHANGELOG.md +54 -0
  4. data/README.md +7 -0
  5. data/kubeclient.gemspec +4 -2
  6. data/lib/kubeclient/common.rb +18 -6
  7. data/lib/kubeclient/config.rb +22 -10
  8. data/lib/kubeclient/version.rb +1 -1
  9. metadata +22 -212
  10. data/.travis.yml +0 -29
  11. data/test/cassettes/kubernetes_guestbook.yml +0 -879
  12. data/test/config/allinone.kubeconfig +0 -20
  13. data/test/config/execauth.kubeconfig +0 -62
  14. data/test/config/external-ca.pem +0 -18
  15. data/test/config/external-cert.pem +0 -19
  16. data/test/config/external-key.rsa +0 -27
  17. data/test/config/external.kubeconfig +0 -20
  18. data/test/config/gcpauth.kubeconfig +0 -22
  19. data/test/config/gcpcmdauth.kubeconfig +0 -26
  20. data/test/config/nouser.kubeconfig +0 -16
  21. data/test/config/oidcauth.kubeconfig +0 -25
  22. data/test/config/timestamps.kubeconfig +0 -25
  23. data/test/config/userauth.kubeconfig +0 -28
  24. data/test/json/bindings_list.json +0 -10
  25. data/test/json/component_status.json +0 -17
  26. data/test/json/component_status_list.json +0 -52
  27. data/test/json/config.istio.io_api_resource_list.json +0 -679
  28. data/test/json/config_map_list.json +0 -9
  29. data/test/json/core_api_resource_list.json +0 -181
  30. data/test/json/core_api_resource_list_without_kind.json +0 -129
  31. data/test/json/core_oapi_resource_list_without_kind.json +0 -197
  32. data/test/json/created_endpoint.json +0 -28
  33. data/test/json/created_namespace.json +0 -20
  34. data/test/json/created_secret.json +0 -16
  35. data/test/json/created_security_context_constraint.json +0 -65
  36. data/test/json/created_service.json +0 -31
  37. data/test/json/empty_pod_list.json +0 -9
  38. data/test/json/endpoint_list.json +0 -48
  39. data/test/json/entity_list.json +0 -56
  40. data/test/json/event_list.json +0 -35
  41. data/test/json/extensions_v1beta1_api_resource_list.json +0 -217
  42. data/test/json/limit_range.json +0 -23
  43. data/test/json/limit_range_list.json +0 -31
  44. data/test/json/namespace.json +0 -13
  45. data/test/json/namespace_exception.json +0 -8
  46. data/test/json/namespace_list.json +0 -32
  47. data/test/json/node.json +0 -29
  48. data/test/json/node_list.json +0 -37
  49. data/test/json/node_notice.json +0 -160
  50. data/test/json/persistent_volume.json +0 -37
  51. data/test/json/persistent_volume_claim.json +0 -32
  52. data/test/json/persistent_volume_claim_list.json +0 -40
  53. data/test/json/persistent_volume_claims_nil_items.json +0 -8
  54. data/test/json/persistent_volume_list.json +0 -45
  55. data/test/json/pod.json +0 -92
  56. data/test/json/pod_list.json +0 -79
  57. data/test/json/pod_template_list.json +0 -9
  58. data/test/json/pods_1.json +0 -265
  59. data/test/json/pods_2.json +0 -102
  60. data/test/json/pods_410.json +0 -9
  61. data/test/json/processed_template.json +0 -27
  62. data/test/json/replication_controller.json +0 -57
  63. data/test/json/replication_controller_list.json +0 -66
  64. data/test/json/resource_quota.json +0 -46
  65. data/test/json/resource_quota_list.json +0 -54
  66. data/test/json/secret_list.json +0 -44
  67. data/test/json/security.openshift.io_api_resource_list.json +0 -69
  68. data/test/json/security_context_constraint_list.json +0 -375
  69. data/test/json/service.json +0 -33
  70. data/test/json/service_account.json +0 -25
  71. data/test/json/service_account_list.json +0 -82
  72. data/test/json/service_illegal_json_404.json +0 -1
  73. data/test/json/service_json_patch.json +0 -26
  74. data/test/json/service_list.json +0 -97
  75. data/test/json/service_merge_patch.json +0 -26
  76. data/test/json/service_patch.json +0 -25
  77. data/test/json/service_update.json +0 -22
  78. data/test/json/template.json +0 -27
  79. data/test/json/template.openshift.io_api_resource_list.json +0 -75
  80. data/test/json/template_list.json +0 -35
  81. data/test/json/versions_list.json +0 -6
  82. data/test/json/watch_stream.json +0 -3
  83. data/test/test_common.rb +0 -95
  84. data/test/test_component_status.rb +0 -29
  85. data/test/test_config.rb +0 -222
  86. data/test/test_endpoint.rb +0 -54
  87. data/test/test_exec_credentials.rb +0 -225
  88. data/test/test_gcp_command_credentials.rb +0 -27
  89. data/test/test_google_application_default_credentials.rb +0 -15
  90. data/test/test_guestbook_go.rb +0 -235
  91. data/test/test_helper.rb +0 -18
  92. data/test/test_kubeclient.rb +0 -881
  93. data/test/test_limit_range.rb +0 -25
  94. data/test/test_missing_methods.rb +0 -80
  95. data/test/test_namespace.rb +0 -59
  96. data/test/test_node.rb +0 -70
  97. data/test/test_oidc_auth_provider.rb +0 -103
  98. data/test/test_persistent_volume.rb +0 -29
  99. data/test/test_persistent_volume_claim.rb +0 -28
  100. data/test/test_pod.rb +0 -81
  101. data/test/test_pod_log.rb +0 -157
  102. data/test/test_process_template.rb +0 -80
  103. data/test/test_replication_controller.rb +0 -47
  104. data/test/test_resource_list_without_kind.rb +0 -78
  105. data/test/test_resource_quota.rb +0 -23
  106. data/test/test_secret.rb +0 -62
  107. data/test/test_security_context_constraint.rb +0 -62
  108. data/test/test_service.rb +0 -357
  109. data/test/test_service_account.rb +0 -26
  110. data/test/test_watch.rb +0 -195
  111. data/test/txt/pod_log.txt +0 -6
  112. data/test/valid_token_file +0 -1
@@ -1,881 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- # Kubernetes client entity tests
4
- class KubeclientTest < MiniTest::Test
5
- def test_json
6
- our_object = Kubeclient::Resource.new
7
- our_object.foo = 'bar'
8
- our_object.nested = {}
9
- our_object.nested.again = {}
10
- our_object.nested.again.again = {}
11
- our_object.nested.again.again.name = 'aaron'
12
-
13
- expected = {
14
- 'foo' => 'bar',
15
- 'nested' => { 'again' => { 'again' => { 'name' => 'aaron' } } }
16
- }
17
-
18
- assert_equal(expected, JSON.parse(JSON.dump(our_object.to_h)))
19
- end
20
-
21
- def test_pass_uri
22
- # URI::Generic#hostname= was added in ruby 1.9.3 and will automatically
23
- # wrap an ipv6 address in []
24
- uri = URI::HTTP.build(port: 8080)
25
- uri.hostname = 'localhost'
26
- client = Kubeclient::Client.new(uri)
27
- rest_client = client.rest_client
28
- assert_equal('http://localhost:8080/api/v1', rest_client.url.to_s)
29
- end
30
-
31
- def test_no_path_in_uri
32
- client = Kubeclient::Client.new('http://localhost:8080', 'v1')
33
- rest_client = client.rest_client
34
- assert_equal('http://localhost:8080/api/v1', rest_client.url.to_s)
35
- end
36
-
37
- def test_no_version_passed
38
- client = Kubeclient::Client.new('http://localhost:8080')
39
- rest_client = client.rest_client
40
- assert_equal('http://localhost:8080/api/v1', rest_client.url.to_s)
41
- end
42
-
43
- def test_pass_proxy
44
- uri = URI::HTTP.build(host: 'localhost', port: 8080)
45
- proxy_uri = URI::HTTP.build(host: 'myproxyhost', port: 8888)
46
- stub_core_api_list
47
-
48
- client = Kubeclient::Client.new(uri, http_proxy_uri: proxy_uri)
49
- rest_client = client.rest_client
50
- assert_equal(proxy_uri.to_s, rest_client.options[:proxy])
51
-
52
- watch_client = client.watch_pods
53
- assert_equal(watch_client.send(:build_client_options)[:proxy][:proxy_address], proxy_uri.host)
54
- assert_equal(watch_client.send(:build_client_options)[:proxy][:proxy_port], proxy_uri.port)
55
- end
56
-
57
- def test_pass_max_redirects
58
- max_redirects = 0
59
- client = Kubeclient::Client.new('http://localhost:8080/api/', http_max_redirects: max_redirects)
60
- rest_client = client.rest_client
61
- assert_equal(max_redirects, rest_client.options[:max_redirects])
62
-
63
- stub_request(:get, 'http://localhost:8080/api')
64
- .to_return(status: 302, headers: { location: 'http://localhost:1234/api' })
65
-
66
- exception = assert_raises(Kubeclient::HttpError) { client.api }
67
- assert_equal(302, exception.error_code)
68
- end
69
-
70
- def test_exception
71
- stub_core_api_list
72
- stub_request(:post, %r{/services})
73
- .to_return(body: open_test_file('namespace_exception.json'), status: 409)
74
-
75
- service = Kubeclient::Resource.new
76
- service.metadata = {}
77
- service.metadata.name = 'redisslave'
78
- service.metadata.namespace = 'default'
79
- # service.port = 80
80
- # service.container_port = 6379
81
- # service.protocol = 'TCP'
82
-
83
- client = Kubeclient::Client.new('http://localhost:8080/api/')
84
-
85
- exception = assert_raises(Kubeclient::HttpError) do
86
- service = client.create_service(service)
87
- end
88
-
89
- assert_instance_of(Kubeclient::HttpError, exception)
90
- assert_equal("converting to : type names don't match (Pod, Namespace)",
91
- exception.message)
92
-
93
- assert_includes(exception.to_s, ' for POST http://localhost:8080/api')
94
- assert_equal(409, exception.error_code)
95
- end
96
-
97
- def test_deprecated_exception
98
- error_message = 'certificate verify failed'
99
-
100
- stub_request(:get, 'http://localhost:8080/api')
101
- .to_raise(OpenSSL::SSL::SSLError.new(error_message))
102
-
103
- client = Kubeclient::Client.new('http://localhost:8080/api/')
104
-
105
- exception = assert_raises(KubeException) { client.api }
106
- assert_equal(error_message, exception.message)
107
- end
108
-
109
- def test_api
110
- stub_request(:get, 'http://localhost:8080/api')
111
- .to_return(status: 200, body: open_test_file('versions_list.json'))
112
-
113
- response = client.api
114
- assert_includes(response, 'versions')
115
- end
116
-
117
- def test_api_ssl_failure
118
- error_message = 'certificate verify failed'
119
-
120
- stub_request(:get, 'http://localhost:8080/api')
121
- .to_raise(OpenSSL::SSL::SSLError.new(error_message))
122
-
123
- client = Kubeclient::Client.new('http://localhost:8080/api/')
124
-
125
- exception = assert_raises(Kubeclient::HttpError) { client.api }
126
- assert_equal(error_message, exception.message)
127
- end
128
-
129
- def test_api_timeout
130
- stub_request(:get, 'http://localhost:8080/api').to_timeout
131
-
132
- client = Kubeclient::Client.new('http://localhost:8080/api/')
133
-
134
- exception = assert_raises(Kubeclient::HttpError) { client.api }
135
- assert_match(/(timed out|timeout)/i, exception.message)
136
- end
137
-
138
- def test_api_valid
139
- stub_request(:get, 'http://localhost:8080/api')
140
- .to_return(status: 200, body: open_test_file('versions_list.json'))
141
-
142
- args = ['http://localhost:8080/api/']
143
-
144
- [nil, 'v1beta3', 'v1'].each do |version|
145
- client = Kubeclient::Client.new(*(version ? args + [version] : args))
146
- assert client.api_valid?
147
- end
148
- end
149
-
150
- def test_api_valid_with_invalid_version
151
- stub_request(:get, 'http://localhost:8080/api')
152
- .to_return(status: 200, body: open_test_file('versions_list.json'))
153
-
154
- client = Kubeclient::Client.new('http://localhost:8080/api/', 'foobar1')
155
- refute client.api_valid?
156
- end
157
-
158
- def test_api_valid_with_unreported_versions
159
- stub_request(:get, 'http://localhost:8080/api')
160
- .to_return(status: 200, body: '{}')
161
-
162
- client = Kubeclient::Client.new('http://localhost:8080/api/')
163
- refute client.api_valid?
164
- end
165
-
166
- def test_api_valid_with_invalid_json
167
- stub_request(:get, 'http://localhost:8080/api')
168
- .to_return(status: 200, body: '[]')
169
-
170
- client = Kubeclient::Client.new('http://localhost:8080/api/')
171
- refute client.api_valid?
172
- end
173
-
174
- def test_api_valid_with_bad_endpoint
175
- stub_request(:get, 'http://localhost:8080/api')
176
- .to_return(status: [404, 'Resource Not Found'])
177
-
178
- client = Kubeclient::Client.new('http://localhost:8080/api/')
179
- assert_raises(Kubeclient::HttpError) { client.api_valid? }
180
- end
181
-
182
- def test_api_valid_with_non_json
183
- stub_request(:get, 'http://localhost:8080/api')
184
- .to_return(status: 200, body: '<html></html>')
185
-
186
- client = Kubeclient::Client.new('http://localhost:8080/api/')
187
- assert_raises(JSON::ParserError) { client.api_valid? }
188
- end
189
-
190
- def test_nonjson_exception
191
- stub_core_api_list
192
- stub_request(:get, %r{/servic})
193
- .to_return(body: open_test_file('service_illegal_json_404.json'), status: 404)
194
-
195
- exception = assert_raises(Kubeclient::ResourceNotFoundError) do
196
- client.get_services
197
- end
198
-
199
- assert(exception.message.include?('Not Found'))
200
- assert_equal(404, exception.error_code)
201
- end
202
-
203
- def test_nonjson_exception_raw
204
- stub_core_api_list
205
- stub_request(:get, %r{/servic})
206
- .to_return(body: open_test_file('service_illegal_json_404.json'), status: 404)
207
-
208
- exception = assert_raises(Kubeclient::ResourceNotFoundError) do
209
- client.get_services(as: :raw)
210
- end
211
-
212
- assert(exception.message.include?('Not Found'))
213
- assert_equal(404, exception.error_code)
214
- end
215
-
216
- def test_entity_list
217
- stub_core_api_list
218
- stub_get_services
219
-
220
- services = client.get_services
221
-
222
- refute_empty(services)
223
- assert_instance_of(Kubeclient::Common::EntityList, services)
224
- # Stripping of 'List' in collection.kind RecursiveOpenStruct mode only is historic.
225
- assert_equal('Service', services.kind)
226
- assert_equal(2, services.size)
227
- assert_instance_of(Kubeclient::Resource, services[0])
228
- assert_instance_of(Kubeclient::Resource, services[1])
229
-
230
- assert_requested(:get, 'http://localhost:8080/api/v1/services', times: 1)
231
- end
232
-
233
- def test_entity_list_raw
234
- stub_core_api_list
235
- stub_get_services
236
-
237
- response = client.get_services(as: :raw)
238
-
239
- refute_empty(response)
240
- assert_equal(open_test_file('entity_list.json').read, response)
241
-
242
- assert_requested(:get, 'http://localhost:8080/api/v1/services', times: 1)
243
- end
244
-
245
- def test_entity_list_parsed
246
- stub_core_api_list
247
- stub_get_services
248
-
249
- response = client.get_services(as: :parsed)
250
- assert_equal Hash, response.class
251
- assert_equal 'ServiceList', response['kind']
252
- assert_equal %w[metadata spec status], response['items'].first.keys
253
- end
254
-
255
- def test_entity_list_parsed_symbolized
256
- stub_core_api_list
257
- stub_get_services
258
-
259
- response = client.get_services(as: :parsed_symbolized)
260
- assert_equal Hash, response.class
261
- assert_equal 'ServiceList', response[:kind]
262
- assert_equal %i[metadata spec status], response[:items].first.keys
263
- end
264
-
265
- def test_entity_list_unknown
266
- stub_core_api_list
267
- stub_get_services
268
-
269
- e = assert_raises(ArgumentError) { client.get_services(as: :whoops) }
270
- assert_equal 'Unsupported format :whoops', e.message
271
- end
272
-
273
- def test_entity_list_raw_failure
274
- stub_core_api_list
275
- stub_request(:get, %r{/services})
276
- .to_return(body: open_test_file('entity_list.json'), status: 500)
277
-
278
- exception = assert_raises(Kubeclient::HttpError) { client.get_services(as: :raw) }
279
- assert_equal('500 Internal Server Error', exception.message)
280
- assert_equal(500, exception.error_code)
281
- end
282
-
283
- def test_entities_with_label_selector
284
- selector = 'component=apiserver'
285
-
286
- stub_core_api_list
287
- stub_get_services
288
-
289
- services = client.get_services(label_selector: selector)
290
-
291
- assert_instance_of(Kubeclient::Common::EntityList, services)
292
- assert_requested(
293
- :get,
294
- "http://localhost:8080/api/v1/services?labelSelector=#{selector}",
295
- times: 1
296
- )
297
- end
298
-
299
- def test_entities_with_resource_version
300
- version = '329'
301
-
302
- stub_core_api_list
303
- stub_get_services
304
-
305
- services = client.get_services(resource_version: version)
306
-
307
- assert_instance_of(Kubeclient::Common::EntityList, services)
308
- assert_requested(
309
- :get,
310
- "http://localhost:8080/api/v1/services?resourceVersion=#{version}",
311
- times: 1
312
- )
313
- end
314
-
315
- def test_entities_with_field_selector
316
- selector = 'involvedObject.name=redis-master'
317
-
318
- stub_core_api_list
319
- stub_get_services
320
-
321
- services = client.get_services(field_selector: selector)
322
-
323
- assert_instance_of(Kubeclient::Common::EntityList, services)
324
- assert_requested(
325
- :get,
326
- "http://localhost:8080/api/v1/services?fieldSelector=#{selector}",
327
- times: 1
328
- )
329
- end
330
-
331
- def test_empty_list
332
- stub_core_api_list
333
- stub_request(:get, %r{/pods})
334
- .to_return(body: open_test_file('empty_pod_list.json'), status: 200)
335
-
336
- pods = client.get_pods
337
- assert_instance_of(Kubeclient::Common::EntityList, pods)
338
- assert_equal(0, pods.size)
339
- end
340
-
341
- def test_get_all
342
- stub_core_api_list
343
-
344
- stub_request(:get, %r{/bindings})
345
- .to_return(body: open_test_file('bindings_list.json'), status: 404)
346
-
347
- stub_request(:get, %r{/configmaps})
348
- .to_return(body: open_test_file('config_map_list.json'), status: 200)
349
-
350
- stub_request(:get, %r{/podtemplates})
351
- .to_return(body: open_test_file('pod_template_list.json'), status: 200)
352
-
353
- stub_request(:get, %r{/services})
354
- .to_return(body: open_test_file('service_list.json'), status: 200)
355
-
356
- stub_request(:get, %r{/pods})
357
- .to_return(body: open_test_file('pod_list.json'), status: 200)
358
-
359
- stub_request(:get, %r{/nodes})
360
- .to_return(body: open_test_file('node_list.json'), status: 200)
361
-
362
- stub_request(:get, %r{/replicationcontrollers})
363
- .to_return(body: open_test_file('replication_controller_list.json'), status: 200)
364
-
365
- stub_request(:get, %r{/events})
366
- .to_return(body: open_test_file('event_list.json'), status: 200)
367
-
368
- stub_request(:get, %r{/endpoints})
369
- .to_return(body: open_test_file('endpoint_list.json'), status: 200)
370
-
371
- stub_request(:get, %r{/namespaces})
372
- .to_return(body: open_test_file('namespace_list.json'), status: 200)
373
-
374
- stub_request(:get, %r{/secrets})
375
- .to_return(body: open_test_file('secret_list.json'), status: 200)
376
-
377
- stub_request(:get, %r{/resourcequotas})
378
- .to_return(body: open_test_file('resource_quota_list.json'), status: 200)
379
-
380
- stub_request(:get, %r{/limitranges})
381
- .to_return(body: open_test_file('limit_range_list.json'), status: 200)
382
-
383
- stub_request(:get, %r{/persistentvolumes})
384
- .to_return(body: open_test_file('persistent_volume_list.json'), status: 200)
385
-
386
- stub_request(:get, %r{/persistentvolumeclaims})
387
- .to_return(body: open_test_file('persistent_volume_claim_list.json'), status: 200)
388
-
389
- stub_request(:get, %r{/componentstatuses})
390
- .to_return(body: open_test_file('component_status_list.json'), status: 200)
391
-
392
- stub_request(:get, %r{/serviceaccounts})
393
- .to_return(body: open_test_file('service_account_list.json'), status: 200)
394
-
395
- result = client.all_entities
396
- assert_equal(16, result.keys.size)
397
- assert_instance_of(Kubeclient::Common::EntityList, result['node'])
398
- assert_instance_of(Kubeclient::Common::EntityList, result['service'])
399
- assert_instance_of(Kubeclient::Common::EntityList, result['replication_controller'])
400
- assert_instance_of(Kubeclient::Common::EntityList, result['pod'])
401
- assert_instance_of(Kubeclient::Common::EntityList, result['event'])
402
- assert_instance_of(Kubeclient::Common::EntityList, result['namespace'])
403
- assert_instance_of(Kubeclient::Common::EntityList, result['secret'])
404
- assert_instance_of(Kubeclient::Resource, result['service'][0])
405
- assert_instance_of(Kubeclient::Resource, result['node'][0])
406
- assert_instance_of(Kubeclient::Resource, result['event'][0])
407
- assert_instance_of(Kubeclient::Resource, result['endpoint'][0])
408
- assert_instance_of(Kubeclient::Resource, result['namespace'][0])
409
- assert_instance_of(Kubeclient::Resource, result['secret'][0])
410
- assert_instance_of(Kubeclient::Resource, result['resource_quota'][0])
411
- assert_instance_of(Kubeclient::Resource, result['limit_range'][0])
412
- assert_instance_of(Kubeclient::Resource, result['persistent_volume'][0])
413
- assert_instance_of(Kubeclient::Resource, result['persistent_volume_claim'][0])
414
- assert_instance_of(Kubeclient::Resource, result['component_status'][0])
415
- assert_instance_of(Kubeclient::Resource, result['service_account'][0])
416
- end
417
-
418
- def test_get_all_raw
419
- stub_core_api_list
420
-
421
- stub_request(:get, %r{/bindings})
422
- .to_return(body: open_test_file('bindings_list.json'), status: 404)
423
-
424
- stub_request(:get, %r{/configmaps})
425
- .to_return(body: open_test_file('config_map_list.json'), status: 200)
426
-
427
- stub_request(:get, %r{/podtemplates})
428
- .to_return(body: open_test_file('pod_template_list.json'), status: 200)
429
-
430
- stub_request(:get, %r{/services})
431
- .to_return(body: open_test_file('service_list.json'), status: 200)
432
-
433
- stub_request(:get, %r{/pods})
434
- .to_return(body: open_test_file('pod_list.json'), status: 200)
435
-
436
- stub_request(:get, %r{/nodes})
437
- .to_return(body: open_test_file('node_list.json'), status: 200)
438
-
439
- stub_request(:get, %r{/replicationcontrollers})
440
- .to_return(body: open_test_file('replication_controller_list.json'), status: 200)
441
-
442
- stub_request(:get, %r{/events})
443
- .to_return(body: open_test_file('event_list.json'), status: 200)
444
-
445
- stub_request(:get, %r{/endpoints})
446
- .to_return(body: open_test_file('endpoint_list.json'), status: 200)
447
-
448
- stub_request(:get, %r{/namespaces})
449
- .to_return(body: open_test_file('namespace_list.json'), status: 200)
450
-
451
- stub_request(:get, %r{/secrets})
452
- .to_return(body: open_test_file('secret_list.json'), status: 200)
453
-
454
- stub_request(:get, %r{/resourcequotas})
455
- .to_return(body: open_test_file('resource_quota_list.json'), status: 200)
456
-
457
- stub_request(:get, %r{/limitranges})
458
- .to_return(body: open_test_file('limit_range_list.json'), status: 200)
459
-
460
- stub_request(:get, %r{/persistentvolumes})
461
- .to_return(body: open_test_file('persistent_volume_list.json'), status: 200)
462
-
463
- stub_request(:get, %r{/persistentvolumeclaims})
464
- .to_return(body: open_test_file('persistent_volume_claim_list.json'), status: 200)
465
-
466
- stub_request(:get, %r{/componentstatuses})
467
- .to_return(body: open_test_file('component_status_list.json'), status: 200)
468
-
469
- stub_request(:get, %r{/serviceaccounts})
470
- .to_return(body: open_test_file('service_account_list.json'), status: 200)
471
-
472
- result = client.all_entities(as: :raw)
473
- assert_equal(16, result.keys.size)
474
-
475
- %w[
476
- component_status config_map endpoint event limit_range namespace node
477
- persistent_volume persistent_volume_claim pod replication_controller
478
- resource_quota secret service service_account
479
- ].each do |entity|
480
- assert_equal(open_test_file("#{entity}_list.json").read, result[entity])
481
- end
482
- end
483
-
484
- def test_api_bearer_token_with_params_success
485
- stub_request(:get, 'http://localhost:8080/api/v1/pods?labelSelector=name=redis-master')
486
- .with(headers: { Authorization: 'Bearer valid_token' })
487
- .to_return(body: open_test_file('pod_list.json'), status: 200)
488
- stub_request(:get, %r{/api/v1$})
489
- .with(headers: { Authorization: 'Bearer valid_token' })
490
- .to_return(body: open_test_file('core_api_resource_list.json'), status: 200)
491
-
492
- client = Kubeclient::Client.new(
493
- 'http://localhost:8080/api/',
494
- auth_options: { bearer_token: 'valid_token' }
495
- )
496
-
497
- pods = client.get_pods(label_selector: 'name=redis-master')
498
-
499
- assert_equal('Pod', pods.kind)
500
- assert_equal(1, pods.size)
501
- end
502
-
503
- def test_api_bearer_token_success
504
- stub_core_api_list
505
- stub_request(:get, 'http://localhost:8080/api/v1/pods')
506
- .with(headers: { Authorization: 'Bearer valid_token' })
507
- .to_return(
508
- body: open_test_file('pod_list.json'), status: 200
509
- )
510
-
511
- client = Kubeclient::Client.new(
512
- 'http://localhost:8080/api/',
513
- auth_options: { bearer_token: 'valid_token' }
514
- )
515
-
516
- pods = client.get_pods
517
-
518
- assert_equal('Pod', pods.kind)
519
- assert_equal(1, pods.size)
520
- end
521
-
522
- def test_api_bearer_token_failure
523
- error_message =
524
- '"/api/v1" is forbidden because ' \
525
- 'system:anonymous cannot list on pods in'
526
- response = OpenStruct.new(code: 401, message: error_message)
527
-
528
- stub_request(:get, 'http://localhost:8080/api/v1')
529
- .with(headers: { Authorization: 'Bearer invalid_token' })
530
- .to_raise(Kubeclient::HttpError.new(403, error_message, response))
531
-
532
- client = Kubeclient::Client.new(
533
- 'http://localhost:8080/api/',
534
- auth_options: { bearer_token: 'invalid_token' }
535
- )
536
-
537
- exception = assert_raises(Kubeclient::HttpError) { client.get_pods }
538
- assert_equal(403, exception.error_code)
539
- assert_equal(error_message, exception.message)
540
- assert_equal(response, exception.response)
541
- end
542
-
543
- def test_api_bearer_token_failure_raw
544
- error_message =
545
- '"/api/v1" is forbidden because ' \
546
- 'system:anonymous cannot list on pods in'
547
- response = OpenStruct.new(code: 401, message: error_message)
548
-
549
- stub_request(:get, 'http://localhost:8080/api/v1')
550
- .with(headers: { Authorization: 'Bearer invalid_token' })
551
- .to_raise(Kubeclient::HttpError.new(403, error_message, response))
552
-
553
- client = Kubeclient::Client.new(
554
- 'http://localhost:8080/api/',
555
- auth_options: { bearer_token: 'invalid_token' }
556
- )
557
-
558
- exception = assert_raises(Kubeclient::HttpError) { client.get_pods(as: :raw) }
559
- assert_equal(403, exception.error_code)
560
- assert_equal(error_message, exception.message)
561
- assert_equal(response, exception.response)
562
- end
563
-
564
- def test_api_basic_auth_success
565
- stub_request(:get, 'http://localhost:8080/api/v1')
566
- .with(basic_auth: %w[username password])
567
- .to_return(body: open_test_file('core_api_resource_list.json'), status: 200)
568
- stub_request(:get, 'http://localhost:8080/api/v1/pods')
569
- .with(basic_auth: %w[username password])
570
- .to_return(body: open_test_file('pod_list.json'), status: 200)
571
-
572
- client = Kubeclient::Client.new(
573
- 'http://localhost:8080/api/',
574
- auth_options: { username: 'username', password: 'password' }
575
- )
576
-
577
- pods = client.get_pods
578
-
579
- assert_equal('Pod', pods.kind)
580
- assert_equal(1, pods.size)
581
- assert_requested(
582
- :get,
583
- 'http://localhost:8080/api/v1/pods',
584
- times: 1
585
- )
586
- end
587
-
588
- def test_api_basic_auth_back_comp_success
589
- stub_request(:get, 'http://localhost:8080/api/v1')
590
- .with(basic_auth: %w[username password])
591
- .to_return(body: open_test_file('core_api_resource_list.json'), status: 200)
592
- stub_request(:get, 'http://localhost:8080/api/v1/pods')
593
- .with(basic_auth: %w[username password])
594
- .to_return(body: open_test_file('pod_list.json'), status: 200)
595
-
596
- client = Kubeclient::Client.new(
597
- 'http://localhost:8080/api/',
598
- auth_options: { user: 'username', password: 'password' }
599
- )
600
-
601
- pods = client.get_pods
602
-
603
- assert_equal('Pod', pods.kind)
604
- assert_equal(1, pods.size)
605
- assert_requested(:get, 'http://localhost:8080/api/v1/pods', times: 1)
606
- end
607
-
608
- def test_api_basic_auth_failure
609
- error_message = 'HTTP status code 401, 401 Unauthorized'
610
- response = OpenStruct.new(code: 401, message: '401 Unauthorized')
611
-
612
- stub_request(:get, 'http://localhost:8080/api/v1')
613
- .with(basic_auth: %w[username password])
614
- .to_raise(Kubeclient::HttpError.new(401, error_message, response))
615
-
616
- client = Kubeclient::Client.new(
617
- 'http://localhost:8080/api/',
618
- auth_options: { username: 'username', password: 'password' }
619
- )
620
-
621
- exception = assert_raises(Kubeclient::HttpError) { client.get_pods }
622
- assert_equal(401, exception.error_code)
623
- assert_equal(error_message, exception.message)
624
- assert_equal(response, exception.response)
625
- assert_requested(:get, 'http://localhost:8080/api/v1', times: 1)
626
- end
627
-
628
- def test_api_basic_auth_failure_raw
629
- error_message = 'HTTP status code 401, 401 Unauthorized'
630
- response = OpenStruct.new(code: 401, message: '401 Unauthorized')
631
-
632
- stub_request(:get, 'http://localhost:8080/api/v1')
633
- .with(basic_auth: %w[username password])
634
- .to_raise(Kubeclient::HttpError.new(401, error_message, response))
635
-
636
- client = Kubeclient::Client.new(
637
- 'http://localhost:8080/api/',
638
- auth_options: { username: 'username', password: 'password' }
639
- )
640
-
641
- exception = assert_raises(Kubeclient::HttpError) { client.get_pods(as: :raw) }
642
- assert_equal(401, exception.error_code)
643
- assert_equal(error_message, exception.message)
644
- assert_equal(response, exception.response)
645
-
646
- assert_requested(:get, 'http://localhost:8080/api/v1', times: 1)
647
- end
648
-
649
- def test_init_username_no_password
650
- expected_msg = 'Basic auth requires both username & password'
651
- exception = assert_raises(ArgumentError) do
652
- Kubeclient::Client.new(
653
- 'http://localhost:8080',
654
- auth_options: { username: 'username' }
655
- )
656
- end
657
- assert_equal(expected_msg, exception.message)
658
- end
659
-
660
- def test_init_user_no_password
661
- expected_msg = 'Basic auth requires both username & password'
662
- exception = assert_raises(ArgumentError) do
663
- Kubeclient::Client.new(
664
- 'http://localhost:8080',
665
- auth_options: { user: 'username' }
666
- )
667
- end
668
- assert_equal(expected_msg, exception.message)
669
- end
670
-
671
- def test_init_username_and_bearer_token
672
- expected_msg = 'Invalid auth options: specify only one of username/password,' \
673
- ' bearer_token or bearer_token_file'
674
- exception = assert_raises(ArgumentError) do
675
- Kubeclient::Client.new(
676
- 'http://localhost:8080',
677
- auth_options: { username: 'username', bearer_token: 'token' }
678
- )
679
- end
680
- assert_equal(expected_msg, exception.message)
681
- end
682
-
683
- def test_init_username_and_bearer_token_file
684
- expected_msg = 'Invalid auth options: specify only one of username/password,' \
685
- ' bearer_token or bearer_token_file'
686
- exception = assert_raises(ArgumentError) do
687
- Kubeclient::Client.new(
688
- 'http://localhost:8080',
689
- auth_options: { username: 'username', bearer_token_file: 'token-file' }
690
- )
691
- end
692
- assert_equal(expected_msg, exception.message)
693
- end
694
-
695
- def test_bearer_token_and_bearer_token_file
696
- expected_msg =
697
- 'Invalid auth options: specify only one of username/password,' \
698
- ' bearer_token or bearer_token_file'
699
- exception = assert_raises(ArgumentError) do
700
- Kubeclient::Client.new(
701
- 'http://localhost:8080',
702
- auth_options: { bearer_token: 'token', bearer_token_file: 'token-file' }
703
- )
704
- end
705
- assert_equal(expected_msg, exception.message)
706
- end
707
-
708
- def test_bearer_token_file_not_exist
709
- expected_msg = 'Token file token-file does not exist'
710
- exception = assert_raises(ArgumentError) do
711
- Kubeclient::Client.new(
712
- 'http://localhost:8080',
713
- auth_options: { bearer_token_file: 'token-file' }
714
- )
715
- end
716
- assert_equal(expected_msg, exception.message)
717
- end
718
-
719
- def test_api_bearer_token_file_success
720
- stub_core_api_list
721
- stub_request(:get, 'http://localhost:8080/api/v1/pods')
722
- .with(headers: { Authorization: 'Bearer valid_token' })
723
- .to_return(body: open_test_file('pod_list.json'), status: 200)
724
-
725
- file = File.join(File.dirname(__FILE__), 'valid_token_file')
726
- client = Kubeclient::Client.new(
727
- 'http://localhost:8080/api/',
728
- auth_options: { bearer_token_file: file }
729
- )
730
-
731
- pods = client.get_pods
732
-
733
- assert_equal('Pod', pods.kind)
734
- assert_equal(1, pods.size)
735
- end
736
-
737
- def test_proxy_url
738
- stub_core_api_list
739
-
740
- client = Kubeclient::Client.new('http://host:8080', 'v1')
741
- assert_equal(
742
- 'http://host:8080/api/v1/namespaces/ns/services/srvname:srvportname/proxy',
743
- client.proxy_url('service', 'srvname', 'srvportname', 'ns')
744
- )
745
-
746
- assert_equal(
747
- 'http://host:8080/api/v1/namespaces/ns/services/srvname:srvportname/proxy',
748
- client.proxy_url('services', 'srvname', 'srvportname', 'ns')
749
- )
750
-
751
- assert_equal(
752
- 'http://host:8080/api/v1/namespaces/ns/pods/srvname:srvportname/proxy',
753
- client.proxy_url('pod', 'srvname', 'srvportname', 'ns')
754
- )
755
-
756
- assert_equal(
757
- 'http://host:8080/api/v1/namespaces/ns/pods/srvname:srvportname/proxy',
758
- client.proxy_url('pods', 'srvname', 'srvportname', 'ns')
759
- )
760
-
761
- # Check no namespace provided
762
- assert_equal(
763
- 'http://host:8080/api/v1/nodes/srvname:srvportname/proxy',
764
- client.proxy_url('nodes', 'srvname', 'srvportname')
765
- )
766
-
767
- assert_equal(
768
- 'http://host:8080/api/v1/nodes/srvname:srvportname/proxy',
769
- client.proxy_url('node', 'srvname', 'srvportname')
770
- )
771
-
772
- # Check integer port
773
- assert_equal(
774
- 'http://host:8080/api/v1/nodes/srvname:5001/proxy',
775
- client.proxy_url('nodes', 'srvname', 5001)
776
- )
777
-
778
- assert_equal(
779
- 'http://host:8080/api/v1/nodes/srvname:5001/proxy',
780
- client.proxy_url('node', 'srvname', 5001)
781
- )
782
- end
783
-
784
- def test_attr_readers
785
- client = Kubeclient::Client.new(
786
- 'http://localhost:8080/api/',
787
- ssl_options: { client_key: 'secret' },
788
- auth_options: { bearer_token: 'token' }
789
- )
790
- assert_equal('/api', client.api_endpoint.path)
791
- assert_equal('secret', client.ssl_options[:client_key])
792
- assert_equal('token', client.auth_options[:bearer_token])
793
- assert_equal('Bearer token', client.headers[:Authorization])
794
- end
795
-
796
- def test_nil_items
797
- # handle https://github.com/kubernetes/kubernetes/issues/13096
798
- stub_core_api_list
799
- stub_request(:get, %r{/persistentvolumeclaims})
800
- .to_return(body: open_test_file('persistent_volume_claims_nil_items.json'), status: 200)
801
-
802
- client.get_persistent_volume_claims
803
- end
804
-
805
- # Timeouts
806
-
807
- def test_timeouts_defaults
808
- client = Kubeclient::Client.new(
809
- 'http://localhost:8080/api/'
810
- )
811
- rest_client = client.rest_client
812
- assert_default_open_timeout(rest_client.open_timeout)
813
- assert_equal(60, rest_client.read_timeout)
814
- end
815
-
816
- def test_timeouts_open
817
- client = Kubeclient::Client.new(
818
- 'http://localhost:8080/api/',
819
- timeouts: { open: 10 }
820
- )
821
- rest_client = client.rest_client
822
- assert_equal(10, rest_client.open_timeout)
823
- assert_equal(60, rest_client.read_timeout)
824
- end
825
-
826
- def test_timeouts_read
827
- client = Kubeclient::Client.new(
828
- 'http://localhost:8080/api/',
829
- timeouts: { read: 300 }
830
- )
831
- rest_client = client.rest_client
832
- assert_default_open_timeout(rest_client.open_timeout)
833
- assert_equal(300, rest_client.read_timeout)
834
- end
835
-
836
- def test_timeouts_both
837
- client = Kubeclient::Client.new(
838
- 'http://localhost:8080/api/',
839
- timeouts: { open: 10, read: 300 }
840
- )
841
- rest_client = client.rest_client
842
- assert_equal(10, rest_client.open_timeout)
843
- assert_equal(300, rest_client.read_timeout)
844
- end
845
-
846
- def test_timeouts_infinite
847
- client = Kubeclient::Client.new(
848
- 'http://localhost:8080/api/',
849
- timeouts: { open: nil, read: nil }
850
- )
851
- rest_client = client.rest_client
852
- assert_nil(rest_client.open_timeout)
853
- assert_nil(rest_client.read_timeout)
854
- end
855
-
856
- def assert_default_open_timeout(actual)
857
- if RUBY_VERSION >= '2.3'
858
- assert_equal(60, actual)
859
- else
860
- assert_nil(actual)
861
- end
862
- end
863
-
864
- private
865
-
866
- def stub_get_services
867
- stub_request(:get, %r{/services})
868
- .to_return(body: open_test_file('entity_list.json'), status: 200)
869
- end
870
-
871
- def client
872
- @client ||= Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
873
- end
874
-
875
- # dup method creates a shallow copy which is not good in this case
876
- # since rename_keys changes the input hash
877
- # hence need to create a deep_copy
878
- def deep_copy(hash)
879
- Marshal.load(Marshal.dump(hash))
880
- end
881
- end