diff options
Diffstat (limited to 'spec/unit')
| -rwxr-xr-x | spec/unit/parser/templatewrapper.rb | 9 | ||||
| -rw-r--r-- | spec/unit/property/keyvalue.rb | 168 | ||||
| -rw-r--r-- | spec/unit/provider/user/user_role_add.rb | 78 | ||||
| -rwxr-xr-x | spec/unit/provider/user/useradd.rb | 94 | ||||
| -rw-r--r-- | spec/unit/type/file/selinux.rb | 1 | ||||
| -rwxr-xr-x | spec/unit/type/user.rb | 20 | ||||
| -rw-r--r-- | spec/unit/util/selinux.rb | 2 |
7 files changed, 356 insertions, 16 deletions
diff --git a/spec/unit/parser/templatewrapper.rb b/spec/unit/parser/templatewrapper.rb index 6dbfbe266..532776223 100755 --- a/spec/unit/parser/templatewrapper.rb +++ b/spec/unit/parser/templatewrapper.rb @@ -86,6 +86,15 @@ describe Puppet::Parser::TemplateWrapper do @tw.instance_variable_get("@one").should == "foo" end + it "should not error out if one of the variables is a symbol" do + template_mock = mock("template", :result => "woot!") + File.expects(:read).with("/tmp/fake_template").returns("template contents") + ERB.expects(:new).with("template contents", 0, "-").returns(template_mock) + + @scope.expects(:to_hash).returns(:_timestamp => "1234") + @tw.result + end + %w{! . ; :}.each do |badchar| it "should translate #{badchar} to _ when setting the instance variables" do template_mock = mock("template", :result => "woot!") diff --git a/spec/unit/property/keyvalue.rb b/spec/unit/property/keyvalue.rb new file mode 100644 index 000000000..6f9eef531 --- /dev/null +++ b/spec/unit/property/keyvalue.rb @@ -0,0 +1,168 @@ +#!/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") } + +require 'puppet/property/keyvalue' + +klass = Puppet::Property::KeyValue + +describe klass do + + it "should be a subclass of Property" do + klass.superclass.must == Puppet::Property + end + + describe "as an instance" do + before do + # Wow that's a messy interface to the resource. + klass.initvars + @resource = stub 'resource', :[]= => nil, :property => nil + @property = klass.new(:resource => @resource) + end + + it "should have a , as default delimiter" do + @property.delimiter.should == ";" + end + + it "should have a = as default separator" do + @property.separator.should == "=" + end + + it "should have a :membership as default membership" do + @property.membership.should == :key_value_membership + end + + it "should return the same value passed into should_to_s" do + @property.should_to_s({:foo => "baz", :bar => "boo"}) == "foo=baz;bar=boo" + end + + it "should return the passed in array values joined with the delimiter from is_to_s" do + @property.is_to_s({"foo" => "baz" , "bar" => "boo"}).should == "foo=baz;bar=boo" + end + + describe "when calling inclusive?" do + it "should use the membership method to look up on the @resource" do + @property.expects(:membership).returns(:key_value_membership) + @resource.expects(:[]).with(:key_value_membership) + @property.inclusive? + end + + it "should return true when @resource[membership] == inclusive" do + @property.stubs(:membership).returns(:key_value_membership) + @resource.stubs(:[]).with(:key_value_membership).returns(:inclusive) + @property.inclusive?.must == true + end + + it "should return false when @resource[membership] != inclusive" do + @property.stubs(:membership).returns(:key_value_membership) + @resource.stubs(:[]).with(:key_value_membership).returns(:minimum) + @property.inclusive?.must == false + end + end + + describe "when calling process_current_hash" do + it "should return {} if hash is :absent" do + @property.process_current_hash(:absent).must == {} + end + + it "should set every key to nil if inclusive?" do + @property.stubs(:inclusive?).returns(true) + @property.process_current_hash({:foo => "bar", :do => "re"}).must == { :foo => nil, :do => nil } + end + + it "should return the hash if !inclusive?" do + @property.stubs(:inclusive?).returns(false) + @property.process_current_hash({:foo => "bar", :do => "re"}).must == {:foo => "bar", :do => "re"} + end + end + + describe "when calling should" do + it "should return nil if @should is nil" do + @property.should.must == nil + end + + it "should call process_current_hash" do + @property.should = ["foo=baz", "bar=boo"] + @property.stubs(:retrieve).returns({:do => "re", :mi => "fa" }) + @property.expects(:process_current_hash).returns({}) + @property.should + end + + it "should return the hashed values of @should and the nilled values of retrieve if inclusive" do + @property.should = ["foo=baz", "bar=boo"] + @property.expects(:retrieve).returns({:do => "re", :mi => "fa" }) + @property.expects(:inclusive?).returns(true) + @property.should.must == { :foo => "baz", :bar => "boo", :do => nil, :mi => nil } + end + + it "should return the hashed @should + the unique values of retrieve if !inclusive" do + @property.should = ["foo=baz", "bar=boo"] + @property.expects(:retrieve).returns({:foo => "diff", :do => "re", :mi => "fa"}) + @property.expects(:inclusive?).returns(false) + @property.should.must == { :foo => "baz", :bar => "boo", :do => "re", :mi => "fa" } + end + end + + describe "when calling retrieve" do + before do + @provider = mock("provider") + @property.stubs(:provider).returns(@provider) + end + + it "should send 'name' to the provider" do + @provider.expects(:send).with(:keys) + @property.expects(:name).returns(:keys) + @property.retrieve + end + + it "should return a hash with the provider returned info" do + @provider.stubs(:send).with(:keys).returns({"do" => "re", "mi" => "fa" }) + @property.stubs(:name).returns(:keys) + @property.retrieve == {"do" => "re", "mi" => "fa" } + end + + it "should return :absent when the provider returns :absent" do + @provider.stubs(:send).with(:keys).returns(:absent) + @property.stubs(:name).returns(:keys) + @property.retrieve == :absent + end + end + + describe "when calling hashify" do + it "should return the array hashified" do + @property.hashify(["foo=baz", "bar=boo"]).must == { :foo => "baz", :bar => "boo" } + end + end + + describe "when calling insync?" do + before do + @provider = mock("provider") + @property.stubs(:provider).returns(@provider) + @property.stubs(:name).returns(:prop_name) + end + + it "should return true unless @should is defined and not nil" do + @property.insync?("foo") == true + end + + it "should return true if the passed in values is nil" do + @property.should = "foo" + @property.insync?(nil) == true + end + + it "should return true if hashified should value == (retrieved) value passed in" do + @provider.stubs(:prop_name).returns({ :foo => "baz", :bar => "boo" }) + @property.should = ["foo=baz", "bar=boo"] + @property.expects(:inclusive?).returns(true) + @property.insync?({ :foo => "baz", :bar => "boo" }).must == true + end + + it "should return false if prepared value != should value" do + @provider.stubs(:prop_name).returns({ "foo" => "bee", "bar" => "boo" }) + @property.should = ["foo=baz", "bar=boo"] + @property.expects(:inclusive?).returns(true) + @property.insync?({ "foo" => "bee", "bar" => "boo" }).must == false + end + end + end +end diff --git a/spec/unit/provider/user/user_role_add.rb b/spec/unit/provider/user/user_role_add.rb index e9bd9a68f..fc2074d44 100644 --- a/spec/unit/provider/user/user_role_add.rb +++ b/spec/unit/provider/user/user_role_add.rb @@ -42,9 +42,9 @@ describe provider_class do end describe "when calling transition" do - it "should return foomod setting the type to bar" do + it "should return the type set to whatever is passed in" do @provider.expects(:command).with(:modify).returns("foomod") - @provider.transition("bar").should == ["foomod", "-K", "type=bar", "fakeval"] + @provider.transition("bar").include?("type=bar") end end @@ -115,17 +115,77 @@ describe provider_class do end end - describe "when getting roles" do + [:roles, :auths, :profiles].each do |val| + describe "when getting #{val}" do + it "should get the user_attributes" do + @provider.expects(:user_attributes) + @provider.send(val) + end + + it "should get the #{val} attribute" do + attributes = mock("attributes") + attributes.expects(:[]).with(val) + @provider.stubs(:user_attributes).returns(attributes) + @provider.send(val) + end + end + end + + describe "when getting the keys" do it "should get the user_attributes" do @provider.expects(:user_attributes) - @provider.roles + @provider.keys + end + + it "should call removed_managed_attributes" do + @provider.stubs(:user_attributes).returns({ :type => "normal", :foo => "something" }) + @provider.expects(:remove_managed_attributes) + @provider.keys + end + + it "should removed managed attribute (type, auths, roles, etc)" do + @provider.stubs(:user_attributes).returns({ :type => "normal", :foo => "something" }) + @provider.keys.should == { :foo => "something" } + end + end + + describe "when adding properties" do + it "should call build_keys_cmd" do + @resource.stubs(:should).returns "" + @resource.expects(:should).with(:keys).returns({ :foo => "bar" }) + @provider.expects(:build_keys_cmd).returns([]) + @provider.add_properties + end + + it "should add the elements of the keys hash to an array" do + @resource.stubs(:should).returns "" + @resource.expects(:should).with(:keys).returns({ :foo => "bar"}) + @provider.add_properties.must == ["-K", "foo=bar"] + end + end + + describe "when calling build_keys_cmd" do + it "should build cmd array with keypairs seperated by -K ending with user" do + @provider.build_keys_cmd({"foo" => "bar", "baz" => "boo"}).should.eql? ["-K", "foo=bar", "-K", "baz=boo"] + end + end + + describe "when setting the keys" do + before do + @provider.stubs(:is_role?).returns(false) + end + + it "should run a command" do + @provider.expects(:run) + @provider.keys=({}) end - it "should get the :roles attribute" do - attributes = mock("attributes") - attributes.expects(:[]).with(:roles) - @provider.stubs(:user_attributes).returns(attributes) - @provider.roles + it "should build the command" do + @resource.stubs(:[]).with(:name).returns("someuser") + @provider.stubs(:command).returns("usermod") + @provider.expects(:build_keys_cmd).returns(["-K", "foo=bar"]) + @provider.expects(:run).with(["usermod", "-K", "foo=bar", "someuser"], "modify attribute key pairs") + @provider.keys=({}) end end end diff --git a/spec/unit/provider/user/useradd.rb b/spec/unit/provider/user/useradd.rb index 96a785589..463212e4e 100755 --- a/spec/unit/provider/user/useradd.rb +++ b/spec/unit/provider/user/useradd.rb @@ -7,25 +7,109 @@ provider_class = Puppet::Type.type(:user).provider(:useradd) describe provider_class do before do @resource = stub("resource", :name => "myuser", :managehome? => nil) + @resource.stubs(:should).returns "fakeval" + @resource.stubs(:[]).returns "fakeval" @provider = provider_class.new(@resource) end # #1360 it "should add -o when allowdupe is enabled and the user is being created" do - @resource.stubs(:should).returns "fakeval" - @resource.stubs(:[]).returns "fakeval" @resource.expects(:allowdupe?).returns true @provider.expects(:execute).with { |args| args.include?("-o") } - @provider.create end it "should add -o when allowdupe is enabled and the uid is being modified" do - @resource.stubs(:should).returns "fakeval" - @resource.stubs(:[]).returns "fakeval" @resource.expects(:allowdupe?).returns true @provider.expects(:execute).with { |args| args.include?("-o") } @provider.uid = 150 end + + describe "when checking to add allow dup" do + it "should check allow dup" do + @resource.expects(:allowdupe?) + @provider.check_allow_dup + end + + it "should return an array with a flag if dup is allowed" do + @resource.stubs(:allowdupe?).returns true + @provider.check_allow_dup.must == ["-o"] + end + + it "should return an empty array if no dup is allowed" do + @resource.stubs(:allowdupe?).returns false + @provider.check_allow_dup.must == [] + end + end + + describe "when checking manage home" do + it "should check manage home" do + @resource.expects(:managehome?) + @provider.check_manage_home + end + + it "should return an array with -m flag if home is managed" do + @resource.stubs(:managehome?).returns true + @provider.check_manage_home.must == ["-m"] + end + + it "should return an array with -M if home is not managed and on Redhat" do + Facter.stubs(:value).with("operatingsystem").returns("RedHat") + @resource.stubs(:managehome?).returns false + @provider.check_manage_home.must == ["-M"] + end + + it "should return an empty array if home is not managed and not on Redhat" do + Facter.stubs(:value).with("operatingsystem").returns("some OS") + @resource.stubs(:managehome?).returns false + @provider.check_manage_home.must == [] + end + end + + describe "when adding properties" do + it "should get the valid properties" + it "should not add the ensure property" + it "should add the flag and value to an array" + it "should return and array of flags and values" + end + + describe "when calling addcmd" do + before do + @resource.stubs(:allowdupe?).returns true + @resource.stubs(:managehome?).returns true + end + + it "should call command with :add" do + @provider.expects(:command).with(:add) + @provider.addcmd + end + + it "should add properties" do + @provider.expects(:add_properties).returns([]) + @provider.addcmd + end + + it "should check and add if dup allowed" do + @provider.expects(:check_allow_dup).returns([]) + @provider.addcmd + end + + it "should check and add if home is managed" do + @provider.expects(:check_manage_home).returns([]) + @provider.addcmd + end + + it "should add the resource :name" do + @resource.expects(:[]).with(:name) + @provider.addcmd + end + + it "should return an array with full command" do + @provider.stubs(:command).with(:add).returns("useradd") + @provider.stubs(:add_properties).returns(["-G", "somegroup"]) + @resource.stubs(:[]).with(:name).returns("someuser") + @provider.addcmd.must == ["useradd", "-G", "somegroup", "-o", "-m", "someuser"] + end + end end diff --git a/spec/unit/type/file/selinux.rb b/spec/unit/type/file/selinux.rb index 5e2c3539c..6da88db3d 100644 --- a/spec/unit/type/file/selinux.rb +++ b/spec/unit/type/file/selinux.rb @@ -56,6 +56,7 @@ property = Puppet::Type.type(:file).attrclass(param) end it "should be able to detect matchpathcon defaults" do + @sel.stubs(:debug) @sel.expects(:get_selinux_default_context).with("/my/file").returns "user_u:role_r:type_t:s0" expectedresult = case param when :seluser then "user_u" diff --git a/spec/unit/type/user.rb b/spec/unit/type/user.rb index 51c6dd801..41b10da81 100755 --- a/spec/unit/type/user.rb +++ b/spec/unit/type/user.rb @@ -32,13 +32,19 @@ describe user do user.provider_feature(:manages_passwords).should_not be_nil end + it "should have a manages_solaris_rbac feature" do + user.provider_feature(:manages_solaris_rbac).should_not be_nil + end + describe "instances" do it "should have a valid provider" do user.create(:name => "foo").provider.class.ancestors.should be_include(Puppet::Provider) end end - [:ensure, :uid, :gid, :home, :comment, :shell, :password, :groups].each do |property| + properties = [:ensure, :uid, :gid, :home, :comment, :shell, :password, :groups, :roles, :auths, :profiles, :project, :keys] + + properties.each do |property| it "should have a %s property" % property do user.attrclass(property).ancestors.should be_include(Puppet::Property) end @@ -184,4 +190,16 @@ describe user do @password.change_to_s("other", "mypass").should_not be_include("mypass") end end + + describe "when manages_solaris_rbac is enabled" do + before do + @provider.stubs(:satisfies?).returns(false) + @provider.expects(:satisfies?).with(:manages_solaris_rbac).returns(true) + end + + it "should support a :role value for ensure" do + @ensure = user.attrclass(:ensure).new(:resource => @resource) + lambda { @ensure.should = :role }.should_not raise_error + end + end end diff --git a/spec/unit/util/selinux.rb b/spec/unit/util/selinux.rb index 515c3a273..7a56f914a 100644 --- a/spec/unit/util/selinux.rb +++ b/spec/unit/util/selinux.rb @@ -117,7 +117,7 @@ describe Puppet::Util::SELinux do it "should use chcon to set a context" do self.expects(:selinux_support?).returns true - self.expects(:execute).with(["/usr/bin/chcon","-h","","user_u:role_r:type_t:s0","/foo"]).returns 0 + self.expects(:execute).with(["/usr/bin/chcon","-h","user_u:role_r:type_t:s0","/foo"]).returns 0 set_selinux_context("/foo", "user_u:role_r:type_t:s0").should be_true end |
