Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
health: catch all exceptions for registries
Browse files Browse the repository at this point in the history
The fact that `RegistryClient.reachable?` might raise an exception is
fine in some situations on the API, but it prevents us from knowing from
other errors, and it also makes things harder in other places of the
code that rely on this check. Hence, let's rescue this exception and
return its value properly.

Signed-off-by: Miquel Sabaté Solà <[email protected]>
  • Loading branch information
mssola committed May 23, 2018
1 parent fe1e9b9 commit 4c25b23
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/portus/health_checks/registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def self.ready

res = ::Registry.get.client.reachable?
["registry is#{res ? "" : " not"} reachable", res]

# TODO: rescue ::Portus::RequestError instead of simply bailing
rescue ::Portus::RequestError => e
[e.to_s, false]
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/portus/request_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class RequestError < StandardError
# Given an inner exception and a message, it builds up a common error
# message.
def initialize(exception:, message:)
@msg = "#{exception.class.name}: #{message}."
@msg = "#{exception.class.name}: #{message}"
Rails.logger.error @msg
end

Expand Down
12 changes: 12 additions & 0 deletions spec/api/grape_api/v1/health_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ def db_helper_msg
expect(response.status).to eq 503
end

it "reached a RequestError when trying to connect to the registry" do
create(:registry, hostname: "whatever", use_ssl: false)

allow_any_instance_of(::Portus::RegistryClient).to receive(:reachable?)
.and_raise(::Portus::RequestError, exception: Net::ReadTimeout, message: "something")

get "/api/v1/health"
expect(response.status).to eq 503
data = JSON.parse(response.body)
expect(data["registry"]["msg"]).to eq "Class: something"
end

it "has both the DB and the registry" do
create(:registry, hostname: "registry.mssola.cat", use_ssl: true)

Expand Down
23 changes: 23 additions & 0 deletions spec/integration/health.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bats -t

load helpers

function setup() {
__setup minimal
}

@test "health runs just fine" {
helper_runner curl.rb /api/v1/health
[[ "${lines[-2]}" =~ "database is up-to-date" ]]
[[ "${lines[-2]}" =~ "clair is reachable" ]]
[[ "${lines[-2]}" =~ "registry is reachable" ]]
}

@test "health reports an invalid registry" {
# Modify the registry hostname to some unknown hostname.
ruby_puts "Registry.get.update(hostname:\"wrong.whatever\")"

helper_runner curl.rb /api/v1/health
[ $status -eq 1 ]
[[ "${lines[-2]}" =~ "SocketError: connection refused" ]]
}
2 changes: 1 addition & 1 deletion spec/lib/portus/request_error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

expect do
raise ::Portus::RequestError.new(exception: e, message: "something")
end.to raise_error(::Portus::RequestError, "StandardError: something.")
end.to raise_error(::Portus::RequestError, "StandardError: something")
end
end

0 comments on commit 4c25b23

Please sign in to comment.