Skip to content

Instantly share code, notes, and snippets.

@jhoblitt
Created June 18, 2013 17:03
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhoblitt/5807254 to your computer and use it in GitHub Desktop.
Save jhoblitt/5807254 to your computer and use it in GitHub Desktop.
Attempting to testing facter facts with rspec
require 'facter'
Facter.add(:tw_cli) do
confine :kernel => "Linux"
setcode do
Facter::Util::Resolution.which('tw_cli')
end
end
require 'spec_helper'
RSpec.configure do |c|
c.include PuppetlabsSpec::Files
c.before :each do
# Ensure that we don't accidentally cache facts and environment
# between test cases.
Facter::Util::Loader.any_instance.stubs(:load_all)
Facter.clear
Facter.clear_messages
# Store any environment variables away to be restored later
@old_env = {}
ENV.each_key {|k| @old_env[k] = ENV[k]}
end
c.after :each do
PuppetlabsSpec::Files.cleanup
end
end
describe 'tw_cli fact', :type => :fact do
describe 'on linux' do
it "should find the tw_cli binary" do
Facter.fact(:kernel).stubs(:value).returns('Linux')
Facter::Util::Resolution.stubs(:which).with('tw_cli').returns('/usr/sbin/tw_cli')
Facter.fact(:tw_cli).value.should == '/usr/sbin/tw_cli'
end
it "should not find the tw_cli binary" do
Facter.fact(:kernel).stubs(:value).returns('Linux')
Facter::Util::Resolution.stubs(:which).with('tw_cli').returns(nil)
Facter.fact(:tw_cli).value.should == nil
end
end
describe 'on no kernel' do
it "should not find the tw_cli binary" do
Facter::Util::Resolution.stubs(:which).with('tw_cli').returns('/usr/sbin/tw_cli')
Facter.fact(:tw_cli).value.should == nil
end
it "should not find the tw_cli binary" do
Facter.fact(:tw_cli).value.should == nil
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment