just getting started with unit tests for sudo.
authorDan Bode <dan@puppetlabs.com>
Mon, 21 Jun 2010 16:30:30 +0000 (09:30 -0700)
committerDan Bode <dan@puppetlabs.com>
Mon, 21 Jun 2010 16:30:30 +0000 (09:30 -0700)
spec/lib/helpers.rb [new file with mode: 0644]
spec/lib/matchers.rb [new file with mode: 0644]
spec/spec.opts [new file with mode: 0644]
spec/spec_helper.rb [new file with mode: 0644]
spec/unit/puppet/provider/ec2/ec2.rb [new file with mode: 0644]
spec/unit/puppet/type/ec2.rb [new file with mode: 0644]
spec/unit/puppet/util/ec2.rb [new file with mode: 0644]
spec/unit/type/sudoers.rb [new file with mode: 0644]

diff --git a/spec/lib/helpers.rb b/spec/lib/helpers.rb
new file mode 100644 (file)
index 0000000..55a18ae
--- /dev/null
@@ -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 (file)
index 0000000..57a35e6
--- /dev/null
@@ -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
diff --git a/spec/spec.opts b/spec/spec.opts
new file mode 100644 (file)
index 0000000..91cd642
--- /dev/null
@@ -0,0 +1,6 @@
+--format
+s
+--colour
+--loadby
+mtime
+--backtrace
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
new file mode 100644 (file)
index 0000000..dbad9b2
--- /dev/null
@@ -0,0 +1,23 @@
+require 'pathname'
+dir = Pathname.new(__FILE__).parent
+$LOAD_PATH.unshift(dir, dir + 'lib', dir + '../lib')
+
+require 'mocha'
+require 'puppet'
+gem 'rspec'
+require 'spec/autorun'
+
+require 'helpers'
+require 'matchers'
+
+Spec::Runner.configure do |config|
+    config.mock_with :mocha
+    config.include Helpers
+    config.include Matchers
+end
+
+# We need this because the RAL uses 'should' as a method.  This
+# allows us the same behaviour but with a different method name.
+class Object
+    alias :must :should
+end
diff --git a/spec/unit/puppet/provider/ec2/ec2.rb b/spec/unit/puppet/provider/ec2/ec2.rb
new file mode 100644 (file)
index 0000000..1846147
--- /dev/null
@@ -0,0 +1,40 @@
+Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") }
+
+provider_class = Puppet::Type.type(:ec2).provider(:ec2)
+
+describe provider_class do
+  before do
+    @resource = stub("resource")
+    @provider = provider_class.new(@resource)
+  end
+
+#  it "should not be suitable if the 'aws' libraries are missing" do
+#    Puppet.features.expects(:aws?).returns false
+#    provider_class.should_not be_suitable
+#  end
+
+#  it "should be suitable if the 'aws' libraries are present" do
+#    Puppet.features.expects(:aws?).returns true
+#    provider_class.should be_suitable
+#  end
+
+#  it "should be present if provided an 'ensure' value of 'present'" do
+#    provider_class.new(:ensure => :present).should be_exists
+#  end
+#
+#  it "should be absent if provided an 'ensure' value of 'absent'" do
+#    provider_class.new(:ensure => :absent).should_not be_exists
+#  end
+#
+#  it "should be absent if not provided an 'ensure' value" do
+#    provider_class.new({}).should_not be_exists
+#  end
+#
+#  it "should be absent if provided with a resource rather than an 'ensure' value" do
+#    provider_class.new(@resource).should_not be_exists
+#  end
+
+#  it "should accept an instance_id at initialization" do
+#    provider_class.new(:instance_id => 50).instance_id.should == 50
+#  end
+end
diff --git a/spec/unit/puppet/type/ec2.rb b/spec/unit/puppet/type/ec2.rb
new file mode 100644 (file)
index 0000000..ce3d51c
--- /dev/null
@@ -0,0 +1,125 @@
+Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") }
+
+describe Puppet::Type.type(:ec2) do
+  before do
+    @type = Puppet::Type.type(:ec2)
+    stub_default_provider!
+    @valid_types = [ 
+      'm1.small', 'm1.large', 'm1.xlarge',
+      'm2.xlarge', 'm2.2xlarge', 'm2.4xlarge', 
+      'c1.medium', 'c1.xlarge'
+    ]
+    @valid_params = {
+      :name => :name,
+      :ensure => :present,
+      :user => 'user',
+      :password => 'password',
+      :image => 'image',
+      :desc => 'description'
+
+    }
+  end
+
+  it "should exist" do
+    @type.should_not be_nil
+  end
+
+  describe "the name parameter" do
+    it "should exist" do
+       @type.attrclass(:name).should_not be_nil
+    end
+    it 'values should be prefixed with PUPPET_' do
+      with(valid_params)[:name].should == "PUPPET_#{valid_params[:name]}"
+    end
+    it 'should be required' do
+      specifying(valid_params_without(:name)).should raise_error(Puppet::Error)
+    end
+  end
+
+  describe "the user parameter" do
+    it "should exist" do
+       @type.attrclass(:user).should_not be_nil
+    end
+    it 'should support setting a value' do
+      with(valid_params)[:user].should == valid_params[:user]
+    end
+    # I think isrequired is broken
+    it 'should be required' do
+      specifying(valid_params_without(:user)).should raise_error(Puppet::Error)
+    end
+  end
+
+  describe "the password parameter" do
+     it "should exist" do
+       @type.attrclass(:password).should_not be_nil
+    end
+    it 'should support setting a value' do
+      with(valid_params)[:password].should == valid_params[:password]
+    end
+    it 'should be required' do
+      specifying(valid_params_without(:password)).should raise_error(Puppet::Error)
+    end
+  end
+  
+  describe "the image parameter" do
+     it "should exist" do
+       @type.attrclass(:image).should_not be_nil
+    end
+    it 'should be required' do
+      specifying(valid_params_without(:image)).should raise_error(Puppet::Error)
+    end
+    it 'should support setting a value' do
+      with(valid_params)[:image].should == valid_params[:image]
+    end
+  end
+
+  describe "the desc parameter" do
+     it "should exist" do
+       @type.attrclass(:desc).should_not be_nil
+    end
+    it 'should not be required' do
+      specifying(valid_params_without(:desc)).should_not raise_error(Puppet::Error)
+    end
+    it 'should accept a value' do
+      with(valid_params)[:desc].should == 'description'
+    end
+  end
+
+  describe 'the type parameter' do
+    it 'should exist' do
+      @type.attrclass(:type).should_not be_nil
+    end
+    it 'should accept valid ec2 types' do
+      @valid_types.each do |t|
+        with(valid_params_with({:type => t}))[:type].should == t
+      end
+    end
+    it 'should not accept invalid types' do
+      specifying(:type => 'm1.freakin-huge').should raise_error(Puppet::Error) 
+    end
+    it 'should default to m1.small' do
+      with(valid_params_without(:type)) do |resource|
+        resource[:type].should == 'm1.small'
+      end
+    end
+  end
+  describe "when specifying the 'ensure' parameter" do
+    it "should exist" do
+      @type.attrclass(:ensure).should_not be_nil
+    end
+    it "should support 'present' as a value" do
+      with(valid_params_with({:ensure => :present}))[:ensure].should == :present
+    end
+    it "should support 'absent' as a value" do
+      with(valid_params.merge(:ensure => :absent)) do |resource|
+        resource[:ensure].should == :absent
+      end
+    end
+    it "should not support other values" do
+      specifying(valid_params.merge(:ensure => :foobar)).should raise_error(Puppet::Error)
+    end
+    it 'should not be required' do
+      specifying(valid_params_without(:ensure)).should_not raise_error(Puppet::Error)
+    end
+  end
+end
diff --git a/spec/unit/puppet/util/ec2.rb b/spec/unit/puppet/util/ec2.rb
new file mode 100644 (file)
index 0000000..c8757ee
--- /dev/null
@@ -0,0 +1,34 @@
+Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") }
+
+require 'puppet/util/ec2'
+
+class Ec2Helper
+  include Puppet::Util::Ec2
+end
+
+# LAK: This way the constants exist, but I expect we'll regret this
+unless Puppet.features.aws?
+  class AWS
+      class EC2
+          class Base
+          end
+      end
+  end
+end
+
+describe Puppet::Util::Ec2 do
+  before do
+    @helper = Ec2Helper.new
+  end
+
+  it "should use AWS::Base to create an EC2 connection" do
+    AWS::EC2::Base.expects(:new).with(:access_key_id => "myuser", :secret_access_key => "mypass")
+    @helper.ec2_connection("myuser", "mypass")
+  end
+
+  it "should call foo and bar when calling baz" do
+    @helper.stubs(:foo).returns "yay"
+    @helper.expects(:bar).with("yay").returns "yip"
+    @helper.baz.should == "yip"
+  end
+end
diff --git a/spec/unit/type/sudoers.rb b/spec/unit/type/sudoers.rb
new file mode 100644 (file)
index 0000000..4e3df0c
--- /dev/null
@@ -0,0 +1,144 @@
+Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") }
+
+describe Puppet::Type.type(:sudoers) do
+  before do
+    @type = Puppet::Type.type(:sudoers)
+    stub_default_provider!
+    @init_params = {
+      :ensure => :present,
+      :name => :name,
+      :comment => :mycomment
+      # :target => '/etc/sudoers'
+    }
+    # user spec setup
+    @spec_params = default_params.merge({
+      :type => 'user_spec',
+      :users => 'danbode',
+      :hosts => 'coolmachine@awesomeocorp.org', 
+      :commands => '/bin/true',
+    })
+    # sudo alias setup
+    @vaild_aliases = [
+      :Cmnd_Alias, :Host_Alias, :User_Alias, :Runas_Alias
+    ]
+    @valid_aliases_short = [
+      :Cmnd, :Host, :User, :Runas
+    ]
+    @alias_params = default_params.merge({
+      :type => 'alias',
+      :sudo_alias => 'Cmnd_Alias',
+      :items => 'item1'
+    })
+    # defaults setup
+    @default_params = default_params.merge({
+      :type => 'defaults',
+      :parameters => 'params'
+    })
+  end
+
+  it "should exist" do
+    puts @type
+    putes @init_params
+    @type.should_not be_nil
+  end
+
+  describe "the name parameter" do
+    puts @type
+    puts @init_params
+    @valid_params = @init_params.merge(@alias_params)
+    it "should exist" do
+       @type.attrclass(:name).should_not be_nil
+    end
+    it 'should be required' do
+      specifying(valid_params_without(:name)).should raise_error(Puppet::Error)
+    end
+    # valid values depend on type.
+  end
+
+#  describe "the user parameter" do
+#    it "should exist" do
+#       @type.attrclass(:user).should_not be_nil
+#    end
+#    it 'should support setting a value' do
+#      with(valid_params)[:user].should == valid_params[:user]
+#    end
+#    # I think isrequired is broken
+#    it 'should be required' do
+#      specifying(valid_params_without(:user)).should raise_error(Puppet::Error)
+#    end
+#  end
+#
+#  describe "the password parameter" do
+#     it "should exist" do
+#       @type.attrclass(:password).should_not be_nil
+#    end
+#    it 'should support setting a value' do
+#      with(valid_params)[:password].should == valid_params[:password]
+#    end
+#    it 'should be required' do
+#      specifying(valid_params_without(:password)).should raise_error(Puppet::Error)
+#    end
+#  end
+#  
+#  describe "the image parameter" do
+#     it "should exist" do
+#       @type.attrclass(:image).should_not be_nil
+#    end
+#    it 'should be required' do
+#      specifying(valid_params_without(:image)).should raise_error(Puppet::Error)
+#    end
+#    it 'should support setting a value' do
+#      with(valid_params)[:image].should == valid_params[:image]
+#    end
+#  end
+#
+#  describe "the desc parameter" do
+#     it "should exist" do
+#       @type.attrclass(:desc).should_not be_nil
+#    end
+#    it 'should not be required' do
+#      specifying(valid_params_without(:desc)).should_not raise_error(Puppet::Error)
+#    end
+#    it 'should accept a value' do
+#      with(valid_params)[:desc].should == 'description'
+#    end
+#  end
+#
+#  describe 'the type parameter' do
+#    it 'should exist' do
+#      @type.attrclass(:type).should_not be_nil
+#    end
+#    it 'should accept valid ec2 types' do
+#      @valid_types.each do |t|
+#        with(valid_params_with({:type => t}))[:type].should == t
+#      end
+#    end
+#    it 'should not accept invalid types' do
+#      specifying(:type => 'm1.freakin-huge').should raise_error(Puppet::Error) 
+#    end
+#    it 'should default to m1.small' do
+#      with(valid_params_without(:type)) do |resource|
+#        resource[:type].should == 'm1.small'
+#      end
+#    end
+#  end
+#  describe "when specifying the 'ensure' parameter" do
+#    it "should exist" do
+#      @type.attrclass(:ensure).should_not be_nil
+#    end
+#    it "should support 'present' as a value" do
+#      with(valid_params_with({:ensure => :present}))[:ensure].should == :present
+#    end
+#    it "should support 'absent' as a value" do
+#      with(valid_params.merge(:ensure => :absent)) do |resource|
+#        resource[:ensure].should == :absent
+#      end
+#    end
+#    it "should not support other values" do
+#      specifying(valid_params.merge(:ensure => :foobar)).should raise_error(Puppet::Error)
+#    end
+#    it 'should not be required' do
+#      specifying(valid_params_without(:ensure)).should_not raise_error(Puppet::Error)
+#    end
+#  end
+end