diff --git a/.kitchen.yml b/.kitchen.yml index 5aceb4ae..1b838192 100644 --- a/.kitchen.yml +++ b/.kitchen.yml @@ -1,7 +1,6 @@ --- driver: name: dokken - chef_version: 12.7.2 privileged: true # because Docker and SystemD/Upstart transport: diff --git a/Gemfile b/Gemfile index d2043eea..c66b59fd 100644 --- a/Gemfile +++ b/Gemfile @@ -1,10 +1,10 @@ source 'https://rubygems.org' -gem 'chef', '>= 12.0' +gem 'chef', '>= 12.5.1' group :style do - gem 'foodcritic', '~> 6.0' - gem 'rubocop', '~> 0.36.0' + gem 'foodcritic', '~> 7.0' + gem 'cookstyle' end group :test do diff --git a/Rakefile b/Rakefile index a7d6aac5..7dde208c 100644 --- a/Rakefile +++ b/Rakefile @@ -1,48 +1,71 @@ #!/usr/bin/env rake -# Import other external rake tasks -Dir.glob('tasks/*.rake').each { |r| import r } +# Style tests. cookstyle (rubocop) and Foodcritic +namespace :style do + begin + require 'cookstyle' + require 'rubocop/rake_task' -require 'foodcritic' -require 'rake/clean' -require 'rspec/core/rake_task' -require 'rubocop/rake_task' - -CLEAN.include %w(.kitchen/ coverage/ doc/) -CLOBBER.include %w(Berksfile.lock Gemfile.lock .yardoc/) - -# Default tasks to run when executing `rake` -task default: %w(lint) + desc 'Run Ruby style checks' + RuboCop::RakeTask.new(:ruby) + rescue LoadError => e + puts ">>> Gem load error: #{e}, omitting style:ruby" unless ENV['CI'] + end -# Style tests. Rubocop and Foodcritic -namespace :lint do - desc 'Run Ruby style checks' - RuboCop::RakeTask.new(:ruby) + begin + require 'foodcritic' - desc 'Run Chef style checks' - FoodCritic::Rake::LintTask.new(:chef) do |t| - t.options = { fail_tags: ['any'] } + desc 'Run Chef style checks' + FoodCritic::Rake::LintTask.new(:chef) do |t| + t.options = { + fail_tags: ['any'], + progress: true + } + end + rescue LoadError + puts ">>> Gem load error: #{e}, omitting style:chef" unless ENV['CI'] end end desc 'Run all style checks' -task lint: ['lint:chef', 'lint:ruby'] +task style: ['style:chef', 'style:ruby'] + +# ChefSpec +begin + require 'rspec/core/rake_task' -# Rspec and ChefSpec -desc 'Run ChefSpec examples' -RSpec::Core::RakeTask.new(:spec) do |t| - t.verbose = false + desc 'Run ChefSpec examples' + RSpec::Core::RakeTask.new(:spec) +rescue LoadError => e + puts ">>> Gem load error: #{e}, omitting spec" unless ENV['CI'] end -desc 'Find notes in code' -task :notes do - puts `egrep --exclude=Rakefile --exclude=*.log -n -r -i '(TODO|FIXME|OPTIMIZE)' .` +# Integration tests. Kitchen.ci +namespace :integration do + begin + require 'kitchen/rake_tasks' + + desc 'Run kitchen integration tests' + Kitchen::RakeTasks.new + rescue LoadError => e + puts ">>> Gem load error: #{e}, omitting #{task.name}" unless ENV['CI'] + rescue Kitchen::UserError => e + puts ">>> Test Kitchen error: #{e}" unless ENV['CI'] + rescue Kitchen::UserError => e + puts ">>> Test Kitchen error: #{e}" unless ENV['CI'] + end end -namespace :test do - task :integration do - concurrency = ENV['CONCURRENCY'] || 1 - path = File.join(File.dirname(__FILE__), 'test', 'integration') - sh('sh', '-c', "bundle exec kitchen test -c #{concurrency}") +namespace :supermarket do + begin + require 'stove/rake_task' + + desc 'Publish cookbook to Supermarket with Stove' + Stove::RakeTask.new + rescue LoadError => e + puts ">>> Gem load error: #{e}, omitting #{task.name}" unless ENV['CI'] end end + +# Default +task default: %w(style spec) diff --git a/libraries/collector_classes.rb b/libraries/collector_classes.rb index 04b3d4de..f70a376e 100644 --- a/libraries/collector_classes.rb +++ b/libraries/collector_classes.rb @@ -6,7 +6,7 @@ class Collector # # Used to send inspec reports to Chef Visibility via the data_collector service # - class ChefVisibility # rubocop:disable Metrics/ClassLength + class ChefVisibility @entity_uuid = nil @run_id = nil @blob = [] @@ -111,7 +111,7 @@ def count_controls(profiles) end # Return a json string containing the inspec report to be sent to the data_collector - def enriched_report # rubocop:disable Metrics/AbcSize + def enriched_report return nil unless @blob.is_a?(Hash) && @blob[:reports].is_a?(Hash) final_report = {} node_name = @blob[:node] # ~FC001, ~FC019, ~FC039 @@ -152,7 +152,7 @@ def enriched_report # rubocop:disable Metrics/AbcSize end # Method used in order to send the inspec report to the data_collector server - def send_report # rubocop:disable PerceivedComplexity, Metrics/CyclomaticComplexity + def send_report unless @entity_uuid && @run_id Chef::Log.warn "entity_uuid(#{@entity_uuid}) or run_id(#{@run_id}) can't be nil, not sending report..." return false diff --git a/libraries/helper.rb b/libraries/helper.rb index 23bb3fd4..43ae3244 100644 --- a/libraries/helper.rb +++ b/libraries/helper.rb @@ -32,7 +32,7 @@ def handle_http_error_code(code) end msg = "Received HTTP error #{code}. Please verify the authentication (e.g. token) is set properly." Chef::Log.error msg - fail msg if @raise_if_unreachable + raise msg if @raise_if_unreachable end #rubocop:disable all diff --git a/libraries/profile.rb b/libraries/profile.rb index f705b406..a50016ba 100644 --- a/libraries/profile.rb +++ b/libraries/profile.rb @@ -6,7 +6,7 @@ # `compliance_profile` custom resource to collect and run Chef Compliance # profiles -class ComplianceProfile < Chef::Resource # rubocop:disable Metrics/ClassLength +class ComplianceProfile < Chef::Resource include ComplianceHelpers use_automatic_resource_name @@ -133,7 +133,7 @@ class ComplianceProfile < Chef::Resource # rubocop:disable Metrics/ClassLength supported_schemes = %w{http https supermarket compliance chefserver} if !supported_schemes.include?(URI(path).scheme) && !::File.exist?(path) Chef::Log.warn "No such path! Skipping: #{path}" - fail "Aborting since profile is not present here: #{path}" if run_context.node['audit']['fail_if_not_present'] + raise "Aborting since profile is not present here: #{path}" if run_context.node['audit']['fail_if_not_present'] return end diff --git a/libraries/report.rb b/libraries/report.rb index eefa7d4a..d0e013a8 100644 --- a/libraries/report.rb +++ b/libraries/report.rb @@ -64,7 +64,7 @@ class ComplianceReport < Chef::Resource Chef::Log.warn "#{collector} is not a supported inspec report collector" end - fail "#{total_failed} audits have failed. Aborting chef-client run." if total_failed > 0 && node['audit']['fail_if_any_audits_failed'] + raise "#{total_failed} audits have failed. Aborting chef-client run." if total_failed > 0 && node['audit']['fail_if_any_audits_failed'] end end diff --git a/metadata.rb b/metadata.rb index cfc11645..15446777 100644 --- a/metadata.rb +++ b/metadata.rb @@ -3,8 +3,8 @@ maintainer 'Chef Software, Inc.' maintainer_email 'cookbooks@chef.io' license 'Apache 2.0' -description 'Allows for fetching and executing compliance profiles, and '\ - 'reporting its results' +description 'Allows for fetching and executing compliance profiles, and reporting its results' +long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) version '0.14.2' source_url 'https://github.com/chef-cookbooks/audit' diff --git a/recipes/default.rb b/recipes/default.rb index d1c1d813..730378f6 100644 --- a/recipes/default.rb +++ b/recipes/default.rb @@ -48,7 +48,7 @@ else next if value == false end - fail "Invalid profile name '#{owner_profile}'. "\ + raise "Invalid profile name '#{owner_profile}'. "\ "Must contain /, e.g. 'john/ssh'" if owner_profile !~ %r{\/} o, p = owner_profile.split('/').last(2) diff --git a/travis.yml b/travis.yml deleted file mode 100644 index e6630a6d..00000000 --- a/travis.yml +++ /dev/null @@ -1,8 +0,0 @@ ---- -rvm: -- 2.0.0 -- 2.1.3 -language: ruby -bundler_args: "--without integration" -gemfile: -- Gemfile