sudo: Fix tests.

This commit is contained in:
Ilya Kulakov 2023-04-26 19:51:26 -07:00
parent 476d97934f
commit 6a4322833f
8 changed files with 108 additions and 33 deletions

View File

@ -16,6 +16,7 @@ describe Cask::Artifact::Pkg, :cask do
"/usr/sbin/installer", "/usr/sbin/installer",
args: ["-pkg", cask.staged_path.join("MyFancyPkg", "Fancy.pkg"), "-target", "/"], args: ["-pkg", cask.staged_path.join("MyFancyPkg", "Fancy.pkg"), "-target", "/"],
sudo: true, sudo: true,
sudo_as_root: true,
print_stdout: true, print_stdout: true,
env: { env: {
"LOGNAME" => ENV.fetch("USER"), "LOGNAME" => ENV.fetch("USER"),
@ -65,6 +66,7 @@ describe Cask::Artifact::Pkg, :cask do
cask.staged_path.join("/tmp/choices.xml") cask.staged_path.join("/tmp/choices.xml")
], ],
sudo: true, sudo: true,
sudo_as_root: true,
print_stdout: true, print_stdout: true,
env: { env: {
"LOGNAME" => ENV.fetch("USER"), "LOGNAME" => ENV.fetch("USER"),

View File

@ -31,14 +31,26 @@ shared_examples "#uninstall_phase or #zap_phase" do
it "works when job is owned by user" do it "works when job is owned by user" do
allow(fake_system_command).to receive(:run) allow(fake_system_command).to receive(:run)
.with("/bin/launchctl", args: ["list", "my.fancy.package.service"], print_stderr: false, sudo: false) .with(
"/bin/launchctl",
args: ["list", "my.fancy.package.service"],
print_stderr: false,
sudo: false,
sudo_as_root: false,
)
.and_return(instance_double(SystemCommand::Result, stdout: service_info)) .and_return(instance_double(SystemCommand::Result, stdout: service_info))
allow(fake_system_command).to receive(:run) allow(fake_system_command).to receive(:run)
.with("/bin/launchctl", args: ["list", "my.fancy.package.service"], print_stderr: false, sudo: true) .with(
"/bin/launchctl",
args: ["list", "my.fancy.package.service"],
print_stderr: false,
sudo: true,
sudo_as_root: true,
)
.and_return(instance_double(SystemCommand::Result, stdout: unknown_response)) .and_return(instance_double(SystemCommand::Result, stdout: unknown_response))
expect(fake_system_command).to receive(:run!) expect(fake_system_command).to receive(:run!)
.with("/bin/launchctl", args: ["remove", "my.fancy.package.service"], sudo: false) .with("/bin/launchctl", args: ["remove", "my.fancy.package.service"], sudo: false, sudo_as_root: false)
.and_return(instance_double(SystemCommand::Result)) .and_return(instance_double(SystemCommand::Result))
subject.public_send(:"#{artifact_dsl_key}_phase", command: fake_system_command) subject.public_send(:"#{artifact_dsl_key}_phase", command: fake_system_command)
@ -46,14 +58,26 @@ shared_examples "#uninstall_phase or #zap_phase" do
it "works when job is owned by system" do it "works when job is owned by system" do
allow(fake_system_command).to receive(:run) allow(fake_system_command).to receive(:run)
.with("/bin/launchctl", args: ["list", "my.fancy.package.service"], print_stderr: false, sudo: false) .with(
"/bin/launchctl",
args: ["list", "my.fancy.package.service"],
print_stderr: false,
sudo: false,
sudo_as_root: false,
)
.and_return(instance_double(SystemCommand::Result, stdout: unknown_response)) .and_return(instance_double(SystemCommand::Result, stdout: unknown_response))
allow(fake_system_command).to receive(:run) allow(fake_system_command).to receive(:run)
.with("/bin/launchctl", args: ["list", "my.fancy.package.service"], print_stderr: false, sudo: true) .with(
"/bin/launchctl",
args: ["list", "my.fancy.package.service"],
print_stderr: false,
sudo: true,
sudo_as_root: true,
)
.and_return(instance_double(SystemCommand::Result, stdout: service_info)) .and_return(instance_double(SystemCommand::Result, stdout: service_info))
expect(fake_system_command).to receive(:run!) expect(fake_system_command).to receive(:run!)
.with("/bin/launchctl", args: ["remove", "my.fancy.package.service"], sudo: true) .with("/bin/launchctl", args: ["remove", "my.fancy.package.service"], sudo: true, sudo_as_root: true)
.and_return(instance_double(SystemCommand::Result)) .and_return(instance_double(SystemCommand::Result))
subject.public_send(:"#{artifact_dsl_key}_phase", command: fake_system_command) subject.public_send(:"#{artifact_dsl_key}_phase", command: fake_system_command)
@ -94,14 +118,26 @@ shared_examples "#uninstall_phase or #zap_phase" do
.and_return(["my.fancy.package.service.12345"]) .and_return(["my.fancy.package.service.12345"])
allow(fake_system_command).to receive(:run) allow(fake_system_command).to receive(:run)
.with("/bin/launchctl", args: ["list", "my.fancy.package.service.12345"], print_stderr: false, sudo: false) .with(
"/bin/launchctl",
args: ["list", "my.fancy.package.service.12345"],
print_stderr: false,
sudo: false,
sudo_as_root: false,
)
.and_return(instance_double(SystemCommand::Result, stdout: unknown_response)) .and_return(instance_double(SystemCommand::Result, stdout: unknown_response))
allow(fake_system_command).to receive(:run) allow(fake_system_command).to receive(:run)
.with("/bin/launchctl", args: ["list", "my.fancy.package.service.12345"], print_stderr: false, sudo: true) .with(
"/bin/launchctl",
args: ["list", "my.fancy.package.service.12345"],
print_stderr: false,
sudo: true,
sudo_as_root: true,
)
.and_return(instance_double(SystemCommand::Result, stdout: service_info)) .and_return(instance_double(SystemCommand::Result, stdout: service_info))
expect(fake_system_command).to receive(:run!) expect(fake_system_command).to receive(:run!)
.with("/bin/launchctl", args: ["remove", "my.fancy.package.service.12345"], sudo: true) .with("/bin/launchctl", args: ["remove", "my.fancy.package.service.12345"], sudo: true, sudo_as_root: true)
.and_return(instance_double(SystemCommand::Result)) .and_return(instance_double(SystemCommand::Result))
subject.public_send(:"#{artifact_dsl_key}_phase", command: fake_system_command) subject.public_send(:"#{artifact_dsl_key}_phase", command: fake_system_command)
@ -148,19 +184,19 @@ shared_examples "#uninstall_phase or #zap_phase" do
it "is supported" do it "is supported" do
allow(subject).to receive(:system_command!) allow(subject).to receive(:system_command!)
.with("/usr/sbin/kextstat", args: ["-l", "-b", kext_id], sudo: true) .with("/usr/sbin/kextstat", args: ["-l", "-b", kext_id], sudo: true, sudo_as_root: true)
.and_return(instance_double("SystemCommand::Result", stdout: "loaded")) .and_return(instance_double("SystemCommand::Result", stdout: "loaded"))
expect(subject).to receive(:system_command!) expect(subject).to receive(:system_command!)
.with("/sbin/kextunload", args: ["-b", kext_id], sudo: true) .with("/sbin/kextunload", args: ["-b", kext_id], sudo: true, sudo_as_root: true)
.and_return(instance_double("SystemCommand::Result")) .and_return(instance_double("SystemCommand::Result"))
expect(subject).to receive(:system_command!) expect(subject).to receive(:system_command!)
.with("/usr/sbin/kextfind", args: ["-b", kext_id], sudo: true) .with("/usr/sbin/kextfind", args: ["-b", kext_id], sudo: true, sudo_as_root: true)
.and_return(instance_double("SystemCommand::Result", stdout: "/Library/Extensions/FancyPackage.kext\n")) .and_return(instance_double("SystemCommand::Result", stdout: "/Library/Extensions/FancyPackage.kext\n"))
expect(subject).to receive(:system_command!) expect(subject).to receive(:system_command!)
.with("/bin/rm", args: ["-rf", "/Library/Extensions/FancyPackage.kext"], sudo: true) .with("/bin/rm", args: ["-rf", "/Library/Extensions/FancyPackage.kext"], sudo: true, sudo_as_root: true)
subject.public_send(:"#{artifact_dsl_key}_phase", command: fake_system_command) subject.public_send(:"#{artifact_dsl_key}_phase", command: fake_system_command)
end end
@ -281,13 +317,14 @@ shared_examples "#uninstall_phase or #zap_phase" do
it "is supported" do it "is supported" do
allow(fake_system_command).to receive(:run).with(any_args).and_call_original allow(fake_system_command).to receive(:run).with(any_args).and_call_original
expect(fake_system_command).to receive(:run).with( expect(fake_system_command).to receive(:run)
cask.staged_path.join("MyFancyPkg", "FancyUninstaller.tool"), .with(
args: ["--please"], cask.staged_path.join("MyFancyPkg", "FancyUninstaller.tool"),
must_succeed: true, args: ["--please"],
print_stdout: true, must_succeed: true,
sudo: false, print_stdout: true,
) sudo: false,
)
InstallHelper.install_without_artifacts(cask) InstallHelper.install_without_artifacts(cask)
subject.public_send(:"#{artifact_dsl_key}_phase", command: fake_system_command) subject.public_send(:"#{artifact_dsl_key}_phase", command: fake_system_command)

View File

@ -67,7 +67,11 @@ shared_examples Cask::Staged do
allow(staged).to receive(:Pathname).and_return(fake_pathname) allow(staged).to receive(:Pathname).and_return(fake_pathname)
expect(fake_system_command).to receive(:run!) expect(fake_system_command).to receive(:run!)
.with("/usr/sbin/chown", args: ["-R", "--", "fake_user:staff", fake_pathname, fake_pathname], sudo: true) .with(
"/usr/sbin/chown",
args: ["-R", "--", "fake_user:staff", fake_pathname, fake_pathname],
sudo: true,
)
staged.set_ownership([fake_pathname.to_s, fake_pathname.to_s]) staged.set_ownership([fake_pathname.to_s, fake_pathname.to_s])
end end
@ -78,7 +82,11 @@ shared_examples Cask::Staged do
allow(staged).to receive(:Pathname).and_return(fake_pathname) allow(staged).to receive(:Pathname).and_return(fake_pathname)
expect(fake_system_command).to receive(:run!) expect(fake_system_command).to receive(:run!)
.with("/usr/sbin/chown", args: ["-R", "--", "other_user:other_group", fake_pathname], sudo: true) .with(
"/usr/sbin/chown",
args: ["-R", "--", "other_user:other_group", fake_pathname],
sudo: true,
)
staged.set_ownership(fake_pathname.to_s, user: "other_user", group: "other_group") staged.set_ownership(fake_pathname.to_s, user: "other_user", group: "other_group")
end end

View File

@ -63,8 +63,9 @@ describe Cask::Pkg, :cask do
expect(fake_system_command).to receive(:run!).with( expect(fake_system_command).to receive(:run!).with(
"/usr/sbin/pkgutil", "/usr/sbin/pkgutil",
args: ["--forget", "my.fake.pkg"], args: ["--forget", "my.fake.pkg"],
sudo: true, sudo: true,
sudo_as_root: true,
) )
pkg.uninstall pkg.uninstall
@ -114,9 +115,10 @@ describe Cask::Pkg, :cask do
allow(fake_system_command).to receive(:run!).and_call_original allow(fake_system_command).to receive(:run!).and_call_original
expect(fake_system_command).to receive(:run!).with( expect(fake_system_command).to receive(:run!).with(
"/usr/bin/xargs", "/usr/bin/xargs",
args: ["-0", "--", a_string_including("rmdir")], args: ["-0", "--", a_string_including("rmdir")],
input: [fake_dir].join("\0"), input: [fake_dir].join("\0"),
sudo: true, sudo: true,
sudo_as_root: true,
).and_return(instance_double(SystemCommand::Result, stdout: "")) ).and_return(instance_double(SystemCommand::Result, stdout: ""))
pkg.uninstall pkg.uninstall

View File

@ -15,7 +15,8 @@ cask "with-uninstall-script-app" do
end end
uninstall script: { uninstall script: {
executable: "#{appdir}/MyFancyApp.app/uninstall.sh", executable: "#{appdir}/MyFancyApp.app/uninstall.sh",
sudo: false, sudo: false,
sudo_as_root: false,
} }
end end

View File

@ -15,7 +15,8 @@ cask "with-uninstall-script-user-relative" do
end end
uninstall script: { uninstall script: {
executable: "~/MyFancyApp.app/uninstall.sh", executable: "~/MyFancyApp.app/uninstall.sh",
sudo: false, sudo: false,
sudo_as_root: false,
} }
end end

View File

@ -5,6 +5,6 @@ require "system_command"
class NeverSudoSystemCommand < SystemCommand class NeverSudoSystemCommand < SystemCommand
def self.run(command, **options) def self.run(command, **options)
super(command, **options.merge(sudo: false)) super(command, **options.merge(sudo: false, sudo_as_root: false))
end end
end end

View File

@ -9,12 +9,14 @@ describe SystemCommand do
env: env, env: env,
must_succeed: true, must_succeed: true,
sudo: sudo, sudo: sudo,
sudo_as_root: sudo_as_root,
) )
end end
let(:env_args) { ["bash", "-c", 'printf "%s" "${A?}" "${B?}" "${C?}"'] } let(:env_args) { ["bash", "-c", 'printf "%s" "${A?}" "${B?}" "${C?}"'] }
let(:env) { { "A" => "1", "B" => "2", "C" => "3" } } let(:env) { { "A" => "1", "B" => "2", "C" => "3" } }
let(:sudo) { false } let(:sudo) { false }
let(:sudo_as_root) { false }
context "when given some environment variables" do context "when given some environment variables" do
its("run!.stdout") { is_expected.to eq("123") } its("run!.stdout") { is_expected.to eq("123") }
@ -45,8 +47,9 @@ describe SystemCommand do
end end
end end
context "when given some environment variables and sudo: true" do context "when given some environment variables and sudo: true, sudo_as_root: false" do
let(:sudo) { true } let(:sudo) { true }
let(:sudo_as_root) { false }
describe "the resulting command line" do describe "the resulting command line" do
it "includes the given variables explicitly" do it "includes the given variables explicitly" do
@ -64,6 +67,27 @@ describe SystemCommand do
end end
end end
end end
context "when given some environment variables and sudo: true, sudo_as_root: true" do
let(:sudo) { true }
let(:sudo_as_root) { true }
describe "the resulting command line" do
it "includes the given variables explicitly" do
expect(Open3)
.to receive(:popen3)
.with(
an_instance_of(Hash), ["/usr/bin/sudo", "/usr/bin/sudo"], "-u", "root",
"-E", "A=1", "B=2", "C=3", "--", "env", *env_args, pgroup: nil
)
.and_wrap_original do |original_popen3, *_, &block|
original_popen3.call("true", &block)
end
command.run!
end
end
end
end end
context "when the exit code is 0" do context "when the exit code is 0" do