summaryrefslogtreecommitdiffstats
path: root/spec/unit/provider
diff options
context:
space:
mode:
Diffstat (limited to 'spec/unit/provider')
-rwxr-xr-xspec/unit/provider/package/apt.rb138
-rwxr-xr-xspec/unit/provider/package/dpkg.rb163
-rwxr-xr-xspec/unit/provider/selboolean.rb37
-rw-r--r--spec/unit/provider/selmodule-example.ppbin0 -> 256 bytes
-rwxr-xr-xspec/unit/provider/selmodule.rb66
-rw-r--r--spec/unit/provider/user/user_role_add.rb131
6 files changed, 535 insertions, 0 deletions
diff --git a/spec/unit/provider/package/apt.rb b/spec/unit/provider/package/apt.rb
new file mode 100755
index 000000000..0ec1cd9ed
--- /dev/null
+++ b/spec/unit/provider/package/apt.rb
@@ -0,0 +1,138 @@
+#!/usr/bin/env ruby
+
+Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") }
+
+provider = Puppet::Type.type(:package).provider(:apt)
+
+describe provider do
+ before do
+ @resource = stub 'resource', :[] => "asdf"
+ @provider = provider.new(@resource)
+
+ @fakeresult = "install ok installed asdf 1.0\n"
+ end
+
+ it "should be versionable" do
+ provider.should be_versionable
+ end
+
+ it "should use :install to update" do
+ @provider.expects(:install)
+ @provider.update
+ end
+
+ it "should use 'apt-get remove' to uninstall" do
+ @provider.expects(:aptget).with("-y", "-q", :remove, "asdf")
+
+ @provider.uninstall
+ end
+
+ it "should use 'apt-get purge' and 'dpkg purge' to purge" do
+ @provider.expects(:aptget).with("-y", "-q", :remove, "--purge", "asdf")
+ @provider.expects(:dpkg).with("--purge", "asdf")
+
+ @provider.purge
+ end
+
+ it "should use 'apt-cache policy' to determine the latest version of a package" do
+ @provider.expects(:aptcache).with(:policy, "asdf").returns "asdf:
+Installed: 1:1.0
+Candidate: 1:1.1
+Version table:
+ 1:1.0
+ 650 http://ftp.osuosl.org testing/main Packages
+*** 1:1.1
+ 100 /var/lib/dpkg/status"
+
+ @provider.latest.should == "1:1.1"
+ end
+
+ it "should print and error and return nil if no policy is found" do
+ @provider.expects(:aptcache).with(:policy, "asdf").returns "asdf:"
+
+ @provider.expects(:err)
+ @provider.latest.should be_nil
+ end
+
+ it "should be able to preseed" do
+ @provider.should respond_to(:run_preseed)
+ end
+
+ it "should preseed with the provided responsefile when preseeding is called for" do
+ @resource.expects(:[]).with(:responsefile).returns "/my/file"
+ FileTest.expects(:exist?).with("/my/file").returns true
+
+ @provider.expects(:info)
+ @provider.expects(:preseed).with("/my/file")
+
+ @provider.run_preseed
+ end
+
+ it "should not preseed if no responsefile is provided" do
+ @resource.expects(:[]).with(:responsefile).returns nil
+
+ @provider.expects(:info)
+ @provider.expects(:preseed).never
+
+ @provider.run_preseed
+ end
+
+ it "should fail if a cdrom is listed in the sources list and :allowcdrom is not specified"
+
+ describe "when installing" do
+ it "should preseed if a responsefile is provided" do
+ @resource.expects(:[]).with(:responsefile).returns "/my/file"
+ @provider.expects(:run_preseed)
+
+ @provider.stubs(:aptget)
+ @provider.install
+ end
+
+ it "should check for a cdrom" do
+ @provider.expects(:checkforcdrom)
+
+ @provider.stubs(:aptget)
+ @provider.install
+ end
+
+ it "should use 'apt-get install' with the package name if no version is asked for" do
+ @resource.expects(:[]).with(:ensure).returns :installed
+ @provider.expects(:aptget).with { |*command| command[-1] == "asdf" and command[-2] == :install }
+
+ @provider.install
+ end
+
+ it "should specify the package version if one is asked for" do
+ @resource.expects(:[]).with(:ensure).returns "1.0"
+ @provider.expects(:aptget).with { |*command| command[-1] == "asdf=1.0" }
+
+ @provider.install
+ end
+
+ it "should do a quiet install" do
+ @provider.expects(:aptget).with { |*command| command.include?("-q") }
+
+ @provider.install
+ end
+
+ it "should default to 'yes' for all questions" do
+ @provider.expects(:aptget).with { |*command| command.include?("-y") }
+
+ @provider.install
+ end
+
+ it "should keep config files if asked" do
+ @resource.expects(:[]).with(:configfiles).returns :keep
+ @provider.expects(:aptget).with { |*command| command.include?("DPkg::Options::=--force-confold") }
+
+ @provider.install
+ end
+
+ it "should replace config files if asked" do
+ @resource.expects(:[]).with(:configfiles).returns :replace
+ @provider.expects(:aptget).with { |*command| command.include?("DPkg::Options::=--force-confnew") }
+
+ @provider.install
+ end
+ end
+end
diff --git a/spec/unit/provider/package/dpkg.rb b/spec/unit/provider/package/dpkg.rb
new file mode 100755
index 000000000..08aaca875
--- /dev/null
+++ b/spec/unit/provider/package/dpkg.rb
@@ -0,0 +1,163 @@
+#!/usr/bin/env ruby
+
+Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") }
+
+provider = Puppet::Type.type(:package).provider(:dpkg)
+
+describe provider do
+ before do
+ @resource = stub 'resource', :[] => "asdf"
+ @provider = provider.new(@resource)
+
+ @fakeresult = "install ok installed asdf 1.0\n"
+ end
+
+ it "should have documentation" do
+ provider.doc.should be_instance_of(String)
+ end
+
+ describe "when listing all instances" do
+ before do
+ provider.stubs(:command).with(:dpkgquery).returns "myquery"
+ end
+
+ it "should use dpkg-query" do
+ provider.expects(:command).with(:dpkgquery).returns "myquery"
+ provider.expects(:execpipe).with("myquery -W --showformat '${Status} ${Package} ${Version}\\n'").returns @fakeresult
+
+ provider.instances
+ end
+
+ it "should create and return an instance with each parsed line from dpkg-query" do
+ pipe = mock 'pipe'
+ pipe.expects(:each).yields @fakeresult
+ provider.expects(:execpipe).yields pipe
+
+ asdf = mock 'pkg1'
+ provider.expects(:new).with(:ensure => "1.0", :error => "ok", :desired => "install", :name => "asdf", :status => "installed", :provider => :dpkg).returns asdf
+
+ provider.instances.should == [asdf]
+ end
+
+ it "should warn on and ignore any lines it does not understand" do
+ pipe = mock 'pipe'
+ pipe.expects(:each).yields "foobar"
+ provider.expects(:execpipe).yields pipe
+
+ Puppet.expects(:warning)
+ provider.expects(:new).never
+
+ provider.instances.should == []
+ end
+ end
+
+ describe "when querying the current state" do
+ it "should use dpkg-query" do
+ @provider.expects(:dpkgquery).with("-W", "--showformat",'${Status} ${Package} ${Version}\\n', "asdf").returns @fakeresult
+
+ @provider.query
+ end
+
+ it "should consider the package purged if dpkg-query fails" do
+ @provider.expects(:dpkgquery).raises Puppet::ExecutionFailure.new("eh")
+
+ @provider.query[:ensure].should == :purged
+ end
+
+ it "should return a hash of the found status with the desired state, error state, status, name, and 'ensure'" do
+ @provider.expects(:dpkgquery).returns @fakeresult
+
+ @provider.query.should == {:ensure => "1.0", :error => "ok", :desired => "install", :name => "asdf", :status => "installed", :provider => :dpkg}
+ end
+
+ it "should consider the package absent if the dpkg-query result cannot be interpreted" do
+ @provider.expects(:dpkgquery).returns "somebaddata"
+
+ @provider.query[:ensure].should == :absent
+ end
+
+ it "should fail if an error is discovered" do
+ @provider.expects(:dpkgquery).returns @fakeresult.sub("ok", "error")
+
+ lambda { @provider.query }.should raise_error(Puppet::Error)
+ end
+
+ it "should consider the package purged if it is marked 'not-installed'" do
+ @provider.expects(:dpkgquery).returns @fakeresult.sub("installed", "not-installed")
+
+ @provider.query[:ensure].should == :purged
+ end
+
+ it "should consider the package absent if its status is neither 'installed' nor 'not-installed'" do
+ @provider.expects(:dpkgquery).returns @fakeresult.sub("installed", "foo")
+
+ @provider.query[:ensure].should == :absent
+ end
+ end
+
+ it "should be able to install" do
+ @provider.should respond_to(:install)
+ end
+
+ describe "when installing" do
+ before do
+ @resource.stubs(:[]).with(:source).returns "mypkg"
+ end
+
+ it "should fail to install if no source is specified in the resource" do
+ @resource.expects(:[]).with(:source).returns nil
+
+ lambda { @provider.install }.should raise_error(ArgumentError)
+ end
+
+ it "should use 'dpkg -i' to install the package" do
+ @resource.expects(:[]).with(:source).returns "mypackagefile"
+ @provider.expects(:dpkg).with { |*command| command[-1] == "mypackagefile" and command[-2] == "-i" }
+
+ @provider.install
+ end
+
+ it "should keep old config files if told to do so" do
+ @resource.expects(:[]).with(:configfiles).returns :keep
+ @provider.expects(:dpkg).with { |*command| command[0] == "--force-confold" }
+
+ @provider.install
+ end
+
+ it "should replace old config files if told to do so" do
+ @resource.expects(:[]).with(:configfiles).returns :replace
+ @provider.expects(:dpkg).with { |*command| command[0] == "--force-confnew" }
+
+ @provider.install
+ end
+ end
+
+ it "should use :install to update" do
+ @provider.expects(:install)
+ @provider.update
+ end
+
+ describe "when determining latest available version" do
+ it "should return the version found by dpkg-deb" do
+ @resource.expects(:[]).with(:source).returns "myfile"
+ @provider.expects(:dpkg_deb).with { |*command| command[-1] == "myfile" }.returns "asdf\t1.0"
+ @provider.latest.should == "1.0"
+ end
+
+ it "should warn if the package file contains a different package" do
+ @provider.expects(:dpkg_deb).returns("foo\tversion")
+ @provider.expects(:warning)
+ @provider.latest
+ end
+ end
+
+ it "should use 'dpkg -r' to uninstall" do
+ @provider.expects(:dpkg).with("-r", "asdf")
+ @provider.uninstall
+ end
+
+ it "should use 'dpkg --purge' to purge" do
+ @provider.expects(:dpkg).with("--purge", "asdf")
+ @provider.purge
+ end
+end
diff --git a/spec/unit/provider/selboolean.rb b/spec/unit/provider/selboolean.rb
new file mode 100755
index 000000000..4006df151
--- /dev/null
+++ b/spec/unit/provider/selboolean.rb
@@ -0,0 +1,37 @@
+#!/usr/bin/env ruby
+
+Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") }
+
+provider_class = Puppet::Type.type(:selboolean).provider(:getsetsebool)
+
+describe provider_class do
+ before :each do
+ @resource = stub("resource", :name => "foo")
+ @resource.stubs(:[]).returns "foo"
+ @provider = provider_class.new(@resource)
+ end
+
+ it "should return :on when getsebool returns on" do
+ @provider.expects(:getsebool).with("foo").returns "foo --> on\n"
+ @provider.value.should == :on
+ end
+
+ it "should return :off when getsebool returns on" do
+ @provider.expects(:getsebool).with("foo").returns "foo --> off\n"
+ @provider.value.should == :off
+ end
+
+ it "should call execpipe when updating boolean setting" do
+ @provider.expects(:command).with(:setsebool).returns "/usr/sbin/setsebool"
+ @provider.expects(:execpipe).with("/usr/sbin/setsebool foo off")
+ @provider.value = :off
+ end
+
+ it "should call execpipe with -P when updating persistent boolean setting" do
+ @resource.stubs(:[]).with(:persistent).returns :true
+ @provider.expects(:command).with(:setsebool).returns "/usr/sbin/setsebool"
+ @provider.expects(:execpipe).with("/usr/sbin/setsebool -P foo off")
+ @provider.value = :off
+ end
+
+end
diff --git a/spec/unit/provider/selmodule-example.pp b/spec/unit/provider/selmodule-example.pp
new file mode 100644
index 000000000..28166ca40
--- /dev/null
+++ b/spec/unit/provider/selmodule-example.pp
Binary files differ
diff --git a/spec/unit/provider/selmodule.rb b/spec/unit/provider/selmodule.rb
new file mode 100755
index 000000000..e92441d23
--- /dev/null
+++ b/spec/unit/provider/selmodule.rb
@@ -0,0 +1,66 @@
+#!/usr/bin/env ruby
+
+# Note: This unit test depends on having a sample SELinux policy file
+# in the same directory as this test called selmodule-example.pp
+# with version 1.5.0. The provided selmodule-example.pp is the first
+# 256 bytes taken from /usr/share/selinux/targeted/nagios.pp on Fedora 9
+
+Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") }
+
+provider_class = Puppet::Type.type(:selmodule).provider(:semodule)
+
+describe provider_class do
+ before :each do
+ @resource = stub("resource", :name => "foo")
+ @resource.stubs(:[]).returns "foo"
+ @provider = provider_class.new(@resource)
+ end
+
+ describe "exists? method" do
+ it "should find a module if it is already loaded" do
+ @provider.expects(:command).with(:semodule).returns "/usr/sbin/semodule"
+ @provider.expects(:execpipe).with("/usr/sbin/semodule --list").yields ["bar\t1.2.3\n", "foo\t4.4.4\n", "bang\t1.0.0\n"]
+ @provider.exists?.should == :true
+ end
+
+ it "should return nil if not loaded" do
+ @provider.expects(:command).with(:semodule).returns "/usr/sbin/semodule"
+ @provider.expects(:execpipe).with("/usr/sbin/semodule --list").yields ["bar\t1.2.3\n", "bang\t1.0.0\n"]
+ @provider.exists?.should be_nil
+ end
+
+ it "should return nil if no modules are loaded" do
+ @provider.expects(:command).with(:semodule).returns "/usr/sbin/semodule"
+ @provider.expects(:execpipe).with("/usr/sbin/semodule --list").yields []
+ @provider.exists?.should be_nil
+ end
+ end
+
+ describe "selmodversion_file" do
+ it "should return 1.5.0 for the example policy file" do
+ @provider.expects(:selmod_name_to_filename).returns "#{File.dirname(__FILE__)}/selmodule-example.pp"
+ @provider.selmodversion_file.should == "1.5.0"
+ end
+ end
+
+ describe "syncversion" do
+ it "should return :true if loaded and file modules are in sync" do
+ @provider.expects(:selmodversion_loaded).returns "1.5.0"
+ @provider.expects(:selmodversion_file).returns "1.5.0"
+ @provider.syncversion.should == :true
+ end
+
+ it "should return :false if loaded and file modules are not in sync" do
+ @provider.expects(:selmodversion_loaded).returns "1.4.0"
+ @provider.expects(:selmodversion_file).returns "1.5.0"
+ @provider.syncversion.should == :false
+ end
+
+ it "should return before checking file version if no loaded policy" do
+ @provider.expects(:selmodversion_loaded).returns nil
+ @provider.syncversion.should == :false
+ end
+
+ end
+
+end
diff --git a/spec/unit/provider/user/user_role_add.rb b/spec/unit/provider/user/user_role_add.rb
new file mode 100644
index 000000000..e9bd9a68f
--- /dev/null
+++ b/spec/unit/provider/user/user_role_add.rb
@@ -0,0 +1,131 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../../spec_helper'
+
+provider_class = Puppet::Type.type(:user).provider(:user_role_add)
+
+describe provider_class do
+ before do
+ @resource = stub("resource", :name => "myuser", :managehome? => nil)
+ @resource.stubs(:should).returns "fakeval"
+ @resource.stubs(:[]).returns "fakeval"
+ @resource.stubs(:allowdupe?).returns false
+ @provider = provider_class.new(@resource)
+ end
+
+ describe "when calling command" do
+ before do
+ klass = stub("provider")
+ klass.stubs(:command).with(:foo).returns("userfoo")
+ klass.stubs(:command).with(:role_foo).returns("rolefoo")
+ @provider.stubs(:class).returns(klass)
+ end
+
+ it "should use the command if not a role and ensure!=role" do
+ @provider.stubs(:is_role?).returns(false)
+ @provider.stubs(:exists?).returns(false)
+ @resource.stubs(:[]).with(:ensure).returns(:present)
+ @provider.command(:foo).should == "userfoo"
+ end
+
+ it "should use the role command when a role" do
+ @provider.stubs(:is_role?).returns(true)
+ @provider.command(:foo).should == "rolefoo"
+ end
+
+ it "should use the role command when !exists and ensure=role" do
+ @provider.stubs(:is_role?).returns(false)
+ @provider.stubs(:exists?).returns(false)
+ @resource.stubs(:[]).with(:ensure).returns(:role)
+ @provider.command(:foo).should == "rolefoo"
+ end
+ end
+
+ describe "when calling transition" do
+ it "should return foomod setting the type to bar" do
+ @provider.expects(:command).with(:modify).returns("foomod")
+ @provider.transition("bar").should == ["foomod", "-K", "type=bar", "fakeval"]
+ end
+ end
+
+ describe "when calling create" do
+ it "should use the add command when the user is not a role" do
+ @provider.stubs(:is_role?).returns(false)
+ @provider.expects(:addcmd).returns("useradd")
+ @provider.expects(:run)
+ @provider.create
+ end
+
+ it "should use transition(normal) when the user is a role" do
+ @provider.stubs(:is_role?).returns(true)
+ @provider.expects(:transition).with("normal")
+ @provider.expects(:run)
+ @provider.create
+ end
+ end
+
+ describe "when calling destroy" do
+ it "should use the delete command if the user exists and is not a role" do
+ @provider.stubs(:exists?).returns(true)
+ @provider.stubs(:is_role?).returns(false)
+ @provider.expects(:deletecmd)
+ @provider.expects(:run)
+ @provider.destroy
+ end
+
+ it "should use the delete command if the user is a role" do
+ @provider.stubs(:exists?).returns(true)
+ @provider.stubs(:is_role?).returns(true)
+ @provider.expects(:deletecmd)
+ @provider.expects(:run)
+ @provider.destroy
+ end
+ end
+
+ describe "when calling create_role" do
+ it "should use the transition(role) if the user exists" do
+ @provider.stubs(:exists?).returns(true)
+ @provider.stubs(:is_role?).returns(false)
+ @provider.expects(:transition).with("role")
+ @provider.expects(:run)
+ @provider.create_role
+ end
+
+ it "should use the add command when role doesn't exists" do
+ @provider.stubs(:exists?).returns(false)
+ @provider.expects(:addcmd)
+ @provider.expects(:run)
+ @provider.create_role
+ end
+ end
+
+ describe "when allow duplicate is enabled" do
+ before do
+ @resource.expects(:allowdupe?).returns true
+ @provider.stubs(:is_role?).returns(false)
+ @provider.expects(:execute).with { |args| args.include?("-o") }
+ end
+
+ it "should add -o when the user is being created" do
+ @provider.create
+ end
+
+ it "should add -o when the uid is being modified" do
+ @provider.uid = 150
+ end
+ end
+
+ describe "when getting roles" do
+ it "should get the user_attributes" do
+ @provider.expects(:user_attributes)
+ @provider.roles
+ end
+
+ it "should get the :roles attribute" do
+ attributes = mock("attributes")
+ attributes.expects(:[]).with(:roles)
+ @provider.stubs(:user_attributes).returns(attributes)
+ @provider.roles
+ end
+ end
+end