summaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authorNick Lewis <nick@puppetlabs.com>2011-08-15 15:51:37 -0700
committerNick Lewis <nick@puppetlabs.com>2011-08-16 11:33:26 -0700
commitb28bcb031346cfd2026361ec5ffb420c1dcf60d7 (patch)
treec9bfc608ea4dc201a7724f6f376c63fdfb4920b3 /spec
parentc9c331bda6f4220d3fde636cdfdc7b0ae449c005 (diff)
downloadpuppet-b28bcb031346cfd2026361ec5ffb420c1dcf60d7.tar.gz
puppet-b28bcb031346cfd2026361ec5ffb420c1dcf60d7.tar.xz
puppet-b28bcb031346cfd2026361ec5ffb420c1dcf60d7.zip
(#5495) Remove dead Windows-specific code from posix exec provider
Because this provider only applies when the posix feature is present (and thus not the windows feature), it can never be used on Windows. Thus, the Windows-specific command handling is unnecessary and unused. Also added more specific error messages for the cases where a command doesn't exist, isn't a file, and isn't executable. These only apply when the command path is absolute (otherwise the message is simply command not found). Reviewed-By: Matt Robinson <matt@puppetlabs.com>
Diffstat (limited to 'spec')
-rwxr-xr-xspec/unit/provider/exec/posix_spec.rb209
1 files changed, 104 insertions, 105 deletions
diff --git a/spec/unit/provider/exec/posix_spec.rb b/spec/unit/provider/exec/posix_spec.rb
index 50697d826..876b9724d 100755
--- a/spec/unit/provider/exec/posix_spec.rb
+++ b/spec/unit/provider/exec/posix_spec.rb
@@ -1,120 +1,119 @@
#!/usr/bin/env rspec
require 'spec_helper'
-provider_class = Puppet::Type.type(:exec).provider(:posix)
+describe Puppet::Type.type(:exec).provider(:posix) do
+ include PuppetSpec::Files
+
+ def make_exe
+ command = tmpfile('my_command')
+ FileUtils.touch(command)
+ File.chmod(0755, command)
+ command
+ end
+
+ let(:resource) { Puppet::Type.type(:exec).new(:title => '/foo') }
+ let(:provider) { described_class.new(resource) }
-describe provider_class do
before :each do
- @resource = Puppet::Resource.new(:exec, 'foo')
- @provider = provider_class.new(@resource)
+ Puppet.features.stubs(:posix?).returns(true)
+ Puppet.features.stubs(:microsoft_windows?).returns(false)
+ end
+
+ describe "#validatecmd" do
+ it "should fail if no path is specified and the command is not fully qualified" do
+ expect { provider.validatecmd("foo") }.to raise_error(
+ Puppet::Error,
+ "'foo' is not qualified and no path was specified. Please qualify the command or specify a path."
+ )
+ end
+
+ it "should pass if a path is given" do
+ provider.resource[:path] = ['/bogus/bin']
+ provider.validatecmd("../foo")
+ end
+
+ it "should pass if command is fully qualifed" do
+ provider.resource[:path] = ['/bogus/bin']
+ provider.validatecmd("/bin/blah/foo")
+ end
end
- ["posix", "microsoft_windows"].each do |feature|
- describe "when in #{feature} environment" do
- before :each do
- if feature == "microsoft_windows"
- Puppet.features.stubs(:microsoft_windows?).returns(true)
- Puppet.features.stubs(:posix?).returns(false)
- else
- Puppet.features.stubs(:posix?).returns(true)
- Puppet.features.stubs(:microsoft_windows?).returns(false)
- end
+ describe "#run" do
+ describe "when the command is an absolute path" do
+ let(:command) { tmpfile('foo') }
+
+ it "should fail if the command doesn't exist" do
+ expect { provider.run(command) }.to raise_error(ArgumentError, "Could not find command '#{command}'")
+ end
+
+ it "should fail if the command isn't a file" do
+ FileUtils.mkdir(command)
+ FileUtils.chmod(0755, command)
+
+ expect { provider.run(command) }.to raise_error(ArgumentError, "'#{command}' is a directory, not a file")
+ end
+
+ it "should fail if the command isn't executable" do
+ FileUtils.touch(command)
+
+ expect { provider.run(command) }.to raise_error(ArgumentError, "'#{command}' is not executable")
+ end
+ end
+
+ describe "when the command is a relative path" do
+ it "should execute the command if it finds it in the path and is executable" do
+ command = make_exe
+ provider.resource[:path] = [File.dirname(command)]
+ filename = File.basename(command)
+
+ Puppet::Util.expects(:execute).with { |cmdline, arguments| (cmdline == [filename]) && (arguments.is_a? Hash) }
+
+ provider.run(filename)
end
- describe "#validatecmd" do
- it "should fail if no path is specified and the command is not fully qualified" do
- lambda { @provider.validatecmd("foo") }.should raise_error(
- Puppet::Error,
- "'foo' is not qualified and no path was specified. Please qualify the command or specify a path."
- )
- end
-
- it "should pass if a path is given" do
- @provider.resource[:path] = ['/bogus/bin']
- @provider.validatecmd("../foo")
- end
-
- it "should pass if command is fully qualifed" do
- @provider.resource[:path] = ['/bogus/bin']
- @provider.validatecmd("/bin/blah/foo")
- end
+ it "should fail if the command isn't in the path" do
+ resource[:path] = ["/fake/path"]
+
+ expect { provider.run('foo') }.to raise_error(ArgumentError, "Could not find command 'foo'")
end
- describe "#run" do
- it "should fail if no path is specified and command does not exist" do
- lambda { @provider.run("foo") }.should raise_error(ArgumentError, "Could not find command 'foo'")
- end
-
- it "should fail if the command isn't in the path" do
- @provider.resource[:path] = ['/bogus/bin']
- lambda { @provider.run("foo") }.should raise_error(ArgumentError, "Could not find command 'foo'")
- end
-
- it "should fail if the command isn't executable" do
- @provider.resource[:path] = ['/bogus/bin']
- File.stubs(:exists?).with("foo").returns(true)
-
- lambda { @provider.run("foo") }.should raise_error(ArgumentError, "'foo' is not executable")
- end
-
- it "should not be able to execute shell builtins" do
- @provider.resource[:path] = ['/bin']
- lambda { @provider.run("cd ..") }.should raise_error(ArgumentError, "Could not find command 'cd'")
- end
-
- it "should execute the command if the command given includes arguments or subcommands" do
- @provider.resource[:path] = ['/bogus/bin']
- File.stubs(:exists?).returns(false)
- File.stubs(:exists?).with("foo").returns(true)
- File.stubs(:executable?).with("foo").returns(true)
-
- Puppet::Util.expects(:execute).with() { |command, arguments| (command == ['foo bar --sillyarg=true --blah']) && (arguments.is_a? Hash) }
- @provider.run("foo bar --sillyarg=true --blah")
- end
-
- it "should fail if quoted command doesn't exist" do
- @provider.resource[:path] = ['/bogus/bin']
- File.stubs(:exists?).returns(false)
- File.stubs(:exists?).with("foo").returns(true)
- File.stubs(:executable?).with("foo").returns(true)
-
- lambda { @provider.run('"foo bar --sillyarg=true --blah"') }.should raise_error(ArgumentError, "Could not find command 'foo bar --sillyarg=true --blah'")
- end
-
- it "should execute the command if it finds it in the path and is executable" do
- @provider.resource[:path] = ['/bogus/bin']
- File.stubs(:exists?).with("foo").returns(true)
- File.stubs(:executable?).with("foo").returns(true)
- Puppet::Util.expects(:execute).with() { |command, arguments| (command == ['foo']) && (arguments.is_a? Hash) }
-
- @provider.run("foo")
- end
-
- if feature == "microsoft_windows"
- [".exe", ".ps1", ".bat", ".com", ""].each do |extension|
- it "should check file extension #{extension} when it can't find the executable" do
- @provider.resource[:path] = ['/bogus/bin']
- File.stubs(:exists?).returns(false)
- File.stubs(:exists?).with("/bogus/bin/foo#{extension}").returns(true)
- File.stubs(:executable?).with("foo").returns(true)
- Puppet::Util.expects(:execute).with() { |command, arguments| (command == ['foo']) && (arguments.is_a? Hash) }
-
- @provider.run("foo")
- end
- end
- end
-
- it "should warn if you're overriding something in environment" do
- @provider.resource[:environment] = ['WHATEVER=/something/else', 'WHATEVER=/foo']
- File.stubs(:exists?).returns(false)
- File.stubs(:exists?).with("foo").returns(true)
- File.stubs(:executable?).with("foo").returns(true)
-
- Puppet::Util.expects(:execute).with() { |command, arguments| (command == ['foo']) && (arguments.is_a? Hash) }
- @provider.run("foo")
- @logs.map {|l| "#{l.level}: #{l.message}" }.should == ["warning: Overriding environment setting 'WHATEVER' with '/foo'"]
- end
+ it "should fail if the command is in the path but not executable" do
+ command = tmpfile('foo')
+ FileUtils.touch(command)
+ resource[:path] = [File.dirname(command)]
+ filename = File.basename(command)
+
+ expect { provider.run(filename) }.to raise_error(ArgumentError, "Could not find command '#{filename}'")
end
end
+
+ it "should not be able to execute shell builtins" do
+ provider.resource[:path] = ['/bin']
+ expect { provider.run("cd ..") }.to raise_error(ArgumentError, "Could not find command 'cd'")
+ end
+
+ it "should execute the command if the command given includes arguments or subcommands" do
+ provider.resource[:path] = ['/bogus/bin']
+ command = make_exe
+
+ Puppet::Util.expects(:execute).with { |cmdline, arguments| (cmdline == ["#{command} bar --sillyarg=true --blah"]) && (arguments.is_a? Hash) }
+ provider.run("#{command} bar --sillyarg=true --blah")
+ end
+
+ it "should fail if quoted command doesn't exist" do
+ provider.resource[:path] = ['/bogus/bin']
+ command = "/foo bar --sillyarg=true --blah"
+
+ expect { provider.run(%Q["#{command}"]) }.to raise_error(ArgumentError, "Could not find command '#{command}'")
+ end
+
+ it "should warn if you're overriding something in environment" do
+ provider.resource[:environment] = ['WHATEVER=/something/else', 'WHATEVER=/foo']
+ command = make_exe
+
+ Puppet::Util.expects(:execute).with { |cmdline, arguments| (cmdline== [command]) && (arguments.is_a? Hash) }
+ provider.run(command)
+ @logs.map {|l| "#{l.level}: #{l.message}" }.should == ["warning: Overriding environment setting 'WHATEVER' with '/foo'"]
+ end
end
end