kubeclient 4.6.0 → 4.9.2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of kubeclient might be problematic. Click here for more details.

Files changed (116) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/actions.yml +35 -0
  3. data/CHANGELOG.md +37 -0
  4. data/README.md +155 -37
  5. data/RELEASING.md +3 -1
  6. data/kubeclient.gemspec +5 -4
  7. data/lib/kubeclient.rb +1 -1
  8. data/lib/kubeclient/common.rb +35 -6
  9. data/lib/kubeclient/config.rb +17 -4
  10. data/lib/kubeclient/exec_credentials.rb +33 -4
  11. data/lib/kubeclient/google_application_default_credentials.rb +6 -1
  12. data/lib/kubeclient/version.rb +1 -1
  13. metadata +13 -217
  14. data/.travis.yml +0 -26
  15. data/test/cassettes/kubernetes_guestbook.yml +0 -879
  16. data/test/config/allinone.kubeconfig +0 -20
  17. data/test/config/execauth.kubeconfig +0 -62
  18. data/test/config/external-ca.pem +0 -18
  19. data/test/config/external-cert.pem +0 -19
  20. data/test/config/external-key.rsa +0 -27
  21. data/test/config/external.kubeconfig +0 -20
  22. data/test/config/gcpauth.kubeconfig +0 -22
  23. data/test/config/gcpcmdauth.kubeconfig +0 -26
  24. data/test/config/nouser.kubeconfig +0 -16
  25. data/test/config/oidcauth.kubeconfig +0 -25
  26. data/test/config/timestamps.kubeconfig +0 -25
  27. data/test/config/userauth.kubeconfig +0 -28
  28. data/test/json/bindings_list.json +0 -10
  29. data/test/json/component_status.json +0 -17
  30. data/test/json/component_status_list.json +0 -52
  31. data/test/json/config.istio.io_api_resource_list.json +0 -679
  32. data/test/json/config_map_list.json +0 -9
  33. data/test/json/core_api_resource_list.json +0 -181
  34. data/test/json/core_api_resource_list_without_kind.json +0 -129
  35. data/test/json/core_oapi_resource_list_without_kind.json +0 -197
  36. data/test/json/created_endpoint.json +0 -28
  37. data/test/json/created_namespace.json +0 -20
  38. data/test/json/created_secret.json +0 -16
  39. data/test/json/created_security_context_constraint.json +0 -65
  40. data/test/json/created_service.json +0 -31
  41. data/test/json/empty_pod_list.json +0 -9
  42. data/test/json/endpoint_list.json +0 -48
  43. data/test/json/entity_list.json +0 -56
  44. data/test/json/event_list.json +0 -35
  45. data/test/json/extensions_v1beta1_api_resource_list.json +0 -217
  46. data/test/json/limit_range.json +0 -23
  47. data/test/json/limit_range_list.json +0 -31
  48. data/test/json/namespace.json +0 -13
  49. data/test/json/namespace_exception.json +0 -8
  50. data/test/json/namespace_list.json +0 -32
  51. data/test/json/node.json +0 -29
  52. data/test/json/node_list.json +0 -37
  53. data/test/json/node_notice.json +0 -160
  54. data/test/json/persistent_volume.json +0 -37
  55. data/test/json/persistent_volume_claim.json +0 -32
  56. data/test/json/persistent_volume_claim_list.json +0 -40
  57. data/test/json/persistent_volume_claims_nil_items.json +0 -8
  58. data/test/json/persistent_volume_list.json +0 -45
  59. data/test/json/pod.json +0 -92
  60. data/test/json/pod_list.json +0 -79
  61. data/test/json/pod_template_list.json +0 -9
  62. data/test/json/pods_1.json +0 -265
  63. data/test/json/pods_2.json +0 -102
  64. data/test/json/pods_410.json +0 -9
  65. data/test/json/processed_template.json +0 -27
  66. data/test/json/replication_controller.json +0 -57
  67. data/test/json/replication_controller_list.json +0 -66
  68. data/test/json/resource_quota.json +0 -46
  69. data/test/json/resource_quota_list.json +0 -54
  70. data/test/json/secret_list.json +0 -44
  71. data/test/json/security.openshift.io_api_resource_list.json +0 -69
  72. data/test/json/security_context_constraint_list.json +0 -375
  73. data/test/json/service.json +0 -33
  74. data/test/json/service_account.json +0 -25
  75. data/test/json/service_account_list.json +0 -82
  76. data/test/json/service_illegal_json_404.json +0 -1
  77. data/test/json/service_json_patch.json +0 -26
  78. data/test/json/service_list.json +0 -97
  79. data/test/json/service_merge_patch.json +0 -26
  80. data/test/json/service_patch.json +0 -25
  81. data/test/json/service_update.json +0 -22
  82. data/test/json/template.json +0 -27
  83. data/test/json/template.openshift.io_api_resource_list.json +0 -75
  84. data/test/json/template_list.json +0 -35
  85. data/test/json/versions_list.json +0 -6
  86. data/test/json/watch_stream.json +0 -3
  87. data/test/test_common.rb +0 -95
  88. data/test/test_component_status.rb +0 -29
  89. data/test/test_config.rb +0 -222
  90. data/test/test_endpoint.rb +0 -54
  91. data/test/test_exec_credentials.rb +0 -125
  92. data/test/test_gcp_command_credentials.rb +0 -27
  93. data/test/test_google_application_default_credentials.rb +0 -15
  94. data/test/test_guestbook_go.rb +0 -235
  95. data/test/test_helper.rb +0 -18
  96. data/test/test_kubeclient.rb +0 -881
  97. data/test/test_limit_range.rb +0 -25
  98. data/test/test_missing_methods.rb +0 -80
  99. data/test/test_namespace.rb +0 -59
  100. data/test/test_node.rb +0 -70
  101. data/test/test_oidc_auth_provider.rb +0 -103
  102. data/test/test_persistent_volume.rb +0 -29
  103. data/test/test_persistent_volume_claim.rb +0 -28
  104. data/test/test_pod.rb +0 -81
  105. data/test/test_pod_log.rb +0 -157
  106. data/test/test_process_template.rb +0 -80
  107. data/test/test_replication_controller.rb +0 -47
  108. data/test/test_resource_list_without_kind.rb +0 -78
  109. data/test/test_resource_quota.rb +0 -23
  110. data/test/test_secret.rb +0 -62
  111. data/test/test_security_context_constraint.rb +0 -62
  112. data/test/test_service.rb +0 -330
  113. data/test/test_service_account.rb +0 -26
  114. data/test/test_watch.rb +0 -195
  115. data/test/txt/pod_log.txt +0 -6
  116. data/test/valid_token_file +0 -1
