2015-09-03 17:25:43 +00:00
|
|
|
#!/usr/bin/env rake
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
require "bundler"
|
|
|
|
require "bundler/gem_helper"
|
|
|
|
require "rake/testtask"
|
|
|
|
require "train"
|
|
|
|
require "fileutils"
|
2023-01-31 16:01:06 +00:00
|
|
|
require_relative "tasks/docs"
|
2015-09-03 17:25:43 +00:00
|
|
|
|
2020-02-10 23:25:09 +00:00
|
|
|
Bundler::GemHelper.install_tasks name: "inspec-core"
|
2019-06-11 22:24:35 +00:00
|
|
|
Bundler::GemHelper.install_tasks name: "inspec"
|
2018-05-03 14:30:01 +00:00
|
|
|
|
2019-04-26 00:50:32 +00:00
|
|
|
task :install do
|
|
|
|
inspec_bin_path = ::File.join(::File.dirname(__FILE__), "inspec-bin")
|
|
|
|
Dir.chdir(inspec_bin_path)
|
|
|
|
sh("rake install")
|
|
|
|
end
|
|
|
|
|
2019-05-18 00:35:30 +00:00
|
|
|
GLOBS = [
|
|
|
|
"test/unit/**/*_test.rb",
|
|
|
|
"test/functional/**/*_test.rb",
|
|
|
|
"lib/plugins/inspec-*/test/**/*_test.rb",
|
2019-06-11 22:24:35 +00:00
|
|
|
].freeze
|
2016-08-25 01:28:43 +00:00
|
|
|
|
2015-09-03 17:25:43 +00:00
|
|
|
# run tests
|
2019-12-03 23:53:38 +00:00
|
|
|
task default: %w{ test }
|
|
|
|
task test: %w{ test:default }
|
2015-09-02 02:13:59 +00:00
|
|
|
|
|
|
|
namespace :test do
|
2019-05-18 00:35:30 +00:00
|
|
|
|
2019-05-18 02:32:18 +00:00
|
|
|
Rake::TestTask.new(:default) do |t|
|
2019-06-11 22:24:35 +00:00
|
|
|
t.libs << "test"
|
2019-05-18 02:32:18 +00:00
|
|
|
t.test_files = Dir[*GLOBS].sort
|
2019-05-31 06:14:00 +00:00
|
|
|
t.warning = !!ENV["W"]
|
2019-05-18 02:32:18 +00:00
|
|
|
t.verbose = !!ENV["V"] # default to off. the test commands are _huge_.
|
2019-06-11 22:24:35 +00:00
|
|
|
t.ruby_opts = ["--dev"] if defined?(JRUBY_VERSION)
|
2019-05-18 02:32:18 +00:00
|
|
|
end
|
2019-06-11 22:24:35 +00:00
|
|
|
task default: [:accept_license]
|
2019-05-18 02:32:18 +00:00
|
|
|
|
|
|
|
begin
|
2019-06-11 22:41:15 +00:00
|
|
|
require "chefstyle"
|
2019-06-11 22:24:35 +00:00
|
|
|
require "rubocop/rake_task"
|
2019-06-11 22:41:15 +00:00
|
|
|
RuboCop::RakeTask.new(:lint) do |task|
|
|
|
|
task.options += ["--display-cop-names", "--no-color", "--parallel"]
|
|
|
|
end
|
2019-05-18 02:32:18 +00:00
|
|
|
rescue LoadError
|
2019-06-11 22:24:35 +00:00
|
|
|
puts "rubocop is not available. Install the rubocop gem to run the lint tests."
|
2019-05-18 02:32:18 +00:00
|
|
|
end
|
2019-05-03 22:39:34 +00:00
|
|
|
|
|
|
|
task :list do
|
|
|
|
puts Dir[*GLOBS].sort
|
|
|
|
end
|
|
|
|
|
2019-09-11 23:43:35 +00:00
|
|
|
# rubocop:disable Style/BlockDelimiters,Layout/ExtraSpacing,Lint/AssignmentInCondition
|
|
|
|
|
|
|
|
def n_threads_run(n_workers, jobs)
|
|
|
|
queue = Queue.new
|
|
|
|
|
|
|
|
jobs.each do |job|
|
|
|
|
queue << job
|
|
|
|
end
|
|
|
|
|
|
|
|
n_workers.times.map {
|
|
|
|
queue << nil # 1 quit value per thread
|
|
|
|
Thread.new do
|
|
|
|
while job = queue.pop # go until quit value
|
|
|
|
yield job
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}.each(&:join)
|
|
|
|
end
|
|
|
|
|
2019-10-17 18:45:27 +00:00
|
|
|
task :parallel do
|
|
|
|
n = (ENV["K"] || 4).to_i
|
2022-06-13 18:24:33 +00:00
|
|
|
warn "Have RUBY_PLATFORM as #{RUBY_PLATFORM}"
|
2019-10-17 18:45:27 +00:00
|
|
|
lock = Mutex.new
|
|
|
|
passed = true
|
|
|
|
|
|
|
|
tests = Dir[*GLOBS].sort
|
|
|
|
tests.concat([nil] * (n - tests.size % n)) unless # square it up
|
|
|
|
tests.size % 4 == 0
|
|
|
|
|
|
|
|
jobs = tests.each_slice(n).to_a.transpose
|
|
|
|
t0 = Time.now
|
|
|
|
out = Queue.new
|
|
|
|
|
|
|
|
n_threads_run n, jobs do |job|
|
|
|
|
job.compact!
|
|
|
|
lock.synchronize do
|
|
|
|
warn "Running #{job.size} files in a thread"
|
|
|
|
end
|
|
|
|
|
|
|
|
t1 = Time.now
|
|
|
|
cmd = "bundle exec minitest #{job.join " "}"
|
|
|
|
output = `#{cmd} 2>&1`
|
|
|
|
t2 = Time.now - t1
|
|
|
|
|
|
|
|
lock.synchronize do
|
|
|
|
if $?.success?
|
|
|
|
warn "Finished #{job.size} files successfully in %d seconds" % [t2]
|
|
|
|
else
|
|
|
|
passed = false
|
|
|
|
warn "Finished #{job.size} files with failures in %d seconds" % [t2]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
out << "#{cmd}\n\n#{output}"
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "done"
|
|
|
|
puts
|
|
|
|
|
|
|
|
out.length.times do
|
|
|
|
puts out.shift
|
|
|
|
puts
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "Ran in %d seconds" % [ Time.now - t0 ]
|
|
|
|
|
|
|
|
exit 1 unless passed
|
|
|
|
end
|
|
|
|
task parallel: [:accept_license] # given isolated being green, why is this needed?
|
|
|
|
|
2020-06-25 13:43:28 +00:00
|
|
|
desc "Run test files in multiple threads"
|
2019-09-12 01:10:28 +00:00
|
|
|
task :isolated do
|
2019-09-11 23:43:35 +00:00
|
|
|
require "fileutils"
|
|
|
|
|
|
|
|
# Only needed for local runs, not CI?
|
|
|
|
FileUtils.rm_rf File.expand_path "~/.inspec"
|
|
|
|
FileUtils.rm_rf File.expand_path "~/.chef"
|
|
|
|
|
|
|
|
# 3 seems to be the magic number... (tho not by that much)
|
2019-10-17 18:44:35 +00:00
|
|
|
bad, good, n = {}, [], (ENV.delete("K") || 3).to_i
|
2019-09-11 23:43:35 +00:00
|
|
|
t0 = Time.now
|
|
|
|
|
|
|
|
tests = Dir[*GLOBS].sort
|
|
|
|
|
|
|
|
n_threads_run n, tests do |path|
|
2019-10-17 18:44:35 +00:00
|
|
|
output = `bundle exec ruby -Ilib -Itest #{path} 2>&1`
|
2019-09-11 23:43:35 +00:00
|
|
|
|
|
|
|
if $?.success?
|
|
|
|
$stderr.print "."
|
|
|
|
good << path
|
|
|
|
else
|
|
|
|
$stderr.print "x"
|
|
|
|
bad[path] = output
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "done"
|
|
|
|
puts "Ran in %d seconds" % [ Time.now - t0 ]
|
|
|
|
|
|
|
|
unless good.empty?
|
|
|
|
puts
|
|
|
|
puts "# Good tests:"
|
|
|
|
good.sort.each do |path|
|
|
|
|
puts path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
unless bad.empty?
|
|
|
|
puts
|
|
|
|
puts "# Bad tests:"
|
|
|
|
bad.keys.each do |path|
|
|
|
|
puts path
|
|
|
|
end
|
|
|
|
puts
|
|
|
|
puts "# Bad Test Output:"
|
|
|
|
bad.each do |path, output|
|
|
|
|
puts
|
|
|
|
puts "# #{path}:"
|
|
|
|
puts output
|
|
|
|
end
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
end
|
2015-09-03 11:22:15 +00:00
|
|
|
|
2019-02-15 03:49:09 +00:00
|
|
|
task :accept_license do
|
2019-06-11 22:24:35 +00:00
|
|
|
FileUtils.mkdir_p(File.join(Dir.home, ".chef", "accepted_licenses"))
|
2019-02-15 03:49:09 +00:00
|
|
|
# If the user has not accepted the license, touch the acceptance
|
|
|
|
# file, but also touch a marker that it is only for testing.
|
2019-06-11 22:24:35 +00:00
|
|
|
unless File.exist?(File.join(Dir.home, ".chef", "accepted_licenses", "inspec"))
|
2019-03-15 17:23:24 +00:00
|
|
|
puts "\n\nTemporarily accepting Chef user license for the duration of testing...\n"
|
2019-06-11 22:24:35 +00:00
|
|
|
FileUtils.touch(File.join(Dir.home, ".chef", "accepted_licenses", "inspec"))
|
|
|
|
FileUtils.touch(File.join(Dir.home, ".chef", "accepted_licenses", "inspec.for_testing"))
|
2019-02-15 03:49:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Regardless of what happens, when this process exits, check for cleanup.
|
|
|
|
at_exit do
|
2019-06-11 22:24:35 +00:00
|
|
|
if File.exist?(File.join(Dir.home, ".chef", "accepted_licenses", "inspec.for_testing"))
|
2019-03-15 17:23:24 +00:00
|
|
|
puts "\n\nRemoving temporary Chef user license acceptance file that was placed for test duration.\n"
|
2019-06-11 22:24:35 +00:00
|
|
|
FileUtils.rm_f(File.join(Dir.home, ".chef", "accepted_licenses", "inspec"))
|
|
|
|
FileUtils.rm_f(File.join(Dir.home, ".chef", "accepted_licenses", "inspec.for_testing"))
|
2019-02-15 03:49:09 +00:00
|
|
|
end
|
|
|
|
end
|
2019-02-06 19:31:51 +00:00
|
|
|
end
|
|
|
|
|
2016-03-16 07:22:46 +00:00
|
|
|
Rake::TestTask.new(:functional) do |t|
|
2019-06-11 22:24:35 +00:00
|
|
|
t.libs << "test"
|
2018-09-18 04:00:54 +00:00
|
|
|
t.test_files = Dir.glob([
|
2019-06-11 22:24:35 +00:00
|
|
|
"test/functional/**/*_test.rb",
|
|
|
|
"lib/plugins/inspec-*/test/functional/**/*_test.rb",
|
2018-09-18 04:00:54 +00:00
|
|
|
])
|
2019-05-31 06:14:00 +00:00
|
|
|
t.warning = !!ENV["W"]
|
2019-05-18 20:26:15 +00:00
|
|
|
t.verbose = !!ENV["V"] # default to off. the test commands are _huge_.
|
2019-06-11 22:24:35 +00:00
|
|
|
t.ruby_opts = ["--dev"] if defined?(JRUBY_VERSION)
|
2016-03-16 07:22:46 +00:00
|
|
|
end
|
2019-02-06 19:31:51 +00:00
|
|
|
# Inject a prerequisite task
|
2019-06-11 22:24:35 +00:00
|
|
|
task functional: [:accept_license]
|
2016-03-16 07:22:46 +00:00
|
|
|
|
2019-05-17 22:42:08 +00:00
|
|
|
Rake::TestTask.new(:unit) do |t|
|
2019-06-11 22:24:35 +00:00
|
|
|
t.libs << "test"
|
2019-05-17 22:42:08 +00:00
|
|
|
t.test_files = Dir.glob([
|
2019-06-11 22:24:35 +00:00
|
|
|
"test/unit/**/*_test.rb",
|
|
|
|
"lib/plugins/inspec-*/test/unit/**/*_test.rb",
|
2019-05-17 22:42:08 +00:00
|
|
|
])
|
2019-05-31 06:14:00 +00:00
|
|
|
t.warning = !!ENV["W"]
|
2019-05-18 20:26:15 +00:00
|
|
|
t.verbose = !!ENV["V"] # default to off. the test commands are _huge_.
|
2019-06-11 22:24:35 +00:00
|
|
|
t.ruby_opts = ["--dev"] if defined?(JRUBY_VERSION)
|
2018-09-12 22:04:16 +00:00
|
|
|
end
|
2019-02-06 19:31:51 +00:00
|
|
|
# Inject a prerequisite task
|
2019-06-11 22:24:35 +00:00
|
|
|
task unit: [:accept_license]
|
2015-11-26 17:58:53 +00:00
|
|
|
end
|
|
|
|
|
2023-01-31 15:07:38 +00:00
|
|
|
# NOTE: Rakefile clean-up was done in PR #6367 (https://github.com/inspec/inspec/pull/6367)
|