diff options
Diffstat (limited to 'spec/unit')
-rwxr-xr-x | spec/unit/module.rb | 27 | ||||
-rwxr-xr-x | spec/unit/network/client/master.rb | 42 | ||||
-rwxr-xr-x | spec/unit/parameter.rb | 24 | ||||
-rwxr-xr-x | spec/unit/parser/ast/vardef.rb | 47 | ||||
-rwxr-xr-x | spec/unit/parser/lexer.rb | 3 | ||||
-rwxr-xr-x | spec/unit/parser/parser.rb | 34 | ||||
-rwxr-xr-x | spec/unit/parser/scope.rb | 37 | ||||
-rwxr-xr-x | spec/unit/property.rb | 35 | ||||
-rw-r--r-- | spec/unit/provider/service/daemontools.rb | 124 | ||||
-rw-r--r-- | spec/unit/provider/service/runit.rb | 117 | ||||
-rwxr-xr-x | spec/unit/provider/user/hpux.rb | 25 | ||||
-rwxr-xr-x | spec/unit/type/file.rb | 6 | ||||
-rwxr-xr-x | spec/unit/type/noop_metaparam.rb | 2 | ||||
-rwxr-xr-x | spec/unit/util/ldap/connection.rb | 13 |
14 files changed, 531 insertions, 5 deletions
diff --git a/spec/unit/module.rb b/spec/unit/module.rb index e79001ae1..4d66550b5 100755 --- a/spec/unit/module.rb +++ b/spec/unit/module.rb @@ -90,23 +90,44 @@ describe Puppet::Module, " when searching for templates" do end it "should use the main templatedir if no module is found" do - Puppet.settings.expects(:value).with(:templatedir, nil).returns("/my/templates") + Puppet::Module.stubs(:templatepath).with(nil).returns(["/my/templates"]) Puppet::Module.expects(:find).with("mymod", nil).returns(nil) Puppet::Module.find_template("mymod/mytemplate").should == "/my/templates/mymod/mytemplate" end it "should return unqualified templates directly in the template dir" do - Puppet.settings.expects(:value).with(:templatedir, nil).returns("/my/templates") + Puppet::Module.stubs(:templatepath).with(nil).returns(["/my/templates"]) Puppet::Module.expects(:find).never Puppet::Module.find_template("mytemplate").should == "/my/templates/mytemplate" end it "should use the environment templatedir if no module is found and an environment is specified" do - Puppet.settings.expects(:value).with(:templatedir, "myenv").returns("/myenv/templates") + Puppet::Module.stubs(:templatepath).with("myenv").returns(["/myenv/templates"]) + Puppet::Module.expects(:find).with("mymod", "myenv").returns(nil) + Puppet::Module.find_template("mymod/mytemplate", "myenv").should == "/myenv/templates/mymod/mytemplate" + end + + it "should use first dir from environment templatedir if no module is found and an environment is specified" do + Puppet::Module.stubs(:templatepath).with("myenv").returns(["/myenv/templates", "/two/templates"]) Puppet::Module.expects(:find).with("mymod", "myenv").returns(nil) Puppet::Module.find_template("mymod/mytemplate", "myenv").should == "/myenv/templates/mymod/mytemplate" end + it "should use a valid dir when templatedir is a path for unqualified templates and the first dir contains template" do + Puppet::Module.stubs(:templatepath).returns(["/one/templates", "/two/templates"]) + File.expects(:exists?).with("/one/templates/mytemplate").returns(true) + Puppet::Module.expects(:find).never + Puppet::Module.find_template("mytemplate").should == "/one/templates/mytemplate" + end + + it "should use a valid dir when templatedir is a path for unqualified templates and only second dir contains template" do + Puppet::Module.stubs(:templatepath).returns(["/one/templates", "/two/templates"]) + File.expects(:exists?).with("/one/templates/mytemplate").returns(false) + File.expects(:exists?).with("/two/templates/mytemplate").returns(true) + Puppet::Module.expects(:find).never + Puppet::Module.find_template("mytemplate").should == "/two/templates/mytemplate" + end + it "should use the node environment if specified" do Puppet.settings.stubs(:value).returns.returns("/my/directory") Puppet.settings.expects(:value).with(:modulepath, "myenv").returns("/my/modules") diff --git a/spec/unit/network/client/master.rb b/spec/unit/network/client/master.rb index 754fd0583..f55ba316c 100755 --- a/spec/unit/network/client/master.rb +++ b/spec/unit/network/client/master.rb @@ -397,4 +397,46 @@ describe Puppet::Network::Client::Master, " when using the cached catalog" do @client.catalog.should equal(ral_config) end + + describe "when calling splay" do + it "should do nothing if splay is not enabled" do + Puppet.stubs(:[]).with(:splay).returns(false) + @client.expects(:rand).never + @client.send(:splay) + end + + describe "when splay is enabled" do + before do + Puppet.stubs(:[]).with(:splay).returns(true) + Puppet.stubs(:[]).with(:splaylimit).returns(42) + end + + it "should sleep for a random time" do + @client.expects(:rand).with(42).returns(42) + @client.expects(:sleep).with(42) + @client.send(:splay) + end + + it "should inform that it is splayed" do + @client.stubs(:rand).with(42).returns(42) + @client.stubs(:sleep).with(42) + Puppet.expects(:info) + @client.send(:splay) + end + + it "should set splay = true" do + @client.stubs(:rand).with(42).returns(42) + @client.stubs(:sleep).with(42) + @client.send(:splay) + @client.send(:splayed?).should == true + end + + it "should do nothing if already splayed" do + @client.stubs(:rand).with(42).returns(42).at_most_once + @client.stubs(:sleep).with(42).at_most_once + @client.send(:splay) + @client.send(:splay) + end + end + end end diff --git a/spec/unit/parameter.rb b/spec/unit/parameter.rb new file mode 100755 index 000000000..d6858c29d --- /dev/null +++ b/spec/unit/parameter.rb @@ -0,0 +1,24 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../spec_helper' + +require 'puppet/parameter' + +describe Puppet::Parameter do + describe "when returning the value" do + before do + @class = Class.new(Puppet::Parameter) + @class.initvars + @parameter = @class.new :resource => mock('resource') + end + + it "should return nil if no value is set" do + @parameter.value.should be_nil + end + + it "should return any set value" do + @parameter.value = "foo" + @parameter.value.should == "foo" + end + end +end diff --git a/spec/unit/parser/ast/vardef.rb b/spec/unit/parser/ast/vardef.rb new file mode 100755 index 000000000..6bd355c89 --- /dev/null +++ b/spec/unit/parser/ast/vardef.rb @@ -0,0 +1,47 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../../spec_helper' + +describe Puppet::Parser::AST::VarDef do + before :each do + @scope = Puppet::Parser::Scope.new() + end + + describe "when evaluating" do + + it "should evaluate arguments" do + name = mock 'name' + value = mock 'value' + + name.expects(:safeevaluate).with(@scope) + value.expects(:safeevaluate).with(@scope) + + vardef = Puppet::Parser::AST::VarDef.new :name => name, :value => value, :file => nil, + :line => nil + vardef.evaluate(@scope) + end + + it "should be in append=false mode if called without append" do + name = stub 'name', :safeevaluate => "var" + value = stub 'value', :safeevaluate => "1" + + @scope.expects(:setvar).with { |name,value,file,line,append| append == nil } + + vardef = Puppet::Parser::AST::VarDef.new :name => name, :value => value, :file => nil, + :line => nil + vardef.evaluate(@scope) + end + + it "should call scope in append mode if append is true" do + name = stub 'name', :safeevaluate => "var" + value = stub 'value', :safeevaluate => "1" + + @scope.expects(:setvar).with { |name,value,file,line,append| append == true } + + vardef = Puppet::Parser::AST::VarDef.new :name => name, :value => value, :file => nil, + :line => nil, :append => true + vardef.evaluate(@scope) + end + + end +end diff --git a/spec/unit/parser/lexer.rb b/spec/unit/parser/lexer.rb index fb666054d..fed1ade7d 100755 --- a/spec/unit/parser/lexer.rb +++ b/spec/unit/parser/lexer.rb @@ -135,7 +135,8 @@ describe Puppet::Parser::Lexer::TOKENS do :QMARK => '?', :BACKSLASH => '\\', :FARROW => '=>', - :PARROW => '+>' + :PARROW => '+>', + :APPENDS => '+=' }.each do |name, string| it "should have a token named #{name.to_s}" do Puppet::Parser::Lexer::TOKENS[name].should_not be_nil diff --git a/spec/unit/parser/parser.rb b/spec/unit/parser/parser.rb new file mode 100755 index 000000000..94b19be40 --- /dev/null +++ b/spec/unit/parser/parser.rb @@ -0,0 +1,34 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../spec_helper' + +describe Puppet::Parser do + + AST = Puppet::Parser::AST + + before :each do + @parser = Puppet::Parser::Parser.new :environment => "development" + end + + describe "when parsing append operator" do + + it "should not raise syntax errors" do + lambda { @parser.parse("$var += something") }.should_not raise_error + end + + it "shouldraise syntax error on incomplete syntax " do + lambda { @parser.parse("$var += ") }.should raise_error + end + + it "should call AST::VarDef with append=true" do + AST::VarDef.expects(:new).with { |h| h[:append] == true } + @parser.parse("$var += 2") + end + + it "should work with arrays too" do + AST::VarDef.expects(:new).with { |h| h[:append] == true } + @parser.parse("$var += ['test']") + end + + end +end diff --git a/spec/unit/parser/scope.rb b/spec/unit/parser/scope.rb new file mode 100755 index 000000000..ec8ab6d7d --- /dev/null +++ b/spec/unit/parser/scope.rb @@ -0,0 +1,37 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../spec_helper' + +describe Puppet::Parser::Scope do + before :each do + @scope = Puppet::Parser::Scope.new() + @topscope = Puppet::Parser::Scope.new() + @scope.stubs(:parent).returns(@topscope) + end + + describe Puppet::Parser::Scope, "when setvar is called with append=true" do + + it "should raise error if the variable is already defined in this scope" do + @scope.setvar("var","1",nil,nil,false) + lambda { @scope.setvar("var","1",nil,nil,true) }.should raise_error(Puppet::ParseError) + end + + it "it should lookup current variable value" do + @scope.expects(:lookupvar).with("var").returns("2") + @scope.setvar("var","1",nil,nil,true) + end + + it "it should store the concatenated string '42'" do + @topscope.setvar("var","4",nil,nil,false) + @scope.setvar("var","2",nil,nil,true) + @scope.lookupvar("var").should == "42" + end + + it "it should store the concatenated array [4,2]" do + @topscope.setvar("var",[4],nil,nil,false) + @scope.setvar("var",[2],nil,nil,true) + @scope.lookupvar("var").should == [4,2] + end + + end +end diff --git a/spec/unit/property.rb b/spec/unit/property.rb new file mode 100755 index 000000000..e5b1e0013 --- /dev/null +++ b/spec/unit/property.rb @@ -0,0 +1,35 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../spec_helper' + +require 'puppet/property' + +describe Puppet::Property do + describe "when setting the value" do + it "should just set the 'should' value" do + @class = Class.new(Puppet::Property) + @class.initvars + @property = @class.new :resource => mock('resource') + + @property.expects(:should=).with("foo") + @property.value = "foo" + end + end + + describe "when returning the value" do + before do + @class = Class.new(Puppet::Property) + @class.initvars + @property = @class.new :resource => mock('resource') + end + + it "should return nil if no value is set" do + @property.value.should be_nil + end + + it "should return any set 'should' value" do + @property.should = "foo" + @property.value.should == "foo" + end + end +end diff --git a/spec/unit/provider/service/daemontools.rb b/spec/unit/provider/service/daemontools.rb new file mode 100644 index 000000000..29e9dd5be --- /dev/null +++ b/spec/unit/provider/service/daemontools.rb @@ -0,0 +1,124 @@ +#!/usr/bin/env ruby +# +# Unit testing for the Daemontools service Provider +# +# author Brice Figureau +# +require File.dirname(__FILE__) + '/../../../spec_helper' + +provider_class = Puppet::Type.type(:service).provider(:daemontools) + +describe provider_class do + + before(:each) do + # Create a mock resource + @resource = stub 'resource' + + @provider = provider_class.new + @servicedir = "/etc/service" + @provider.servicedir=@servicedir + @daemondir = "/var/lib/service" + @provider.class.defpath=@daemondir + + # A catch all; no parameters set + @resource.stubs(:[]).returns(nil) + + # But set name, source and path (because we won't run + # the thing that will fetch the resource path from the provider) + @resource.stubs(:[]).with(:name).returns "myservice" + @resource.stubs(:[]).with(:ensure).returns :enabled + @resource.stubs(:[]).with(:path).returns @daemondir + @resource.stubs(:ref).returns "Service[myservice]" + + @provider.stubs(:resource).returns @resource + end + + it "should have a restartcmd method" do + @provider.should respond_to(:restartcmd) + end + + it "should have a start method" do + @provider.should respond_to(:start) + end + + it "should have a stop method" do + @provider.should respond_to(:stop) + end + + it "should have an enabled? method" do + @provider.should respond_to(:enabled?) + end + + it "should have an enable method" do + @provider.should respond_to(:enable) + end + + it "should have a disable method" do + @provider.should respond_to(:disable) + end + + describe "when starting" do + it "should call enable" do + @provider.expects(:enable) + @provider.start + end + end + + describe "when stopping" do + it "should call disable" do + @provider.expects(:disable) + @provider.stop + end + end + + describe "when enabling" do + it "should create a symlink between daemon dir and service dir" do + FileTest.stubs(:symlink?).returns(false) + File.expects(:symlink).with(File.join(@daemondir,"myservice"), File.join(@servicedir,"myservice")).returns(0) + @provider.enable + end + end + + describe "when disabling" do + it "should stop and then remove the symlink between daemon dir and service dir" do + FileTest.stubs(:directory?).returns(false) + FileTest.stubs(:symlink?).returns(true) + File.expects(:unlink).with(File.join(@servicedir,"myservice")).returns(0) + @provider.stubs(:texecute).returns("") + @provider.disable + end + end + + describe "when disabling" do + it "should also call 'svc -dx /etc/service/myservice'" do + FileTest.stubs(:directory?).returns(false) + FileTest.stubs(:symlink?).returns(true) + File.expects(:unlink).with(File.join(@servicedir,"myservice")).returns(0) + @provider.expects(:texecute).with("stop", [nil, '-dx', File.join(@servicedir,"myservice")]).returns "" + @provider.disable + end + end + + describe "when checking status" do + it "should call the external command 'svstat /etc/service/myservice'" do + @provider.expects(:svstat).with(File.join(@servicedir,"myservice")) + @provider.status + end + end + + describe "when checking status" do + it "and svstat fails, properly raise a Puppet::Error" do + @provider.expects(:svstat).with(File.join(@servicedir,"myservice")).raises(Puppet::ExecutionFailure, "failure") + lambda { @provider.status }.should raise_error(Puppet::Error, 'Could not get status for service Service[myservice]: failure') + end + it "and svstat returns up, then return :running" do + @provider.expects(:svstat).with(File.join(@servicedir,"myservice")).returns("/etc/service/myservice: up (pid 454) 954326 seconds") + @provider.status.should == :running + end + it "and svstat returns not running, then return :stopped" do + @provider.expects(:svstat).with(File.join(@servicedir,"myservice")).returns("/etc/service/myservice: supervise not running") + @provider.status.should == :stopped + end + end + + end diff --git a/spec/unit/provider/service/runit.rb b/spec/unit/provider/service/runit.rb new file mode 100644 index 000000000..8eb53849b --- /dev/null +++ b/spec/unit/provider/service/runit.rb @@ -0,0 +1,117 @@ +#!/usr/bin/env ruby +# +# Unit testing for the Runit service Provider +# +# author Brice Figureau +# +require File.dirname(__FILE__) + '/../../../spec_helper' + +provider_class = Puppet::Type.type(:service).provider(:runit) + +describe provider_class do + + before(:each) do + # Create a mock resource + @resource = stub 'resource' + + @provider = provider_class.new + @servicedir = "/etc/service" + @provider.servicedir=@servicedir + @daemondir = "/etc/sv" + @provider.class.defpath=@daemondir + + # A catch all; no parameters set + @resource.stubs(:[]).returns(nil) + + # But set name, source and path (because we won't run + # the thing that will fetch the resource path from the provider) + @resource.stubs(:[]).with(:name).returns "myservice" + @resource.stubs(:[]).with(:ensure).returns :enabled + @resource.stubs(:[]).with(:path).returns @daemondir + @resource.stubs(:ref).returns "Service[myservice]" + + @provider.stubs(:resource).returns @resource + end + + it "should have a restartcmd method" do + @provider.should respond_to(:restartcmd) + end + + it "should have a start method" do + @provider.should respond_to(:start) + end + + it "should have a stop method" do + @provider.should respond_to(:stop) + end + + it "should have an enabled? method" do + @provider.should respond_to(:enabled?) + end + + it "should have an enable method" do + @provider.should respond_to(:enable) + end + + it "should have a disable method" do + @provider.should respond_to(:disable) + end + + describe "when starting" do + it "should call enable" do + @provider.expects(:enable) + @provider.start + end + end + + describe "when stopping" do + it "should execute external command 'sv stop /etc/service/myservice'" do + @provider.expects(:ucommand).with(:stop).returns("") + @provider.stop + end + end + + describe "when enabling" do + it "should create a symlink between daemon dir and service dir" do + FileTest.stubs(:symlink?).returns(false) + File.expects(:symlink).with(File.join(@daemondir,"myservice"), File.join(@servicedir,"myservice")).returns(0) + @provider.enable + end + end + + describe "when disabling" do + it "should remove the '/etc/service/myservice' symlink" do + FileTest.stubs(:directory?).returns(false) + FileTest.stubs(:symlink?).returns(true) + File.expects(:unlink).with(File.join(@servicedir,"myservice")).returns(0) + @provider.disable + end + end + + describe "when checking status" do + it "should call the external command 'sv status /etc/sv/myservice'" do + @provider.expects(:sv).with('status',File.join(@daemondir,"myservice")) + @provider.status + end + end + + describe "when checking status" do + it "and sv status fails, properly raise a Puppet::Error" do + @provider.expects(:sv).with('status',File.join(@daemondir,"myservice")).raises(Puppet::ExecutionFailure, "fail: /etc/sv/myservice: file not found") + lambda { @provider.status }.should raise_error(Puppet::Error, 'Could not get status for service Service[myservice]: fail: /etc/sv/myservice: file not found') + end + it "and sv status returns up, then return :running" do + @provider.expects(:sv).with('status',File.join(@daemondir,"myservice")).returns("run: /etc/sv/myservice: (pid 9029) 6s") + @provider.status.should == :running + end + it "and sv status returns not running, then return :stopped" do + @provider.expects(:sv).with('status',File.join(@daemondir,"myservice")).returns("fail: /etc/sv/myservice: runsv not running") + @provider.status.should == :stopped + end + it "and sv status returns a warning, then return :stopped" do + @provider.expects(:sv).with('status',File.join(@daemondir,"myservice")).returns("warning: /etc/sv/myservice: unable to open supervise/ok: file does not exist") + @provider.status.should == :stopped + end + end + + end diff --git a/spec/unit/provider/user/hpux.rb b/spec/unit/provider/user/hpux.rb new file mode 100755 index 000000000..4129a7ab6 --- /dev/null +++ b/spec/unit/provider/user/hpux.rb @@ -0,0 +1,25 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../../spec_helper' + +provider_class = Puppet::Type.type(:user).provider(:hpuxuseradd) + +describe provider_class do + # left from the useradd test... I have no clue what I'm doing. + before do + @resource = stub("resource", :name => "myuser", :managehome? => nil, :should => "fakeval", :[] => "fakeval") + @provider = provider_class.new(@resource) + end + + it "should add -F when modifying a user" do + @resource.expects(:allowdupe?).returns true + @provider.expects(:execute).with { |args| args.include?("-F") } + @provider.uid = 1000 + end + + it "should add -F when deleting a user" do + @provider.stubs(:exists?).returns(true) + @provider.expects(:execute).with { |args| args.include?("-F") } + @provider.delete + end +end diff --git a/spec/unit/type/file.rb b/spec/unit/type/file.rb index 7f9688f0b..663c5dc15 100755 --- a/spec/unit/type/file.rb +++ b/spec/unit/type/file.rb @@ -69,6 +69,12 @@ describe Puppet::Type.type(:file) do @filesource.server.stubs(:describe).raises(Puppet::Network::XMLRPCClientError.new("Testing")) lambda { @file.retrieve }.should raise_error(Puppet::Error) end + + it "should fail during eval_generate if no remote sources exist" do + file = Puppet::Type.type(:file).create :path => "/foobar", :source => "/this/file/does/not/exist", :recurse => true + + lambda { file.eval_generate }.should raise_error(Puppet::Error) + end end describe "when managing links" do diff --git a/spec/unit/type/noop_metaparam.rb b/spec/unit/type/noop_metaparam.rb index 540603ef9..98cb0409e 100755 --- a/spec/unit/type/noop_metaparam.rb +++ b/spec/unit/type/noop_metaparam.rb @@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/../../spec_helper' -require 'puppet/metatype/metaparams' +require 'puppet/type' describe Puppet::Type.type(:file).attrclass(:noop) do before do diff --git a/spec/unit/util/ldap/connection.rb b/spec/unit/util/ldap/connection.rb index 9392466c7..8bc85a620 100755 --- a/spec/unit/util/ldap/connection.rb +++ b/spec/unit/util/ldap/connection.rb @@ -152,5 +152,18 @@ describe Puppet::Util::Ldap::Connection do Puppet::Util::Ldap::Connection.expects(:new).with { |host, port, options| options[:ssl] == false } Puppet::Util::Ldap::Connection.instance end + + it "should set the ldapuser if one is set" do + Puppet.settings.expects(:value).with(:ldapuser).returns "foo" + Puppet::Util::Ldap::Connection.expects(:new).with { |host, port, options| options[:user] == "foo" } + Puppet::Util::Ldap::Connection.instance + end + + it "should set the ldapuser and ldappassword if both is set" do + Puppet.settings.expects(:value).with(:ldapuser).returns "foo" + Puppet.settings.expects(:value).with(:ldappassword).returns "bar" + Puppet::Util::Ldap::Connection.expects(:new).with { |host, port, options| options[:user] == "foo" and options[:password] == "bar" } + Puppet::Util::Ldap::Connection.instance + end end end |