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_relative "tasks/maintainers"
|
|
|
|
require_relative "tasks/spdx"
|
|
|
|
require "fileutils"
|
2015-09-03 17:25:43 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
Bundler::GemHelper.install_tasks name: "inspec"
|
2018-05-03 14:30:01 +00:00
|
|
|
|
2017-06-28 12:55:01 +00:00
|
|
|
def prompt(message)
|
|
|
|
print(message)
|
|
|
|
STDIN.gets.chomp
|
|
|
|
end
|
|
|
|
|
2017-04-27 18:08:28 +00:00
|
|
|
# The docs tasks rely on ruby-progressbar. If we can't load it, then don't
|
|
|
|
# load the docs tasks. This is necessary to allow this Rakefile to work
|
|
|
|
# when the "tests" gem group in the Gemfile has been excluded, such as
|
|
|
|
# during an appbundle-updater run.
|
|
|
|
begin
|
2019-06-11 22:24:35 +00:00
|
|
|
require "ruby-progressbar"
|
|
|
|
require_relative "tasks/docs"
|
2017-04-27 18:08:28 +00:00
|
|
|
rescue LoadError
|
2019-06-11 22:24:35 +00:00
|
|
|
puts "docs tasks are unavailable because the ruby-progressbar gem is not available."
|
2015-09-03 17:25:43 +00:00
|
|
|
end
|
|
|
|
|
2018-08-02 15:43:46 +00:00
|
|
|
begin
|
2019-06-11 22:24:35 +00:00
|
|
|
require "git"
|
|
|
|
require_relative "tasks/contrib"
|
2018-08-02 15:43:46 +00:00
|
|
|
rescue LoadError
|
2019-06-11 22:24:35 +00:00
|
|
|
puts "contrib tasks are unavailable because the git gem is not available."
|
2018-08-02 15:43:46 +00:00
|
|
|
end
|
|
|
|
|
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-06-11 22:24:35 +00:00
|
|
|
task default: ["test:lint", "test:default"]
|
2019-06-12 19:16:07 +00:00
|
|
|
task test: ["test:lint", "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-07-27 20:03:52 +00:00
|
|
|
task :missing do
|
|
|
|
missing = Dir["test/**/*"] - Dir[*GLOBS]
|
|
|
|
|
|
|
|
missing.reject! { |f| ! File.file? f }
|
|
|
|
missing.reject! { |f| f =~ %r{test/(integration|cookbooks)} }
|
|
|
|
missing.reject! { |f| f =~ %r{test/unit/mock} }
|
2019-08-19 17:52:28 +00:00
|
|
|
missing.reject! { |f| f =~ /test.*helper/ }
|
2019-07-27 20:03:52 +00:00
|
|
|
missing.reject! { |f| f =~ %r{test/docker} }
|
|
|
|
|
|
|
|
puts missing.sort
|
|
|
|
end
|
|
|
|
|
2015-09-02 02:13:59 +00:00
|
|
|
task :isolated do
|
2019-05-03 22:39:34 +00:00
|
|
|
failures = Dir[*GLOBS]
|
2019-06-11 22:24:35 +00:00
|
|
|
failures.reject! do |file|
|
|
|
|
system(Gem.ruby, "-Ilib:test", file)
|
|
|
|
end
|
2019-05-03 22:39:34 +00:00
|
|
|
|
|
|
|
unless failures.empty?
|
|
|
|
puts "These test files failed:\n"
|
|
|
|
puts failures
|
|
|
|
raise "broken tests..."
|
|
|
|
end
|
2015-09-02 02:13:59 +00:00
|
|
|
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]
|
2018-09-12 22:04:16 +00:00
|
|
|
|
2015-09-14 08:28:07 +00:00
|
|
|
task :resources do
|
2019-06-11 22:24:35 +00:00
|
|
|
tests = Dir["test/unit/resource/*_test.rb"]
|
2015-09-03 11:22:15 +00:00
|
|
|
return if tests.empty?
|
2019-07-09 00:20:30 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
sh(Gem.ruby, "test/docker_test.rb", *tests)
|
2015-09-03 11:22:15 +00:00
|
|
|
end
|
2015-10-21 20:52:41 +00:00
|
|
|
|
2018-09-25 19:53:26 +00:00
|
|
|
task :integration, [:os] do |task, args|
|
2019-06-11 22:24:35 +00:00
|
|
|
concurrency = ENV["CONCURRENCY"] || 1
|
|
|
|
os = args[:os] || ENV["OS"] || ""
|
|
|
|
ENV["DOCKER"] = "true" if ENV["docker"].nil?
|
2019-05-15 22:46:09 +00:00
|
|
|
sh("bundle exec kitchen test -c #{concurrency} #{os}")
|
2015-10-21 20:52:41 +00:00
|
|
|
end
|
2019-02-06 19:31:51 +00:00
|
|
|
# Inject a prerequisite task
|
2019-06-11 22:24:35 +00:00
|
|
|
task 'integration': [:accept_license]
|
2015-12-18 21:54:56 +00:00
|
|
|
|
|
|
|
task :ssh, [:target] do |_t, args|
|
2019-06-11 22:24:35 +00:00
|
|
|
tests_path = File.join(File.dirname(__FILE__), "test", "integration", "test", "integration", "default")
|
|
|
|
key_files = ENV["key_files"] || File.join(ENV["HOME"], ".ssh", "id_rsa")
|
2015-12-18 21:54:56 +00:00
|
|
|
|
|
|
|
sh_cmd = "bin/inspec exec #{tests_path}/"
|
2019-07-09 00:20:30 +00:00
|
|
|
sh_cmd += ENV["test"] ? "#{ENV["test"]}_spec.rb" : "*"
|
2019-06-11 22:24:35 +00:00
|
|
|
sh_cmd += " --sudo" unless args[:target].split("@")[0] == "root"
|
2015-12-18 21:54:56 +00:00
|
|
|
sh_cmd += " -t ssh://#{args[:target]}"
|
|
|
|
sh_cmd += " --key_files=#{key_files}"
|
2019-07-09 00:20:30 +00:00
|
|
|
sh_cmd += " --format=#{ENV["format"]}" if ENV["format"]
|
2015-12-18 21:54:56 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
sh("sh", "-c", sh_cmd)
|
2015-12-18 21:54:56 +00:00
|
|
|
end
|
2018-02-07 14:53:21 +00:00
|
|
|
|
|
|
|
project_dir = File.dirname(__FILE__)
|
2017-12-15 06:37:36 +00:00
|
|
|
namespace :aws do
|
2019-06-11 22:24:35 +00:00
|
|
|
%w{default minimal}.each do |account|
|
|
|
|
integration_dir = File.join(project_dir, "test", "integration", "aws", account)
|
|
|
|
attribute_file = File.join(integration_dir, ".attribute.yml")
|
2018-02-26 16:10:04 +00:00
|
|
|
|
2017-12-15 06:37:36 +00:00
|
|
|
task :"setup:#{account}", :tf_workspace do |t, args|
|
2019-06-11 22:24:35 +00:00
|
|
|
tf_workspace = args[:tf_workspace] || ENV["INSPEC_TERRAFORM_ENV"]
|
2017-12-15 06:37:36 +00:00
|
|
|
abort("You must either call the top-level test:aws:#{account} task, or set the INSPEC_TERRAFORM_ENV variable.") unless tf_workspace
|
|
|
|
puts "----> Setup"
|
2019-06-11 22:24:35 +00:00
|
|
|
abort("You must set the environment variable AWS_REGION") unless ENV["AWS_REGION"]
|
2017-12-15 06:37:36 +00:00
|
|
|
puts "----> Checking for required AWS profile..."
|
|
|
|
sh("aws configure get aws_access_key_id --profile inspec-aws-test-#{account} > /dev/null")
|
2018-04-19 17:01:27 +00:00
|
|
|
sh("cd #{integration_dir}/build/ && terraform init -upgrade")
|
2017-12-15 06:37:36 +00:00
|
|
|
sh("cd #{integration_dir}/build/ && terraform workspace new #{tf_workspace}")
|
2018-04-19 17:01:27 +00:00
|
|
|
sh("cd #{integration_dir}/build/ && AWS_PROFILE=inspec-aws-test-#{account} terraform plan -out inspec-aws-#{account}.plan")
|
|
|
|
sh("cd #{integration_dir}/build/ && AWS_PROFILE=inspec-aws-test-#{account} terraform apply -auto-approve inspec-aws-#{account}.plan")
|
2017-12-15 06:37:36 +00:00
|
|
|
Rake::Task["test:aws:dump_attrs:#{account}"].execute
|
|
|
|
end
|
2018-02-26 16:10:04 +00:00
|
|
|
|
2017-12-15 06:37:36 +00:00
|
|
|
task :"dump_attrs:#{account}" do
|
|
|
|
sh("cd #{integration_dir}/build/ && AWS_PROFILE=inspec-aws-test-#{account} terraform output > #{attribute_file}")
|
|
|
|
raw_output = File.read(attribute_file)
|
|
|
|
yaml_output = raw_output.gsub(" = ", " : ")
|
2019-06-11 22:24:35 +00:00
|
|
|
File.open(attribute_file, "w") { |file| file.puts yaml_output }
|
2017-12-15 06:37:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
task :"run:#{account}" do
|
|
|
|
puts "----> Run"
|
2018-02-08 04:26:37 +00:00
|
|
|
sh("bundle exec inspec exec #{integration_dir}/verify -t aws://${AWS_REGION}/inspec-aws-test-#{account} --attrs #{attribute_file}")
|
2017-12-15 06:37:36 +00:00
|
|
|
end
|
2018-02-26 16:10:04 +00:00
|
|
|
|
2017-12-15 06:37:36 +00:00
|
|
|
task :"cleanup:#{account}", :tf_workspace do |t, args|
|
2019-06-11 22:24:35 +00:00
|
|
|
tf_workspace = args[:tf_workspace] || ENV["INSPEC_TERRAFORM_ENV"]
|
2017-12-15 06:37:36 +00:00
|
|
|
abort("You must either call the top-level test:aws:#{account} task, or set the INSPEC_TERRAFORM_ENV variable.") unless tf_workspace
|
|
|
|
puts "----> Cleanup"
|
|
|
|
sh("cd #{integration_dir}/build/ && AWS_PROFILE=inspec-aws-test-#{account} terraform destroy -force")
|
|
|
|
sh("cd #{integration_dir}/build/ && terraform workspace select default")
|
|
|
|
sh("cd #{integration_dir}/build && terraform workspace delete #{tf_workspace}")
|
|
|
|
end
|
2018-02-26 16:10:04 +00:00
|
|
|
|
2017-12-15 06:37:36 +00:00
|
|
|
task :"#{account}" do
|
2019-06-11 22:24:35 +00:00
|
|
|
tf_workspace = ENV["INSPEC_TERRAFORM_ENV"] || prompt("Please enter a workspace for your integration tests to run in: ")
|
2017-12-15 06:37:36 +00:00
|
|
|
begin
|
2019-06-11 22:24:35 +00:00
|
|
|
Rake::Task["test:aws:setup:#{account}"].execute({ tf_workspace: tf_workspace })
|
2017-12-15 06:37:36 +00:00
|
|
|
Rake::Task["test:aws:run:#{account}"].execute
|
|
|
|
rescue
|
|
|
|
abort("Integration testing has failed for the #{account} account")
|
|
|
|
ensure
|
2019-06-11 22:24:35 +00:00
|
|
|
Rake::Task["test:aws:cleanup:#{account}"].execute({ tf_workspace: tf_workspace })
|
2017-12-15 06:37:36 +00:00
|
|
|
end
|
|
|
|
end
|
2017-07-05 20:30:27 +00:00
|
|
|
end
|
2017-02-16 20:22:33 +00:00
|
|
|
end
|
2018-02-08 15:44:11 +00:00
|
|
|
desc "Perform AWS Integration Tests"
|
2019-07-09 00:20:30 +00:00
|
|
|
task aws: %i{aws:default aws:minimal}
|
2018-02-08 15:44:11 +00:00
|
|
|
|
2018-02-06 18:40:21 +00:00
|
|
|
namespace :azure do
|
|
|
|
# Specify the directory for the integration tests
|
2019-06-11 22:24:35 +00:00
|
|
|
integration_dir = File.join(project_dir, "test", "integration", "azure")
|
|
|
|
tf_vars_file = File.join(integration_dir, "build", "terraform.tfvars")
|
|
|
|
attribute_file = File.join(integration_dir, ".attribute.yml")
|
2018-02-06 18:40:21 +00:00
|
|
|
|
2018-02-26 21:37:36 +00:00
|
|
|
task :setup, :tf_workspace do |t, args|
|
2019-06-11 22:24:35 +00:00
|
|
|
tf_workspace = args[:tf_workspace] || ENV["INSPEC_TERRAFORM_ENV"]
|
2018-02-26 21:37:36 +00:00
|
|
|
abort("You must either call the top-level test:azure task, or set the INSPEC_TERRAFORM_ENV variable.") unless tf_workspace
|
2018-04-29 18:00:12 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
puts "----> Setup Terraform Workspace"
|
2018-04-29 18:00:12 +00:00
|
|
|
|
2018-04-19 17:01:27 +00:00
|
|
|
sh("cd #{integration_dir}/build/ && terraform init -upgrade")
|
2018-02-26 21:37:36 +00:00
|
|
|
sh("cd #{integration_dir}/build/ && terraform workspace new #{tf_workspace}")
|
2018-02-06 18:40:21 +00:00
|
|
|
|
2018-04-29 18:00:12 +00:00
|
|
|
Rake::Task["test:azure:vars"].execute
|
|
|
|
Rake::Task["test:azure:plan"].execute
|
|
|
|
Rake::Task["test:azure:apply"].execute
|
|
|
|
end
|
|
|
|
|
|
|
|
desc "Generate terraform.tfvars file"
|
|
|
|
task :vars do |t, args|
|
|
|
|
|
|
|
|
next if File.exist?(tf_vars_file)
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
puts "----> Generating Vars"
|
2018-04-29 18:00:12 +00:00
|
|
|
|
2018-02-26 21:37:36 +00:00
|
|
|
# Generate Azure crendentials
|
2019-06-11 22:24:35 +00:00
|
|
|
connection = Train.create("azure").connection
|
2018-04-11 19:13:23 +00:00
|
|
|
creds = connection.options
|
2018-02-06 18:40:21 +00:00
|
|
|
|
|
|
|
# Determine the storage account name and the admin password
|
2019-07-01 17:30:53 +00:00
|
|
|
require "securerandom"
|
2019-07-01 18:20:59 +00:00
|
|
|
sa_name = ("a".."z").to_a.sample(15).join
|
|
|
|
admin_password = SecureRandom.alphanumeric 72
|
2018-02-06 18:40:21 +00:00
|
|
|
|
|
|
|
# Use the first 4 characters of the storage account to create a suffix
|
|
|
|
suffix = sa_name[0..3]
|
|
|
|
|
2018-04-29 18:00:12 +00:00
|
|
|
content = <<~VARS
|
|
|
|
subscription_id = "#{creds[:subscription_id]}"
|
|
|
|
client_id = "#{creds[:client_id]}"
|
|
|
|
client_secret = "#{creds[:client_secret]}"
|
|
|
|
tenant_id = "#{creds[:tenant_id]}"
|
|
|
|
storage_account_name = "#{sa_name}"
|
|
|
|
admin_password = "#{admin_password}"
|
|
|
|
suffix = "#{suffix}"
|
|
|
|
VARS
|
|
|
|
|
2019-07-09 00:20:30 +00:00
|
|
|
content << "location = \"#{ENV["AZURE_LOCATION"]}\"\n" if ENV["AZURE_LOCATION"]
|
2018-04-29 18:00:12 +00:00
|
|
|
|
|
|
|
File.write(tf_vars_file, content)
|
|
|
|
end
|
|
|
|
|
|
|
|
desc "generate plan from state using terraform.tfvars file"
|
|
|
|
task :plan, [:tf_workspace] => [:vars] do |t, args|
|
2019-06-11 22:24:35 +00:00
|
|
|
tf_workspace = args[:tf_workspace] || ENV["INSPEC_TERRAFORM_ENV"]
|
2018-04-29 18:00:12 +00:00
|
|
|
abort("You must set the INSPEC_TERRAFORM_ENV variable.") unless tf_workspace
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
puts "----> Generating Plan"
|
2018-04-29 18:00:12 +00:00
|
|
|
|
|
|
|
sh("cd #{integration_dir}/build/ && terraform plan -out inspec-azure.plan")
|
|
|
|
end
|
|
|
|
|
|
|
|
desc "apply terraform plan"
|
|
|
|
task :apply, [:tf_workspace] => [:plan] do |t, args|
|
2019-06-11 22:24:35 +00:00
|
|
|
tf_workspace = args[:tf_workspace] || ENV["INSPEC_TERRAFORM_ENV"]
|
2018-04-29 18:00:12 +00:00
|
|
|
abort("You must set the INSPEC_TERRAFORM_ENV variable.") unless tf_workspace
|
2019-06-11 22:24:35 +00:00
|
|
|
puts "----> Applying Plan"
|
2018-04-29 18:00:12 +00:00
|
|
|
|
|
|
|
sh("cd #{integration_dir}/build/ && terraform workspace select #{tf_workspace}")
|
|
|
|
|
|
|
|
sh("cd #{integration_dir}/build/ && terraform apply inspec-azure.plan")
|
|
|
|
|
2018-02-26 21:37:36 +00:00
|
|
|
Rake::Task["test:azure:dump_attrs"].execute
|
2018-02-06 18:40:21 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
task :dump_attrs do
|
2018-02-26 21:37:36 +00:00
|
|
|
sh("cd #{integration_dir}/build/ && terraform output > #{attribute_file}")
|
2019-06-11 22:24:35 +00:00
|
|
|
raw_output = File.read(attribute_file)
|
|
|
|
yaml_output = raw_output.gsub(" = ", " : ")
|
|
|
|
File.open(attribute_file, "w") { |file| file.puts yaml_output }
|
2018-04-29 18:00:12 +00:00
|
|
|
end
|
2018-02-26 21:37:36 +00:00
|
|
|
|
|
|
|
task :run do
|
2019-06-11 22:24:35 +00:00
|
|
|
puts "----> Run"
|
2018-02-06 18:40:21 +00:00
|
|
|
sh("bundle exec inspec exec #{integration_dir}/verify -t azure://1e0b427a-d58b-494e-ae4f-ee558463ebbf")
|
|
|
|
end
|
|
|
|
|
2018-02-26 21:37:36 +00:00
|
|
|
task :cleanup, :tf_workspace do |t, args|
|
2019-06-11 22:24:35 +00:00
|
|
|
tf_workspace = args[:tf_workspace] || ENV["INSPEC_TERRAFORM_ENV"]
|
2018-02-26 21:37:36 +00:00
|
|
|
abort("You must either call the top-level test:azure task, or set the INSPEC_TERRAFORM_ENV variable.") unless tf_workspace
|
2019-06-11 22:24:35 +00:00
|
|
|
puts "----> Cleanup"
|
2018-02-26 21:37:36 +00:00
|
|
|
|
2018-04-29 18:00:12 +00:00
|
|
|
sh("cd #{integration_dir}/build/ && terraform destroy -force ")
|
2018-02-26 21:37:36 +00:00
|
|
|
|
|
|
|
sh("cd #{integration_dir}/build/ && terraform workspace select default")
|
|
|
|
sh("cd #{integration_dir}/build && terraform workspace delete #{tf_workspace}")
|
2018-04-29 18:00:12 +00:00
|
|
|
File.delete(tf_vars_file)
|
2018-02-06 18:40:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
desc "Perform Azure Integration Tests"
|
|
|
|
task :azure do
|
2019-06-11 22:24:35 +00:00
|
|
|
tf_workspace = ENV["INSPEC_TERRAFORM_ENV"] || prompt("Please enter a workspace for your integration tests to run in: ")
|
2018-02-26 21:37:36 +00:00
|
|
|
begin
|
2019-06-11 22:24:35 +00:00
|
|
|
Rake::Task["test:azure:setup"].execute({ tf_workspace: tf_workspace })
|
2018-02-26 21:37:36 +00:00
|
|
|
Rake::Task["test:azure:run"].execute
|
|
|
|
rescue
|
|
|
|
abort("Integration testing has failed")
|
|
|
|
ensure
|
2019-06-11 22:24:35 +00:00
|
|
|
Rake::Task["test:azure:cleanup"].execute({ tf_workspace: tf_workspace })
|
2018-02-26 21:37:36 +00:00
|
|
|
end
|
2018-02-06 18:40:21 +00:00
|
|
|
end
|
2015-09-02 02:13:59 +00:00
|
|
|
end
|
2015-11-20 21:38:12 +00:00
|
|
|
|
2015-11-26 17:58:53 +00:00
|
|
|
# Print the current version of this gem or update it.
|
|
|
|
#
|
|
|
|
# @param [Type] target the new version you want to set, or nil if you only want to show
|
|
|
|
def inspec_version(target = nil)
|
2019-06-11 22:24:35 +00:00
|
|
|
path = "lib/inspec/version.rb"
|
|
|
|
require_relative path.sub(/.rb$/, "")
|
2015-11-26 17:58:53 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
nu_version = target.nil? ? "" : " -> #{target}"
|
2015-11-26 17:58:53 +00:00
|
|
|
puts "Inspec: #{Inspec::VERSION}#{nu_version}"
|
|
|
|
|
|
|
|
unless target.nil?
|
|
|
|
raw = File.read(path)
|
2016-01-15 20:41:20 +00:00
|
|
|
nu = raw.sub(/VERSION.*/, "VERSION = '#{target}'.freeze")
|
2015-11-26 17:58:53 +00:00
|
|
|
File.write(path, nu)
|
|
|
|
load(path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Check if a command is available
|
|
|
|
#
|
|
|
|
# @param [Type] x the command you are interested in
|
|
|
|
# @param [Type] msg the message to display if the command is missing
|
|
|
|
def require_command(x, msg = nil)
|
|
|
|
return if system("command -v #{x} || exit 1")
|
2019-07-09 00:20:30 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
msg ||= "Please install it first!"
|
2015-11-26 17:58:53 +00:00
|
|
|
puts "\033[31;1mCan't find command #{x.inspect}. #{msg}\033[0m"
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
|
|
|
# Check if a required environment variable has been set
|
|
|
|
#
|
|
|
|
# @param [String] x the variable you are interested in
|
|
|
|
# @param [String] msg the message you want to display if the variable is missing
|
|
|
|
def require_env(x, msg = nil)
|
|
|
|
exists = `env | grep "^#{x}="`
|
|
|
|
return unless exists.empty?
|
2019-07-09 00:20:30 +00:00
|
|
|
|
2015-11-26 17:58:53 +00:00
|
|
|
puts "\033[31;1mCan't find environment variable #{x.inspect}. #{msg}\033[0m"
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
|
|
|
# Check the requirements for running an update of this repository.
|
|
|
|
def check_update_requirements
|
2019-06-11 22:24:35 +00:00
|
|
|
require_command "git"
|
2015-11-26 17:58:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Show the current version of this gem.
|
2019-06-11 22:24:35 +00:00
|
|
|
desc "Show the version of this gem"
|
2015-11-26 17:58:53 +00:00
|
|
|
task :version do
|
|
|
|
inspec_version
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
desc "Release a new docker image"
|
2016-05-13 10:59:33 +00:00
|
|
|
task :release_docker do
|
|
|
|
version = Inspec::VERSION
|
|
|
|
cmd = "rm *.gem; gem build *gemspec && "\
|
|
|
|
"mv *.gem inspec.gem && "\
|
|
|
|
"docker build -t chef/inspec:#{version} . && "\
|
2016-11-28 11:59:29 +00:00
|
|
|
"docker push chef/inspec:#{version} && "\
|
|
|
|
"docker tag chef/inspec:#{version} chef/inspec:latest &&"\
|
2016-11-21 04:51:39 +00:00
|
|
|
"docker push chef/inspec:latest"
|
2016-05-13 10:59:33 +00:00
|
|
|
puts "--> #{cmd}"
|
2019-06-11 22:24:35 +00:00
|
|
|
sh("sh", "-c", cmd)
|
2016-05-13 10:59:33 +00:00
|
|
|
end
|