diff --git a/.travis.yml b/.travis.yml index 1c295ead..9cf85299 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,10 @@ gemfile: kubernetes/Gemfile rvm: - 2.6 +before_install: + - gem update --system + - gem install bundler + script: - cd kubernetes - bundle exec rubocop ./src diff --git a/kubernetes/Gemfile b/kubernetes/Gemfile index beefe220..dba8f1ba 100644 --- a/kubernetes/Gemfile +++ b/kubernetes/Gemfile @@ -5,4 +5,5 @@ gemspec group :development, :test do gem 'rake', '~> 12.0.0' gem 'rubocop', '~> 0.65.0' + gem 'webmock', '~> 3.5.1' end diff --git a/kubernetes/Gemfile.lock b/kubernetes/Gemfile.lock index daccf56d..68cf6da3 100644 --- a/kubernetes/Gemfile.lock +++ b/kubernetes/Gemfile.lock @@ -24,7 +24,7 @@ GEM ethon (0.12.0) ffi (>= 1.3.0) ffi (1.10.0) - hashdiff (0.3.8) + hashdiff (0.3.9) jaro_winkler (1.5.2) json (2.1.0) parallel (1.14.0) @@ -58,14 +58,14 @@ GEM ruby-progressbar (~> 1.7) unicode-display_width (~> 1.4.0) ruby-progressbar (1.10.0) - safe_yaml (1.0.4) + safe_yaml (1.0.5) sys-uname (1.0.4) ffi (>= 1.0.0) typhoeus (1.3.1) ethon (>= 0.9.0) unicode-display_width (1.4.1) vcr (3.0.3) - webmock (1.24.6) + webmock (3.5.1) addressable (>= 2.3.6) crack (>= 0.3.2) hashdiff @@ -83,7 +83,7 @@ DEPENDENCIES rspec (~> 3.6, >= 3.6.0) rubocop (~> 0.65.0) vcr (~> 3.0, >= 3.0.1) - webmock (~> 1.24, >= 1.24.3) + webmock (~> 3.5.1) BUNDLED WITH - 1.16.1 + 2.0.1 diff --git a/kubernetes/spec/.rubocop.yml b/kubernetes/spec/.rubocop.yml new file mode 100644 index 00000000..a371e736 --- /dev/null +++ b/kubernetes/spec/.rubocop.yml @@ -0,0 +1,2 @@ +Metrics/BlockLength: + Max: 99 \ No newline at end of file diff --git a/kubernetes/spec/spec_helper.rb b/kubernetes/spec/spec_helper.rb index dcae4db6..293ac14b 100644 --- a/kubernetes/spec/spec_helper.rb +++ b/kubernetes/spec/spec_helper.rb @@ -12,6 +12,9 @@ require 'kubernetes' require 'helpers/file_fixtures' +require 'webmock/rspec' +WebMock.disable_net_connect!(allow_localhost: true) + # The following was generated by the `rspec --init` command. Conventionally, # all specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. # The generated `.rspec` file contains `--require spec_helper` which will cause diff --git a/kubernetes/spec/utils_spec.rb b/kubernetes/spec/utils_spec.rb index 37ef85cf..7ef04688 100644 --- a/kubernetes/spec/utils_spec.rb +++ b/kubernetes/spec/utils_spec.rb @@ -18,8 +18,6 @@ require 'helpers/file_fixtures' require 'kubernetes/utils' - -# rubocop:disable BlockLength describe Kubernetes do describe '.load_incluster_config' do let(:incluster_config) do @@ -115,4 +113,3 @@ end end end -# rubocop:enable BlockLength diff --git a/kubernetes/spec/watch_spec.rb b/kubernetes/spec/watch_spec.rb index 7c3500ef..a3bd2efb 100644 --- a/kubernetes/spec/watch_spec.rb +++ b/kubernetes/spec/watch_spec.rb @@ -20,6 +20,60 @@ Kubernetes::Watch.new(nil) end + it 'should connect correctly with resource version' do + config = Kubernetes::Configuration.new + config.scheme = 'http' + config.host = 'k8s.io:8080' + client = Kubernetes::ApiClient.new(config) + url = 'http://k8s.io:8080/some/path?watch=true&resourceVersion=foo' + + WebMock.stub_request(:get, url) + .with( + headers: { + 'Authorization' => '', + 'Content-Type' => 'application/json', + 'Expect' => '', + 'User-Agent' => 'Swagger-Codegen/1.0.0-alpha2/ruby' + } + ) + .to_return(status: 200, body: "{}\n", headers: {}) + + watch = Kubernetes::Watch.new(client) + result = [] + watch.connect('/some/path', 'foo') do |obj| + result << obj + end + end + + it 'should connect correctly' do + config = Kubernetes::Configuration.new + config.scheme = 'http' + config.host = 'k8s.io:8080' + client = Kubernetes::ApiClient.new(config) + body = "{ \"foo\": \"bar\" }\n{ \"baz\": \"blah\" }\n{}\n" + + WebMock.stub_request(:get, 'http://k8s.io:8080/some/path?watch=true') + .with( + headers: { + 'Authorization' => '', + 'Content-Type' => 'application/json', + 'Expect' => '', + 'User-Agent' => 'Swagger-Codegen/1.0.0-alpha2/ruby' + } + ) + .to_return(status: 200, body: body, headers: {}) + + watch = Kubernetes::Watch.new(client) + result = [] + watch.connect('/some/path', nil) do |obj| + result << obj + end + + expect(result.length).to eq(3) + expect(result[0]['foo']).to eq('bar') + expect(result[1]['baz']).to eq('blah') + end + it 'should parse chunks correctly' do client = Kubernetes::Watch.new(nil) last = '' diff --git a/kubernetes/src/kubernetes/.rubocop.yml b/kubernetes/src/kubernetes/.rubocop.yml new file mode 100644 index 00000000..deca6595 --- /dev/null +++ b/kubernetes/src/kubernetes/.rubocop.yml @@ -0,0 +1,2 @@ +Metrics/MethodLength: + Max: 50 \ No newline at end of file diff --git a/kubernetes/src/kubernetes/config/kube_config.rb b/kubernetes/src/kubernetes/config/kube_config.rb index 79e543db..a5a848dd 100644 --- a/kubernetes/src/kubernetes/config/kube_config.rb +++ b/kubernetes/src/kubernetes/config/kube_config.rb @@ -40,7 +40,7 @@ def list_context_names(config_file = KUBE_CONFIG_DEFAULT_LOCATION) attr_accessor :path attr_writer :config - def initialize(path, config_hash = nil) + def initialize(path = nil, config_hash = nil) @path = path @config = config_hash end diff --git a/kubernetes/src/kubernetes/watch.rb b/kubernetes/src/kubernetes/watch.rb index 80e616a3..e99068ef 100644 --- a/kubernetes/src/kubernetes/watch.rb +++ b/kubernetes/src/kubernetes/watch.rb @@ -23,9 +23,16 @@ def initialize(client) @client = client end - def connect(path, &_block) + def make_url(path, resource_version) + query = '?watch=true' + query += "&resourceVersion=#{resource_version}" if resource_version + path + query + end + + def connect(path, resource_version = nil, &_block) opts = { auth_names: ['BearerToken'] } - request = @client.build_request('GET', path + '?watch=true', opts) + url = make_url(path, resource_version) + request = @client.build_request('GET', url, opts) last = '' request.on_body do |chunk| last, pieces = split_lines(last, chunk)