ruboty-k8s 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. checksums.yaml +4 -4
  2. data/lib/ruboty/k8s/version.rb +1 -1
  3. data/ruboty-k8s.gemspec +1 -9
  4. metadata +4 -183
  5. data/lib/kubeclient/.gitignore +0 -15
  6. data/lib/kubeclient/.rubocop.yml +0 -16
  7. data/lib/kubeclient/.travis.yml +0 -12
  8. data/lib/kubeclient/Gemfile +0 -4
  9. data/lib/kubeclient/LICENSE.txt +0 -22
  10. data/lib/kubeclient/README.md +0 -428
  11. data/lib/kubeclient/Rakefile +0 -12
  12. data/lib/kubeclient/kubeclient.gemspec +0 -31
  13. data/lib/kubeclient/lib/kubeclient.rb +0 -32
  14. data/lib/kubeclient/lib/kubeclient/common.rb +0 -512
  15. data/lib/kubeclient/lib/kubeclient/config.rb +0 -126
  16. data/lib/kubeclient/lib/kubeclient/entity_list.rb +0 -16
  17. data/lib/kubeclient/lib/kubeclient/kube_exception.rb +0 -14
  18. data/lib/kubeclient/lib/kubeclient/missing_kind_compatibility.rb +0 -68
  19. data/lib/kubeclient/lib/kubeclient/version.rb +0 -4
  20. data/lib/kubeclient/lib/kubeclient/watch_notice.rb +0 -7
  21. data/lib/kubeclient/lib/kubeclient/watch_stream.rb +0 -80
  22. data/lib/kubeclient/test/cassettes/kubernetes_guestbook.yml +0 -879
  23. data/lib/kubeclient/test/config/allinone.kubeconfig +0 -20
  24. data/lib/kubeclient/test/config/external-ca.pem +0 -18
  25. data/lib/kubeclient/test/config/external-cert.pem +0 -19
  26. data/lib/kubeclient/test/config/external-key.rsa +0 -27
  27. data/lib/kubeclient/test/config/external.kubeconfig +0 -20
  28. data/lib/kubeclient/test/config/nouser.kubeconfig +0 -16
  29. data/lib/kubeclient/test/config/userauth.kubeconfig +0 -28
  30. data/lib/kubeclient/test/json/bindings_list.json +0 -10
  31. data/lib/kubeclient/test/json/component_status.json +0 -17
  32. data/lib/kubeclient/test/json/component_status_list.json +0 -52
  33. data/lib/kubeclient/test/json/config_map_list.json +0 -9
  34. data/lib/kubeclient/test/json/core_api_resource_list.json +0 -181
  35. data/lib/kubeclient/test/json/core_api_resource_list_without_kind.json +0 -129
  36. data/lib/kubeclient/test/json/core_oapi_resource_list_without_kind.json +0 -197
  37. data/lib/kubeclient/test/json/created_endpoint.json +0 -28
  38. data/lib/kubeclient/test/json/created_namespace.json +0 -20
  39. data/lib/kubeclient/test/json/created_secret.json +0 -16
  40. data/lib/kubeclient/test/json/created_service.json +0 -31
  41. data/lib/kubeclient/test/json/empty_pod_list.json +0 -9
  42. data/lib/kubeclient/test/json/endpoint_list.json +0 -48
  43. data/lib/kubeclient/test/json/entity_list.json +0 -56
  44. data/lib/kubeclient/test/json/event_list.json +0 -35
  45. data/lib/kubeclient/test/json/limit_range.json +0 -23
  46. data/lib/kubeclient/test/json/limit_range_list.json +0 -31
  47. data/lib/kubeclient/test/json/namespace.json +0 -13
  48. data/lib/kubeclient/test/json/namespace_exception.json +0 -8
  49. data/lib/kubeclient/test/json/namespace_list.json +0 -32
  50. data/lib/kubeclient/test/json/node.json +0 -29
  51. data/lib/kubeclient/test/json/node_list.json +0 -37
  52. data/lib/kubeclient/test/json/persistent_volume.json +0 -37
  53. data/lib/kubeclient/test/json/persistent_volume_claim.json +0 -32
  54. data/lib/kubeclient/test/json/persistent_volume_claim_list.json +0 -40
  55. data/lib/kubeclient/test/json/persistent_volume_claims_nil_items.json +0 -8
  56. data/lib/kubeclient/test/json/persistent_volume_list.json +0 -45
  57. data/lib/kubeclient/test/json/pod.json +0 -92
  58. data/lib/kubeclient/test/json/pod_list.json +0 -79
  59. data/lib/kubeclient/test/json/pod_template_list.json +0 -9
  60. data/lib/kubeclient/test/json/processed_template.json +0 -27
  61. data/lib/kubeclient/test/json/replication_controller.json +0 -57
  62. data/lib/kubeclient/test/json/replication_controller_list.json +0 -66
  63. data/lib/kubeclient/test/json/resource_quota.json +0 -46
  64. data/lib/kubeclient/test/json/resource_quota_list.json +0 -54
  65. data/lib/kubeclient/test/json/secret_list.json +0 -44
  66. data/lib/kubeclient/test/json/service.json +0 -33
  67. data/lib/kubeclient/test/json/service_account.json +0 -25
  68. data/lib/kubeclient/test/json/service_account_list.json +0 -82
  69. data/lib/kubeclient/test/json/service_illegal_json_404.json +0 -1
  70. data/lib/kubeclient/test/json/service_list.json +0 -97
  71. data/lib/kubeclient/test/json/service_patch.json +0 -25
  72. data/lib/kubeclient/test/json/service_update.json +0 -22
  73. data/lib/kubeclient/test/json/versions_list.json +0 -6
  74. data/lib/kubeclient/test/json/watch_stream.json +0 -3
  75. data/lib/kubeclient/test/test_common.rb +0 -32
  76. data/lib/kubeclient/test/test_component_status.rb +0 -30
  77. data/lib/kubeclient/test/test_config.rb +0 -72
  78. data/lib/kubeclient/test/test_endpoint.rb +0 -35
  79. data/lib/kubeclient/test/test_guestbook_go.rb +0 -238
  80. data/lib/kubeclient/test/test_helper.rb +0 -10
  81. data/lib/kubeclient/test/test_kubeclient.rb +0 -611
  82. data/lib/kubeclient/test/test_limit_range.rb +0 -27
  83. data/lib/kubeclient/test/test_missing_methods.rb +0 -42
  84. data/lib/kubeclient/test/test_namespace.rb +0 -61
  85. data/lib/kubeclient/test/test_node.rb +0 -33
  86. data/lib/kubeclient/test/test_persistent_volume.rb +0 -30
  87. data/lib/kubeclient/test/test_persistent_volume_claim.rb +0 -30
  88. data/lib/kubeclient/test/test_pod.rb +0 -29
  89. data/lib/kubeclient/test/test_pod_log.rb +0 -50
  90. data/lib/kubeclient/test/test_process_template.rb +0 -44
  91. data/lib/kubeclient/test/test_replication_controller.rb +0 -27
  92. data/lib/kubeclient/test/test_resource_list_without_kind.rb +0 -78
  93. data/lib/kubeclient/test/test_resource_quota.rb +0 -25
  94. data/lib/kubeclient/test/test_secret.rb +0 -70
  95. data/lib/kubeclient/test/test_service.rb +0 -293
  96. data/lib/kubeclient/test/test_service_account.rb +0 -28
  97. data/lib/kubeclient/test/test_watch.rb +0 -119
  98. data/lib/kubeclient/test/txt/pod_log.txt +0 -6
  99. data/lib/kubeclient/test/valid_token_file +0 -1
