From 34db75baa083e7d426ea4b16f1bde1e1e39e1b72 Mon Sep 17 00:00:00 2001 From: Dan Bode Date: Sun, 17 Oct 2010 23:30:10 -0700 Subject: Ramped up unit tests. --- spec/unit/provider/sudoers/parsed_spec.rb | 159 +++++++++++++++++++----------- spec/unit/type/sudoers.rb | 54 +--------- 2 files changed, 104 insertions(+), 109 deletions(-) diff --git a/spec/unit/provider/sudoers/parsed_spec.rb b/spec/unit/provider/sudoers/parsed_spec.rb index 3cf001c..1bcdfae 100644 --- a/spec/unit/provider/sudoers/parsed_spec.rb +++ b/spec/unit/provider/sudoers/parsed_spec.rb @@ -1,72 +1,119 @@ require 'pathname'; Pathname.new(__FILE__).realpath.ascend { |x| begin; require (x + 'spec_helper.rb'); break; rescue LoadError; end } +module ParsedHelper + def parse_line_helper(line, result_hash) + hash = @init_records.merge(result_hash) + record = @provider.parse_line(line) + hash.each do |h,k| + record[h.to_sym].should == k + end + end +end + #describe_provider :sudoers, :parsed, :resource => {:path => '/tmp/vcsrepo'} do describe Puppet::Type.type(:sudoers).provider(:parsed) do + include ParsedHelper before(:each) do @provider = Puppet::Type.type(:sudoers).provider(:parsed) + @init_records = {} end - it 'should not be null' do + it 'should not be nil' do @provider.should_not be_nil end - describe 'setup' do - it 'should fail if visudo is not in path' do - ENV['PATH']='' - @provider = Puppet::Type.type(:sudoers).provider(:parsed) + describe 'when parsing a line' do + describe 'when the line is a default' do + before do + @init_records = {:type => 'default'} + @params_hash = {:name => 'Defaults', :parameters => ['param', 'param2']} + end + ['Defaults', 'Defaults@host', 'Defaults:user_list', 'Defaults>runlist'].each do |d| + it "should parse default #{d} with a single parameter" do + parse_line_helper("#{d} param", { + :name => d, :parameters => ['param']} + ) + end + end + it 'should parse a parameter list' do + parse_line_helper('Defaults param,param2', @params_hash) + end + it 'should parse a parameter list with spaces' do + parse_line_helper('Defaults param, param2', @params_hash) + end + it 'should not care about spaces before and after' do + parse_line_helper(' Defaults param, param2 ', @params_hash) + end + end + + describe 'when the line is an alias' do + before do + @init_records = {:type => 'alias', :name => 'NAM3_1'} + end + ['User_Alias', 'Runas_Alias', 'Host_Alias', 'Cmnd_Alias'].each do |a| + it "should parse a(n) #{a}" do + parse_line_helper("#{a} NAM3_1 = var", + {:items => ['var'], :sudo_alias => a} + ) + end + end + it 'should parse a list' do + parse_line_helper('User_Alias NAM3_1 = var,var2', + {:sudo_alias => 'User_Alias', :items => ['var', 'var2']} + ) + end + end + describe 'when the line is a user spec' do + before do + @init_records = {:type => 'user_spec'} + end + it 'should require user and host' do + lambda {@provider.parse_line('x = y')}.should raise_error(Puppet::Error) + end + it 'should call my function' do + parse_line_helper('x y = z', + {:users => ['x'], :hosts => ['y'], :commands => ['z']} + ) + end + end + describe 'when parsing a comment' do + it 'should parse comments' do + parse_line_helper('# something!!', {:comment => ' something!!'}) + end + it 'should parse out a namevar line' do + parse_line_helper('#Puppet NAMEVAR foo', {:name => 'foo'}) + end + end + end + + describe 'when processing prefetch hook' do + # test that namevar is consumed from previous line + it 'should use previous namevar as :name for user spec' do + @provider.stubs(:retrieve).with('foo').returns( + [ + {:record_type => :comment, :name => 'foo'}, + {:record_type => :parsed, :type => 'user_spec' } + ] + ) + records = @provider.prefetch_target('foo') + records.size.should == 1 + records.last[:name].should == 'foo' end - it 'should work if visudo is in path' do + it 'should supply a fakenamevar if one is missing' do + @provider.stubs(:retrieve).with('foo').returns( + [ + {:record_type => :parsed, :type => 'user_spec' } + ] + ) + records = @provider.prefetch_target('foo') + records[0][:name].should == 'fake_namevar_0' end + # there is some crazy code that tries to associate comments with types + end -# context "parsing lines" do -# context "should ignore empty lines" do -# -# end -# context "should ignore comment lines" do -# -# end -# context "parsing invalid lines" do -# -# end -# context "parsing alias lines" do -# -# end -# context "parsing user spec lines" do -# context "prefetch comment NAMEVAR lines for user spec" -# end -# end -# context "parsing defaults lines" do -# -# end -# end -# -# context "dissallow type changes" do -# # not sure if this requires a type -# end -# -# -# context "Writing lines" do -# context "write comment lines" do -# -# end -# context "write blank lines" do -# -# end -# context "write user alias lines" do -# -# end -# context "write user spec lines" do -# -# end -# context "write defaults lines" do -# -# end -# context "fail for invalid types" do -# -# end -# context "fail for invalid lines" do -# -# end -# end + describe 'when writing out the target files' do + # test to_line + + end end diff --git a/spec/unit/type/sudoers.rb b/spec/unit/type/sudoers.rb index c2e1068..39c4e0b 100644 --- a/spec/unit/type/sudoers.rb +++ b/spec/unit/type/sudoers.rb @@ -67,6 +67,7 @@ describe Puppet::Type.type(:sudoers) do @valid_params = @alias_params end describe 'require attributes' do + # isrequired in puppet is broken #self.should_require([:sudo_alias, :items]) end describe "sudo_alias" do @@ -166,57 +167,4 @@ describe Puppet::Type.type(:sudoers) do end end end -# describe "the user parameter" do -# 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 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 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 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 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 -# end end -- cgit v1.2.3