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 +++++++++++++++++++----------- 1 file changed, 103 insertions(+), 56 deletions(-) (limited to 'spec/unit/provider/sudoers/parsed_spec.rb') 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 -- cgit v1.2.3