From 21748811a56d7f61c5e39957aee777d749a7e29d Mon Sep 17 00:00:00 2001 From: Alex Pop Date: Mon, 2 Mar 2020 17:11:54 +0000 Subject: [PATCH] Changes based on feedback Update tests to pass travs Signed-off-by: Alex Pop --- .travis.yml | 33 +++++++++---------- libraries/helper.rb | 4 +++ libraries/matchers.rb | 3 -- spec/unit/recipes/default_spec.rb | 2 +- spec/unit/report/fetcher_spec.rb | 6 ++-- .../test_helper/recipes/force_inspec_core.rb | 20 ++++++----- .../test_helper/recipes/install_inspec.rb | 20 ++++++----- .../recipes/setup_waiver_fixture.rb | 15 +++------ test/integration/inspec-attributes/default.rb | 2 +- test/integration/waivers/default.rb | 8 ++--- .../recipes/bootstrap_localhost.rb | 6 ++-- .../recipes/configure_services.rb | 2 +- .../recipes/converge_localhost.rb | 4 +-- 13 files changed, 62 insertions(+), 63 deletions(-) delete mode 100644 libraries/matchers.rb diff --git a/.travis.yml b/.travis.yml index fda73c3c..6dee27de 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,62 +28,59 @@ before_install: - CHEF_LICENSE="accept-no-persist" sudo chef gem install webmock - eval "$(chef shell-init bash)" - chef --version -- cookstyle --version -- foodcritic --version -- kitchen --version - kitchen list matrix: include: # Run the style, unit and chefspec tests - - rvm: 2.6.3 + - rvm: 2.6.5 script: rake # Run integration tests with test-kitchen - - rvm: 2.6.3 + - rvm: 2.6.5 script: rake $SUITE env: SUITE=test:integration OS='default-centos-7' - - rvm: 2.6.3 + - rvm: 2.6.5 script: rake $SUITE env: SUITE=test:integration OS='default-ubuntu-1804' - - rvm: 2.6.3 + - rvm: 2.6.5 script: rake $SUITE env: SUITE=test:integration OS='missing-profile-no-fail-ubuntu-1804' - - rvm: 2.6.3 + - rvm: 2.6.5 script: rake $SUITE && exit 1 || echo "OK" env: SUITE=test:integration OS='missing-profile-fail-ubuntu-1804' # Next 5 suites are disabled due to an issue with the gem() inspec resource # see https://github.com/chef-cookbooks/audit/issues/406 # - # - rvm: 2.6.3 + # - rvm: 2.6.5 # script: rake $SUITE # env: SUITE=test:integration OS='chef15-compatible-inspec-ubuntu-1804' # Next 4 suites test running inspec 3 or 4 on audit cookbook. This is only permitted on chef 14 or older. - # - rvm: 2.6.3 + # - rvm: 2.6.5 # script: rake $SUITE # env: SUITE=test:integration OS='gem-install-core-version4-centos-7' CHEF_VERSION=14 - # - rvm: 2.6.3 + # - rvm: 2.6.5 # script: rake $SUITE # env: SUITE=test:integration OS='gem-install-core-version3-centos-7' CHEF_VERSION=14 - # - rvm: 2.6.3 + # - rvm: 2.6.5 # script: rake $SUITE # env: SUITE=test:integration OS='gem-install-core-version4-ubuntu-1804' CHEF_VERSION=14 - # - rvm: 2.6.3 + # - rvm: 2.6.5 # script: rake $SUITE # env: SUITE=test:integration OS='gem-install-core-version3-ubuntu-1804' CHEF_VERSION=14 - - rvm: 2.6.3 + - rvm: 2.6.5 script: rake $SUITE env: SUITE=test:integration OS='inspec-attributes-ubuntu-1804' - - rvm: 2.6.3 + - rvm: 2.6.5 script: rake $SUITE env: SUITE=test:integration OS='chef-node-enabled-ubuntu-1804' - - rvm: 2.6.3 + - rvm: 2.6.5 script: rake $SUITE env: SUITE=test:integration OS='chef-node-disabled-ubuntu-1804' - - rvm: 2.6.3 + - rvm: 2.6.5 script: rake $SUITE env: SUITE=test:integration OS='hash-centos-7' - - rvm: 2.6.3 + - rvm: 2.6.5 script: rake $SUITE env: SUITE=test:integration OS='hash-ubuntu-1804' diff --git a/libraries/helper.rb b/libraries/helper.rb index d2203469..30dc5962 100644 --- a/libraries/helper.rb +++ b/libraries/helper.rb @@ -136,6 +136,10 @@ def load_audit_handler # taking node['audit'] as parameter so that it can be called from the chef-server fetcher as well # audit['collector'] is the legacy reporter, def get_reporters(audit) + if audit.nil? + Chef::Log.warn("node ['audit'] is not defined") + return [] + end if audit['collector'] Chef::Log.warn("node ['audit']['collector'] is deprecated and will be removed from the next major version of the cookbook. Please use node ['audit']['reporter']") return handle_reporters(audit['collector']) diff --git a/libraries/matchers.rb b/libraries/matchers.rb deleted file mode 100644 index 2131000c..00000000 --- a/libraries/matchers.rb +++ /dev/null @@ -1,3 +0,0 @@ -# encoding: utf-8 - -# used by ChefSpec diff --git a/spec/unit/recipes/default_spec.rb b/spec/unit/recipes/default_spec.rb index 116739fb..648bb01d 100644 --- a/spec/unit/recipes/default_spec.rb +++ b/spec/unit/recipes/default_spec.rb @@ -22,7 +22,7 @@ describe 'audit::default' do context 'When all attributes are default, on an unspecified platform' do let(:chef_run) do - runner = ChefSpec::ServerRunner.new(platform: 'ubuntu', version: '14.04') + runner = ChefSpec::ServerRunner.new(platform: 'ubuntu', version: '18.04') runner.converge(described_recipe) end diff --git a/spec/unit/report/fetcher_spec.rb b/spec/unit/report/fetcher_spec.rb index 3fcfae06..6c5d7b2d 100644 --- a/spec/unit/report/fetcher_spec.rb +++ b/spec/unit/report/fetcher_spec.rb @@ -91,10 +91,10 @@ }, 'profile' => ['admin', 'linux-baseline', '2.0'], } - - Fetchers::Url.new('non_profile_url', config).send(:http_opts) + mynode.default['audit']['reporter'] = 'chef-server' + ChefServer::Fetcher.target_url('non_profile_url', config).read } - expect { myproc.call }.to raise_error(RuntimeError) + expect { myproc.call }.to raise_error(Errno::ECONNREFUSED) end end end diff --git a/test/cookbooks/test_helper/recipes/force_inspec_core.rb b/test/cookbooks/test_helper/recipes/force_inspec_core.rb index 33c7c477..f406ff95 100644 --- a/test/cookbooks/test_helper/recipes/force_inspec_core.rb +++ b/test/cookbooks/test_helper/recipes/force_inspec_core.rb @@ -1,16 +1,20 @@ chef_gem 'inspec-core' do - action :nothing -end.run_action(:remove) + compile_time true + action :remove +end chef_gem 'inspec-core-bin' do - action :nothing -end.run_action(:remove) + compile_time true + action :remove +end chef_gem 'inspec' do - action :nothing -end.run_action(:remove) + compile_time true + action :remove +end chef_gem 'inspec-core' do + compile_time true version '4.3.2' - action :nothing -end.run_action(:install) + action :install +end diff --git a/test/cookbooks/test_helper/recipes/install_inspec.rb b/test/cookbooks/test_helper/recipes/install_inspec.rb index 121d270b..22cb8b14 100644 --- a/test/cookbooks/test_helper/recipes/install_inspec.rb +++ b/test/cookbooks/test_helper/recipes/install_inspec.rb @@ -1,16 +1,20 @@ chef_gem 'inspec-core' do - action :nothing -end.run_action(:remove) + compile_time true + action :remove +end chef_gem 'inspec-core-bin' do - action :nothing -end.run_action(:remove) + compile_time true + action :remove +end chef_gem 'inspec' do - action :nothing -end.run_action(:remove) + compile_time true + action :remove +end chef_gem 'inspec' do + compile_time true version '1.19.1' - action :nothing -end.run_action(:install) + action :install +end diff --git a/test/cookbooks/test_helper/recipes/setup_waiver_fixture.rb b/test/cookbooks/test_helper/recipes/setup_waiver_fixture.rb index 3753e662..d6260a59 100644 --- a/test/cookbooks/test_helper/recipes/setup_waiver_fixture.rb +++ b/test/cookbooks/test_helper/recipes/setup_waiver_fixture.rb @@ -1,19 +1,12 @@ -[ - "controls", - "waivers", -].each do |subdir| - directory "#{node["audit"]["profiles"]["waiver-test-profile"]["path"]}/#{subdir}" do +%w(controls waivers).each do |subdir| + directory "#{node['audit']['profiles']['waiver-test-profile']['path']}/#{subdir}" do recursive true end end -[ - "inspec.yml", - "controls/waiver-check.rb", - "waivers/waivers.yaml", -].each do |file| - cookbook_file "#{node["audit"]["profiles"]["waiver-test-profile"]["path"]}/#{file}" do +%w(inspec.yml controls/waiver-check.rb waivers/waivers.yaml).each do |file| + cookbook_file "#{node['audit']['profiles']['waiver-test-profile']['path']}/#{file}" do source "waivers-fixture/#{file}" end end diff --git a/test/integration/inspec-attributes/default.rb b/test/integration/inspec-attributes/default.rb index 4de7a343..0d004974 100644 --- a/test/integration/inspec-attributes/default.rb +++ b/test/integration/inspec-attributes/default.rb @@ -9,7 +9,7 @@ results << r end end -attribute_control = results.find { |x| x['code_desc'] == 'File /opt/kitchen/cache/attribute-file-exists.test should exist' } +attribute_control = results.find { |x| x['code_desc'] == 'File /opt/kitchen/cache/attribute-file-exists.test is expected to exist' } attribute_control = {} if attribute_control.nil? describe 'attribute control' do diff --git a/test/integration/waivers/default.rb b/test/integration/waivers/default.rb index ecf73f5f..c9117c6a 100644 --- a/test/integration/waivers/default.rb +++ b/test/integration/waivers/default.rb @@ -5,14 +5,14 @@ # The test fixture has two controls - the first should pass, # the second should be a skip with a waiver justification -control "the unwaivered control" do +control 'the unwaivered control' do describe controls[0]['results'][0]['status'] do - it { should cmp "passed" } + it { should cmp 'passed' } end end -control "the waivered control" do +control 'the waivered control' do describe controls[1]['results'][0]['status'] do - it { should cmp "skipped" } + it { should cmp 'skipped' } end end diff --git a/test/kitchen-automate/recipes/bootstrap_localhost.rb b/test/kitchen-automate/recipes/bootstrap_localhost.rb index af0feeee..141509dd 100644 --- a/test/kitchen-automate/recipes/bootstrap_localhost.rb +++ b/test/kitchen-automate/recipes/bootstrap_localhost.rb @@ -24,8 +24,8 @@ end # Ensure the node is in the expected state -raise 'Cannot find /tmp/kitchen/client.pem' unless File.exist?('/tmp/kitchen/client.pem') -raise 'Cannot find /home/ec2-user/.ssh/authorized_keys' unless File.exist?('/home/ec2-user/.ssh/authorized_keys') +raise 'Cannot find /tmp/kitchen/client.pem' unless ::File.exist?('/tmp/kitchen/client.pem') +raise 'Cannot find /home/ec2-user/.ssh/authorized_keys' unless ::File.exist?('/home/ec2-user/.ssh/authorized_keys') # Add a public key to be used for SSH bootstrapping execute 'Add SSH public key to be used by self bootstrapping' do @@ -49,5 +49,5 @@ EOH live_stream true action :run - not_if { File.exist?('/etc/chef/client.pem') } + not_if { ::File.exist?('/etc/chef/client.pem') } end diff --git a/test/kitchen-automate/recipes/configure_services.rb b/test/kitchen-automate/recipes/configure_services.rb index 9d904bda..178b465b 100644 --- a/test/kitchen-automate/recipes/configure_services.rb +++ b/test/kitchen-automate/recipes/configure_services.rb @@ -33,5 +33,5 @@ touch /etc/opscode/chef-server-reconfigured.txt EOH action :run - not_if { File.exist?('/etc/opscode/chef-server-reconfigured.txt') } + not_if { ::File.exist?('/etc/opscode/chef-server-reconfigured.txt') } end diff --git a/test/kitchen-automate/recipes/converge_localhost.rb b/test/kitchen-automate/recipes/converge_localhost.rb index 33f96bfe..39c24445 100644 --- a/test/kitchen-automate/recipes/converge_localhost.rb +++ b/test/kitchen-automate/recipes/converge_localhost.rb @@ -42,7 +42,7 @@ EOH live_stream true action :run - only_if { File.exist?('/etc/chef/client.pem') } + only_if { ::File.exist?('/etc/chef/client.pem') } end # Create the attributes file to test the 'chef-visibility' collector @@ -91,5 +91,5 @@ EOH live_stream true action :run - only_if { File.exist?('/etc/chef/client.pem') } + only_if { ::File.exist?('/etc/chef/client.pem') } end