From 171f34b3d518ffe86f830c8291d77455ca489b5c Mon Sep 17 00:00:00 2001 From: Dan Bode Date: Thu, 29 Jul 2010 10:39:48 -0700 Subject: wrote a great set of unit tests. --- spec/lib/helpers.rb | 47 +++++++++- spec/unit/type/sudoers.rb | 218 +++++++++++++++++++++++++++++++--------------- 2 files changed, 192 insertions(+), 73 deletions(-) (limited to 'spec') diff --git a/spec/lib/helpers.rb b/spec/lib/helpers.rb index 55a18ae..ca061d0 100644 --- a/spec/lib/helpers.rb +++ b/spec/lib/helpers.rb @@ -7,7 +7,48 @@ module Helpers } def self.included(obj) - obj.instance_eval { attr_reader :valid_params } + obj.instance_eval { attr_accessor :valid_params } + end + # self is available at the describe level + def restricted_params(key, params, opts={}, invalid='invalid') + params.each do |param| + #let(:param) {param} + #let(:key) {key} + with(valid_params_with({key => param}))[key].should == param + end + lambda {with(valid_params_with({key => invalid}))}.should raise_error + end + + # test that a list of attributes are required + def should_require(*keys) + keys.each do |k| + lambda { with(valid_params_without(k)) }.should raise_error Puppet::Error + end + end + # tests that an attribute should accept a value + def should_accept(attr, value) + k=attr.to_sym + with(valid_params_with({k => value}))[k].should == value + end + # tests that an attribute should not accept a value + def should_not_accept(attr, value) + k=attr.to_sym + lambda {with(valid_params_with({k => value}))}.should raise_error Puppet::Error + end + + + # tests that an attribute accepts an array + # - single element array, multiple element array + # - string is converted into an array + def should_accept_array(attr, value=['one', 'two']) + should_accept(attr, value) + should_accept(attr, value.first.to_a ) + with(valid_params_with({attr => value.first}))[attr].should == value.first.to_a + end + + # test that an attribute defaults to a value + def should_default_to(attr, defaultto) + with(valid_params_without(attr.to_sym))[:comment].should == defaultto end # Creates a new resource of +type+ @@ -49,11 +90,11 @@ module Helpers end # Stub the default provider to get around confines for testing - def stub_default_provider! + def stub_default_provider!(name) unless defined?(@type) raise ArgumentError, "@type must be set" end - provider = @type.provider(:ec2) + provider = @type.provider(name.to_sym) @type.stubs(:defaultprovider => provider) end diff --git a/spec/unit/type/sudoers.rb b/spec/unit/type/sudoers.rb index 4e3df0c..c2e1068 100644 --- a/spec/unit/type/sudoers.rb +++ b/spec/unit/type/sudoers.rb @@ -1,64 +1,172 @@ 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 + before(:each) do @type = Puppet::Type.type(:sudoers) - stub_default_provider! + stub_default_provider!(:parsed) + # these are the initial params used for testing @init_params = { :ensure => :present, :name => :name, - :comment => :mycomment + :comment => :mycomment, + :type => 'default', + :parameters => ['1'] # :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 + # these are all of the attributes that exist + @attributes=[ + :ensure, :name, :comment, :target, :type, + :sudo_alias, :items, + :parameters, + :users, :hosts, :commands ] - @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' - }) + @valid_params = @init_params end it "should exist" do - puts @type - putes @init_params @type.should_not be_nil end + it "should not have valid attributes that are nil" do + @attributes.each do |attr| + @type.attrclass(attr).should_not be_nil + end + end + + describe 'shared attributes' do + describe 'ensure' do + it 'should only accept absent/present' do + restricted_params(:ensure, [:absent, :present], @valid_params) + end + end + describe 'comment attribute' do + it 'should accept a value' do + should_accept(:comment, 'foo') + end + it 'should default to empty string' do + should_default_to(:comment, '') + end + end + describe 'name attribute' do + it 'should accept a value' do + should_accept(:name, 'foo') + end + it 'should be required' do + should_require(:name) + end + end + 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 + describe "the user alias" do + before(:each) do + @alias_params = @init_params.merge({ + :type => 'alias', + :sudo_alias => 'Cmnd_Alias', + :items => 'item1' + }) + # set what your valid params are + @valid_params = @alias_params end - it 'should be required' do - specifying(valid_params_without(:name)).should raise_error(Puppet::Error) + describe 'require attributes' do + #self.should_require([:sudo_alias, :items]) + end + describe "sudo_alias" do + it "should only accept certain aliases" do + valid= [ + :Cmnd_Alias, :Host_Alias, :User_Alias, :Runas_Alias, + :Cmnd, :Host, :User, :Runas + ] + restricted_params(:sudo_alias, valid, @valid_params) + end + end + describe 'items' do + it 'should be required' do + should_require(:items) + end + it 'should take a single element' do + with(valid_params_with({:items => 'one'}))[:items] .should == ['one'] + end + it 'should take a single element array' do + should_accept(:items, ['one']) + end + it 'should take an array' do + should_accept(:items, ['one', 'two']) + end + end + describe 'type' do + it 'should not accept other type' do + lambda { with(valid_params_with({:type => 'bad_type'}))}.should raise_error + end + it 'should not accept other type' do + lambda { with(valid_params_with({:type => 'user_spec'}))}.should raise_error + end + end + end + + describe 'sudo defaults' do + before do + @default_params = @init_params.merge({ + :type => 'default', + :parameters => 'params' + }) + # set what your valid params are + @valid_params = @default_params + end + describe 'parameters' do + it 'should take a single element' do + with(valid_params_with({:parameters => 'one'}))[:parameters].should == ['one'] + end + it 'should take a single element array' do + should_accept(:parameters, ['one']) + end + it 'should take an array' do + should_accept(:parameters, ['one', 'two']) + end + it 'should require a parameter' do + should_require(:parameters) + end end - # valid values depend on type. end + describe 'user specs' do + before do + # user spec setup + @spec_params = @init_params.merge({ + :type => 'user_spec', + :users => 'danbode', + :hosts => 'coolmachine@awesomeocorp.org', + :commands => '/bin/true', + }) + @valid_params = @spec_params + end + describe 'users' do + it 'should accept an array' do + should_accept_array(:users, ['alice', 'bob']) + end + it 'should not accept Defaults' do + should_not_accept(:usrs, 'Defaults') + end + it 'should be required' do + should_require(:users) + end + end + describe 'hosts' do + it 'should accept an array' do + should_accept_array(:hosts, ['alice', 'bob']) + end + it 'should be required' do + should_require(:hosts) + end + end + describe 'commands' do + it 'should accept an array' do + should_accept_array(:commands, ['alice', 'bob']) + end + it 'should be required' do + should_require(:commands) + end + 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 @@ -69,9 +177,6 @@ describe Puppet::Type.type(:sudoers) do # 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 @@ -81,9 +186,6 @@ describe Puppet::Type.type(:sudoers) do # 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 @@ -93,9 +195,6 @@ describe Puppet::Type.type(:sudoers) do # 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 @@ -105,9 +204,6 @@ describe Puppet::Type.type(:sudoers) do # 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 @@ -122,23 +218,5 @@ describe Puppet::Type.type(:sudoers) do # 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 -- cgit v1.2.3