From 8ee12203f3e31d6b616d7ed2d205fa85cb540c15 Mon Sep 17 00:00:00 2001 From: David Schmitt Date: Wed, 12 Jun 2019 14:53:55 +0100 Subject: [PATCH 1/2] (maint) Fixup Gemfile for JRuby 1.7 installs --- .travis.yml | 2 +- Gemfile | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 3c7a2284..391e4425 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,7 +20,7 @@ matrix: cache: bundler: true directories: ~/.rvm - before_install: rvm use jruby-1.7.26 --install --binary --fuzzy + before_install: rvm use jruby-1.7.26 --install --binary --fuzzy && gem install bundler -v '~> 1.7.0' # disable coverage on jruby9k as this confuses codecov - env: RVM="jruby-9.1.9.0" PUPPET_GEM_VERSION='~> 5' JRUBY_OPTS="--debug" before_cache: pushd ~/.rvm && rm -rf archives rubies/ruby-2.2.7 rubies/ruby-2.3.4 && popd diff --git a/Gemfile b/Gemfile index 922fb4aa..4fac43ca 100644 --- a/Gemfile +++ b/Gemfile @@ -7,16 +7,14 @@ gemspec group :tests do gem 'codecov' - # license_finder does not install on windows using older versions of rubygems. - # ruby 2.4 is confirmed working on appveyor. - gem 'license_finder' if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.4.0') gem 'rake', '~> 10.0' gem 'rspec', '~> 3.0' # rubocop 0.58 throws when testing against ruby 2.1, so pin to the latest, # unless we're dealing with jruby... if RUBY_PLATFORM == 'java' - # load any rubocop version that works on java for the Rakefile - gem 'rubocop' + # load a rubocop version that works on java for the Rakefile + gem 'parser', '2.3.3.1' + gem 'rubocop', '0.41.2' elsif Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.2.0') gem 'rubocop', '0.57.2' # the last version of parallel to support ruby 2.1 @@ -27,6 +25,9 @@ group :tests do # This needs to be removed once we drop puppet4 support. gem 'rubocop', '~> 0.57.0' gem 'rubocop-rspec' + # license_finder does not install on windows using older versions of rubygems. + # ruby 2.4 is confirmed working on appveyor. + gem 'license_finder' if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.4.0') end gem 'simplecov-console' # the test gems required for module testing From c6d1b3a97c2099edd28799a36c4de2eab6883a64 Mon Sep 17 00:00:00 2001 From: David Schmitt Date: Wed, 12 Jun 2019 14:57:23 +0100 Subject: [PATCH 2/2] (FM-7839) Implement 'to_json' method for ResourceShim for bolt bolt's `get_resources` uses the `libexec/query_resources.rb` to retrieve resources as json from the target nodes. Especially https://github.com/puppetlabs/bolt/blob/7dec6e49d66c7f9ae06088d46ee3330de0ed2fed/libexec/query_resources.rb#L71 causes `ResourceShims` to fail as they did not implement `to_json`. This integration test models what `libexec/query_resources.rb` is doing. The added `to_json(*)` method now passes this test, and - shown in manual testing - works for bolt. --- lib/puppet/resource_api/glue.rb | 7 +++++++ spec/integration/resource_api_spec.rb | 4 ++++ spec/puppet/resource_api/glue_spec.rb | 17 +++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/lib/puppet/resource_api/glue.rb b/lib/puppet/resource_api/glue.rb index 8eee448d..30773a08 100644 --- a/lib/puppet/resource_api/glue.rb +++ b/lib/puppet/resource_api/glue.rb @@ -45,6 +45,13 @@ def to_hash values end + def to_json(*) + attrs = filtered_keys.map { |k| [k.to_s, values[k]] unless values[k].nil? } + attributes = Hash[*attrs.compact.flatten] + resource = { title => attributes } + resource.to_json + end + # attribute names that are not title or namevars def filtered_keys values.keys.reject { |k| k == :title || !attr_def[k] || (attr_def[k][:behaviour] == :namevar && @namevars.size == 1) } diff --git a/spec/integration/resource_api_spec.rb b/spec/integration/resource_api_spec.rb index c4ff82ba..b8d1cdf8 100644 --- a/spec/integration/resource_api_spec.rb +++ b/spec/integration/resource_api_spec.rb @@ -223,6 +223,10 @@ def get(_context) describe 'its title' do it { expect(instance.to_resource.title).to eq 'somename' } end + + it 'can serialize to json' do + expect({ 'resources' => [instance.to_resource] }.to_json).to eq '{"resources":[{"somename":{"ensure":"absent","boolean_param":false,"integer_param":99,"float_param":3.21,"ensure_param":"present","variant_pattern_param":"1234ABCD","url_param":"http://www.puppet.com","string_array_param":"d","e":"f","string_param":"default value"}}]}' # rubocop:disable Metrics/LineLength + end end it('ensure is reported as a symbol') { expect(instance[:ensure]).to be_a Symbol } diff --git a/spec/puppet/resource_api/glue_spec.rb b/spec/puppet/resource_api/glue_spec.rb index c31b8c00..b8b656ea 100644 --- a/spec/puppet/resource_api/glue_spec.rb +++ b/spec/puppet/resource_api/glue_spec.rb @@ -47,6 +47,23 @@ end end + describe '.to_json' do + it { expect(instance.to_json).to eq '{"title":{"attr":"value","attr_ro":"fixed"}}' } + + context 'with nil values' do + subject(:instance) do + described_class.new({ namevarname: title, attr: nil, attr_ro: 'fixed' }, 'typename', [:namevarname], + namevarname: { type: 'String', behaviour: :namevar, desc: 'the title' }, + attr: { type: 'String', desc: 'a string parameter' }, + attr_ro: { type: 'String', desc: 'a string readonly', behaviour: :read_only }) + end + + it 'doesn\'t output them' do + expect(instance.to_json).to eq '{"title":{"attr_ro":"fixed"}}' + end + end + end + describe '.to_hierayaml' do it { expect(instance.to_hierayaml).to eq " title:\n attr: value\n attr_ro: fixed\n" }