Merge branch 'issue/1110' into 'develop'

#1110 fixed /api/pleroma/healthcheck

Closes #1110

See merge request pleroma/pleroma!1453
This commit is contained in:
kaniini 2019-07-19 16:20:23 +00:00
commit 6561709742
2 changed files with 79 additions and 17 deletions

View File

@ -8,7 +8,9 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do
require Logger require Logger
alias Pleroma.Activity alias Pleroma.Activity
alias Pleroma.Config
alias Pleroma.Emoji alias Pleroma.Emoji
alias Pleroma.Healthcheck
alias Pleroma.Notification alias Pleroma.Notification
alias Pleroma.Plugs.AuthenticationPlug alias Pleroma.Plugs.AuthenticationPlug
alias Pleroma.User alias Pleroma.User
@ -23,7 +25,8 @@ def help_test(conn, _params) do
end end
def remote_subscribe(conn, %{"nickname" => nick, "profile" => _}) do def remote_subscribe(conn, %{"nickname" => nick, "profile" => _}) do
with %User{} = user <- User.get_cached_by_nickname(nick), avatar = User.avatar_url(user) do with %User{} = user <- User.get_cached_by_nickname(nick),
avatar = User.avatar_url(user) do
conn conn
|> render("subscribe.html", %{nickname: nick, avatar: avatar, error: false}) |> render("subscribe.html", %{nickname: nick, avatar: avatar, error: false})
else else
@ -338,20 +341,21 @@ def captcha(conn, _params) do
end end
def healthcheck(conn, _params) do def healthcheck(conn, _params) do
info = with true <- Config.get([:instance, :healthcheck]),
if Pleroma.Config.get([:instance, :healthcheck]) do %{healthy: true} = info <- Healthcheck.system_info() do
Pleroma.Healthcheck.system_info() json(conn, info)
else else
%{} %{healthy: false} = info ->
end service_unavailable(conn, info)
conn = _ ->
if info[:healthy] do service_unavailable(conn, %{})
conn end
else end
Plug.Conn.put_status(conn, :service_unavailable)
end
json(conn, info) defp service_unavailable(conn, info) do
conn
|> put_status(:service_unavailable)
|> json(info)
end end
end end

View File

@ -10,6 +10,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do
alias Pleroma.User alias Pleroma.User
alias Pleroma.Web.CommonAPI alias Pleroma.Web.CommonAPI
import Pleroma.Factory import Pleroma.Factory
import Mock
setup do setup do
Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end) Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
@ -231,10 +232,67 @@ test "show follow account page if the `acct` is a account link", %{conn: conn} d
end end
end end
test "GET /api/pleroma/healthcheck", %{conn: conn} do describe "GET /api/pleroma/healthcheck" do
conn = get(conn, "/api/pleroma/healthcheck") setup do
config_healthcheck = Pleroma.Config.get([:instance, :healthcheck])
assert conn.status in [200, 503] on_exit(fn ->
Pleroma.Config.put([:instance, :healthcheck], config_healthcheck)
end)
:ok
end
test "returns 503 when healthcheck disabled", %{conn: conn} do
Pleroma.Config.put([:instance, :healthcheck], false)
response =
conn
|> get("/api/pleroma/healthcheck")
|> json_response(503)
assert response == %{}
end
test "returns 200 when healthcheck enabled and all ok", %{conn: conn} do
Pleroma.Config.put([:instance, :healthcheck], true)
with_mock Pleroma.Healthcheck,
system_info: fn -> %Pleroma.Healthcheck{healthy: true} end do
response =
conn
|> get("/api/pleroma/healthcheck")
|> json_response(200)
assert %{
"active" => _,
"healthy" => true,
"idle" => _,
"memory_used" => _,
"pool_size" => _
} = response
end
end
test "returns 503 when healthcheck enabled and health is false", %{conn: conn} do
Pleroma.Config.put([:instance, :healthcheck], true)
with_mock Pleroma.Healthcheck,
system_info: fn -> %Pleroma.Healthcheck{healthy: false} end do
response =
conn
|> get("/api/pleroma/healthcheck")
|> json_response(503)
assert %{
"active" => _,
"healthy" => false,
"idle" => _,
"memory_used" => _,
"pool_size" => _
} = response
end
end
end end
describe "POST /api/pleroma/disable_account" do describe "POST /api/pleroma/disable_account" do