From e0ba6a812e04d0530c8260c77015f8f11c7818c9 Mon Sep 17 00:00:00 2001 From: Chris Price Date: Tue, 24 Apr 2012 20:29:06 -0700 Subject: [PATCH] (#13429) refactor puppetral agent for telly compatibility This commit includes: * Small change to puppetral agent to ensure puppet settings are initialized properly prior to use; this is necessary for compatibility with Telly * Add "puppet_spec_helper" (from puppetlabs_spec_helper project) to local spec_helper --- agent/puppetca/agent/puppetca.rb | 9 ++++++++- agent/puppetca/spec/puppetca_agent_spec.rb | 5 ++++- agent/puppetral/agent/puppetral.rb | 6 ++++++ agent/puppetral/spec/puppetral_spec.rb | 18 ++++++++++++++++++ spec/spec_helper.rb | 1 + 5 files changed, 37 insertions(+), 2 deletions(-) diff --git a/agent/puppetca/agent/puppetca.rb b/agent/puppetca/agent/puppetca.rb index b4655c4..28cb85e 100644 --- a/agent/puppetca/agent/puppetca.rb +++ b/agent/puppetca/agent/puppetca.rb @@ -23,7 +23,7 @@ def startup_hook signed = paths_for_cert(certname)[:signed] csr = paths_for_cert(certname)[:request] - msg = [] + msg = new_msg_array if has_cert?(certname) File.unlink(signed) @@ -105,6 +105,13 @@ def paths_for_cert(certname) {:signed => "#{@cadir}/signed/#{certname}.pem", :request => "#{@cadir}/requests/#{certname}.pem"} end + + # This is only here to simplify testing, so that we can simulate + # various lengths of the msg queue + def new_msg_array() + [] + end + private :new_msg_array end end end diff --git a/agent/puppetca/spec/puppetca_agent_spec.rb b/agent/puppetca/spec/puppetca_agent_spec.rb index 061a024..43d1ced 100755 --- a/agent/puppetca/spec/puppetca_agent_spec.rb +++ b/agent/puppetca/spec/puppetca_agent_spec.rb @@ -48,10 +48,13 @@ end it "should return the message if there are no certs but msg.size is not 0" do + @msg_array = [] + @agent.expects(:paths_for_cert).with("certname").twice.returns({:signed => "signed", :request => "request"}) @agent.expects(:has_cert?).with("certname").returns(false) @agent.expects(:cert_waiting?).with("certname").returns(false) - Array.any_instance.expects(:size).returns(1) + @agent.expects(:new_msg_array).returns(@msg_array) + @msg_array.expects(:size).returns(1) result = @agent.call(:clean, :certname => "certname") result.should be_successful result.should have_data_items(:msg => "") diff --git a/agent/puppetral/agent/puppetral.rb b/agent/puppetral/agent/puppetral.rb index 004729b..deed795 100644 --- a/agent/puppetral/agent/puppetral.rb +++ b/agent/puppetral/agent/puppetral.rb @@ -1,4 +1,5 @@ require 'puppet' +require 'puppet/face/resource' module MCollective module Agent @@ -17,6 +18,11 @@ module Agent # You can use puppetral to declare instances of any sensible Puppet type, # as long as you supply all of the attributes that the type requires. class Puppetral "Resource Abstraction Layer Agent", :description => "View and edit resources with Puppet's resource abstraction layer", :author => "R.I.Pienaar , Max Martin ", diff --git a/agent/puppetral/spec/puppetral_spec.rb b/agent/puppetral/spec/puppetral_spec.rb index 25d30cc..b2d93bf 100755 --- a/agent/puppetral/spec/puppetral_spec.rb +++ b/agent/puppetral/spec/puppetral_spec.rb @@ -1,5 +1,6 @@ #!/usr/bin/env rspec require 'spec_helper' +require 'tmpdir' describe "puppetral agent" do before :all do @@ -7,6 +8,23 @@ @agent = MCollective::Test::LocalAgentTest.new("puppetral", :agent_file => agent_file).plugin end + # Not entirely thrilled with this; these tests end up causing puppet to actually + # write some files in vardir, so we need to make sure that vardir is a real + # directory that we can write to. In an ideal world, I'd prefer that external + # tools need not be quite so familiar with puppet's internal state and settings + # implementations in order to run tests. However, the current behavior of + # puppet in "test" mode is to use "/dev/null" for all of the state / config + # directories so that we will detect tests that attempt to write to them and + # fail, since most spec tests should not be doing so. --cprice 2012-04-24 + before :each do + @vardir = Dir.mktmpdir + Puppet[:vardir] = @vardir + end + + after :each do + FileUtils.rm_rf(@vardir) if File.directory?(@vardir) + end + describe "#find" do it "should retrieve information about the type and title passed" do result = @agent.call(:find, :type => 'User', :title => 'bob') diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index fa4ce15..7e155f9 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -7,6 +7,7 @@ require 'rspec/mocks' require 'mocha' require 'tempfile' +require 'puppet_spec_helper' RSpec.configure do |config| config.mock_with :mocha