Skip to content

Commit 46d7579

Browse files
committed
Add better unit tests. Add support for watch from resource version.
1 parent ec67474 commit 46d7579

File tree

8 files changed

+70
-9
lines changed

8 files changed

+70
-9
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ gemfile: kubernetes/Gemfile
22
rvm:
33
- 2.6
44

5+
before_install:
6+
- gem update --system
7+
- gem install bundler
8+
59
script:
610
- cd kubernetes
711
- bundle exec rubocop ./src

examples/watch/watch.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@
2020

2121
watch = Kubernetes::Watch.new(client)
2222

23-
watch.connect('/api/v1/namespaces') do |obj|
23+
watch.connect('/api/v1/namespaces', nil) do |obj|
2424
pp obj
2525
end

kubernetes/Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ gemspec
55
group :development, :test do
66
gem 'rake', '~> 12.0.0'
77
gem 'rubocop', '~> 0.65.0'
8+
gem 'webmock', '~> 3.5.1'
89
end

kubernetes/Gemfile.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ GEM
2424
ethon (0.12.0)
2525
ffi (>= 1.3.0)
2626
ffi (1.10.0)
27-
hashdiff (0.3.8)
27+
hashdiff (0.3.9)
2828
jaro_winkler (1.5.2)
2929
json (2.1.0)
3030
parallel (1.14.0)
@@ -58,14 +58,14 @@ GEM
5858
ruby-progressbar (~> 1.7)
5959
unicode-display_width (~> 1.4.0)
6060
ruby-progressbar (1.10.0)
61-
safe_yaml (1.0.4)
61+
safe_yaml (1.0.5)
6262
sys-uname (1.0.4)
6363
ffi (>= 1.0.0)
6464
typhoeus (1.3.1)
6565
ethon (>= 0.9.0)
6666
unicode-display_width (1.4.1)
6767
vcr (3.0.3)
68-
webmock (1.24.6)
68+
webmock (3.5.1)
6969
addressable (>= 2.3.6)
7070
crack (>= 0.3.2)
7171
hashdiff
@@ -83,7 +83,7 @@ DEPENDENCIES
8383
rspec (~> 3.6, >= 3.6.0)
8484
rubocop (~> 0.65.0)
8585
vcr (~> 3.0, >= 3.0.1)
86-
webmock (~> 1.24, >= 1.24.3)
86+
webmock (~> 3.5.1)
8787

8888
BUNDLED WITH
89-
1.16.1
89+
2.0.1

kubernetes/spec/spec_helper.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
require 'kubernetes'
1313
require 'helpers/file_fixtures'
1414

15+
require 'webmock/rspec'
16+
WebMock.disable_net_connect!(allow_localhost: true)
17+
1518
# The following was generated by the `rspec --init` command. Conventionally,
1619
# all specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
1720
# The generated `.rspec` file contains `--require spec_helper` which will cause

kubernetes/spec/watch_spec.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,57 @@
2020
Kubernetes::Watch.new(nil)
2121
end
2222

23+
it 'should connect correctly with resource version' do
24+
config = Kubernetes::Configuration.new
25+
config.scheme = 'http'
26+
config.host = 'k8s.io:8080'
27+
client = Kubernetes::ApiClient.new(config)
28+
29+
WebMock.stub_request(:get, "http://k8s.io:8080/some/path?watch=true&resourceVersion=foo").
30+
with(
31+
headers: {
32+
'Authorization'=>'',
33+
'Content-Type'=>'application/json',
34+
'Expect'=>'',
35+
'User-Agent'=>'Swagger-Codegen/1.0.0-alpha2/ruby'
36+
}).
37+
to_return(status: 200, body: "{}\n", headers: {})
38+
39+
watch = Kubernetes::Watch.new(client)
40+
result = []
41+
watch.connect('/some/path', 'foo') do |obj|
42+
result << obj
43+
end
44+
end
45+
46+
47+
it 'should connect correctly' do
48+
config = Kubernetes::Configuration.new
49+
config.scheme = 'http'
50+
config.host = 'k8s.io:8080'
51+
client = Kubernetes::ApiClient.new(config)
52+
53+
WebMock.stub_request(:get, "http://k8s.io:8080/some/path?watch=true").
54+
with(
55+
headers: {
56+
'Authorization'=>'',
57+
'Content-Type'=>'application/json',
58+
'Expect'=>'',
59+
'User-Agent'=>'Swagger-Codegen/1.0.0-alpha2/ruby'
60+
}).
61+
to_return(status: 200, body: "{ \"foo\": \"bar\" }\n{ \"baz\": \"blah\" }\n{}\n", headers: {})
62+
63+
watch = Kubernetes::Watch.new(client)
64+
result = []
65+
watch.connect('/some/path', nil) do |obj|
66+
result << obj
67+
end
68+
69+
expect(result.length).to eq(3)
70+
expect(result[0]['foo']).to eq('bar')
71+
expect(result[1]['baz']).to eq('blah')
72+
end
73+
2374
it 'should parse chunks correctly' do
2475
client = Kubernetes::Watch.new(nil)
2576
last = ''

kubernetes/src/kubernetes/config/kube_config.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def list_context_names(config_file = KUBE_CONFIG_DEFAULT_LOCATION)
4040
attr_accessor :path
4141
attr_writer :config
4242

43-
def initialize(path, config_hash = nil)
43+
def initialize(path = nil, config_hash = nil)
4444
@path = path
4545
@config = config_hash
4646
end

kubernetes/src/kubernetes/watch.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ def initialize(client)
2323
@client = client
2424
end
2525

26-
def connect(path, &_block)
26+
def connect(path, resource_version, &_block)
2727
opts = { auth_names: ['BearerToken'] }
28-
request = @client.build_request('GET', path + '?watch=true', opts)
28+
query = '?watch=true'
29+
query += "&resourceVersion=#{resource_version}" if resource_version
30+
request = @client.build_request('GET', path + query, opts)
2931
last = ''
3032
request.on_body do |chunk|
3133
last, pieces = split_lines(last, chunk)

0 commit comments

Comments
 (0)