Skip to content

Commit

Permalink
Merge branch 'master' of git@github.com:railsmachine/shadow_puppet
Browse files Browse the repository at this point in the history
  • Loading branch information
jnewland committed May 4, 2009
2 parents 3c7bc18 + 0dc775a commit 289f7c4
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 3 deletions.
69 changes: 69 additions & 0 deletions lib/shadow_puppet/test.rb
@@ -0,0 +1,69 @@
module Puppet
module Parser
class Resource

# clearing out some puppet methods that we probably won't need for testing
# that are also used in the params hash when defining the resource.
undef path
undef source
undef require

# This allows access to resource options as methods on the resource.
def method_missing name, *args
if params.keys.include? name.to_sym
params[name.to_sym].value
end
end
end
end
end

module ShadowPuppet
# To test manifests, access puppet resources using the plural form of the resource name.
# This returns a hash of all resources of that type.
#
# manifest.execs
# manifest.packages
#
# You can access resource options as methods on the resource
#
# manifest.files['/etc/motd'].content
# manifest.execs['service ssh restart'].onlyif
#
# ===Example
#
# Given this manifest:
#
# class TestManifest < ShadowPuppet::Manifest
# def myrecipe
# file '/etc/motd', :content => 'Welcome to the machine!', :mode => '644'
# exec 'newaliases', :refreshonly => true
# end
# recipe :myrecipe
# end
#
# A test for the manifest could look like this:
#
# manifest = TestManifest.new
# manifest.myrecipe
# assert_match /Welcome/, manifest.files['/etc/motd']
# assert manifest.execs['newaliases'].refreshonly
#
class Manifest
# Creates an instance method for every puppet type
# that either creates or references a resource
def self.register_puppet_types_for_testing
Puppet::Type.loadall
Puppet::Type.eachtype do |type|
plural_type = type.name.to_s.downcase.pluralize
#undefine the method rdoc placeholders
undef_method(plural_type) rescue nil
define_method(plural_type) do |*args|
puppet_resources[type]
end
end
end
register_puppet_types_for_testing

end
end
3 changes: 2 additions & 1 deletion shadow_puppet.gemspec
Expand Up @@ -6,7 +6,7 @@ Gem::Specification.new do |s|
s.email = ["jesse@railsmachine.com"]
s.homepage = 'http://railsmachine.github.com/shadow_puppet'
s.rubyforge_project = 'moonshine'
s.version = "0.3.0"
s.version = "0.3.1"
s.date = '2009-03-12'

s.default_executable = 'shadow_puppet'
Expand All @@ -19,6 +19,7 @@ Gem::Specification.new do |s|
"lib/shadow_puppet.rb",
"lib/shadow_puppet/manifest.rb",
"lib/shadow_puppet/core_ext.rb",
"lib/shadow_puppet/test.rb"
]
s.require_paths = ["lib"]
s.has_rdoc = true
Expand Down
11 changes: 11 additions & 0 deletions spec/fixtures/manifests.rb
Expand Up @@ -81,4 +81,15 @@ def foo(string)
file('/tmp/moonshine_foo', :ensure => 'present', :content => string.to_s)
end
recipe :foo
end

# setting up a few different resource types to test the test helpers
class TestHelpers < ShadowPuppet::Manifest

def foo
exec('foo', :command => 'true',:onlyif => 'test `hostname` == "foo"')
package('bar',:ensure => :installed)
file('baz', :content => 'bar',:mode => '644',:owner => 'rails')
end

end
4 changes: 2 additions & 2 deletions spec/manifest_spec.rb
Expand Up @@ -35,11 +35,11 @@
end

it "creates resources" do
@manifest.puppet_resources[Puppet::Type::Exec].keys.sort.should == ['foo']
@manifest.execs.keys.sort.should == ['foo']
end

it "applies our customizations to resources" do
@manifest.puppet_resources[Puppet::Type::Exec]["foo"].params[:path].value.should == ENV["PATH"]
@manifest.execs["foo"].path.should == ENV["PATH"]
end

describe "and then executing" do
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Expand Up @@ -2,6 +2,7 @@
gem 'rspec'
require 'spec'
require File.join(File.dirname(__FILE__), '..', 'lib', 'shadow_puppet.rb')
require File.join(File.dirname(__FILE__), '..', 'lib', 'shadow_puppet', 'test.rb')
Dir.glob(File.join(File.dirname(__FILE__), 'fixtures', '*.rb')).each do |manifest|
require manifest
end
42 changes: 42 additions & 0 deletions spec/test_spec.rb
@@ -0,0 +1,42 @@
require File.dirname(__FILE__) + '/spec_helper.rb'

describe "ShadowPuppet's test helpers" do

it "should be created when register_puppet_types_for_testing is called" do
Puppet::Type.newtype(:dummy){ }
Puppet::Type.type(:dummy).provide(:generic){ }

BlankManifest.new.respond_to?(:dummies).should == false
ShadowPuppet::Manifest.register_puppet_types_for_testing
BlankManifest.new.respond_to?(:dummies).should == true
end

describe "when used in tests" do

before do
@manifest = TestHelpers.new
@manifest.foo
end

it "should allow simple resource lookup" do
@manifest.execs.should == @manifest.puppet_resources[Puppet::Type::Exec]
@manifest.packages.should == @manifest.puppet_resources[Puppet::Type::Package]
@manifest.files.should == @manifest.puppet_resources[Puppet::Type::File]
end

# making sure that properties such as, e.g the :onlyif condition of Exec[foo]
# can be accessed simply as manifest.execs['foo'].onlyif rather than via the
# param hash
it "should allow referencing params directly" do
%w(execs files packages).each do |type|
@manifest.send(type.to_sym).each do |name,resource|
resource.params.keys.each do |param|
resource.send(param.to_sym).should == resource.params[param.to_sym].value
end
end
end
end

end

end

0 comments on commit 289f7c4

Please sign in to comment.