2016-12-15 08:53:01 +00:00
|
|
|
#!/usr/bin/env rake
|
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
require 'rake/testtask'
|
|
|
|
require 'rubocop/rake_task'
|
2017-04-29 23:46:53 +00:00
|
|
|
require 'securerandom'
|
2016-12-15 08:53:01 +00:00
|
|
|
|
2017-06-28 12:55:01 +00:00
|
|
|
def prompt(message)
|
|
|
|
print(message)
|
|
|
|
STDIN.gets.chomp
|
|
|
|
end
|
|
|
|
|
2016-12-15 08:53:01 +00:00
|
|
|
# Rubocop
|
|
|
|
desc 'Run Rubocop lint checks'
|
|
|
|
task :rubocop do
|
|
|
|
RuboCop::RakeTask.new
|
|
|
|
end
|
|
|
|
|
2017-01-27 18:38:04 +00:00
|
|
|
# Minitest
|
|
|
|
Rake::TestTask.new do |t|
|
|
|
|
t.libs << 'libraries'
|
|
|
|
t.libs << 'test/unit'
|
|
|
|
t.pattern = "test/unit/**/*_test.rb"
|
|
|
|
end
|
|
|
|
|
2016-12-15 08:53:01 +00:00
|
|
|
# lint the project
|
|
|
|
desc 'Run robocop linter'
|
|
|
|
task lint: [:rubocop]
|
|
|
|
|
|
|
|
# run tests
|
2017-01-27 18:38:04 +00:00
|
|
|
task default: [:lint, :test]
|
2016-12-15 08:53:01 +00:00
|
|
|
|
|
|
|
namespace :test do
|
2017-04-29 23:46:53 +00:00
|
|
|
project_dir = File.dirname(__FILE__)
|
|
|
|
attribute_file = File.join(project_dir, ".attribute.yml")
|
|
|
|
integration_dir = File.join(project_dir, "test/integration")
|
2017-02-23 17:41:28 +00:00
|
|
|
|
2016-12-15 08:53:01 +00:00
|
|
|
# run inspec check to verify that the profile is properly configured
|
|
|
|
task :check do
|
2017-04-29 23:46:53 +00:00
|
|
|
sh("bundle exec inspec check #{project_dir}")
|
|
|
|
end
|
|
|
|
|
2017-06-28 12:55:01 +00:00
|
|
|
task :configure_test_environment, :namespace do |t, args|
|
2017-04-29 23:46:53 +00:00
|
|
|
puts "----> Creating terraform environment"
|
2017-06-28 12:55:01 +00:00
|
|
|
sh("cd #{integration_dir}/build/ && terraform env new #{args[:namespace]}")
|
2016-12-15 08:53:01 +00:00
|
|
|
end
|
2017-01-16 15:14:24 +00:00
|
|
|
|
2017-02-21 19:26:07 +00:00
|
|
|
task :setup_integration_tests do
|
|
|
|
puts "----> Setup"
|
2017-01-16 15:14:24 +00:00
|
|
|
sh("cd #{integration_dir}/build/ && terraform plan")
|
|
|
|
sh("cd #{integration_dir}/build/ && terraform apply")
|
2017-04-29 23:46:53 +00:00
|
|
|
sh("cd #{integration_dir}/build/ && terraform output > #{attribute_file}")
|
|
|
|
|
|
|
|
raw_output = File.read(attribute_file)
|
|
|
|
yaml_output = raw_output.gsub(" = ", " : ")
|
|
|
|
File.open(attribute_file, "w") {|file| file.puts yaml_output}
|
2017-02-21 19:26:07 +00:00
|
|
|
end
|
|
|
|
|
2017-04-29 23:46:53 +00:00
|
|
|
|
2017-02-21 19:26:07 +00:00
|
|
|
task :run_integration_tests do
|
|
|
|
puts "----> Run"
|
2017-04-29 23:46:53 +00:00
|
|
|
sh("bundle exec inspec exec #{integration_dir}/verify --attrs #{attribute_file}")
|
2017-01-16 15:14:24 +00:00
|
|
|
end
|
|
|
|
|
2017-02-21 19:26:07 +00:00
|
|
|
task :cleanup_integration_tests do
|
|
|
|
puts "----> Cleanup"
|
|
|
|
sh("cd #{integration_dir}/build/ && terraform destroy -force")
|
|
|
|
end
|
|
|
|
|
2017-06-28 12:55:01 +00:00
|
|
|
task :destroy_test_environment, :namespace do |t, args|
|
2017-04-29 23:46:53 +00:00
|
|
|
puts "----> Destroying terraform environment"
|
|
|
|
sh("cd #{integration_dir}/build/ && terraform env select default")
|
2017-06-28 12:55:01 +00:00
|
|
|
sh("cd #{integration_dir}/build && terraform env delete #{args[:namespace]}")
|
2017-04-29 23:46:53 +00:00
|
|
|
end
|
|
|
|
|
2017-02-16 20:22:33 +00:00
|
|
|
task :integration do
|
2017-06-28 12:55:01 +00:00
|
|
|
namespace = ENV['INSPEC_TERRAFORM_ENV'] || prompt("Please enter a namespace for your integration tests to run in: ")
|
|
|
|
Rake::Task["test:configure_test_environment"].execute({:namespace => namespace})
|
2017-02-21 19:26:07 +00:00
|
|
|
Rake::Task["test:cleanup_integration_tests"].execute
|
|
|
|
Rake::Task["test:setup_integration_tests"].execute
|
|
|
|
Rake::Task["test:run_integration_tests"].execute
|
|
|
|
Rake::Task["test:cleanup_integration_tests"].execute
|
2017-06-28 12:55:01 +00:00
|
|
|
Rake::Task["test:destroy_test_environment"].execute({:namespace => namespace})
|
2017-02-16 20:22:33 +00:00
|
|
|
end
|
2016-12-15 08:53:01 +00:00
|
|
|
end
|