forked from railsmachine/shadow_puppet
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of git@github.com:railsmachine/shadow_puppet
- Loading branch information
Showing
6 changed files
with
127 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |