summaryrefslogtreecommitdiff
path: root/spec/lib
diff options
context:
space:
mode:
authorDan Bode <dan@puppetlabs.com>2010-06-21 09:30:30 -0700
committerDan Bode <dan@puppetlabs.com>2010-06-21 09:30:30 -0700
commitfe6b50e0383af01023f010b26cd1cf2fa2f3c9c4 (patch)
tree00ddb55d3da661be666105cdfa9b7bdc8629f056 /spec/lib
parent38e373fc67ff7447eb1052a40979d1e9df5934ba (diff)
just getting started with unit tests for sudo.
Diffstat (limited to 'spec/lib')
-rw-r--r--spec/lib/helpers.rb64
-rw-r--r--spec/lib/matchers.rb30
2 files changed, 94 insertions, 0 deletions
diff --git a/spec/lib/helpers.rb b/spec/lib/helpers.rb
new file mode 100644
index 0000000..55a18ae
--- /dev/null
+++ b/spec/lib/helpers.rb
@@ -0,0 +1,64 @@
+module Helpers
+
+ TEST_DIR = Pathname.new(__FILE__).parent + '..'
+
+ TYPES = {
+ :ec2 => :ec2
+ }
+
+ def self.included(obj)
+ obj.instance_eval { attr_reader :valid_params }
+ end
+
+ # Creates a new resource of +type+
+ def with(opts = {}, &block)
+ resource = @type.new(opts)
+ block ? (yield resource) : resource
+ end
+
+ # what is the difference?
+ # Returns a lambda creating a resource (ready for use with +should+)
+ def specifying(opts = {}, &block)
+ specification = lambda { with(opts) }
+ block ? (yield specification) : specification
+ end
+
+ # Sets up an expection that a resource for +type+ is not created
+ def should_not_create(type)
+ raise "Invalid type #{type}" unless TYPES[type]
+ Puppet::Type.type(TYPES[type]).expects(:new).never
+ end
+
+ # Sets up an expection that a resource for +type+ is created
+ def should_create(type)
+ raise "Invalid type #{type}" unless TYPES[type]
+ Puppet::Type.type(TYPES[type]).expects(:new).with { |args| yield(args) }
+ end
+
+ # Return the +@valid_params+ without one or more keys
+ # Note: Useful since resource types don't like it when +nil+ is
+ # passed as a parameter value
+ def valid_params_without(*keys)
+ valid_params.reject { |k, v| keys.include?(k) }
+ end
+
+ # yeah! I added this one!
+ def valid_params_with(opts = {})
+ opts.each { |k, v| valid_params[k] = v}
+ valid_params
+ end
+
+ # Stub the default provider to get around confines for testing
+ def stub_default_provider!
+ unless defined?(@type)
+ raise ArgumentError, "@type must be set"
+ end
+ provider = @type.provider(:ec2)
+ @type.stubs(:defaultprovider => provider)
+ end
+
+ def fixture(name, ext = '.txt')
+ (TEST_DIR + 'fixtures' + "#{name}#{ext}").read
+ end
+
+end
diff --git a/spec/lib/matchers.rb b/spec/lib/matchers.rb
new file mode 100644
index 0000000..57a35e6
--- /dev/null
+++ b/spec/lib/matchers.rb
@@ -0,0 +1,30 @@
+module Matchers
+
+ class AutoRequireMatcher
+ def initialize(*expected)
+ @expected = expected
+ end
+
+ def matches?(resource)
+ resource_type = resource.class
+ configuration = resource_type.instance_variable_get(:@autorequires) || {}
+ @autorequires = configuration.inject([]) do |memo, (param, block)|
+ memo + resource.instance_eval(&block)
+ end
+ @autorequires.include?(@expected)
+ end
+ def failure_message_for_should
+ "expected resource autorequires (#{@autorequires.inspect}) to include #{@expected.inspect}"
+ end
+ def failure_message_for_should_not
+ "expected resource autorequires (#{@autorequires.inspect}) to not include #{@expected.inspect}"
+ end
+ end
+
+ # call-seq:
+ # autorequire :logical_volume, 'mylv'
+ def autorequire(type, name)
+ AutoRequireMatcher.new(type, name)
+ end
+
+end