kubeclient-rollback-dev 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (97) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -0
  3. data/.rubocop.yml +16 -0
  4. data/.travis.yml +12 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +428 -0
  8. data/Rakefile +12 -0
  9. data/kubeclient.gemspec +29 -0
  10. data/lib/kubeclient/common.rb +512 -0
  11. data/lib/kubeclient/config.rb +126 -0
  12. data/lib/kubeclient/entity_list.rb +16 -0
  13. data/lib/kubeclient/kube_exception.rb +14 -0
  14. data/lib/kubeclient/missing_kind_compatibility.rb +68 -0
  15. data/lib/kubeclient/version.rb +4 -0
  16. data/lib/kubeclient/watch_notice.rb +7 -0
  17. data/lib/kubeclient/watch_stream.rb +80 -0
  18. data/lib/kubeclient.rb +32 -0
  19. data/test/cassettes/kubernetes_guestbook.yml +879 -0
  20. data/test/config/allinone.kubeconfig +20 -0
  21. data/test/config/external-ca.pem +18 -0
  22. data/test/config/external-cert.pem +19 -0
  23. data/test/config/external-key.rsa +27 -0
  24. data/test/config/external.kubeconfig +20 -0
  25. data/test/config/nouser.kubeconfig +16 -0
  26. data/test/config/userauth.kubeconfig +28 -0
  27. data/test/json/bindings_list.json +10 -0
  28. data/test/json/component_status.json +17 -0
  29. data/test/json/component_status_list.json +52 -0
  30. data/test/json/config_map_list.json +9 -0
  31. data/test/json/core_api_resource_list.json +181 -0
  32. data/test/json/core_api_resource_list_without_kind.json +129 -0
  33. data/test/json/core_oapi_resource_list_without_kind.json +197 -0
  34. data/test/json/created_endpoint.json +28 -0
  35. data/test/json/created_namespace.json +20 -0
  36. data/test/json/created_secret.json +16 -0
  37. data/test/json/created_service.json +31 -0
  38. data/test/json/empty_pod_list.json +9 -0
  39. data/test/json/endpoint_list.json +48 -0
  40. data/test/json/entity_list.json +56 -0
  41. data/test/json/event_list.json +35 -0
  42. data/test/json/limit_range.json +23 -0
  43. data/test/json/limit_range_list.json +31 -0
  44. data/test/json/namespace.json +13 -0
  45. data/test/json/namespace_exception.json +8 -0
  46. data/test/json/namespace_list.json +32 -0
  47. data/test/json/node.json +29 -0
  48. data/test/json/node_list.json +37 -0
  49. data/test/json/persistent_volume.json +37 -0
  50. data/test/json/persistent_volume_claim.json +32 -0
  51. data/test/json/persistent_volume_claim_list.json +40 -0
  52. data/test/json/persistent_volume_claims_nil_items.json +8 -0
  53. data/test/json/persistent_volume_list.json +45 -0
  54. data/test/json/pod.json +92 -0
  55. data/test/json/pod_list.json +79 -0
  56. data/test/json/pod_template_list.json +9 -0
  57. data/test/json/processed_template.json +27 -0
  58. data/test/json/replication_controller.json +57 -0
  59. data/test/json/replication_controller_list.json +66 -0
  60. data/test/json/resource_quota.json +46 -0
  61. data/test/json/resource_quota_list.json +54 -0
  62. data/test/json/secret_list.json +44 -0
  63. data/test/json/service.json +33 -0
  64. data/test/json/service_account.json +25 -0
  65. data/test/json/service_account_list.json +82 -0
  66. data/test/json/service_illegal_json_404.json +1 -0
  67. data/test/json/service_list.json +97 -0
  68. data/test/json/service_patch.json +25 -0
  69. data/test/json/service_update.json +22 -0
  70. data/test/json/versions_list.json +6 -0
  71. data/test/json/watch_stream.json +3 -0
  72. data/test/test_common.rb +32 -0
  73. data/test/test_component_status.rb +30 -0
  74. data/test/test_config.rb +72 -0
  75. data/test/test_endpoint.rb +35 -0
  76. data/test/test_guestbook_go.rb +238 -0
  77. data/test/test_helper.rb +10 -0
  78. data/test/test_kubeclient.rb +611 -0
  79. data/test/test_limit_range.rb +27 -0
  80. data/test/test_missing_methods.rb +42 -0
  81. data/test/test_namespace.rb +61 -0
  82. data/test/test_node.rb +33 -0
  83. data/test/test_persistent_volume.rb +30 -0
  84. data/test/test_persistent_volume_claim.rb +30 -0
  85. data/test/test_pod.rb +29 -0
  86. data/test/test_pod_log.rb +50 -0
  87. data/test/test_process_template.rb +44 -0
  88. data/test/test_replication_controller.rb +27 -0
  89. data/test/test_resource_list_without_kind.rb +78 -0
  90. data/test/test_resource_quota.rb +25 -0
  91. data/test/test_secret.rb +70 -0
  92. data/test/test_service.rb +293 -0
  93. data/test/test_service_account.rb +28 -0
  94. data/test/test_watch.rb +119 -0
  95. data/test/txt/pod_log.txt +6 -0
  96. data/test/valid_token_file +1 -0
  97. metadata +343 -0
@@ -0,0 +1,293 @@
1
+ require '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_request(:get, %r{/api/v1$})
29
+ .to_return(body: open_test_file('core_api_resource_list.json'),
30
+ status: 200)
31
+
32
+ stub_request(:post, expected_url)
33
+ .to_return(body: open_test_file('created_service.json'), status: 201)
34
+
35
+ client = Kubeclient::Client.new 'http://localhost:8080/api/'
36
+ created = client.create_service our_service
37
+
38
+ assert_instance_of(Kubeclient::Service, created)
39
+ assert_equal(created.metadata.name, our_service.metadata.name)
40
+ assert_equal(created.spec.ports.size, our_service.spec.ports.size)
41
+
42
+ # Check that original entity_config is not modified by kind/apiVersion patches:
43
+ assert_equal(our_service.kind, nil)
44
+
45
+ assert_requested(:post, expected_url, times: 1) do |req|
46
+ data = JSON.parse(req.body)
47
+ data['kind'] == 'Service' &&
48
+ data['apiVersion'] == 'v1' &&
49
+ data['metadata']['name'] == 'guestbook' &&
50
+ data['metadata']['namespace'] == 'staging'
51
+ end
52
+ end
53
+
54
+ def test_construct_service_from_symbol_keys
55
+ service = Kubeclient::Resource.new
56
+ service.metadata = {
57
+ labels: { tier: 'frontend' },
58
+ name: 'test-service',
59
+ namespace: 'staging'
60
+ }
61
+ service.spec = {
62
+ ports: [{
63
+ port: 3000,
64
+ targetPort: 'http-server',
65
+ protocol: 'TCP'
66
+ }]
67
+ }
68
+
69
+ expected_url = 'http://localhost:8080/api/v1/namespaces/staging/services'
70
+ stub_request(:get, %r{/api/v1$})
71
+ .to_return(body: open_test_file('core_api_resource_list.json'),
72
+ status: 200)
73
+ stub_request(:post, expected_url)
74
+ .to_return(body: open_test_file('created_service.json'), status: 201)
75
+
76
+ client = Kubeclient::Client.new 'http://localhost:8080/api/'
77
+ client.create_service service
78
+
79
+ assert_requested(:post, expected_url, times: 1) do |req|
80
+ data = JSON.parse(req.body)
81
+ data['kind'] == 'Service' &&
82
+ data['apiVersion'] == 'v1' &&
83
+ data['metadata']['name'] == 'test-service' &&
84
+ data['metadata']['labels']['tier'] == 'frontend' &&
85
+ data['metadata']['namespace'] == 'staging'
86
+ end
87
+ end
88
+
89
+ def test_construct_service_from_string_keys
90
+ service = Kubeclient::Resource.new
91
+ service.metadata = {
92
+ 'labels' => { 'tier' => 'frontend' },
93
+ 'name' => 'test-service',
94
+ 'namespace' => 'staging'
95
+ }
96
+ service.spec = {
97
+ 'ports' => [{
98
+ 'port' => 3000,
99
+ 'targetPort' => 'http-server',
100
+ 'protocol' => 'TCP'
101
+ }]
102
+ }
103
+
104
+ stub_request(:get, %r{/api/v1$})
105
+ .to_return(body: open_test_file('core_api_resource_list.json'),
106
+ status: 200)
107
+ expected_url = 'http://localhost:8080/api/v1/namespaces/staging/services'
108
+ stub_request(:post, %r{namespaces/staging/services})
109
+ .to_return(body: open_test_file('created_service.json'), status: 201)
110
+
111
+ client = Kubeclient::Client.new 'http://localhost:8080/api/'
112
+ client.create_service service
113
+
114
+ assert_requested(:post, expected_url, times: 1) do |req|
115
+ data = JSON.parse(req.body)
116
+ data['kind'] == 'Service' &&
117
+ data['apiVersion'] == 'v1' &&
118
+ data['metadata']['name'] == 'test-service' &&
119
+ data['metadata']['labels']['tier'] == 'frontend' &&
120
+ data['metadata']['namespace'] == 'staging'
121
+ end
122
+ end
123
+
124
+ def test_conversion_from_json_v1
125
+ stub_request(:get, %r{/api/v1$})
126
+ .to_return(body: open_test_file('core_api_resource_list.json'),
127
+ status: 200)
128
+ stub_request(:get, %r{/services})
129
+ .to_return(body: open_test_file('service.json'),
130
+ status: 200)
131
+
132
+ client = Kubeclient::Client.new 'http://localhost:8080/api/'
133
+ service = client.get_service 'redis-slave', 'development'
134
+
135
+ assert_instance_of(Kubeclient::Service, service)
136
+ assert_equal('2015-04-05T13:00:31Z',
137
+ service.metadata.creationTimestamp)
138
+ assert_equal('bdb80a8f-db93-11e4-b293-f8b156af4ae1', service.metadata.uid)
139
+ assert_equal('redis-slave', service.metadata.name)
140
+ assert_equal('2815', service.metadata.resourceVersion)
141
+ assert_equal('v1', service.apiVersion)
142
+ assert_equal('10.0.0.140', service.spec.clusterIP)
143
+ assert_equal('development', service.metadata.namespace)
144
+
145
+ assert_equal('TCP', service.spec.ports[0].protocol)
146
+ assert_equal(6379, service.spec.ports[0].port)
147
+ assert_equal('', service.spec.ports[0].name)
148
+ assert_equal('redis-server', service.spec.ports[0].targetPort)
149
+
150
+ assert_requested(:get,
151
+ 'http://localhost:8080/api/v1/namespaces/development/services/redis-slave',
152
+ times: 1)
153
+ end
154
+
155
+ def test_delete_service
156
+ our_service = Kubeclient::Resource.new
157
+ our_service.name = 'redis-service'
158
+ # TODO, new ports assignment to be added
159
+ our_service.labels = {}
160
+ our_service.labels.component = 'apiserver'
161
+ our_service.labels.provider = 'kubernetes'
162
+
163
+ stub_request(:get, %r{/api/v1$})
164
+ .to_return(body: open_test_file('core_api_resource_list.json'),
165
+ status: 200)
166
+ stub_request(:delete, %r{/namespaces/default/services})
167
+ .to_return(status: 200)
168
+
169
+ client = Kubeclient::Client.new 'http://localhost:8080/api/', 'v1'
170
+ client.delete_service our_service.name, 'default'
171
+
172
+ assert_requested(:delete,
173
+ 'http://localhost:8080/api/v1/namespaces/default/services/redis-service',
174
+ times: 1)
175
+ end
176
+
177
+ def test_get_service_no_ns
178
+ stub_request(:get, %r{/api/v1$})
179
+ .to_return(body: open_test_file('core_api_resource_list.json'),
180
+ status: 200)
181
+ # when not specifying namespace for entities which
182
+ # are not node or namespace, the request will fail
183
+ stub_request(:get, %r{/services/redis-slave})
184
+ .to_return(status: 404)
185
+
186
+ client = Kubeclient::Client.new 'http://localhost:8080/api/'
187
+
188
+ exception = assert_raises(KubeException) do
189
+ client.get_service 'redis-slave'
190
+ end
191
+ assert_equal(404, exception.error_code)
192
+ end
193
+
194
+ def test_get_service
195
+ stub_request(:get, %r{/api/v1$})
196
+ .to_return(body: open_test_file('core_api_resource_list.json'),
197
+ status: 200)
198
+ stub_request(:get, %r{/namespaces/development/services/redis-slave})
199
+ .to_return(body: open_test_file('service.json'),
200
+ status: 200)
201
+
202
+ client = Kubeclient::Client.new 'http://localhost:8080/api/'
203
+ service = client.get_service 'redis-slave', 'development'
204
+ assert_equal('redis-slave', service.metadata.name)
205
+
206
+ assert_requested(:get,
207
+ 'http://localhost:8080/api/v1/namespaces/development/services/redis-slave',
208
+ times: 1)
209
+ end
210
+
211
+ def test_update_service
212
+ service = Kubeclient::Resource.new
213
+ name = 'my_service'
214
+
215
+ service.metadata = {}
216
+ service.metadata.name = name
217
+ service.metadata.namespace = 'development'
218
+
219
+ stub_request(:get, %r{/api/v1$})
220
+ .to_return(body: open_test_file('core_api_resource_list.json'),
221
+ status: 200)
222
+ expected_url = "http://localhost:8080/api/v1/namespaces/development/services/#{name}"
223
+ stub_request(:put, expected_url)
224
+ .to_return(body: open_test_file('service_update.json'), status: 201)
225
+
226
+ client = Kubeclient::Client.new 'http://localhost:8080/api/', 'v1'
227
+ client.update_service service
228
+
229
+ assert_requested(:put, expected_url, times: 1) do |req|
230
+ data = JSON.parse(req.body)
231
+ data['metadata']['name'] == name &&
232
+ data['metadata']['namespace'] == 'development'
233
+ end
234
+ end
235
+
236
+ def test_update_service_with_string_keys
237
+ service = Kubeclient::Resource.new
238
+ name = 'my_service'
239
+
240
+ service.metadata = {
241
+ 'name' => name,
242
+ 'namespace' => 'development'
243
+ }
244
+
245
+ stub_request(:get, %r{/api/v1$})
246
+ .to_return(body: open_test_file('core_api_resource_list.json'),
247
+ status: 200)
248
+ expected_url = "http://localhost:8080/api/v1/namespaces/development/services/#{name}"
249
+ stub_request(:put, expected_url)
250
+ .to_return(body: open_test_file('service_update.json'), status: 201)
251
+
252
+ client = Kubeclient::Client.new 'http://localhost:8080/api/', 'v1'
253
+ client.update_service service
254
+
255
+ assert_requested(:put, expected_url, times: 1) do |req|
256
+ data = JSON.parse(req.body)
257
+ data['metadata']['name'] == name &&
258
+ data['metadata']['namespace'] == 'development'
259
+ end
260
+ end
261
+
262
+ def test_patch_service
263
+ service = Kubeclient::Resource.new
264
+ name = 'my_service'
265
+
266
+ service.metadata = {}
267
+ service.metadata.name = name
268
+ service.metadata.namespace = 'development'
269
+
270
+ stub_request(:get, %r{/api/v1$})
271
+ .to_return(body: open_test_file('core_api_resource_list.json'),
272
+ status: 200)
273
+ expected_url = "http://localhost:8080/api/v1/namespaces/development/services/#{name}"
274
+ stub_request(:patch, expected_url)
275
+ .to_return(body: open_test_file('service_patch.json'), status: 200)
276
+
277
+ patch = {
278
+ metadata: {
279
+ annotations: {
280
+ key: 'value'
281
+ }
282
+ }
283
+ }
284
+
285
+ client = Kubeclient::Client.new 'http://localhost:8080/api/', 'v1'
286
+ client.patch_service name, patch, 'development'
287
+
288
+ assert_requested(:patch, expected_url, times: 1) do |req|
289
+ data = JSON.parse(req.body)
290
+ data['metadata']['annotations']['key'] == 'value'
291
+ end
292
+ end
293
+ end
@@ -0,0 +1,28 @@
1
+ require 'test_helper'
2
+
3
+ # ServiceAccount tests
4
+ class TestServiceAccount < MiniTest::Test
5
+ def test_get_from_json_v1
6
+ stub_request(:get, %r{/serviceaccounts})
7
+ .to_return(body: open_test_file('service_account.json'),
8
+ status: 200)
9
+ stub_request(:get, %r{/api/v1$})
10
+ .to_return(body: open_test_file('core_api_resource_list.json'),
11
+ status: 200)
12
+
13
+ client = Kubeclient::Client.new 'http://localhost:8080/api/', 'v1'
14
+ account = client.get_service_account 'default'
15
+
16
+ assert_instance_of(Kubeclient::ServiceAccount, account)
17
+ assert_equal('default', account.metadata.name)
18
+ assert_equal('default-token-6s23q', account.secrets[0].name)
19
+ assert_equal('default-dockercfg-62tf3', account.secrets[1].name)
20
+
21
+ assert_requested(:get,
22
+ 'http://localhost:8080/api/v1/serviceaccounts/default',
23
+ times: 1)
24
+ assert_requested(:get,
25
+ 'http://localhost:8080/api/v1',
26
+ times: 1)
27
+ end
28
+ end
@@ -0,0 +1,119 @@
1
+ require 'test_helper'
2
+
3
+ # Watch entity tests
4
+ class TestWatch < MiniTest::Test
5
+ def test_watch_pod_success
6
+ expected = [
7
+ { 'type' => 'ADDED', 'resourceVersion' => '1389' },
8
+ { 'type' => 'MODIFIED', 'resourceVersion' => '1390' },
9
+ { 'type' => 'DELETED', 'resourceVersion' => '1398' }
10
+ ]
11
+
12
+ stub_request(:get, %r{/api/v1$})
13
+ .to_return(body: open_test_file('core_api_resource_list.json'),
14
+ status: 200)
15
+
16
+ stub_request(:get, %r{.*\/watch/pods})
17
+ .to_return(body: open_test_file('watch_stream.json'),
18
+ status: 200)
19
+
20
+ client = Kubeclient::Client.new 'http://localhost:8080/api/', 'v1'
21
+
22
+ client.watch_pods.to_enum.with_index do |notice, index|
23
+ assert_instance_of(Kubeclient::Common::WatchNotice, notice)
24
+ assert_equal(expected[index]['type'], notice.type)
25
+ assert_equal('Pod', notice.object.kind)
26
+ assert_equal('php', notice.object.metadata.name)
27
+ assert_equal(expected[index]['resourceVersion'],
28
+ notice.object.metadata.resourceVersion)
29
+ end
30
+ end
31
+
32
+ def test_watch_pod_failure
33
+ stub_request(:get, %r{/api/v1$})
34
+ .to_return(body: open_test_file('core_api_resource_list.json'),
35
+ status: 200)
36
+ stub_request(:get, %r{.*\/watch/pods}).to_return(status: 404)
37
+
38
+ client = Kubeclient::Client.new 'http://localhost:8080/api/', 'v1'
39
+ assert_raises KubeException do
40
+ client.watch_pods.each do
41
+ end
42
+ end
43
+ end
44
+
45
+ # Ensure that WatchStream respects a format that's not JSON
46
+ def test_watch_stream_text
47
+ url = 'http://www.example.com/foobar'
48
+ expected_lines = open_test_file('pod_log.txt').read.split("\n")
49
+
50
+ stub_request(:get, url)
51
+ .to_return(body: open_test_file('pod_log.txt'),
52
+ status: 200)
53
+
54
+ stream = Kubeclient::Common::WatchStream.new(URI.parse(url), {}, format: :txt)
55
+ stream.to_enum.with_index do |line, index|
56
+ assert_instance_of(String, line)
57
+ assert_equal(expected_lines[index], line)
58
+ end
59
+ end
60
+
61
+ def test_watch_with_resource_version
62
+ api_host = 'http://localhost:8080/api'
63
+ version = '1995'
64
+ stub_request(:get, %r{/api/v1$})
65
+ .to_return(body: open_test_file('core_api_resource_list.json'),
66
+ status: 200)
67
+ stub_request(:get, %r{.*\/watch/events})
68
+ .to_return(body: open_test_file('watch_stream.json'),
69
+ status: 200)
70
+
71
+ client = Kubeclient::Client.new api_host, 'v1'
72
+ results = client.watch_events(version).to_enum
73
+
74
+ assert_equal(3, results.count)
75
+ assert_requested(:get,
76
+ "#{api_host}/v1/watch/events?resourceVersion=#{version}",
77
+ times: 1)
78
+ end
79
+
80
+ def test_watch_with_label_selector
81
+ api_host = 'http://localhost:8080/api'
82
+ selector = 'name=redis-master'
83
+
84
+ stub_request(:get, %r{/api/v1$})
85
+ .to_return(body: open_test_file('core_api_resource_list.json'),
86
+ status: 200)
87
+ stub_request(:get, %r{.*\/watch/events})
88
+ .to_return(body: open_test_file('watch_stream.json'),
89
+ status: 200)
90
+
91
+ client = Kubeclient::Client.new api_host, 'v1'
92
+ results = client.watch_events(label_selector: selector).to_enum
93
+
94
+ assert_equal(3, results.count)
95
+ assert_requested(:get,
96
+ "#{api_host}/v1/watch/events?labelSelector=#{selector}",
97
+ times: 1)
98
+ end
99
+
100
+ def test_watch_with_field_selector
101
+ api_host = 'http://localhost:8080/api'
102
+ selector = 'involvedObject.kind=Pod'
103
+
104
+ stub_request(:get, %r{/api/v1$})
105
+ .to_return(body: open_test_file('core_api_resource_list.json'),
106
+ status: 200)
107
+ stub_request(:get, %r{.*\/watch/events})
108
+ .to_return(body: open_test_file('watch_stream.json'),
109
+ status: 200)
110
+
111
+ client = Kubeclient::Client.new api_host, 'v1'
112
+ results = client.watch_events(field_selector: selector).to_enum
113
+
114
+ assert_equal(3, results.count)
115
+ assert_requested(:get,
116
+ "#{api_host}/v1/watch/events?fieldSelector=#{selector}",
117
+ times: 1)
118
+ end
119
+ end
@@ -0,0 +1,6 @@
1
+ Initializing server...
2
+ ...loaded configuration
3
+ ...updated settings
4
+ ...discovered local servers
5
+ ...frobinated disks
6
+ Complete!
@@ -0,0 +1 @@
1
+ valid_token