require 'spec_helper' <% test.classes.each do |cls| -%> describe "<%= cls.name %>" do <% test.nodes.each do |node| -%> context "using fact set <%= node.name %>" do node_facts = <%= node.fact_set %> let(:facts) { node_facts } <% if node.trusted_set -%> trusted_facts = <%= node.trusted_set %> let(:trusted_facts) { trusted_facts } <% end -%> <% if @before_conditions -%> before :each do <% @before_conditions.each do |function| -%> <%= function %> <% end -%> end <% end -%> <% if @mock_functions -%> # Create a from_json function so that we can parse the output of json for mocked functions before :each do Puppet::Parser::Functions.newfunction(:from_json, :type => :rvalue) { |args| require 'multi_json' MultiJson.load(args[0]) } end <% end -%> <% unless opts[:no_workarounds] -%> before :each do # Curtrently there is some code within Puppet that will try to execute # commands when compiling a catalog even though it shouldn't. One example is # the groups attribute of the user resource on AIX. If we are running on # Windows but pretending to be UNIX this will definitely fail so we need to # mock it (or vice versa) # Details: # https://github.com/puppetlabs/puppet/blob/master/lib/puppet/util/execution.rb#L191 expected_null_file = Puppet::Util::Platform.windows? ? 'NUL' : '/dev/null' unless File.exist? expected_null_file allow(Puppet::Util::Execution).to receive(:execute).and_raise(Puppet::ExecutionFailure.new("Onceover caused this")) end # The windows ACL module causes issues when compiled on other platforms # These are detailed in the following ticket: # https://github.com/rodjek/rspec-puppet/issues/665 # # The below should work around this common issue if Puppet::Type.type(:acl) allow_any_instance_of(Puppet::Type.type(:acl).provider(:windows)).to receive(:validate) allow_any_instance_of(Puppet::Type.type(:acl).provider(:windows)).to receive(:respond_to?).with(:get_account_name).and_return(false) allow_any_instance_of(Puppet::Type.type(:acl).provider(:windows)).to receive(:respond_to?).with(:get_group_name).and_return(false) allow_any_instance_of(Puppet::Type.type(:acl).provider(:windows)).to receive(:respond_to?).with(:validate).and_return(true) end # The windows_adsi provider also has an issue where the contructor takes # a different number of arguments depending on whether the ADSI # underlying connectivity exists. This causes the following error: # # wrong number of arguments (given 1, expected 0) # # This fixes that if we aren't using Windows begin # Test to see if this works without modification require 'puppet/util/windows' require 'puppet/util/windows/adsi' Puppet::Util::Windows::ADSI.computer_name rescue LoadError, StandardError # Declare an entire mocked module because for some reason we can't load it unless we are on Windows module Puppet::Util::Windows module ADSI class ADSIObject; end class User < ADSIObject; end class UserProfile; end class Group < ADSIObject; end end module File; end module Registry end module SID class Principal; end end class EventLog; end end # Mock commonly used things allow(Puppet::Util::Windows::SID).to receive(:name_to_sid).and_return('S-1-5-32-544') allow(Puppet::Util::Windows::SID).to receive(:name_to_principal).and_return(nil) allow_any_instance_of(Puppet::Util::Windows::ADSI::User).to receive(:initialize) allow_any_instance_of(Puppet::Util::Windows::ADSI::User).to receive(:groups).and_return([]) allow_any_instance_of(Puppet::Util::Windows::ADSI::User).to receive(:name_sid_hash).and_return({}) # Group instance methods allow_any_instance_of(Puppet::Util::Windows::ADSI::Group).to receive(:members).and_return([]) allow_any_instance_of(Puppet::Util::Windows::ADSI::Group).to receive(:members_sids).and_return([]) # Class methods allow(Puppet::Util::Windows::ADSI::Group).to receive(:name_sid_hash).and_return({}) end end <% end -%> <% if @after_conditions -%> after :each do <% @after_conditions.each do |function| -%> <%= function %> <% end -%> end <% end -%> let(:pre_condition) { pp = <%= '<<' %>-'END' $onceover_class = '<%= cls.name %>' $onceover_node = '<%= node.name %>' # Begin user-specified pre_condition <%= (pre_condition || "").chomp %> # End user-specified pre_condition <% if @mock_functions -%> <% require 'multi_json' -%> # Mocking functions <% @mock_functions.each do |function,params| -%> <% if params['returns'].is_a? String -%> <% json = params['returns'].dump[1..-2].to_json -%> <% else -%> <% json = params['returns'].to_json -%> <% end -%> function <%= function %> (*$args) { from_json('<%= json %>') } <% end -%> <% end -%> END } it { should compile } end <% end -%> end <% end -%>