Skip to content

Instantly share code, notes, and snippets.

@kenjiskywalker
Last active January 8, 2016 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kenjiskywalker/6118553 to your computer and use it in GitHub Desktop.
Save kenjiskywalker/6118553 to your computer and use it in GitHub Desktop.
serverspec read chef json file
require "/root/chef/spec/spec_helper"
if property["mysql"]["is_slave"]
describe file('/etc/my.cnf') do
it { should contain 'read_only' }
end
describe command("echo \"show variables;\" \| mysql \| grep \^read_only") do
it { should return_stdout "read_only ON" }
end
end
...
<% if node[:mysql][:is_slave] %>
read_only
<% end %>
...
{
"mysql":{
"is_slave": true
}
}
require 'rubygems'
require 'rake'
require 'rspec/core/rake_task'
require 'json'
require 'chef/run_list'
json_files = Dir::glob("../chef/nodes/*.json")
Chef::Config[:cookbook_path] = '../chef/site-cookbooks/'
Chef::Config[:role_path] = '../chef/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
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 = ['../chef/site-cookbooks/{' + recipes.join(',') + '}/spec/*_spec.rb', "spec/#{target_host}/*_spec.rb"]
end
end
end
require 'serverspec'
require 'pathname'
require 'net/ssh'
require 'json'
include SpecInfra::Helper::Ssh
include SpecInfra::Helper::DetectOS
include Serverspec::Helper::Properties
RSpec.configure do |c|
c.host = ENV['TARGET_HOST']
set_property JSON.parse(File.read("../chef/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