1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
# frozen_string_literal: true
require_relative 'test_helper'
# Watch entity tests
class TestWatch < Minitest::Test
def test_watch_pod_success
stub_core_api_list
expected = [
{ 'type' => 'ADDED', 'resourceVersion' => '1389' },
{ 'type' => 'MODIFIED', 'resourceVersion' => '1390' },
{ 'type' => 'DELETED', 'resourceVersion' => '1398' }
]
stub_request(:get, %r{/watch/pods})
.to_return(body: open_test_file('watch_stream.json'),
status: 200)
client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
client.watch_pods.to_enum.with_index do |notice, index|
assert_instance_of(Kubeclient::Resource, notice)
assert_equal(expected[index]['type'], notice.type)
assert_equal('Pod', notice.object.kind)
assert_equal('php', notice.object.metadata.name)
assert_equal(expected[index]['resourceVersion'],
notice.object.metadata.resourceVersion)
end
end
def test_watch_pod_block
stub_core_api_list
stub_request(:get, %r{/watch/pods})
.to_return(body: open_test_file('watch_stream.json'),
status: 200)
client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
yielded = []
client.watch_pods { |notice| yielded << notice.type }
assert_equal %w[ADDED MODIFIED DELETED], yielded
end
def test_watch_pod_raw
stub_core_api_list
stub_request(:get, %r{/watch/pods}).to_return(
body: open_test_file('watch_stream.json'),
status: 200
)
client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
got = nil
client.watch_pods(as: :raw).each { |notice| got = notice }
assert_match(/\A{"type":"DELETED"/, got)
end
def test_watch_pod_failure
stub_core_api_list
stub_request(:get, %r{/watch/pods}).to_return(status: 404)
client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
assert_raises(Kubeclient::HttpError) do
client.watch_pods.each do
end
end
end
def test_watch_pod_follow_redirect
stub_core_api_list
redirect = 'http://localhost:1234/api/v1/watch/pods'
stub_request(:get, %r{/watch/pods})
.to_return(status: 302, headers: { location: redirect })
stub_request(:get, redirect).to_return(
body: open_test_file('watch_stream.json'),
status: 200
)
client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
got = nil
client.watch_pods.each { |notice| got = notice }
assert_equal('DELETED', got.type)
end
def test_watch_pod_max_redirect
stub_core_api_list
redirect = 'http://localhost:1234/api/v1/watcher/pods'
stub_request(:get, %r{/watch/pods})
.to_return(status: 302, headers: { location: redirect })
stub_request(:get, redirect).to_return(
body: open_test_file('watch_stream.json'),
status: 200
)
client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1', http_max_redirects: 0)
assert_raises(Kubeclient::HttpError) do
client.watch_pods.each do
end
end
end
def test_watch_pod_api_bearer_token_file_success
stub_core_api_list
file = Tempfile.new('token')
client = Kubeclient::Client.new(
'http://localhost:8080/api/', 'v1',
auth_options: { bearer_token_file: file.path }
)
watcher = client.watch_pods(as: :raw)
begin
file.write("valid_token")
file.rewind
stub_token = stub_request(:get, %r{/watch/pods})
.with(headers: { Authorization: 'Bearer valid_token' })
.to_return(body: open_test_file('watch_stream.json'), status: 200)
got = nil
watcher.each { |notice| got = notice }
assert_match(/\A{"type":"DELETED"/, got)
remove_request_stub(stub_token)
file.write("rotated_token")
file.close
stub_request(:get, %r{/watch/pods})
.with(headers: { Authorization: 'Bearer rotated_token' })
.to_return(body: open_test_file('watch_stream.json'), status: 200)
got = nil
watcher.each { |notice| got = notice }
assert_match(/\A{"type":"DELETED"/, got)
ensure
file.close
file.unlink # deletes the temp file
end
end
def test_watch_pod_api_bearer_token_success
stub_core_api_list
file = Tempfile.new('token')
client = Kubeclient::Client.new(
'http://localhost:8080/api/', 'v1',
auth_options: { bearer_token: "valid_token" }
)
stub_token = stub_request(:get, %r{/watch/pods})
.with(headers: { Authorization: 'Bearer valid_token' })
.to_return(body: open_test_file('watch_stream.json'), status: 200)
got = nil
client.watch_pods(as: :raw).each { |notice| got = notice }
assert_match(/\A{"type":"DELETED"/, got)
end
# Ensure that WatchStream respects a format that's not JSON
def test_watch_stream_text
url = 'http://www.example.com/foobar'
expected_lines = open_test_file('pod_log.txt').read.split("\n")
stub_request(:get, url)
.to_return(body: open_test_file('pod_log.txt'),
status: 200)
stream = Kubeclient::Common::WatchStream.new(URI.parse(url), {}, formatter: ->(v) { v })
stream.to_enum.with_index do |line, index|
assert_instance_of(String, line)
assert_equal(expected_lines[index], line)
end
end
def test_watch_with_resource_version
api_host = 'http://localhost:8080/api'
version = '1995'
stub_core_api_list
stub_request(:get, %r{.*\/watch/events})
.to_return(body: open_test_file('watch_stream.json'),
status: 200)
client = Kubeclient::Client.new(api_host, 'v1')
results = client.watch_events(version).to_enum
assert_equal(3, results.count)
assert_requested(:get,
"#{api_host}/v1/watch/events?resourceVersion=#{version}",
times: 1)
end
def test_watch_with_label_selector
api_host = 'http://localhost:8080/api'
selector = 'name=redis-master'
stub_core_api_list
stub_request(:get, %r{.*\/watch/events})
.to_return(body: open_test_file('watch_stream.json'),
status: 200)
client = Kubeclient::Client.new(api_host, 'v1')
results = client.watch_events(label_selector: selector).to_enum
assert_equal(3, results.count)
assert_requested(:get,
"#{api_host}/v1/watch/events?labelSelector=#{selector}",
times: 1)
end
def test_watch_with_field_selector
api_host = 'http://localhost:8080/api'
selector = 'involvedObject.kind=Pod'
stub_core_api_list
stub_request(:get, %r{.*\/watch/events})
.to_return(body: open_test_file('watch_stream.json'),
status: 200)
client = Kubeclient::Client.new(api_host, 'v1')
results = client.watch_events(field_selector: selector).to_enum
assert_equal(3, results.count)
assert_requested(:get,
"#{api_host}/v1/watch/events?fieldSelector=#{selector}",
times: 1)
end
def test_watch_with_finish_and_ebadf
api_host = 'http://localhost:8080/api'
stub_core_api_list
stub_request(:get, %r{.*\/watch/events})
.to_return(body: open_test_file('watch_stream.json'), status: 200)
client = Kubeclient::Client.new(api_host, 'v1')
watcher = client.watch_events
# explodes when StandardError is not caught
watcher.each do
watcher.finish
raise StandardError
end
assert_requested(:get, "#{api_host}/v1/watch/events", times: 1)
end
end
|