@@ -1,10 +0,0 @@
1
- require 'minitest/autorun'
2
- require 'webmock/minitest'
3
- require 'json'
4
- require 'kubeclient'
5
-
6
- # Assumes test files will be in a subdirectory with the same name as the
7
- # file suffix. e.g. a file named foo.json would be a "json" subdirectory.
8
- def open_test_file(name)
9
- File.new(File.join(File.dirname(__FILE__), name.split('.').last, name))
10
- end
@@ -1,611 +0,0 @@
1
- require '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_request(:get, %r{/api/v1$})
47
- .to_return(body: open_test_file('core_api_resource_list.json'), status: 200)
48
-
49
- client = Kubeclient::Client.new(uri, http_proxy_uri: proxy_uri)
50
- rest_client = client.rest_client
51
- assert_equal(proxy_uri.to_s, rest_client.options[:proxy])
52
-
53
- watch_client = client.watch_pods
54
- assert_equal(watch_client.send(:build_client_options)[:proxy][:proxy_address], proxy_uri.host)
55
- assert_equal(watch_client.send(:build_client_options)[:proxy][:proxy_port], proxy_uri.port)
56
- end
57
-
58
- def test_exception
59
- stub_request(:get, %r{/api/v1$})
60
- .to_return(body: open_test_file('core_api_resource_list.json'), status: 200)
61
- stub_request(:post, %r{/services})
62
- .to_return(body: open_test_file('namespace_exception.json'), status: 409)
63
-
64
- service = Kubeclient::Resource.new
65
- service.metadata = {}
66
- service.metadata.name = 'redisslave'
67
- service.metadata.namespace = 'default'
68
- # service.port = 80
69
- # service.container_port = 6379
70
- # service.protocol = 'TCP'
71
-
72
- client = Kubeclient::Client.new('http://localhost:8080/api/')
73
-
74
- exception = assert_raises(KubeException) do
75
- service = client.create_service service
76
- end
77
-
78
- assert_instance_of(KubeException, exception)
79
- assert_equal("converting to : type names don't match (Pod, Namespace)",
80
- exception.message)
81
- assert_equal(409, exception.error_code)
82
- end
83
-
84
- def test_api
85
- stub_request(:get, 'http://localhost:8080/api')
86
- .to_return(status: 200, body: open_test_file('versions_list.json'))
87
-
88
- client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
89
- response = client.api
90
- assert_includes(response, 'versions')
91
- end
92
-
93
- def test_api_ssl_failure
94
- error_message = 'certificate verify failed'
95
-
96
- stub_request(:get, 'http://localhost:8080/api')
97
- .to_raise(OpenSSL::SSL::SSLError.new(error_message))
98
-
99
- client = Kubeclient::Client.new('http://localhost:8080/api/')
100
-
101
- exception = assert_raises(KubeException) { client.api }
102
- assert_equal(error_message, exception.message)
103
- end
104
-
105
- def test_api_valid
106
- stub_request(:get, 'http://localhost:8080/api')
107
- .to_return(status: 200, body: open_test_file('versions_list.json'))
108
-
109
- args = ['http://localhost:8080/api/']
110
-
111
- [nil, 'v1beta3', 'v1'].each do |version|
112
- client = Kubeclient::Client.new(*(version ? args + [version] : args))
113
- assert client.api_valid?
114
- end
115
- end
116
-
117
- def test_api_valid_with_invalid_version
118
- stub_request(:get, 'http://localhost:8080/api')
119
- .to_return(status: 200, body: open_test_file('versions_list.json'))
120
-
121
- client = Kubeclient::Client.new('http://localhost:8080/api/', 'foobar1')
122
- refute client.api_valid?
123
- end
124
-
125
- def test_api_valid_with_unreported_versions
126
- stub_request(:get, 'http://localhost:8080/api')
127
- .to_return(status: 200, body: '{}')
128
-
129
- client = Kubeclient::Client.new('http://localhost:8080/api/')
130
- refute client.api_valid?
131
- end
132
-
133
- def test_api_valid_with_invalid_json
134
- stub_request(:get, 'http://localhost:8080/api')
135
- .to_return(status: 200, body: '[]')
136
-
137
- client = Kubeclient::Client.new('http://localhost:8080/api/')
138
- refute client.api_valid?
139
- end
140
-
141
- def test_api_valid_with_bad_endpoint
142
- stub_request(:get, 'http://localhost:8080/api')
143
- .to_return(status: [404, 'Resource Not Found'])
144
-
145
- client = Kubeclient::Client.new('http://localhost:8080/api/')
146
- assert_raises(KubeException) { client.api_valid? }
147
- end
148
-
149
- def test_api_valid_with_non_json
150
- stub_request(:get, 'http://localhost:8080/api')
151
- .to_return(status: 200, body: '<html></html>')
152
-
153
- client = Kubeclient::Client.new('http://localhost:8080/api/')
154
- assert_raises(JSON::ParserError) { client.api_valid? }
155
- end
156
-
157
- def test_nonjson_exception
158
- stub_request(:get, %r{/api/v1$})
159
- .to_return(body: open_test_file('core_api_resource_list.json'), status: 200)
160
- stub_request(:get, %r{/servic})
161
- .to_return(body: open_test_file('service_illegal_json_404.json'), status: 404)
162
-
163
- client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
164
-
165
- exception = assert_raises(KubeException) do
166
- client.get_services
167
- end
168
-
169
- assert_instance_of(KubeException, exception)
170
- assert(exception.message.include?('Not Found'))
171
- assert_equal(404, exception.error_code)
172
- end
173
-
174
- def test_entity_list
175
- stub_request(:get, %r{/api/v1$})
176
- .to_return(body: open_test_file('core_api_resource_list.json'), status: 200)
177
- stub_request(:get, %r{/services})
178
- .to_return(body: open_test_file('entity_list.json'), status: 200)
179
-
180
- client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
181
- services = client.get_services
182
-
183
- refute_empty(services)
184
- assert_instance_of(Kubeclient::Common::EntityList, services)
185
- assert_equal('Service', services.kind)
186
- assert_equal(2, services.size)
187
- assert_instance_of(Kubeclient::Service, services[0])
188
- assert_instance_of(Kubeclient::Service, services[1])
189
-
190
- assert_requested(:get, 'http://localhost:8080/api/v1/services', times: 1)
191
- end
192
-
193
- def test_entities_with_label_selector
194
- selector = 'component=apiserver'
195
-
196
- stub_request(:get, %r{/api/v1$})
197
- .to_return(body: open_test_file('core_api_resource_list.json'), status: 200)
198
- stub_request(:get, %r{/services})
199
- .to_return(body: open_test_file('entity_list.json'), status: 200)
200
-
201
- client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
202
- services = client.get_services(label_selector: selector)
203
-
204
- assert_instance_of(Kubeclient::Common::EntityList, services)
205
- assert_requested(
206
- :get,
207
- "http://localhost:8080/api/v1/services?labelSelector=#{selector}",
208
- times: 1
209
- )
210
- end
211
-
212
- def test_entities_with_field_selector
213
- selector = 'involvedObject.name=redis-master'
214
-
215
- stub_request(:get, %r{/api/v1$})
216
- .to_return(body: open_test_file('core_api_resource_list.json'), status: 200)
217
- stub_request(:get, %r{/services})
218
- .to_return(body: open_test_file('entity_list.json'), status: 200)
219
-
220
- client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
221
- services = client.get_services(field_selector: selector)
222
-
223
- assert_instance_of(Kubeclient::Common::EntityList, services)
224
- assert_requested(
225
- :get,
226
- "http://localhost:8080/api/v1/services?fieldSelector=#{selector}",
227
- times: 1
228
- )
229
- end
230
-
231
- def test_empty_list
232
- stub_request(:get, %r{/api/v1$})
233
- .to_return(body: open_test_file('core_api_resource_list.json'), status: 200)
234
- stub_request(:get, %r{/pods})
235
- .to_return(body: open_test_file('empty_pod_list.json'), status: 200)
236
-
237
- client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
238
- pods = client.get_pods
239
- assert_instance_of(Kubeclient::Common::EntityList, pods)
240
- assert_equal(0, pods.size)
241
- end
242
-
243
- def test_get_all
244
- stub_request(:get, %r{/api/v1$})
245
- .to_return(body: open_test_file('core_api_resource_list.json'), status: 200)
246
-
247
- stub_request(:get, %r{/bindings})
248
- .to_return(body: open_test_file('bindings_list.json'), status: 404)
249
-
250
- stub_request(:get, %r{/configmaps})
251
- .to_return(body: open_test_file('config_map_list.json'), status: 200)
252
-
253
- stub_request(:get, %r{/podtemplates})
254
- .to_return(body: open_test_file('pod_template_list.json'), status: 200)
255
-
256
- stub_request(:get, %r{/services})
257
- .to_return(body: open_test_file('service_list.json'), status: 200)
258
-
259
- stub_request(:get, %r{/pods})
260
- .to_return(body: open_test_file('pod_list.json'), status: 200)
261
-
262
- stub_request(:get, %r{/nodes})
263
- .to_return(body: open_test_file('node_list.json'), status: 200)
264
-
265
- stub_request(:get, %r{/replicationcontrollers})
266
- .to_return(body: open_test_file('replication_controller_list.json'), status: 200)
267
-
268
- stub_request(:get, %r{/events})
269
- .to_return(body: open_test_file('event_list.json'), status: 200)
270
-
271
- stub_request(:get, %r{/endpoints})
272
- .to_return(body: open_test_file('endpoint_list.json'), status: 200)
273
-
274
- stub_request(:get, %r{/namespaces})
275
- .to_return(body: open_test_file('namespace_list.json'), status: 200)
276
-
277
- stub_request(:get, %r{/secrets})
278
- .to_return(body: open_test_file('secret_list.json'), status: 200)
279
-
280
- stub_request(:get, %r{/resourcequotas})
281
- .to_return(body: open_test_file('resource_quota_list.json'), status: 200)
282
-
283
- stub_request(:get, %r{/limitranges})
284
- .to_return(body: open_test_file('limit_range_list.json'), status: 200)
285
-
286
- stub_request(:get, %r{/persistentvolumes})
287
- .to_return(body: open_test_file('persistent_volume_list.json'), status: 200)
288
-
289
- stub_request(:get, %r{/persistentvolumeclaims})
290
- .to_return(body: open_test_file('persistent_volume_claim_list.json'), status: 200)
291
-
292
- stub_request(:get, %r{/componentstatuses})
293
- .to_return(body: open_test_file('component_status_list.json'), status: 200)
294
-
295
- stub_request(:get, %r{/serviceaccounts})
296
- .to_return(body: open_test_file('service_account_list.json'), status: 200)
297
-
298
- client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
299
- result = client.all_entities
300
- assert_equal(16, result.keys.size)
301
- assert_instance_of(Kubeclient::Common::EntityList, result['node'])
302
- assert_instance_of(Kubeclient::Common::EntityList, result['service'])
303
- assert_instance_of(Kubeclient::Common::EntityList, result['replication_controller'])
304
- assert_instance_of(Kubeclient::Common::EntityList, result['pod'])
305
- assert_instance_of(Kubeclient::Common::EntityList, result['event'])
306
- assert_instance_of(Kubeclient::Common::EntityList, result['namespace'])
307
- assert_instance_of(Kubeclient::Common::EntityList, result['secret'])
308
- assert_instance_of(Kubeclient::Service, result['service'][0])
309
- assert_instance_of(Kubeclient::Node, result['node'][0])
310
- assert_instance_of(Kubeclient::Event, result['event'][0])
311
- assert_instance_of(Kubeclient::Endpoint, result['endpoint'][0])
312
- assert_instance_of(Kubeclient::Namespace, result['namespace'][0])
313
- assert_instance_of(Kubeclient::Secret, result['secret'][0])
314
- assert_instance_of(Kubeclient::ResourceQuota, result['resource_quota'][0])
315
- assert_instance_of(Kubeclient::LimitRange, result['limit_range'][0])
316
- assert_instance_of(Kubeclient::PersistentVolume, result['persistent_volume'][0])
317
- assert_instance_of(Kubeclient::PersistentVolumeClaim, result['persistent_volume_claim'][0])
318
- assert_instance_of(Kubeclient::ComponentStatus, result['component_status'][0])
319
- assert_instance_of(Kubeclient::ServiceAccount, result['service_account'][0])
320
- end
321
-
322
- def test_api_bearer_token_with_params_success
323
- stub_request(:get, 'http://localhost:8080/api/v1/pods?labelSelector=name=redis-master')
324
- .with(headers: { Authorization: 'Bearer valid_token' })
325
- .to_return(body: open_test_file('pod_list.json'), status: 200)
326
- stub_request(:get, %r{/api/v1$})
327
- .with(headers: { Authorization: 'Bearer valid_token' })
328
- .to_return(body: open_test_file('core_api_resource_list.json'), status: 200)
329
-
330
- client = Kubeclient::Client.new(
331
- 'http://localhost:8080/api/',
332
- auth_options: { bearer_token: 'valid_token' }
333
- )
334
-
335
- pods = client.get_pods(label_selector: 'name=redis-master')
336
-
337
- assert_equal('Pod', pods.kind)
338
- assert_equal(1, pods.size)
339
- end
340
-
341
- def test_api_bearer_token_success
342
- stub_request(:get, %r{/api/v1$})
343
- .to_return(
344
- body: open_test_file('core_api_resource_list.json'), status: 200
345
- )
346
- stub_request(:get, 'http://localhost:8080/api/v1/pods')
347
- .with(headers: { Authorization: 'Bearer valid_token' })
348
- .to_return(
349
- body: open_test_file('pod_list.json'), status: 200
350
- )
351
-
352
- client = Kubeclient::Client.new(
353
- 'http://localhost:8080/api/',
354
- auth_options: { bearer_token: 'valid_token' }
355
- )
356
-
357
- pods = client.get_pods
358
-
359
- assert_equal('Pod', pods.kind)
360
- assert_equal(1, pods.size)
361
- end
362
-
363
- def test_api_bearer_token_failure
364
- error_message =
365
- '"/api/v1" is forbidden because ' \
366
- 'system:anonymous cannot list on pods in'
367
- response = OpenStruct.new(code: 401, message: error_message)
368
-
369
- stub_request(:get, 'http://localhost:8080/api/v1')
370
- .with(headers: { Authorization: 'Bearer invalid_token' })
371
- .to_raise(KubeException.new(403, error_message, response))
372
-
373
- client = Kubeclient::Client.new(
374
- 'http://localhost:8080/api/',
375
- auth_options: { bearer_token: 'invalid_token' }
376
- )
377
-
378
- exception = assert_raises(KubeException) { client.get_pods }
379
- assert_equal(403, exception.error_code)
380
- assert_equal(error_message, exception.message)
381
- assert_equal(response, exception.response)
382
- end
383
-
384
- def test_api_basic_auth_success
385
- stub_request(:get, 'http://username:password@localhost:8080/api/v1')
386
- .to_return(body: open_test_file('core_api_resource_list.json'), status: 200)
387
- stub_request(:get, 'http://username:password@localhost:8080/api/v1/pods')
388
- .to_return(body: open_test_file('pod_list.json'), status: 200)
389
-
390
- client = Kubeclient::Client.new(
391
- 'http://localhost:8080/api/',
392
- auth_options: { username: 'username', password: 'password' }
393
- )
394
-
395
- pods = client.get_pods
396
-
397
- assert_equal('Pod', pods.kind)
398
- assert_equal(1, pods.size)
399
- assert_requested(
400
- :get,
401
- 'http://username:password@localhost:8080/api/v1/pods',
402
- times: 1
403
- )
404
- end
405
-
406
- def test_api_basic_auth_back_comp_success
407
- stub_request(:get, 'http://username:password@localhost:8080/api/v1')
408
- .to_return(body: open_test_file('core_api_resource_list.json'), status: 200)
409
- stub_request(:get, 'http://username:password@localhost:8080/api/v1/pods')
410
- .to_return(body: open_test_file('pod_list.json'), status: 200)
411
-
412
- client = Kubeclient::Client.new(
413
- 'http://localhost:8080/api/',
414
- auth_options: { user: 'username', password: 'password' }
415
- )
416
-
417
- pods = client.get_pods
418
-
419
- assert_equal('Pod', pods.kind)
420
- assert_equal(1, pods.size)
421
- assert_requested(:get, 'http://username:password@localhost:8080/api/v1/pods', times: 1)
422
- end
423
-
424
- def test_api_basic_auth_failure
425
- error_message = 'HTTP status code 401, 401 Unauthorized'
426
- response = OpenStruct.new(code: 401, message: '401 Unauthorized')
427
-
428
- stub_request(:get, 'http://username:password@localhost:8080/api/v1')
429
- .to_raise(KubeException.new(401, error_message, response))
430
-
431
- client = Kubeclient::Client.new(
432
- 'http://localhost:8080/api/',
433
- auth_options: { username: 'username', password: 'password' }
434
- )
435
-
436
- exception = assert_raises(KubeException) { client.get_pods }
437
- assert_equal(401, exception.error_code)
438
- assert_equal(error_message, exception.message)
439
- assert_equal(response, exception.response)
440
- assert_requested(:get, 'http://username:password@localhost:8080/api/v1', times: 1)
441
- end
442
-
443
- def test_init_username_no_password
444
- expected_msg = 'Basic auth requires both username & password'
445
- exception = assert_raises(ArgumentError) do
446
- Kubeclient::Client.new(
447
- 'http://localhost:8080',
448
- auth_options: { username: 'username' }
449
- )
450
- end
451
- assert_equal(expected_msg, exception.message)
452
- end
453
-
454
- def test_init_user_no_password
455
- expected_msg = 'Basic auth requires both username & password'
456
- exception = assert_raises(ArgumentError) do
457
- Kubeclient::Client.new(
458
- 'http://localhost:8080',
459
- auth_options: { user: 'username' }
460
- )
461
- end
462
- assert_equal(expected_msg, exception.message)
463
- end
464
-
465
- def test_init_username_and_bearer_token
466
- expected_msg = 'Invalid auth options: specify only one of username/password,' \
467
- ' bearer_token or bearer_token_file'
468
- exception = assert_raises(ArgumentError) do
469
- Kubeclient::Client.new(
470
- 'http://localhost:8080',
471
- auth_options: { username: 'username', bearer_token: 'token' }
472
- )
473
- end
474
- assert_equal(expected_msg, exception.message)
475
- end
476
-
477
- def test_init_user_and_bearer_token
478
- expected_msg = 'Invalid auth options: specify only one of username/password,' \
479
- ' bearer_token or bearer_token_file'
480
- exception = assert_raises(ArgumentError) do
481
- Kubeclient::Client.new(
482
- 'http://localhost:8080',
483
- auth_options: { username: 'username', bearer_token: 'token' }
484
- )
485
- end
486
- assert_equal(expected_msg, exception.message)
487
- end
488
-
489
- def test_bearer_token_and_bearer_token_file
490
- expected_msg =
491
- 'Invalid auth options: specify only one of username/password,' \
492
- ' bearer_token or bearer_token_file'
493
- exception = assert_raises(ArgumentError) do
494
- Kubeclient::Client.new(
495
- 'http://localhost:8080',
496
- auth_options: { bearer_token: 'token', bearer_token_file: 'token-file' }
497
- )
498
- end
499
- assert_equal(expected_msg, exception.message)
500
- end
501
-
502
- def test_bearer_token_file_not_exist
503
- expected_msg = 'Token file token-file does not exist'
504
- exception = assert_raises(ArgumentError) do
505
- Kubeclient::Client.new(
506
- 'http://localhost:8080',
507
- auth_options: { bearer_token_file: 'token-file' }
508
- )
509
- end
510
- assert_equal(expected_msg, exception.message)
511
- end
512
-
513
- def test_api_bearer_token_file_success
514
- stub_request(:get, %r{/api/v1$})
515
- .to_return(body: open_test_file('core_api_resource_list.json'), status: 200)
516
- stub_request(:get, 'http://localhost:8080/api/v1/pods')
517
- .with(headers: { Authorization: 'Bearer valid_token' })
518
- .to_return(body: open_test_file('pod_list.json'), status: 200)
519
-
520
- file = File.join(File.dirname(__FILE__), 'valid_token_file')
521
- client = Kubeclient::Client.new(
522
- 'http://localhost:8080/api/',
523
- auth_options: { bearer_token_file: file }
524
- )
525
-
526
- pods = client.get_pods
527
-
528
- assert_equal('Pod', pods.kind)
529
- assert_equal(1, pods.size)
530
- end
531
-
532
- def test_proxy_url
533
- stub_request(:get, %r{/api/v1$})
534
- .to_return(body: open_test_file('core_api_resource_list.json'), status: 200)
535
-
536
- client = Kubeclient::Client.new('http://host:8080', 'v1')
537
- assert_equal(
538
- 'http://host:8080/api/v1/proxy/namespaces/ns/services/srvname:srvportname',
539
- client.proxy_url('service', 'srvname', 'srvportname', 'ns')
540
- )
541
-
542
- assert_equal(
543
- 'http://host:8080/api/v1/proxy/namespaces/ns/services/srvname:srvportname',
544
- client.proxy_url('services', 'srvname', 'srvportname', 'ns')
545
- )
546
-
547
- assert_equal(
548
- 'http://host:8080/api/v1/namespaces/ns/pods/srvname:srvportname/proxy',
549
- client.proxy_url('pod', 'srvname', 'srvportname', 'ns')
550
- )
551
-
552
- assert_equal(
553
- 'http://host:8080/api/v1/namespaces/ns/pods/srvname:srvportname/proxy',
554
- client.proxy_url('pods', 'srvname', 'srvportname', 'ns')
555
- )
556
-
557
- # Check no namespace provided
558
- assert_equal(
559
- 'http://host:8080/api/v1/proxy/nodes/srvname:srvportname',
560
- client.proxy_url('nodes', 'srvname', 'srvportname')
561
- )
562
-
563
- assert_equal(
564
- 'http://host:8080/api/v1/proxy/nodes/srvname:srvportname',
565
- client.proxy_url('node', 'srvname', 'srvportname')
566
- )
567
-
568
- # Check integer port
569
- assert_equal(
570
- 'http://host:8080/api/v1/proxy/nodes/srvname:5001',
571
- client.proxy_url('nodes', 'srvname', 5001)
572
- )
573
-
574
- assert_equal(
575
- 'http://host:8080/api/v1/proxy/nodes/srvname:5001',
576
- client.proxy_url('node', 'srvname', 5001)
577
- )
578
- end
579
-
580
- def test_attr_readers
581
- client = Kubeclient::Client.new(
582
- 'http://localhost:8080/api/',
583
- ssl_options: { client_key: 'secret' },
584
- auth_options: { bearer_token: 'token' }
585
- )
586
- assert_equal('/api', client.api_endpoint.path)
587
- assert_equal('secret', client.ssl_options[:client_key])
588
- assert_equal('token', client.auth_options[:bearer_token])
589
- assert_equal('Bearer token', client.headers[:Authorization])
590
- end
591
-
592
- def test_nil_items
593
- # handle https://github.com/kubernetes/kubernetes/issues/13096
594
- stub_request(:get, %r{/api/v1$})
595
- .to_return(body: open_test_file('core_api_resource_list.json'), status: 200)
596
- stub_request(:get, %r{/persistentvolumeclaims})
597
- .to_return(body: open_test_file('persistent_volume_claims_nil_items.json'), status: 200)
598
-
599
- client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
600
- client.get_persistent_volume_claims
601
- end
602
-
603
- private
604
-
605
- # dup method creates a shallow copy which is not good in this case
606
- # since rename_keys changes the input hash
607
- # hence need to create a deep_copy
608
- def deep_copy(hash)
609
- Marshal.load(Marshal.dump(hash))
610
- end
611
- end