rakefile
require 'rake'
require 'rspec/core/rake_task'
require 'json'
def get_roles(source_file)
roles = []
JSON.parse(File.read(source_file))['run_list'].each do |content|
#print "==> DEBUG: Get content = #{content} \n"
if /^role.*/ =~ content
roles << content.gsub(/role\[(.+)\]/, '\1')
end
end
roles
end
def get_recipes_from_anyjson(source_file)
recipes = []
JSON.parse(File.read("#{source_file}"))['run_list'].each do |content|
#print "==> DEBUG: Get content = #{content} \n"
if /^recipe.*/ =~ content
recipes << content.gsub(/recipe\[(.+)\]/, '\1')
end
end
recipes
end
namespace :spec do
all = []
Dir.glob('nodes/*.json').each do |node_file|
#print "==> DEBUG: Read nodefile = #{node_file} \n"
recipes = []
recipes << get_recipes_from_anyjson(node_file)
#print "==> DEBUG: Get recipes = #{recipes} \n"
get_roles(node_file).each do |role|
recipes << get_recipes_from_anyjson("roles/#{role}.json")
end
#print "==> DEBUG: Get recipes = #{recipes} \n"
recipes.flatten!
#print "==> DEBUG: Get recipes = #{recipes} \n"
node = File.basename(node_file, '.json')
node_short = node.split('.')[0]
all << node_short
desc "Run serverspec to #{node_short}"
RSpec::Core::RakeTask.new(node_short) do |t|
ENV['TARGET_HOST'] = node
print "==> INFO: Access node : #{node_short} \n"
print "==> INFO: Testing spec file = cookbooks/#{recipes.join(',')}/spec/*_spec.rb \n"
t.pattern = "cookbooks/{#{recipes.join(',')}}/spec/*_spec.rb"
t.verbose = false
end
end
task :all => all
end