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.
- checksums.yaml +4 -4
- data/.github/workflows/actions.yml +35 -0
- data/CHANGELOG.md +37 -0
- data/README.md +155 -37
- data/RELEASING.md +3 -1
- data/kubeclient.gemspec +5 -4
- data/lib/kubeclient.rb +1 -1
- data/lib/kubeclient/common.rb +35 -6
- data/lib/kubeclient/config.rb +17 -4
- data/lib/kubeclient/exec_credentials.rb +33 -4
- data/lib/kubeclient/google_application_default_credentials.rb +6 -1
- data/lib/kubeclient/version.rb +1 -1
- metadata +13 -217
- data/.travis.yml +0 -26
- data/test/cassettes/kubernetes_guestbook.yml +0 -879
- data/test/config/allinone.kubeconfig +0 -20
- data/test/config/execauth.kubeconfig +0 -62
- data/test/config/external-ca.pem +0 -18
- data/test/config/external-cert.pem +0 -19
- data/test/config/external-key.rsa +0 -27
- data/test/config/external.kubeconfig +0 -20
- data/test/config/gcpauth.kubeconfig +0 -22
- data/test/config/gcpcmdauth.kubeconfig +0 -26
- data/test/config/nouser.kubeconfig +0 -16
- data/test/config/oidcauth.kubeconfig +0 -25
- data/test/config/timestamps.kubeconfig +0 -25
- data/test/config/userauth.kubeconfig +0 -28
- data/test/json/bindings_list.json +0 -10
- data/test/json/component_status.json +0 -17
- data/test/json/component_status_list.json +0 -52
- data/test/json/config.istio.io_api_resource_list.json +0 -679
- data/test/json/config_map_list.json +0 -9
- data/test/json/core_api_resource_list.json +0 -181
- data/test/json/core_api_resource_list_without_kind.json +0 -129
- data/test/json/core_oapi_resource_list_without_kind.json +0 -197
- data/test/json/created_endpoint.json +0 -28
- data/test/json/created_namespace.json +0 -20
- data/test/json/created_secret.json +0 -16
- data/test/json/created_security_context_constraint.json +0 -65
- data/test/json/created_service.json +0 -31
- data/test/json/empty_pod_list.json +0 -9
- data/test/json/endpoint_list.json +0 -48
- data/test/json/entity_list.json +0 -56
- data/test/json/event_list.json +0 -35
- data/test/json/extensions_v1beta1_api_resource_list.json +0 -217
- data/test/json/limit_range.json +0 -23
- data/test/json/limit_range_list.json +0 -31
- data/test/json/namespace.json +0 -13
- data/test/json/namespace_exception.json +0 -8
- data/test/json/namespace_list.json +0 -32
- data/test/json/node.json +0 -29
- data/test/json/node_list.json +0 -37
- data/test/json/node_notice.json +0 -160
- data/test/json/persistent_volume.json +0 -37
- data/test/json/persistent_volume_claim.json +0 -32
- data/test/json/persistent_volume_claim_list.json +0 -40
- data/test/json/persistent_volume_claims_nil_items.json +0 -8
- data/test/json/persistent_volume_list.json +0 -45
- data/test/json/pod.json +0 -92
- data/test/json/pod_list.json +0 -79
- data/test/json/pod_template_list.json +0 -9
- data/test/json/pods_1.json +0 -265
- data/test/json/pods_2.json +0 -102
- data/test/json/pods_410.json +0 -9
- data/test/json/processed_template.json +0 -27
- data/test/json/replication_controller.json +0 -57
- data/test/json/replication_controller_list.json +0 -66
- data/test/json/resource_quota.json +0 -46
- data/test/json/resource_quota_list.json +0 -54
- data/test/json/secret_list.json +0 -44
- data/test/json/security.openshift.io_api_resource_list.json +0 -69
- data/test/json/security_context_constraint_list.json +0 -375
- data/test/json/service.json +0 -33
- data/test/json/service_account.json +0 -25
- data/test/json/service_account_list.json +0 -82
- data/test/json/service_illegal_json_404.json +0 -1
- data/test/json/service_json_patch.json +0 -26
- data/test/json/service_list.json +0 -97
- data/test/json/service_merge_patch.json +0 -26
- data/test/json/service_patch.json +0 -25
- data/test/json/service_update.json +0 -22
- data/test/json/template.json +0 -27
- data/test/json/template.openshift.io_api_resource_list.json +0 -75
- data/test/json/template_list.json +0 -35
- data/test/json/versions_list.json +0 -6
- data/test/json/watch_stream.json +0 -3
- data/test/test_common.rb +0 -95
- data/test/test_component_status.rb +0 -29
- data/test/test_config.rb +0 -222
- data/test/test_endpoint.rb +0 -54
- data/test/test_exec_credentials.rb +0 -125
- data/test/test_gcp_command_credentials.rb +0 -27
- data/test/test_google_application_default_credentials.rb +0 -15
- data/test/test_guestbook_go.rb +0 -235
- data/test/test_helper.rb +0 -18
- data/test/test_kubeclient.rb +0 -881
- data/test/test_limit_range.rb +0 -25
- data/test/test_missing_methods.rb +0 -80
- data/test/test_namespace.rb +0 -59
- data/test/test_node.rb +0 -70
- data/test/test_oidc_auth_provider.rb +0 -103
- data/test/test_persistent_volume.rb +0 -29
- data/test/test_persistent_volume_claim.rb +0 -28
- data/test/test_pod.rb +0 -81
- data/test/test_pod_log.rb +0 -157
- data/test/test_process_template.rb +0 -80
- data/test/test_replication_controller.rb +0 -47
- data/test/test_resource_list_without_kind.rb +0 -78
- data/test/test_resource_quota.rb +0 -23
- data/test/test_secret.rb +0 -62
- data/test/test_security_context_constraint.rb +0 -62
- data/test/test_service.rb +0 -330
- data/test/test_service_account.rb +0 -26
- data/test/test_watch.rb +0 -195
- data/test/txt/pod_log.txt +0 -6
- data/test/valid_token_file +0 -1
@@ -1,15 +0,0 @@
|
|
1
|
-
require_relative 'test_helper'
|
2
|
-
require 'googleauth'
|
3
|
-
|
4
|
-
# Unit tests for the ApplicationDefaultCredentials token provider
|
5
|
-
class GoogleApplicationDefaultCredentialsTest < MiniTest::Test
|
6
|
-
def test_token
|
7
|
-
auth = Minitest::Mock.new
|
8
|
-
auth.expect(:apply, nil, [{}])
|
9
|
-
auth.expect(:access_token, 'valid_token')
|
10
|
-
|
11
|
-
Google::Auth.stub(:get_application_default, auth) do
|
12
|
-
assert_equal('valid_token', Kubeclient::GoogleApplicationDefaultCredentials.token)
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
data/test/test_guestbook_go.rb
DELETED
@@ -1,235 +0,0 @@
|
|
1
|
-
require_relative 'test_helper'
|
2
|
-
require 'vcr'
|
3
|
-
|
4
|
-
# creation of google's example of guest book
|
5
|
-
class CreateGuestbookGo < MiniTest::Test
|
6
|
-
def test_create_guestbook_entities
|
7
|
-
VCR.configure do |c|
|
8
|
-
c.cassette_library_dir = 'test/cassettes'
|
9
|
-
c.hook_into(:webmock)
|
10
|
-
end
|
11
|
-
|
12
|
-
# WebMock.allow_net_connect!
|
13
|
-
VCR.use_cassette('kubernetes_guestbook') do # , record: :new_episodes) do
|
14
|
-
client = Kubeclient::Client.new('http://10.35.0.23:8080/api/', 'v1')
|
15
|
-
|
16
|
-
testing_ns = Kubeclient::Resource.new
|
17
|
-
testing_ns.metadata = {}
|
18
|
-
testing_ns.metadata.name = 'kubeclient-ns'
|
19
|
-
|
20
|
-
# delete in case they existed before so creation can be tested
|
21
|
-
delete_namespace(client, testing_ns.metadata.name)
|
22
|
-
delete_services(
|
23
|
-
client, testing_ns.metadata.name,
|
24
|
-
['guestbook', 'redis-master', 'redis-slave']
|
25
|
-
)
|
26
|
-
delete_replication_controllers(
|
27
|
-
client, testing_ns.metadata.name,
|
28
|
-
['guestbook', 'redis-master', 'redis-slave']
|
29
|
-
)
|
30
|
-
|
31
|
-
client.create_namespace(testing_ns)
|
32
|
-
services = create_services(client, testing_ns.metadata.name)
|
33
|
-
replicators = create_replication_controllers(client, testing_ns.metadata.name)
|
34
|
-
|
35
|
-
get_namespaces(client)
|
36
|
-
get_services(client, testing_ns.metadata.name)
|
37
|
-
get_replication_controllers(client, testing_ns.metadata.name)
|
38
|
-
|
39
|
-
delete_services(client, testing_ns.metadata.name, services)
|
40
|
-
delete_replication_controllers(client, testing_ns.metadata.name, replicators)
|
41
|
-
|
42
|
-
client.delete_namespace(testing_ns.metadata.name)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def delete_namespace(client, namespace_name)
|
47
|
-
client.delete_namespace(namespace_name)
|
48
|
-
rescue Kubeclient::ResourceNotFoundError => exception
|
49
|
-
assert_equal(404, exception.error_code)
|
50
|
-
end
|
51
|
-
|
52
|
-
def get_namespaces(client)
|
53
|
-
namespaces = client.get_namespaces
|
54
|
-
assert(true, namespaces.size > 2)
|
55
|
-
end
|
56
|
-
|
57
|
-
def get_services(client, ns)
|
58
|
-
retrieved_services = client.get_services(namespace: ns)
|
59
|
-
assert_equal(3, retrieved_services.size)
|
60
|
-
end
|
61
|
-
|
62
|
-
def get_replication_controllers(client, ns)
|
63
|
-
retrieved_replicators = client.get_replication_controllers(namespace: ns)
|
64
|
-
assert_equal(3, retrieved_replicators.size)
|
65
|
-
end
|
66
|
-
|
67
|
-
def create_services(client, ns)
|
68
|
-
guestbook_service = client.create_service(guestbook_service(ns))
|
69
|
-
redis_service = client.create_service(redis_service(ns))
|
70
|
-
redis_slave_service = client.create_service(redis_slave_service(ns))
|
71
|
-
[guestbook_service, redis_service, redis_slave_service]
|
72
|
-
end
|
73
|
-
|
74
|
-
def create_replication_controllers(client, namespace)
|
75
|
-
rc = client.create_replication_controller(guestbook_rc(namespace))
|
76
|
-
rc2 = client.create_replication_controller(redis_master_rc(namespace))
|
77
|
-
rc3 = client.create_replication_controller(redis_slave_rc(namespace))
|
78
|
-
[rc, rc2, rc3]
|
79
|
-
end
|
80
|
-
|
81
|
-
def delete_services(client, namespace, services)
|
82
|
-
# if the entity is not found, no need to fail the test
|
83
|
-
services.each do |service|
|
84
|
-
begin
|
85
|
-
if service.instance_of?(Kubeclient::Resource)
|
86
|
-
client.delete_service(service.metadata.name, namespace)
|
87
|
-
else
|
88
|
-
# it's just a string - service name
|
89
|
-
client.delete_service(service, namespace)
|
90
|
-
end
|
91
|
-
rescue Kubeclient::ResourceNotFoundError => exception
|
92
|
-
assert_equal(404, exception.error_code)
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
def delete_replication_controllers(client, namespace, replication_controllers)
|
98
|
-
# if the entity is not found, no need to fail the test
|
99
|
-
replication_controllers.each do |rc|
|
100
|
-
begin
|
101
|
-
if rc.instance_of?(Kubeclient::Resource)
|
102
|
-
client.delete_replication_controller(rc.metadata.name, namespace)
|
103
|
-
else
|
104
|
-
# it's just a string - rc name
|
105
|
-
client.delete_replication_controller(rc, namespace)
|
106
|
-
end
|
107
|
-
rescue Kubeclient::ResourceNotFoundError => exception
|
108
|
-
assert_equal(404, exception.error_code)
|
109
|
-
end
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
private
|
114
|
-
|
115
|
-
def construct_base_rc(namespace)
|
116
|
-
rc = Kubeclient::Resource.new
|
117
|
-
rc.metadata = {}
|
118
|
-
rc.metadata.namespace = namespace
|
119
|
-
rc.metadata.labels = {}
|
120
|
-
rc.spec = {}
|
121
|
-
rc.spec.selector = {}
|
122
|
-
rc.spec.template = {}
|
123
|
-
rc.spec.template.metadata = {}
|
124
|
-
rc.spec.template.spec = {}
|
125
|
-
rc.spec.template.metadata.labels = {}
|
126
|
-
rc
|
127
|
-
end
|
128
|
-
|
129
|
-
def redis_master_rc(namespace)
|
130
|
-
rc = construct_base_rc(namespace)
|
131
|
-
rc.metadata.name = 'redis-master'
|
132
|
-
rc.metadata.labels.app = 'redis'
|
133
|
-
rc.metadata.labels.role = 'master'
|
134
|
-
rc.spec.replicas = 1
|
135
|
-
rc.spec.selector.app = 'redis'
|
136
|
-
rc.spec.selector.role = 'master'
|
137
|
-
rc.spec.template.metadata.labels.app = 'redis'
|
138
|
-
rc.spec.template.metadata.labels.role = 'master'
|
139
|
-
rc.spec.template.spec.containers = [{
|
140
|
-
'name' => 'redis-master',
|
141
|
-
'image' => 'redis',
|
142
|
-
'ports' => [{
|
143
|
-
'name' => 'redis-server',
|
144
|
-
'containerPort' => 6379
|
145
|
-
}]
|
146
|
-
}]
|
147
|
-
rc
|
148
|
-
end
|
149
|
-
|
150
|
-
def redis_slave_rc(namespace)
|
151
|
-
rc = construct_base_rc(namespace)
|
152
|
-
rc.metadata.name = 'redis-slave'
|
153
|
-
rc.metadata.labels.app = 'redis'
|
154
|
-
rc.metadata.labels.role = 'slave'
|
155
|
-
rc.spec.replicas = 2
|
156
|
-
rc.spec.selector.app = 'redis'
|
157
|
-
rc.spec.selector.role = 'slave'
|
158
|
-
rc.spec.template.metadata.labels.app = 'redis'
|
159
|
-
rc.spec.template.metadata.labels.role = 'slave'
|
160
|
-
rc.spec.template.spec.containers = [{
|
161
|
-
'name' => 'redis-slave',
|
162
|
-
'image' => 'kubernetes/redis-slave:v2',
|
163
|
-
'ports' => [{
|
164
|
-
'name' => 'redis-server',
|
165
|
-
'containerPort' => 6379
|
166
|
-
}]
|
167
|
-
}]
|
168
|
-
rc
|
169
|
-
end
|
170
|
-
|
171
|
-
def guestbook_rc(namespace)
|
172
|
-
rc = construct_base_rc(namespace)
|
173
|
-
rc.metadata.name = 'guestbook'
|
174
|
-
rc.metadata.labels.app = 'guestbook'
|
175
|
-
rc.metadata.labels.role = 'slave'
|
176
|
-
rc.spec.replicas = 3
|
177
|
-
rc.spec.selector.app = 'guestbook'
|
178
|
-
rc.spec.template.metadata.labels.app = 'guestbook'
|
179
|
-
rc.spec.template.spec.containers = [
|
180
|
-
{
|
181
|
-
'name' => 'guestbook',
|
182
|
-
'image' => 'kubernetes/guestbook:v2',
|
183
|
-
'ports' => [
|
184
|
-
{
|
185
|
-
'name' => 'http-server',
|
186
|
-
'containerPort' => 3000
|
187
|
-
}
|
188
|
-
]
|
189
|
-
}
|
190
|
-
]
|
191
|
-
rc
|
192
|
-
end
|
193
|
-
|
194
|
-
def base_service(namespace)
|
195
|
-
our_service = Kubeclient::Resource.new
|
196
|
-
our_service.metadata = {}
|
197
|
-
our_service.metadata.namespace = namespace
|
198
|
-
our_service.metadata.labels = {}
|
199
|
-
our_service.spec = {}
|
200
|
-
our_service.spec.selector = {}
|
201
|
-
our_service
|
202
|
-
end
|
203
|
-
|
204
|
-
def redis_slave_service(namespace)
|
205
|
-
our_service = base_service(namespace)
|
206
|
-
our_service.metadata.name = 'redis-slave'
|
207
|
-
our_service.metadata.labels.app = 'redis'
|
208
|
-
our_service.metadata.labels.role = 'slave'
|
209
|
-
our_service.spec.ports = [{ 'port' => 6379, 'targetPort' => 'redis-server' }]
|
210
|
-
our_service.spec.selector.app = 'redis'
|
211
|
-
our_service.spec.selector.role = 'slave'
|
212
|
-
our_service
|
213
|
-
end
|
214
|
-
|
215
|
-
def redis_service(namespace)
|
216
|
-
our_service = base_service(namespace)
|
217
|
-
our_service.metadata.name = 'redis-master'
|
218
|
-
our_service.metadata.labels.app = 'redis'
|
219
|
-
our_service.metadata.labels.role = 'master'
|
220
|
-
our_service.spec.ports = [{ 'port' => 6379, 'targetPort' => 'redis-server' }]
|
221
|
-
our_service.spec.selector.app = 'redis'
|
222
|
-
our_service.spec.selector.role = 'master'
|
223
|
-
our_service
|
224
|
-
end
|
225
|
-
|
226
|
-
def guestbook_service(namespace)
|
227
|
-
our_service = base_service(namespace)
|
228
|
-
our_service.metadata.name = 'guestbook'
|
229
|
-
our_service.metadata.labels.name = 'guestbook'
|
230
|
-
our_service.spec.ports = [{ 'port' => 3000, 'targetPort' => 'http-server' }]
|
231
|
-
our_service.spec.selector.app = 'guestbook'
|
232
|
-
our_service.type = 'LoadBalancer'
|
233
|
-
our_service
|
234
|
-
end
|
235
|
-
end
|
data/test/test_helper.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
require 'bundler/setup'
|
2
|
-
require 'minitest/autorun'
|
3
|
-
require 'minitest/rg'
|
4
|
-
require 'webmock/minitest'
|
5
|
-
require 'mocha/minitest'
|
6
|
-
require 'json'
|
7
|
-
require 'kubeclient'
|
8
|
-
|
9
|
-
# Assumes test files will be in a subdirectory with the same name as the
|
10
|
-
# file suffix. e.g. a file named foo.json would be a "json" subdirectory.
|
11
|
-
def open_test_file(name)
|
12
|
-
File.new(File.join(File.dirname(__FILE__), name.split('.').last, name))
|
13
|
-
end
|
14
|
-
|
15
|
-
def stub_core_api_list
|
16
|
-
stub_request(:get, %r{/api/v1$})
|
17
|
-
.to_return(body: open_test_file('core_api_resource_list.json'), status: 200)
|
18
|
-
end
|
data/test/test_kubeclient.rb
DELETED
@@ -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
|