@@ -1,62 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- # kind: 'SecurityContextConstraints' entity tests.
4
- # This is one of the unusual `kind`s that are already plural (https://github.com/kubernetes/kubernetes/issues/8115).
5
- # We force singular in method names like 'create_endpoint',
6
- # but `kind` should remain plural as in kubernetes.
7
- class TestSecurityContextConstraints < MiniTest::Test
8
- def test_create_security_context_constraint
9
- stub_request(:get, %r{/apis/security.openshift.io/v1$}).to_return(
10
- body: open_test_file('security.openshift.io_api_resource_list.json'),
11
- status: 200
12
- )
13
-
14
- testing_scc = Kubeclient::Resource.new(
15
- metadata: {
16
- name: 'teleportation'
17
- },
18
- runAsUser: {
19
- type: 'MustRunAs'
20
- },
21
- seLinuxContext: {
22
- type: 'MustRunAs'
23
- }
24
- )
25
- req_body = '{"metadata":{"name":"teleportation"},"runAsUser":{"type":"MustRunAs"},' \
26
- '"seLinuxContext":{"type":"MustRunAs"},' \
27
- '"kind":"SecurityContextConstraints","apiVersion":"security.openshift.io/v1"}'
28
-
29
- stub_request(:post, 'http://localhost:8080/apis/security.openshift.io/v1/securitycontextconstraints')
30
- .with(body: req_body)
31
- .to_return(body: open_test_file('created_security_context_constraint.json'), status: 201)
32
-
33
- client = Kubeclient::Client.new('http://localhost:8080/apis/security.openshift.io', 'v1')
34
- created_scc = client.create_security_context_constraint(testing_scc)
35
- assert_equal('SecurityContextConstraints', created_scc.kind)
36
- assert_equal('security.openshift.io/v1', created_scc.apiVersion)
37
-
38
- client = Kubeclient::Client.new('http://localhost:8080/apis/security.openshift.io', 'v1',
39
- as: :parsed_symbolized)
40
- created_scc = client.create_security_context_constraint(testing_scc)
41
- assert_equal('SecurityContextConstraints', created_scc[:kind])
42
- assert_equal('security.openshift.io/v1', created_scc[:apiVersion])
43
- end
44
-
45
- def test_get_security_context_constraints
46
- stub_request(:get, %r{/apis/security.openshift.io/v1$}).to_return(
47
- body: open_test_file('security.openshift.io_api_resource_list.json'),
48
- status: 200
49
- )
50
- stub_request(:get, %r{/securitycontextconstraints})
51
- .to_return(body: open_test_file('security_context_constraint_list.json'), status: 200)
52
- client = Kubeclient::Client.new('http://localhost:8080/apis/security.openshift.io', 'v1')
53
-
54
- collection = client.get_security_context_constraints(as: :parsed_symbolized)
55
- assert_equal('SecurityContextConstraintsList', collection[:kind])
56
- assert_equal('security.openshift.io/v1', collection[:apiVersion])
57
-
58
- # Stripping of 'List' in collection.kind RecursiveOpenStruct mode only is historic.
59
- collection = client.get_security_context_constraints
60
- assert_equal('SecurityContextConstraints', collection.kind)
61
- end
62
- end
data/test/test_service.rb DELETED
@@ -1,330 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- # Service entity tests
4
- class TestService < MiniTest::Test
5
- def test_construct_our_own_service
6
- our_service = Kubeclient::Resource.new
7
- our_service.metadata = {}
8
- our_service.metadata.name = 'guestbook'
9
- our_service.metadata.namespace = 'staging'
10
- our_service.metadata.labels = {}
11
- our_service.metadata.labels.name = 'guestbook'
12
-
13
- our_service.spec = {}
14
- our_service.spec.ports = [{
15
- 'port' => 3000,
16
- 'targetPort' => 'http-server',
17
- 'protocol' => 'TCP'
18
- }]
19
-
20
- assert_equal('guestbook', our_service.metadata.labels.name)
21
-
22
- hash = our_service.to_h
23
-
24
- assert_equal(our_service.metadata.labels.name,
25
- hash[:metadata][:labels][:name])
26
-
27
- expected_url = 'http://localhost:8080/api/v1/namespaces/staging/services'
28
- stub_core_api_list
29
- stub_request(:post, expected_url)
30
- .to_return(body: open_test_file('created_service.json'), status: 201)
31
-
32
- client = Kubeclient::Client.new('http://localhost:8080/api/')
33
- created = client.create_service(our_service)
34
-
35
- assert_instance_of(Kubeclient::Resource, created)
36
- assert_equal(created.metadata.name, our_service.metadata.name)
37
- assert_equal(created.spec.ports.size, our_service.spec.ports.size)
38
-
39
- # Check that original entity_config is not modified by kind/apiVersion patches:
40
- assert_nil(our_service.kind)
41
-
42
- assert_requested(:post, expected_url, times: 1) do |req|
43
- data = JSON.parse(req.body)
44
- data['kind'] == 'Service' &&
45
- data['apiVersion'] == 'v1' &&
46
- data['metadata']['name'] == 'guestbook' &&
47
- data['metadata']['namespace'] == 'staging'
48
- end
49
- end
50
-
51
- def test_construct_service_from_symbol_keys
52
- service = Kubeclient::Resource.new
53
- service.metadata = {
54
- labels: { tier: 'frontend' },
55
- name: 'test-service',
56
- namespace: 'staging'
57
- }
58
- service.spec = {
59
- ports: [{
60
- port: 3000,
61
- targetPort: 'http-server',
62
- protocol: 'TCP'
63
- }]
64
- }
65
-
66
- expected_url = 'http://localhost:8080/api/v1/namespaces/staging/services'
67
- stub_core_api_list
68
- stub_request(:post, expected_url)
69
- .to_return(body: open_test_file('created_service.json'), status: 201)
70
-
71
- client = Kubeclient::Client.new('http://localhost:8080/api/')
72
- client.create_service(service)
73
-
74
- assert_requested(:post, expected_url, times: 1) do |req|
75
- data = JSON.parse(req.body)
76
- data['kind'] == 'Service' &&
77
- data['apiVersion'] == 'v1' &&
78
- data['metadata']['name'] == 'test-service' &&
79
- data['metadata']['labels']['tier'] == 'frontend' &&
80
- data['metadata']['namespace'] == 'staging'
81
- end
82
- end
83
-
84
- def test_construct_service_from_string_keys
85
- service = Kubeclient::Resource.new
86
- service.metadata = {
87
- 'labels' => { 'tier' => 'frontend' },
88
- 'name' => 'test-service',
89
- 'namespace' => 'staging'
90
- }
91
- service.spec = {
92
- 'ports' => [{
93
- 'port' => 3000,
94
- 'targetPort' => 'http-server',
95
- 'protocol' => 'TCP'
96
- }]
97
- }
98
-
99
- stub_core_api_list
100
- expected_url = 'http://localhost:8080/api/v1/namespaces/staging/services'
101
- stub_request(:post, %r{namespaces/staging/services})
102
- .to_return(body: open_test_file('created_service.json'), status: 201)
103
-
104
- client = Kubeclient::Client.new('http://localhost:8080/api/')
105
- client.create_service(service)
106
-
107
- assert_requested(:post, expected_url, times: 1) do |req|
108
- data = JSON.parse(req.body)
109
- data['kind'] == 'Service' &&
110
- data['apiVersion'] == 'v1' &&
111
- data['metadata']['name'] == 'test-service' &&
112
- data['metadata']['labels']['tier'] == 'frontend' &&
113
- data['metadata']['namespace'] == 'staging'
114
- end
115
- end
116
-
117
- def test_conversion_from_json_v1
118
- stub_core_api_list
119
- stub_request(:get, %r{/services})
120
- .to_return(body: open_test_file('service.json'),
121
- status: 200)
122
-
123
- client = Kubeclient::Client.new('http://localhost:8080/api/')
124
- service = client.get_service('redis-slave', 'development')
125
-
126
- assert_instance_of(Kubeclient::Resource, service)
127
- assert_equal('2015-04-05T13:00:31Z',
128
- service.metadata.creationTimestamp)
129
- assert_equal('bdb80a8f-db93-11e4-b293-f8b156af4ae1', service.metadata.uid)
130
- assert_equal('redis-slave', service.metadata.name)
131
- assert_equal('2815', service.metadata.resourceVersion)
132
- assert_equal('v1', service.apiVersion)
133
- assert_equal('10.0.0.140', service.spec.clusterIP)
134
- assert_equal('development', service.metadata.namespace)
135
-
136
- assert_equal('TCP', service.spec.ports[0].protocol)
137
- assert_equal(6379, service.spec.ports[0].port)
138
- assert_equal('', service.spec.ports[0].name)
139
- assert_equal('redis-server', service.spec.ports[0].targetPort)
140
-
141
- assert_requested(:get,
142
- 'http://localhost:8080/api/v1/namespaces/development/services/redis-slave',
143
- times: 1)
144
- end
145
-
146
- def test_delete_service
147
- our_service = Kubeclient::Resource.new
148
- our_service.name = 'redis-service'
149
- # TODO, new ports assignment to be added
150
- our_service.labels = {}
151
- our_service.labels.component = 'apiserver'
152
- our_service.labels.provider = 'kubernetes'
153
-
154
- stub_core_api_list
155
- stub_request(:delete, %r{/namespaces/default/services})
156
- .to_return(body: open_test_file('service.json'), status: 200)
157
-
158
- client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
159
- our_service = client.delete_service(our_service.name, 'default')
160
- assert_kind_of(RecursiveOpenStruct, our_service)
161
-
162
- assert_requested(:delete,
163
- 'http://localhost:8080/api/v1/namespaces/default/services/redis-service',
164
- times: 1)
165
- end
166
-
167
- def test_get_service_no_ns
168
- stub_core_api_list
169
- # when not specifying namespace for entities which
170
- # are not node or namespace, the request will fail
171
- stub_request(:get, %r{/services/redis-slave})
172
- .to_return(status: 404)
173
-
174
- client = Kubeclient::Client.new('http://localhost:8080/api/')
175
-
176
- exception = assert_raises(Kubeclient::HttpError) do
177
- client.get_service('redis-slave')
178
- end
179
- assert_equal(404, exception.error_code)
180
- end
181
-
182
- def test_get_service
183
- stub_core_api_list
184
- stub_request(:get, %r{/namespaces/development/services/redis-slave})
185
- .to_return(body: open_test_file('service.json'),
186
- status: 200)
187
-
188
- client = Kubeclient::Client.new('http://localhost:8080/api/')
189
- service = client.get_service('redis-slave', 'development')
190
- assert_equal('redis-slave', service.metadata.name)
191
-
192
- assert_requested(:get,
193
- 'http://localhost:8080/api/v1/namespaces/development/services/redis-slave',
194
- times: 1)
195
- end
196
-
197
- def test_update_service
198
- service = Kubeclient::Resource.new
199
- name = 'my_service'
200
-
201
- service.metadata = {}
202
- service.metadata.name = name
203
- service.metadata.namespace = 'development'
204
-
205
- stub_core_api_list
206
- expected_url = "http://localhost:8080/api/v1/namespaces/development/services/#{name}"
207
- stub_request(:put, expected_url)
208
- .to_return(body: open_test_file('service_update.json'), status: 201)
209
-
210
- client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
211
- service = client.update_service(service)
212
- assert_kind_of(RecursiveOpenStruct, service)
213
-
214
- assert_requested(:put, expected_url, times: 1) do |req|
215
- data = JSON.parse(req.body)
216
- data['metadata']['name'] == name &&
217
- data['metadata']['namespace'] == 'development'
218
- end
219
- end
220
-
221
- def test_update_service_with_string_keys
222
- service = Kubeclient::Resource.new
223
- name = 'my_service'
224
-
225
- service.metadata = {
226
- 'name' => name,
227
- 'namespace' => 'development'
228
- }
229
-
230
- stub_core_api_list
231
- expected_url = "http://localhost:8080/api/v1/namespaces/development/services/#{name}"
232
- stub_request(:put, expected_url)
233
- .to_return(body: open_test_file('service_update.json'), status: 201)
234
-
235
- client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
236
- service = client.update_service(service)
237
- assert_kind_of(RecursiveOpenStruct, service)
238
-
239
- assert_requested(:put, expected_url, times: 1) do |req|
240
- data = JSON.parse(req.body)
241
- data['metadata']['name'] == name &&
242
- data['metadata']['namespace'] == 'development'
243
- end
244
- end
245
-
246
- def test_patch_service
247
- service = Kubeclient::Resource.new
248
- name = 'my_service'
249
-
250
- service.metadata = {}
251
- service.metadata.name = name
252
- service.metadata.namespace = 'development'
253
-
254
- stub_core_api_list
255
- expected_url = "http://localhost:8080/api/v1/namespaces/development/services/#{name}"
256
- stub_request(:patch, expected_url)
257
- .to_return(body: open_test_file('service_patch.json'), status: 200)
258
-
259
- patch = {
260
- metadata: {
261
- annotations: {
262
- key: 'value'
263
- }
264
- }
265
- }
266
-
267
- client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
268
- service = client.patch_service(name, patch, 'development')
269
- assert_kind_of(RecursiveOpenStruct, service)
270
-
271
- assert_requested(:patch, expected_url, times: 1) do |req|
272
- data = JSON.parse(req.body)
273
- data['metadata']['annotations']['key'] == 'value'
274
- end
275
- end
276
-
277
- def test_json_patch_service
278
- service = Kubeclient::Resource.new
279
- name = 'my-service'
280
-
281
- service.metadata = {}
282
- service.metadata.name = name
283
- service.metadata.namespace = 'development'
284
-
285
- stub_core_api_list
286
- expected_url = "http://localhost:8080/api/v1/namespaces/development/services/#{name}"
287
- stub_request(:patch, expected_url)
288
- .to_return(body: open_test_file('service_json_patch.json'), status: 200)
289
-
290
- patch = [
291
- { 'op' => 'add', 'path' => '/spec/type', 'value' => 'LoadBalancer' }
292
- ]
293
-
294
- client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
295
- service = client.json_patch_service(name, patch, 'development')
296
- assert_kind_of(RecursiveOpenStruct, service)
297
-
298
- assert_requested(:patch, expected_url, times: 1) do |req|
299
- data = JSON.parse(req.body)
300
- req.headers['Content-Type'] == 'application/json-patch+json' &&
301
- data == patch
302
- end
303
- end
304
-
305
- def test_merge_patch_service
306
- service = Kubeclient::Resource.new
307
- name = 'my-service'
308
-
309
- service.metadata = {}
310
- service.metadata.name = name
311
- service.metadata.namespace = 'development'
312
-
313
- stub_core_api_list
314
- expected_url = "http://localhost:8080/api/v1/namespaces/development/services/#{name}"
315
- stub_request(:patch, expected_url)
316
- .to_return(body: open_test_file('service_merge_patch.json'), status: 200)
317
-
318
- patch = { spec: { type: 'NodePort' } }
319
-
320
- client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
321
- service = client.merge_patch_service(name, patch, 'development')
322
- assert_kind_of(RecursiveOpenStruct, service)
323
-
324
- assert_requested(:patch, expected_url, times: 1) do |req|
325
- data = JSON.parse(req.body)
326
- req.headers['Content-Type'] == 'application/merge-patch+json' &&
327
- data['spec']['type'] == 'NodePort'
328
- end
329
- end
330
- end
@@ -1,26 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- # ServiceAccount tests
4
- class TestServiceAccount < MiniTest::Test
5
- def test_get_from_json_v1
6
- stub_core_api_list
7
- stub_request(:get, %r{/serviceaccounts})
8
- .to_return(body: open_test_file('service_account.json'),
9
- status: 200)
10
-
11
- client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
12
- account = client.get_service_account('default')
13
-
14
- assert_instance_of(Kubeclient::Resource, account)
15
- assert_equal('default', account.metadata.name)
16
- assert_equal('default-token-6s23q', account.secrets[0].name)
17
- assert_equal('default-dockercfg-62tf3', account.secrets[1].name)
18
-
19
- assert_requested(:get,
20
- 'http://localhost:8080/api/v1/serviceaccounts/default',
21
- times: 1)
22
- assert_requested(:get,
23
- 'http://localhost:8080/api/v1',
24
- times: 1)
25
- end
26
- end
data/test/test_watch.rb DELETED
@@ -1,195 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- # Watch entity tests
4
- class TestWatch < MiniTest::Test
5
- def test_watch_pod_success
6
- stub_core_api_list
7
-
8
- expected = [
9
- { 'type' => 'ADDED', 'resourceVersion' => '1389' },
10
- { 'type' => 'MODIFIED', 'resourceVersion' => '1390' },
11
- { 'type' => 'DELETED', 'resourceVersion' => '1398' }
12
- ]
13
-
14
- stub_request(:get, %r{/watch/pods})
15
- .to_return(body: open_test_file('watch_stream.json'),
16
- status: 200)
17
-
18
- client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
19
-
20
- client.watch_pods.to_enum.with_index do |notice, index|
21
- assert_instance_of(Kubeclient::Resource, notice)
22
- assert_equal(expected[index]['type'], notice.type)
23
- assert_equal('Pod', notice.object.kind)
24
- assert_equal('php', notice.object.metadata.name)
25
- assert_equal(expected[index]['resourceVersion'],
26
- notice.object.metadata.resourceVersion)
27
- end
28
- end
29
-
30
- def test_watch_pod_block
31
- stub_core_api_list
32
- stub_request(:get, %r{/watch/pods})
33
- .to_return(body: open_test_file('watch_stream.json'),
34
- status: 200)
35
-
36
- client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
37
- yielded = []
38
- client.watch_pods { |notice| yielded << notice.type }
39
-
40
- assert_equal %w[ADDED MODIFIED DELETED], yielded
41
- end
42
-
43
- def test_watch_pod_raw
44
- stub_core_api_list
45
-
46
- stub_request(:get, %r{/watch/pods}).to_return(
47
- body: open_test_file('watch_stream.json'),
48
- status: 200
49
- )
50
-
51
- client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
52
-
53
- got = nil
54
- client.watch_pods(as: :raw).each { |notice| got = notice }
55
- assert_match(/\A{"type":"DELETED"/, got)
56
- end
57
-
58
- def test_watch_pod_failure
59
- stub_core_api_list
60
- stub_request(:get, %r{/watch/pods}).to_return(status: 404)
61
-
62
- client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
63
- assert_raises(Kubeclient::HttpError) do
64
- client.watch_pods.each do
65
- end
66
- end
67
- end
68
-
69
- def test_watch_pod_follow_redirect
70
- stub_core_api_list
71
-
72
- redirect = 'http://localhost:1234/api/v1/watch/pods'
73
- stub_request(:get, %r{/watch/pods})
74
- .to_return(status: 302, headers: { location: redirect })
75
-
76
- stub_request(:get, redirect).to_return(
77
- body: open_test_file('watch_stream.json'),
78
- status: 200
79
- )
80
-
81
- client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
82
-
83
- got = nil
84
- client.watch_pods.each { |notice| got = notice }
85
- assert_equal('DELETED', got.type)
86
- end
87
-
88
- def test_watch_pod_max_redirect
89
- stub_core_api_list
90
-
91
- redirect = 'http://localhost:1234/api/v1/watcher/pods'
92
- stub_request(:get, %r{/watch/pods})
93
- .to_return(status: 302, headers: { location: redirect })
94
-
95
- stub_request(:get, redirect).to_return(
96
- body: open_test_file('watch_stream.json'),
97
- status: 200
98
- )
99
-
100
- client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1', http_max_redirects: 0)
101
-
102
- assert_raises(Kubeclient::HttpError) do
103
- client.watch_pods.each do
104
- end
105
- end
106
- end
107
-
108
- # Ensure that WatchStream respects a format that's not JSON
109
- def test_watch_stream_text
110
- url = 'http://www.example.com/foobar'
111
- expected_lines = open_test_file('pod_log.txt').read.split("\n")
112
-
113
- stub_request(:get, url)
114
- .to_return(body: open_test_file('pod_log.txt'),
115
- status: 200)
116
-
117
- stream = Kubeclient::Common::WatchStream.new(URI.parse(url), {}, formatter: ->(v) { v })
118
- stream.to_enum.with_index do |line, index|
119
- assert_instance_of(String, line)
120
- assert_equal(expected_lines[index], line)
121
- end
122
- end
123
-
124
- def test_watch_with_resource_version
125
- api_host = 'http://localhost:8080/api'
126
- version = '1995'
127
- stub_core_api_list
128
- stub_request(:get, %r{.*\/watch/events})
129
- .to_return(body: open_test_file('watch_stream.json'),
130
- status: 200)
131
-
132
- client = Kubeclient::Client.new(api_host, 'v1')
133
- results = client.watch_events(version).to_enum
134
-
135
- assert_equal(3, results.count)
136
- assert_requested(:get,
137
- "#{api_host}/v1/watch/events?resourceVersion=#{version}",
138
- times: 1)
139
- end
140
-
141
- def test_watch_with_label_selector
142
- api_host = 'http://localhost:8080/api'
143
- selector = 'name=redis-master'
144
-
145
- stub_core_api_list
146
- stub_request(:get, %r{.*\/watch/events})
147
- .to_return(body: open_test_file('watch_stream.json'),
148
- status: 200)
149
-
150
- client = Kubeclient::Client.new(api_host, 'v1')
151
- results = client.watch_events(label_selector: selector).to_enum
152
-
153
- assert_equal(3, results.count)
154
- assert_requested(:get,
155
- "#{api_host}/v1/watch/events?labelSelector=#{selector}",
156
- times: 1)
157
- end
158
-
159
- def test_watch_with_field_selector
160
- api_host = 'http://localhost:8080/api'
161
- selector = 'involvedObject.kind=Pod'
162
-
163
- stub_core_api_list
164
- stub_request(:get, %r{.*\/watch/events})
165
- .to_return(body: open_test_file('watch_stream.json'),
166
- status: 200)
167
-
168
- client = Kubeclient::Client.new(api_host, 'v1')
169
- results = client.watch_events(field_selector: selector).to_enum
170
-
171
- assert_equal(3, results.count)
172
- assert_requested(:get,
173
- "#{api_host}/v1/watch/events?fieldSelector=#{selector}",
174
- times: 1)
175
- end
176
-
177
- def test_watch_with_finish_and_ebadf
178
- api_host = 'http://localhost:8080/api'
179
-
180
- stub_core_api_list
181
- stub_request(:get, %r{.*\/watch/events})
182
- .to_return(body: open_test_file('watch_stream.json'), status: 200)
183
-
184
- client = Kubeclient::Client.new(api_host, 'v1')
185
- watcher = client.watch_events
186
-
187
- # explodes when StandardError is not caught
188
- watcher.each do
189
- watcher.finish
190
- raise StandardError
191
- end
192
-
193
- assert_requested(:get, "#{api_host}/v1/watch/events", times: 1)
194
- end
195
- end