diff options
author | Markus Roberts <Markus@reality.com> | 2010-07-09 18:12:17 -0700 |
---|---|---|
committer | Markus Roberts <Markus@reality.com> | 2010-07-09 18:12:17 -0700 |
commit | 3180b9d9b2c844dade1d361326600f7001ec66dd (patch) | |
tree | 98fe7c5ac7eb942aac9c39f019a17b0b3f5a57f4 /spec/unit/provider | |
parent | 543225970225de5697734bfaf0a6eee996802c04 (diff) | |
download | puppet-3180b9d9b2c844dade1d361326600f7001ec66dd.tar.gz puppet-3180b9d9b2c844dade1d361326600f7001ec66dd.tar.xz puppet-3180b9d9b2c844dade1d361326600f7001ec66dd.zip |
Code smell: Two space indentation
Replaced 106806 occurances of ^( +)(.*$) with
The ruby community almost universally (i.e. everyone but Luke, Markus, and the other eleven people
who learned ruby in the 1900s) uses two-space indentation.
3 Examples:
The code:
end
# Tell getopt which arguments are valid
def test_get_getopt_args
element = Setting.new :name => "foo", :desc => "anything", :settings => Puppet::Util::Settings.new
assert_equal([["--foo", GetoptLong::REQUIRED_ARGUMENT]], element.getopt_args, "Did not produce appropriate getopt args")
becomes:
end
# Tell getopt which arguments are valid
def test_get_getopt_args
element = Setting.new :name => "foo", :desc => "anything", :settings => Puppet::Util::Settings.new
assert_equal([["--foo", GetoptLong::REQUIRED_ARGUMENT]], element.getopt_args, "Did not produce appropriate getopt args")
The code:
assert_equal(str, val)
assert_instance_of(Float, result)
end
# Now test it with a passed object
becomes:
assert_equal(str, val)
assert_instance_of(Float, result)
end
# Now test it with a passed object
The code:
end
assert_nothing_raised do
klass[:Yay] = "boo"
klass["Cool"] = :yayness
end
becomes:
end
assert_nothing_raised do
klass[:Yay] = "boo"
klass["Cool"] = :yayness
end
Diffstat (limited to 'spec/unit/provider')
45 files changed, 4194 insertions, 4194 deletions
diff --git a/spec/unit/provider/augeas/augeas_spec.rb b/spec/unit/provider/augeas/augeas_spec.rb index 47655a753..07b632083 100644 --- a/spec/unit/provider/augeas/augeas_spec.rb +++ b/spec/unit/provider/augeas/augeas_spec.rb @@ -6,408 +6,408 @@ provider_class = Puppet::Type.type(:augeas).provider(:augeas) describe provider_class do - describe "command parsing" do - before do - @resource = stub("resource") - @provider = provider_class.new(@resource) - end - - it "should break apart a single line into three tokens and clean up the context" do - @resource.stubs(:[]).returns("/context") - tokens = @provider.parse_commands("set Jar/Jar Binks") - tokens.size.should == 1 - tokens[0].size.should == 3 - tokens[0][0].should == "set" - tokens[0][1].should == "/context/Jar/Jar" - tokens[0][2].should == "Binks" - end - - it "should break apart a multiple line into six tokens" do - @resource.stubs(:[]).returns("") - tokens = @provider.parse_commands("set /Jar/Jar Binks\nrm anakin") - tokens.size.should == 2 - tokens[0].size.should == 3 - tokens[1].size.should == 2 - tokens[0][0].should == "set" - tokens[0][1].should == "/Jar/Jar" - tokens[0][2].should == "Binks" - tokens[1][0].should == "rm" - tokens[1][1].should == "anakin" - end - - it "should strip whitespace and ignore blank lines" do - @resource.stubs(:[]).returns("") - tokens = @provider.parse_commands(" set /Jar/Jar Binks \t\n \n\n rm anakin ") - tokens.size.should == 2 - tokens[0].size.should == 3 - tokens[1].size.should == 2 - tokens[0][0].should == "set" - tokens[0][1].should == "/Jar/Jar" - tokens[0][2].should == "Binks" - tokens[1][0].should == "rm" - tokens[1][1].should == "anakin" - end - - it "should handle arrays" do - @resource.stubs(:[]).returns("/foo/") - commands = ["set /Jar/Jar Binks", "rm anakin"] - tokens = @provider.parse_commands(commands) - tokens.size.should == 2 - tokens[0].size.should == 3 - tokens[1].size.should == 2 - tokens[0][0].should == "set" - tokens[0][1].should == "/Jar/Jar" - tokens[0][2].should == "Binks" - tokens[1][0].should == "rm" - tokens[1][1].should == "/foo/anakin" - end - - # This is not supported in the new parsing class - #it "should concat the last values" do - # provider = provider_class.new - # tokens = provider.parse_commands("set /Jar/Jar Binks is my copilot") - # tokens.size.should == 1 - # tokens[0].size.should == 3 - # tokens[0][0].should == "set" - # tokens[0][1].should == "/Jar/Jar" - # tokens[0][2].should == "Binks is my copilot" - #end - - it "should accept spaces in the value and single ticks" do - @resource.stubs(:[]).returns("/foo/") - tokens = @provider.parse_commands("set JarJar 'Binks is my copilot'") - tokens.size.should == 1 - tokens[0].size.should == 3 - tokens[0][0].should == "set" - tokens[0][1].should == "/foo/JarJar" - tokens[0][2].should == "Binks is my copilot" - end - - it "should accept spaces in the value and double ticks" do - @resource.stubs(:[]).returns("/foo/") - tokens = @provider.parse_commands('set /JarJar "Binks is my copilot"') - tokens.size.should == 1 - tokens[0].size.should == 3 - tokens[0][0].should == "set" - tokens[0][1].should == '/JarJar' - tokens[0][2].should == 'Binks is my copilot' - end - - it "should accept mixed ticks" do - @resource.stubs(:[]).returns("/foo/") - tokens = @provider.parse_commands('set JarJar "Some \'Test\'"') - tokens.size.should == 1 - tokens[0].size.should == 3 - tokens[0][0].should == "set" - tokens[0][1].should == '/foo/JarJar' - tokens[0][2].should == "Some \'Test\'" - end - - it "should handle predicates with literals" do - @resource.stubs(:[]).returns("/foo/") - tokens = @provider.parse_commands("rm */*[module='pam_console.so']") - tokens.should == [["rm", "/foo/*/*[module='pam_console.so']"]] - end - - it "should handle whitespace in predicates" do - @resource.stubs(:[]).returns("/foo/") - tokens = @provider.parse_commands("ins 42 before /files/etc/hosts/*/ipaddr[ . = '127.0.0.1' ]") - tokens.should == [["ins", "42", "before","/files/etc/hosts/*/ipaddr[ . = '127.0.0.1' ]"]] - end - - it "should handle multiple predicates" do - @resource.stubs(:[]).returns("/foo/") - tokens = @provider.parse_commands("clear pam.d/*/*[module = 'system-auth'][type = 'account']") - tokens.should == [["clear", "/foo/pam.d/*/*[module = 'system-auth'][type = 'account']"]] - end - - it "should handle nested predicates" do - @resource.stubs(:[]).returns("/foo/") - args = ["clear", "/foo/pam.d/*/*[module[ ../type = 'type] = 'system-auth'][type[last()] = 'account']"] - tokens = @provider.parse_commands(args.join(" ")) - tokens.should == [ args ] - end - - it "should handle escaped doublequotes in doublequoted string" do - @resource.stubs(:[]).returns("/foo/") - tokens = @provider.parse_commands("set /foo \"''\\\"''\"") - tokens.should == [[ "set", "/foo", "''\\\"''" ]] - end - - it "should allow escaped spaces and brackets in paths" do - @resource.stubs(:[]).returns("/foo/") - args = [ "set", "/white\\ space/\\[section", "value" ] - tokens = @provider.parse_commands(args.join(" \t ")) - tokens.should == [ args ] - end - - it "should allow single quoted escaped spaces in paths" do - @resource.stubs(:[]).returns("/foo/") - args = [ "set", "'/white\\ space/key'", "value" ] - tokens = @provider.parse_commands(args.join(" \t ")) - tokens.should == [[ "set", "/white\\ space/key", "value" ]] - end - - it "should allow double quoted escaped spaces in paths" do - @resource.stubs(:[]).returns("/foo/") - args = [ "set", '"/white\\ space/key"', "value" ] - tokens = @provider.parse_commands(args.join(" \t ")) - tokens.should == [[ "set", "/white\\ space/key", "value" ]] - end - - it "should remove trailing slashes" do - @resource.stubs(:[]).returns("/foo/") - tokens = @provider.parse_commands("set foo/ bar") - tokens.should == [[ "set", "/foo/foo", "bar" ]] - end - end - - describe "get filters" do - before do - augeas_stub = stub("augeas", :get => "value") - @provider = provider_class.new - @provider.aug= augeas_stub - end - - it "should return false for a = nonmatch" do - command = ["get", "fake value", "==", "value"] - @provider.process_get(command).should == true - end - - it "should return true for a != match" do - command = ["get", "fake value", "!=", "value"] - @provider.process_get(command).should == false - end - - it "should return true for a =~ match" do - command = ["get", "fake value", "=~", "val*"] - @provider.process_get(command).should == true - end - - it "should return false for a == nonmatch" do - command = ["get", "fake value", "=~", "num*"] - @provider.process_get(command).should == false - end - end - - describe "match filters" do - before do - resource = stub("resource", :[] => "") - augeas_stub = stub("augeas", :match => ["set", "of", "values"]) - @provider = provider_class.new(resource) - @provider.aug= augeas_stub - end - - it "should return true for size match" do - command = ["match", "fake value", "size == 3"] - @provider.process_match(command).should == true - end - - it "should return false for a size non match" do - command = ["match", "fake value", "size < 3"] - @provider.process_match(command).should == false - end - - it "should return true for includes match" do - command = ["match", "fake value", "include values"] - @provider.process_match(command).should == true - end - - it "should return false for includes non match" do - command = ["match", "fake value", "include JarJar"] - @provider.process_match(command).should == false - end - - it "should return true for includes match" do - command = ["match", "fake value", "not_include JarJar"] - @provider.process_match(command).should == true - end - - it "should return false for includes non match" do - command = ["match", "fake value", "not_include values"] - @provider.process_match(command).should == false - end - - it "should return true for an array match" do - command = ["match", "fake value", "== ['set', 'of', 'values']"] - @provider.process_match(command).should == true - end - - it "should return false for an array non match" do - command = ["match", "fake value", "== ['this', 'should', 'not', 'match']"] - @provider.process_match(command).should == false - end - - it "should return false for an array match with noteq" do - command = ["match", "fake value", "!= ['set', 'of', 'values']"] - @provider.process_match(command).should == false - end - - it "should return true for an array non match with noteq" do - command = ["match", "fake value", "!= ['this', 'should', 'not', 'match']"] - @provider.process_match(command).should == true - end - end - - describe "need to run" do - it "should handle no filters" do - resource = stub("resource") - resource.stubs(:[]).returns(false).then.returns("").then.returns("") - augeas_stub = stub("augeas", :match => ["set", "of", "values"]) - augeas_stub.stubs("close") - provider = provider_class.new(resource) - provider.aug= augeas_stub - provider.stubs(:get_augeas_version).returns("0.3.5") - provider.need_to_run?.should == true - end - - it "should return true when a get filter matches" do - resource = stub("resource") - resource.stubs(:[]).returns(false).then.returns("get path == value").then.returns("") - provider = provider_class.new(resource) - augeas_stub = stub("augeas", :get => "value") - augeas_stub.stubs("close") - provider.aug= augeas_stub - provider.stubs(:get_augeas_version).returns("0.3.5") - provider.need_to_run?.should == true - end - - it "should return false when a get filter does not match" do - resource = stub("resource") - resource.stubs(:[]).returns(false).then.returns("get path == another value").then.returns("") - provider = provider_class.new(resource) - augeas_stub = stub("augeas", :get => "value") - augeas_stub.stubs("close") - provider.aug= augeas_stub - provider.stubs(:get_augeas_version).returns("0.3.5") - provider.need_to_run?.should == false - end - - it "should return true when a match filter matches" do - resource = stub("resource") - resource.stubs(:[]).returns(false).then.returns("match path size == 3").then.returns("") - provider = provider_class.new(resource) - augeas_stub = stub("augeas", :match => ["set", "of", "values"]) - augeas_stub.stubs("close") - provider.aug= augeas_stub - provider.stubs(:get_augeas_version).returns("0.3.5") - provider.need_to_run?.should == true - end - - it "should return false when a match filter does not match" do - resource = stub("resource") - resource.stubs(:[]).returns(false).then.returns("match path size == 2").then.returns("") - provider = provider_class.new(resource) - augeas_stub = stub("augeas", :match => ["set", "of", "values"]) - augeas_stub.stubs("close") - provider.aug= augeas_stub - provider.stubs(:get_augeas_version).returns("0.3.5") - provider.need_to_run?.should == false - end - - #This is a copy of the last one, with setting the force to true - it "setting force should not change the above logic" do - resource = stub("resource") - resource.stubs(:[]).returns(true).then.returns("match path size == 2").then.returns("") - provider = provider_class.new(resource) - augeas_stub = stub("augeas", :match => ["set", "of", "values"]) - augeas_stub.stubs("close") - provider.aug= augeas_stub - provider.stubs(:get_augeas_version).returns("0.3.5") - provider.need_to_run?.should == false - end - end - - describe "augeas execution integration" do - - before do - @resource = stub("resource") - @provider = provider_class.new(@resource) - @augeas = stub("augeas") - @provider.aug= @augeas - @provider.stubs(:get_augeas_version).returns("0.3.5") - end - - it "should handle set commands" do - command = "set JarJar Binks" - context = "/some/path/" - @resource.expects(:[]).times(2).returns(command).then.returns(context) - @augeas.expects(:set).with("/some/path/JarJar", "Binks").returns(true) - @augeas.expects(:save).returns(true) - @augeas.expects(:close) - @provider.execute_changes.should == :executed - end - - it "should handle rm commands" do - command = "rm /Jar/Jar" - context = "" - @resource.expects(:[]).times(2).returns(command).then.returns(context) - @augeas.expects(:rm).with("/Jar/Jar") - @augeas.expects(:save).returns(true) - @augeas.expects(:close) - @provider.execute_changes.should == :executed - end - - it "should handle remove commands" do - command = "remove /Jar/Jar" - context = "" - @resource.expects(:[]).times(2).returns(command).then.returns(context) - @augeas.expects(:rm).with("/Jar/Jar") - @augeas.expects(:save).returns(true) - @augeas.expects(:close) - @provider.execute_changes.should == :executed - end - - it "should handle clear commands" do - command = "clear Jar/Jar" - context = "/foo/" - @resource.expects(:[]).times(2).returns(command).then.returns(context) - @augeas.expects(:clear).with("/foo/Jar/Jar").returns(true) - @augeas.expects(:save).returns(true) - @augeas.expects(:close) - @provider.execute_changes.should == :executed - end - - - it "should handle ins commands with before" do - command = "ins Binks before Jar/Jar" - context = "/foo" - @resource.expects(:[]).times(2).returns(command).then.returns(context) - @augeas.expects(:insert).with("/foo/Jar/Jar", "Binks", true) - @augeas.expects(:save).returns(true) - @augeas.expects(:close) - @provider.execute_changes.should == :executed - end - - it "should handle ins commands with after" do - command = "ins Binks after /Jar/Jar" - context = "/foo" - @resource.expects(:[]).times(2).returns(command).then.returns(context) - @augeas.expects(:insert).with("/Jar/Jar", "Binks", false) - @augeas.expects(:save).returns(true) - @augeas.expects(:close) - @provider.execute_changes.should == :executed - end - - it "should handle ins with no context" do - command = "ins Binks after /Jar/Jar" - context = "" # this is the default - @resource.expects(:[]).times(2).returns(command).then.returns(context) - @augeas.expects(:insert).with("/Jar/Jar", "Binks", false) - @augeas.expects(:save).returns(true) - @augeas.expects(:close) - @provider.execute_changes.should == :executed - end - - it "should handle multiple commands" do - command = ["ins Binks after /Jar/Jar", "clear Jar/Jar"] - context = "/foo/" - @resource.expects(:[]).times(2).returns(command).then.returns(context) - @augeas.expects(:insert).with("/Jar/Jar", "Binks", false) - @augeas.expects(:clear).with("/foo/Jar/Jar").returns(true) - @augeas.expects(:save).returns(true) - @augeas.expects(:close) - @provider.execute_changes.should == :executed - end + describe "command parsing" do + before do + @resource = stub("resource") + @provider = provider_class.new(@resource) end + + it "should break apart a single line into three tokens and clean up the context" do + @resource.stubs(:[]).returns("/context") + tokens = @provider.parse_commands("set Jar/Jar Binks") + tokens.size.should == 1 + tokens[0].size.should == 3 + tokens[0][0].should == "set" + tokens[0][1].should == "/context/Jar/Jar" + tokens[0][2].should == "Binks" + end + + it "should break apart a multiple line into six tokens" do + @resource.stubs(:[]).returns("") + tokens = @provider.parse_commands("set /Jar/Jar Binks\nrm anakin") + tokens.size.should == 2 + tokens[0].size.should == 3 + tokens[1].size.should == 2 + tokens[0][0].should == "set" + tokens[0][1].should == "/Jar/Jar" + tokens[0][2].should == "Binks" + tokens[1][0].should == "rm" + tokens[1][1].should == "anakin" + end + + it "should strip whitespace and ignore blank lines" do + @resource.stubs(:[]).returns("") + tokens = @provider.parse_commands(" set /Jar/Jar Binks \t\n \n\n rm anakin ") + tokens.size.should == 2 + tokens[0].size.should == 3 + tokens[1].size.should == 2 + tokens[0][0].should == "set" + tokens[0][1].should == "/Jar/Jar" + tokens[0][2].should == "Binks" + tokens[1][0].should == "rm" + tokens[1][1].should == "anakin" + end + + it "should handle arrays" do + @resource.stubs(:[]).returns("/foo/") + commands = ["set /Jar/Jar Binks", "rm anakin"] + tokens = @provider.parse_commands(commands) + tokens.size.should == 2 + tokens[0].size.should == 3 + tokens[1].size.should == 2 + tokens[0][0].should == "set" + tokens[0][1].should == "/Jar/Jar" + tokens[0][2].should == "Binks" + tokens[1][0].should == "rm" + tokens[1][1].should == "/foo/anakin" + end + + # This is not supported in the new parsing class + #it "should concat the last values" do + # provider = provider_class.new + # tokens = provider.parse_commands("set /Jar/Jar Binks is my copilot") + # tokens.size.should == 1 + # tokens[0].size.should == 3 + # tokens[0][0].should == "set" + # tokens[0][1].should == "/Jar/Jar" + # tokens[0][2].should == "Binks is my copilot" + #end + + it "should accept spaces in the value and single ticks" do + @resource.stubs(:[]).returns("/foo/") + tokens = @provider.parse_commands("set JarJar 'Binks is my copilot'") + tokens.size.should == 1 + tokens[0].size.should == 3 + tokens[0][0].should == "set" + tokens[0][1].should == "/foo/JarJar" + tokens[0][2].should == "Binks is my copilot" + end + + it "should accept spaces in the value and double ticks" do + @resource.stubs(:[]).returns("/foo/") + tokens = @provider.parse_commands('set /JarJar "Binks is my copilot"') + tokens.size.should == 1 + tokens[0].size.should == 3 + tokens[0][0].should == "set" + tokens[0][1].should == '/JarJar' + tokens[0][2].should == 'Binks is my copilot' + end + + it "should accept mixed ticks" do + @resource.stubs(:[]).returns("/foo/") + tokens = @provider.parse_commands('set JarJar "Some \'Test\'"') + tokens.size.should == 1 + tokens[0].size.should == 3 + tokens[0][0].should == "set" + tokens[0][1].should == '/foo/JarJar' + tokens[0][2].should == "Some \'Test\'" + end + + it "should handle predicates with literals" do + @resource.stubs(:[]).returns("/foo/") + tokens = @provider.parse_commands("rm */*[module='pam_console.so']") + tokens.should == [["rm", "/foo/*/*[module='pam_console.so']"]] + end + + it "should handle whitespace in predicates" do + @resource.stubs(:[]).returns("/foo/") + tokens = @provider.parse_commands("ins 42 before /files/etc/hosts/*/ipaddr[ . = '127.0.0.1' ]") + tokens.should == [["ins", "42", "before","/files/etc/hosts/*/ipaddr[ . = '127.0.0.1' ]"]] + end + + it "should handle multiple predicates" do + @resource.stubs(:[]).returns("/foo/") + tokens = @provider.parse_commands("clear pam.d/*/*[module = 'system-auth'][type = 'account']") + tokens.should == [["clear", "/foo/pam.d/*/*[module = 'system-auth'][type = 'account']"]] + end + + it "should handle nested predicates" do + @resource.stubs(:[]).returns("/foo/") + args = ["clear", "/foo/pam.d/*/*[module[ ../type = 'type] = 'system-auth'][type[last()] = 'account']"] + tokens = @provider.parse_commands(args.join(" ")) + tokens.should == [ args ] + end + + it "should handle escaped doublequotes in doublequoted string" do + @resource.stubs(:[]).returns("/foo/") + tokens = @provider.parse_commands("set /foo \"''\\\"''\"") + tokens.should == [[ "set", "/foo", "''\\\"''" ]] + end + + it "should allow escaped spaces and brackets in paths" do + @resource.stubs(:[]).returns("/foo/") + args = [ "set", "/white\\ space/\\[section", "value" ] + tokens = @provider.parse_commands(args.join(" \t ")) + tokens.should == [ args ] + end + + it "should allow single quoted escaped spaces in paths" do + @resource.stubs(:[]).returns("/foo/") + args = [ "set", "'/white\\ space/key'", "value" ] + tokens = @provider.parse_commands(args.join(" \t ")) + tokens.should == [[ "set", "/white\\ space/key", "value" ]] + end + + it "should allow double quoted escaped spaces in paths" do + @resource.stubs(:[]).returns("/foo/") + args = [ "set", '"/white\\ space/key"', "value" ] + tokens = @provider.parse_commands(args.join(" \t ")) + tokens.should == [[ "set", "/white\\ space/key", "value" ]] + end + + it "should remove trailing slashes" do + @resource.stubs(:[]).returns("/foo/") + tokens = @provider.parse_commands("set foo/ bar") + tokens.should == [[ "set", "/foo/foo", "bar" ]] + end + end + + describe "get filters" do + before do + augeas_stub = stub("augeas", :get => "value") + @provider = provider_class.new + @provider.aug= augeas_stub + end + + it "should return false for a = nonmatch" do + command = ["get", "fake value", "==", "value"] + @provider.process_get(command).should == true + end + + it "should return true for a != match" do + command = ["get", "fake value", "!=", "value"] + @provider.process_get(command).should == false + end + + it "should return true for a =~ match" do + command = ["get", "fake value", "=~", "val*"] + @provider.process_get(command).should == true + end + + it "should return false for a == nonmatch" do + command = ["get", "fake value", "=~", "num*"] + @provider.process_get(command).should == false + end + end + + describe "match filters" do + before do + resource = stub("resource", :[] => "") + augeas_stub = stub("augeas", :match => ["set", "of", "values"]) + @provider = provider_class.new(resource) + @provider.aug= augeas_stub + end + + it "should return true for size match" do + command = ["match", "fake value", "size == 3"] + @provider.process_match(command).should == true + end + + it "should return false for a size non match" do + command = ["match", "fake value", "size < 3"] + @provider.process_match(command).should == false + end + + it "should return true for includes match" do + command = ["match", "fake value", "include values"] + @provider.process_match(command).should == true + end + + it "should return false for includes non match" do + command = ["match", "fake value", "include JarJar"] + @provider.process_match(command).should == false + end + + it "should return true for includes match" do + command = ["match", "fake value", "not_include JarJar"] + @provider.process_match(command).should == true + end + + it "should return false for includes non match" do + command = ["match", "fake value", "not_include values"] + @provider.process_match(command).should == false + end + + it "should return true for an array match" do + command = ["match", "fake value", "== ['set', 'of', 'values']"] + @provider.process_match(command).should == true + end + + it "should return false for an array non match" do + command = ["match", "fake value", "== ['this', 'should', 'not', 'match']"] + @provider.process_match(command).should == false + end + + it "should return false for an array match with noteq" do + command = ["match", "fake value", "!= ['set', 'of', 'values']"] + @provider.process_match(command).should == false + end + + it "should return true for an array non match with noteq" do + command = ["match", "fake value", "!= ['this', 'should', 'not', 'match']"] + @provider.process_match(command).should == true + end + end + + describe "need to run" do + it "should handle no filters" do + resource = stub("resource") + resource.stubs(:[]).returns(false).then.returns("").then.returns("") + augeas_stub = stub("augeas", :match => ["set", "of", "values"]) + augeas_stub.stubs("close") + provider = provider_class.new(resource) + provider.aug= augeas_stub + provider.stubs(:get_augeas_version).returns("0.3.5") + provider.need_to_run?.should == true + end + + it "should return true when a get filter matches" do + resource = stub("resource") + resource.stubs(:[]).returns(false).then.returns("get path == value").then.returns("") + provider = provider_class.new(resource) + augeas_stub = stub("augeas", :get => "value") + augeas_stub.stubs("close") + provider.aug= augeas_stub + provider.stubs(:get_augeas_version).returns("0.3.5") + provider.need_to_run?.should == true + end + + it "should return false when a get filter does not match" do + resource = stub("resource") + resource.stubs(:[]).returns(false).then.returns("get path == another value").then.returns("") + provider = provider_class.new(resource) + augeas_stub = stub("augeas", :get => "value") + augeas_stub.stubs("close") + provider.aug= augeas_stub + provider.stubs(:get_augeas_version).returns("0.3.5") + provider.need_to_run?.should == false + end + + it "should return true when a match filter matches" do + resource = stub("resource") + resource.stubs(:[]).returns(false).then.returns("match path size == 3").then.returns("") + provider = provider_class.new(resource) + augeas_stub = stub("augeas", :match => ["set", "of", "values"]) + augeas_stub.stubs("close") + provider.aug= augeas_stub + provider.stubs(:get_augeas_version).returns("0.3.5") + provider.need_to_run?.should == true + end + + it "should return false when a match filter does not match" do + resource = stub("resource") + resource.stubs(:[]).returns(false).then.returns("match path size == 2").then.returns("") + provider = provider_class.new(resource) + augeas_stub = stub("augeas", :match => ["set", "of", "values"]) + augeas_stub.stubs("close") + provider.aug= augeas_stub + provider.stubs(:get_augeas_version).returns("0.3.5") + provider.need_to_run?.should == false + end + + #This is a copy of the last one, with setting the force to true + it "setting force should not change the above logic" do + resource = stub("resource") + resource.stubs(:[]).returns(true).then.returns("match path size == 2").then.returns("") + provider = provider_class.new(resource) + augeas_stub = stub("augeas", :match => ["set", "of", "values"]) + augeas_stub.stubs("close") + provider.aug= augeas_stub + provider.stubs(:get_augeas_version).returns("0.3.5") + provider.need_to_run?.should == false + end + end + + describe "augeas execution integration" do + + before do + @resource = stub("resource") + @provider = provider_class.new(@resource) + @augeas = stub("augeas") + @provider.aug= @augeas + @provider.stubs(:get_augeas_version).returns("0.3.5") + end + + it "should handle set commands" do + command = "set JarJar Binks" + context = "/some/path/" + @resource.expects(:[]).times(2).returns(command).then.returns(context) + @augeas.expects(:set).with("/some/path/JarJar", "Binks").returns(true) + @augeas.expects(:save).returns(true) + @augeas.expects(:close) + @provider.execute_changes.should == :executed + end + + it "should handle rm commands" do + command = "rm /Jar/Jar" + context = "" + @resource.expects(:[]).times(2).returns(command).then.returns(context) + @augeas.expects(:rm).with("/Jar/Jar") + @augeas.expects(:save).returns(true) + @augeas.expects(:close) + @provider.execute_changes.should == :executed + end + + it "should handle remove commands" do + command = "remove /Jar/Jar" + context = "" + @resource.expects(:[]).times(2).returns(command).then.returns(context) + @augeas.expects(:rm).with("/Jar/Jar") + @augeas.expects(:save).returns(true) + @augeas.expects(:close) + @provider.execute_changes.should == :executed + end + + it "should handle clear commands" do + command = "clear Jar/Jar" + context = "/foo/" + @resource.expects(:[]).times(2).returns(command).then.returns(context) + @augeas.expects(:clear).with("/foo/Jar/Jar").returns(true) + @augeas.expects(:save).returns(true) + @augeas.expects(:close) + @provider.execute_changes.should == :executed + end + + + it "should handle ins commands with before" do + command = "ins Binks before Jar/Jar" + context = "/foo" + @resource.expects(:[]).times(2).returns(command).then.returns(context) + @augeas.expects(:insert).with("/foo/Jar/Jar", "Binks", true) + @augeas.expects(:save).returns(true) + @augeas.expects(:close) + @provider.execute_changes.should == :executed + end + + it "should handle ins commands with after" do + command = "ins Binks after /Jar/Jar" + context = "/foo" + @resource.expects(:[]).times(2).returns(command).then.returns(context) + @augeas.expects(:insert).with("/Jar/Jar", "Binks", false) + @augeas.expects(:save).returns(true) + @augeas.expects(:close) + @provider.execute_changes.should == :executed + end + + it "should handle ins with no context" do + command = "ins Binks after /Jar/Jar" + context = "" # this is the default + @resource.expects(:[]).times(2).returns(command).then.returns(context) + @augeas.expects(:insert).with("/Jar/Jar", "Binks", false) + @augeas.expects(:save).returns(true) + @augeas.expects(:close) + @provider.execute_changes.should == :executed + end + + it "should handle multiple commands" do + command = ["ins Binks after /Jar/Jar", "clear Jar/Jar"] + context = "/foo/" + @resource.expects(:[]).times(2).returns(command).then.returns(context) + @augeas.expects(:insert).with("/Jar/Jar", "Binks", false) + @augeas.expects(:clear).with("/foo/Jar/Jar").returns(true) + @augeas.expects(:save).returns(true) + @augeas.expects(:close) + @provider.execute_changes.should == :executed + end + end end diff --git a/spec/unit/provider/confine/exists_spec.rb b/spec/unit/provider/confine/exists_spec.rb index a3539c4f5..c3958e317 100755 --- a/spec/unit/provider/confine/exists_spec.rb +++ b/spec/unit/provider/confine/exists_spec.rb @@ -5,77 +5,77 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/provider/confine/exists' describe Puppet::Provider::Confine::Exists do - before do - @confine = Puppet::Provider::Confine::Exists.new("/my/file") - @confine.label = "eh" + before do + @confine = Puppet::Provider::Confine::Exists.new("/my/file") + @confine.label = "eh" + end + + it "should be named :exists" do + Puppet::Provider::Confine::Exists.name.should == :exists + end + + it "should use the 'pass?' method to test validity" do + @confine.expects(:pass?).with("/my/file") + @confine.valid? + end + + it "should return false if the value is false" do + @confine.pass?(false).should be_false + end + + it "should return false if the value does not point to a file" do + FileTest.expects(:exist?).with("/my/file").returns false + @confine.pass?("/my/file").should be_false + end + + it "should return true if the value points to a file" do + FileTest.expects(:exist?).with("/my/file").returns true + @confine.pass?("/my/file").should be_true + end + + it "should produce a message saying that a file is missing" do + @confine.message("/my/file").should be_include("does not exist") + end + + describe "and the confine is for binaries" do + before { @confine.stubs(:for_binary).returns true } + it "should use its 'binary' method to look up the full path of the file" do + @confine.expects(:binary).returns nil + @confine.pass?("/my/file") end - it "should be named :exists" do - Puppet::Provider::Confine::Exists.name.should == :exists + it "should return false if no binary can be found" do + @confine.expects(:binary).with("/my/file").returns nil + @confine.pass?("/my/file").should be_false end - it "should use the 'pass?' method to test validity" do - @confine.expects(:pass?).with("/my/file") - @confine.valid? + it "should return true if the binary can be found and the file exists" do + @confine.expects(:binary).with("/my/file").returns "/my/file" + FileTest.expects(:exist?).with("/my/file").returns true + @confine.pass?("/my/file").should be_true end - it "should return false if the value is false" do - @confine.pass?(false).should be_false + it "should return false if the binary can be found but the file does not exist" do + @confine.expects(:binary).with("/my/file").returns "/my/file" + FileTest.expects(:exist?).with("/my/file").returns true + @confine.pass?("/my/file").should be_true end + end - it "should return false if the value does not point to a file" do - FileTest.expects(:exist?).with("/my/file").returns false - @confine.pass?("/my/file").should be_false - end - - it "should return true if the value points to a file" do - FileTest.expects(:exist?).with("/my/file").returns true - @confine.pass?("/my/file").should be_true - end - - it "should produce a message saying that a file is missing" do - @confine.message("/my/file").should be_include("does not exist") - end + it "should produce a summary containing all missing files" do + FileTest.stubs(:exist?).returns true + FileTest.expects(:exist?).with("/two").returns false + FileTest.expects(:exist?).with("/four").returns false - describe "and the confine is for binaries" do - before { @confine.stubs(:for_binary).returns true } - it "should use its 'binary' method to look up the full path of the file" do - @confine.expects(:binary).returns nil - @confine.pass?("/my/file") - end - - it "should return false if no binary can be found" do - @confine.expects(:binary).with("/my/file").returns nil - @confine.pass?("/my/file").should be_false - end - - it "should return true if the binary can be found and the file exists" do - @confine.expects(:binary).with("/my/file").returns "/my/file" - FileTest.expects(:exist?).with("/my/file").returns true - @confine.pass?("/my/file").should be_true - end - - it "should return false if the binary can be found but the file does not exist" do - @confine.expects(:binary).with("/my/file").returns "/my/file" - FileTest.expects(:exist?).with("/my/file").returns true - @confine.pass?("/my/file").should be_true - end - end - - it "should produce a summary containing all missing files" do - FileTest.stubs(:exist?).returns true - FileTest.expects(:exist?).with("/two").returns false - FileTest.expects(:exist?).with("/four").returns false + confine = Puppet::Provider::Confine::Exists.new %w{/one /two /three /four} + confine.summary.should == %w{/two /four} + end - confine = Puppet::Provider::Confine::Exists.new %w{/one /two /three /four} - confine.summary.should == %w{/two /four} - end + it "should summarize multiple instances by returning a flattened array of their summaries" do + c1 = mock '1', :summary => %w{one} + c2 = mock '2', :summary => %w{two} + c3 = mock '3', :summary => %w{three} - it "should summarize multiple instances by returning a flattened array of their summaries" do - c1 = mock '1', :summary => %w{one} - c2 = mock '2', :summary => %w{two} - c3 = mock '3', :summary => %w{three} - - Puppet::Provider::Confine::Exists.summarize([c1, c2, c3]).should == %w{one two three} - end + Puppet::Provider::Confine::Exists.summarize([c1, c2, c3]).should == %w{one two three} + end end diff --git a/spec/unit/provider/confine/false_spec.rb b/spec/unit/provider/confine/false_spec.rb index 425aa3077..999fc4714 100755 --- a/spec/unit/provider/confine/false_spec.rb +++ b/spec/unit/provider/confine/false_spec.rb @@ -5,49 +5,49 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/provider/confine/false' describe Puppet::Provider::Confine::False do - it "should be named :false" do - Puppet::Provider::Confine::False.name.should == :false + it "should be named :false" do + Puppet::Provider::Confine::False.name.should == :false + end + + it "should require a value" do + lambda { Puppet::Provider::Confine.new }.should raise_error(ArgumentError) + end + + describe "when testing values" do + before { @confine = Puppet::Provider::Confine::False.new("foo") } + + it "should use the 'pass?' method to test validity" do + @confine = Puppet::Provider::Confine::False.new("foo") + @confine.label = "eh" + @confine.expects(:pass?).with("foo") + @confine.valid? end - it "should require a value" do - lambda { Puppet::Provider::Confine.new }.should raise_error(ArgumentError) + it "should return true if the value is false" do + @confine.pass?(false).should be_true end - describe "when testing values" do - before { @confine = Puppet::Provider::Confine::False.new("foo") } - - it "should use the 'pass?' method to test validity" do - @confine = Puppet::Provider::Confine::False.new("foo") - @confine.label = "eh" - @confine.expects(:pass?).with("foo") - @confine.valid? - end - - it "should return true if the value is false" do - @confine.pass?(false).should be_true - end - - it "should return false if the value is not false" do - @confine.pass?("else").should be_false - end - - it "should produce a message that a value is true" do - @confine = Puppet::Provider::Confine::False.new("foo") - @confine.message("eh").should be_include("true") - end + it "should return false if the value is not false" do + @confine.pass?("else").should be_false end - it "should be able to produce a summary with the number of incorrectly true values" do - confine = Puppet::Provider::Confine::False.new %w{one two three four} - confine.expects(:pass?).times(4).returns(true).returns(false).returns(true).returns(false) - confine.summary.should == 2 + it "should produce a message that a value is true" do + @confine = Puppet::Provider::Confine::False.new("foo") + @confine.message("eh").should be_include("true") end + end - it "should summarize multiple instances by summing their summaries" do - c1 = mock '1', :summary => 1 - c2 = mock '2', :summary => 2 - c3 = mock '3', :summary => 3 + it "should be able to produce a summary with the number of incorrectly true values" do + confine = Puppet::Provider::Confine::False.new %w{one two three four} + confine.expects(:pass?).times(4).returns(true).returns(false).returns(true).returns(false) + confine.summary.should == 2 + end - Puppet::Provider::Confine::False.summarize([c1, c2, c3]).should == 6 - end + it "should summarize multiple instances by summing their summaries" do + c1 = mock '1', :summary => 1 + c2 = mock '2', :summary => 2 + c3 = mock '3', :summary => 3 + + Puppet::Provider::Confine::False.summarize([c1, c2, c3]).should == 6 + end end diff --git a/spec/unit/provider/confine/feature_spec.rb b/spec/unit/provider/confine/feature_spec.rb index 1db81ba44..e8368efac 100755 --- a/spec/unit/provider/confine/feature_spec.rb +++ b/spec/unit/provider/confine/feature_spec.rb @@ -5,56 +5,56 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/provider/confine/feature' describe Puppet::Provider::Confine::Feature do - it "should be named :feature" do - Puppet::Provider::Confine::Feature.name.should == :feature + it "should be named :feature" do + Puppet::Provider::Confine::Feature.name.should == :feature + end + + it "should require a value" do + lambda { Puppet::Provider::Confine::Feature.new }.should raise_error(ArgumentError) + end + + it "should always convert values to an array" do + Puppet::Provider::Confine::Feature.new("/some/file").values.should be_instance_of(Array) + end + + describe "when testing values" do + before do + @features = mock 'features' + Puppet.stubs(:features).returns @features + @confine = Puppet::Provider::Confine::Feature.new("myfeature") + @confine.label = "eh" end - it "should require a value" do - lambda { Puppet::Provider::Confine::Feature.new }.should raise_error(ArgumentError) + it "should use the Puppet features instance to test validity" do + @features.expects(:myfeature?) + @confine.valid? end - it "should always convert values to an array" do - Puppet::Provider::Confine::Feature.new("/some/file").values.should be_instance_of(Array) + it "should return true if the feature is present" do + @features.expects(:myfeature?).returns true + @confine.pass?("myfeature").should be_true end - describe "when testing values" do - before do - @features = mock 'features' - Puppet.stubs(:features).returns @features - @confine = Puppet::Provider::Confine::Feature.new("myfeature") - @confine.label = "eh" - end - - it "should use the Puppet features instance to test validity" do - @features.expects(:myfeature?) - @confine.valid? - end - - it "should return true if the feature is present" do - @features.expects(:myfeature?).returns true - @confine.pass?("myfeature").should be_true - end - - it "should return false if the value is false" do - @features.expects(:myfeature?).returns false - @confine.pass?("myfeature").should be_false - end - - it "should log that a feature is missing" do - @confine.message("myfeat").should be_include("missing") - end + it "should return false if the value is false" do + @features.expects(:myfeature?).returns false + @confine.pass?("myfeature").should be_false end - it "should summarize multiple instances by returning a flattened array of all missing features" do - confines = [] - confines << Puppet::Provider::Confine::Feature.new(%w{one two}) - confines << Puppet::Provider::Confine::Feature.new(%w{two}) - confines << Puppet::Provider::Confine::Feature.new(%w{three four}) + it "should log that a feature is missing" do + @confine.message("myfeat").should be_include("missing") + end + end - features = mock 'feature' - features.stub_everything - Puppet.stubs(:features).returns features + it "should summarize multiple instances by returning a flattened array of all missing features" do + confines = [] + confines << Puppet::Provider::Confine::Feature.new(%w{one two}) + confines << Puppet::Provider::Confine::Feature.new(%w{two}) + confines << Puppet::Provider::Confine::Feature.new(%w{three four}) - Puppet::Provider::Confine::Feature.summarize(confines).sort.should == %w{one two three four}.sort - end + features = mock 'feature' + features.stub_everything + Puppet.stubs(:features).returns features + + Puppet::Provider::Confine::Feature.summarize(confines).sort.should == %w{one two three four}.sort + end end diff --git a/spec/unit/provider/confine/true_spec.rb b/spec/unit/provider/confine/true_spec.rb index 2675afe75..23041e6d5 100755 --- a/spec/unit/provider/confine/true_spec.rb +++ b/spec/unit/provider/confine/true_spec.rb @@ -5,49 +5,49 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/provider/confine/true' describe Puppet::Provider::Confine::True do - it "should be named :true" do - Puppet::Provider::Confine::True.name.should == :true + it "should be named :true" do + Puppet::Provider::Confine::True.name.should == :true + end + + it "should require a value" do + lambda { Puppet::Provider::Confine::True.new }.should raise_error(ArgumentError) + end + + describe "when testing values" do + before do + @confine = Puppet::Provider::Confine::True.new("foo") + @confine.label = "eh" end - it "should require a value" do - lambda { Puppet::Provider::Confine::True.new }.should raise_error(ArgumentError) + it "should use the 'pass?' method to test validity" do + @confine.expects(:pass?).with("foo") + @confine.valid? end - describe "when testing values" do - before do - @confine = Puppet::Provider::Confine::True.new("foo") - @confine.label = "eh" - end - - it "should use the 'pass?' method to test validity" do - @confine.expects(:pass?).with("foo") - @confine.valid? - end - - it "should return true if the value is not false" do - @confine.pass?("else").should be_true - end - - it "should return false if the value is false" do - @confine.pass?(nil).should be_false - end + it "should return true if the value is not false" do + @confine.pass?("else").should be_true + end - it "should produce the message that a value is false" do - @confine.message("eh").should be_include("false") - end + it "should return false if the value is false" do + @confine.pass?(nil).should be_false end - it "should produce the number of false values when asked for a summary" do - @confine = Puppet::Provider::Confine::True.new %w{one two three four} - @confine.expects(:pass?).times(4).returns(true).returns(false).returns(true).returns(false) - @confine.summary.should == 2 + it "should produce the message that a value is false" do + @confine.message("eh").should be_include("false") end + end - it "should summarize multiple instances by summing their summaries" do - c1 = mock '1', :summary => 1 - c2 = mock '2', :summary => 2 - c3 = mock '3', :summary => 3 + it "should produce the number of false values when asked for a summary" do + @confine = Puppet::Provider::Confine::True.new %w{one two three four} + @confine.expects(:pass?).times(4).returns(true).returns(false).returns(true).returns(false) + @confine.summary.should == 2 + end - Puppet::Provider::Confine::True.summarize([c1, c2, c3]).should == 6 - end + it "should summarize multiple instances by summing their summaries" do + c1 = mock '1', :summary => 1 + c2 = mock '2', :summary => 2 + c3 = mock '3', :summary => 3 + + Puppet::Provider::Confine::True.summarize([c1, c2, c3]).should == 6 + end end diff --git a/spec/unit/provider/confine/variable_spec.rb b/spec/unit/provider/confine/variable_spec.rb index e554ac036..eda2dd4c5 100755 --- a/spec/unit/provider/confine/variable_spec.rb +++ b/spec/unit/provider/confine/variable_spec.rb @@ -5,103 +5,103 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/provider/confine/variable' describe Puppet::Provider::Confine::Variable do - it "should be named :variable" do - Puppet::Provider::Confine::Variable.name.should == :variable + it "should be named :variable" do + Puppet::Provider::Confine::Variable.name.should == :variable + end + + it "should require a value" do + lambda { Puppet::Provider::Confine::Variable.new }.should raise_error(ArgumentError) + end + + it "should always convert values to an array" do + Puppet::Provider::Confine::Variable.new("/some/file").values.should be_instance_of(Array) + end + + it "should have an accessor for its name" do + Puppet::Provider::Confine::Variable.new(:bar).should respond_to(:name) + end + + describe "when testing values" do + before do + @confine = Puppet::Provider::Confine::Variable.new("foo") + @confine.name = :myvar end - it "should require a value" do - lambda { Puppet::Provider::Confine::Variable.new }.should raise_error(ArgumentError) + it "should use settings if the variable name is a valid setting" do + Puppet.settings.expects(:valid?).with(:myvar).returns true + Puppet.settings.expects(:value).with(:myvar).returns "foo" + @confine.valid? end - it "should always convert values to an array" do - Puppet::Provider::Confine::Variable.new("/some/file").values.should be_instance_of(Array) + it "should use Facter if the variable name is not a valid setting" do + Puppet.settings.expects(:valid?).with(:myvar).returns false + Facter.expects(:value).with(:myvar).returns "foo" + @confine.valid? end - it "should have an accessor for its name" do - Puppet::Provider::Confine::Variable.new(:bar).should respond_to(:name) + it "should be valid if the value matches the facter value" do + @confine.expects(:test_value).returns "foo" + + @confine.should be_valid + end + + it "should return false if the value does not match the facter value" do + @confine.expects(:test_value).returns "fee" + + @confine.should_not be_valid + end + + it "should be case insensitive" do + @confine.expects(:test_value).returns "FOO" + + @confine.should be_valid end - describe "when testing values" do - before do - @confine = Puppet::Provider::Confine::Variable.new("foo") - @confine.name = :myvar - end - - it "should use settings if the variable name is a valid setting" do - Puppet.settings.expects(:valid?).with(:myvar).returns true - Puppet.settings.expects(:value).with(:myvar).returns "foo" - @confine.valid? - end - - it "should use Facter if the variable name is not a valid setting" do - Puppet.settings.expects(:valid?).with(:myvar).returns false - Facter.expects(:value).with(:myvar).returns "foo" - @confine.valid? - end - - it "should be valid if the value matches the facter value" do - @confine.expects(:test_value).returns "foo" - - @confine.should be_valid - end - - it "should return false if the value does not match the facter value" do - @confine.expects(:test_value).returns "fee" - - @confine.should_not be_valid - end - - it "should be case insensitive" do - @confine.expects(:test_value).returns "FOO" - - @confine.should be_valid - end - - it "should not care whether the value is a string or symbol" do - @confine.expects(:test_value).returns "FOO" - - @confine.should be_valid - end - - it "should produce a message that the fact value is not correct" do - @confine = Puppet::Provider::Confine::Variable.new(%w{bar bee}) - @confine.name = "eh" - message = @confine.message("value") - message.should be_include("facter") - message.should be_include("bar,bee") - end - - it "should be valid if the test value matches any of the provided values" do - @confine = Puppet::Provider::Confine::Variable.new(%w{bar bee}) - @confine.expects(:test_value).returns "bee" - @confine.should be_valid - end + it "should not care whether the value is a string or symbol" do + @confine.expects(:test_value).returns "FOO" + + @confine.should be_valid + end + + it "should produce a message that the fact value is not correct" do + @confine = Puppet::Provider::Confine::Variable.new(%w{bar bee}) + @confine.name = "eh" + message = @confine.message("value") + message.should be_include("facter") + message.should be_include("bar,bee") end - describe "when summarizing multiple instances" do - it "should return a hash of failing variables and their values" do - c1 = Puppet::Provider::Confine::Variable.new("one") - c1.name = "uno" - c1.expects(:valid?).returns false - c2 = Puppet::Provider::Confine::Variable.new("two") - c2.name = "dos" - c2.expects(:valid?).returns true - c3 = Puppet::Provider::Confine::Variable.new("three") - c3.name = "tres" - c3.expects(:valid?).returns false - - Puppet::Provider::Confine::Variable.summarize([c1, c2, c3]).should == {"uno" => %w{one}, "tres" => %w{three}} - end - - it "should combine the values of multiple confines with the same fact" do - c1 = Puppet::Provider::Confine::Variable.new("one") - c1.name = "uno" - c1.expects(:valid?).returns false - c2 = Puppet::Provider::Confine::Variable.new("two") - c2.name = "uno" - c2.expects(:valid?).returns false - - Puppet::Provider::Confine::Variable.summarize([c1, c2]).should == {"uno" => %w{one two}} - end + it "should be valid if the test value matches any of the provided values" do + @confine = Puppet::Provider::Confine::Variable.new(%w{bar bee}) + @confine.expects(:test_value).returns "bee" + @confine.should be_valid + end + end + + describe "when summarizing multiple instances" do + it "should return a hash of failing variables and their values" do + c1 = Puppet::Provider::Confine::Variable.new("one") + c1.name = "uno" + c1.expects(:valid?).returns false + c2 = Puppet::Provider::Confine::Variable.new("two") + c2.name = "dos" + c2.expects(:valid?).returns true + c3 = Puppet::Provider::Confine::Variable.new("three") + c3.name = "tres" + c3.expects(:valid?).returns false + + Puppet::Provider::Confine::Variable.summarize([c1, c2, c3]).should == {"uno" => %w{one}, "tres" => %w{three}} + end + + it "should combine the values of multiple confines with the same fact" do + c1 = Puppet::Provider::Confine::Variable.new("one") + c1.name = "uno" + c1.expects(:valid?).returns false + c2 = Puppet::Provider::Confine::Variable.new("two") + c2.name = "uno" + c2.expects(:valid?).returns false + + Puppet::Provider::Confine::Variable.summarize([c1, c2]).should == {"uno" => %w{one two}} end + end end diff --git a/spec/unit/provider/confine_collection_spec.rb b/spec/unit/provider/confine_collection_spec.rb index 444281c77..e5f7f0300 100755 --- a/spec/unit/provider/confine_collection_spec.rb +++ b/spec/unit/provider/confine_collection_spec.rb @@ -5,130 +5,130 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/provider/confine_collection' describe Puppet::Provider::ConfineCollection do - it "should be able to add confines" do - Puppet::Provider::ConfineCollection.new("label").should respond_to(:confine) - end + it "should be able to add confines" do + Puppet::Provider::ConfineCollection.new("label").should respond_to(:confine) + end + + it "should require a label at initialization" do + lambda { Puppet::Provider::ConfineCollection.new }.should raise_error(ArgumentError) + end + + it "should make its label available" do + Puppet::Provider::ConfineCollection.new("mylabel").label.should == "mylabel" + end + + describe "when creating confine instances" do + it "should create an instance of the named test with the provided values" do + test_class = mock 'test_class' + test_class.expects(:new).with(%w{my values}).returns(stub('confine', :label= => nil)) + Puppet::Provider::Confine.expects(:test).with(:foo).returns test_class - it "should require a label at initialization" do - lambda { Puppet::Provider::ConfineCollection.new }.should raise_error(ArgumentError) + Puppet::Provider::ConfineCollection.new("label").confine :foo => %w{my values} end - it "should make its label available" do - Puppet::Provider::ConfineCollection.new("mylabel").label.should == "mylabel" + it "should copy its label to the confine instance" do + confine = mock 'confine' + test_class = mock 'test_class' + test_class.expects(:new).returns confine + Puppet::Provider::Confine.expects(:test).returns test_class + + confine.expects(:label=).with("label") + + Puppet::Provider::ConfineCollection.new("label").confine :foo => %w{my values} end - describe "when creating confine instances" do - it "should create an instance of the named test with the provided values" do - test_class = mock 'test_class' - test_class.expects(:new).with(%w{my values}).returns(stub('confine', :label= => nil)) - Puppet::Provider::Confine.expects(:test).with(:foo).returns test_class - - Puppet::Provider::ConfineCollection.new("label").confine :foo => %w{my values} - end - - it "should copy its label to the confine instance" do - confine = mock 'confine' - test_class = mock 'test_class' - test_class.expects(:new).returns confine - Puppet::Provider::Confine.expects(:test).returns test_class - - confine.expects(:label=).with("label") - - Puppet::Provider::ConfineCollection.new("label").confine :foo => %w{my values} - end - - describe "and the test cannot be found" do - it "should create a Facter test with the provided values and set the name to the test name" do - confine = Puppet::Provider::Confine.test(:variable).new(%w{my values}) - confine.expects(:name=).with(:foo) - confine.class.expects(:new).with(%w{my values}).returns confine - Puppet::Provider::ConfineCollection.new("label").confine(:foo => %w{my values}) - end - end - - describe "and the 'for_binary' option was provided" do - it "should mark the test as a binary confine" do - confine = Puppet::Provider::Confine.test(:exists).new(:bar) - confine.expects(:for_binary=).with true - Puppet::Provider::Confine.test(:exists).expects(:new).with(:bar).returns confine - Puppet::Provider::ConfineCollection.new("label").confine :exists => :bar, :for_binary => true - end - end + describe "and the test cannot be found" do + it "should create a Facter test with the provided values and set the name to the test name" do + confine = Puppet::Provider::Confine.test(:variable).new(%w{my values}) + confine.expects(:name=).with(:foo) + confine.class.expects(:new).with(%w{my values}).returns confine + Puppet::Provider::ConfineCollection.new("label").confine(:foo => %w{my values}) + end end - it "should be valid if no confines are present" do - Puppet::Provider::ConfineCollection.new("label").should be_valid + describe "and the 'for_binary' option was provided" do + it "should mark the test as a binary confine" do + confine = Puppet::Provider::Confine.test(:exists).new(:bar) + confine.expects(:for_binary=).with true + Puppet::Provider::Confine.test(:exists).expects(:new).with(:bar).returns confine + Puppet::Provider::ConfineCollection.new("label").confine :exists => :bar, :for_binary => true + end end + end - it "should be valid if all confines pass" do - c1 = stub 'c1', :valid? => true, :label= => nil - c2 = stub 'c2', :valid? => true, :label= => nil + it "should be valid if no confines are present" do + Puppet::Provider::ConfineCollection.new("label").should be_valid + end - Puppet::Provider::Confine.test(:true).expects(:new).returns(c1) - Puppet::Provider::Confine.test(:false).expects(:new).returns(c2) + it "should be valid if all confines pass" do + c1 = stub 'c1', :valid? => true, :label= => nil + c2 = stub 'c2', :valid? => true, :label= => nil - confiner = Puppet::Provider::ConfineCollection.new("label") - confiner.confine :true => :bar, :false => :bee + Puppet::Provider::Confine.test(:true).expects(:new).returns(c1) + Puppet::Provider::Confine.test(:false).expects(:new).returns(c2) - confiner.should be_valid - end + confiner = Puppet::Provider::ConfineCollection.new("label") + confiner.confine :true => :bar, :false => :bee - it "should not be valid if any confines fail" do - c1 = stub 'c1', :valid? => true, :label= => nil - c2 = stub 'c2', :valid? => false, :label= => nil + confiner.should be_valid + end - Puppet::Provider::Confine.test(:true).expects(:new).returns(c1) - Puppet::Provider::Confine.test(:false).expects(:new).returns(c2) + it "should not be valid if any confines fail" do + c1 = stub 'c1', :valid? => true, :label= => nil + c2 = stub 'c2', :valid? => false, :label= => nil - confiner = Puppet::Provider::ConfineCollection.new("label") - confiner.confine :true => :bar, :false => :bee + Puppet::Provider::Confine.test(:true).expects(:new).returns(c1) + Puppet::Provider::Confine.test(:false).expects(:new).returns(c2) - confiner.should_not be_valid - end + confiner = Puppet::Provider::ConfineCollection.new("label") + confiner.confine :true => :bar, :false => :bee - describe "when providing a summary" do - before do - @confiner = Puppet::Provider::ConfineCollection.new("label") - end + confiner.should_not be_valid + end - it "should return a hash" do - @confiner.summary.should be_instance_of(Hash) - end + describe "when providing a summary" do + before do + @confiner = Puppet::Provider::ConfineCollection.new("label") + end - it "should return an empty hash if the confiner is valid" do - @confiner.summary.should == {} - end + it "should return a hash" do + @confiner.summary.should be_instance_of(Hash) + end - it "should add each test type's summary to the hash" do - @confiner.confine :true => :bar, :false => :bee - Puppet::Provider::Confine.test(:true).expects(:summarize).returns :tsumm - Puppet::Provider::Confine.test(:false).expects(:summarize).returns :fsumm + it "should return an empty hash if the confiner is valid" do + @confiner.summary.should == {} + end - @confiner.summary.should == {:true => :tsumm, :false => :fsumm} - end + it "should add each test type's summary to the hash" do + @confiner.confine :true => :bar, :false => :bee + Puppet::Provider::Confine.test(:true).expects(:summarize).returns :tsumm + Puppet::Provider::Confine.test(:false).expects(:summarize).returns :fsumm - it "should not include tests that return 0" do - @confiner.confine :true => :bar, :false => :bee - Puppet::Provider::Confine.test(:true).expects(:summarize).returns 0 - Puppet::Provider::Confine.test(:false).expects(:summarize).returns :fsumm + @confiner.summary.should == {:true => :tsumm, :false => :fsumm} + end - @confiner.summary.should == {:false => :fsumm} - end + it "should not include tests that return 0" do + @confiner.confine :true => :bar, :false => :bee + Puppet::Provider::Confine.test(:true).expects(:summarize).returns 0 + Puppet::Provider::Confine.test(:false).expects(:summarize).returns :fsumm - it "should not include tests that return empty arrays" do - @confiner.confine :true => :bar, :false => :bee - Puppet::Provider::Confine.test(:true).expects(:summarize).returns [] - Puppet::Provider::Confine.test(:false).expects(:summarize).returns :fsumm + @confiner.summary.should == {:false => :fsumm} + end + + it "should not include tests that return empty arrays" do + @confiner.confine :true => :bar, :false => :bee + Puppet::Provider::Confine.test(:true).expects(:summarize).returns [] + Puppet::Provider::Confine.test(:false).expects(:summarize).returns :fsumm - @confiner.summary.should == {:false => :fsumm} - end + @confiner.summary.should == {:false => :fsumm} + end - it "should not include tests that return empty hashes" do - @confiner.confine :true => :bar, :false => :bee - Puppet::Provider::Confine.test(:true).expects(:summarize).returns({}) - Puppet::Provider::Confine.test(:false).expects(:summarize).returns :fsumm + it "should not include tests that return empty hashes" do + @confiner.confine :true => :bar, :false => :bee + Puppet::Provider::Confine.test(:true).expects(:summarize).returns({}) + Puppet::Provider::Confine.test(:false).expects(:summarize).returns :fsumm - @confiner.summary.should == {:false => :fsumm} - end + @confiner.summary.should == {:false => :fsumm} end + end end diff --git a/spec/unit/provider/confine_spec.rb b/spec/unit/provider/confine_spec.rb index 2e0637243..1fa7f3d11 100755 --- a/spec/unit/provider/confine_spec.rb +++ b/spec/unit/provider/confine_spec.rb @@ -5,74 +5,74 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/provider/confine' describe Puppet::Provider::Confine do - it "should require a value" do - lambda { Puppet::Provider::Confine.new }.should raise_error(ArgumentError) + it "should require a value" do + lambda { Puppet::Provider::Confine.new }.should raise_error(ArgumentError) + end + + it "should always convert values to an array" do + Puppet::Provider::Confine.new("/some/file").values.should be_instance_of(Array) + end + + it "should have a 'true' test" do + Puppet::Provider::Confine.test(:true).should be_instance_of(Class) + end + + it "should have a 'false' test" do + Puppet::Provider::Confine.test(:false).should be_instance_of(Class) + end + + it "should have a 'feature' test" do + Puppet::Provider::Confine.test(:feature).should be_instance_of(Class) + end + + it "should have an 'exists' test" do + Puppet::Provider::Confine.test(:exists).should be_instance_of(Class) + end + + it "should have a 'variable' test" do + Puppet::Provider::Confine.test(:variable).should be_instance_of(Class) + end + + describe "when testing all values" do + before do + @confine = Puppet::Provider::Confine.new(%w{a b c}) + @confine.label = "foo" end - it "should always convert values to an array" do - Puppet::Provider::Confine.new("/some/file").values.should be_instance_of(Array) + it "should be invalid if any values fail" do + @confine.stubs(:pass?).returns true + @confine.expects(:pass?).with("b").returns false + @confine.should_not be_valid end - it "should have a 'true' test" do - Puppet::Provider::Confine.test(:true).should be_instance_of(Class) + it "should be valid if all values pass" do + @confine.stubs(:pass?).returns true + @confine.should be_valid end - it "should have a 'false' test" do - Puppet::Provider::Confine.test(:false).should be_instance_of(Class) + it "should short-cut at the first failing value" do + @confine.expects(:pass?).once.returns false + @confine.valid? end - it "should have a 'feature' test" do - Puppet::Provider::Confine.test(:feature).should be_instance_of(Class) + it "should log failing confines with the label and message" do + @confine.stubs(:pass?).returns false + @confine.expects(:message).returns "My message" + @confine.expects(:label).returns "Mylabel" + Puppet.expects(:debug).with("Mylabel: My message") + @confine.valid? end + end - it "should have an 'exists' test" do - Puppet::Provider::Confine.test(:exists).should be_instance_of(Class) - end - - it "should have a 'variable' test" do - Puppet::Provider::Confine.test(:variable).should be_instance_of(Class) - end - - describe "when testing all values" do - before do - @confine = Puppet::Provider::Confine.new(%w{a b c}) - @confine.label = "foo" - end - - it "should be invalid if any values fail" do - @confine.stubs(:pass?).returns true - @confine.expects(:pass?).with("b").returns false - @confine.should_not be_valid - end - - it "should be valid if all values pass" do - @confine.stubs(:pass?).returns true - @confine.should be_valid - end - - it "should short-cut at the first failing value" do - @confine.expects(:pass?).once.returns false - @confine.valid? - end - - it "should log failing confines with the label and message" do - @confine.stubs(:pass?).returns false - @confine.expects(:message).returns "My message" - @confine.expects(:label).returns "Mylabel" - Puppet.expects(:debug).with("Mylabel: My message") - @confine.valid? - end - end - - describe "when testing the result of the values" do - before { @confine = Puppet::Provider::Confine.new(%w{a b c d}) } + describe "when testing the result of the values" do + before { @confine = Puppet::Provider::Confine.new(%w{a b c d}) } - it "should return an array with the result of the test for each value" do - @confine.stubs(:pass?).returns true - @confine.expects(:pass?).with("b").returns false - @confine.expects(:pass?).with("d").returns false + it "should return an array with the result of the test for each value" do + @confine.stubs(:pass?).returns true + @confine.expects(:pass?).with("b").returns false + @confine.expects(:pass?).with("d").returns false - @confine.result.should == [true, false, true, false] - end + @confine.result.should == [true, false, true, false] end + end end diff --git a/spec/unit/provider/confiner_spec.rb b/spec/unit/provider/confiner_spec.rb index 0a0d67fb5..d513c7232 100755 --- a/spec/unit/provider/confiner_spec.rb +++ b/spec/unit/provider/confiner_spec.rb @@ -5,59 +5,59 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/provider/confiner' describe Puppet::Provider::Confiner do + before do + @object = Object.new + @object.extend(Puppet::Provider::Confiner) + end + + it "should have a method for defining confines" do + @object.should respond_to(:confine) + end + + it "should have a method for returning its confine collection" do + @object.should respond_to(:confine_collection) + end + + it "should have a method for testing suitability" do + @object.should respond_to(:suitable?) + end + + it "should delegate its confine method to its confine collection" do + coll = mock 'collection' + @object.stubs(:confine_collection).returns coll + coll.expects(:confine).with(:foo => :bar, :bee => :baz) + @object.confine(:foo => :bar, :bee => :baz) + end + + it "should create a new confine collection if one does not exist" do + Puppet::Provider::ConfineCollection.expects(:new).with("mylabel").returns "mycoll" + @object.expects(:to_s).returns "mylabel" + @object.confine_collection.should == "mycoll" + end + + it "should reuse the confine collection" do + @object.confine_collection.should equal(@object.confine_collection) + end + + describe "when testing suitability" do before do - @object = Object.new - @object.extend(Puppet::Provider::Confiner) + @coll = mock 'collection' + @object.stubs(:confine_collection).returns @coll end - it "should have a method for defining confines" do - @object.should respond_to(:confine) + it "should return true if the confine collection is valid" do + @coll.expects(:valid?).returns true + @object.should be_suitable end - it "should have a method for returning its confine collection" do - @object.should respond_to(:confine_collection) + it "should return false if the confine collection is invalid" do + @coll.expects(:valid?).returns false + @object.should_not be_suitable end - it "should have a method for testing suitability" do - @object.should respond_to(:suitable?) - end - - it "should delegate its confine method to its confine collection" do - coll = mock 'collection' - @object.stubs(:confine_collection).returns coll - coll.expects(:confine).with(:foo => :bar, :bee => :baz) - @object.confine(:foo => :bar, :bee => :baz) - end - - it "should create a new confine collection if one does not exist" do - Puppet::Provider::ConfineCollection.expects(:new).with("mylabel").returns "mycoll" - @object.expects(:to_s).returns "mylabel" - @object.confine_collection.should == "mycoll" - end - - it "should reuse the confine collection" do - @object.confine_collection.should equal(@object.confine_collection) - end - - describe "when testing suitability" do - before do - @coll = mock 'collection' - @object.stubs(:confine_collection).returns @coll - end - - it "should return true if the confine collection is valid" do - @coll.expects(:valid?).returns true - @object.should be_suitable - end - - it "should return false if the confine collection is invalid" do - @coll.expects(:valid?).returns false - @object.should_not be_suitable - end - - it "should return the summary of the confine collection if a long result is asked for" do - @coll.expects(:summary).returns "myresult" - @object.suitable?(false).should == "myresult" - end + it "should return the summary of the confine collection if a long result is asked for" do + @coll.expects(:summary).returns "myresult" + @object.suitable?(false).should == "myresult" end + end end diff --git a/spec/unit/provider/group/groupadd_spec.rb b/spec/unit/provider/group/groupadd_spec.rb index fb4b811cb..33d9acd98 100755 --- a/spec/unit/provider/group/groupadd_spec.rb +++ b/spec/unit/provider/group/groupadd_spec.rb @@ -5,27 +5,27 @@ require File.dirname(__FILE__) + '/../../../spec_helper' provider_class = Puppet::Type.type(:group).provider(:groupadd) describe provider_class do - before do - @resource = stub("resource", :name => "mygroup") - @provider = provider_class.new(@resource) - end + before do + @resource = stub("resource", :name => "mygroup") + @provider = provider_class.new(@resource) + end - # #1360 - it "should add -o when allowdupe is enabled and the group 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") } + # #1360 + it "should add -o when allowdupe is enabled and the group 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 + @provider.create + end - it "should add -o when allowdupe is enabled and the gid 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") } + it "should add -o when allowdupe is enabled and the gid 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.gid = 150 - end + @provider.gid = 150 + end end diff --git a/spec/unit/provider/group/ldap_spec.rb b/spec/unit/provider/group/ldap_spec.rb index ab2bd72aa..b8c54be94 100755 --- a/spec/unit/provider/group/ldap_spec.rb +++ b/spec/unit/provider/group/ldap_spec.rb @@ -8,98 +8,98 @@ require File.dirname(__FILE__) + '/../../../spec_helper' provider_class = Puppet::Type.type(:group).provider(:ldap) describe provider_class do - it "should have the Ldap provider class as its baseclass" do - provider_class.superclass.should equal(Puppet::Provider::Ldap) + it "should have the Ldap provider class as its baseclass" do + provider_class.superclass.should equal(Puppet::Provider::Ldap) + end + + it "should manage :posixGroup objectclass" do + provider_class.manager.objectclasses.should == [:posixGroup] + end + + it "should use 'ou=Groups' as its relative base" do + provider_class.manager.location.should == "ou=Groups" + end + + it "should use :cn as its rdn" do + provider_class.manager.rdn.should == :cn + end + + it "should map :name to 'cn'" do + provider_class.manager.ldap_name(:name).should == 'cn' + end + + it "should map :gid to 'gidNumber'" do + provider_class.manager.ldap_name(:gid).should == 'gidNumber' + end + + it "should map :members to 'memberUid', to be used by the user ldap provider" do + provider_class.manager.ldap_name(:members).should == 'memberUid' + end + + describe "when being created" do + before do + # So we don't try to actually talk to ldap + @connection = mock 'connection' + provider_class.manager.stubs(:connect).yields @connection end - it "should manage :posixGroup objectclass" do - provider_class.manager.objectclasses.should == [:posixGroup] - end + describe "with no gid specified" do + it "should pick the first available GID after the largest existing GID" do + low = {:name=>["luke"], :gid=>["600"]} + high = {:name=>["testing"], :gid=>["640"]} + provider_class.manager.expects(:search).returns([low, high]) - it "should use 'ou=Groups' as its relative base" do - provider_class.manager.location.should == "ou=Groups" - end + resource = stub 'resource', :should => %w{whatever} + resource.stubs(:should).with(:gid).returns nil + resource.stubs(:should).with(:ensure).returns :present + instance = provider_class.new(:name => "luke", :ensure => :absent) + instance.stubs(:resource).returns resource - it "should use :cn as its rdn" do - provider_class.manager.rdn.should == :cn - end + @connection.expects(:add).with { |dn, attrs| attrs["gidNumber"] == ["641"] } - it "should map :name to 'cn'" do - provider_class.manager.ldap_name(:name).should == 'cn' - end + instance.create + instance.flush + end - it "should map :gid to 'gidNumber'" do - provider_class.manager.ldap_name(:gid).should == 'gidNumber' - end + it "should pick '501' as its GID if no groups are found" do + provider_class.manager.expects(:search).returns nil + + resource = stub 'resource', :should => %w{whatever} + resource.stubs(:should).with(:gid).returns nil + resource.stubs(:should).with(:ensure).returns :present + instance = provider_class.new(:name => "luke", :ensure => :absent) + instance.stubs(:resource).returns resource - it "should map :members to 'memberUid', to be used by the user ldap provider" do - provider_class.manager.ldap_name(:members).should == 'memberUid' + @connection.expects(:add).with { |dn, attrs| attrs["gidNumber"] == ["501"] } + + instance.create + instance.flush + end end + end + + it "should have a method for converting group names to GIDs" do + provider_class.should respond_to(:name2id) + end - describe "when being created" do - before do - # So we don't try to actually talk to ldap - @connection = mock 'connection' - provider_class.manager.stubs(:connect).yields @connection - end - - describe "with no gid specified" do - it "should pick the first available GID after the largest existing GID" do - low = {:name=>["luke"], :gid=>["600"]} - high = {:name=>["testing"], :gid=>["640"]} - provider_class.manager.expects(:search).returns([low, high]) - - resource = stub 'resource', :should => %w{whatever} - resource.stubs(:should).with(:gid).returns nil - resource.stubs(:should).with(:ensure).returns :present - instance = provider_class.new(:name => "luke", :ensure => :absent) - instance.stubs(:resource).returns resource - - @connection.expects(:add).with { |dn, attrs| attrs["gidNumber"] == ["641"] } - - instance.create - instance.flush - end - - it "should pick '501' as its GID if no groups are found" do - provider_class.manager.expects(:search).returns nil - - resource = stub 'resource', :should => %w{whatever} - resource.stubs(:should).with(:gid).returns nil - resource.stubs(:should).with(:ensure).returns :present - instance = provider_class.new(:name => "luke", :ensure => :absent) - instance.stubs(:resource).returns resource - - @connection.expects(:add).with { |dn, attrs| attrs["gidNumber"] == ["501"] } - - instance.create - instance.flush - end - end + describe "when converting from a group name to GID" do + it "should use the ldap manager to look up the GID" do + provider_class.manager.expects(:search).with("cn=foo") + provider_class.name2id("foo") end - it "should have a method for converting group names to GIDs" do - provider_class.should respond_to(:name2id) + it "should return nil if no group is found" do + provider_class.manager.expects(:search).with("cn=foo").returns nil + provider_class.name2id("foo").should be_nil + provider_class.manager.expects(:search).with("cn=bar").returns [] + provider_class.name2id("bar").should be_nil end - describe "when converting from a group name to GID" do - it "should use the ldap manager to look up the GID" do - provider_class.manager.expects(:search).with("cn=foo") - provider_class.name2id("foo") - end - - it "should return nil if no group is found" do - provider_class.manager.expects(:search).with("cn=foo").returns nil - provider_class.name2id("foo").should be_nil - provider_class.manager.expects(:search).with("cn=bar").returns [] - provider_class.name2id("bar").should be_nil - end - - # We shouldn't ever actually have more than one gid, but it doesn't hurt - # to test for the possibility. - it "should return the first gid from the first returned group" do - provider_class.manager.expects(:search).with("cn=foo").returns [{:name => "foo", :gid => [10, 11]}, {:name => :bar, :gid => [20, 21]}] - provider_class.name2id("foo").should == 10 - end + # We shouldn't ever actually have more than one gid, but it doesn't hurt + # to test for the possibility. + it "should return the first gid from the first returned group" do + provider_class.manager.expects(:search).with("cn=foo").returns [{:name => "foo", :gid => [10, 11]}, {:name => :bar, :gid => [20, 21]}] + provider_class.name2id("foo").should == 10 end + end end diff --git a/spec/unit/provider/ldap_spec.rb b/spec/unit/provider/ldap_spec.rb index 6c5820883..588cafc1f 100755 --- a/spec/unit/provider/ldap_spec.rb +++ b/spec/unit/provider/ldap_spec.rb @@ -8,241 +8,241 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/provider/ldap' describe Puppet::Provider::Ldap do - before do - @class = Class.new(Puppet::Provider::Ldap) + before do + @class = Class.new(Puppet::Provider::Ldap) + end + + it "should be able to define its manager" do + manager = mock 'manager' + Puppet::Util::Ldap::Manager.expects(:new).returns manager + @class.stubs :mk_resource_methods + manager.expects(:manages).with(:one) + @class.manages(:one).should equal(manager) + @class.manager.should equal(manager) + end + + it "should be able to prefetch instances from ldap" do + @class.should respond_to(:prefetch) + end + + it "should create its resource getter/setter methods when the manager is defined" do + manager = mock 'manager' + Puppet::Util::Ldap::Manager.expects(:new).returns manager + @class.expects :mk_resource_methods + manager.stubs(:manages) + @class.manages(:one).should equal(manager) + end + + it "should have an instances method" do + @class.should respond_to(:instances) + end + + describe "when providing a list of instances" do + it "should convert all results returned from the manager's :search method into provider instances" do + manager = mock 'manager' + @class.stubs(:manager).returns manager + + manager.expects(:search).returns %w{one two three} + + @class.expects(:new).with("one").returns(1) + @class.expects(:new).with("two").returns(2) + @class.expects(:new).with("three").returns(3) + + @class.instances.should == [1,2,3] end + end - it "should be able to define its manager" do - manager = mock 'manager' - Puppet::Util::Ldap::Manager.expects(:new).returns manager - @class.stubs :mk_resource_methods - manager.expects(:manages).with(:one) - @class.manages(:one).should equal(manager) - @class.manager.should equal(manager) - end + it "should have a prefetch method" do + @class.should respond_to(:prefetch) + end - it "should be able to prefetch instances from ldap" do - @class.should respond_to(:prefetch) - end + describe "when prefetching" do + before do + @manager = mock 'manager' + @class.stubs(:manager).returns @manager - it "should create its resource getter/setter methods when the manager is defined" do - manager = mock 'manager' - Puppet::Util::Ldap::Manager.expects(:new).returns manager - @class.expects :mk_resource_methods - manager.stubs(:manages) - @class.manages(:one).should equal(manager) - end + @resource = mock 'resource' - it "should have an instances method" do - @class.should respond_to(:instances) + @resources = {"one" => @resource} end - describe "when providing a list of instances" do - it "should convert all results returned from the manager's :search method into provider instances" do - manager = mock 'manager' - @class.stubs(:manager).returns manager - - manager.expects(:search).returns %w{one two three} + it "should find an entry for each passed resource" do + @manager.expects(:find).with("one").returns nil - @class.expects(:new).with("one").returns(1) - @class.expects(:new).with("two").returns(2) - @class.expects(:new).with("three").returns(3) - - @class.instances.should == [1,2,3] - end - end - - it "should have a prefetch method" do - @class.should respond_to(:prefetch) + @class.stubs(:new) + @resource.stubs(:provider=) + @class.prefetch(@resources) end - describe "when prefetching" do - before do - @manager = mock 'manager' - @class.stubs(:manager).returns @manager + describe "resources that do not exist" do + it "should create a provider with :ensure => :absent" do + result = mock 'result' + @manager.expects(:find).with("one").returns nil - @resource = mock 'resource' + @class.expects(:new).with(:ensure => :absent).returns "myprovider" - @resources = {"one" => @resource} - end + @resource.expects(:provider=).with("myprovider") - it "should find an entry for each passed resource" do - @manager.expects(:find).with("one").returns nil + @class.prefetch(@resources) + end + end - @class.stubs(:new) - @resource.stubs(:provider=) - @class.prefetch(@resources) - end + describe "resources that exist" do + it "should create a provider with the results of the find" do + @manager.expects(:find).with("one").returns("one" => "two") - describe "resources that do not exist" do - it "should create a provider with :ensure => :absent" do - result = mock 'result' - @manager.expects(:find).with("one").returns nil + @class.expects(:new).with("one" => "two", :ensure => :present).returns "myprovider" - @class.expects(:new).with(:ensure => :absent).returns "myprovider" + @resource.expects(:provider=).with("myprovider") - @resource.expects(:provider=).with("myprovider") + @class.prefetch(@resources) + end - @class.prefetch(@resources) - end - end + it "should set :ensure to :present in the returned values" do + @manager.expects(:find).with("one").returns("one" => "two") - describe "resources that exist" do - it "should create a provider with the results of the find" do - @manager.expects(:find).with("one").returns("one" => "two") + @class.expects(:new).with("one" => "two", :ensure => :present).returns "myprovider" - @class.expects(:new).with("one" => "two", :ensure => :present).returns "myprovider" + @resource.expects(:provider=).with("myprovider") - @resource.expects(:provider=).with("myprovider") + @class.prefetch(@resources) + end + end + end - @class.prefetch(@resources) - end + describe "when being initialized" do + it "should fail if no manager has been defined" do + lambda { @class.new }.should raise_error(Puppet::DevError) + end - it "should set :ensure to :present in the returned values" do - @manager.expects(:find).with("one").returns("one" => "two") + it "should fail if the manager is invalid" do + manager = stub "manager", :valid? => false + @class.stubs(:manager).returns manager + lambda { @class.new }.should raise_error(Puppet::DevError) + end - @class.expects(:new).with("one" => "two", :ensure => :present).returns "myprovider" + describe "with a hash" do + before do + @manager = stub "manager", :valid? => true + @class.stubs(:manager).returns @manager + + @resource_class = mock 'resource_class' + @class.stubs(:resource_type).returns @resource_class + + @property_class = stub 'property_class', :array_matching => :all, :superclass => Puppet::Property + @resource_class.stubs(:attrclass).with(:one).returns(@property_class) + @resource_class.stubs(:valid_parameter?).returns true + end + + it "should store a copy of the hash as its ldap_properties" do + instance = @class.new(:one => :two) + instance.ldap_properties.should == {:one => :two} + end + + it "should only store the first value of each value array for those attributes that do not match all values" do + @property_class.expects(:array_matching).returns :first + instance = @class.new(:one => %w{two three}) + instance.properties.should == {:one => "two"} + end + + it "should store the whole value array for those attributes that match all values" do + @property_class.expects(:array_matching).returns :all + instance = @class.new(:one => %w{two three}) + instance.properties.should == {:one => %w{two three}} + end + + it "should only use the first value for attributes that are not properties" do + # Yay. hackish, but easier than mocking everything. + @resource_class.expects(:attrclass).with(:a).returns Puppet::Type.type(:user).attrclass(:name) + @property_class.stubs(:array_matching).returns :all + + instance = @class.new(:one => %w{two three}, :a => %w{b c}) + instance.properties.should == {:one => %w{two three}, :a => "b"} + end + + it "should discard any properties not valid in the resource class" do + @resource_class.expects(:valid_parameter?).with(:a).returns false + @property_class.stubs(:array_matching).returns :all + + instance = @class.new(:one => %w{two three}, :a => %w{b}) + instance.properties.should == {:one => %w{two three}} + end + end + end - @resource.expects(:provider=).with("myprovider") + describe "when an instance" do + before do + @manager = stub "manager", :valid? => true + @class.stubs(:manager).returns @manager + @instance = @class.new - @class.prefetch(@resources) - end - end + @property_class = stub 'property_class', :array_matching => :all, :superclass => Puppet::Property + @resource_class = stub 'resource_class', :attrclass => @property_class, :valid_parameter? => true, :validproperties => [:one, :two] + @class.stubs(:resource_type).returns @resource_class end - describe "when being initialized" do - it "should fail if no manager has been defined" do - lambda { @class.new }.should raise_error(Puppet::DevError) - end + it "should have a method for creating the ldap entry" do + @instance.should respond_to(:create) + end - it "should fail if the manager is invalid" do - manager = stub "manager", :valid? => false - @class.stubs(:manager).returns manager - lambda { @class.new }.should raise_error(Puppet::DevError) - end + it "should have a method for removing the ldap entry" do + @instance.should respond_to(:delete) + end - describe "with a hash" do - before do - @manager = stub "manager", :valid? => true - @class.stubs(:manager).returns @manager + it "should have a method for returning the class's manager" do + @instance.manager.should equal(@manager) + end - @resource_class = mock 'resource_class' - @class.stubs(:resource_type).returns @resource_class + it "should indicate when the ldap entry already exists" do + @instance = @class.new(:ensure => :present) + @instance.exists?.should be_true + end - @property_class = stub 'property_class', :array_matching => :all, :superclass => Puppet::Property - @resource_class.stubs(:attrclass).with(:one).returns(@property_class) - @resource_class.stubs(:valid_parameter?).returns true - end + it "should indicate when the ldap entry does not exist" do + @instance = @class.new(:ensure => :absent) + @instance.exists?.should be_false + end - it "should store a copy of the hash as its ldap_properties" do - instance = @class.new(:one => :two) - instance.ldap_properties.should == {:one => :two} - end + describe "is being flushed" do + it "should call the manager's :update method with its name, current attributes, and desired attributes" do + @instance.stubs(:name).returns "myname" + @instance.stubs(:ldap_properties).returns(:one => :two) + @instance.stubs(:properties).returns(:three => :four) + @manager.expects(:update).with(@instance.name, {:one => :two}, {:three => :four}) + @instance.flush + end + end - it "should only store the first value of each value array for those attributes that do not match all values" do - @property_class.expects(:array_matching).returns :first - instance = @class.new(:one => %w{two three}) - instance.properties.should == {:one => "two"} - end - - it "should store the whole value array for those attributes that match all values" do - @property_class.expects(:array_matching).returns :all - instance = @class.new(:one => %w{two three}) - instance.properties.should == {:one => %w{two three}} - end + describe "is being created" do + before do + @rclass = mock 'resource_class' + @rclass.stubs(:validproperties).returns([:one, :two]) + @resource = mock 'resource' + @resource.stubs(:class).returns @rclass + @resource.stubs(:should).returns nil + @instance.stubs(:resource).returns @resource + end + + it "should set its :ensure value to :present" do + @instance.create + @instance.properties[:ensure].should == :present + end + + it "should set all of the other attributes from the resource" do + @resource.expects(:should).with(:one).returns "oneval" + @resource.expects(:should).with(:two).returns "twoval" + + @instance.create + @instance.properties[:one].should == "oneval" + @instance.properties[:two].should == "twoval" + end + end - it "should only use the first value for attributes that are not properties" do - # Yay. hackish, but easier than mocking everything. - @resource_class.expects(:attrclass).with(:a).returns Puppet::Type.type(:user).attrclass(:name) - @property_class.stubs(:array_matching).returns :all - - instance = @class.new(:one => %w{two three}, :a => %w{b c}) - instance.properties.should == {:one => %w{two three}, :a => "b"} - end - - it "should discard any properties not valid in the resource class" do - @resource_class.expects(:valid_parameter?).with(:a).returns false - @property_class.stubs(:array_matching).returns :all - - instance = @class.new(:one => %w{two three}, :a => %w{b}) - instance.properties.should == {:one => %w{two three}} - end - end - end - - describe "when an instance" do - before do - @manager = stub "manager", :valid? => true - @class.stubs(:manager).returns @manager - @instance = @class.new - - @property_class = stub 'property_class', :array_matching => :all, :superclass => Puppet::Property - @resource_class = stub 'resource_class', :attrclass => @property_class, :valid_parameter? => true, :validproperties => [:one, :two] - @class.stubs(:resource_type).returns @resource_class - end - - it "should have a method for creating the ldap entry" do - @instance.should respond_to(:create) - end - - it "should have a method for removing the ldap entry" do - @instance.should respond_to(:delete) - end - - it "should have a method for returning the class's manager" do - @instance.manager.should equal(@manager) - end - - it "should indicate when the ldap entry already exists" do - @instance = @class.new(:ensure => :present) - @instance.exists?.should be_true - end - - it "should indicate when the ldap entry does not exist" do - @instance = @class.new(:ensure => :absent) - @instance.exists?.should be_false - end - - describe "is being flushed" do - it "should call the manager's :update method with its name, current attributes, and desired attributes" do - @instance.stubs(:name).returns "myname" - @instance.stubs(:ldap_properties).returns(:one => :two) - @instance.stubs(:properties).returns(:three => :four) - @manager.expects(:update).with(@instance.name, {:one => :two}, {:three => :four}) - @instance.flush - end - end - - describe "is being created" do - before do - @rclass = mock 'resource_class' - @rclass.stubs(:validproperties).returns([:one, :two]) - @resource = mock 'resource' - @resource.stubs(:class).returns @rclass - @resource.stubs(:should).returns nil - @instance.stubs(:resource).returns @resource - end - - it "should set its :ensure value to :present" do - @instance.create - @instance.properties[:ensure].should == :present - end - - it "should set all of the other attributes from the resource" do - @resource.expects(:should).with(:one).returns "oneval" - @resource.expects(:should).with(:two).returns "twoval" - - @instance.create - @instance.properties[:one].should == "oneval" - @instance.properties[:two].should == "twoval" - end - end - - describe "is being deleted" do - it "should set its :ensure value to :absent" do - @instance.delete - @instance.properties[:ensure].should == :absent - end - end + describe "is being deleted" do + it "should set its :ensure value to :absent" do + @instance.delete + @instance.properties[:ensure].should == :absent + end end + end end diff --git a/spec/unit/provider/macauthorization_spec.rb b/spec/unit/provider/macauthorization_spec.rb index b462be140..d9e04e957 100755 --- a/spec/unit/provider/macauthorization_spec.rb +++ b/spec/unit/provider/macauthorization_spec.rb @@ -12,136 +12,136 @@ provider_class = Puppet::Type.type(:macauthorization).provider(:macauthorization describe provider_class do - before :each do - # Create a mock resource - @resource = stub 'resource' + before :each do + # Create a mock resource + @resource = stub 'resource' - @authname = "foo.spam.eggs.puppettest" - @authplist = {} + @authname = "foo.spam.eggs.puppettest" + @authplist = {} - @rules = {@authname => @authplist} + @rules = {@authname => @authplist} - authdb = {} - authdb["rules"] = { "foorule" => "foo" } - authdb["rights"] = { "fooright" => "foo" } + authdb = {} + authdb["rules"] = { "foorule" => "foo" } + authdb["rights"] = { "fooright" => "foo" } - # Stub out Plist::parse_xml - Plist.stubs(:parse_xml).returns(authdb) + # Stub out Plist::parse_xml + Plist.stubs(:parse_xml).returns(authdb) - # A catch all; no parameters set - @resource.stubs(:[]).returns(nil) + # A catch all; no parameters set + @resource.stubs(:[]).returns(nil) - # But set name, ensure - @resource.stubs(:[]).with(:name).returns @authname - @resource.stubs(:[]).with(:ensure).returns :present - @resource.stubs(:ref).returns "MacAuthorization[#{@authname}]" + # But set name, ensure + @resource.stubs(:[]).with(:name).returns @authname + @resource.stubs(:[]).with(:ensure).returns :present + @resource.stubs(:ref).returns "MacAuthorization[#{@authname}]" - @provider = provider_class.new(@resource) - end + @provider = provider_class.new(@resource) + end + + it "should have a create method" do + @provider.should respond_to(:create) + end + + it "should have a destroy method" do + @provider.should respond_to(:destroy) + end + + it "should have an exists? method" do + @provider.should respond_to(:exists?) + end - it "should have a create method" do - @provider.should respond_to(:create) + it "should have a flush method" do + @provider.should respond_to(:flush) + end + + properties = [ :allow_root, :authenticate_user, :auth_class, :comment, + :group, :k_of_n, :mechanisms, :rule, :session_owner, + :shared, :timeout, :tries, :auth_type ] + + properties.each do |prop| + it "should have a #{prop.to_s} method" do + @provider.should respond_to(prop.to_s) end - it "should have a destroy method" do - @provider.should respond_to(:destroy) + it "should have a #{prop.to_s}= method" do + @provider.should respond_to(prop.to_s + "=") end + end - it "should have an exists? method" do - @provider.should respond_to(:exists?) + describe "when destroying a right" do + before :each do + @resource.stubs(:[]).with(:auth_type).returns(:right) end - it "should have a flush method" do - @provider.should respond_to(:flush) + it "should call the internal method destroy_right" do + @provider.expects(:destroy_right) + @provider.destroy + end + it "should call the external command 'security authorizationdb remove @authname" do + @provider.expects(:security).with("authorizationdb", :remove, @authname) + @provider.destroy end + end - properties = [ :allow_root, :authenticate_user, :auth_class, :comment, - :group, :k_of_n, :mechanisms, :rule, :session_owner, - :shared, :timeout, :tries, :auth_type ] + describe "when destroying a rule" do + before :each do + @resource.stubs(:[]).with(:auth_type).returns(:rule) + end - properties.each do |prop| - it "should have a #{prop.to_s} method" do - @provider.should respond_to(prop.to_s) - end + it "should call the internal method destroy_rule" do + @provider.expects(:destroy_rule) + @provider.destroy + end + end - it "should have a #{prop.to_s}= method" do - @provider.should respond_to(prop.to_s + "=") - end + describe "when flushing a right" do + before :each do + @resource.stubs(:[]).with(:auth_type).returns(:right) end - describe "when destroying a right" do - before :each do - @resource.stubs(:[]).with(:auth_type).returns(:right) - end - - it "should call the internal method destroy_right" do - @provider.expects(:destroy_right) - @provider.destroy - end - it "should call the external command 'security authorizationdb remove @authname" do - @provider.expects(:security).with("authorizationdb", :remove, @authname) - @provider.destroy - end + it "should call the internal method flush_right" do + @provider.expects(:flush_right) + @provider.flush end - describe "when destroying a rule" do - before :each do - @resource.stubs(:[]).with(:auth_type).returns(:rule) - end + it "should call the internal method set_right" do + @provider.expects(:set_right) + @provider.flush + end - it "should call the internal method destroy_rule" do - @provider.expects(:destroy_rule) - @provider.destroy - end + it "should read and write to the auth database with the right arguments" do + @provider.expects(:execute).with { |cmds, args| + cmds.include?("read") and + cmds.include?(@authname) and + args[:combine] == false + }.once + + @provider.expects(:execute).with { |cmds, args| + cmds.include?("write") and + cmds.include?(@authname) and + args[:combine] == false and + args[:stdinfile] != nil + }.once + @provider.flush end - describe "when flushing a right" do - before :each do - @resource.stubs(:[]).with(:auth_type).returns(:right) - end - - it "should call the internal method flush_right" do - @provider.expects(:flush_right) - @provider.flush - end - - it "should call the internal method set_right" do - @provider.expects(:set_right) - @provider.flush - end - - it "should read and write to the auth database with the right arguments" do - @provider.expects(:execute).with { |cmds, args| - cmds.include?("read") and - cmds.include?(@authname) and - args[:combine] == false - }.once - - @provider.expects(:execute).with { |cmds, args| - cmds.include?("write") and - cmds.include?(@authname) and - args[:combine] == false and - args[:stdinfile] != nil - }.once - @provider.flush - end + end + describe "when flushing a rule" do + before :each do + @resource.stubs(:[]).with(:auth_type).returns(:rule) end - describe "when flushing a rule" do - before :each do - @resource.stubs(:[]).with(:auth_type).returns(:rule) - end - - it "should call the internal method flush_rule" do - @provider.expects(:flush_rule) - @provider.flush - end + it "should call the internal method flush_rule" do + @provider.expects(:flush_rule) + @provider.flush + end - it "should call the internal method set_rule" do - @provider.expects(:set_rule) - @provider.flush - end + it "should call the internal method set_rule" do + @provider.expects(:set_rule) + @provider.flush end + end end diff --git a/spec/unit/provider/mcx/mcxcontent_spec.rb b/spec/unit/provider/mcx/mcxcontent_spec.rb index d8f431afe..d3fb043b6 100755 --- a/spec/unit/provider/mcx/mcxcontent_spec.rb +++ b/spec/unit/provider/mcx/mcxcontent_spec.rb @@ -25,151 +25,151 @@ provider_class = Puppet::Type.type(:mcx).provider(:mcxcontent) # describe creates a new ExampleGroup object. describe provider_class do - # :each executes before each test. - # :all executes once for the test group and before :each. + # :each executes before each test. + # :all executes once for the test group and before :each. + before :each do + # Create a mock resource + @resource = stub 'resource' + + @provider = provider_class.new + @attached_to = "/Users/foobar" + @ds_path = "/Local/Default/Users/foobar" + + # A catch all; no parameters set + @resource.stubs(:[]).returns(nil) + + # But set name, ensure and enable + @resource.stubs(:[]).with(:name).returns @attached_to + @resource.stubs(:[]).with(:ensure).returns :present + @resource.stubs(:ref).returns "Mcx[#{@attached_to}]" + + # stub out the provider methods that actually touch the filesystem + # or execute commands + @provider.class.stubs(:execute).returns('') + @provider.stubs(:execute).returns('') + @provider.stubs(:resource).returns @resource + end + + it "should have a create method." do + @provider.should respond_to(:create) + end + + it "should have a destroy method." do + @provider.should respond_to(:destroy) + end + + it "should have an exists? method." do + @provider.should respond_to(:exists?) + end + + it "should have an content method." do + @provider.should respond_to(:content) + end + + it "should have an content= method." do + @provider.should respond_to(:content=) + end + + describe "when managing the resource" do + it "should execute external command dscl from :create" do + @provider.class.expects(:dscl).returns('').once + @provider.create + end + it "should execute external command dscl from :destroy" do + @provider.class.expects(:dscl).with('localhost', '-mcxdelete', @ds_path).returns('').once + @provider.destroy + end + it "should execute external command dscl from :exists?" do + @provider.class.expects(:dscl).with('localhost', '-mcxexport', @ds_path).returns('').once + @provider.exists? + end + it "should execute external command dscl from :content" do + @provider.class.expects(:dscl).with('localhost', '-mcxexport', @ds_path).returns('') + @provider.content + end + it "should execute external command dscl from :content=" do + @provider.class.expects(:dscl).returns('') + @provider.content='' + end + end + + describe "when creating and parsing the name for ds_type" do before :each do - # Create a mock resource - @resource = stub 'resource' - - @provider = provider_class.new - @attached_to = "/Users/foobar" - @ds_path = "/Local/Default/Users/foobar" - - # A catch all; no parameters set - @resource.stubs(:[]).returns(nil) - - # But set name, ensure and enable - @resource.stubs(:[]).with(:name).returns @attached_to - @resource.stubs(:[]).with(:ensure).returns :present - @resource.stubs(:ref).returns "Mcx[#{@attached_to}]" - - # stub out the provider methods that actually touch the filesystem - # or execute commands - @provider.class.stubs(:execute).returns('') - @provider.stubs(:execute).returns('') - @provider.stubs(:resource).returns @resource - end - - it "should have a create method." do - @provider.should respond_to(:create) - end - - it "should have a destroy method." do - @provider.should respond_to(:destroy) - end - - it "should have an exists? method." do - @provider.should respond_to(:exists?) - end - - it "should have an content method." do - @provider.should respond_to(:content) - end - - it "should have an content= method." do - @provider.should respond_to(:content=) - end - - describe "when managing the resource" do - it "should execute external command dscl from :create" do - @provider.class.expects(:dscl).returns('').once - @provider.create - end - it "should execute external command dscl from :destroy" do - @provider.class.expects(:dscl).with('localhost', '-mcxdelete', @ds_path).returns('').once - @provider.destroy - end - it "should execute external command dscl from :exists?" do - @provider.class.expects(:dscl).with('localhost', '-mcxexport', @ds_path).returns('').once - @provider.exists? - end - it "should execute external command dscl from :content" do - @provider.class.expects(:dscl).with('localhost', '-mcxexport', @ds_path).returns('') - @provider.content - end - it "should execute external command dscl from :content=" do - @provider.class.expects(:dscl).returns('') - @provider.content='' - end - end - - describe "when creating and parsing the name for ds_type" do - before :each do - @resource.stubs(:[]).with(:name).returns "/Foo/bar" - end - it "should not accept /Foo/bar" do - lambda { @provider.create }.should raise_error(MCXContentProviderException) - end - it "should accept /Foo/bar with ds_type => user" do - @resource.stubs(:[]).with(:ds_type).returns "user" - lambda { @provider.create }.should_not raise_error(MCXContentProviderException) - end - it "should accept /Foo/bar with ds_type => group" do - @resource.stubs(:[]).with(:ds_type).returns "group" - lambda { @provider.create }.should_not raise_error(MCXContentProviderException) - end - it "should accept /Foo/bar with ds_type => computer" do - @resource.stubs(:[]).with(:ds_type).returns "computer" - lambda { @provider.create }.should_not raise_error(MCXContentProviderException) - end - it "should accept :name => /Foo/bar with ds_type => computerlist" do - @resource.stubs(:[]).with(:ds_type).returns "computerlist" - lambda { @provider.create }.should_not raise_error(MCXContentProviderException) - end - end - - describe "when creating and :name => foobar" do - before :each do - @resource.stubs(:[]).with(:name).returns "foobar" - end - it "should not accept unspecified :ds_type and :ds_name" do - lambda { @provider.create }.should raise_error(MCXContentProviderException) - end - it "should not accept unspecified :ds_type" do - @resource.stubs(:[]).with(:ds_type).returns "user" - lambda { @provider.create }.should raise_error(MCXContentProviderException) - end - it "should not accept unspecified :ds_name" do - @resource.stubs(:[]).with(:ds_name).returns "foo" - lambda { @provider.create }.should raise_error(MCXContentProviderException) - end - it "should accept :ds_type => user, ds_name => foo" do - @resource.stubs(:[]).with(:ds_type).returns "user" - @resource.stubs(:[]).with(:ds_name).returns "foo" - lambda { @provider.create }.should_not raise_error(MCXContentProviderException) - end - it "should accept :ds_type => group, ds_name => foo" do - @resource.stubs(:[]).with(:ds_type).returns "group" - @resource.stubs(:[]).with(:ds_name).returns "foo" - lambda { @provider.create }.should_not raise_error(MCXContentProviderException) - end - it "should accept :ds_type => computer, ds_name => foo" do - @resource.stubs(:[]).with(:ds_type).returns "computer" - @resource.stubs(:[]).with(:ds_name).returns "foo" - lambda { @provider.create }.should_not raise_error(MCXContentProviderException) - end - it "should accept :ds_type => computerlist, ds_name => foo" do - @resource.stubs(:[]).with(:ds_type).returns "computerlist" - @resource.stubs(:[]).with(:ds_name).returns "foo" - lambda { @provider.create }.should_not raise_error(MCXContentProviderException) - end - it "should not accept :ds_type => bogustype, ds_name => foo" do - @resource.stubs(:[]).with(:ds_type).returns "bogustype" - @resource.stubs(:[]).with(:ds_name).returns "foo" - lambda { @provider.create }.should raise_error(MCXContentProviderException) - end - end - - describe "when gathering existing instances" do - it "should define an instances class method." do - @provider.class.should respond_to(:instances) - end - it "should call external command dscl -list /Local/Default/<ds_type> on each known ds_type" do - @provider.class.expects(:dscl).with('localhost', '-list', "/Local/Default/Users").returns('') - @provider.class.expects(:dscl).with('localhost', '-list', "/Local/Default/Groups").returns('') - @provider.class.expects(:dscl).with('localhost', '-list', "/Local/Default/Computers").returns('') - @provider.class.expects(:dscl).with('localhost', '-list', "/Local/Default/ComputerLists").returns('') - @provider.class.instances - end + @resource.stubs(:[]).with(:name).returns "/Foo/bar" + end + it "should not accept /Foo/bar" do + lambda { @provider.create }.should raise_error(MCXContentProviderException) + end + it "should accept /Foo/bar with ds_type => user" do + @resource.stubs(:[]).with(:ds_type).returns "user" + lambda { @provider.create }.should_not raise_error(MCXContentProviderException) + end + it "should accept /Foo/bar with ds_type => group" do + @resource.stubs(:[]).with(:ds_type).returns "group" + lambda { @provider.create }.should_not raise_error(MCXContentProviderException) + end + it "should accept /Foo/bar with ds_type => computer" do + @resource.stubs(:[]).with(:ds_type).returns "computer" + lambda { @provider.create }.should_not raise_error(MCXContentProviderException) + end + it "should accept :name => /Foo/bar with ds_type => computerlist" do + @resource.stubs(:[]).with(:ds_type).returns "computerlist" + lambda { @provider.create }.should_not raise_error(MCXContentProviderException) + end + end + + describe "when creating and :name => foobar" do + before :each do + @resource.stubs(:[]).with(:name).returns "foobar" + end + it "should not accept unspecified :ds_type and :ds_name" do + lambda { @provider.create }.should raise_error(MCXContentProviderException) + end + it "should not accept unspecified :ds_type" do + @resource.stubs(:[]).with(:ds_type).returns "user" + lambda { @provider.create }.should raise_error(MCXContentProviderException) + end + it "should not accept unspecified :ds_name" do + @resource.stubs(:[]).with(:ds_name).returns "foo" + lambda { @provider.create }.should raise_error(MCXContentProviderException) + end + it "should accept :ds_type => user, ds_name => foo" do + @resource.stubs(:[]).with(:ds_type).returns "user" + @resource.stubs(:[]).with(:ds_name).returns "foo" + lambda { @provider.create }.should_not raise_error(MCXContentProviderException) + end + it "should accept :ds_type => group, ds_name => foo" do + @resource.stubs(:[]).with(:ds_type).returns "group" + @resource.stubs(:[]).with(:ds_name).returns "foo" + lambda { @provider.create }.should_not raise_error(MCXContentProviderException) + end + it "should accept :ds_type => computer, ds_name => foo" do + @resource.stubs(:[]).with(:ds_type).returns "computer" + @resource.stubs(:[]).with(:ds_name).returns "foo" + lambda { @provider.create }.should_not raise_error(MCXContentProviderException) + end + it "should accept :ds_type => computerlist, ds_name => foo" do + @resource.stubs(:[]).with(:ds_type).returns "computerlist" + @resource.stubs(:[]).with(:ds_name).returns "foo" + lambda { @provider.create }.should_not raise_error(MCXContentProviderException) + end + it "should not accept :ds_type => bogustype, ds_name => foo" do + @resource.stubs(:[]).with(:ds_type).returns "bogustype" + @resource.stubs(:[]).with(:ds_name).returns "foo" + lambda { @provider.create }.should raise_error(MCXContentProviderException) + end + end + + describe "when gathering existing instances" do + it "should define an instances class method." do + @provider.class.should respond_to(:instances) + end + it "should call external command dscl -list /Local/Default/<ds_type> on each known ds_type" do + @provider.class.expects(:dscl).with('localhost', '-list', "/Local/Default/Users").returns('') + @provider.class.expects(:dscl).with('localhost', '-list', "/Local/Default/Groups").returns('') + @provider.class.expects(:dscl).with('localhost', '-list', "/Local/Default/Computers").returns('') + @provider.class.expects(:dscl).with('localhost', '-list', "/Local/Default/ComputerLists").returns('') + @provider.class.instances end + end end diff --git a/spec/unit/provider/mount/parsed_spec.rb b/spec/unit/provider/mount/parsed_spec.rb index dfc2c0e7b..7d2e8a84c 100755 --- a/spec/unit/provider/mount/parsed_spec.rb +++ b/spec/unit/provider/mount/parsed_spec.rb @@ -9,185 +9,185 @@ require 'puppettest/support/utils' require 'puppettest/fileparsing' module ParsedMountTesting - include PuppetTest::Support::Utils - include PuppetTest::FileParsing - - def fake_fstab - os = Facter['operatingsystem'] - if os == "Solaris" - name = "solaris.fstab" - elsif os == "FreeBSD" - name = "freebsd.fstab" - else - # Catchall for other fstabs - name = "linux.fstab" - end - oldpath = @provider_class.default_target - fakefile(File::join("data/types/mount", name)) + include PuppetTest::Support::Utils + include PuppetTest::FileParsing + + def fake_fstab + os = Facter['operatingsystem'] + if os == "Solaris" + name = "solaris.fstab" + elsif os == "FreeBSD" + name = "freebsd.fstab" + else + # Catchall for other fstabs + name = "linux.fstab" end + oldpath = @provider_class.default_target + fakefile(File::join("data/types/mount", name)) + end - def mkmountargs - mount = nil - - if defined?(@pcount) - @pcount += 1 - else - @pcount = 1 - end - args = { - :name => "/fspuppet#{@pcount}", - :device => "/dev/dsk#{@pcount}", - } + def mkmountargs + mount = nil - @provider_class.fields(:parsed).each do |field| - args[field] = "fake#{field}#{@pcount}" unless args.include? field - end + if defined?(@pcount) + @pcount += 1 + else + @pcount = 1 + end + args = { + :name => "/fspuppet#{@pcount}", + :device => "/dev/dsk#{@pcount}", + } - args + @provider_class.fields(:parsed).each do |field| + args[field] = "fake#{field}#{@pcount}" unless args.include? field end - def mkmount - hash = mkmountargs - #hash[:provider] = @provider_class.name + args + end - fakeresource = stub :type => :mount, :name => hash[:name] - fakeresource.stubs(:[]).with(:name).returns(hash[:name]) - fakeresource.stubs(:should).with(:target).returns(nil) + def mkmount + hash = mkmountargs + #hash[:provider] = @provider_class.name - mount = @provider_class.new(fakeresource) - hash[:record_type] = :parsed - hash[:ensure] = :present - mount.property_hash = hash + fakeresource = stub :type => :mount, :name => hash[:name] + fakeresource.stubs(:[]).with(:name).returns(hash[:name]) + fakeresource.stubs(:should).with(:target).returns(nil) - mount - end + mount = @provider_class.new(fakeresource) + hash[:record_type] = :parsed + hash[:ensure] = :present + mount.property_hash = hash - # Here we just create a fake host type that answers to all of the methods - # but does not modify our actual system. - def mkfaketype - @provider.stubs(:filetype).returns(Puppet::Util::FileType.filetype(:ram)) - end + mount + end + + # Here we just create a fake host type that answers to all of the methods + # but does not modify our actual system. + def mkfaketype + @provider.stubs(:filetype).returns(Puppet::Util::FileType.filetype(:ram)) + end end provider_class = Puppet::Type.type(:mount).provider(:parsed) describe provider_class do - before :each do - @mount_class = Puppet::Type.type(:mount) - @provider_class = @mount_class.provider(:parsed) - end + before :each do + @mount_class = Puppet::Type.type(:mount) + @provider_class = @mount_class.provider(:parsed) + end - describe provider_class do - include ParsedMountTesting + describe provider_class do + include ParsedMountTesting - it "should be able to parse all of the example mount tabs" do - tab = fake_fstab - @provider = @provider_class + it "should be able to parse all of the example mount tabs" do + tab = fake_fstab + @provider = @provider_class - # LAK:FIXME Again, a relatively bad test, but I don't know how to rspec-ify this. - # I suppose this is more of an integration test? I dunno. - fakedataparse(tab) do - # Now just make we've got some mounts we know will be there - hashes = @provider_class.target_records(tab).find_all { |i| i.is_a? Hash } - (hashes.length > 0).should be_true - root = hashes.find { |i| i[:name] == "/" } + # LAK:FIXME Again, a relatively bad test, but I don't know how to rspec-ify this. + # I suppose this is more of an integration test? I dunno. + fakedataparse(tab) do + # Now just make we've got some mounts we know will be there + hashes = @provider_class.target_records(tab).find_all { |i| i.is_a? Hash } + (hashes.length > 0).should be_true + root = hashes.find { |i| i[:name] == "/" } - proc { @provider_class.to_file(hashes) }.should_not raise_error - end - end + proc { @provider_class.to_file(hashes) }.should_not raise_error + end + end - # LAK:FIXME I can't mock Facter because this test happens at parse-time. - it "should default to /etc/vfstab on Solaris and /etc/fstab everywhere else" do - should = case Facter.value(:operatingsystem) - when "Solaris"; "/etc/vfstab" - else - "/etc/fstab" - end - Puppet::Type.type(:mount).provider(:parsed).default_target.should == should + # LAK:FIXME I can't mock Facter because this test happens at parse-time. + it "should default to /etc/vfstab on Solaris and /etc/fstab everywhere else" do + should = case Facter.value(:operatingsystem) + when "Solaris"; "/etc/vfstab" + else + "/etc/fstab" end + Puppet::Type.type(:mount).provider(:parsed).default_target.should == should + end - it "should not crash on incomplete lines in fstab" do - parse = @provider_class.parse <<-FSTAB + it "should not crash on incomplete lines in fstab" do + parse = @provider_class.parse <<-FSTAB /dev/incomplete /dev/device name - FSTAB + FSTAB - lambda{ @provider_class.to_line(parse[0]) }.should_not raise_error - end + lambda{ @provider_class.to_line(parse[0]) }.should_not raise_error end + end - describe provider_class, " when mounting an absent filesystem" do - include ParsedMountTesting + describe provider_class, " when mounting an absent filesystem" do + include ParsedMountTesting - # #730 - Make sure 'flush' is called when a mount is moving from absent to mounted - it "should flush the fstab to disk" do - mount = mkmount + # #730 - Make sure 'flush' is called when a mount is moving from absent to mounted + it "should flush the fstab to disk" do + mount = mkmount - # Mark the mount as absent - mount.property_hash[:ensure] = :absent + # Mark the mount as absent + mount.property_hash[:ensure] = :absent - mount.stubs(:mountcmd) # just so we don't actually try to mount anything + mount.stubs(:mountcmd) # just so we don't actually try to mount anything - mount.expects(:flush) - mount.mount - end + mount.expects(:flush) + mount.mount + end + end + + describe provider_class, " when modifying the filesystem tab" do + include ParsedMountTesting + before do + Puppet.settings.stubs(:use) + # Never write to disk, only to RAM. + #@provider_class.stubs(:filetype).returns(Puppet::Util::FileType.filetype(:ram)) + @provider_class.stubs(:target_object).returns(Puppet::Util::FileType.filetype(:ram).new("eh")) + @provider_class.clear + + @mount = mkmount + @target = @provider_class.default_target end - describe provider_class, " when modifying the filesystem tab" do - include ParsedMountTesting - before do - Puppet.settings.stubs(:use) - # Never write to disk, only to RAM. - #@provider_class.stubs(:filetype).returns(Puppet::Util::FileType.filetype(:ram)) - @provider_class.stubs(:target_object).returns(Puppet::Util::FileType.filetype(:ram).new("eh")) - @provider_class.clear - - @mount = mkmount - @target = @provider_class.default_target - end - - it "should write the mount to disk when :flush is called" do - old_text = @provider_class.target_object(@provider_class.default_target).read + it "should write the mount to disk when :flush is called" do + old_text = @provider_class.target_object(@provider_class.default_target).read - @mount.flush + @mount.flush - text = @provider_class.target_object(@provider_class.default_target).read - text.should == old_text + @mount.class.to_line(@mount.property_hash) + "\n" - end + text = @provider_class.target_object(@provider_class.default_target).read + text.should == old_text + @mount.class.to_line(@mount.property_hash) + "\n" end + end - describe provider_class, " when parsing information about the root filesystem" do - confine "Mount type not tested on Darwin" => Facter["operatingsystem"].value != "Darwin" - include ParsedMountTesting + describe provider_class, " when parsing information about the root filesystem" do + confine "Mount type not tested on Darwin" => Facter["operatingsystem"].value != "Darwin" + include ParsedMountTesting - before do - @mount = @mount_class.new :name => "/" - @provider = @mount.provider - end + before do + @mount = @mount_class.new :name => "/" + @provider = @mount.provider + end - it "should have a filesystem tab" do - FileTest.should be_exist(@provider_class.default_target) - end + it "should have a filesystem tab" do + FileTest.should be_exist(@provider_class.default_target) + end - it "should find the root filesystem" do - @provider_class.prefetch("/" => @mount) - @mount.provider.property_hash[:ensure].should == :present - end + it "should find the root filesystem" do + @provider_class.prefetch("/" => @mount) + @mount.provider.property_hash[:ensure].should == :present + end - it "should determine that the root fs is mounted" do - @provider_class.prefetch("/" => @mount) - @mount.provider.should be_mounted - end + it "should determine that the root fs is mounted" do + @provider_class.prefetch("/" => @mount) + @mount.provider.should be_mounted end + end - describe provider_class, " when mounting and unmounting" do - include ParsedMountTesting + describe provider_class, " when mounting and unmounting" do + include ParsedMountTesting - it "should call the 'mount' command to mount the filesystem" + it "should call the 'mount' command to mount the filesystem" - it "should call the 'unmount' command to unmount the filesystem" + it "should call the 'unmount' command to unmount the filesystem" - it "should specify the filesystem when remounting a filesystem" - end + it "should specify the filesystem when remounting a filesystem" + end end diff --git a/spec/unit/provider/mount_spec.rb b/spec/unit/provider/mount_spec.rb index 41abcd424..55a52b44a 100755 --- a/spec/unit/provider/mount_spec.rb +++ b/spec/unit/provider/mount_spec.rb @@ -5,126 +5,126 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/provider/mount' describe Puppet::Provider::Mount do - before :each do - @mounter = Object.new - @mounter.extend(Puppet::Provider::Mount) + before :each do + @mounter = Object.new + @mounter.extend(Puppet::Provider::Mount) - @name = "/" + @name = "/" - @resource = stub 'resource' - @resource.stubs(:[]).with(:name).returns(@name) + @resource = stub 'resource' + @resource.stubs(:[]).with(:name).returns(@name) - @mounter.stubs(:resource).returns(@resource) + @mounter.stubs(:resource).returns(@resource) + end + + describe Puppet::Provider::Mount, " when mounting" do + + it "should use the 'mountcmd' method to mount" do + @mounter.stubs(:options).returns(nil) + @mounter.expects(:mountcmd) + + @mounter.mount end - describe Puppet::Provider::Mount, " when mounting" do + it "should flush before mounting if a flush method exists" do + @mounter.meta_def(:flush) { } + @mounter.expects(:flush) + @mounter.stubs(:mountcmd) + @mounter.stubs(:options).returns(nil) - it "should use the 'mountcmd' method to mount" do - @mounter.stubs(:options).returns(nil) - @mounter.expects(:mountcmd) + @mounter.mount + end - @mounter.mount - end + it "should add the options following '-o' if they exist and are not set to :absent" do + @mounter.stubs(:options).returns("ro") + @mounter.expects(:mountcmd).with { |*ary| ary[0] == "-o" and ary[1] == "ro" } - it "should flush before mounting if a flush method exists" do - @mounter.meta_def(:flush) { } - @mounter.expects(:flush) - @mounter.stubs(:mountcmd) - @mounter.stubs(:options).returns(nil) + @mounter.mount + end - @mounter.mount - end + it "should specify the filesystem name to the mount command" do + @mounter.stubs(:options).returns(nil) + @mounter.expects(:mountcmd).with { |*ary| ary[-1] == @name } - it "should add the options following '-o' if they exist and are not set to :absent" do - @mounter.stubs(:options).returns("ro") - @mounter.expects(:mountcmd).with { |*ary| ary[0] == "-o" and ary[1] == "ro" } + @mounter.mount + end + end - @mounter.mount - end + describe Puppet::Provider::Mount, " when remounting" do - it "should specify the filesystem name to the mount command" do - @mounter.stubs(:options).returns(nil) - @mounter.expects(:mountcmd).with { |*ary| ary[-1] == @name } + it "should use '-o remount' if the resource specifies it supports remounting" do + @mounter.stubs(:info) + @resource.stubs(:[]).with(:remounts).returns(:true) + @mounter.expects(:mountcmd).with("-o", "remount", @name) + @mounter.remount + end - @mounter.mount - end + it "should unmount and mount if the resource does not specify it supports remounting" do + @mounter.stubs(:info) + @resource.stubs(:[]).with(:remounts).returns(false) + @mounter.expects(:unmount) + @mounter.expects(:mount) + @mounter.remount end - describe Puppet::Provider::Mount, " when remounting" do - - it "should use '-o remount' if the resource specifies it supports remounting" do - @mounter.stubs(:info) - @resource.stubs(:[]).with(:remounts).returns(:true) - @mounter.expects(:mountcmd).with("-o", "remount", @name) - @mounter.remount - end - - it "should unmount and mount if the resource does not specify it supports remounting" do - @mounter.stubs(:info) - @resource.stubs(:[]).with(:remounts).returns(false) - @mounter.expects(:unmount) - @mounter.expects(:mount) - @mounter.remount - end - - it "should log that it is remounting" do - @resource.stubs(:[]).with(:remounts).returns(:true) - @mounter.stubs(:mountcmd) - @mounter.expects(:info).with("Remounting") - @mounter.remount - end + it "should log that it is remounting" do + @resource.stubs(:[]).with(:remounts).returns(:true) + @mounter.stubs(:mountcmd) + @mounter.expects(:info).with("Remounting") + @mounter.remount end + end - describe Puppet::Provider::Mount, " when unmounting" do + describe Puppet::Provider::Mount, " when unmounting" do - it "should call the :umount command with the resource name" do - @mounter.expects(:umount).with(@name) - @mounter.unmount - end + it "should call the :umount command with the resource name" do + @mounter.expects(:umount).with(@name) + @mounter.unmount end + end - describe Puppet::Provider::Mount, " when determining if it is mounted" do + describe Puppet::Provider::Mount, " when determining if it is mounted" do - it "should parse the results of running the mount command with no arguments" do - Facter.stubs(:value).returns("whatever") - @mounter.expects(:mountcmd).returns("") + it "should parse the results of running the mount command with no arguments" do + Facter.stubs(:value).returns("whatever") + @mounter.expects(:mountcmd).returns("") - @mounter.mounted? - end + @mounter.mounted? + end - it "should match ' on /private/var/automount<name>' if the operating system is Darwin" do - Facter.stubs(:value).with("operatingsystem").returns("Darwin") - @mounter.expects(:mountcmd).returns("/dev/whatever on /private/var/automount/\ndevfs on /dev") + it "should match ' on /private/var/automount<name>' if the operating system is Darwin" do + Facter.stubs(:value).with("operatingsystem").returns("Darwin") + @mounter.expects(:mountcmd).returns("/dev/whatever on /private/var/automount/\ndevfs on /dev") - @mounter.should be_mounted - end + @mounter.should be_mounted + end - it "should match ' on <name>' if the operating system is Darwin" do - Facter.stubs(:value).with("operatingsystem").returns("Darwin") - @mounter.expects(:mountcmd).returns("/dev/disk03 on / (local, journaled)\ndevfs on /dev") + it "should match ' on <name>' if the operating system is Darwin" do + Facter.stubs(:value).with("operatingsystem").returns("Darwin") + @mounter.expects(:mountcmd).returns("/dev/disk03 on / (local, journaled)\ndevfs on /dev") - @mounter.should be_mounted - end + @mounter.should be_mounted + end - it "should match '^<name> on' if the operating system is Solaris" do - Facter.stubs(:value).with("operatingsystem").returns("Solaris") - @mounter.expects(:mountcmd).returns("/ on /dev/dsk/whatever\n/var on /dev/dsk/other") + it "should match '^<name> on' if the operating system is Solaris" do + Facter.stubs(:value).with("operatingsystem").returns("Solaris") + @mounter.expects(:mountcmd).returns("/ on /dev/dsk/whatever\n/var on /dev/dsk/other") - @mounter.should be_mounted - end + @mounter.should be_mounted + end - it "should match ' on <name>' if the operating system is not Darwin or Solaris" do - Facter.stubs(:value).with("operatingsystem").returns("Debian") - @mounter.expects(:mountcmd).returns("/dev/dsk/whatever on / and stuff\n/dev/other/disk on /var and stuff") + it "should match ' on <name>' if the operating system is not Darwin or Solaris" do + Facter.stubs(:value).with("operatingsystem").returns("Debian") + @mounter.expects(:mountcmd).returns("/dev/dsk/whatever on / and stuff\n/dev/other/disk on /var and stuff") - @mounter.should be_mounted - end + @mounter.should be_mounted + end - it "should not be considered mounted if it did not match the mount output" do - Facter.stubs(:value).with("operatingsystem").returns("Debian") - @mounter.expects(:mountcmd).returns("/dev/dsk/whatever on /something/else and stuff\n/dev/other/disk on /var and stuff") + it "should not be considered mounted if it did not match the mount output" do + Facter.stubs(:value).with("operatingsystem").returns("Debian") + @mounter.expects(:mountcmd).returns("/dev/dsk/whatever on /something/else and stuff\n/dev/other/disk on /var and stuff") - @mounter.should_not be_mounted - end + @mounter.should_not be_mounted end + end end diff --git a/spec/unit/provider/naginator_spec.rb b/spec/unit/provider/naginator_spec.rb index d0d43aa4b..72195756c 100755 --- a/spec/unit/provider/naginator_spec.rb +++ b/spec/unit/provider/naginator_spec.rb @@ -5,54 +5,54 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/provider/naginator' describe Puppet::Provider::Naginator do - before do - @resource_type = stub 'resource_type', :name => :nagios_test - @class = Class.new(Puppet::Provider::Naginator) + before do + @resource_type = stub 'resource_type', :name => :nagios_test + @class = Class.new(Puppet::Provider::Naginator) - @class.stubs(:resource_type).returns @resource_type - end + @class.stubs(:resource_type).returns @resource_type + end - it "should be able to look up the associated Nagios type" do - nagios_type = mock "nagios_type" - nagios_type.stubs :attr_accessor - Nagios::Base.expects(:type).with(:test).returns nagios_type + it "should be able to look up the associated Nagios type" do + nagios_type = mock "nagios_type" + nagios_type.stubs :attr_accessor + Nagios::Base.expects(:type).with(:test).returns nagios_type - @class.nagios_type.should equal(nagios_type) - end + @class.nagios_type.should equal(nagios_type) + end - it "should use the Nagios type to determine whether an attribute is valid" do - nagios_type = mock "nagios_type" - nagios_type.stubs :attr_accessor - Nagios::Base.expects(:type).with(:test).returns nagios_type + it "should use the Nagios type to determine whether an attribute is valid" do + nagios_type = mock "nagios_type" + nagios_type.stubs :attr_accessor + Nagios::Base.expects(:type).with(:test).returns nagios_type - nagios_type.expects(:parameters).returns [:foo, :bar] + nagios_type.expects(:parameters).returns [:foo, :bar] - @class.valid_attr?(:test, :foo).should be_true - end + @class.valid_attr?(:test, :foo).should be_true + end - it "should use Naginator to parse configuration snippets" do - parser = mock 'parser' - parser.expects(:parse).with("my text").returns "my instances" - Nagios::Parser.expects(:new).returns(parser) + it "should use Naginator to parse configuration snippets" do + parser = mock 'parser' + parser.expects(:parse).with("my text").returns "my instances" + Nagios::Parser.expects(:new).returns(parser) - @class.parse("my text").should == "my instances" - end + @class.parse("my text").should == "my instances" + end - it "should join Nagios::Base records with '\\n' when asked to convert them to text" do - @class.expects(:header).returns "myheader\n" + it "should join Nagios::Base records with '\\n' when asked to convert them to text" do + @class.expects(:header).returns "myheader\n" - @class.to_file([:one, :two]).should == "myheader\none\ntwo" - end + @class.to_file([:one, :two]).should == "myheader\none\ntwo" + end - it "should be able to prefetch instance from configuration files" do - @class.should respond_to(:prefetch) - end + it "should be able to prefetch instance from configuration files" do + @class.should respond_to(:prefetch) + end - it "should be able to generate a list of instances" do - @class.should respond_to(:instances) - end + it "should be able to generate a list of instances" do + @class.should respond_to(:instances) + end - it "should never skip records" do - @class.should_not be_skip_record("foo") - end + it "should never skip records" do + @class.should_not be_skip_record("foo") + end end diff --git a/spec/unit/provider/package/aix_spec.rb b/spec/unit/provider/package/aix_spec.rb index 691749df9..26732a3f0 100755 --- a/spec/unit/provider/package/aix_spec.rb +++ b/spec/unit/provider/package/aix_spec.rb @@ -5,62 +5,62 @@ require File.dirname(__FILE__) + '/../../../spec_helper' provider_class = Puppet::Type.type(:package).provider(:aix) describe provider_class do - before(:each) do - # Create a mock resource - @resource = stub 'resource' + before(:each) do + # Create a mock resource + @resource = stub 'resource' - # A catch all; no parameters set - @resource.stubs(:[]).returns(nil) + # A catch all; no parameters set + @resource.stubs(:[]).returns(nil) - # But set name and source - @resource.stubs(:[]).with(:name).returns "mypackage" - @resource.stubs(:[]).with(:source).returns "mysource" - @resource.stubs(:[]).with(:ensure).returns :installed + # But set name and source + @resource.stubs(:[]).with(:name).returns "mypackage" + @resource.stubs(:[]).with(:source).returns "mysource" + @resource.stubs(:[]).with(:ensure).returns :installed - @provider = provider_class.new - @provider.resource = @resource - end - - [:install, :uninstall, :latest, :query, :update].each do |method| - it "should have a #{method} method" do - @provider.should respond_to(method) - end - end + @provider = provider_class.new + @provider.resource = @resource + end - it "should uninstall a package" do - @provider.expects(:installp).with('-gu', 'mypackage') - @provider.uninstall + [:install, :uninstall, :latest, :query, :update].each do |method| + it "should have a #{method} method" do + @provider.should respond_to(method) end + end - describe "when installing" do - it "should install a package" do - @resource.stubs(:should).with(:ensure).returns(:installed) - @provider.expects(:installp).with('-acgwXY', '-d', 'mysource', 'mypackage') - @provider.install - end + it "should uninstall a package" do + @provider.expects(:installp).with('-gu', 'mypackage') + @provider.uninstall + end - it "should install a specific package version" do - @resource.stubs(:should).with(:ensure).returns("1.2.3.4") - @provider.expects(:installp).with('-acgwXY', '-d', 'mysource', 'mypackage 1.2.3.4') - @provider.install - end + describe "when installing" do + it "should install a package" do + @resource.stubs(:should).with(:ensure).returns(:installed) + @provider.expects(:installp).with('-acgwXY', '-d', 'mysource', 'mypackage') + @provider.install end - describe "when finding the latest version" do - it "should return the current version when no later version is present" do - @provider.stubs(:latest_info).returns(nil) - @provider.stubs(:properties).returns( { :ensure => "1.2.3.4" } ) - @provider.latest.should == "1.2.3.4" - end + it "should install a specific package version" do + @resource.stubs(:should).with(:ensure).returns("1.2.3.4") + @provider.expects(:installp).with('-acgwXY', '-d', 'mysource', 'mypackage 1.2.3.4') + @provider.install + end + end - it "should return the latest version of a package" do - @provider.stubs(:latest_info).returns( { :version => "1.2.3.5" } ) - @provider.latest.should == "1.2.3.5" - end + describe "when finding the latest version" do + it "should return the current version when no later version is present" do + @provider.stubs(:latest_info).returns(nil) + @provider.stubs(:properties).returns( { :ensure => "1.2.3.4" } ) + @provider.latest.should == "1.2.3.4" end - it "update should install a package" do - @provider.expects(:install).with(false) - @provider.update + it "should return the latest version of a package" do + @provider.stubs(:latest_info).returns( { :version => "1.2.3.5" } ) + @provider.latest.should == "1.2.3.5" end + end + + it "update should install a package" do + @provider.expects(:install).with(false) + @provider.update + end end diff --git a/spec/unit/provider/package/apt_spec.rb b/spec/unit/provider/package/apt_spec.rb index 54f2c0771..3c6bf62af 100755 --- a/spec/unit/provider/package/apt_spec.rb +++ b/spec/unit/provider/package/apt_spec.rb @@ -5,141 +5,141 @@ Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f provider = Puppet::Type.type(:package).provider(:apt) describe provider do - before do - @resource = stub 'resource', :[] => "asdf" - @provider = provider.new(@resource) + before do + @resource = stub 'resource', :[] => "asdf" + @provider = provider.new(@resource) - @fakeresult = "install ok installed asdf 1.0\n" - end + @fakeresult = "install ok installed asdf 1.0\n" + end - it "should be versionable" do - provider.should be_versionable - 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 :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") + it "should use 'apt-get remove' to uninstall" do + @provider.expects(:aptget).with("-y", "-q", :remove, "asdf") - @provider.uninstall - end + @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") + 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 + @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: + 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 + 650 http://ftp.osuosl.org testing/main Packages *** 1:1.1 - 100 /var/lib/dpkg/status" + 100 /var/lib/dpkg/status" - @provider.latest.should == "1:1.1" - end + @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:" + 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 + @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 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 + 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.expects(:info) + @provider.expects(:preseed).with("/my/file") - @provider.run_preseed - end + @provider.run_preseed + end - it "should not preseed if no responsefile is provided" do - @resource.expects(:[]).with(:responsefile).returns nil + it "should not preseed if no responsefile is provided" do + @resource.expects(:[]).with(:responsefile).returns nil - @provider.expects(:info) - @provider.expects(:preseed).never + @provider.expects(:info) + @provider.expects(:preseed).never - @provider.run_preseed - end + @provider.run_preseed + end - it "should fail if a cdrom is listed in the sources list and :allowcdrom is not specified" + 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) + 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 + @provider.stubs(:aptget) + @provider.install + end - it "should check for a cdrom" do - @provider.expects(:checkforcdrom) + it "should check for a cdrom" do + @provider.expects(:checkforcdrom) - @provider.stubs(:aptget) - @provider.install - end + @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 } + 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 + @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" } + 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 + @provider.install + end - it "should use --force-yes if a package version is specified" do - @resource.expects(:[]).with(:ensure).returns "1.0" - @provider.expects(:aptget).with { |*command| command.include?("--force-yes") } + it "should use --force-yes if a package version is specified" do + @resource.expects(:[]).with(:ensure).returns "1.0" + @provider.expects(:aptget).with { |*command| command.include?("--force-yes") } - @provider.install - end + @provider.install + end - it "should do a quiet install" do - @provider.expects(:aptget).with { |*command| command.include?("-q") } + it "should do a quiet install" do + @provider.expects(:aptget).with { |*command| command.include?("-q") } - @provider.install - end + @provider.install + end - it "should default to 'yes' for all questions" do - @provider.expects(:aptget).with { |*command| command.include?("-y") } + it "should default to 'yes' for all questions" do + @provider.expects(:aptget).with { |*command| command.include?("-y") } - @provider.install - end + @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") } + 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 + @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") } + 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 + @provider.install end + end end diff --git a/spec/unit/provider/package/dpkg_spec.rb b/spec/unit/provider/package/dpkg_spec.rb index 68ffb19a4..47ac9766e 100755 --- a/spec/unit/provider/package/dpkg_spec.rb +++ b/spec/unit/provider/package/dpkg_spec.rb @@ -5,221 +5,221 @@ Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f provider = Puppet::Type.type(:package).provider(:dpkg) describe provider do - before do - @resource = stub 'resource', :[] => "asdf" - @provider = provider.new(@resource) - @provider.expects(:execute).never # forbid "manual" executions - - @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 + before do + @resource = stub 'resource', :[] => "asdf" + @provider = provider.new(@resource) + @provider.expects(:execute).never # forbid "manual" executions - 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 + @fakeresult = "install ok installed asdf 1.0\n" + end - provider.instances - end + it "should have documentation" do + provider.doc.should be_instance_of(String) + 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 + describe "when listing all instances" do + before do + provider.stubs(:command).with(:dpkgquery).returns "myquery" + end - asdf = mock 'pkg1' - provider.expects(:new).with(:ensure => "1.0", :error => "ok", :desired => "install", :name => "asdf", :status => "installed", :provider => :dpkg).returns asdf + 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.should == [asdf] - end + provider.instances + 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 + 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 - Puppet.expects(:warning) - provider.expects(:new).never + 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 == [] - end + provider.instances.should == [asdf] 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 + 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 - @provider.query - end + Puppet.expects(:warning) + provider.expects(:new).never - it "should consider the package purged if dpkg-query fails" do - @provider.expects(:dpkgquery).raises Puppet::ExecutionFailure.new("eh") + provider.instances.should == [] + end + end - @provider.query[:ensure].should == :purged - 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 - 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 + end - @provider.query.should == {:ensure => "1.0", :error => "ok", :desired => "install", :name => "asdf", :status => "installed", :provider => :dpkg} - end + it "should consider the package purged if dpkg-query fails" do + @provider.expects(:dpkgquery).raises Puppet::ExecutionFailure.new("eh") - it "should consider the package absent if the dpkg-query result cannot be interpreted" do - @provider.expects(:dpkgquery).returns "somebaddata" + @provider.query[:ensure].should == :purged + end - @provider.query[:ensure].should == :absent - 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 - it "should fail if an error is discovered" do - @provider.expects(:dpkgquery).returns @fakeresult.sub("ok", "error") + @provider.query.should == {:ensure => "1.0", :error => "ok", :desired => "install", :name => "asdf", :status => "installed", :provider => :dpkg} + end - lambda { @provider.query }.should raise_error(Puppet::Error) - end + it "should consider the package absent if the dpkg-query result cannot be interpreted" do + @provider.expects(:dpkgquery).returns "somebaddata" - 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 == :absent + end - @provider.query[:ensure].should == :purged - end + it "should fail if an error is discovered" do + @provider.expects(:dpkgquery).returns @fakeresult.sub("ok", "error") - it "should consider the package absent if it is marked 'config-files'" do - @provider.expects(:dpkgquery).returns @fakeresult.sub("installed", "config-files") - @provider.query[:ensure].should == :absent - end + lambda { @provider.query }.should raise_error(Puppet::Error) + end - it "should consider the package absent if it is marked 'half-installed'" do - @provider.expects(:dpkgquery).returns @fakeresult.sub("installed", "half-installed") - @provider.query[:ensure].should == :absent - end + it "should consider the package purged if it is marked 'not-installed'" do + @provider.expects(:dpkgquery).returns @fakeresult.sub("installed", "not-installed") - it "should consider the package absent if it is marked 'unpacked'" do - @provider.expects(:dpkgquery).returns @fakeresult.sub("installed", "unpacked") - @provider.query[:ensure].should == :absent - end + @provider.query[:ensure].should == :purged + end - it "should consider the package absent if it is marked 'half-configured'" do - @provider.expects(:dpkgquery).returns @fakeresult.sub("installed", "half-configured") - @provider.query[:ensure].should == :absent - end + it "should consider the package absent if it is marked 'config-files'" do + @provider.expects(:dpkgquery).returns @fakeresult.sub("installed", "config-files") + @provider.query[:ensure].should == :absent + end - it "should consider the package held if its state is 'hold'" do - @provider.expects(:dpkgquery).returns @fakeresult.sub("install", "hold") - @provider.query[:ensure].should == :held - end + it "should consider the package absent if it is marked 'half-installed'" do + @provider.expects(:dpkgquery).returns @fakeresult.sub("installed", "half-installed") + @provider.query[:ensure].should == :absent end - it "should be able to install" do - @provider.should respond_to(:install) + it "should consider the package absent if it is marked 'unpacked'" do + @provider.expects(:dpkgquery).returns @fakeresult.sub("installed", "unpacked") + @provider.query[:ensure].should == :absent end - describe "when installing" do - before do - @resource.stubs(:[]).with(:source).returns "mypkg" - end + it "should consider the package absent if it is marked 'half-configured'" do + @provider.expects(:dpkgquery).returns @fakeresult.sub("installed", "half-configured") + @provider.query[:ensure].should == :absent + end - it "should fail to install if no source is specified in the resource" do - @resource.expects(:[]).with(:source).returns nil + it "should consider the package held if its state is 'hold'" do + @provider.expects(:dpkgquery).returns @fakeresult.sub("install", "hold") + @provider.query[:ensure].should == :held + end + end - lambda { @provider.install }.should raise_error(ArgumentError) - end + it "should be able to install" do + @provider.should respond_to(:install) + end - it "should use 'dpkg -i' to install the package" do - @resource.expects(:[]).with(:source).returns "mypackagefile" - @provider.expects(:unhold) - @provider.expects(:dpkg).with { |*command| command[-1] == "mypackagefile" and command[-2] == "-i" } + describe "when installing" do + before do + @resource.stubs(:[]).with(:source).returns "mypkg" + end - @provider.install - end + it "should fail to install if no source is specified in the resource" do + @resource.expects(:[]).with(:source).returns nil - it "should keep old config files if told to do so" do - @resource.expects(:[]).with(:configfiles).returns :keep - @provider.expects(:unhold) - @provider.expects(:dpkg).with { |*command| command[0] == "--force-confold" } + lambda { @provider.install }.should raise_error(ArgumentError) + end - @provider.install - end + it "should use 'dpkg -i' to install the package" do + @resource.expects(:[]).with(:source).returns "mypackagefile" + @provider.expects(:unhold) + @provider.expects(:dpkg).with { |*command| command[-1] == "mypackagefile" and command[-2] == "-i" } - it "should replace old config files if told to do so" do - @resource.expects(:[]).with(:configfiles).returns :replace - @provider.expects(:unhold) - @provider.expects(:dpkg).with { |*command| command[0] == "--force-confnew" } + @provider.install + end - @provider.install - end + it "should keep old config files if told to do so" do + @resource.expects(:[]).with(:configfiles).returns :keep + @provider.expects(:unhold) + @provider.expects(:dpkg).with { |*command| command[0] == "--force-confold" } - it "should ensure any hold is removed" do - @provider.expects(:unhold).once - @provider.expects(:dpkg) - @provider.install - end + @provider.install end - describe "when holding or unholding" do - before do - @tempfile = stub 'tempfile', :print => nil, :close => nil, :flush => nil, :path => "/other/file" - @tempfile.stubs(:write) - Tempfile.stubs(:new).returns @tempfile - end - - it "should install first if holding" do - @provider.stubs(:execute) - @provider.expects(:install).once - @provider.hold - end + it "should replace old config files if told to do so" do + @resource.expects(:[]).with(:configfiles).returns :replace + @provider.expects(:unhold) + @provider.expects(:dpkg).with { |*command| command[0] == "--force-confnew" } - it "should execute dpkg --set-selections when holding" do - @provider.stubs(:install) - @provider.expects(:execute).with([:dpkg, '--set-selections'], {:stdinfile => @tempfile.path}).once - @provider.hold - end + @provider.install + end - it "should execute dpkg --set-selections when unholding" do - @provider.stubs(:install) - @provider.expects(:execute).with([:dpkg, '--set-selections'], {:stdinfile => @tempfile.path}).once - @provider.hold - end + it "should ensure any hold is removed" do + @provider.expects(:unhold).once + @provider.expects(:dpkg) + @provider.install end + end - it "should use :install to update" do - @provider.expects(:install) - @provider.update + describe "when holding or unholding" do + before do + @tempfile = stub 'tempfile', :print => nil, :close => nil, :flush => nil, :path => "/other/file" + @tempfile.stubs(:write) + Tempfile.stubs(:new).returns @tempfile 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 install first if holding" do + @provider.stubs(:execute) + @provider.expects(:install).once + @provider.hold + 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 + it "should execute dpkg --set-selections when holding" do + @provider.stubs(:install) + @provider.expects(:execute).with([:dpkg, '--set-selections'], {:stdinfile => @tempfile.path}).once + @provider.hold + end - it "should cope with names containing ++" do - @resource = stub 'resource', :[] => "asdf++" - @provider = provider.new(@resource) - @provider.expects(:dpkg_deb).returns "asdf++\t1.0" - @provider.latest.should == "1.0" - end + it "should execute dpkg --set-selections when unholding" do + @provider.stubs(:install) + @provider.expects(:execute).with([:dpkg, '--set-selections'], {:stdinfile => @tempfile.path}).once + @provider.hold + 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 use 'dpkg -r' to uninstall" do - @provider.expects(:dpkg).with("-r", "asdf") - @provider.uninstall + 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 - it "should use 'dpkg --purge' to purge" do - @provider.expects(:dpkg).with("--purge", "asdf") - @provider.purge + it "should cope with names containing ++" do + @resource = stub 'resource', :[] => "asdf++" + @provider = provider.new(@resource) + @provider.expects(:dpkg_deb).returns "asdf++\t1.0" + @provider.latest.should == "1.0" 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/package/gem_spec.rb b/spec/unit/provider/package/gem_spec.rb index 3dc1fa347..063e1474b 100644 --- a/spec/unit/provider/package/gem_spec.rb +++ b/spec/unit/provider/package/gem_spec.rb @@ -5,83 +5,83 @@ require File.dirname(__FILE__) + '/../../../spec_helper' provider_class = Puppet::Type.type(:package).provider(:gem) describe provider_class do - it "should have an install method" do - @provider = provider_class.new - @provider.should respond_to(:install) + it "should have an install method" do + @provider = provider_class.new + @provider.should respond_to(:install) + end + + describe "when installing" do + before do + # Create a mock resource + @resource = stub 'resource' + + # A catch all; no parameters set + @resource.stubs(:[]).returns nil + + # We have to set a name, though + @resource.stubs(:[]).with(:name).returns "myresource" + @resource.stubs(:[]).with(:ensure).returns :installed + + @provider = provider_class.new + @provider.stubs(:resource).returns @resource end - describe "when installing" do - before do - # Create a mock resource - @resource = stub 'resource' + it "should use the path to the gem" do + provider_class.stubs(:command).with(:gemcmd).returns "/my/gem" + @provider.expects(:execute).with { |args| args[0] == "/my/gem" }.returns "" + @provider.install + end - # A catch all; no parameters set - @resource.stubs(:[]).returns nil + it "should specify that the gem is being installed" do + @provider.expects(:execute).with { |args| args[1] == "install" }.returns "" + @provider.install + end - # We have to set a name, though - @resource.stubs(:[]).with(:name).returns "myresource" - @resource.stubs(:[]).with(:ensure).returns :installed + it "should specify that dependencies should be included" do + @provider.expects(:execute).with { |args| args[2] == "--include-dependencies" }.returns "" + @provider.install + end - @provider = provider_class.new - @provider.stubs(:resource).returns @resource - end + it "should specify the package name" do + @provider.expects(:execute).with { |args| args[3] == "myresource" }.returns "" + @provider.install + end - it "should use the path to the gem" do - provider_class.stubs(:command).with(:gemcmd).returns "/my/gem" - @provider.expects(:execute).with { |args| args[0] == "/my/gem" }.returns "" - @provider.install + describe "when a source is specified" do + describe "as a normal file" do + it "should use the file name instead of the gem name" do + @resource.stubs(:[]).with(:source).returns "/my/file" + @provider.expects(:execute).with { |args| args[3] == "/my/file" }.returns "" + @provider.install end - - it "should specify that the gem is being installed" do - @provider.expects(:execute).with { |args| args[1] == "install" }.returns "" - @provider.install + end + describe "as a file url" do + it "should use the file name instead of the gem name" do + @resource.stubs(:[]).with(:source).returns "file:///my/file" + @provider.expects(:execute).with { |args| args[3] == "/my/file" }.returns "" + @provider.install end - - it "should specify that dependencies should be included" do - @provider.expects(:execute).with { |args| args[2] == "--include-dependencies" }.returns "" - @provider.install + end + describe "as a puppet url" do + it "should fail" do + @resource.stubs(:[]).with(:source).returns "puppet://my/file" + lambda { @provider.install }.should raise_error(Puppet::Error) end - - it "should specify the package name" do - @provider.expects(:execute).with { |args| args[3] == "myresource" }.returns "" - @provider.install + end + describe "as a non-file and non-puppet url" do + it "should treat the source as a gem repository" do + @resource.stubs(:[]).with(:source).returns "http://host/my/file" + @provider.expects(:execute).with { |args| args[3..5] == ["--source", "http://host/my/file", "myresource"] }.returns "" + @provider.install end - - describe "when a source is specified" do - describe "as a normal file" do - it "should use the file name instead of the gem name" do - @resource.stubs(:[]).with(:source).returns "/my/file" - @provider.expects(:execute).with { |args| args[3] == "/my/file" }.returns "" - @provider.install - end - end - describe "as a file url" do - it "should use the file name instead of the gem name" do - @resource.stubs(:[]).with(:source).returns "file:///my/file" - @provider.expects(:execute).with { |args| args[3] == "/my/file" }.returns "" - @provider.install - end - end - describe "as a puppet url" do - it "should fail" do - @resource.stubs(:[]).with(:source).returns "puppet://my/file" - lambda { @provider.install }.should raise_error(Puppet::Error) - end - end - describe "as a non-file and non-puppet url" do - it "should treat the source as a gem repository" do - @resource.stubs(:[]).with(:source).returns "http://host/my/file" - @provider.expects(:execute).with { |args| args[3..5] == ["--source", "http://host/my/file", "myresource"] }.returns "" - @provider.install - end - end - describe "with an invalid uri" do - it "should fail" do - URI.expects(:parse).raises(ArgumentError) - @resource.stubs(:[]).with(:source).returns "http:::::uppet:/:/my/file" - lambda { @provider.install }.should raise_error(Puppet::Error) - end - end + end + describe "with an invalid uri" do + it "should fail" do + URI.expects(:parse).raises(ArgumentError) + @resource.stubs(:[]).with(:source).returns "http:::::uppet:/:/my/file" + lambda { @provider.install }.should raise_error(Puppet::Error) end + end end + end end diff --git a/spec/unit/provider/package/hpux_spec.rb b/spec/unit/provider/package/hpux_spec.rb index 8343c6052..e4ac22e21 100644 --- a/spec/unit/provider/package/hpux_spec.rb +++ b/spec/unit/provider/package/hpux_spec.rb @@ -5,48 +5,48 @@ require File.dirname(__FILE__) + '/../../../spec_helper' provider_class = Puppet::Type.type(:package).provider(:hpux) describe provider_class do - before(:each) do - # Create a mock resource - @resource = stub 'resource' - - # A catch all; no parameters set - @resource.stubs(:[]).returns(nil) - - # But set name and source - @resource.stubs(:[]).with(:name).returns "mypackage" - @resource.stubs(:[]).with(:source).returns "mysource" - @resource.stubs(:[]).with(:ensure).returns :installed - - @provider = provider_class.new - @provider.stubs(:resource).returns @resource - end - - it "should have an install method" do - @provider = provider_class.new - @provider.should respond_to(:install) - end - - it "should have an uninstall method" do - @provider = provider_class.new - @provider.should respond_to(:uninstall) - end - - it "should have a swlist method" do - @provider = provider_class.new - @provider.should respond_to(:swlist) - end - - describe "when installing" do - it "should use a command-line like 'swinstall -x mount_all_filesystems=false -s SOURCE PACKAGE-NAME'" do - @provider.expects(:swinstall).with('-x', 'mount_all_filesystems=false', '-s', 'mysource', 'mypackage') - @provider.install - end + before(:each) do + # Create a mock resource + @resource = stub 'resource' + + # A catch all; no parameters set + @resource.stubs(:[]).returns(nil) + + # But set name and source + @resource.stubs(:[]).with(:name).returns "mypackage" + @resource.stubs(:[]).with(:source).returns "mysource" + @resource.stubs(:[]).with(:ensure).returns :installed + + @provider = provider_class.new + @provider.stubs(:resource).returns @resource + end + + it "should have an install method" do + @provider = provider_class.new + @provider.should respond_to(:install) + end + + it "should have an uninstall method" do + @provider = provider_class.new + @provider.should respond_to(:uninstall) + end + + it "should have a swlist method" do + @provider = provider_class.new + @provider.should respond_to(:swlist) + end + + describe "when installing" do + it "should use a command-line like 'swinstall -x mount_all_filesystems=false -s SOURCE PACKAGE-NAME'" do + @provider.expects(:swinstall).with('-x', 'mount_all_filesystems=false', '-s', 'mysource', 'mypackage') + @provider.install end + end - describe "when uninstalling" do - it "should use a command-line like 'swremove -x mount_all_filesystems=false PACKAGE-NAME'" do - @provider.expects(:swremove).with('-x', 'mount_all_filesystems=false', 'mypackage') - @provider.uninstall - end + describe "when uninstalling" do + it "should use a command-line like 'swremove -x mount_all_filesystems=false PACKAGE-NAME'" do + @provider.expects(:swremove).with('-x', 'mount_all_filesystems=false', 'mypackage') + @provider.uninstall end + end end diff --git a/spec/unit/provider/package/nim_spec.rb b/spec/unit/provider/package/nim_spec.rb index 5d9083b70..53a9cc839 100755 --- a/spec/unit/provider/package/nim_spec.rb +++ b/spec/unit/provider/package/nim_spec.rb @@ -5,38 +5,38 @@ require File.dirname(__FILE__) + '/../../../spec_helper' provider_class = Puppet::Type.type(:package).provider(:nim) describe provider_class do - before(:each) do - # Create a mock resource - @resource = stub 'resource' - - # A catch all; no parameters set - @resource.stubs(:[]).returns(nil) - - # But set name and source - @resource.stubs(:[]).with(:name).returns "mypackage" - @resource.stubs(:[]).with(:source).returns "mysource" - @resource.stubs(:[]).with(:ensure).returns :installed - - @provider = provider_class.new - @provider.resource = @resource - end - - it "should have an install method" do - @provider = provider_class.new - @provider.should respond_to(:install) + before(:each) do + # Create a mock resource + @resource = stub 'resource' + + # A catch all; no parameters set + @resource.stubs(:[]).returns(nil) + + # But set name and source + @resource.stubs(:[]).with(:name).returns "mypackage" + @resource.stubs(:[]).with(:source).returns "mysource" + @resource.stubs(:[]).with(:ensure).returns :installed + + @provider = provider_class.new + @provider.resource = @resource + end + + it "should have an install method" do + @provider = provider_class.new + @provider.should respond_to(:install) + end + + describe "when installing" do + it "should install a package" do + @resource.stubs(:should).with(:ensure).returns(:installed) + @provider.expects(:nimclient).with("-o", "cust", "-a", "installp_flags=acgwXY", "-a", "lpp_source=mysource", "-a", "filesets='mypackage'") + @provider.install end - describe "when installing" do - it "should install a package" do - @resource.stubs(:should).with(:ensure).returns(:installed) - @provider.expects(:nimclient).with("-o", "cust", "-a", "installp_flags=acgwXY", "-a", "lpp_source=mysource", "-a", "filesets='mypackage'") - @provider.install - end - - it "should install a versioned package" do - @resource.stubs(:should).with(:ensure).returns("1.2.3.4") - @provider.expects(:nimclient).with("-o", "cust", "-a", "installp_flags=acgwXY", "-a", "lpp_source=mysource", "-a", "filesets='mypackage 1.2.3.4'") - @provider.install - end + it "should install a versioned package" do + @resource.stubs(:should).with(:ensure).returns("1.2.3.4") + @provider.expects(:nimclient).with("-o", "cust", "-a", "installp_flags=acgwXY", "-a", "lpp_source=mysource", "-a", "filesets='mypackage 1.2.3.4'") + @provider.install end + end end diff --git a/spec/unit/provider/package/pkg_spec.rb b/spec/unit/provider/package/pkg_spec.rb index b29405e26..1544b8b7e 100644 --- a/spec/unit/provider/package/pkg_spec.rb +++ b/spec/unit/provider/package/pkg_spec.rb @@ -5,59 +5,59 @@ Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f provider = Puppet::Type.type(:package).provider(:pkg) describe provider do - before do - @resource = stub 'resource', :[] => "dummy" - @provider = provider.new(@resource) - - @fakeresult = "install ok installed dummy 1.0\n" - end - - def self.it_should_respond_to(*actions) - actions.each do |action| - it "should respond to :#{action}" do - @provider.should respond_to(action) - end - end - end - - it_should_respond_to :install, :uninstall, :update, :query, :latest - - it "should be versionable" do - provider.should_not be_versionable - end - - it "should use :install to update" do - @provider.expects(:install) - @provider.update - end - - it "should parse a line correctly" do - result = provider.parse_line("dummy 1.0@1.0-1.0 installed ----") - result.should == {:name => "dummy", :version => "1.0@1.0-1.0", - :ensure => :present, :status => "installed", - :provider => :pkg, :error => "ok"} - end - - it "should fail to parse an incorrect line" do - result = provider.parse_line("foo") - result.should be_nil - end - - it "should fail to list a missing package" do - @provider.expects(:pkg).with(:list, "-H", "dummy").returns "1" - @provider.query.should == {:status=>"missing", :ensure=>:absent, - :name=>"dummy", :error=>"ok"} - end - - it "should fail to list a package when it can't parse the output line" do - @provider.expects(:pkg).with(:list, "-H", "dummy").returns "failed" - @provider.query.should == {:status=>"missing", :ensure=>:absent, :name=>"dummy", :error=>"ok"} - end - - it "should list package correctly" do - @provider.expects(:pkg).with(:list, "-H", "dummy").returns "dummy 1.0@1.0-1.0 installed ----" - @provider.query.should == {:name => "dummy", :version => "1.0@1.0-1.0", - :ensure => :present, :status => "installed", - :provider => :pkg, :error => "ok"} - end + before do + @resource = stub 'resource', :[] => "dummy" + @provider = provider.new(@resource) + + @fakeresult = "install ok installed dummy 1.0\n" + end + + def self.it_should_respond_to(*actions) + actions.each do |action| + it "should respond to :#{action}" do + @provider.should respond_to(action) + end + end + end + + it_should_respond_to :install, :uninstall, :update, :query, :latest + + it "should be versionable" do + provider.should_not be_versionable + end + + it "should use :install to update" do + @provider.expects(:install) + @provider.update + end + + it "should parse a line correctly" do + result = provider.parse_line("dummy 1.0@1.0-1.0 installed ----") + result.should == {:name => "dummy", :version => "1.0@1.0-1.0", + :ensure => :present, :status => "installed", + :provider => :pkg, :error => "ok"} + end + + it "should fail to parse an incorrect line" do + result = provider.parse_line("foo") + result.should be_nil + end + + it "should fail to list a missing package" do + @provider.expects(:pkg).with(:list, "-H", "dummy").returns "1" + @provider.query.should == {:status=>"missing", :ensure=>:absent, + :name=>"dummy", :error=>"ok"} + end + + it "should fail to list a package when it can't parse the output line" do + @provider.expects(:pkg).with(:list, "-H", "dummy").returns "failed" + @provider.query.should == {:status=>"missing", :ensure=>:absent, :name=>"dummy", :error=>"ok"} + end + + it "should list package correctly" do + @provider.expects(:pkg).with(:list, "-H", "dummy").returns "dummy 1.0@1.0-1.0 installed ----" + @provider.query.should == {:name => "dummy", :version => "1.0@1.0-1.0", + :ensure => :present, :status => "installed", + :provider => :pkg, :error => "ok"} + end end diff --git a/spec/unit/provider/package/pkgdmg_spec.rb b/spec/unit/provider/package/pkgdmg_spec.rb index d4c5a9287..1fd5b4ac4 100755 --- a/spec/unit/provider/package/pkgdmg_spec.rb +++ b/spec/unit/provider/package/pkgdmg_spec.rb @@ -5,80 +5,80 @@ Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f provider = Puppet::Type.type(:package).provider(:pkgdmg) describe provider do - before do - @resource = stub 'resource', :[] => "dummypkgdmg" - @provider = provider.new(@resource) - - @fakemountpoint = "/tmp/dmg.foo" - @fakepkgfile = "/tmp/test.pkg" - @fakehdiutilinfo = {"system-entities" => [{"mount-point" => @fakemountpoint}] } - @fakehdiutilplist = Plist::Emit.dump(@fakehdiutilinfo) - - @hdiutilmountargs = ["mount", "-plist", "-nobrowse", "-readonly", - "-noidme", "-mountrandom", "/tmp"] + before do + @resource = stub 'resource', :[] => "dummypkgdmg" + @provider = provider.new(@resource) + + @fakemountpoint = "/tmp/dmg.foo" + @fakepkgfile = "/tmp/test.pkg" + @fakehdiutilinfo = {"system-entities" => [{"mount-point" => @fakemountpoint}] } + @fakehdiutilplist = Plist::Emit.dump(@fakehdiutilinfo) + + @hdiutilmountargs = ["mount", "-plist", "-nobrowse", "-readonly", + "-noidme", "-mountrandom", "/tmp"] + end + + it "should not be versionable" do + provider.versionable?.should be_false + end + + it "should not be uninstallable" do + provider.uninstallable?.should be_false + end + + describe "when installing it should fail when" do + it "no source is specified" do + @resource.stubs(:[]).with(:source).returns nil + lambda { @provider.install }.should raise_error(Puppet::Error) end - it "should not be versionable" do - provider.versionable?.should be_false + it "no name is specified" do + @resource.stubs(:[]).with(:name).returns nil + lambda { @provider.install }.should raise_error(Puppet::Error) end - it "should not be uninstallable" do - provider.uninstallable?.should be_false + it "the source does not end in .dmg or .pkg" do + @resource.stubs(:[]).with(:source).returns "notendingindotdmgorpkg" + lambda { @provider.install }.should raise_error(Puppet::Error) end - describe "when installing it should fail when" do - it "no source is specified" do - @resource.stubs(:[]).with(:source).returns nil - lambda { @provider.install }.should raise_error(Puppet::Error) - end - - it "no name is specified" do - @resource.stubs(:[]).with(:name).returns nil - lambda { @provider.install }.should raise_error(Puppet::Error) - end - - it "the source does not end in .dmg or .pkg" do - @resource.stubs(:[]).with(:source).returns "notendingindotdmgorpkg" - lambda { @provider.install }.should raise_error(Puppet::Error) - end - - it "a disk image with no system entities is mounted" do - @provider.stubs(:[]).with(:hdiutil).returns "" - lambda { @provider.install }.should raise_error(Puppet::Error) - end + it "a disk image with no system entities is mounted" do + @provider.stubs(:[]).with(:hdiutil).returns "" + lambda { @provider.install }.should raise_error(Puppet::Error) end + end - # These tests shouldn't be this messy. The pkgdmg provider needs work... - describe "when installing a pkgdmg" do - before do - fh = mock 'filehandle' - fh.stubs(:path).yields "/tmp/foo" - @resource.stubs(:[]).with(:source).returns "foo.dmg" - File.stubs(:open).yields fh - end - - it "should call hdiutil to mount and eject the disk image" do - Dir.stubs(:entries).returns [] - @provider.class.expects(:hdiutil).with("eject", @fakemountpoint).returns 0 - @provider.class.expects(:hdiutil).with("mount", "-plist", "-nobrowse", "-readonly", "-noidme", "-mountrandom", "/tmp", nil).returns @fakehdiutilplist - @provider.install - end + # These tests shouldn't be this messy. The pkgdmg provider needs work... + describe "when installing a pkgdmg" do + before do + fh = mock 'filehandle' + fh.stubs(:path).yields "/tmp/foo" + @resource.stubs(:[]).with(:source).returns "foo.dmg" + File.stubs(:open).yields fh + end - it "should call installpkg if a pkg/mpkg is found on the dmg" do - Dir.stubs(:entries).returns ["foo.pkg"] - @provider.class.stubs(:hdiutil).returns @fakehdiutilplist - @provider.class.expects(:installpkg).with("#{@fakemountpoint}/foo.pkg", @resource[:name], "foo.dmg").returns "" - @provider.install - end + it "should call hdiutil to mount and eject the disk image" do + Dir.stubs(:entries).returns [] + @provider.class.expects(:hdiutil).with("eject", @fakemountpoint).returns 0 + @provider.class.expects(:hdiutil).with("mount", "-plist", "-nobrowse", "-readonly", "-noidme", "-mountrandom", "/tmp", nil).returns @fakehdiutilplist + @provider.install end - describe "when installing flat pkg file" do - it "should call installpkg if a flat pkg file is found instead of a .dmg image" do - @resource.stubs(:[]).with(:source).returns "/tmp/test.pkg" - @resource.stubs(:[]).with(:name).returns "testpkg" - @provider.class.expects(:installpkgdmg).with("#{@fakepkgfile}", "testpkg").returns "" - @provider.install - end + it "should call installpkg if a pkg/mpkg is found on the dmg" do + Dir.stubs(:entries).returns ["foo.pkg"] + @provider.class.stubs(:hdiutil).returns @fakehdiutilplist + @provider.class.expects(:installpkg).with("#{@fakemountpoint}/foo.pkg", @resource[:name], "foo.dmg").returns "" + @provider.install end + end + + describe "when installing flat pkg file" do + it "should call installpkg if a flat pkg file is found instead of a .dmg image" do + @resource.stubs(:[]).with(:source).returns "/tmp/test.pkg" + @resource.stubs(:[]).with(:name).returns "testpkg" + @provider.class.expects(:installpkgdmg).with("#{@fakepkgfile}", "testpkg").returns "" + @provider.install + end + end end diff --git a/spec/unit/provider/package/zypper_spec.rb b/spec/unit/provider/package/zypper_spec.rb index b7f3687c6..5ac13b659 100644 --- a/spec/unit/provider/package/zypper_spec.rb +++ b/spec/unit/provider/package/zypper_spec.rb @@ -5,77 +5,77 @@ require File.dirname(__FILE__) + '/../../../spec_helper' provider_class = Puppet::Type.type(:package).provider(:zypper) describe provider_class do - before(:each) do - # Create a mock resource - @resource = stub 'resource' - - # A catch all; no parameters set - @resource.stubs(:[]).returns(nil) - - # But set name and source - @resource.stubs(:[]).with(:name).returns "mypackage" - @resource.stubs(:[]).with(:ensure).returns :installed - @resource.stubs(:command).with(:zypper).returns "/usr/bin/zypper" - - @provider = provider_class.new(@resource) - end - - it "should have an install method" do - @provider = provider_class.new - @provider.should respond_to(:install) - end - - it "should have a latest method" do - @provider = provider_class.new - @provider.should respond_to(:uninstall) - end - - it "should have an update method" do - @provider = provider_class.new - @provider.should respond_to(:update) - end - - it "should have a latest method" do - @provider = provider_class.new - @provider.should respond_to(:latest) + before(:each) do + # Create a mock resource + @resource = stub 'resource' + + # A catch all; no parameters set + @resource.stubs(:[]).returns(nil) + + # But set name and source + @resource.stubs(:[]).with(:name).returns "mypackage" + @resource.stubs(:[]).with(:ensure).returns :installed + @resource.stubs(:command).with(:zypper).returns "/usr/bin/zypper" + + @provider = provider_class.new(@resource) + end + + it "should have an install method" do + @provider = provider_class.new + @provider.should respond_to(:install) + end + + it "should have a latest method" do + @provider = provider_class.new + @provider.should respond_to(:uninstall) + end + + it "should have an update method" do + @provider = provider_class.new + @provider.should respond_to(:update) + end + + it "should have a latest method" do + @provider = provider_class.new + @provider.should respond_to(:latest) + end + + describe "when installing" do + it "should use a command-line with versioned package'" do + @resource.stubs(:should).with(:ensure).returns "1.2.3-4.5.6" + @provider.expects(:zypper).with('--quiet', :install, '-l', '-y', 'mypackage-1.2.3-4.5.6') + @provider.expects(:query).returns "mypackage 0 1.2.3 4.5.6 x86_64" + @provider.install end - describe "when installing" do - it "should use a command-line with versioned package'" do - @resource.stubs(:should).with(:ensure).returns "1.2.3-4.5.6" - @provider.expects(:zypper).with('--quiet', :install, '-l', '-y', 'mypackage-1.2.3-4.5.6') - @provider.expects(:query).returns "mypackage 0 1.2.3 4.5.6 x86_64" - @provider.install - end - - it "should use a command-line without versioned package" do - @resource.stubs(:should).with(:ensure).returns :latest - @provider.expects(:zypper).with('--quiet', :install, '-l', '-y', 'mypackage') - @provider.expects(:query).returns "mypackage 0 1.2.3 4.5.6 x86_64" - @provider.install - end + it "should use a command-line without versioned package" do + @resource.stubs(:should).with(:ensure).returns :latest + @provider.expects(:zypper).with('--quiet', :install, '-l', '-y', 'mypackage') + @provider.expects(:query).returns "mypackage 0 1.2.3 4.5.6 x86_64" + @provider.install end + end - describe "when updating" do - it "should call install method of instance" do - @provider.expects(:install) - @provider.update - end + describe "when updating" do + it "should call install method of instance" do + @provider.expects(:install) + @provider.update end + end - describe "when getting latest version" do - it "should return a version string" do + describe "when getting latest version" do + it "should return a version string" do - fake_data = "Loading repository data... + fake_data = "Loading repository data... Reading installed packages... S | Repository | Name | Version | Arch --+----------------+-----------------------+-----------------+------- v | SLES11-Updates | cups | 1.1.1 | x86_64 v | SLES11-Updates | mypackage | 1.3.9h-8.20.1 | x86_64" - @provider.expects(:zypper).with("list-updates").returns fake_data - @provider.latest.should == "1.3.9h-8.20.1" - end + @provider.expects(:zypper).with("list-updates").returns fake_data + @provider.latest.should == "1.3.9h-8.20.1" end + end end diff --git a/spec/unit/provider/parsedfile_spec.rb b/spec/unit/provider/parsedfile_spec.rb index f20b6b235..d40f77370 100755 --- a/spec/unit/provider/parsedfile_spec.rb +++ b/spec/unit/provider/parsedfile_spec.rb @@ -6,90 +6,90 @@ require 'puppet/provider/parsedfile' # Most of the tests for this are still in test/ral/provider/parsedfile.rb. describe Puppet::Provider::ParsedFile do - before do - @class = Class.new(Puppet::Provider::ParsedFile) - end + before do + @class = Class.new(Puppet::Provider::ParsedFile) + end - describe "when looking up records loaded from disk" do - it "should return nil if no records have been loaded" do - @class.record?("foo").should be_nil - end + describe "when looking up records loaded from disk" do + it "should return nil if no records have been loaded" do + @class.record?("foo").should be_nil + end + end + + describe "when generating a list of instances" do + it "should return an instance for each record parsed from all of the registered targets" do + @class.expects(:targets).returns %w{/one /two} + @class.stubs(:skip_record?).returns false + one = [:uno1, :uno2] + two = [:dos1, :dos2] + @class.expects(:prefetch_target).with("/one").returns one + @class.expects(:prefetch_target).with("/two").returns two + + results = [] + (one + two).each do |inst| + results << inst.to_s + "_instance" + @class.expects(:new).with(inst).returns(results[-1]) + end + + @class.instances.should == results end - describe "when generating a list of instances" do - it "should return an instance for each record parsed from all of the registered targets" do - @class.expects(:targets).returns %w{/one /two} - @class.stubs(:skip_record?).returns false - one = [:uno1, :uno2] - two = [:dos1, :dos2] - @class.expects(:prefetch_target).with("/one").returns one - @class.expects(:prefetch_target).with("/two").returns two - - results = [] - (one + two).each do |inst| - results << inst.to_s + "_instance" - @class.expects(:new).with(inst).returns(results[-1]) - end - - @class.instances.should == results - end - - it "should skip specified records" do - @class.expects(:targets).returns %w{/one} - @class.expects(:skip_record?).with(:uno).returns false - @class.expects(:skip_record?).with(:dos).returns true - one = [:uno, :dos] - @class.expects(:prefetch_target).returns one - - @class.expects(:new).with(:uno).returns "eh" - @class.expects(:new).with(:dos).never - - @class.instances - end + it "should skip specified records" do + @class.expects(:targets).returns %w{/one} + @class.expects(:skip_record?).with(:uno).returns false + @class.expects(:skip_record?).with(:dos).returns true + one = [:uno, :dos] + @class.expects(:prefetch_target).returns one + + @class.expects(:new).with(:uno).returns "eh" + @class.expects(:new).with(:dos).never + + @class.instances end + end - describe "when flushing a file's records to disk" do - before do - # This way we start with some @records, like we would in real life. - @class.stubs(:retrieve).returns [] - @class.default_target = "/foo/bar" - @class.initvars - @class.prefetch + describe "when flushing a file's records to disk" do + before do + # This way we start with some @records, like we would in real life. + @class.stubs(:retrieve).returns [] + @class.default_target = "/foo/bar" + @class.initvars + @class.prefetch - @filetype = Puppet::Util::FileType.filetype(:flat).new("/my/file") - Puppet::Util::FileType.filetype(:flat).stubs(:new).with("/my/file").returns @filetype + @filetype = Puppet::Util::FileType.filetype(:flat).new("/my/file") + Puppet::Util::FileType.filetype(:flat).stubs(:new).with("/my/file").returns @filetype - @filetype.stubs(:write) - end + @filetype.stubs(:write) + end - it "should back up the file being written if the filetype can be backed up" do - @filetype.expects(:backup) + it "should back up the file being written if the filetype can be backed up" do + @filetype.expects(:backup) - @class.flush_target("/my/file") - end + @class.flush_target("/my/file") + end - it "should not try to back up the file if the filetype cannot be backed up" do - @filetype = Puppet::Util::FileType.filetype(:ram).new("/my/file") - Puppet::Util::FileType.filetype(:flat).expects(:new).returns @filetype + it "should not try to back up the file if the filetype cannot be backed up" do + @filetype = Puppet::Util::FileType.filetype(:ram).new("/my/file") + Puppet::Util::FileType.filetype(:flat).expects(:new).returns @filetype - @filetype.stubs(:write) + @filetype.stubs(:write) - @class.flush_target("/my/file") - end + @class.flush_target("/my/file") + end - it "should not back up the file more than once between calls to 'prefetch'" do - @filetype.expects(:backup).once + it "should not back up the file more than once between calls to 'prefetch'" do + @filetype.expects(:backup).once - @class.flush_target("/my/file") - @class.flush_target("/my/file") - end + @class.flush_target("/my/file") + @class.flush_target("/my/file") + end - it "should back the file up again once the file has been reread" do - @filetype.expects(:backup).times(2) + it "should back the file up again once the file has been reread" do + @filetype.expects(:backup).times(2) - @class.flush_target("/my/file") - @class.prefetch - @class.flush_target("/my/file") - end + @class.flush_target("/my/file") + @class.prefetch + @class.flush_target("/my/file") end + end end diff --git a/spec/unit/provider/selboolean_spec.rb b/spec/unit/provider/selboolean_spec.rb index 2004485ca..b37b44b45 100755 --- a/spec/unit/provider/selboolean_spec.rb +++ b/spec/unit/provider/selboolean_spec.rb @@ -5,33 +5,33 @@ Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f 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 + 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_spec.rb b/spec/unit/provider/selmodule_spec.rb index ed20ea97f..fda6d0d78 100755 --- a/spec/unit/provider/selmodule_spec.rb +++ b/spec/unit/provider/selmodule_spec.rb @@ -10,57 +10,57 @@ Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f 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 + before :each do + @resource = stub("resource", :name => "foo") + @resource.stubs(:[]).returns "foo" + @provider = provider_class.new(@resource) + 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 + 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 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 + 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 - 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 + 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 "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 + 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 - 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 + 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 before checking file version if no loaded policy" do - @provider.expects(:selmodversion_loaded).returns nil - @provider.syncversion.should == :false - 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/service/daemontools_spec.rb b/spec/unit/provider/service/daemontools_spec.rb index 2d61d2322..5e8b8d055 100755 --- a/spec/unit/provider/service/daemontools_spec.rb +++ b/spec/unit/provider/service/daemontools_spec.rb @@ -10,157 +10,157 @@ provider_class = Puppet::Type.type(:service).provider(:daemontools) describe provider_class do - before(:each) do - # Create a mock resource - @resource = stub 'resource' + 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 + @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) + # 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]" + # 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.resource = @resource + @provider.resource = @resource - @provider.stubs(:command).with(:svc).returns "svc" - @provider.stubs(:command).with(:svstat).returns "svstat" + @provider.stubs(:command).with(:svc).returns "svc" + @provider.stubs(:command).with(:svstat).returns "svstat" - @provider.stubs(:svc) - @provider.stubs(:svstat) - end + @provider.stubs(:svc) + @provider.stubs(:svstat) + end - it "should have a restart method" do - @provider.should respond_to(:restart) - end + it "should have a restart method" do + @provider.should respond_to(:restart) + end - it "should have a start method" do - @provider.should respond_to(:start) - 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 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 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 an enable method" do + @provider.should respond_to(:enable) + end - it "should have a disable method" do - @provider.should respond_to(:disable) - end + it "should have a disable method" do + @provider.should respond_to(:disable) + end - describe "when starting" do - it "should use 'svc' to start the service" do - @provider.stubs(:enabled?).returns :true - @provider.expects(:svc).with("-u", "/etc/service/myservice") + describe "when starting" do + it "should use 'svc' to start the service" do + @provider.stubs(:enabled?).returns :true + @provider.expects(:svc).with("-u", "/etc/service/myservice") - @provider.start - end + @provider.start + end - it "should enable the service if it is not enabled" do - @provider.stubs(:svc) + it "should enable the service if it is not enabled" do + @provider.stubs(:svc) - @provider.expects(:enabled?).returns :false - @provider.expects(:enable) + @provider.expects(:enabled?).returns :false + @provider.expects(:enable) - @provider.start - end + @provider.start end + end - describe "when stopping" do - it "should use 'svc' to stop the service" do - @provider.stubs(:disable) - @provider.expects(:svc).with("-d", "/etc/service/myservice") + describe "when stopping" do + it "should use 'svc' to stop the service" do + @provider.stubs(:disable) + @provider.expects(:svc).with("-d", "/etc/service/myservice") - @provider.stop - end + @provider.stop end + end - describe "when restarting" do - it "should use 'svc' to restart the service" do - @provider.expects(:svc).with("-t", "/etc/service/myservice") + describe "when restarting" do + it "should use 'svc' to restart the service" do + @provider.expects(:svc).with("-t", "/etc/service/myservice") - @provider.restart - end + @provider.restart 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 + 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 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")) + @provider.stubs(:texecute).returns("") + @provider.disable end - describe "when disabling" do - it "should 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")) - @provider.stubs(:texecute).returns("") - @provider.disable - end - - it "should stop the service" do - FileTest.stubs(:directory?).returns(false) - FileTest.stubs(:symlink?).returns(true) - File.stubs(:unlink) - @provider.expects(:stop) - @provider.disable - end + it "should stop the service" do + FileTest.stubs(:directory?).returns(false) + FileTest.stubs(:symlink?).returns(true) + File.stubs(:unlink) + @provider.expects(:stop) + @provider.disable end + end - describe "when checking if the service is enabled?" do - it "should return true if it is running" do - @provider.stubs(:status).returns(:running) + describe "when checking if the service is enabled?" do + it "should return true if it is running" do + @provider.stubs(:status).returns(:running) - @provider.enabled?.should == :true - end + @provider.enabled?.should == :true + end - [true, false].each do |t| - it "should return #{t} if the symlink exists" do - @provider.stubs(:status).returns(:stopped) - FileTest.stubs(:symlink?).returns(t) + [true, false].each do |t| + it "should return #{t} if the symlink exists" do + @provider.stubs(:status).returns(:stopped) + FileTest.stubs(:symlink?).returns(t) - @provider.enabled?.should == "#{t}".to_sym - end - end + @provider.enabled?.should == "#{t}".to_sym + end 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 + 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 + 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/debian_spec.rb b/spec/unit/provider/service/debian_spec.rb index 1d30eb821..8dee2ee94 100755 --- a/spec/unit/provider/service/debian_spec.rb +++ b/spec/unit/provider/service/debian_spec.rb @@ -9,81 +9,81 @@ provider_class = Puppet::Type.type(:service).provider(:debian) describe provider_class do - before(:each) do - # Create a mock resource - @resource = stub 'resource' + before(:each) do + # Create a mock resource + @resource = stub 'resource' - @provider = provider_class.new + @provider = provider_class.new - # A catch all; no parameters set - @resource.stubs(:[]).returns(nil) + # A catch all; no parameters set + @resource.stubs(:[]).returns(nil) - # But set name, source and path - @resource.stubs(:[]).with(:name).returns "myservice" - @resource.stubs(:[]).with(:ensure).returns :enabled - @resource.stubs(:ref).returns "Service[myservice]" + # But set name, source and path + @resource.stubs(:[]).with(:name).returns "myservice" + @resource.stubs(:[]).with(:ensure).returns :enabled + @resource.stubs(:ref).returns "Service[myservice]" - @provider.resource = @resource + @provider.resource = @resource - @provider.stubs(:command).with(:update_rc).returns "update_rc" - @provider.stubs(:command).with(:invoke_rc).returns "invoke_rc" + @provider.stubs(:command).with(:update_rc).returns "update_rc" + @provider.stubs(:command).with(:invoke_rc).returns "invoke_rc" - @provider.stubs(:update_rc) - @provider.stubs(:invoke_rc) - end + @provider.stubs(:update_rc) + @provider.stubs(:invoke_rc) + 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 - it "should have an enabled? method" do - @provider.should respond_to(:enabled?) + describe "when enabling" do + it "should call update-rc.d twice" do + @provider.expects(:update_rc).twice + @provider.enable end + end - it "should have an enable method" do - @provider.should respond_to(:enable) + describe "when disabling" do + it "should call update-rc.d twice" do + @provider.expects(:update_rc).twice + @provider.disable end + end - it "should have a disable method" do - @provider.should respond_to(:disable) + describe "when checking whether it is enabled" do + it "should call Kernel.system() with the appropriate parameters" do + @provider.expects(:system).with("/usr/sbin/invoke-rc.d", "--quiet", "--query", @resource[:name], "start").once + @provider.enabled? end - describe "when enabling" do - it "should call update-rc.d twice" do - @provider.expects(:update_rc).twice - @provider.enable - end + it "should return true when invoke-rc.d exits with 104 status" do + @provider.stubs(:system) + $CHILD_STATUS.stubs(:exitstatus).returns(104) + @provider.enabled?.should == :true end - describe "when disabling" do - it "should call update-rc.d twice" do - @provider.expects(:update_rc).twice - @provider.disable - end + it "should return true when invoke-rc.d exits with 106 status" do + @provider.stubs(:system) + $CHILD_STATUS.stubs(:exitstatus).returns(106) + @provider.enabled?.should == :true end - describe "when checking whether it is enabled" do - it "should call Kernel.system() with the appropriate parameters" do - @provider.expects(:system).with("/usr/sbin/invoke-rc.d", "--quiet", "--query", @resource[:name], "start").once - @provider.enabled? - end - - it "should return true when invoke-rc.d exits with 104 status" do - @provider.stubs(:system) - $CHILD_STATUS.stubs(:exitstatus).returns(104) - @provider.enabled?.should == :true - end - - it "should return true when invoke-rc.d exits with 106 status" do - @provider.stubs(:system) - $CHILD_STATUS.stubs(:exitstatus).returns(106) - @provider.enabled?.should == :true - end - - # pick a range of non-[104.106] numbers, strings and booleans to test with. - [-100, -1, 0, 1, 100, "foo", "", :true, :false].each do |exitstatus| - it "should return false when invoke-rc.d exits with #{exitstatus} status" do - @provider.stubs(:system) - $CHILD_STATUS.stubs(:exitstatus).returns(exitstatus) - @provider.enabled?.should == :false - end - end + # pick a range of non-[104.106] numbers, strings and booleans to test with. + [-100, -1, 0, 1, 100, "foo", "", :true, :false].each do |exitstatus| + it "should return false when invoke-rc.d exits with #{exitstatus} status" do + @provider.stubs(:system) + $CHILD_STATUS.stubs(:exitstatus).returns(exitstatus) + @provider.enabled?.should == :false + end end + end end diff --git a/spec/unit/provider/service/init_spec.rb b/spec/unit/provider/service/init_spec.rb index b8c279473..bbc88ff76 100755 --- a/spec/unit/provider/service/init_spec.rb +++ b/spec/unit/provider/service/init_spec.rb @@ -9,160 +9,160 @@ provider_class = Puppet::Type.type(:service).provider(:init) describe provider_class do - before :each do - @class = Puppet::Type.type(:service).provider(:init) - @resource = stub 'resource' - @resource.stubs(:[]).returns(nil) - @resource.stubs(:[]).with(:name).returns "myservice" + before :each do + @class = Puppet::Type.type(:service).provider(:init) + @resource = stub 'resource' + @resource.stubs(:[]).returns(nil) + @resource.stubs(:[]).with(:name).returns "myservice" # @resource.stubs(:[]).with(:ensure).returns :enabled - @resource.stubs(:[]).with(:path).returns ["/service/path","/alt/service/path"] + @resource.stubs(:[]).with(:path).returns ["/service/path","/alt/service/path"] # @resource.stubs(:ref).returns "Service[myservice]" - File.stubs(:directory?).returns(true) + File.stubs(:directory?).returns(true) + + @provider = provider_class.new + @provider.resource = @resource + end - @provider = provider_class.new - @provider.resource = @resource + describe "when getting all service instances" do + before :each do + @services = ['one', 'two', 'three', 'four'] + Dir.stubs(:entries).returns @services + FileTest.stubs(:directory?).returns(true) + FileTest.stubs(:executable?).returns(true) + @class.stubs(:defpath).returns('tmp') + end + it "should return instances for all services" do + @services.each do |inst| + @class.expects(:new).with{|hash| hash[:name] == inst}.returns("#{inst}_instance") + end + results = @services.collect {|x| "#{x}_instance"} + @class.instances.should == results + end + it "should omit an array of services from exclude list" do + exclude = ['two', 'four'] + (@services-exclude).each do |inst| + @class.expects(:new).with{|hash| hash[:name] == inst}.returns("#{inst}_instance") + end + results = (@services-exclude).collect {|x| "#{x}_instance"} + @class.get_services(@class.defpath, exclude).should == results end + it "should omit a single service from the exclude list" do + exclude = 'two' + (@services-exclude.to_a).each do |inst| + @class.expects(:new).with{|hash| hash[:name] == inst}.returns("#{inst}_instance") + end + results = @services.reject{|x| x==exclude }.collect {|x| "#{x}_instance"} + @class.get_services(@class.defpath, exclude).should == results + end + it "should use defpath" do + @services.each do |inst| + @class.expects(:new).with{|hash| hash[:path] == @class.defpath}.returns("#{inst}_instance") + end + results = @services.sort.collect {|x| "#{x}_instance"} + @class.instances.sort.should == results + end + it "should set hasstatus to true for providers" do + @services.each do |inst| + @class.expects(:new).with{|hash| hash[:name] == inst && hash[:hasstatus] == true}.returns("#{inst}_instance") + end + results = @services.collect {|x| "#{x}_instance"} + @class.instances.should == results + end + end - describe "when getting all service instances" do - before :each do - @services = ['one', 'two', 'three', 'four'] - Dir.stubs(:entries).returns @services - FileTest.stubs(:directory?).returns(true) - FileTest.stubs(:executable?).returns(true) - @class.stubs(:defpath).returns('tmp') - end - it "should return instances for all services" do - @services.each do |inst| - @class.expects(:new).with{|hash| hash[:name] == inst}.returns("#{inst}_instance") - end - results = @services.collect {|x| "#{x}_instance"} - @class.instances.should == results - end - it "should omit an array of services from exclude list" do - exclude = ['two', 'four'] - (@services-exclude).each do |inst| - @class.expects(:new).with{|hash| hash[:name] == inst}.returns("#{inst}_instance") - end - results = (@services-exclude).collect {|x| "#{x}_instance"} - @class.get_services(@class.defpath, exclude).should == results - end - it "should omit a single service from the exclude list" do - exclude = 'two' - (@services-exclude.to_a).each do |inst| - @class.expects(:new).with{|hash| hash[:name] == inst}.returns("#{inst}_instance") - end - results = @services.reject{|x| x==exclude }.collect {|x| "#{x}_instance"} - @class.get_services(@class.defpath, exclude).should == results - end - it "should use defpath" do - @services.each do |inst| - @class.expects(:new).with{|hash| hash[:path] == @class.defpath}.returns("#{inst}_instance") - end - results = @services.sort.collect {|x| "#{x}_instance"} - @class.instances.sort.should == results - end - it "should set hasstatus to true for providers" do - @services.each do |inst| - @class.expects(:new).with{|hash| hash[:name] == inst && hash[:hasstatus] == true}.returns("#{inst}_instance") - end - results = @services.collect {|x| "#{x}_instance"} - @class.instances.should == results - end + describe "when searching for the init script" do + it "should discard paths that do not exist" do + File.stubs(:exist?).returns(false) + File.stubs(:directory?).returns(false) + @provider.paths.should be_empty end - describe "when searching for the init script" do - it "should discard paths that do not exist" do - File.stubs(:exist?).returns(false) - File.stubs(:directory?).returns(false) - @provider.paths.should be_empty - end + it "should discard paths that are not directories" do + File.stubs(:exist?).returns(true) + File.stubs(:directory?).returns(false) + @provider.paths.should be_empty + end - it "should discard paths that are not directories" do - File.stubs(:exist?).returns(true) - File.stubs(:directory?).returns(false) - @provider.paths.should be_empty - end + it "should be able to find the init script in the service path" do + File.expects(:stat).with("/service/path/myservice").returns true + @provider.initscript.should == "/service/path/myservice" + end + it "should be able to find the init script in the service path" do + File.expects(:stat).with("/alt/service/path/myservice").returns true + @provider.initscript.should == "/alt/service/path/myservice" + end + it "should fail if the service isn't there" do + lambda { @provider.initscript }.should raise_error(Puppet::Error, "Could not find init script for 'myservice'") + end + end - it "should be able to find the init script in the service path" do - File.expects(:stat).with("/service/path/myservice").returns true - @provider.initscript.should == "/service/path/myservice" - end - it "should be able to find the init script in the service path" do - File.expects(:stat).with("/alt/service/path/myservice").returns true - @provider.initscript.should == "/alt/service/path/myservice" + describe "if the init script is present" do + before :each do + File.stubs(:stat).with("/service/path/myservice").returns true + end + + [:start, :stop, :status, :restart].each do |method| + it "should have a #{method} method" do + @provider.should respond_to(method) + end + describe "when running #{method}" do + + it "should use any provided explicit command" do + @resource.stubs(:[]).with(method).returns "/user/specified/command" + @provider.expects(:execute).with { |command, *args| command == ["/user/specified/command"] } + @provider.send(method) end - it "should fail if the service isn't there" do - lambda { @provider.initscript }.should raise_error(Puppet::Error, "Could not find init script for 'myservice'") + + it "should pass #{method} to the init script when no explicit command is provided" do + @resource.stubs(:[]).with("has#{method}".intern).returns :true + @provider.expects(:execute).with { |command, *args| command == ["/service/path/myservice",method]} + @provider.send(method) end + end end - describe "if the init script is present" do + describe "when checking status" do + describe "when hasstatus is :true" do before :each do - File.stubs(:stat).with("/service/path/myservice").returns true + @resource.stubs(:[]).with(:hasstatus).returns :true end - - [:start, :stop, :status, :restart].each do |method| - it "should have a #{method} method" do - @provider.should respond_to(method) - end - describe "when running #{method}" do - - it "should use any provided explicit command" do - @resource.stubs(:[]).with(method).returns "/user/specified/command" - @provider.expects(:execute).with { |command, *args| command == ["/user/specified/command"] } - @provider.send(method) - end - - it "should pass #{method} to the init script when no explicit command is provided" do - @resource.stubs(:[]).with("has#{method}".intern).returns :true - @provider.expects(:execute).with { |command, *args| command == ["/service/path/myservice",method]} - @provider.send(method) - end - end + it "should execute the command" do + @provider.expects(:texecute).with(:status, ['/service/path/myservice', :status], false).returns("") + @provider.status end - - describe "when checking status" do - describe "when hasstatus is :true" do - before :each do - @resource.stubs(:[]).with(:hasstatus).returns :true - end - it "should execute the command" do - @provider.expects(:texecute).with(:status, ['/service/path/myservice', :status], false).returns("") - @provider.status - end - it "should consider the process running if the command returns 0" do - @provider.expects(:texecute).with(:status, ['/service/path/myservice', :status], false).returns("") - $CHILD_STATUS.stubs(:exitstatus).returns(0) - @provider.status.should == :running - end - [-10,-1,1,10].each { |ec| - it "should consider the process stopped if the command returns something non-0" do - @provider.expects(:texecute).with(:status, ['/service/path/myservice', :status], false).returns("") - $CHILD_STATUS.stubs(:exitstatus).returns(ec) - @provider.status.should == :stopped - end - } - end - describe "when hasstatus is not :true" do - it "should consider the service :running if it has a pid" do - @provider.expects(:getpid).returns "1234" - @provider.status.should == :running - end - it "should consider the service :stopped if it doesn't have a pid" do - @provider.expects(:getpid).returns nil - @provider.status.should == :stopped - end - end + it "should consider the process running if the command returns 0" do + @provider.expects(:texecute).with(:status, ['/service/path/myservice', :status], false).returns("") + $CHILD_STATUS.stubs(:exitstatus).returns(0) + @provider.status.should == :running end - - describe "when restarting and hasrestart is not :true" do - it "should stop and restart the process" do - @provider.expects(:texecute).with(:stop, ['/service/path/myservice', :stop ], true).returns("") - @provider.expects(:texecute).with(:start,['/service/path/myservice', :start], true).returns("") - $CHILD_STATUS.stubs(:exitstatus).returns(0) - @provider.restart - end + [-10,-1,1,10].each { |ec| + it "should consider the process stopped if the command returns something non-0" do + @provider.expects(:texecute).with(:status, ['/service/path/myservice', :status], false).returns("") + $CHILD_STATUS.stubs(:exitstatus).returns(ec) + @provider.status.should == :stopped + end + } + end + describe "when hasstatus is not :true" do + it "should consider the service :running if it has a pid" do + @provider.expects(:getpid).returns "1234" + @provider.status.should == :running + end + it "should consider the service :stopped if it doesn't have a pid" do + @provider.expects(:getpid).returns nil + @provider.status.should == :stopped end + end + end + describe "when restarting and hasrestart is not :true" do + it "should stop and restart the process" do + @provider.expects(:texecute).with(:stop, ['/service/path/myservice', :stop ], true).returns("") + @provider.expects(:texecute).with(:start,['/service/path/myservice', :start], true).returns("") + $CHILD_STATUS.stubs(:exitstatus).returns(0) + @provider.restart + end end + + end end diff --git a/spec/unit/provider/service/launchd_spec.rb b/spec/unit/provider/service/launchd_spec.rb index 62570e2a8..320ee3ace 100755 --- a/spec/unit/provider/service/launchd_spec.rb +++ b/spec/unit/provider/service/launchd_spec.rb @@ -11,190 +11,190 @@ provider_class = Puppet::Type.type(:service).provider(:launchd) describe provider_class do - before :each do - # Create a mock resource - @resource = stub 'resource' + before :each do + # Create a mock resource + @resource = stub 'resource' - @provider = provider_class.new - @joblabel = "com.foo.food" - @jobplist = {} + @provider = provider_class.new + @joblabel = "com.foo.food" + @jobplist = {} + + # A catch all; no parameters set + @resource.stubs(:[]).returns(nil) - # A catch all; no parameters set - @resource.stubs(:[]).returns(nil) - - # But set name, ensure and enable - @resource.stubs(:[]).with(:name).returns @joblabel - @resource.stubs(:[]).with(:ensure).returns :enabled - @resource.stubs(:[]).with(:enable).returns :true - @resource.stubs(:ref).returns "Service[#{@joblabel}]" - - # stub out the provider methods that actually touch the filesystem - # or execute commands - @provider.stubs(:plist_from_label).returns([@joblabel, @jobplist]) - @provider.stubs(:execute).returns("") - @provider.stubs(:resource).returns @resource - - # We stub this out for the normal case as 10.6 is "special". - provider_class.stubs(:get_macosx_version_major).returns("10.5") - - end - - it "should have a start method for #{@provider.object_id}" 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 - - it "should have a status method" do - @provider.should respond_to(:status) - end - - - describe "when checking status" do - it "should call the external command 'launchctl list' once" do - @provider.expects(:launchctl).with(:list).returns("rotating-strawberry-madonnas") - @provider.status - end - it "should return stopped if not listed in launchctl list output" do - @provider.stubs(:launchctl).with(:list).returns("rotating-strawberry-madonnas") - @provider.status.should == :stopped - end - it "should return running if listed in launchctl list output" do - @provider.stubs(:launchctl).with(:list).returns(@joblabel) - @provider.status.should == :running - end - end - - describe "when checking whether the service is enabled" do - it "should return true if the job plist says disabled is false" do - @provider.stubs(:plist_from_label).returns(["foo", {"Disabled" => false}]) - @provider.enabled?.should == :true - end - it "should return true if the job plist has no disabled key" do - @provider.stubs(:plist_from_label).returns(["foo", {}]) - @provider.enabled?.should == :true - end - it "should return false if the job plist says disabled is true" do - @provider.stubs(:plist_from_label).returns(["foo", {"Disabled" => true}]) - @provider.enabled?.should == :false - end - end - - describe "when checking whether the service is enabled on OS X 10.6" do - it "should return true if the job plist says disabled is true and the global overrides says disabled is false" do - provider_class.stubs(:get_macosx_version_major).returns("10.6") - @provider.stubs(:plist_from_label).returns(["foo", {"Disabled" => true}]) - Plist.stubs(:parse_xml).returns({@resource[:name] => {"Disabled" => false}}) - @provider.enabled?.should == :true - end - it "should return false if the job plist says disabled is false and the global overrides says disabled is true" do - provider_class.stubs(:get_macosx_version_major).returns("10.6") - @provider.stubs(:plist_from_label).returns(["foo", {"Disabled" => false}]) - Plist.stubs(:parse_xml).returns({@resource[:name] => {"Disabled" => true}}) - @provider.enabled?.should == :false - end - it "should return true if the job plist and the global overrides have no disabled keys" do - provider_class.stubs(:get_macosx_version_major).returns("10.6") - @provider.stubs(:plist_from_label).returns(["foo", {}]) - Plist.stubs(:parse_xml).returns({}) - @provider.enabled?.should == :true - end - end - - describe "when starting the service" do - it "should look for the relevant plist once" do - @provider.expects(:plist_from_label).once - @provider.start - end - it "should execute 'launchctl load' once without writing to the plist if the job is enabled" do - @provider.stubs(:enabled?).returns :true - @provider.expects(:execute).with([:launchctl, :load, @resource[:name]]).once - @provider.start - end - it "should execute 'launchctl load' with writing to the plist once if the job is disabled" do - @provider.stubs(:enabled?).returns :false - @provider.expects(:execute).with([:launchctl, :load, "-w", @resource[:name]]).once - @provider.start - end - it "should disable the job once if the job is disabled and should be disabled at boot" do - @provider.stubs(:enabled?).returns :false - @resource.stubs(:[]).with(:enable).returns :false - @provider.expects(:disable).once - @provider.start - end - end - - describe "when stopping the service" do - it "should look for the relevant plist once" do - @provider.expects(:plist_from_label).once - @provider.stop - end - it "should execute 'launchctl unload' once without writing to the plist if the job is disabled" do - @provider.stubs(:enabled?).returns :false - @provider.expects(:execute).with([:launchctl, :unload, @resource[:name]]).once - @provider.stop - end - it "should execute 'launchctl unload' with writing to the plist once if the job is enabled" do - @provider.stubs(:enabled?).returns :true - @provider.expects(:execute).with([:launchctl, :unload, "-w", @resource[:name]]).once - @provider.stop - end - it "should enable the job once if the job is enabled and should be enabled at boot" do - @provider.stubs(:enabled?).returns :true - @resource.stubs(:[]).with(:enable).returns :true - @provider.expects(:enable).once - @provider.stop - end - end - - describe "when enabling the service" do - it "should look for the relevant plist once" do - @provider.expects(:plist_from_label).once - @provider.stop - end - it "should check if the job is enabled once" do - @provider.expects(:enabled?).once - @provider.stop - end - end - - describe "when disabling the service" do - it "should look for the relevant plist once" do - @provider.expects(:plist_from_label).once - @provider.stop - end - end - - describe "when enabling the service on OS X 10.6" do - it "should write to the global launchd overrides file once" do - provider_class.stubs(:get_macosx_version_major).returns("10.6") - Plist.stubs(:parse_xml).returns({}) - Plist::Emit.expects(:save_plist).once - @provider.enable - end - end - - describe "when disabling the service on OS X 10.6" do - it "should write to the global launchd overrides file once" do - provider_class.stubs(:get_macosx_version_major).returns("10.6") - Plist.stubs(:parse_xml).returns({}) - Plist::Emit.expects(:save_plist).once - @provider.enable - end - end + # But set name, ensure and enable + @resource.stubs(:[]).with(:name).returns @joblabel + @resource.stubs(:[]).with(:ensure).returns :enabled + @resource.stubs(:[]).with(:enable).returns :true + @resource.stubs(:ref).returns "Service[#{@joblabel}]" + + # stub out the provider methods that actually touch the filesystem + # or execute commands + @provider.stubs(:plist_from_label).returns([@joblabel, @jobplist]) + @provider.stubs(:execute).returns("") + @provider.stubs(:resource).returns @resource + + # We stub this out for the normal case as 10.6 is "special". + provider_class.stubs(:get_macosx_version_major).returns("10.5") + + end + + it "should have a start method for #{@provider.object_id}" 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 + + it "should have a status method" do + @provider.should respond_to(:status) + end + + + describe "when checking status" do + it "should call the external command 'launchctl list' once" do + @provider.expects(:launchctl).with(:list).returns("rotating-strawberry-madonnas") + @provider.status + end + it "should return stopped if not listed in launchctl list output" do + @provider.stubs(:launchctl).with(:list).returns("rotating-strawberry-madonnas") + @provider.status.should == :stopped + end + it "should return running if listed in launchctl list output" do + @provider.stubs(:launchctl).with(:list).returns(@joblabel) + @provider.status.should == :running + end + end + + describe "when checking whether the service is enabled" do + it "should return true if the job plist says disabled is false" do + @provider.stubs(:plist_from_label).returns(["foo", {"Disabled" => false}]) + @provider.enabled?.should == :true + end + it "should return true if the job plist has no disabled key" do + @provider.stubs(:plist_from_label).returns(["foo", {}]) + @provider.enabled?.should == :true + end + it "should return false if the job plist says disabled is true" do + @provider.stubs(:plist_from_label).returns(["foo", {"Disabled" => true}]) + @provider.enabled?.should == :false + end + end + + describe "when checking whether the service is enabled on OS X 10.6" do + it "should return true if the job plist says disabled is true and the global overrides says disabled is false" do + provider_class.stubs(:get_macosx_version_major).returns("10.6") + @provider.stubs(:plist_from_label).returns(["foo", {"Disabled" => true}]) + Plist.stubs(:parse_xml).returns({@resource[:name] => {"Disabled" => false}}) + @provider.enabled?.should == :true + end + it "should return false if the job plist says disabled is false and the global overrides says disabled is true" do + provider_class.stubs(:get_macosx_version_major).returns("10.6") + @provider.stubs(:plist_from_label).returns(["foo", {"Disabled" => false}]) + Plist.stubs(:parse_xml).returns({@resource[:name] => {"Disabled" => true}}) + @provider.enabled?.should == :false + end + it "should return true if the job plist and the global overrides have no disabled keys" do + provider_class.stubs(:get_macosx_version_major).returns("10.6") + @provider.stubs(:plist_from_label).returns(["foo", {}]) + Plist.stubs(:parse_xml).returns({}) + @provider.enabled?.should == :true + end + end + + describe "when starting the service" do + it "should look for the relevant plist once" do + @provider.expects(:plist_from_label).once + @provider.start + end + it "should execute 'launchctl load' once without writing to the plist if the job is enabled" do + @provider.stubs(:enabled?).returns :true + @provider.expects(:execute).with([:launchctl, :load, @resource[:name]]).once + @provider.start + end + it "should execute 'launchctl load' with writing to the plist once if the job is disabled" do + @provider.stubs(:enabled?).returns :false + @provider.expects(:execute).with([:launchctl, :load, "-w", @resource[:name]]).once + @provider.start + end + it "should disable the job once if the job is disabled and should be disabled at boot" do + @provider.stubs(:enabled?).returns :false + @resource.stubs(:[]).with(:enable).returns :false + @provider.expects(:disable).once + @provider.start + end + end + + describe "when stopping the service" do + it "should look for the relevant plist once" do + @provider.expects(:plist_from_label).once + @provider.stop + end + it "should execute 'launchctl unload' once without writing to the plist if the job is disabled" do + @provider.stubs(:enabled?).returns :false + @provider.expects(:execute).with([:launchctl, :unload, @resource[:name]]).once + @provider.stop + end + it "should execute 'launchctl unload' with writing to the plist once if the job is enabled" do + @provider.stubs(:enabled?).returns :true + @provider.expects(:execute).with([:launchctl, :unload, "-w", @resource[:name]]).once + @provider.stop + end + it "should enable the job once if the job is enabled and should be enabled at boot" do + @provider.stubs(:enabled?).returns :true + @resource.stubs(:[]).with(:enable).returns :true + @provider.expects(:enable).once + @provider.stop + end + end + + describe "when enabling the service" do + it "should look for the relevant plist once" do + @provider.expects(:plist_from_label).once + @provider.stop + end + it "should check if the job is enabled once" do + @provider.expects(:enabled?).once + @provider.stop + end + end + + describe "when disabling the service" do + it "should look for the relevant plist once" do + @provider.expects(:plist_from_label).once + @provider.stop + end + end + + describe "when enabling the service on OS X 10.6" do + it "should write to the global launchd overrides file once" do + provider_class.stubs(:get_macosx_version_major).returns("10.6") + Plist.stubs(:parse_xml).returns({}) + Plist::Emit.expects(:save_plist).once + @provider.enable + end + end + + describe "when disabling the service on OS X 10.6" do + it "should write to the global launchd overrides file once" do + provider_class.stubs(:get_macosx_version_major).returns("10.6") + Plist.stubs(:parse_xml).returns({}) + Plist::Emit.expects(:save_plist).once + @provider.enable + end + end end diff --git a/spec/unit/provider/service/redhat_spec.rb b/spec/unit/provider/service/redhat_spec.rb index 82596fdcf..fd5822788 100755 --- a/spec/unit/provider/service/redhat_spec.rb +++ b/spec/unit/provider/service/redhat_spec.rb @@ -8,114 +8,114 @@ provider_class = Puppet::Type.type(:service).provider(:redhat) describe provider_class do + before :each do + @class = Puppet::Type.type(:service).provider(:redhat) + @resource = stub 'resource' + @resource.stubs(:[]).returns(nil) + @resource.stubs(:[]).with(:name).returns "myservice" + @provider = provider_class.new + @resource.stubs(:provider).returns @provider + @provider.resource = @resource + @provider.stubs(:get).with(:hasstatus).returns false + FileTest.stubs(:file?).with('/sbin/service').returns true + FileTest.stubs(:executable?).with('/sbin/service').returns true + end + + # test self.instances + describe "when getting all service instances" do before :each do - @class = Puppet::Type.type(:service).provider(:redhat) - @resource = stub 'resource' - @resource.stubs(:[]).returns(nil) - @resource.stubs(:[]).with(:name).returns "myservice" - @provider = provider_class.new - @resource.stubs(:provider).returns @provider - @provider.resource = @resource - @provider.stubs(:get).with(:hasstatus).returns false - FileTest.stubs(:file?).with('/sbin/service').returns true - FileTest.stubs(:executable?).with('/sbin/service').returns true + @services = ['one', 'two', 'three', 'four', 'kudzu', 'functions', 'halt', 'killall', 'single', 'linuxconf'] + @not_services = ['functions', 'halt', 'killall', 'single', 'linuxconf'] + Dir.stubs(:entries).returns @services + FileTest.stubs(:directory?).returns(true) + FileTest.stubs(:executable?).returns(true) end - - # test self.instances - describe "when getting all service instances" do - before :each do - @services = ['one', 'two', 'three', 'four', 'kudzu', 'functions', 'halt', 'killall', 'single', 'linuxconf'] - @not_services = ['functions', 'halt', 'killall', 'single', 'linuxconf'] - Dir.stubs(:entries).returns @services - FileTest.stubs(:directory?).returns(true) - FileTest.stubs(:executable?).returns(true) - end - it "should return instances for all services" do - (@services-@not_services).each do |inst| - @class.expects(:new).with{|hash| hash[:name] == inst && hash[:path] == '/etc/init.d'}.returns("#{inst}_instance") - end - results = (@services-@not_services).collect {|x| "#{x}_instance"} - @class.instances.should == results - end - it "should call service status when initialized from provider" do - @resource.stubs(:[]).with(:status).returns nil - @provider.stubs(:get).with(:hasstatus).returns true - @provider.expects(:execute).with{|command, *args| command == ['/sbin/service', 'myservice', 'status']} - @provider.send(:status) - end + it "should return instances for all services" do + (@services-@not_services).each do |inst| + @class.expects(:new).with{|hash| hash[:name] == inst && hash[:path] == '/etc/init.d'}.returns("#{inst}_instance") + end + results = (@services-@not_services).collect {|x| "#{x}_instance"} + @class.instances.should == results end - - it "should have an enabled? method" do - @provider.should respond_to(:enabled?) + it "should call service status when initialized from provider" do + @resource.stubs(:[]).with(:status).returns nil + @provider.stubs(:get).with(:hasstatus).returns true + @provider.expects(:execute).with{|command, *args| command == ['/sbin/service', 'myservice', 'status']} + @provider.send(:status) end + end - it "should have an enable method" do - @provider.should respond_to(:enable) - end + it "should have an enabled? method" do + @provider.should respond_to(:enabled?) + end - it "should have a disable method" do - @provider.should respond_to(:disable) - end + it "should have an enable method" do + @provider.should respond_to(:enable) + end - [:start, :stop, :status, :restart].each do |method| - it "should have a #{method} method" do - @provider.should respond_to(method) - end - describe "when running #{method}" do + it "should have a disable method" do + @provider.should respond_to(:disable) + end + + [:start, :stop, :status, :restart].each do |method| + it "should have a #{method} method" do + @provider.should respond_to(method) + end + describe "when running #{method}" do - it "should use any provided explicit command" do - @resource.stubs(:[]).with(method).returns "/user/specified/command" - @provider.expects(:execute).with { |command, *args| command == ["/user/specified/command"] } - @provider.send(method) - end + it "should use any provided explicit command" do + @resource.stubs(:[]).with(method).returns "/user/specified/command" + @provider.expects(:execute).with { |command, *args| command == ["/user/specified/command"] } + @provider.send(method) + end - it "should execute the service script with #{method} when no explicit command is provided" do - @resource.stubs(:[]).with("has#{method}".intern).returns :true - @provider.expects(:execute).with { |command, *args| command == ['/sbin/service', 'myservice', method.to_s]} - @provider.send(method) - end - end + it "should execute the service script with #{method} when no explicit command is provided" do + @resource.stubs(:[]).with("has#{method}".intern).returns :true + @provider.expects(:execute).with { |command, *args| command == ['/sbin/service', 'myservice', method.to_s]} + @provider.send(method) + end end + end - describe "when checking status" do - describe "when hasstatus is :true" do - before :each do - @resource.stubs(:[]).with(:hasstatus).returns :true - end - it "should execute the service script with fail_on_failure false" do - @provider.expects(:texecute).with(:status, ['/sbin/service', 'myservice', 'status'], false) - @provider.status - end - it "should consider the process running if the command returns 0" do - @provider.expects(:texecute).with(:status, ['/sbin/service', 'myservice', 'status'], false) - $CHILD_STATUS.stubs(:exitstatus).returns(0) - @provider.status.should == :running - end - [-10,-1,1,10].each { |ec| - it "should consider the process stopped if the command returns something non-0" do - @provider.expects(:texecute).with(:status, ['/sbin/service', 'myservice', 'status'], false) - $CHILD_STATUS.stubs(:exitstatus).returns(ec) - @provider.status.should == :stopped - end - } - end - describe "when hasstatus is not :true" do - it "should consider the service :running if it has a pid" do - @provider.expects(:getpid).returns "1234" - @provider.status.should == :running - end - it "should consider the service :stopped if it doesn't have a pid" do - @provider.expects(:getpid).returns nil - @provider.status.should == :stopped - end + describe "when checking status" do + describe "when hasstatus is :true" do + before :each do + @resource.stubs(:[]).with(:hasstatus).returns :true + end + it "should execute the service script with fail_on_failure false" do + @provider.expects(:texecute).with(:status, ['/sbin/service', 'myservice', 'status'], false) + @provider.status + end + it "should consider the process running if the command returns 0" do + @provider.expects(:texecute).with(:status, ['/sbin/service', 'myservice', 'status'], false) + $CHILD_STATUS.stubs(:exitstatus).returns(0) + @provider.status.should == :running + end + [-10,-1,1,10].each { |ec| + it "should consider the process stopped if the command returns something non-0" do + @provider.expects(:texecute).with(:status, ['/sbin/service', 'myservice', 'status'], false) + $CHILD_STATUS.stubs(:exitstatus).returns(ec) + @provider.status.should == :stopped end + } + end + describe "when hasstatus is not :true" do + it "should consider the service :running if it has a pid" do + @provider.expects(:getpid).returns "1234" + @provider.status.should == :running + end + it "should consider the service :stopped if it doesn't have a pid" do + @provider.expects(:getpid).returns nil + @provider.status.should == :stopped + end end + end - describe "when restarting and hasrestart is not :true" do - it "should stop and restart the process with the server script" do - @provider.expects(:texecute).with(:stop, ['/sbin/service', 'myservice', 'stop'], true) - @provider.expects(:texecute).with(:start, ['/sbin/service', 'myservice', 'start'], true) - @provider.restart - end + describe "when restarting and hasrestart is not :true" do + it "should stop and restart the process with the server script" do + @provider.expects(:texecute).with(:stop, ['/sbin/service', 'myservice', 'stop'], true) + @provider.expects(:texecute).with(:start, ['/sbin/service', 'myservice', 'start'], true) + @provider.restart end + end end diff --git a/spec/unit/provider/service/runit_spec.rb b/spec/unit/provider/service/runit_spec.rb index 42309ee5f..80ce456b4 100755 --- a/spec/unit/provider/service/runit_spec.rb +++ b/spec/unit/provider/service/runit_spec.rb @@ -10,131 +10,131 @@ provider_class = Puppet::Type.type(:service).provider(:runit) describe provider_class do - before(:each) do - # Create a mock resource - @resource = stub 'resource' + 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 + @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) + # 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]" + # 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(:sv) + @provider.stubs(:sv) - @provider.stubs(:resource).returns @resource - end + @provider.stubs(:resource).returns @resource + end - it "should have a restart method" do - @provider.should respond_to(:restart) - 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 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 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 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 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 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 enable the service if it is not enabled" do - @provider.stubs(:sv) + it "should have a disable method" do + @provider.should respond_to(:disable) + end - @provider.expects(:enabled?).returns :false - @provider.expects(:enable) + describe "when starting" do + it "should enable the service if it is not enabled" do + @provider.stubs(:sv) - @provider.start - end + @provider.expects(:enabled?).returns :false + @provider.expects(:enable) - it "should execute external command 'sv start /etc/service/myservice'" do - @provider.stubs(:enabled?).returns :true - @provider.expects(:sv).with("start", "/etc/service/myservice") - @provider.start - end + @provider.start end - describe "when stopping" do - it "should execute external command 'sv stop /etc/service/myservice'" do - @provider.expects(:sv).with("stop", "/etc/service/myservice") - @provider.stop - end + it "should execute external command 'sv start /etc/service/myservice'" do + @provider.stubs(:enabled?).returns :true + @provider.expects(:sv).with("start", "/etc/service/myservice") + @provider.start end + end - describe "when restarting" do - it "should call 'sv restart /etc/service/myservice'" do - @provider.expects(:sv).with("restart","/etc/service/myservice") - @provider.restart - end + describe "when stopping" do + it "should execute external command 'sv stop /etc/service/myservice'" do + @provider.expects(:sv).with("stop", "/etc/service/myservice") + @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 + describe "when restarting" do + it "should call 'sv restart /etc/service/myservice'" do + @provider.expects(:sv).with("restart","/etc/service/myservice") + @provider.restart 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 + 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 + 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 + 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/service/src_spec.rb b/spec/unit/provider/service/src_spec.rb index eaa968f6e..eff98f030 100755 --- a/spec/unit/provider/service/src_spec.rb +++ b/spec/unit/provider/service/src_spec.rb @@ -9,89 +9,89 @@ provider_class = Puppet::Type.type(:service).provider(:src) describe provider_class do - before :each do - @resource = stub 'resource' - @resource.stubs(:[]).returns(nil) - @resource.stubs(:[]).with(:name).returns "myservice" - - @provider = provider_class.new - @provider.resource = @resource - - @provider.stubs(:command).with(:stopsrc).returns "/usr/bin/stopsrc" - @provider.stubs(:command).with(:startsrc).returns "/usr/bin/startsrc" - @provider.stubs(:command).with(:lssrc).returns "/usr/bin/lssrc" - @provider.stubs(:command).with(:refresh).returns "/usr/bin/refresh" - - @provider.stubs(:stopsrc) - @provider.stubs(:startsrc) - @provider.stubs(:lssrc) - @provider.stubs(:refresh) + before :each do + @resource = stub 'resource' + @resource.stubs(:[]).returns(nil) + @resource.stubs(:[]).with(:name).returns "myservice" + + @provider = provider_class.new + @provider.resource = @resource + + @provider.stubs(:command).with(:stopsrc).returns "/usr/bin/stopsrc" + @provider.stubs(:command).with(:startsrc).returns "/usr/bin/startsrc" + @provider.stubs(:command).with(:lssrc).returns "/usr/bin/lssrc" + @provider.stubs(:command).with(:refresh).returns "/usr/bin/refresh" + + @provider.stubs(:stopsrc) + @provider.stubs(:startsrc) + @provider.stubs(:lssrc) + @provider.stubs(:refresh) + end + + [:start, :stop, :status, :restart].each do |method| + it "should have a #{method} method" do + @provider.should respond_to(method) end + end - [:start, :stop, :status, :restart].each do |method| - it "should have a #{method} method" do - @provider.should respond_to(method) - end - end + it "should execute the startsrc command" do + @provider.expects(:execute).with(['/usr/bin/startsrc', '-s', "myservice"], {:squelch => true, :failonfail => true}) + @provider.start + end - it "should execute the startsrc command" do - @provider.expects(:execute).with(['/usr/bin/startsrc', '-s', "myservice"], {:squelch => true, :failonfail => true}) - @provider.start - end + it "should execute the stopsrc command" do + @provider.expects(:execute).with(['/usr/bin/stopsrc', '-s', "myservice"], {:squelch => true, :failonfail => true}) + @provider.stop + end - it "should execute the stopsrc command" do - @provider.expects(:execute).with(['/usr/bin/stopsrc', '-s', "myservice"], {:squelch => true, :failonfail => true}) - @provider.stop - end - - it "should execute status and return running if the subsystem is active" do - sample_output = <<_EOF_ + it "should execute status and return running if the subsystem is active" do + sample_output = <<_EOF_ Subsystem Group PID Status myservice tcpip 1234 active _EOF_ - @provider.expects(:execute).with(['/usr/bin/lssrc', '-s', "myservice"]).returns sample_output - @provider.status.should == :running - end + @provider.expects(:execute).with(['/usr/bin/lssrc', '-s', "myservice"]).returns sample_output + @provider.status.should == :running + end - it "should execute status and return stopped if the subsystem is inoperative" do - sample_output = <<_EOF_ + it "should execute status and return stopped if the subsystem is inoperative" do + sample_output = <<_EOF_ Subsystem Group PID Status myservice tcpip inoperative _EOF_ - @provider.expects(:execute).with(['/usr/bin/lssrc', '-s', "myservice"]).returns sample_output - @provider.status.should == :stopped - end + @provider.expects(:execute).with(['/usr/bin/lssrc', '-s', "myservice"]).returns sample_output + @provider.status.should == :stopped + end - it "should execute status and return nil if the status is not known" do - sample_output = <<_EOF_ + it "should execute status and return nil if the status is not known" do + sample_output = <<_EOF_ Subsystem Group PID Status myservice tcpip randomdata _EOF_ - @provider.expects(:execute).with(['/usr/bin/lssrc', '-s', "myservice"]).returns sample_output - @provider.status.should == nil - end + @provider.expects(:execute).with(['/usr/bin/lssrc', '-s', "myservice"]).returns sample_output + @provider.status.should == nil + end - it "should execute restart which runs refresh" do - sample_output = <<_EOF_ + it "should execute restart which runs refresh" do + sample_output = <<_EOF_ #subsysname:synonym:cmdargs:path:uid:auditid:standin:standout:standerr:action:multi:contact:svrkey:svrmtype:priority:signorm:sigforce:display:waittime:grpname: myservice:::/usr/sbin/inetd:0:0:/dev/console:/dev/console:/dev/console:-O:-Q:-K:0:0:20:0:0:-d:20:tcpip: _EOF_ - @provider.expects(:execute).with(['/usr/bin/lssrc', '-Ss', "myservice"]).returns sample_output - @provider.expects(:execute).with(['/usr/bin/refresh', '-s', "myservice"]) - @provider.restart - end + @provider.expects(:execute).with(['/usr/bin/lssrc', '-Ss', "myservice"]).returns sample_output + @provider.expects(:execute).with(['/usr/bin/refresh', '-s', "myservice"]) + @provider.restart + end - it "should execute restart which runs stopsrc then startsrc" do - sample_output = <<_EOF_ + it "should execute restart which runs stopsrc then startsrc" do + sample_output = <<_EOF_ #subsysname:synonym:cmdargs:path:uid:auditid:standin:standout:standerr:action:multi:contact:svrkey:svrmtype:priority:signorm:sigforce:display:waittime:grpname: myservice::--no-daemonize:/usr/sbin/puppetd:0:0:/dev/null:/var/log/puppet.log:/var/log/puppet.log:-O:-Q:-S:0:0:20:15:9:-d:20::" _EOF_ - @provider.expects(:execute).with(['/usr/bin/lssrc', '-Ss', "myservice"]).returns sample_output - @provider.expects(:execute).with(['/usr/bin/stopsrc', '-s', "myservice"], {:squelch => true, :failonfail => true}) - @provider.expects(:execute).with(['/usr/bin/startsrc', '-s', "myservice"], {:squelch => true, :failonfail => true}) - @provider.restart - end + @provider.expects(:execute).with(['/usr/bin/lssrc', '-Ss', "myservice"]).returns sample_output + @provider.expects(:execute).with(['/usr/bin/stopsrc', '-s', "myservice"], {:squelch => true, :failonfail => true}) + @provider.expects(:execute).with(['/usr/bin/startsrc', '-s', "myservice"], {:squelch => true, :failonfail => true}) + @provider.restart + end end diff --git a/spec/unit/provider/ssh_authorized_key/parsed_spec.rb b/spec/unit/provider/ssh_authorized_key/parsed_spec.rb index 3b14ad3d7..059d010bb 100755 --- a/spec/unit/provider/ssh_authorized_key/parsed_spec.rb +++ b/spec/unit/provider/ssh_authorized_key/parsed_spec.rb @@ -10,219 +10,219 @@ require 'puppettest/fileparsing' provider_class = Puppet::Type.type(:ssh_authorized_key).provider(:parsed) describe provider_class do - include PuppetSpec::Files - include PuppetTest - include PuppetTest::FileParsing - - before :each do - @sshauthkey_class = Puppet::Type.type(:ssh_authorized_key) - @provider = @sshauthkey_class.provider(:parsed) - @keyfile = File.join(Dir.tmpdir, 'authorized_keys') - @provider.any_instance.stubs(:target).returns @keyfile - @user = 'random_bob' - Puppet::Util.stubs(:uid).with(@user).returns 12345 + include PuppetSpec::Files + include PuppetTest + include PuppetTest::FileParsing + + before :each do + @sshauthkey_class = Puppet::Type.type(:ssh_authorized_key) + @provider = @sshauthkey_class.provider(:parsed) + @keyfile = File.join(Dir.tmpdir, 'authorized_keys') + @provider.any_instance.stubs(:target).returns @keyfile + @user = 'random_bob' + Puppet::Util.stubs(:uid).with(@user).returns 12345 + end + + after :each do + @provider.initvars + end + + def mkkey(args) + fakeresource = fakeresource(:ssh_authorized_key, args[:name]) + fakeresource.stubs(:should).with(:user).returns @user + fakeresource.stubs(:should).with(:target).returns @keyfile + + key = @provider.new(fakeresource) + args.each do |p,v| + key.send(p.to_s + "=", v) end - after :each do - @provider.initvars + key + end + + def genkey(key) + @provider.stubs(:filetype).returns(Puppet::Util::FileType::FileTypeRam) + File.stubs(:chown) + File.stubs(:chmod) + Puppet::Util::SUIDManager.stubs(:asuser).yields + key.flush + @provider.target_object(@keyfile).read + end + + PuppetTest.fakedata("data/providers/ssh_authorized_key/parsed").each { |file| + it "should be able to parse example data in #{file}" do + fakedataparse(file) end + } - def mkkey(args) - fakeresource = fakeresource(:ssh_authorized_key, args[:name]) - fakeresource.stubs(:should).with(:user).returns @user - fakeresource.stubs(:should).with(:target).returns @keyfile + it "should be able to generate a basic authorized_keys file" do - key = @provider.new(fakeresource) - args.each do |p,v| - key.send(p.to_s + "=", v) - end + key = mkkey( + { + :name => "Just Testing", + :key => "AAAAfsfddsjldjgksdflgkjsfdlgkj", + :type => "ssh-dss", + :ensure => :present, - key - end + :options => [:absent] + }) - def genkey(key) - @provider.stubs(:filetype).returns(Puppet::Util::FileType::FileTypeRam) - File.stubs(:chown) - File.stubs(:chmod) - Puppet::Util::SUIDManager.stubs(:asuser).yields - key.flush - @provider.target_object(@keyfile).read - end + genkey(key).should == "ssh-dss AAAAfsfddsjldjgksdflgkjsfdlgkj Just Testing\n" + end - PuppetTest.fakedata("data/providers/ssh_authorized_key/parsed").each { |file| - it "should be able to parse example data in #{file}" do - fakedataparse(file) - end - } + it "should be able to generate a authorized_keys file with options" do - it "should be able to generate a basic authorized_keys file" do + key = mkkey( + { + :name => "root@localhost", + :key => "AAAAfsfddsjldjgksdflgkjsfdlgkj", + :type => "ssh-rsa", + :ensure => :present, - key = mkkey( - { - :name => "Just Testing", - :key => "AAAAfsfddsjldjgksdflgkjsfdlgkj", - :type => "ssh-dss", - :ensure => :present, + :options => ['from="192.168.1.1"', "no-pty", "no-X11-forwarding"] + }) - :options => [:absent] - }) + genkey(key).should == "from=\"192.168.1.1\",no-pty,no-X11-forwarding ssh-rsa AAAAfsfddsjldjgksdflgkjsfdlgkj root@localhost\n" + end - genkey(key).should == "ssh-dss AAAAfsfddsjldjgksdflgkjsfdlgkj Just Testing\n" - end + it "should be able to parse options containing commas via its parse_options method" do + options = %w{from="host1.reductlivelabs.com,host.reductivelabs.com" command="/usr/local/bin/run" ssh-pty} + optionstr = options.join(", ") - it "should be able to generate a authorized_keys file with options" do + @provider.parse_options(optionstr).should == options + end - key = mkkey( - { - :name => "root@localhost", - :key => "AAAAfsfddsjldjgksdflgkjsfdlgkj", - :type => "ssh-rsa", - :ensure => :present, + it "should use '' as name for entries that lack a comment" do + line = "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAut8aOSxenjOqF527dlsdHWV4MNoAsX14l9M297+SQXaQ5Z3BedIxZaoQthkDALlV/25A1COELrg9J2MqJNQc8Xe9XQOIkBQWWinUlD/BXwoOTWEy8C8zSZPHZ3getMMNhGTBO+q/O+qiJx3y5cA4MTbw2zSxukfWC87qWwcZ64UUlegIM056vPsdZWFclS9hsROVEa57YUMrehQ1EGxT4Z5j6zIopufGFiAPjZigq/vqgcAqhAKP6yu4/gwO6S9tatBeEjZ8fafvj1pmvvIplZeMr96gHE7xS3pEEQqnB3nd4RY7AF6j9kFixnsytAUO7STPh/M3pLiVQBN89TvWPQ==" - :options => ['from="192.168.1.1"', "no-pty", "no-X11-forwarding"] - }) + @provider.parse(line)[0][:name].should == "" + end +end - genkey(key).should == "from=\"192.168.1.1\",no-pty,no-X11-forwarding ssh-rsa AAAAfsfddsjldjgksdflgkjsfdlgkj root@localhost\n" - end +describe provider_class do + before :each do + @resource = stub("resource", :name => "foo") + @resource.stubs(:[]).returns "foo" - it "should be able to parse options containing commas via its parse_options method" do - options = %w{from="host1.reductlivelabs.com,host.reductivelabs.com" command="/usr/local/bin/run" ssh-pty} - optionstr = options.join(", ") + @provider = provider_class.new(@resource) + provider_class.stubs(:filetype).returns(Puppet::Util::FileType::FileTypeRam) + Puppet::Util::SUIDManager.stubs(:asuser).yields + end - @provider.parse_options(optionstr).should == options + describe "when flushing" do + before :each do + # Stub file and directory operations + Dir.stubs(:mkdir) + File.stubs(:chmod) + File.stubs(:chown) end - it "should use '' as name for entries that lack a comment" do - line = "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAut8aOSxenjOqF527dlsdHWV4MNoAsX14l9M297+SQXaQ5Z3BedIxZaoQthkDALlV/25A1COELrg9J2MqJNQc8Xe9XQOIkBQWWinUlD/BXwoOTWEy8C8zSZPHZ3getMMNhGTBO+q/O+qiJx3y5cA4MTbw2zSxukfWC87qWwcZ64UUlegIM056vPsdZWFclS9hsROVEa57YUMrehQ1EGxT4Z5j6zIopufGFiAPjZigq/vqgcAqhAKP6yu4/gwO6S9tatBeEjZ8fafvj1pmvvIplZeMr96gHE7xS3pEEQqnB3nd4RY7AF6j9kFixnsytAUO7STPh/M3pLiVQBN89TvWPQ==" + describe "and both a user and a target have been specified" do + before :each do + Puppet::Util.stubs(:uid).with("random_bob").returns 12345 + @resource.stubs(:should).with(:user).returns "random_bob" + target = "/tmp/.ssh_dir/place_to_put_authorized_keys" + @resource.stubs(:should).with(:target).returns target + end + + it "should create the directory" do + File.stubs(:exist?).with("/tmp/.ssh_dir").returns false + Dir.expects(:mkdir).with("/tmp/.ssh_dir", 0700) + @provider.flush + end + + it "should chown the directory to the user" do + uid = Puppet::Util.uid("random_bob") + File.expects(:chown).with(uid, nil, "/tmp/.ssh_dir") + @provider.flush + end + + it "should chown the key file to the user" do + uid = Puppet::Util.uid("random_bob") + File.expects(:chown).with(uid, nil, "/tmp/.ssh_dir/place_to_put_authorized_keys") + @provider.flush + end + + it "should chmod the key file to 0600" do + File.expects(:chmod).with(0600, "/tmp/.ssh_dir/place_to_put_authorized_keys") + @provider.flush + end + end - @provider.parse(line)[0][:name].should == "" + describe "and a user has been specified with no target" do + before :each do + @resource.stubs(:should).with(:user).returns "nobody" + @resource.stubs(:should).with(:target).returns nil + # + # I'd like to use random_bob here and something like + # + # File.stubs(:expand_path).with("~random_bob/.ssh").returns "/users/r/random_bob/.ssh" + # + # but mocha objects strenuously to stubbing File.expand_path + # so I'm left with using nobody. + @dir = File.expand_path("~nobody/.ssh") + end + + it "should create the directory if it doesn't exist" do + File.stubs(:exist?).with(@dir).returns false + Dir.expects(:mkdir).with(@dir,0700) + @provider.flush + end + + it "should not create or chown the directory if it already exist" do + File.stubs(:exist?).with(@dir).returns false + Dir.expects(:mkdir).never + @provider.flush + end + + it "should chown the directory to the user if it creates it" do + File.stubs(:exist?).with(@dir).returns false + Dir.stubs(:mkdir).with(@dir,0700) + uid = Puppet::Util.uid("nobody") + File.expects(:chown).with(uid, nil, @dir) + @provider.flush + end + + it "should not create or chown the directory if it already exist" do + File.stubs(:exist?).with(@dir).returns false + Dir.expects(:mkdir).never + File.expects(:chown).never + @provider.flush + end + + it "should chown the key file to the user" do + uid = Puppet::Util.uid("nobody") + File.expects(:chown).with(uid, nil, File.expand_path("~nobody/.ssh/authorized_keys")) + @provider.flush + end + + it "should chmod the key file to 0600" do + File.expects(:chmod).with(0600, File.expand_path("~nobody/.ssh/authorized_keys")) + @provider.flush + end end -end -describe provider_class do - before :each do - @resource = stub("resource", :name => "foo") - @resource.stubs(:[]).returns "foo" + describe "and a target has been specified with no user" do + before :each do + @resource.stubs(:should).with(:user).returns nil + @resource.stubs(:should).with(:target).returns("/tmp/.ssh_dir/place_to_put_authorized_keys") + end - @provider = provider_class.new(@resource) - provider_class.stubs(:filetype).returns(Puppet::Util::FileType::FileTypeRam) - Puppet::Util::SUIDManager.stubs(:asuser).yields + it "should raise an error" do + proc { @provider.flush }.should raise_error + end end - describe "when flushing" do - before :each do - # Stub file and directory operations - Dir.stubs(:mkdir) - File.stubs(:chmod) - File.stubs(:chown) - end - - describe "and both a user and a target have been specified" do - before :each do - Puppet::Util.stubs(:uid).with("random_bob").returns 12345 - @resource.stubs(:should).with(:user).returns "random_bob" - target = "/tmp/.ssh_dir/place_to_put_authorized_keys" - @resource.stubs(:should).with(:target).returns target - end - - it "should create the directory" do - File.stubs(:exist?).with("/tmp/.ssh_dir").returns false - Dir.expects(:mkdir).with("/tmp/.ssh_dir", 0700) - @provider.flush - end - - it "should chown the directory to the user" do - uid = Puppet::Util.uid("random_bob") - File.expects(:chown).with(uid, nil, "/tmp/.ssh_dir") - @provider.flush - end - - it "should chown the key file to the user" do - uid = Puppet::Util.uid("random_bob") - File.expects(:chown).with(uid, nil, "/tmp/.ssh_dir/place_to_put_authorized_keys") - @provider.flush - end - - it "should chmod the key file to 0600" do - File.expects(:chmod).with(0600, "/tmp/.ssh_dir/place_to_put_authorized_keys") - @provider.flush - end - end - - describe "and a user has been specified with no target" do - before :each do - @resource.stubs(:should).with(:user).returns "nobody" - @resource.stubs(:should).with(:target).returns nil - # - # I'd like to use random_bob here and something like - # - # File.stubs(:expand_path).with("~random_bob/.ssh").returns "/users/r/random_bob/.ssh" - # - # but mocha objects strenuously to stubbing File.expand_path - # so I'm left with using nobody. - @dir = File.expand_path("~nobody/.ssh") - end - - it "should create the directory if it doesn't exist" do - File.stubs(:exist?).with(@dir).returns false - Dir.expects(:mkdir).with(@dir,0700) - @provider.flush - end - - it "should not create or chown the directory if it already exist" do - File.stubs(:exist?).with(@dir).returns false - Dir.expects(:mkdir).never - @provider.flush - end - - it "should chown the directory to the user if it creates it" do - File.stubs(:exist?).with(@dir).returns false - Dir.stubs(:mkdir).with(@dir,0700) - uid = Puppet::Util.uid("nobody") - File.expects(:chown).with(uid, nil, @dir) - @provider.flush - end - - it "should not create or chown the directory if it already exist" do - File.stubs(:exist?).with(@dir).returns false - Dir.expects(:mkdir).never - File.expects(:chown).never - @provider.flush - end - - it "should chown the key file to the user" do - uid = Puppet::Util.uid("nobody") - File.expects(:chown).with(uid, nil, File.expand_path("~nobody/.ssh/authorized_keys")) - @provider.flush - end - - it "should chmod the key file to 0600" do - File.expects(:chmod).with(0600, File.expand_path("~nobody/.ssh/authorized_keys")) - @provider.flush - end - end - - describe "and a target has been specified with no user" do - before :each do - @resource.stubs(:should).with(:user).returns nil - @resource.stubs(:should).with(:target).returns("/tmp/.ssh_dir/place_to_put_authorized_keys") - end - - it "should raise an error" do - proc { @provider.flush }.should raise_error - end - end - - describe "and a invalid user has been specified with no target" do - before :each do - @resource.stubs(:should).with(:user).returns "thisusershouldnotexist" - @resource.stubs(:should).with(:target).returns nil - end - - it "should catch an exception and raise a Puppet error" do - lambda { @provider.flush }.should raise_error(Puppet::Error) - end - end + describe "and a invalid user has been specified with no target" do + before :each do + @resource.stubs(:should).with(:user).returns "thisusershouldnotexist" + @resource.stubs(:should).with(:target).returns nil + end + it "should catch an exception and raise a Puppet error" do + lambda { @provider.flush }.should raise_error(Puppet::Error) + end end + + end end diff --git a/spec/unit/provider/sshkey/parsed_spec.rb b/spec/unit/provider/sshkey/parsed_spec.rb index 4180ffbaf..1a5470974 100755 --- a/spec/unit/provider/sshkey/parsed_spec.rb +++ b/spec/unit/provider/sshkey/parsed_spec.rb @@ -5,34 +5,34 @@ require File.dirname(__FILE__) + '/../../../spec_helper' provider_class = Puppet::Type.type(:sshkey).provider(:parsed) describe provider_class do - before do - @sshkey_class = Puppet::Type.type(:sshkey) - @provider_class = @sshkey_class.provider(:parsed) - @key = 'AAAAB3NzaC1yc2EAAAABIwAAAQEAzwHhxXvIrtfIwrudFqc8yQcIfMudrgpnuh1F3AV6d2BrLgu/yQE7W5UyJMUjfj427sQudRwKW45O0Jsnr33F4mUw+GIMlAAmp9g24/OcrTiB8ZUKIjoPy/cO4coxGi8/NECtRzpD/ZUPFh6OEpyOwJPMb7/EC2Az6Otw4StHdXUYw22zHazBcPFnv6zCgPx1hA7QlQDWTu4YcL0WmTYQCtMUb3FUqrcFtzGDD0ytosgwSd+JyN5vj5UwIABjnNOHPZ62EY1OFixnfqX/+dUwrFSs5tPgBF/KkC6R7tmbUfnBON6RrGEmu+ajOTOLy23qUZB4CQ53V7nyAWhzqSK+hw==' - end - - it "should parse the name from the first field" do - @provider_class.parse_line('test ssh-rsa '+@key)[:name].should == "test" - end - - it "should parse the first component of the first field as the name" do - @provider_class.parse_line('test,alias ssh-rsa '+@key)[:name].should == "test" - end - - it "should parse host_aliases from the remaining components of the first field" do - @provider_class.parse_line('test,alias ssh-rsa '+@key)[:host_aliases].should == ["alias"] - end - - it "should parse multiple host_aliases" do - @provider_class.parse_line('test,alias1,alias2,alias3 ssh-rsa '+@key)[:host_aliases].should == ["alias1","alias2","alias3"] - end - - it "should not drop an empty host_alias" do - @provider_class.parse_line('test,alias, ssh-rsa '+@key)[:host_aliases].should == ["alias",""] - end - - it "should recognise when there are no host aliases" do - @provider_class.parse_line('test ssh-rsa '+@key)[:host_aliases].should == [] - end + before do + @sshkey_class = Puppet::Type.type(:sshkey) + @provider_class = @sshkey_class.provider(:parsed) + @key = 'AAAAB3NzaC1yc2EAAAABIwAAAQEAzwHhxXvIrtfIwrudFqc8yQcIfMudrgpnuh1F3AV6d2BrLgu/yQE7W5UyJMUjfj427sQudRwKW45O0Jsnr33F4mUw+GIMlAAmp9g24/OcrTiB8ZUKIjoPy/cO4coxGi8/NECtRzpD/ZUPFh6OEpyOwJPMb7/EC2Az6Otw4StHdXUYw22zHazBcPFnv6zCgPx1hA7QlQDWTu4YcL0WmTYQCtMUb3FUqrcFtzGDD0ytosgwSd+JyN5vj5UwIABjnNOHPZ62EY1OFixnfqX/+dUwrFSs5tPgBF/KkC6R7tmbUfnBON6RrGEmu+ajOTOLy23qUZB4CQ53V7nyAWhzqSK+hw==' + end + + it "should parse the name from the first field" do + @provider_class.parse_line('test ssh-rsa '+@key)[:name].should == "test" + end + + it "should parse the first component of the first field as the name" do + @provider_class.parse_line('test,alias ssh-rsa '+@key)[:name].should == "test" + end + + it "should parse host_aliases from the remaining components of the first field" do + @provider_class.parse_line('test,alias ssh-rsa '+@key)[:host_aliases].should == ["alias"] + end + + it "should parse multiple host_aliases" do + @provider_class.parse_line('test,alias1,alias2,alias3 ssh-rsa '+@key)[:host_aliases].should == ["alias1","alias2","alias3"] + end + + it "should not drop an empty host_alias" do + @provider_class.parse_line('test,alias, ssh-rsa '+@key)[:host_aliases].should == ["alias",""] + end + + it "should recognise when there are no host aliases" do + @provider_class.parse_line('test ssh-rsa '+@key)[:host_aliases].should == [] + end end diff --git a/spec/unit/provider/user/hpux_spec.rb b/spec/unit/provider/user/hpux_spec.rb index 4129a7ab6..f53f6c31e 100755 --- a/spec/unit/provider/user/hpux_spec.rb +++ b/spec/unit/provider/user/hpux_spec.rb @@ -5,21 +5,21 @@ 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 + # 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 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 + 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/provider/user/ldap_spec.rb b/spec/unit/provider/user/ldap_spec.rb index adc2180ff..b6635705d 100755 --- a/spec/unit/provider/user/ldap_spec.rb +++ b/spec/unit/provider/user/ldap_spec.rb @@ -8,272 +8,272 @@ require File.dirname(__FILE__) + '/../../../spec_helper' provider_class = Puppet::Type.type(:user).provider(:ldap) describe provider_class do - it "should have the Ldap provider class as its baseclass" do - provider_class.superclass.should equal(Puppet::Provider::Ldap) + it "should have the Ldap provider class as its baseclass" do + provider_class.superclass.should equal(Puppet::Provider::Ldap) + end + + it "should manage :posixAccount and :person objectclasses" do + provider_class.manager.objectclasses.should == [:posixAccount, :person] + end + + it "should use 'ou=People' as its relative base" do + provider_class.manager.location.should == "ou=People" + end + + it "should use :uid as its rdn" do + provider_class.manager.rdn.should == :uid + end + + it "should be able to manage passwords" do + provider_class.should be_manages_passwords + end + + it "should use the ldap group provider to convert group names to numbers" do + provider = provider_class.new(:name => "foo") + Puppet::Type.type(:group).provider(:ldap).expects(:name2id).with("bar").returns 10 + + provider.gid = 'bar' + provider.gid.should == 10 + end + + {:name => "uid", + :password => "userPassword", + :comment => "cn", + :uid => "uidNumber", + :gid => "gidNumber", + :home => "homeDirectory", + :shell => "loginShell" + }.each do |puppet, ldap| + it "should map :#{puppet.to_s} to '#{ldap}'" do + provider_class.manager.ldap_name(puppet).should == ldap end + end - it "should manage :posixAccount and :person objectclasses" do - provider_class.manager.objectclasses.should == [:posixAccount, :person] + describe "when being created" do + before do + # So we don't try to actually talk to ldap + @connection = mock 'connection' + provider_class.manager.stubs(:connect).yields @connection end - it "should use 'ou=People' as its relative base" do - provider_class.manager.location.should == "ou=People" - end + it "should generate the sn as the last field of the cn" do + resource = stub 'resource', :should => %w{whatever} + resource.stubs(:should).with(:comment).returns ["Luke Kanies"] + resource.stubs(:should).with(:ensure).returns :present + instance = provider_class.new(:name => "luke", :ensure => :absent) + instance.stubs(:resource).returns resource - it "should use :uid as its rdn" do - provider_class.manager.rdn.should == :uid - end + @connection.expects(:add).with { |dn, attrs| attrs["sn"] == ["Kanies"] } - it "should be able to manage passwords" do - provider_class.should be_manages_passwords + instance.create + instance.flush end - it "should use the ldap group provider to convert group names to numbers" do - provider = provider_class.new(:name => "foo") - Puppet::Type.type(:group).provider(:ldap).expects(:name2id).with("bar").returns 10 + describe "with no uid specified" do + it "should pick the first available UID after the largest existing UID" do + low = {:name=>["luke"], :shell=>:absent, :uid=>["600"], :home=>["/h"], :gid=>["1000"], :password=>["blah"], :comment=>["l k"]} + high = {:name=>["testing"], :shell=>:absent, :uid=>["640"], :home=>["/h"], :gid=>["1000"], :password=>["blah"], :comment=>["t u"]} + provider_class.manager.expects(:search).returns([low, high]) - provider.gid = 'bar' - provider.gid.should == 10 - end + resource = stub 'resource', :should => %w{whatever} + resource.stubs(:should).with(:uid).returns nil + resource.stubs(:should).with(:ensure).returns :present + instance = provider_class.new(:name => "luke", :ensure => :absent) + instance.stubs(:resource).returns resource - {:name => "uid", - :password => "userPassword", - :comment => "cn", - :uid => "uidNumber", - :gid => "gidNumber", - :home => "homeDirectory", - :shell => "loginShell" - }.each do |puppet, ldap| - it "should map :#{puppet.to_s} to '#{ldap}'" do - provider_class.manager.ldap_name(puppet).should == ldap - end - end + @connection.expects(:add).with { |dn, attrs| attrs["uidNumber"] == ["641"] } + + instance.create + instance.flush + end + + it "should pick 501 of no users exist" do + provider_class.manager.expects(:search).returns nil - describe "when being created" do - before do - # So we don't try to actually talk to ldap - @connection = mock 'connection' - provider_class.manager.stubs(:connect).yields @connection - end - - it "should generate the sn as the last field of the cn" do - resource = stub 'resource', :should => %w{whatever} - resource.stubs(:should).with(:comment).returns ["Luke Kanies"] - resource.stubs(:should).with(:ensure).returns :present - instance = provider_class.new(:name => "luke", :ensure => :absent) - instance.stubs(:resource).returns resource - - @connection.expects(:add).with { |dn, attrs| attrs["sn"] == ["Kanies"] } - - instance.create - instance.flush - end - - describe "with no uid specified" do - it "should pick the first available UID after the largest existing UID" do - low = {:name=>["luke"], :shell=>:absent, :uid=>["600"], :home=>["/h"], :gid=>["1000"], :password=>["blah"], :comment=>["l k"]} - high = {:name=>["testing"], :shell=>:absent, :uid=>["640"], :home=>["/h"], :gid=>["1000"], :password=>["blah"], :comment=>["t u"]} - provider_class.manager.expects(:search).returns([low, high]) - - resource = stub 'resource', :should => %w{whatever} - resource.stubs(:should).with(:uid).returns nil - resource.stubs(:should).with(:ensure).returns :present - instance = provider_class.new(:name => "luke", :ensure => :absent) - instance.stubs(:resource).returns resource - - @connection.expects(:add).with { |dn, attrs| attrs["uidNumber"] == ["641"] } - - instance.create - instance.flush - end - - it "should pick 501 of no users exist" do - provider_class.manager.expects(:search).returns nil - - resource = stub 'resource', :should => %w{whatever} - resource.stubs(:should).with(:uid).returns nil - resource.stubs(:should).with(:ensure).returns :present - instance = provider_class.new(:name => "luke", :ensure => :absent) - instance.stubs(:resource).returns resource - - @connection.expects(:add).with { |dn, attrs| attrs["uidNumber"] == ["501"] } - - instance.create - instance.flush - end - end + resource = stub 'resource', :should => %w{whatever} + resource.stubs(:should).with(:uid).returns nil + resource.stubs(:should).with(:ensure).returns :present + instance = provider_class.new(:name => "luke", :ensure => :absent) + instance.stubs(:resource).returns resource + + @connection.expects(:add).with { |dn, attrs| attrs["uidNumber"] == ["501"] } + + instance.create + instance.flush + end end + end - describe "when flushing" do - before do - provider_class.stubs(:suitable?).returns true + describe "when flushing" do + before do + provider_class.stubs(:suitable?).returns true - @instance = provider_class.new(:name => "myname", :groups => %w{whatever}, :uid => "400") - end + @instance = provider_class.new(:name => "myname", :groups => %w{whatever}, :uid => "400") + end - it "should remove the :groups value before updating" do - @instance.class.manager.expects(:update).with { |name, ldap, puppet| puppet[:groups].nil? } + it "should remove the :groups value before updating" do + @instance.class.manager.expects(:update).with { |name, ldap, puppet| puppet[:groups].nil? } - @instance.flush - end + @instance.flush + end - it "should empty the property hash" do - @instance.class.manager.stubs(:update) + it "should empty the property hash" do + @instance.class.manager.stubs(:update) - @instance.flush + @instance.flush - @instance.uid.should == :absent - end + @instance.uid.should == :absent + end - it "should empty the ldap property hash" do - @instance.class.manager.stubs(:update) + it "should empty the ldap property hash" do + @instance.class.manager.stubs(:update) - @instance.flush + @instance.flush - @instance.ldap_properties[:uid].should be_nil - end + @instance.ldap_properties[:uid].should be_nil end + end - describe "when checking group membership" do - before do - @groups = Puppet::Type.type(:group).provider(:ldap) - @group_manager = @groups.manager - provider_class.stubs(:suitable?).returns true + describe "when checking group membership" do + before do + @groups = Puppet::Type.type(:group).provider(:ldap) + @group_manager = @groups.manager + provider_class.stubs(:suitable?).returns true - @instance = provider_class.new(:name => "myname") - end + @instance = provider_class.new(:name => "myname") + end - it "should show its group membership as the sorted list of all groups returned by an ldap query of group memberships" do - one = {:name => "one"} - two = {:name => "two"} - @group_manager.expects(:search).with("memberUid=myname").returns([two, one]) + it "should show its group membership as the sorted list of all groups returned by an ldap query of group memberships" do + one = {:name => "one"} + two = {:name => "two"} + @group_manager.expects(:search).with("memberUid=myname").returns([two, one]) - @instance.groups.should == "one,two" - end + @instance.groups.should == "one,two" + end - it "should show its group membership as :absent if no matching groups are found in ldap" do - @group_manager.expects(:search).with("memberUid=myname").returns(nil) + it "should show its group membership as :absent if no matching groups are found in ldap" do + @group_manager.expects(:search).with("memberUid=myname").returns(nil) - @instance.groups.should == :absent - end + @instance.groups.should == :absent + end - it "should cache the group value" do - @group_manager.expects(:search).with("memberUid=myname").once.returns nil + it "should cache the group value" do + @group_manager.expects(:search).with("memberUid=myname").once.returns nil - @instance.groups - @instance.groups.should == :absent - end + @instance.groups + @instance.groups.should == :absent end + end - describe "when modifying group membership" do - before do - @groups = Puppet::Type.type(:group).provider(:ldap) - @group_manager = @groups.manager - provider_class.stubs(:suitable?).returns true + describe "when modifying group membership" do + before do + @groups = Puppet::Type.type(:group).provider(:ldap) + @group_manager = @groups.manager + provider_class.stubs(:suitable?).returns true - @one = {:name => "one", :gid => "500"} - @group_manager.stubs(:find).with("one").returns(@one) + @one = {:name => "one", :gid => "500"} + @group_manager.stubs(:find).with("one").returns(@one) - @two = {:name => "one", :gid => "600"} - @group_manager.stubs(:find).with("two").returns(@two) + @two = {:name => "one", :gid => "600"} + @group_manager.stubs(:find).with("two").returns(@two) - @instance = provider_class.new(:name => "myname") + @instance = provider_class.new(:name => "myname") - @instance.stubs(:groups).returns :absent - end + @instance.stubs(:groups).returns :absent + end - it "should fail if the group does not exist" do - @group_manager.expects(:find).with("mygroup").returns nil + it "should fail if the group does not exist" do + @group_manager.expects(:find).with("mygroup").returns nil - lambda { @instance.groups = "mygroup" }.should raise_error(Puppet::Error) - end + lambda { @instance.groups = "mygroup" }.should raise_error(Puppet::Error) + end - it "should only pass the attributes it cares about to the group manager" do - @group_manager.expects(:update).with { |name, attrs| attrs[:gid].nil? } + it "should only pass the attributes it cares about to the group manager" do + @group_manager.expects(:update).with { |name, attrs| attrs[:gid].nil? } - @instance.groups = "one" - end + @instance.groups = "one" + end - it "should always include :ensure => :present in the current values" do - @group_manager.expects(:update).with { |name, is, should| is[:ensure] == :present } + it "should always include :ensure => :present in the current values" do + @group_manager.expects(:update).with { |name, is, should| is[:ensure] == :present } - @instance.groups = "one" - end + @instance.groups = "one" + end - it "should always include :ensure => :present in the desired values" do - @group_manager.expects(:update).with { |name, is, should| should[:ensure] == :present } + it "should always include :ensure => :present in the desired values" do + @group_manager.expects(:update).with { |name, is, should| should[:ensure] == :present } - @instance.groups = "one" - end + @instance.groups = "one" + end - it "should always pass the group's original member list" do - @one[:members] = %w{yay ness} - @group_manager.expects(:update).with { |name, is, should| is[:members] == %w{yay ness} } + it "should always pass the group's original member list" do + @one[:members] = %w{yay ness} + @group_manager.expects(:update).with { |name, is, should| is[:members] == %w{yay ness} } - @instance.groups = "one" - end + @instance.groups = "one" + end - it "should find the group again when resetting its member list, so it has the full member list" do - @group_manager.expects(:find).with("one").returns(@one) + it "should find the group again when resetting its member list, so it has the full member list" do + @group_manager.expects(:find).with("one").returns(@one) - @group_manager.stubs(:update) + @group_manager.stubs(:update) - @instance.groups = "one" - end + @instance.groups = "one" + end - describe "for groups that have no members" do - it "should create a new members attribute with its value being the user's name" do - @group_manager.expects(:update).with { |name, is, should| should[:members] == %w{myname} } + describe "for groups that have no members" do + it "should create a new members attribute with its value being the user's name" do + @group_manager.expects(:update).with { |name, is, should| should[:members] == %w{myname} } - @instance.groups = "one" - end - end + @instance.groups = "one" + end + end - describe "for groups it is being removed from" do - it "should replace the group's member list with one missing the user's name" do - @one[:members] = %w{myname a} - @two[:members] = %w{myname b} + describe "for groups it is being removed from" do + it "should replace the group's member list with one missing the user's name" do + @one[:members] = %w{myname a} + @two[:members] = %w{myname b} - @group_manager.expects(:update).with { |name, is, should| name == "two" and should[:members] == %w{b} } + @group_manager.expects(:update).with { |name, is, should| name == "two" and should[:members] == %w{b} } - @instance.stubs(:groups).returns "one,two" - @instance.groups = "one" - end + @instance.stubs(:groups).returns "one,two" + @instance.groups = "one" + end - it "should mark the member list as empty if there are no remaining members" do - @one[:members] = %w{myname} - @two[:members] = %w{myname b} + it "should mark the member list as empty if there are no remaining members" do + @one[:members] = %w{myname} + @two[:members] = %w{myname b} - @group_manager.expects(:update).with { |name, is, should| name == "one" and should[:members] == :absent } + @group_manager.expects(:update).with { |name, is, should| name == "one" and should[:members] == :absent } - @instance.stubs(:groups).returns "one,two" - @instance.groups = "two" - end - end + @instance.stubs(:groups).returns "one,two" + @instance.groups = "two" + end + end - describe "for groups that already have members" do - it "should replace each group's member list with a new list including the user's name" do - @one[:members] = %w{a b} - @group_manager.expects(:update).with { |name, is, should| should[:members] == %w{a b myname} } - @two[:members] = %w{b c} - @group_manager.expects(:update).with { |name, is, should| should[:members] == %w{b c myname} } + describe "for groups that already have members" do + it "should replace each group's member list with a new list including the user's name" do + @one[:members] = %w{a b} + @group_manager.expects(:update).with { |name, is, should| should[:members] == %w{a b myname} } + @two[:members] = %w{b c} + @group_manager.expects(:update).with { |name, is, should| should[:members] == %w{b c myname} } - @instance.groups = "one,two" - end - end + @instance.groups = "one,two" + end + end - describe "for groups of which it is a member" do - it "should do nothing" do - @one[:members] = %w{a b} - @group_manager.expects(:update).with { |name, is, should| should[:members] == %w{a b myname} } + describe "for groups of which it is a member" do + it "should do nothing" do + @one[:members] = %w{a b} + @group_manager.expects(:update).with { |name, is, should| should[:members] == %w{a b myname} } - @two[:members] = %w{c myname} - @group_manager.expects(:update).with { |name, *other| name == "two" }.never + @two[:members] = %w{c myname} + @group_manager.expects(:update).with { |name, *other| name == "two" }.never - @instance.stubs(:groups).returns "two" + @instance.stubs(:groups).returns "two" - @instance.groups = "one,two" - end - end + @instance.groups = "one,two" + end end + end end diff --git a/spec/unit/provider/user/user_role_add_spec.rb b/spec/unit/provider/user/user_role_add_spec.rb index 4dbfb83be..211f4260a 100644 --- a/spec/unit/provider/user/user_role_add_spec.rb +++ b/spec/unit/provider/user/user_role_add_spec.rb @@ -5,245 +5,245 @@ 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 - @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 the type set to whatever is passed in" do - @provider.expects(:command).with(:modify).returns("foomod") - @provider.transition("bar").include?("type=bar") - end - end - - describe "when calling create" do - before do - @provider.stubs(:password=) - end - - 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.stubs(:password=) - @provider.create - end - - it "should add -o when the uid is being modified" do - @provider.uid = 150 - end - end - - [: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.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 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 - - describe "when getting the hashed password" do - before do - @array = mock "array" - end - - it "should readlines of /etc/shadow" do - File.expects(:readlines).with("/etc/shadow").returns([]) - @provider.password - end - - it "should reject anything that doesn't start with alpha numerics" do - @array.expects(:reject).returns([]) - File.stubs(:readlines).with("/etc/shadow").returns(@array) - @provider.password - end - - it "should collect splitting on ':'" do - @array.stubs(:reject).returns(@array) - @array.expects(:collect).returns([]) - File.stubs(:readlines).with("/etc/shadow").returns(@array) - @provider.password - end - - it "should find the matching user" do - @resource.stubs(:[]).with(:name).returns("username") - @array.stubs(:reject).returns(@array) - @array.stubs(:collect).returns([["username", "hashedpassword"], ["someoneelse", "theirpassword"]]) - File.stubs(:readlines).with("/etc/shadow").returns(@array) - @provider.password.must == "hashedpassword" - end - - it "should get the right password" do - @resource.stubs(:[]).with(:name).returns("username") - File.stubs(:readlines).with("/etc/shadow").returns(["#comment", " nonsense", " ", "username:hashedpassword:stuff:foo:bar:::", "other:pword:yay:::"]) - @provider.password.must == "hashedpassword" - end - end - - describe "when setting the password" do - #how can you mock these blocks up? - it "should open /etc/shadow for reading and /etc/shadow_tmp for writing" do - File.expects(:open).with("/etc/shadow", "r") - File.stubs(:rename) - @provider.password=("hashedpassword") - end - - it "should rename the /etc/shadow_tmp to /etc/shadow" do - File.stubs(:open).with("/etc/shadow", "r") - File.expects(:rename).with("/etc/shadow_tmp", "/etc/shadow") - @provider.password=("hashedpassword") - end + 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 the type set to whatever is passed in" do + @provider.expects(:command).with(:modify).returns("foomod") + @provider.transition("bar").include?("type=bar") + end + end + + describe "when calling create" do + before do + @provider.stubs(:password=) + end + + 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.stubs(:password=) + @provider.create + end + + it "should add -o when the uid is being modified" do + @provider.uid = 150 + end + end + + [: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.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 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 + + describe "when getting the hashed password" do + before do + @array = mock "array" + end + + it "should readlines of /etc/shadow" do + File.expects(:readlines).with("/etc/shadow").returns([]) + @provider.password + end + + it "should reject anything that doesn't start with alpha numerics" do + @array.expects(:reject).returns([]) + File.stubs(:readlines).with("/etc/shadow").returns(@array) + @provider.password + end + + it "should collect splitting on ':'" do + @array.stubs(:reject).returns(@array) + @array.expects(:collect).returns([]) + File.stubs(:readlines).with("/etc/shadow").returns(@array) + @provider.password + end + + it "should find the matching user" do + @resource.stubs(:[]).with(:name).returns("username") + @array.stubs(:reject).returns(@array) + @array.stubs(:collect).returns([["username", "hashedpassword"], ["someoneelse", "theirpassword"]]) + File.stubs(:readlines).with("/etc/shadow").returns(@array) + @provider.password.must == "hashedpassword" + end + + it "should get the right password" do + @resource.stubs(:[]).with(:name).returns("username") + File.stubs(:readlines).with("/etc/shadow").returns(["#comment", " nonsense", " ", "username:hashedpassword:stuff:foo:bar:::", "other:pword:yay:::"]) + @provider.password.must == "hashedpassword" + end + end + + describe "when setting the password" do + #how can you mock these blocks up? + it "should open /etc/shadow for reading and /etc/shadow_tmp for writing" do + File.expects(:open).with("/etc/shadow", "r") + File.stubs(:rename) + @provider.password=("hashedpassword") + end + + it "should rename the /etc/shadow_tmp to /etc/shadow" do + File.stubs(:open).with("/etc/shadow", "r") + File.expects(:rename).with("/etc/shadow_tmp", "/etc/shadow") + @provider.password=("hashedpassword") + end + end end diff --git a/spec/unit/provider/user/useradd_spec.rb b/spec/unit/provider/user/useradd_spec.rb index 463212e4e..6eb9717b8 100755 --- a/spec/unit/provider/user/useradd_spec.rb +++ b/spec/unit/provider/user/useradd_spec.rb @@ -5,111 +5,111 @@ require File.dirname(__FILE__) + '/../../../spec_helper' 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) + 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.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.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 - # #1360 - it "should add -o when allowdupe is enabled and the user is being created" do - @resource.expects(:allowdupe?).returns true - @provider.expects(:execute).with { |args| args.include?("-o") } - @provider.create + describe "when calling addcmd" do + before do + @resource.stubs(:allowdupe?).returns true + @resource.stubs(:managehome?).returns true end - it "should add -o when allowdupe is enabled and the uid is being modified" do - @resource.expects(:allowdupe?).returns true - @provider.expects(:execute).with { |args| args.include?("-o") } + it "should call command with :add" do + @provider.expects(:command).with(:add) + @provider.addcmd + end - @provider.uid = 150 + it "should add properties" do + @provider.expects(:add_properties).returns([]) + @provider.addcmd 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 + it "should check and add if dup allowed" do + @provider.expects(:check_allow_dup).returns([]) + @provider.addcmd 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 + it "should check and add if home is managed" do + @provider.expects(:check_manage_home).returns([]) + @provider.addcmd 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" + it "should add the resource :name" do + @resource.expects(:[]).with(:name) + @provider.addcmd 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 + 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/provider/zfs/solaris_spec.rb b/spec/unit/provider/zfs/solaris_spec.rb index 8d1d8ccd6..43a88b1c7 100755 --- a/spec/unit/provider/zfs/solaris_spec.rb +++ b/spec/unit/provider/zfs/solaris_spec.rb @@ -5,95 +5,95 @@ require File.dirname(__FILE__) + '/../../../spec_helper' provider_class = Puppet::Type.type(:zfs).provider(:solaris) describe provider_class do - before do - @resource = stub("resource", :name => "myzfs") - @resource.stubs(:[]).with(:name).returns "myzfs" - @resource.stubs(:[]).returns "shouldvalue" - @provider = provider_class.new(@resource) + before do + @resource = stub("resource", :name => "myzfs") + @resource.stubs(:[]).with(:name).returns "myzfs" + @resource.stubs(:[]).returns "shouldvalue" + @provider = provider_class.new(@resource) + end + + it "should have a create method" do + @provider.should respond_to(:create) + end + + it "should have a destroy method" do + @provider.should respond_to(:destroy) + end + + it "should have an exists? method" do + @provider.should respond_to(:exists?) + end + + 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(:mountpoint).returns "/foo" + properties = @provider.add_properties + properties.include?("-o").should == true + properties.include?("mountpoint=/foo").should == true + properties.detect { |a| a.include?("quota") }.should == nil end + end - it "should have a create method" do - @provider.should respond_to(:create) + describe "when calling create" do + it "should call add_properties" do + @provider.stubs(:zfs) + @provider.expects(:add_properties).returns([]) + @provider.create end - it "should have a destroy method" do - @provider.should respond_to(:destroy) + it "should call zfs with create, properties and this zfs" do + @provider.stubs(:add_properties).returns(%w{a b}) + @provider.expects(:zfs).with(:create, "a", "b", @resource[:name]) + @provider.create end + end - it "should have an exists? method" do - @provider.should respond_to(:exists?) + describe "when calling destroy" do + it "should call zfs with :destroy and this zfs" do + @provider.expects(:zfs).with(:destroy, @resource[:name]) + @provider.destroy end + end - 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(:mountpoint).returns "/foo" - properties = @provider.add_properties - properties.include?("-o").should == true - properties.include?("mountpoint=/foo").should == true - properties.detect { |a| a.include?("quota") }.should == nil - end + describe "when calling exist?" do + it "should call zfs with :list" do + #return stuff because we have to slice and dice it + @provider.expects(:zfs).with(:list).returns("NAME USED AVAIL REFER MOUNTPOINT\nmyzfs 100K 27.4M /myzfs") + @provider.exists? end - describe "when calling create" do - it "should call add_properties" do - @provider.stubs(:zfs) - @provider.expects(:add_properties).returns([]) - @provider.create - end - - it "should call zfs with create, properties and this zfs" do - @provider.stubs(:add_properties).returns(%w{a b}) - @provider.expects(:zfs).with(:create, "a", "b", @resource[:name]) - @provider.create - end + it "should return true if returned values match the name" do + @provider.stubs(:zfs).with(:list).returns("NAME USED AVAIL REFER MOUNTPOINT\n#{@resource[:name]} 100K 27.4M /myzfs") + @provider.exists?.should == true end - describe "when calling destroy" do - it "should call zfs with :destroy and this zfs" do - @provider.expects(:zfs).with(:destroy, @resource[:name]) - @provider.destroy - end + it "should return false if returned values don't match the name" do + @provider.stubs(:zfs).with(:list).returns("no soup for you") + @provider.exists?.should == false end - describe "when calling exist?" do - it "should call zfs with :list" do - #return stuff because we have to slice and dice it - @provider.expects(:zfs).with(:list).returns("NAME USED AVAIL REFER MOUNTPOINT\nmyzfs 100K 27.4M /myzfs") - @provider.exists? - end + end - it "should return true if returned values match the name" do - @provider.stubs(:zfs).with(:list).returns("NAME USED AVAIL REFER MOUNTPOINT\n#{@resource[:name]} 100K 27.4M /myzfs") - @provider.exists?.should == true - end - - it "should return false if returned values don't match the name" do - @provider.stubs(:zfs).with(:list).returns("no soup for you") - @provider.exists?.should == false - end + [:mountpoint, :compression, :copies, :quota, :reservation, :sharenfs, :snapdir].each do |prop| + describe "when getting the #{prop} value" do + it "should call zfs with :get, #{prop} and this zfs" do + @provider.expects(:zfs).with(:get, "-H", "-o", "value", prop, @resource[:name]).returns("value\n") + @provider.send(prop) + end + it "should get the third value of the second line from the output" do + @provider.stubs(:zfs).with(:get, "-H", "-o", "value", prop, @resource[:name]).returns("value\n") + @provider.send(prop).should == "value" + end end - [:mountpoint, :compression, :copies, :quota, :reservation, :sharenfs, :snapdir].each do |prop| - describe "when getting the #{prop} value" do - it "should call zfs with :get, #{prop} and this zfs" do - @provider.expects(:zfs).with(:get, "-H", "-o", "value", prop, @resource[:name]).returns("value\n") - @provider.send(prop) - end - - it "should get the third value of the second line from the output" do - @provider.stubs(:zfs).with(:get, "-H", "-o", "value", prop, @resource[:name]).returns("value\n") - @provider.send(prop).should == "value" - end - end - - describe "when setting the #{prop} value" do - it "should call zfs with :set, #{prop}=value and this zfs" do - @provider.expects(:zfs).with(:set, "#{prop}=value", @resource[:name]) - @provider.send("#{prop}=".intern, "value") - end - end + describe "when setting the #{prop} value" do + it "should call zfs with :set, #{prop}=value and this zfs" do + @provider.expects(:zfs).with(:set, "#{prop}=value", @resource[:name]) + @provider.send("#{prop}=".intern, "value") + end end + end end diff --git a/spec/unit/provider/zone/solaris_spec.rb b/spec/unit/provider/zone/solaris_spec.rb index 0459e5e6c..44416aa61 100755 --- a/spec/unit/provider/zone/solaris_spec.rb +++ b/spec/unit/provider/zone/solaris_spec.rb @@ -5,51 +5,51 @@ require File.dirname(__FILE__) + '/../../../spec_helper' provider_class = Puppet::Type.type(:zone).provider(:solaris) describe provider_class do - before do - @resource = stub("resource", :name => "mypool") - @resource.stubs(:[]).returns "shouldvalue" - @provider = provider_class.new(@resource) + before do + @resource = stub("resource", :name => "mypool") + @resource.stubs(:[]).returns "shouldvalue" + @provider = provider_class.new(@resource) + end + + describe "when calling configure" do + it "should add the create args to the create str" do + @resource.stubs(:properties).returns([]) + @resource.stubs(:[]).with(:create_args).returns("create_args") + @provider.expects(:setconfig).with("create -b create_args\nset zonepath=shouldvalue\ncommit\n") + @provider.configure end + end - describe "when calling configure" do - it "should add the create args to the create str" do - @resource.stubs(:properties).returns([]) - @resource.stubs(:[]).with(:create_args).returns("create_args") - @provider.expects(:setconfig).with("create -b create_args\nset zonepath=shouldvalue\ncommit\n") - @provider.configure - end + describe "when installing" do + it "should call zoneadm" do + @provider.expects(:zoneadm) + @provider.install end - describe "when installing" do - it "should call zoneadm" do - @provider.expects(:zoneadm) - @provider.install - end - - describe "when cloning" do - before { @resource.stubs(:[]).with(:clone).returns(:clone_argument) } - - it "sohuld clone with the resource's clone attribute" do - @provider.expects(:zoneadm).with(:clone, :clone_argument) - @provider.install - end - end - - describe "when not cloning" do - before { @resource.stubs(:[]).with(:clone).returns(nil)} - - it "should just install if there are no install args" do - @resource.stubs(:[]).with(:install_args).returns(nil) - @provider.expects(:zoneadm).with(:install) - @provider.install - end - - it "should add the install args to the command if they exist" do - @resource.stubs(:[]).with(:install_args).returns("install args") - @provider.expects(:zoneadm).with(:install, ["install", "args"]) - @provider.install - end - end + describe "when cloning" do + before { @resource.stubs(:[]).with(:clone).returns(:clone_argument) } + + it "sohuld clone with the resource's clone attribute" do + @provider.expects(:zoneadm).with(:clone, :clone_argument) + @provider.install + end + end + + describe "when not cloning" do + before { @resource.stubs(:[]).with(:clone).returns(nil)} + + it "should just install if there are no install args" do + @resource.stubs(:[]).with(:install_args).returns(nil) + @provider.expects(:zoneadm).with(:install) + @provider.install + end + + it "should add the install args to the command if they exist" do + @resource.stubs(:[]).with(:install_args).returns("install args") + @provider.expects(:zoneadm).with(:install, ["install", "args"]) + @provider.install + end end + end end diff --git a/spec/unit/provider/zpool/solaris_spec.rb b/spec/unit/provider/zpool/solaris_spec.rb index b52ddf8f5..99e6997e4 100755 --- a/spec/unit/provider/zpool/solaris_spec.rb +++ b/spec/unit/provider/zpool/solaris_spec.rb @@ -5,175 +5,175 @@ require File.dirname(__FILE__) + '/../../../spec_helper' provider_class = Puppet::Type.type(:zpool).provider(:solaris) describe provider_class do - before do - @resource = stub("resource", :name => "mypool") - @resource.stubs(:[]).returns "shouldvalue" - @provider = provider_class.new(@resource) + before do + @resource = stub("resource", :name => "mypool") + @resource.stubs(:[]).returns "shouldvalue" + @provider = provider_class.new(@resource) + end + + describe "when getting the instance" do + it "should call process_zpool_data with the result of get_pool_data only once" do + @provider.stubs(:get_pool_data).returns(["foo", "disk"]) + @provider.expects(:process_zpool_data).with(["foo", "disk"]).returns("stuff").once + @provider.current_pool + @provider.current_pool end - - describe "when getting the instance" do - it "should call process_zpool_data with the result of get_pool_data only once" do - @provider.stubs(:get_pool_data).returns(["foo", "disk"]) - @provider.expects(:process_zpool_data).with(["foo", "disk"]).returns("stuff").once - @provider.current_pool - @provider.current_pool - end + end + + describe "when calling flush" do + it "should need to reload the pool" do + @provider.stubs(:get_pool_data) + @provider.expects(:process_zpool_data).returns("stuff").times(2) + @provider.current_pool + @provider.flush + @provider.current_pool end + end - describe "when calling flush" do - it "should need to reload the pool" do - @provider.stubs(:get_pool_data) - @provider.expects(:process_zpool_data).returns("stuff").times(2) - @provider.current_pool - @provider.flush - @provider.current_pool - end + describe "when procesing zpool data" do + before do + @zpool_data = ["foo", "disk"] end - describe "when procesing zpool data" do - before do - @zpool_data = ["foo", "disk"] - end - - describe "when there is no data" do - it "should return a hash with ensure=>:absent" do - @provider.process_zpool_data([])[:ensure].should == :absent - end - end - - describe "when there is a spare" do - it "should add the spare disk to the hash" do - @zpool_data += ["spares", "spare_disk"] - @provider.process_zpool_data(@zpool_data)[:spare].should == ["spare_disk"] - end - end + describe "when there is no data" do + it "should return a hash with ensure=>:absent" do + @provider.process_zpool_data([])[:ensure].should == :absent + end + end - describe "when there are two spares" do - it "should add the spare disk to the hash as a single string" do - @zpool_data += ["spares", "spare_disk", "spare_disk2"] - @provider.process_zpool_data(@zpool_data)[:spare].should == ["spare_disk spare_disk2"] - end - end + describe "when there is a spare" do + it "should add the spare disk to the hash" do + @zpool_data += ["spares", "spare_disk"] + @provider.process_zpool_data(@zpool_data)[:spare].should == ["spare_disk"] + end + end - describe "when there is a log" do - it "should add the log disk to the hash" do - @zpool_data += ["logs", "log_disk"] - @provider.process_zpool_data(@zpool_data)[:log].should == ["log_disk"] - end - end + describe "when there are two spares" do + it "should add the spare disk to the hash as a single string" do + @zpool_data += ["spares", "spare_disk", "spare_disk2"] + @provider.process_zpool_data(@zpool_data)[:spare].should == ["spare_disk spare_disk2"] + end + end - describe "when there are two logs" do - it "should add the log disks to the hash as a single string" do - @zpool_data += ["spares", "spare_disk", "spare_disk2"] - @provider.process_zpool_data(@zpool_data)[:spare].should == ["spare_disk spare_disk2"] - end - end + describe "when there is a log" do + it "should add the log disk to the hash" do + @zpool_data += ["logs", "log_disk"] + @provider.process_zpool_data(@zpool_data)[:log].should == ["log_disk"] + end + end - describe "when the vdev is a single mirror" do - it "should call create_multi_array with mirror" do - @zpool_data = ["mirrorpool", "mirror", "disk1", "disk2"] - @provider.process_zpool_data(@zpool_data)[:mirror].should == ["disk1 disk2"] - end - end + describe "when there are two logs" do + it "should add the log disks to the hash as a single string" do + @zpool_data += ["spares", "spare_disk", "spare_disk2"] + @provider.process_zpool_data(@zpool_data)[:spare].should == ["spare_disk spare_disk2"] + end + end - describe "when the vdev is a double mirror" do - it "should call create_multi_array with mirror" do - @zpool_data = ["mirrorpool", "mirror", "disk1", "disk2", "mirror", "disk3", "disk4"] - @provider.process_zpool_data(@zpool_data)[:mirror].should == ["disk1 disk2", "disk3 disk4"] - end - end + describe "when the vdev is a single mirror" do + it "should call create_multi_array with mirror" do + @zpool_data = ["mirrorpool", "mirror", "disk1", "disk2"] + @provider.process_zpool_data(@zpool_data)[:mirror].should == ["disk1 disk2"] + end + end - describe "when the vdev is a raidz1" do - it "should call create_multi_array with raidz1" do - @zpool_data = ["mirrorpool", "raidz1", "disk1", "disk2"] - @provider.process_zpool_data(@zpool_data)[:raidz].should == ["disk1 disk2"] - end - end + describe "when the vdev is a double mirror" do + it "should call create_multi_array with mirror" do + @zpool_data = ["mirrorpool", "mirror", "disk1", "disk2", "mirror", "disk3", "disk4"] + @provider.process_zpool_data(@zpool_data)[:mirror].should == ["disk1 disk2", "disk3 disk4"] + end + end - describe "when the vdev is a raidz2" do - it "should call create_multi_array with raidz2 and set the raid_parity" do - @zpool_data = ["mirrorpool", "raidz2", "disk1", "disk2"] - pool = @provider.process_zpool_data(@zpool_data) - pool[:raidz].should == ["disk1 disk2"] - pool[:raid_parity].should == "raidz2" - end - end + describe "when the vdev is a raidz1" do + it "should call create_multi_array with raidz1" do + @zpool_data = ["mirrorpool", "raidz1", "disk1", "disk2"] + @provider.process_zpool_data(@zpool_data)[:raidz].should == ["disk1 disk2"] + end end - describe "when calling the getters and setters" do - [:disk, :mirror, :raidz, :log, :spare].each do |field| - describe "when calling #{field}" do - it "should get the #{field} value from the current_pool hash" do - pool_hash = mock "pool hash" - pool_hash.expects(:[]).with(field) - @provider.stubs(:current_pool).returns(pool_hash) - @provider.send(field) - end - end - - describe "when setting the #{field}" do - it "should warn the #{field} values were not in sync" do - Puppet.expects(:warning).with("NO CHANGES BEING MADE: zpool #{field} does not match, should be 'shouldvalue' currently is 'currentvalue'") - @provider.stubs(:current_pool).returns(Hash.new("currentvalue")) - @provider.send((field.to_s + "=").intern, "shouldvalue") - end - end - end + describe "when the vdev is a raidz2" do + it "should call create_multi_array with raidz2 and set the raid_parity" do + @zpool_data = ["mirrorpool", "raidz2", "disk1", "disk2"] + pool = @provider.process_zpool_data(@zpool_data) + pool[:raidz].should == ["disk1 disk2"] + pool[:raid_parity].should == "raidz2" + end end + end + + describe "when calling the getters and setters" do + [:disk, :mirror, :raidz, :log, :spare].each do |field| + describe "when calling #{field}" do + it "should get the #{field} value from the current_pool hash" do + pool_hash = mock "pool hash" + pool_hash.expects(:[]).with(field) + @provider.stubs(:current_pool).returns(pool_hash) + @provider.send(field) + end + end + + describe "when setting the #{field}" do + it "should warn the #{field} values were not in sync" do + Puppet.expects(:warning).with("NO CHANGES BEING MADE: zpool #{field} does not match, should be 'shouldvalue' currently is 'currentvalue'") + @provider.stubs(:current_pool).returns(Hash.new("currentvalue")) + @provider.send((field.to_s + "=").intern, "shouldvalue") + end + end + end + end - describe "when calling create" do - before do - @resource.stubs(:[]).with(:pool).returns("mypool") - @provider.stubs(:zpool) - end + describe "when calling create" do + before do + @resource.stubs(:[]).with(:pool).returns("mypool") + @provider.stubs(:zpool) + end - it "should call build_vdevs" do - @provider.expects(:build_vdevs).returns([]) - @provider.create - end + it "should call build_vdevs" do + @provider.expects(:build_vdevs).returns([]) + @provider.create + end - it "should call build_named with 'spares' and 'log" do - @provider.expects(:build_named).with("spare").returns([]) - @provider.expects(:build_named).with("log").returns([]) - @provider.create - end + it "should call build_named with 'spares' and 'log" do + @provider.expects(:build_named).with("spare").returns([]) + @provider.expects(:build_named).with("log").returns([]) + @provider.create + end - it "should call zpool with arguments from build_vdevs and build_named" do - @provider.expects(:zpool).with(:create, 'mypool', 'shouldvalue', 'spare', 'shouldvalue', 'log', 'shouldvalue') - @provider.create - end + it "should call zpool with arguments from build_vdevs and build_named" do + @provider.expects(:zpool).with(:create, 'mypool', 'shouldvalue', 'spare', 'shouldvalue', 'log', 'shouldvalue') + @provider.create end + end - describe "when calling delete" do - it "should call zpool with destroy and the pool name" do - @resource.stubs(:[]).with(:pool).returns("poolname") - @provider.expects(:zpool).with(:destroy, "poolname") - @provider.delete - end + describe "when calling delete" do + it "should call zpool with destroy and the pool name" do + @resource.stubs(:[]).with(:pool).returns("poolname") + @provider.expects(:zpool).with(:destroy, "poolname") + @provider.delete end + end - describe "when calling exists?" do - before do - @current_pool = Hash.new(:absent) - @provider.stubs(:get_pool_data).returns([]) - @provider.stubs(:process_zpool_data).returns(@current_pool) - end + describe "when calling exists?" do + before do + @current_pool = Hash.new(:absent) + @provider.stubs(:get_pool_data).returns([]) + @provider.stubs(:process_zpool_data).returns(@current_pool) + end - it "should get the current pool" do - @provider.expects(:process_zpool_data).returns(@current_pool) - @provider.exists? - end + it "should get the current pool" do + @provider.expects(:process_zpool_data).returns(@current_pool) + @provider.exists? + end - it "should return false if the current_pool is absent" do - #the before sets it up - @provider.exists?.should == false - end + it "should return false if the current_pool is absent" do + #the before sets it up + @provider.exists?.should == false + end - it "should return true if the current_pool has values" do - @current_pool[:pool] = "mypool" - @provider.exists?.should == true - end + it "should return true if the current_pool has values" do + @current_pool[:pool] = "mypool" + @provider.exists?.should == true end + end end |