summaryrefslogtreecommitdiffstats
path: root/spec/unit
diff options
context:
space:
mode:
authorMax Martin <max@puppetlabs.com>2011-03-30 13:02:19 -0700
committerMax Martin <max@puppetlabs.com>2011-03-30 13:02:19 -0700
commit9c06fbd762cddcc41a7185a36f2a8e72879125eb (patch)
treebfd37ac5887307857d0b0d79a1db034010eed424 /spec/unit
parent4196699f5fbb90ceecbb709c8502622eaad39062 (diff)
parent11309a214148abb2167c1bd0adba0e035b2d8384 (diff)
Merge branch 'next'
* next: (39 commits) (#5908) Add support for new update-rc.d disable API (#6830) Fix tests that depended on special inherited behavior (#6830) Fix overly stubbed tests (#6830) Fix instance_variables now comes back as symbols (#6830) Fix badly stubbed Time object in test (#6830) Fix MD5 handling to work with Ruby 1.9 (#6830) Fix File class scoping (#6830) Handle case where array is actually a string (#6830) Fix case where instance_variables returns symbols in Ruby 1.9 (#6862) Add a default subject for the mail_patches rake task Fixed #6256 - Creation of rrd directory. (#6855) ResourceType#search now accepts a regex (#5477) Allow watch_file to watch non-existent files, especially site.pp (#5477) Allow watch_file to watch non-existent files, especially site.pp Fixing #6851 - ResourceType#find/search loads types Fixing Module#path detection Fixed #6850 - Clean up ResourceType#to_pson (#6830) Fix stat method calls to not use an unneeded argument (#4576) Raise an error when a node is classified into a non-existent class Update CHANGELOG for 2.6.7 ...
Diffstat (limited to 'spec/unit')
-rw-r--r--spec/unit/application/master_spec.rb4
-rwxr-xr-xspec/unit/file_serving/fileset_spec.rb8
-rwxr-xr-xspec/unit/indirector/active_record_spec.rb7
-rwxr-xr-xspec/unit/indirector/code_spec.rb9
-rwxr-xr-xspec/unit/indirector/direct_file_server_spec.rb9
-rwxr-xr-xspec/unit/indirector/exec_spec.rb12
-rwxr-xr-xspec/unit/indirector/file_server_spec.rb11
-rwxr-xr-xspec/unit/indirector/file_spec.rb9
-rwxr-xr-xspec/unit/indirector/ldap_spec.rb7
-rwxr-xr-xspec/unit/indirector/memory_spec.rb7
-rwxr-xr-xspec/unit/indirector/plain_spec.rb7
-rwxr-xr-xspec/unit/indirector/queue_spec.rb7
-rwxr-xr-xspec/unit/indirector/resource_type/parser_spec.rb95
-rwxr-xr-xspec/unit/indirector/rest_spec.rb18
-rwxr-xr-xspec/unit/indirector/ssl_file_spec.rb13
-rwxr-xr-xspec/unit/indirector/yaml_spec.rb13
-rwxr-xr-xspec/unit/module_spec.rb39
-rwxr-xr-xspec/unit/network/authconfig_spec.rb2
-rwxr-xr-xspec/unit/network/rest_authconfig_spec.rb2
-rwxr-xr-xspec/unit/node/environment_spec.rb29
-rwxr-xr-xspec/unit/parser/ast/casestatement_spec.rb26
-rwxr-xr-xspec/unit/parser/compiler_spec.rb24
-rwxr-xr-xspec/unit/parser/functions/create_resources_spec.rb135
-rwxr-xr-xspec/unit/parser/lexer_spec.rb12
-rw-r--r--spec/unit/parser/type_loader_spec.rb97
-rw-r--r--spec/unit/provider/augeas/augeas_spec.rb54
-rwxr-xr-xspec/unit/provider/service/debian_spec.rb16
-rwxr-xr-xspec/unit/provider/service/smf_spec.rb137
-rwxr-xr-xspec/unit/provider/zfs/solaris_spec.rb1
-rwxr-xr-xspec/unit/resource/type_spec.rb22
-rwxr-xr-xspec/unit/type/exec_spec.rb16
-rwxr-xr-xspec/unit/util/loadedfile_spec.rb7
-rwxr-xr-xspec/unit/util/rdoc/parser_spec.rb7
33 files changed, 720 insertions, 142 deletions
diff --git a/spec/unit/application/master_spec.rb b/spec/unit/application/master_spec.rb
index 1173752d9..14478a61a 100644
--- a/spec/unit/application/master_spec.rb
+++ b/spec/unit/application/master_spec.rb
@@ -177,8 +177,8 @@ describe Puppet::Application::Master do
lambda { @master.setup }.should raise_error(SystemExit)
end
- it "should tell Puppet.settings to use :main,:ssl and :master category" do
- Puppet.settings.expects(:use).with(:main,:master,:ssl)
+ it "should tell Puppet.settings to use :main,:ssl,:master and :metrics category" do
+ Puppet.settings.expects(:use).with(:main,:master,:ssl,:metrics)
@master.setup
end
diff --git a/spec/unit/file_serving/fileset_spec.rb b/spec/unit/file_serving/fileset_spec.rb
index 1ef9cdc50..3b12c953e 100755
--- a/spec/unit/file_serving/fileset_spec.rb
+++ b/spec/unit/file_serving/fileset_spec.rb
@@ -13,6 +13,14 @@ describe Puppet::FileServing::Fileset, " when initializing" do
proc { Puppet::FileServing::Fileset.new("some/file") }.should raise_error(ArgumentError)
end
+ it "should not fail if the path is fully qualified, with a trailing separator" do
+ path = "/some/path/with/trailing/separator"
+ path_with_separator = "#{path}#{File::SEPARATOR}"
+ File.stubs(:lstat).with(path).returns stub('stat')
+ fileset = Puppet::FileServing::Fileset.new(path_with_separator)
+ fileset.path.should == path
+ end
+
it "should fail if its path does not exist" do
File.expects(:lstat).with("/some/file").returns nil
proc { Puppet::FileServing::Fileset.new("/some/file") }.should raise_error(ArgumentError)
diff --git a/spec/unit/indirector/active_record_spec.rb b/spec/unit/indirector/active_record_spec.rb
index 4765cc5ea..4fab17da2 100755
--- a/spec/unit/indirector/active_record_spec.rb
+++ b/spec/unit/indirector/active_record_spec.rb
@@ -14,10 +14,9 @@ describe Puppet::Indirector::ActiveRecord do
@indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model
Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection)
- @active_record_class = Class.new(Puppet::Indirector::ActiveRecord) do
- def self.to_s
- "Mystuff::Testing"
- end
+ module Testing; end
+ @active_record_class = class Testing::MyActiveRecord < Puppet::Indirector::ActiveRecord
+ self
end
@ar_model = mock 'ar_model'
diff --git a/spec/unit/indirector/code_spec.rb b/spec/unit/indirector/code_spec.rb
index 452d009f8..1c9e4d2f1 100755
--- a/spec/unit/indirector/code_spec.rb
+++ b/spec/unit/indirector/code_spec.rb
@@ -4,16 +4,15 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
require 'puppet/indirector/code'
describe Puppet::Indirector::Code do
- before do
+ before :all do
Puppet::Indirector::Terminus.stubs(:register_terminus_class)
@model = mock 'model'
@indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model
Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection)
- @code_class = Class.new(Puppet::Indirector::Code) do
- def self.to_s
- "Mystuff::Testing"
- end
+ module Testing; end
+ @code_class = class Testing::MyCode < Puppet::Indirector::Code
+ self
end
@searcher = @code_class.new
diff --git a/spec/unit/indirector/direct_file_server_spec.rb b/spec/unit/indirector/direct_file_server_spec.rb
index 8eab4ad76..5d9af626f 100755
--- a/spec/unit/indirector/direct_file_server_spec.rb
+++ b/spec/unit/indirector/direct_file_server_spec.rb
@@ -8,16 +8,15 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
require 'puppet/indirector/direct_file_server'
describe Puppet::Indirector::DirectFileServer do
- before :each do
+ before :all do
Puppet::Indirector::Terminus.stubs(:register_terminus_class)
@model = mock 'model'
@indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model
Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection)
- @direct_file_class = Class.new(Puppet::Indirector::DirectFileServer) do
- def self.to_s
- "Testing::Mytype"
- end
+ module Testing; end
+ @direct_file_class = class Testing::Mytype < Puppet::Indirector::DirectFileServer
+ self
end
@server = @direct_file_class.new
diff --git a/spec/unit/indirector/exec_spec.rb b/spec/unit/indirector/exec_spec.rb
index d4fb22443..5abb00ae9 100755
--- a/spec/unit/indirector/exec_spec.rb
+++ b/spec/unit/indirector/exec_spec.rb
@@ -5,17 +5,17 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
require 'puppet/indirector/exec'
describe Puppet::Indirector::Exec do
- before do
+ before :all do
@indirection = stub 'indirection', :name => :testing
Puppet::Indirector::Indirection.expects(:instance).with(:testing).returns(@indirection)
- @exec_class = Class.new(Puppet::Indirector::Exec) do
- def self.to_s
- "Testing::Mytype"
- end
-
+ module Testing; end
+ @exec_class = class Testing::MyTesting < Puppet::Indirector::Exec
attr_accessor :command
+ self
end
+ end
+ before :each do
@searcher = @exec_class.new
@searcher.command = ["/echo"]
diff --git a/spec/unit/indirector/file_server_spec.rb b/spec/unit/indirector/file_server_spec.rb
index a81d50487..079eba0ae 100755
--- a/spec/unit/indirector/file_server_spec.rb
+++ b/spec/unit/indirector/file_server_spec.rb
@@ -10,18 +10,19 @@ require 'puppet/file_serving/configuration'
describe Puppet::Indirector::FileServer do
- before :each do
+ before :all do
Puppet::Indirector::Terminus.stubs(:register_terminus_class)
@model = mock 'model'
@indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model
Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection)
- @file_server_class = Class.new(Puppet::Indirector::FileServer) do
- def self.to_s
- "Testing::Mytype"
- end
+ module Testing; end
+ @file_server_class = class Testing::MyFileServer < Puppet::Indirector::FileServer
+ self
end
+ end
+ before :each do
@file_server = @file_server_class.new
@uri = "puppet://host/my/local/file"
diff --git a/spec/unit/indirector/file_spec.rb b/spec/unit/indirector/file_spec.rb
index 8fd197eac..96d5b2ae7 100755
--- a/spec/unit/indirector/file_spec.rb
+++ b/spec/unit/indirector/file_spec.rb
@@ -5,16 +5,15 @@ require 'puppet/indirector/file'
describe Puppet::Indirector::File do
- before :each do
+ before :all do
Puppet::Indirector::Terminus.stubs(:register_terminus_class)
@model = mock 'model'
@indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model
Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection)
- @file_class = Class.new(Puppet::Indirector::File) do
- def self.to_s
- "Testing::Mytype"
- end
+ module Testing; end
+ @file_class = class Testing::MyFile < Puppet::Indirector::File
+ self
end
@searcher = @file_class.new
diff --git a/spec/unit/indirector/ldap_spec.rb b/spec/unit/indirector/ldap_spec.rb
index 2178a8709..ab5dab9ce 100755
--- a/spec/unit/indirector/ldap_spec.rb
+++ b/spec/unit/indirector/ldap_spec.rb
@@ -8,10 +8,9 @@ describe Puppet::Indirector::Ldap do
before do
@indirection = stub 'indirection', :name => :testing
Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection)
- @ldap_class = Class.new(Puppet::Indirector::Ldap) do
- def self.to_s
- "Testing::Mytype"
- end
+ module Testing; end
+ @ldap_class = class Testing::MyLdap < Puppet::Indirector::Ldap
+ self
end
@connection = mock 'ldap'
diff --git a/spec/unit/indirector/memory_spec.rb b/spec/unit/indirector/memory_spec.rb
index f9010326f..751adb1b6 100755
--- a/spec/unit/indirector/memory_spec.rb
+++ b/spec/unit/indirector/memory_spec.rb
@@ -14,10 +14,9 @@ describe Puppet::Indirector::Memory do
@indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model
Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection)
- @memory_class = Class.new(Puppet::Indirector::Memory) do
- def self.to_s
- "Mystuff::Testing"
- end
+ module Testing; end
+ @memory_class = class Testing::MyMemory < Puppet::Indirector::Memory
+ self
end
@searcher = @memory_class.new
diff --git a/spec/unit/indirector/plain_spec.rb b/spec/unit/indirector/plain_spec.rb
index 54c0233f8..dfaa701bd 100755
--- a/spec/unit/indirector/plain_spec.rb
+++ b/spec/unit/indirector/plain_spec.rb
@@ -10,10 +10,9 @@ describe Puppet::Indirector::Plain do
@indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model
Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection)
- @plain_class = Class.new(Puppet::Indirector::Plain) do
- def self.to_s
- "Mystuff::Testing"
- end
+ module Testing; end
+ @plain_class = class Testing::MyPlain < Puppet::Indirector::Plain
+ self
end
@searcher = @plain_class.new
diff --git a/spec/unit/indirector/queue_spec.rb b/spec/unit/indirector/queue_spec.rb
index 49e5e1015..6f5b44b4c 100755
--- a/spec/unit/indirector/queue_spec.rb
+++ b/spec/unit/indirector/queue_spec.rb
@@ -31,10 +31,9 @@ describe Puppet::Indirector::Queue, :if => Puppet.features.pson? do
@model = mock 'model'
@indirection = stub 'indirection', :name => :my_queue, :register_terminus_type => nil, :model => @model
Puppet::Indirector::Indirection.stubs(:instance).with(:my_queue).returns(@indirection)
- @store_class = Class.new(Puppet::Indirector::Queue) do
- def self.to_s
- 'MyQueue::MyType'
- end
+ module MyQueue; end
+ @store_class = class MyQueue::MyType < Puppet::Indirector::Queue
+ self
end
@store = @store_class.new
diff --git a/spec/unit/indirector/resource_type/parser_spec.rb b/spec/unit/indirector/resource_type/parser_spec.rb
index 739e58b35..f86b319f9 100755
--- a/spec/unit/indirector/resource_type/parser_spec.rb
+++ b/spec/unit/indirector/resource_type/parser_spec.rb
@@ -3,13 +3,15 @@
require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')
require 'puppet/indirector/resource_type/parser'
+require 'puppet_spec/files'
describe Puppet::Indirector::ResourceType::Parser do
+ include PuppetSpec::Files
+
before do
@terminus = Puppet::Indirector::ResourceType::Parser.new
@request = Puppet::Indirector::Request.new(:resource_type, :find, "foo")
- @krt = Puppet::Resource::TypeCollection.new(@request.environment)
- @request.environment.stubs(:known_resource_types).returns @krt
+ @krt = @request.environment.known_resource_types
end
it "should be registered with the resource_type indirection" do
@@ -17,16 +19,29 @@ describe Puppet::Indirector::ResourceType::Parser do
end
describe "when finding" do
- it "should use the request's environment's list of known resource types" do
- @request.environment.known_resource_types.expects(:hostclass).returns nil
+ it "should return any found type from the request's environment" do
+ type = Puppet::Resource::Type.new(:hostclass, "foo")
+ @request.environment.known_resource_types.add(type)
- @terminus.find(@request)
+ @terminus.find(@request).should == type
end
- it "should return any found type" do
- type = @krt.add(Puppet::Resource::Type.new(:hostclass, "foo"))
+ it "should attempt to load the type if none is found in memory" do
+ dir = tmpdir("find_a_type")
+ FileUtils.mkdir_p(dir)
+ Puppet[:modulepath] = dir
- @terminus.find(@request).should == type
+ # Make a new request, since we've reset the env
+ @request = Puppet::Indirector::Request.new(:resource_type, :find, "foo::bar")
+
+ manifest_path = File.join(dir, "foo", "manifests")
+ FileUtils.mkdir_p(manifest_path)
+
+ File.open(File.join(manifest_path, "bar.pp"), "w") { |f| f.puts "class foo::bar {}" }
+
+ result = @terminus.find(@request)
+ result.should be_instance_of(Puppet::Resource::Type)
+ result.name.should == "foo::bar"
end
it "should return nil if no type can be found" do
@@ -52,8 +67,35 @@ describe Puppet::Indirector::ResourceType::Parser do
@terminus.search(@request)
end
- it "should fail if anyther other than '*' was provided as the search key" do
- @request.key = "foo*"
+ it "should return all results if '*' is provided as the search string" do
+ @request.key = "*"
+ type = @krt.add(Puppet::Resource::Type.new(:hostclass, "foo"))
+ node = @krt.add(Puppet::Resource::Type.new(:node, "bar"))
+ define = @krt.add(Puppet::Resource::Type.new(:definition, "baz"))
+
+ result = @terminus.search(@request)
+ result.should be_include(type)
+ result.should be_include(node)
+ result.should be_include(define)
+ end
+
+ it "should treat any search string not '*' as a regex" do
+ @request.key = "a"
+ foo = @krt.add(Puppet::Resource::Type.new(:hostclass, "foo"))
+ bar = @krt.add(Puppet::Resource::Type.new(:hostclass, "bar"))
+ baz = @krt.add(Puppet::Resource::Type.new(:hostclass, "baz"))
+
+ result = @terminus.search(@request)
+ result.should be_include(bar)
+ result.should be_include(baz)
+ result.should_not be_include(foo)
+ end
+
+ it "should fail if a provided search string is not '*' and is not a valid regex" do
+ @request.key = "*foo*"
+
+ # Add one instance so we don't just get an empty array"
+ @krt.add(Puppet::Resource::Type.new(:hostclass, "foo"))
lambda { @terminus.search(@request) }.should raise_error(ArgumentError)
end
@@ -68,8 +110,41 @@ describe Puppet::Indirector::ResourceType::Parser do
result.should be_include(define)
end
+ it "should not return the 'main' class" do
+ main = @krt.add(Puppet::Resource::Type.new(:hostclass, ""))
+
+ # So there is a return value
+ foo = @krt.add(Puppet::Resource::Type.new(:hostclass, "foo"))
+
+ @terminus.search(@request).should_not be_include(main)
+ end
+
it "should return nil if no types can be found" do
@terminus.search(@request).should be_nil
end
+
+ it "should load all resource types from all search paths" do
+ dir = tmpdir("searching_in_all")
+ first = File.join(dir, "first")
+ second = File.join(dir, "second")
+ FileUtils.mkdir_p(first)
+ FileUtils.mkdir_p(second)
+ Puppet[:modulepath] = "#{first}:#{second}"
+
+ # Make a new request, since we've reset the env
+ @request = Puppet::Indirector::Request.new(:resource_type, :search, "*")
+
+ onepath = File.join(first, "one", "manifests")
+ FileUtils.mkdir_p(onepath)
+ twopath = File.join(first, "two", "manifests")
+ FileUtils.mkdir_p(twopath)
+
+ File.open(File.join(onepath, "oneklass.pp"), "w") { |f| f.puts "class one::oneklass {}" }
+ File.open(File.join(twopath, "twoklass.pp"), "w") { |f| f.puts "class two::twoklass {}" }
+
+ result = @terminus.search(@request)
+ result.find { |t| t.name == "one::oneklass" }.should be_instance_of(Puppet::Resource::Type)
+ result.find { |t| t.name == "two::twoklass" }.should be_instance_of(Puppet::Resource::Type)
+ end
end
end
diff --git a/spec/unit/indirector/rest_spec.rb b/spec/unit/indirector/rest_spec.rb
index 547e68dd3..326d85f9d 100755
--- a/spec/unit/indirector/rest_spec.rb
+++ b/spec/unit/indirector/rest_spec.rb
@@ -26,19 +26,27 @@ shared_examples_for "a REST http call" do
end
describe Puppet::Indirector::REST do
- before do
+ before :all do
Puppet::Indirector::Terminus.stubs(:register_terminus_class)
@model = stub('model', :supported_formats => %w{}, :convert_from => nil)
@instance = stub('model instance', :name= => nil)
@indirection = stub('indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model)
- Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection)
+ Puppet::Indirector::Indirection.expects(:instance).returns(@indirection)
- @rest_class = Class.new(Puppet::Indirector::REST) do
- def self.to_s
- "This::Is::A::Test::Class"
+ module This
+ module Is
+ module A
+ module Test
+ end
+ end
end
end
+ @rest_class = class This::Is::A::Test::Class < Puppet::Indirector::REST
+ self
+ end
+ end
+ before :each do
@response = stub('mock response', :body => 'result', :code => "200")
@response.stubs(:[]).with('content-type').returns "text/plain"
@response.stubs(:[]).with('content-encoding').returns nil
diff --git a/spec/unit/indirector/ssl_file_spec.rb b/spec/unit/indirector/ssl_file_spec.rb
index 4549127f9..ca97cf49e 100755
--- a/spec/unit/indirector/ssl_file_spec.rb
+++ b/spec/unit/indirector/ssl_file_spec.rb
@@ -8,15 +8,16 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
require 'puppet/indirector/ssl_file'
describe Puppet::Indirector::SslFile do
- before do
- @model = mock 'model'
+ before :all do
@indirection = stub 'indirection', :name => :testing, :model => @model
Puppet::Indirector::Indirection.expects(:instance).with(:testing).returns(@indirection)
- @file_class = Class.new(Puppet::Indirector::SslFile) do
- def self.to_s
- "Testing::Mytype"
- end
+ module Testing; end
+ @file_class = class Testing::MyType < Puppet::Indirector::SslFile
+ self
end
+ end
+ before :each do
+ @model = mock 'model'
@setting = :certdir
@file_class.store_in @setting
diff --git a/spec/unit/indirector/yaml_spec.rb b/spec/unit/indirector/yaml_spec.rb
index 35017991b..188e300d6 100755
--- a/spec/unit/indirector/yaml_spec.rb
+++ b/spec/unit/indirector/yaml_spec.rb
@@ -5,14 +5,15 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
require 'puppet/indirector/yaml'
describe Puppet::Indirector::Yaml, " when choosing file location" do
- before :each do
+ before :all do
@indirection = stub 'indirection', :name => :my_yaml, :register_terminus_type => nil
- Puppet::Indirector::Indirection.stubs(:instance).with(:my_yaml).returns(@indirection)
- @store_class = Class.new(Puppet::Indirector::Yaml) do
- def self.to_s
- "MyYaml::MyType"
- end
+ Puppet::Indirector::Indirection.expects(:instance).with(:my_yaml).returns(@indirection)
+ module MyYaml; end
+ @store_class = class MyYaml::MyType < Puppet::Indirector::Yaml
+ self
end
+ end
+ before :each do
@store = @store_class.new
@subject = Object.new
diff --git a/spec/unit/module_spec.rb b/spec/unit/module_spec.rb
index 54f5444ee..f3120e16b 100755
--- a/spec/unit/module_spec.rb
+++ b/spec/unit/module_spec.rb
@@ -1,8 +1,11 @@
#!/usr/bin/env ruby
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+require 'puppet_spec/files'
describe Puppet::Module do
+ include PuppetSpec::Files
+
before do
# This is necessary because of the extra checks we have for the deprecated
# 'plugins' directory
@@ -267,17 +270,39 @@ describe Puppet::Module do
end
it "should return the path to the first found instance in its environment's module paths as its path" do
+ dir = tmpdir("deep_path")
+ first = File.join(dir, "first")
+ second = File.join(dir, "second")
+
+ FileUtils.mkdir_p(first)
+ FileUtils.mkdir_p(second)
+ Puppet[:modulepath] = "#{first}:#{second}"
+
+ modpath = File.join(first, "foo")
+ FileUtils.mkdir_p(modpath)
+
+ # Make a second one, which we shouldn't find
+ FileUtils.mkdir_p(File.join(second, "foo"))
+
mod = Puppet::Module.new("foo")
- env = mock 'environment'
- mod.stubs(:environment).returns env
+ mod.path.should == modpath
+ end
+
+ it "should be able to find itself in a directory other than the first directory in the module path" do
+ dir = tmpdir("deep_path")
+ first = File.join(dir, "first")
+ second = File.join(dir, "second")
- env.expects(:modulepath).returns %w{/a /b /c}
+ FileUtils.mkdir_p(first)
+ FileUtils.mkdir_p(second)
+ Puppet[:modulepath] = "#{first}:#{second}"
- FileTest.expects(:exist?).with("/a/foo").returns false
- FileTest.expects(:exist?).with("/b/foo").returns true
- FileTest.expects(:exist?).with("/c/foo").never
+ modpath = File.join(second, "foo")
+ FileUtils.mkdir_p(modpath)
- mod.path.should == "/b/foo"
+ mod = Puppet::Module.new("foo")
+ mod.should be_exist
+ mod.path.should == modpath
end
it "should be considered existent if it exists in at least one module path" do
diff --git a/spec/unit/network/authconfig_spec.rb b/spec/unit/network/authconfig_spec.rb
index 4367e25e6..9d69e99ac 100755
--- a/spec/unit/network/authconfig_spec.rb
+++ b/spec/unit/network/authconfig_spec.rb
@@ -12,7 +12,7 @@ describe Puppet::Network::AuthConfig do
FileTest.stubs(:exists?).returns(true)
File.stubs(:stat).returns(stub('stat', :ctime => :now))
- Time.stubs(:now).returns :now
+ Time.stubs(:now).returns Time.now
@authconfig = Puppet::Network::AuthConfig.new("dummy", false)
end
diff --git a/spec/unit/network/rest_authconfig_spec.rb b/spec/unit/network/rest_authconfig_spec.rb
index 270d1d094..e0bcb5af0 100755
--- a/spec/unit/network/rest_authconfig_spec.rb
+++ b/spec/unit/network/rest_authconfig_spec.rb
@@ -22,7 +22,7 @@ describe Puppet::Network::RestAuthConfig do
before :each do
FileTest.stubs(:exists?).returns(true)
File.stubs(:stat).returns(stub('stat', :ctime => :now))
- Time.stubs(:now).returns :now
+ Time.stubs(:now).returns Time.now
@authconfig = Puppet::Network::RestAuthConfig.new("dummy", false)
@authconfig.stubs(:read)
diff --git a/spec/unit/node/environment_spec.rb b/spec/unit/node/environment_spec.rb
index 153be5f60..05527e70f 100755
--- a/spec/unit/node/environment_spec.rb
+++ b/spec/unit/node/environment_spec.rb
@@ -6,6 +6,7 @@ require 'puppet/node/environment'
require 'puppet/util/execution'
describe Puppet::Node::Environment do
+ include PuppetSpec::Files
after do
Puppet::Node::Environment.clear
end
@@ -276,16 +277,11 @@ describe Puppet::Node::Environment do
describe "when performing initial import" do
before do
- @parser = stub 'parser', :file= => nil, :string => nil, :parse => nil
+ @parser = stub 'parser'
Puppet::Parser::Parser.stubs(:new).returns @parser
@env = Puppet::Node::Environment.new("env")
end
- it "should create a new parser instance" do
- Puppet::Parser::Parser.expects(:new).returns @parser
- @env.instance_eval { perform_initial_import }
- end
-
it "should set the parser's string to the 'code' setting and parse if code is available" do
Puppet.settings[:code] = "my code"
@parser.expects(:string=).with "my code"
@@ -294,25 +290,26 @@ describe Puppet::Node::Environment do
end
it "should set the parser's file to the 'manifest' setting and parse if no code is available and the manifest is available" do
- File.stubs(:expand_path).with("/my/file").returns "/my/file"
- File.expects(:exist?).with("/my/file").returns true
- Puppet.settings[:manifest] = "/my/file"
- @parser.expects(:file=).with "/my/file"
+ filename = tmpfile('myfile')
+ File.open(filename, 'w'){|f| }
+ Puppet.settings[:manifest] = filename
+ @parser.expects(:file=).with filename
@parser.expects(:parse)
@env.instance_eval { perform_initial_import }
end
- it "should not attempt to load a manifest if none is present" do
- File.stubs(:expand_path).with("/my/file").returns "/my/file"
- File.expects(:exist?).with("/my/file").returns false
- Puppet.settings[:manifest] = "/my/file"
- @parser.expects(:file=).never
- @parser.expects(:parse).never
+ it "should pass the manifest file to the parser even if it does not exist on disk" do
+ filename = tmpfile('myfile')
+ Puppet.settings[:code] = ""
+ Puppet.settings[:manifest] = filename
+ @parser.expects(:file=).with(filename).once
+ @parser.expects(:parse).once
@env.instance_eval { perform_initial_import }
end
it "should fail helpfully if there is an error importing" do
File.stubs(:exist?).returns true
+ @parser.expects(:file=).once
@parser.expects(:parse).raises ArgumentError
lambda { @env.instance_eval { perform_initial_import } }.should raise_error(Puppet::Error)
end
diff --git a/spec/unit/parser/ast/casestatement_spec.rb b/spec/unit/parser/ast/casestatement_spec.rb
index a77c04c43..bce3ad801 100755
--- a/spec/unit/parser/ast/casestatement_spec.rb
+++ b/spec/unit/parser/ast/casestatement_spec.rb
@@ -13,11 +13,14 @@ describe Puppet::Parser::AST::CaseStatement do
@test = stub 'test'
@test.stubs(:safeevaluate).with(@scope).returns("value")
- @option1 = stub 'option1', :eachopt => nil, :default? => false
- @option2 = stub 'option2', :eachopt => nil, :default? => false
+ @option1 = Puppet::Parser::AST::CaseOpt.new({})
+ @option1.stubs(:eachopt)
+ @option1.stubs(:default?).returns false
+ @option2 = Puppet::Parser::AST::CaseOpt.new({})
+ @option2.stubs(:eachopt)
+ @option2.stubs(:default?).returns false
- @options = stub 'options'
- @options.stubs(:each).multiple_yields(@option1, @option2)
+ @options = Puppet::Parser::AST::ASTArray.new(:children => [@option1, @option2])
@casestmt = Puppet::Parser::AST::CaseStatement.new :test => @test, :options => @options
end
@@ -29,8 +32,6 @@ describe Puppet::Parser::AST::CaseStatement do
end
it "should scan each option" do
- @options.expects(:each).multiple_yields(@option1, @option2)
-
@casestmt.evaluate(@scope)
end
@@ -137,12 +138,15 @@ describe Puppet::Parser::AST::CaseStatement do
options = tests.collect do |result, values|
values = values.collect { |v| AST::Leaf.new :value => v }
- AST::CaseOpt.new(
- :value => AST::ASTArray.new(:children => values),
-
- :statements => AST::Leaf.new(:value => result))
+ AST::CaseOpt.new(
+ :value => AST::ASTArray.new(:children => values),
+ :statements => AST::Leaf.new(:value => result)
+ )
end
- options << AST::CaseOpt.new(:value => AST::Default.new(:value => "default"), :statements => AST::Leaf.new(:value => "default"))
+ options << AST::CaseOpt.new(
+ :value => AST::Default.new(:value => "default"),
+ :statements => AST::Leaf.new(:value => "default")
+ )
ast = nil
param = AST::Variable.new(:value => "testparam")
diff --git a/spec/unit/parser/compiler_spec.rb b/spec/unit/parser/compiler_spec.rb
index 18f9a93b6..e4b18e14b 100755
--- a/spec/unit/parser/compiler_spec.rb
+++ b/spec/unit/parser/compiler_spec.rb
@@ -576,18 +576,16 @@ describe Puppet::Parser::Compiler do
proc { @compiler.evaluate_classes(%w{one two}, scope) }.should raise_error(Puppet::DevError)
end
- it "should tag the catalog with the name of each not-found class" do
- @compiler.catalog.expects(:tag).with("notfound")
+ it "should raise an error if a class is not found" do
@scope.expects(:find_hostclass).with("notfound").returns(nil)
- @compiler.evaluate_classes(%w{notfound}, @scope)
+ lambda{ @compiler.evaluate_classes(%w{notfound}, @scope) }.should raise_error(Puppet::Error, /Could not find class/)
end
- # I wish it would fail
- it "should log when it can't find class" do
+
+ it "should raise an error when it can't find class" do
klasses = {'foo'=>nil}
@node.classes = klasses
@compiler.topscope.stubs(:find_hostclass).with('foo').returns(nil)
- Puppet.expects(:info).with('Could not find class foo for testnode')
- @compiler.compile
+ lambda{ @compiler.compile }.should raise_error(Puppet::Error, /Could not find class foo for testnode/)
end
end
@@ -714,18 +712,6 @@ describe Puppet::Parser::Compiler do
Puppet::Parser::Resource.expects(:new).never
@compiler.evaluate_classes(%w{MyClass}, @scope, false)
end
-
- it "should return the list of found classes" do
- @compiler.catalog.stubs(:tag)
-
- @compiler.stubs(:add_resource)
- @scope.stubs(:find_hostclass).with("notfound").returns(nil)
- @scope.stubs(:class_scope).with(@class)
-
- Puppet::Parser::Resource.stubs(:new).returns(@resource)
- @class.stubs :ensure_in_catalog
- @compiler.evaluate_classes(%w{myclass notfound}, @scope).should == %w{myclass}
- end
end
describe "when evaluating AST nodes with no AST nodes present" do
diff --git a/spec/unit/parser/functions/create_resources_spec.rb b/spec/unit/parser/functions/create_resources_spec.rb
new file mode 100755
index 000000000..d4095b777
--- /dev/null
+++ b/spec/unit/parser/functions/create_resources_spec.rb
@@ -0,0 +1,135 @@
+require 'puppet'
+require File.dirname(__FILE__) + '/../../../spec_helper'
+
+describe 'function for dynamically creating resources' do
+
+ def get_scope
+ @topscope = Puppet::Parser::Scope.new
+ # This is necessary so we don't try to use the compiler to discover our parent.
+ @topscope.parent = nil
+ @scope = Puppet::Parser::Scope.new
+ @scope.compiler = Puppet::Parser::Compiler.new(Puppet::Node.new("floppy", :environment => 'production'))
+ @scope.parent = @topscope
+ @compiler = @scope.compiler
+ end
+ before :each do
+ get_scope
+ Puppet::Parser::Functions.function(:create_resources)
+ end
+
+ it "should exist" do
+ Puppet::Parser::Functions.function(:create_resources).should == "function_create_resources"
+ end
+ it 'should require two arguments' do
+ lambda { @scope.function_create_resources(['foo']) }.should raise_error(ArgumentError, 'create_resources(): wrong number of arguments (1; must be 2)')
+ end
+ describe 'when creating native types' do
+ before :each do
+ Puppet[:code]='notify{test:}'
+ get_scope
+ @scope.resource=Puppet::Parser::Resource.new('class', 't', :scope => @scope)
+ end
+ it 'empty hash should not cause resources to be added' do
+ @scope.function_create_resources(['file', {}])
+ @compiler.catalog.resources.size == 1
+ end
+ it 'should be able to add' do
+ @scope.function_create_resources(['file', {'/etc/foo'=>{'ensure'=>'present'}}])
+ @compiler.catalog.resource(:file, "/etc/foo")['ensure'].should == 'present'
+ end
+ it 'should accept multiple types' do
+ type_hash = {}
+ type_hash['foo'] = {'message' => 'one'}
+ type_hash['bar'] = {'message' => 'two'}
+ @scope.function_create_resources(['notify', type_hash])
+ @compiler.catalog.resource(:notify, "foo")['message'].should == 'one'
+ @compiler.catalog.resource(:notify, "bar")['message'].should == 'two'
+ end
+ it 'should fail to add non-existing type' do
+ lambda { @scope.function_create_resources(['foo', {}]) }.should raise_error(ArgumentError, 'could not create resource of unknown type foo')
+ end
+ it 'should be able to add edges' do
+ @scope.function_create_resources(['notify', {'foo'=>{'require' => 'Notify[test]'}}])
+ @scope.compiler.compile
+ edge = @scope.compiler.catalog.to_ral.relationship_graph.edges.detect do |edge|
+ edge.source.title == 'test'
+ end
+ edge.source.title.should == 'test'
+ edge.target.title.should == 'foo'
+ end
+ end
+ describe 'when dynamically creating resource types' do
+ before :each do
+ Puppet[:code]=
+'define foo($one){notify{$name: message => $one}}
+notify{test:}
+'
+ get_scope
+ @scope.resource=Puppet::Parser::Resource.new('class', 't', :scope => @scope)
+ Puppet::Parser::Functions.function(:create_resources)
+ end
+ it 'should be able to create defined resoure types' do
+ @scope.function_create_resources(['foo', {'blah'=>{'one'=>'two'}}])
+ # still have to compile for this to work...
+ # I am not sure if this constraint ruins the tests
+ @scope.compiler.compile
+ @compiler.catalog.resource(:notify, "blah")['message'].should == 'two'
+ end
+ it 'should fail if defines are missing params' do
+ @scope.function_create_resources(['foo', {'blah'=>{}}])
+ lambda { @scope.compiler.compile }.should raise_error(Puppet::ParseError, 'Must pass one to Foo[blah] at line 1')
+ end
+ it 'should be able to add multiple defines' do
+ hash = {}
+ hash['blah'] = {'one' => 'two'}
+ hash['blaz'] = {'one' => 'three'}
+ @scope.function_create_resources(['foo', hash])
+ # still have to compile for this to work...
+ # I am not sure if this constraint ruins the tests
+ @scope.compiler.compile
+ @compiler.catalog.resource(:notify, "blah")['message'].should == 'two'
+ @compiler.catalog.resource(:notify, "blaz")['message'].should == 'three'
+ end
+ it 'should be able to add edges' do
+ @scope.function_create_resources(['foo', {'blah'=>{'one'=>'two', 'require' => 'Notify[test]'}}])
+ @scope.compiler.compile
+ edge = @scope.compiler.catalog.to_ral.relationship_graph.edges.detect do |edge|
+ edge.source.title == 'test'
+ end
+ edge.source.title.should == 'test'
+ edge.target.title.should == 'blah'
+ @compiler.catalog.resource(:notify, "blah")['message'].should == 'two'
+ end
+ end
+ describe 'when creating classes' do
+ before :each do
+ Puppet[:code]=
+'class bar($one){notify{test: message => $one}}
+notify{tester:}
+'
+ get_scope
+ @scope.resource=Puppet::Parser::Resource.new('class', 't', :scope => @scope)
+ Puppet::Parser::Functions.function(:create_resources)
+ end
+ it 'should be able to create classes' do
+ @scope.function_create_resources(['class', {'bar'=>{'one'=>'two'}}])
+ @scope.compiler.compile
+ @compiler.catalog.resource(:notify, "test")['message'].should == 'two'
+ @compiler.catalog.resource(:class, "bar").should_not be_nil#['message'].should == 'two'
+ end
+ it 'should fail to create non-existing classes' do
+ lambda { @scope.function_create_resources(['class', {'blah'=>{'one'=>'two'}}]) }.should raise_error(ArgumentError ,'could not find hostclass blah')
+ end
+ it 'should be able to add edges' do
+ @scope.function_create_resources(['class', {'bar'=>{'one'=>'two', 'require' => 'Notify[tester]'}}])
+ @scope.compiler.compile
+ edge = @scope.compiler.catalog.to_ral.relationship_graph.edges.detect do |e|
+ e.source.title == 'tester'
+ end
+ edge.source.title.should == 'tester'
+ edge.target.title.should == 'test'
+ #@compiler.catalog.resource(:notify, "blah")['message'].should == 'two'
+ end
+
+ end
+end
diff --git a/spec/unit/parser/lexer_spec.rb b/spec/unit/parser/lexer_spec.rb
index 96df61348..bc9e22e48 100755
--- a/spec/unit/parser/lexer_spec.rb
+++ b/spec/unit/parser/lexer_spec.rb
@@ -676,3 +676,15 @@ describe "Puppet::Parser::Lexer in the old tests when lexing example files" do
end
end
end
+
+describe "when trying to lex an non-existent file" do
+ include PuppetSpec::Files
+
+ it "should return an empty list of tokens" do
+ lexer = Puppet::Parser::Lexer.new
+ lexer.file = nofile = tmpfile('lexer')
+ File.exists?(nofile).should == false
+
+ lexer.fullscan.should == [[false,false]]
+ end
+end
diff --git a/spec/unit/parser/type_loader_spec.rb b/spec/unit/parser/type_loader_spec.rb
index bd41adfb6..12bc1ccd6 100644
--- a/spec/unit/parser/type_loader_spec.rb
+++ b/spec/unit/parser/type_loader_spec.rb
@@ -93,6 +93,103 @@ describe Puppet::Parser::TypeLoader do
end
end
+ describe "when importing all" do
+ before do
+ @base = tmpdir("base")
+
+ # Create two module path directories
+ @modulebase1 = File.join(@base, "first")
+ FileUtils.mkdir_p(@modulebase1)
+ @modulebase2 = File.join(@base, "second")
+ FileUtils.mkdir_p(@modulebase2)
+
+ Puppet[:modulepath] = "#{@modulebase1}:#{@modulebase2}"
+ end
+
+ def mk_module(basedir, name)
+ module_dir = File.join(basedir, name)
+
+ # Go ahead and make our manifest directory
+ FileUtils.mkdir_p(File.join(module_dir, "manifests"))
+
+ return Puppet::Module.new(name)
+ end
+
+ # We have to pass the base path so that we can
+ # write to modules that are in the second search path
+ def mk_manifests(base, mod, type, files)
+ exts = {"ruby" => ".rb", "puppet" => ".pp"}
+ files.collect do |file|
+ name = mod.name + "::" + file.gsub("/", "::")
+ path = File.join(base, mod.name, "manifests", file + exts[type])
+ FileUtils.mkdir_p(File.split(path)[0])
+
+ # write out the class
+ if type == "ruby"
+ File.open(path, "w") { |f| f.print "hostclass '#{name}' do\nend" }
+ else
+ File.open(path, "w") { |f| f.print "class #{name} {}" }
+ end
+ name
+ end
+ end
+
+ it "should load all puppet manifests from all modules in the specified environment" do
+ @module1 = mk_module(@modulebase1, "one")
+ @module2 = mk_module(@modulebase2, "two")
+
+ mk_manifests(@modulebase1, @module1, "puppet", %w{a b})
+ mk_manifests(@modulebase2, @module2, "puppet", %w{c d})
+
+ @loader.import_all
+
+ @loader.environment.known_resource_types.hostclass("one::a").should be_instance_of(Puppet::Resource::Type)
+ @loader.environment.known_resource_types.hostclass("one::b").should be_instance_of(Puppet::Resource::Type)
+ @loader.environment.known_resource_types.hostclass("two::c").should be_instance_of(Puppet::Resource::Type)
+ @loader.environment.known_resource_types.hostclass("two::d").should be_instance_of(Puppet::Resource::Type)
+ end
+
+ it "should load all ruby manifests from all modules in the specified environment" do
+ @module1 = mk_module(@modulebase1, "one")
+ @module2 = mk_module(@modulebase2, "two")
+
+ mk_manifests(@modulebase1, @module1, "ruby", %w{a b})
+ mk_manifests(@modulebase2, @module2, "ruby", %w{c d})
+
+ @loader.import_all
+
+ @loader.environment.known_resource_types.hostclass("one::a").should be_instance_of(Puppet::Resource::Type)
+ @loader.environment.known_resource_types.hostclass("one::b").should be_instance_of(Puppet::Resource::Type)
+ @loader.environment.known_resource_types.hostclass("two::c").should be_instance_of(Puppet::Resource::Type)
+ @loader.environment.known_resource_types.hostclass("two::d").should be_instance_of(Puppet::Resource::Type)
+ end
+
+ it "should not load manifests from duplicate modules later in the module path" do
+ @module1 = mk_module(@modulebase1, "one")
+
+ # duplicate
+ @module2 = mk_module(@modulebase2, "one")
+
+ mk_manifests(@modulebase1, @module1, "puppet", %w{a})
+ mk_manifests(@modulebase2, @module2, "puppet", %w{c})
+
+ @loader.import_all
+
+ @loader.environment.known_resource_types.hostclass("one::c").should be_nil
+ end
+
+ it "should load manifests from subdirectories" do
+ @module1 = mk_module(@modulebase1, "one")
+
+ mk_manifests(@modulebase1, @module1, "puppet", %w{a a/b a/b/c})
+
+ @loader.import_all
+
+ @loader.environment.known_resource_types.hostclass("one::a::b").should be_instance_of(Puppet::Resource::Type)
+ @loader.environment.known_resource_types.hostclass("one::a::b::c").should be_instance_of(Puppet::Resource::Type)
+ end
+ end
+
describe "when parsing a file" do
before do
@parser = Puppet::Parser::Parser.new(@loader.environment)
diff --git a/spec/unit/provider/augeas/augeas_spec.rb b/spec/unit/provider/augeas/augeas_spec.rb
index 9fcc85660..c65f39097 100644
--- a/spec/unit/provider/augeas/augeas_spec.rb
+++ b/spec/unit/provider/augeas/augeas_spec.rb
@@ -433,5 +433,59 @@ describe provider_class do
@augeas.expects(:close)
@provider.execute_changes.should == :executed
end
+
+ it "should handle defvar commands" do
+ command = "defvar myjar Jar/Jar"
+ context = "/foo/"
+ @resource.expects(:[]).times(2).returns(command).then.returns(context)
+ @augeas.expects(:defvar).with("myjar", "/foo/Jar/Jar").returns(true)
+ @augeas.expects(:save).returns(true)
+ @augeas.expects(:close)
+ @provider.execute_changes.should == :executed
+ end
+
+ it "should pass through augeas variables without context" do
+ command = ["defvar myjar Jar/Jar","set $myjar/Binks 1"]
+ context = "/foo/"
+ @resource.expects(:[]).times(2).returns(command).then.returns(context)
+ @augeas.expects(:defvar).with("myjar", "/foo/Jar/Jar").returns(true)
+ # this is the important bit, shouldn't be /foo/$myjar/Binks
+ @augeas.expects(:set).with("$myjar/Binks", "1").returns(true)
+ @augeas.expects(:save).returns(true)
+ @augeas.expects(:close)
+ @provider.execute_changes.should == :executed
+ end
+
+ it "should handle defnode commands" do
+ command = "defnode newjar Jar/Jar[last()+1] Binks"
+ context = "/foo/"
+ @resource.expects(:[]).times(2).returns(command).then.returns(context)
+ @augeas.expects(:defnode).with("newjar", "/foo/Jar/Jar[last()+1]", "Binks").returns(true)
+ @augeas.expects(:save).returns(true)
+ @augeas.expects(:close)
+ @provider.execute_changes.should == :executed
+ end
+
+ it "should handle mv commands" do
+ command = "mv Jar/Jar Binks"
+ context = "/foo/"
+ @resource.expects(:[]).times(2).returns(command).then.returns(context)
+ @augeas.expects(:mv).with("/foo/Jar/Jar", "/foo/Binks").returns(true)
+ @augeas.expects(:save).returns(true)
+ @augeas.expects(:close)
+ @provider.execute_changes.should == :executed
+ end
+
+ it "should handle setm commands" do
+ command = ["set test[1]/Jar/Jar Foo","set test[2]/Jar/Jar Bar","setm test Jar/Jar Binks"]
+ context = "/foo/"
+ @resource.expects(:[]).times(2).returns(command).then.returns(context)
+ @augeas.expects(:set).with("/foo/test[1]/Jar/Jar", "Foo").returns(true)
+ @augeas.expects(:set).with("/foo/test[2]/Jar/Jar", "Bar").returns(true)
+ @augeas.expects(:setm).with("/foo/test", "Jar/Jar", "Binks").returns(true)
+ @augeas.expects(:save).returns(true)
+ @augeas.expects(:close)
+ @provider.execute_changes.should == :executed
+ end
end
end
diff --git a/spec/unit/provider/service/debian_spec.rb b/spec/unit/provider/service/debian_spec.rb
index 8c9522deb..b5edf6882 100755
--- a/spec/unit/provider/service/debian_spec.rb
+++ b/spec/unit/provider/service/debian_spec.rb
@@ -52,8 +52,20 @@ describe provider_class do
end
describe "when disabling" do
- it "should call update-rc.d twice" do
- @provider.expects(:update_rc).twice
+ it "should be able to disable services with newer sysv-rc versions" do
+ @provider.stubs(:`).with("dpkg --compare-versions $(dpkg-query -W --showformat '${Version}' sysv-rc) ge 2.88 ; echo $?").returns "0"
+
+ @provider.expects(:update_rc).with(@resource[:name], "disable")
+
+ @provider.disable
+ end
+
+ it "should be able to enable services with older sysv-rc versions" do
+ @provider.stubs(:`).with("dpkg --compare-versions $(dpkg-query -W --showformat '${Version}' sysv-rc) ge 2.88 ; echo $?").returns "1"
+
+ @provider.expects(:update_rc).with("-f", @resource[:name], "remove")
+ @provider.expects(:update_rc).with(@resource[:name], "stop", "00", "1", "2", "3", "4", "5", "6", ".")
+
@provider.disable
end
end
diff --git a/spec/unit/provider/service/smf_spec.rb b/spec/unit/provider/service/smf_spec.rb
new file mode 100755
index 000000000..40e96ab23
--- /dev/null
+++ b/spec/unit/provider/service/smf_spec.rb
@@ -0,0 +1,137 @@
+#!/usr/bin/env ruby
+#
+# Unit testing for the SMF service Provider
+#
+# author Dominic Cleal
+#
+require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')
+
+provider_class = Puppet::Type.type(:service).provider(:smf)
+
+describe provider_class do
+
+ before(:each) do
+ # Create a mock resource
+ @resource = Puppet::Type.type(:service).new(
+ :name => "/system/myservice", :ensure => :running, :enable => :true)
+ @provider = provider_class.new(@resource)
+
+ FileTest.stubs(:file?).with('/usr/sbin/svcadm').returns true
+ FileTest.stubs(:executable?).with('/usr/sbin/svcadm').returns true
+ FileTest.stubs(:file?).with('/usr/bin/svcs').returns true
+ FileTest.stubs(:executable?).with('/usr/bin/svcs').returns true
+ end
+
+ it "should have a restart method" do
+ @provider.should respond_to(:restart)
+ 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 checking status" do
+ it "should call the external command 'svcs /system/myservice' once" do
+ @provider.expects(:svcs).with('-H', '-o', 'state,nstate', "/system/myservice").returns("online\t-")
+ @provider.status
+ end
+ it "should return stopped if svcs can't find the service" do
+ @provider.stubs(:svcs).raises(Puppet::ExecutionFailure.new("no svc found"))
+ @provider.status.should == :stopped
+ end
+ it "should return running if online in svcs output" do
+ @provider.stubs(:svcs).returns("online\t-")
+ @provider.status.should == :running
+ end
+ it "should return stopped if disabled in svcs output" do
+ @provider.stubs(:svcs).returns("disabled\t-")
+ @provider.status.should == :stopped
+ end
+ it "should return maintenance if in maintenance in svcs output" do
+ @provider.stubs(:svcs).returns("maintenance\t-")
+ @provider.status.should == :maintenance
+ end
+ it "should return target state if transitioning in svcs output" do
+ @provider.stubs(:svcs).returns("online\tdisabled")
+ @provider.status.should == :stopped
+ end
+ it "should throw error if it's a legacy service in svcs output" do
+ @provider.stubs(:svcs).returns("legacy_run\t-")
+ lambda { @provider.status }.should raise_error(Puppet::Error, "Cannot manage legacy services through SMF")
+ end
+ end
+
+ describe "when starting" do
+ it "should enable the service if it is not enabled" do
+ @provider.expects(:status).returns :stopped
+ @provider.expects(:texecute)
+ @provider.start
+ end
+
+ it "should always execute external command 'svcadm enable /system/myservice'" do
+ @provider.stubs(:status).returns :running
+ @provider.expects(:texecute).with(:start, ["/usr/sbin/svcadm", :enable, "/system/myservice"], true)
+ @provider.start
+ end
+
+ it "should execute external command 'svcadm clear /system/myservice' if in maintenance" do
+ @provider.stubs(:status).returns :maintenance
+ @provider.expects(:texecute).with(:start, ["/usr/sbin/svcadm", :clear, "/system/myservice"], true)
+ @provider.start
+ end
+ end
+
+ describe "when starting a service with a manifest" do
+ before(:each) do
+ @resource = Puppet::Type.type(:service).new(:name => "/system/myservice", :ensure => :running, :enable => :true, :manifest => "/tmp/myservice.xml")
+ @provider = provider_class.new(@resource)
+ $CHILD_STATUS.stubs(:exitstatus).returns(1)
+ end
+
+ it "should import the manifest if service is missing" do
+ @provider.expects(:svccfg).with(:import, "/tmp/myservice.xml")
+ @provider.expects(:texecute).with(:start, ["/usr/sbin/svcadm", :enable, "/system/myservice"], true)
+ @provider.start
+ end
+
+ it "should handle failures if importing a manifest" do
+ @provider.expects(:svccfg).raises(Puppet::ExecutionFailure.new("can't svccfg import"))
+ lambda { @provider.start }.should raise_error(Puppet::Error, "Cannot config /system/myservice to enable it: can't svccfg import")
+ end
+ end
+
+ describe "when stopping" do
+ it "should execute external command 'svcadm disable /system/myservice'" do
+ @provider.expects(:texecute).with(:stop, ["/usr/sbin/svcadm", :disable, "/system/myservice"], true)
+ @provider.stop
+ end
+ end
+
+ describe "when restarting" do
+ it "should call 'svcadm restart /system/myservice'" do
+ @provider.expects(:texecute).with(:restart, ["/usr/sbin/svcadm", :restart, "/system/myservice"], true)
+ @provider.restart
+ end
+ end
+
+end
diff --git a/spec/unit/provider/zfs/solaris_spec.rb b/spec/unit/provider/zfs/solaris_spec.rb
index 4998ba6fe..84a2be9f6 100755
--- a/spec/unit/provider/zfs/solaris_spec.rb
+++ b/spec/unit/provider/zfs/solaris_spec.rb
@@ -27,6 +27,7 @@ describe provider_class do
describe "when calling add_properties" do
it "should add -o and the key=value for each properties with a value" do
@resource.stubs(:[]).with(:quota).returns ""
+ @resource.stubs(:[]).with(:refquota).returns ""
@resource.stubs(:[]).with(:mountpoint).returns "/foo"
properties = @provider.add_properties
properties.include?("-o").should == true
diff --git a/spec/unit/resource/type_spec.rb b/spec/unit/resource/type_spec.rb
index e9c203526..41b5554d9 100755
--- a/spec/unit/resource/type_spec.rb
+++ b/spec/unit/resource/type_spec.rb
@@ -55,12 +55,24 @@ describe Puppet::Resource::Type do
double_convert.arguments.should == {"one" => nil, "two" => "foo"}
end
- it "should include any extra attributes" do
- @type.file = "/my/file"
- @type.line = 50
+ it "should not include arguments if none are present" do
+ @type.to_pson["arguments"].should be_nil
+ end
+
+ [:line, :doc, :file, :parent].each do |attr|
+ it "should include #{attr} when set" do
+ @type.send(attr.to_s + "=", "value")
+ double_convert.send(attr).should == "value"
+ end
+
+ it "should not include #{attr} when not set" do
+ @type.to_pson[attr.to_s].should be_nil
+ end
+ end
- double_convert.file.should == "/my/file"
- double_convert.line.should == 50
+ it "should not include docs if they are empty" do
+ @type.doc = ""
+ @type.to_pson["doc"].should be_nil
end
end
diff --git a/spec/unit/type/exec_spec.rb b/spec/unit/type/exec_spec.rb
index a1ffb1636..86b824423 100755
--- a/spec/unit/type/exec_spec.rb
+++ b/spec/unit/type/exec_spec.rb
@@ -307,7 +307,7 @@ describe Puppet::Type.type(:exec) do
end
describe "when setting timeout" do
- [-3.5, -1, 0, 0.1, 1, 10, 4294967295].each do |valid|
+ [0, 0.1, 1, 10, 4294967295].each do |valid|
it "should accept '#{valid}' as valid" do
@exec[:timeout] = valid
@exec[:timeout].should == valid
@@ -319,7 +319,7 @@ describe Puppet::Type.type(:exec) do
end
end
- ['1/2', '1_000_000', '+12', '', 'foo'].each do |invalid|
+ ['1/2', '', 'foo', '5foo'].each do |invalid|
it "should reject '#{invalid}' as invalid" do
expect { @exec[:timeout] = invalid }.
should raise_error Puppet::Error, /The timeout must be a number/
@@ -337,6 +337,18 @@ describe Puppet::Type.type(:exec) do
sleep_exec = Puppet::Type.type(:exec).new(:name => 'sleep 1', :path => ['/bin'], :timeout => '0.2')
lambda { sleep_exec.refresh }.should raise_error Puppet::Error, "Command exceeded timeout"
end
+
+ it "should convert timeout to a float" do
+ resource = Puppet::Type.type(:exec).new :command => "/bin/false", :timeout => "12"
+ resource[:timeout].should be_a(Float)
+ resource[:timeout].should == 12.0
+ end
+
+ it "should munge negative timeouts to 0.0" do
+ resource = Puppet::Type.type(:exec).new :command => "/bin/false", :timeout => "-12.0"
+ resource.parameter(:timeout).value.should be_a(Float)
+ resource.parameter(:timeout).value.should == 0.0
+ end
end
describe "when setting tries" do
diff --git a/spec/unit/util/loadedfile_spec.rb b/spec/unit/util/loadedfile_spec.rb
index 23d2766d7..92daeb953 100755
--- a/spec/unit/util/loadedfile_spec.rb
+++ b/spec/unit/util/loadedfile_spec.rb
@@ -6,6 +6,7 @@ require 'tempfile'
require 'puppet/util/loadedfile'
describe Puppet::Util::LoadedFile do
+ include PuppetSpec::Files
before(:each) do
@f = Tempfile.new('loadedfile_test')
@f.puts "yayness"
@@ -18,6 +19,12 @@ describe Puppet::Util::LoadedFile do
@fake_now = Time.now + (2 * Puppet[:filetimeout])
end
+ it "should accept files that don't exist" do
+ nofile = tmpfile('testfile')
+ File.exists?(nofile).should == false
+ lambda{ Puppet::Util::LoadedFile.new(nofile) }.should_not raise_error
+ end
+
it "should recognize when the file has not changed" do
# Use fake "now" so that we can be sure changed? actually checks, without sleeping
# for Puppet[:filetimeout] seconds.
diff --git a/spec/unit/util/rdoc/parser_spec.rb b/spec/unit/util/rdoc/parser_spec.rb
index ab54c8cd3..f118dc99b 100755
--- a/spec/unit/util/rdoc/parser_spec.rb
+++ b/spec/unit/util/rdoc/parser_spec.rb
@@ -20,8 +20,9 @@ describe RDoc::Parser do
@parser.stubs(:scan_top_level)
parser = stub 'parser'
Puppet::Parser::Parser.stubs(:new).returns(parser)
- parser.expects(:parse).returns(Puppet::Parser::AST::Hostclass.new(''))
+ parser.expects(:parse).returns(Puppet::Parser::AST::Hostclass.new('')).at_least_once
parser.expects(:file=).with("module/manifests/init.pp")
+ parser.expects(:file=).with("/dev/null/manifests/site.pp")
@parser.scan
end
@@ -29,7 +30,7 @@ describe RDoc::Parser do
it "should scan the ast for Puppet files" do
parser = stub_everything 'parser'
Puppet::Parser::Parser.stubs(:new).returns(parser)
- parser.expects(:parse).returns(Puppet::Parser::AST::Hostclass.new(''))
+ parser.expects(:parse).returns(Puppet::Parser::AST::Hostclass.new('')).at_least_once
@parser.expects(:scan_top_level)
@@ -39,7 +40,7 @@ describe RDoc::Parser do
it "should return a PuppetTopLevel to RDoc" do
parser = stub_everything 'parser'
Puppet::Parser::Parser.stubs(:new).returns(parser)
- parser.expects(:parse).returns(Puppet::Parser::AST::Hostclass.new(''))
+ parser.expects(:parse).returns(Puppet::Parser::AST::Hostclass.new('')).at_least_once
@parser.expects(:scan_top_level)