Skip to content

Commit adf30f9

Browse files
committed
ActionMailerCheck: Support :sendmail and :test
This adds support for checking that the `sendmail` binary chosen is executable when ActionMailer is using the `:sendmail` delivery_method, and stubs out the check when using the `:test` delivery_method. Additionally, this change ensures that other delivery_methods fail explicitly instead of falling through and being silently ignored.
1 parent 870dad5 commit adf30f9

3 files changed

Lines changed: 61 additions & 5 deletions

File tree

‎CHANGELOG.markdown‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#### Unreleased
2+
* ActionMailerCheck: Support :sendmail and :test
3+
> awilfox: https://github.com/okcomputer-ruby/okcomputer/pull/21
24
* Add SolidQueue checks: `SolidQueueCheck` (liveness + job stats),
35
`SolidQueueBackedUpCheck` (per-queue backlog), `SolidQueueFailedJobsCheck`
46
(total failed jobs), `SolidQueueFailedJobsRateCheck` (rapid increase in

‎lib/ok_computer/built_in_checks/action_mailer_check.rb‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,34 @@ def initialize(klass = ActionMailer::Base, timeout = 5)
1414

1515
# Public: Return the status of the check
1616
def check
17-
tcp_socket_request
18-
mark_message "#{klass} check to #{host}:#{port} successful"
19-
rescue => e
20-
mark_message "#{klass} at #{host}:#{port} is not accepting connections: '#{e}'"
21-
mark_failure
17+
case klass.delivery_method
18+
when :smtp
19+
begin
20+
tcp_socket_request
21+
mark_message "#{klass} check to #{host}:#{port} successful"
22+
rescue => e
23+
mark_message "#{klass} at #{host}:#{port} is not accepting connections: '#{e}'"
24+
mark_failure
25+
end
26+
when :sendmail
27+
begin
28+
location = klass.sendmail_settings[:location]
29+
if File.executable?(location)
30+
mark_message "#{klass} sendmail executable #{location} can be executed"
31+
else
32+
mark_message "#{klass} sendmail executable #{location} is not executable"
33+
mark_failure
34+
end
35+
rescue => e
36+
mark_message "#{klass} error checking sendmail executable: '#{e}'"
37+
mark_failure
38+
end
39+
when :test
40+
mark_message "#{klass} is in test mode"
41+
else
42+
mark_message "unknown delivery method #{klass.delivery_method}"
43+
mark_failure
44+
end
2245
end
2346
end
2447
end

‎spec/ok_computer/built_in_checks/action_mailer_check_spec.rb‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class ActionMailerSubclass < ActionMailer::Base
5454
describe '#check' do
5555
context "when mailer is accepting connections" do
5656
before do
57+
ActionMailer::Base.delivery_method = :smtp
5758
ActionMailer::Base.smtp_settings[:address] = 'localhost'
5859
ActionMailer::Base.smtp_settings[:port] = 25
5960
expect(TCPSocket).to receive(:new).with('localhost', 25).and_return(double(:socket, :close => true))
@@ -64,6 +65,7 @@ class ActionMailerSubclass < ActionMailer::Base
6465
end
6566

6667
context "when mailer does not accept connection" do
68+
before { ActionMailer::Base.delivery_method = :smtp }
6769
let(:amcheck) { described_class.new(ActionMailerSubclass) }
6870
it 'is not successful' do
6971
expect(amcheck).to receive(:tcp_socket_request).and_raise(Errno::ECONNREFUSED)
@@ -74,6 +76,35 @@ class ActionMailerSubclass < ActionMailer::Base
7476
expect(amcheck).to have_message "OkComputer::ActionMailerSubclass at mail.example.com:666 is not accepting connections: 'Connection refused'"
7577
end
7678
end
79+
80+
context "when mailer is in sendmail mode" do
81+
before do
82+
ActionMailer::Base.delivery_method = :sendmail
83+
ActionMailer::Base.sendmail_settings[:location] = '/usr/sbin/sendmail'
84+
expect(File).to receive(:executable?).with('/usr/sbin/sendmail').and_return(true)
85+
end
86+
87+
it { is_expected.to be_successful_check }
88+
it { is_expected.to have_message "ActionMailer::Base sendmail executable /usr/sbin/sendmail can be executed" }
89+
end
90+
91+
context "when sendmail is not installed" do
92+
before do
93+
ActionMailer::Base.delivery_method = :sendmail
94+
ActionMailer::Base.sendmail_settings[:location] = '/usr/sbin/sendmail'
95+
expect(File).to receive(:executable?).with('/usr/sbin/sendmail').and_return(false)
96+
end
97+
98+
it { is_expected.not_to be_successful_check }
99+
it { is_expected.to have_message "ActionMailer::Base sendmail executable /usr/sbin/sendmail is not executable" }
100+
end
101+
102+
context "when mailer is in test mode" do
103+
before { ActionMailer::Base.delivery_method = :test }
104+
105+
it { is_expected.to be_successful_check }
106+
it { is_expected.to have_message "ActionMailer::Base is in test mode" }
107+
end
77108
end
78109
end
79110
end

0 commit comments

Comments
 (0)