Created
February 9, 2013 19:25
-
-
Save jumanjiman/4746683 to your computer and use it in GitHub Desktop.
demo how to use rspec-hiera-puppet to load hiera.yaml when using dynamic puppet environments
This file contains hidden or 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
original_verbosity = $VERBOSE | |
$VERBOSE = nil | |
require 'rspec-puppet' | |
require 'rspec-hiera-puppet' | |
require 'test/unit' | |
require 'mocha/setup' | |
module Helpers | |
extend RSpec::SharedContext | |
def hiera_config_file | |
thisfile = File.dirname(__FILE__) | |
if thisfile.include?('modules') | |
repo_root=File.expand_path(File.join(thisfile,'..','..','..')) | |
else | |
repo_root=File.expand_path(File.join(thisfile,'..')) | |
end | |
h_config = YAML.load(File.open("#{repo_root}/modules/puppet/files/configs/hiera.yaml")) | |
h_config[:yaml][:datadir] = "#{repo_root}/hieradata" | |
h_config | |
end | |
let(:hiera_config) { hiera_config_file } | |
end | |
fixture_path = File.expand_path(File.join(__FILE__, '..', 'fixtures')) | |
# Correct the loadpath to not include the environmental puppet | |
$:.delete_if {|path| path.include?('/usr/lib/ruby/site_ruby/1.8') } | |
RSpec.configure do |c| | |
c.mock_with 'mocha' | |
# == core options == | |
# For core options and their meanings, see | |
# https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/configuration.rb#L76-124 | |
#c.fail_fast = true | |
# show times for 10 slowest examples (unless there are failed examples) | |
#c.profile_examples = true | |
# == rspec-puppet options == | |
c.module_path = File.join(fixture_path, 'modules') | |
c.manifest_dir = File.join(fixture_path, 'manifests') | |
c.include(Helpers) | |
c.before :each do | |
# don't cache facts and environment between test cases | |
Facter::Util::Loader.any_instance.stubs(:load_all) | |
Facter.clear | |
Facter.clear_messages | |
# Store environment variables (to be restored later) | |
@old_env = {} | |
ENV.each_key {|k| @old_env[k] = ENV[k]} | |
end | |
c.after :each do | |
# Restore environment variables | |
@old_env.each_pair {|k, v| ENV[k] = v} | |
to_remove = ENV.keys.reject {|key| @old_env.include? key } | |
to_remove.each {|key| ENV.delete key } | |
end | |
end | |
module FacterSpec | |
module ConfigHelper | |
def given_a_configuration_of(config) | |
Facter::Util::Config.stubs(:is_windows?).returns(config[:is_windows]) | |
Facter::Util::Config.stubs(:external_facts_dir).returns(config[:external_facts_dir] || "data_dir") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment