Last active
December 19, 2015 06:19
-
-
Save kenjiskywalker/5910474 to your computer and use it in GitHub Desktop.
read chef run_list do serverspec test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'rake' | |
require 'rspec/core/rake_task' | |
require 'yaml' | |
require 'json' | |
require 'chef/run_list' | |
json_files = Dir::glob("./nodes/*.json") | |
Chef::Config[:cookbook_path] = './cookbook/' | |
Chef::Config[:role_path] = './roles/' | |
desc "Run serverspec to all hosts" | |
task :default => 'serverspec:all' | |
host_run_list = {} | |
json_files.each do |json_file| host_config = JSON.parse(File.read(json_file)) | |
host_name = File.basename(json_file, ".json") | |
host_run_list[host_name] = host_config["run_list"] || [] | |
end | |
namespace :serverspec do | |
### start task | |
task :all => host_run_list.keys | |
host_run_list.keys.each do |target_host| | |
desc "Run serverspec to #{target_host}" | |
RSpec::Core::RakeTask.new(target_host.to_sym) do |t| | |
ENV['TARGET_HOST'] = target_host | |
target_run_list = host_run_list[target_host] | |
recipes = Chef::RunList.new(*target_run_list).expand("_default", "disk").recipes | |
t.pattern = ['cookbooks/{' + recipes.join(',') + '}/spec/*_spec.rb', "spec/#{target_host}/*_spec.rb"] | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'serverspec' | |
require 'pathname' | |
require 'net/ssh' | |
require 'yaml' | |
require 'json' | |
include Serverspec::Helper::Ssh | |
include Serverspec::Helper::DetectOS | |
include Serverspec::Helper::Attributes | |
RSpec.configure do |c| | |
c.host = ENV['TARGET_HOST'] | |
attr_set JSON.parse(File.read("./nodes/#{c.host}.json")) | |
options = Net::SSH::Config.for(c.host) | |
user = 'root' | |
c.ssh = Net::SSH.start(c.host, user, options) | |
c.os = backend.check_os | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment