brew/Library/Homebrew/test/service_spec.rb

351 lines
9.9 KiB
Ruby
Raw Normal View History

2020-12-11 23:14:50 +01:00
# typed: false
# frozen_string_literal: true
require "formula"
require "service"
describe Homebrew::Service do
let(:klass) do
Class.new(Formula) do
url "https://brew.sh/test-1.0.tbz"
end
end
let(:name) { "formula_name" }
let(:path) { Formulary.core_path(name) }
let(:spec) { :stable }
let(:f) { klass.new(name, path, spec) }
2021-04-13 16:59:59 +02:00
describe "#std_service_path_env" do
it "returns valid std_service_path_env" do
f.class.service do
run opt_bin/"beanstalkd"
run_type :immediate
environment_variables PATH: std_service_path_env
error_log_path var/"log/beanstalkd.error.log"
log_path var/"log/beanstalkd.log"
working_dir var
keep_alive true
end
path = f.service.std_service_path_env
expect(path).to eq("#{HOMEBREW_PREFIX}/bin:#{HOMEBREW_PREFIX}/sbin:/usr/bin:/bin:/usr/sbin:/sbin")
end
end
2021-11-02 17:17:45 +01:00
describe "#run_type" do
it "throws for cron type" do
f.class.service do
run opt_bin/"beanstalkd"
run_type :cron
end
expect { f.service.manual_command }.to raise_error TypeError, "Service#run_type does not support cron"
end
it "throws for unexpected type" do
f.class.service do
run opt_bin/"beanstalkd"
run_type :cow
end
expect {
f.service.manual_command
}.to raise_error TypeError, "Service#run_type allows: 'immediate'/'interval'/'cron'"
end
end
describe "#manual_command" do
it "returns valid manual_command" do
f.class.service do
run "#{HOMEBREW_PREFIX}/bin/beanstalkd"
run_type :immediate
environment_variables PATH: std_service_path_env, ETC_DIR: etc/"beanstalkd"
error_log_path var/"log/beanstalkd.error.log"
log_path var/"log/beanstalkd.log"
working_dir var
keep_alive true
end
path = f.service.manual_command
expect(path).to eq("ETC_DIR=\"#{HOMEBREW_PREFIX}/etc/beanstalkd\" #{HOMEBREW_PREFIX}/bin/beanstalkd")
end
it "returns valid manual_command without variables" do
f.class.service do
run opt_bin/"beanstalkd"
run_type :immediate
environment_variables PATH: std_service_path_env
error_log_path var/"log/beanstalkd.error.log"
log_path var/"log/beanstalkd.log"
working_dir var
keep_alive true
end
path = f.service.manual_command
expect(path).to eq("#{HOMEBREW_PREFIX}/opt/formula_name/bin/beanstalkd")
end
end
2020-12-11 23:14:50 +01:00
describe "#to_plist" do
it "returns valid plist" do
f.class.service do
2021-04-29 09:43:39 +02:00
run [opt_bin/"beanstalkd", "test"]
2020-12-11 23:14:50 +01:00
run_type :immediate
environment_variables PATH: std_service_path_env, FOO: "BAR", ETC_DIR: etc/"beanstalkd"
2020-12-11 23:14:50 +01:00
error_log_path var/"log/beanstalkd.error.log"
log_path var/"log/beanstalkd.log"
2021-05-22 18:17:36 +02:00
input_path var/"in/beanstalkd"
root_dir var
2020-12-11 23:14:50 +01:00
working_dir var
keep_alive true
2021-11-02 17:17:45 +01:00
process_type :interactive
2021-05-22 18:17:36 +02:00
restart_delay 30
2021-11-02 17:17:45 +01:00
interval 5
2021-05-22 18:17:36 +02:00
macos_legacy_timers true
2020-12-11 23:14:50 +01:00
end
plist = f.service.to_plist
2021-04-29 09:43:39 +02:00
plist_expect = <<~EOS
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
\t<key>EnvironmentVariables</key>
\t<dict>
\t\t<key>ETC_DIR</key>
\t\t<string>#{HOMEBREW_PREFIX}/etc/beanstalkd</string>
\t\t<key>FOO</key>
\t\t<string>BAR</string>
2021-04-29 09:43:39 +02:00
\t\t<key>PATH</key>
\t\t<string>#{HOMEBREW_PREFIX}/bin:#{HOMEBREW_PREFIX}/sbin:/usr/bin:/bin:/usr/sbin:/sbin</string>
\t</dict>
\t<key>KeepAlive</key>
\t<true/>
\t<key>Label</key>
\t<string>homebrew.mxcl.formula_name</string>
2021-05-22 18:17:36 +02:00
\t<key>LegacyTimers</key>
\t<true/>
\t<key>ProcessType</key>
\t<string>Interactive</string>
2021-04-29 09:43:39 +02:00
\t<key>ProgramArguments</key>
\t<array>
\t\t<string>#{HOMEBREW_PREFIX}/opt/formula_name/bin/beanstalkd</string>
\t\t<string>test</string>
\t</array>
2021-05-22 18:17:36 +02:00
\t<key>RootDirectory</key>
\t<string>#{HOMEBREW_PREFIX}/var</string>
2021-04-29 09:43:39 +02:00
\t<key>RunAtLoad</key>
\t<true/>
\t<key>StandardErrorPath</key>
\t<string>#{HOMEBREW_PREFIX}/var/log/beanstalkd.error.log</string>
2021-05-22 18:17:36 +02:00
\t<key>StandardInPath</key>
\t<string>#{HOMEBREW_PREFIX}/var/in/beanstalkd</string>
2021-04-29 09:43:39 +02:00
\t<key>StandardOutPath</key>
\t<string>#{HOMEBREW_PREFIX}/var/log/beanstalkd.log</string>
2021-05-22 18:17:36 +02:00
\t<key>TimeOut</key>
\t<integer>30</integer>
2021-04-29 09:43:39 +02:00
\t<key>WorkingDirectory</key>
\t<string>#{HOMEBREW_PREFIX}/var</string>
</dict>
</plist>
EOS
expect(plist).to eq(plist_expect)
2020-12-11 23:14:50 +01:00
end
it "returns valid partial plist" do
f.class.service do
2021-04-29 09:43:39 +02:00
run opt_bin/"beanstalkd"
2020-12-11 23:14:50 +01:00
run_type :immediate
end
plist = f.service.to_plist
2021-04-29 09:43:39 +02:00
plist_expect = <<~EOS
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
\t<key>Label</key>
\t<string>homebrew.mxcl.formula_name</string>
\t<key>ProgramArguments</key>
\t<array>
\t\t<string>#{HOMEBREW_PREFIX}/opt/formula_name/bin/beanstalkd</string>
\t</array>
\t<key>RunAtLoad</key>
\t<true/>
</dict>
</plist>
EOS
expect(plist).to eq(plist_expect)
2020-12-11 23:14:50 +01:00
end
2021-11-02 17:17:45 +01:00
it "returns valid interval plist" do
f.class.service do
run opt_bin/"beanstalkd"
run_type :interval
interval 5
end
plist = f.service.to_plist
plist_expect = <<~EOS
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
\t<key>Label</key>
\t<string>homebrew.mxcl.formula_name</string>
\t<key>ProgramArguments</key>
\t<array>
\t\t<string>#{HOMEBREW_PREFIX}/opt/formula_name/bin/beanstalkd</string>
\t</array>
\t<key>RunAtLoad</key>
\t<false/>
\t<key>StartInterval</key>
\t<integer>5</integer>
</dict>
</plist>
EOS
expect(plist).to eq(plist_expect)
end
2020-12-11 23:14:50 +01:00
end
2021-04-13 16:59:59 +02:00
describe "#to_systemd_unit" do
it "returns valid unit" do
f.class.service do
2021-04-29 09:43:39 +02:00
run [opt_bin/"beanstalkd", "test"]
2021-04-13 16:59:59 +02:00
run_type :immediate
environment_variables PATH: std_service_path_env, FOO: "BAR"
2021-04-13 16:59:59 +02:00
error_log_path var/"log/beanstalkd.error.log"
log_path var/"log/beanstalkd.log"
2021-05-22 18:17:36 +02:00
input_path var/"in/beanstalkd"
root_dir var
2021-04-13 16:59:59 +02:00
working_dir var
keep_alive true
2021-11-02 17:17:45 +01:00
process_type :interactive
2021-05-22 18:17:36 +02:00
restart_delay 30
macos_legacy_timers true
2021-04-13 16:59:59 +02:00
end
unit = f.service.to_systemd_unit
std_path = "#{HOMEBREW_PREFIX}/bin:#{HOMEBREW_PREFIX}/sbin:/usr/bin:/bin:/usr/sbin:/sbin"
2021-04-29 09:43:39 +02:00
unit_expect = <<~EOS
[Unit]
Description=Homebrew generated unit for formula_name
2021-06-05 17:59:17 +02:00
[Install]
WantedBy=multi-user.target
2021-04-29 09:43:39 +02:00
[Service]
Type=simple
ExecStart=#{HOMEBREW_PREFIX}/opt/#{name}/bin/beanstalkd test
Restart=always
2021-05-22 18:17:36 +02:00
RestartSec=30
2021-04-29 09:43:39 +02:00
WorkingDirectory=#{HOMEBREW_PREFIX}/var
2021-05-22 18:17:36 +02:00
RootDirectory=#{HOMEBREW_PREFIX}/var
StandardInput=file:#{HOMEBREW_PREFIX}/var/in/beanstalkd
2021-04-29 09:43:39 +02:00
StandardOutput=append:#{HOMEBREW_PREFIX}/var/log/beanstalkd.log
StandardError=append:#{HOMEBREW_PREFIX}/var/log/beanstalkd.error.log
Environment=\"PATH=#{std_path}\"
Environment=\"FOO=BAR\"
2021-04-29 09:43:39 +02:00
EOS
expect(unit).to eq(unit_expect.strip)
2021-04-13 16:59:59 +02:00
end
it "returns valid partial unit" do
f.class.service do
run opt_bin/"beanstalkd"
run_type :immediate
end
unit = f.service.to_systemd_unit
2021-04-29 09:43:39 +02:00
unit_expect = <<~EOS
[Unit]
Description=Homebrew generated unit for formula_name
2021-06-05 17:59:17 +02:00
[Install]
WantedBy=multi-user.target
2021-04-29 09:43:39 +02:00
[Service]
Type=simple
ExecStart=#{HOMEBREW_PREFIX}/opt/#{name}/bin/beanstalkd
EOS
expect(unit).to eq(unit_expect)
2021-04-13 16:59:59 +02:00
end
end
2021-11-20 15:14:50 +01:00
describe "#to_systemd_timer" do
it "returns valid timer" do
f.class.service do
run [opt_bin/"beanstalkd", "test"]
run_type :interval
interval 5
end
unit = f.service.to_systemd_timer
unit_expect = <<~EOS
[Unit]
Description=Homebrew generated timer for formula_name
[Install]
WantedBy=timers.target
[Timer]
Unit=homebrew.formula_name
OnUnitActiveSec=5
EOS
expect(unit).to eq(unit_expect.strip)
end
it "returns valid partial timer" do
f.class.service do
run opt_bin/"beanstalkd"
run_type :immediate
end
unit = f.service.to_systemd_timer
unit_expect = <<~EOS
[Unit]
Description=Homebrew generated timer for formula_name
[Install]
WantedBy=timers.target
[Timer]
Unit=homebrew.formula_name
EOS
expect(unit).to eq(unit_expect)
end
end
describe "#timed?" do
it "returns false for immediate" do
f.class.service do
run [opt_bin/"beanstalkd", "test"]
run_type :immediate
end
expect(f.service.timed?).to eq(false)
end
it "returns true for interval" do
f.class.service do
run [opt_bin/"beanstalkd", "test"]
run_type :interval
end
expect(f.service.timed?).to eq(true)
end
end
describe "#command" do
it "returns @run data" do
f.class.service do
2021-04-29 09:43:39 +02:00
run [opt_bin/"beanstalkd", "test"]
run_type :immediate
end
command = f.service.command
2021-04-29 09:43:39 +02:00
expect(command).to eq(["#{HOMEBREW_PREFIX}/opt/#{name}/bin/beanstalkd", "test"])
end
end
2020-12-11 23:14:50 +01:00
end