Skip to content

Commit

Permalink
test: create helper module for defining e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hauleth committed Jan 3, 2025
1 parent 375bd15 commit c5b9f20
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 81 deletions.
43 changes: 4 additions & 39 deletions test/integration/external_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Supavisor.Integration.ExternalTest do
use ExUnit.Case, async: false
use Supavisor.E2ECase, async: false

@moduletag integration: true

Expand All @@ -13,7 +13,9 @@ defmodule Supavisor.Integration.ExternalTest do
{:ok, npm: npm}
end

setup :external_id
setup ctx do
create_instance([ctx.runtime, ctx.library, ctx.mode])
end

setup ctx do
if get_tool(ctx.runtime) do
Expand Down Expand Up @@ -89,41 +91,4 @@ defmodule Supavisor.Integration.ExternalTest do

defp port("session"), do: Application.fetch_env!(:supavisor, :proxy_port_session)
defp port("transaction"), do: Application.fetch_env!(:supavisor, :proxy_port_transaction)

defp external_id(ctx) do
external_id =
[ctx.runtime, ctx.library, ctx.mode]
|> Enum.map_join("_", &String.replace(&1, ~r/\W/, ""))

# Ensure that there are no leftovers
_ = Supavisor.Tenants.delete_tenant_by_external_id(external_id)

_ = Supavisor.Repo.query("DROP DATABASE IF EXISTS #{external_id}")
assert {:ok, _} = Supavisor.Repo.query("CREATE DATABASE #{external_id}")

assert {:ok, tenant} =
Supavisor.Tenants.create_tenant(%{
default_parameter_status: %{},
db_host: "localhost",
db_port: 6432,
db_database: external_id,
auth_query: "SELECT rolname, rolpassword FROM pg_authid WHERE rolname=$1;",
external_id: external_id,
users: [
%{
"pool_size" => 15,
"db_user" => "postgres",
"db_password" => "postgres",
"is_manager" => true,
"mode_type" => "session"
}
]
})

on_exit(fn ->
Supavisor.Tenants.delete_tenant(tenant)
end)

{:ok, user: "postgres.#{external_id}", db: tenant.db_database, external_id: external_id}
end
end
51 changes: 9 additions & 42 deletions test/supavisor/client_handler/stats_test.exs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
defmodule Supavisor.ClientHandler.StatsTest do
use Supavisor.DataCase, async: false
use Supavisor.E2ECase, async: false

@moduletag telemetry: true

setup :external_id

# Listen on Telemetry events
setup ctx do
:telemetry.attach(
{ctx.test, :client},
Expand All @@ -30,6 +29,11 @@ defmodule Supavisor.ClientHandler.StatsTest do
send(pid, {:telemetry, {name, measurement, meta}})
end

setup ctx do
create_instance([__MODULE__, ctx.line])
end

# Connect to the instance
setup ctx do
conn =
start_supervised!(
Expand Down Expand Up @@ -70,7 +74,7 @@ defmodule Supavisor.ClientHandler.StatsTest do
test "do not not increase if other tenant is used", ctx do
external_id = ctx.external_id

{:ok, other} = external_id(ctx, "another")
{:ok, other} = create_instance([__MODULE__, "another"])

# Cleanup initial data related to sign in
assert_receive {:telemetry, {:client, _, %{tenant: ^external_id}}}
Expand Down Expand Up @@ -118,7 +122,7 @@ defmodule Supavisor.ClientHandler.StatsTest do
test "do not not increase if other tenant is used", ctx do
external_id = ctx.external_id

{:ok, other} = external_id(ctx, "another")
{:ok, other} = create_instance([__MODULE__, "another"])

# Cleanup initial data related to sign in
assert_receive {:telemetry, {:db, _, %{tenant: ^external_id}}}
Expand All @@ -139,41 +143,4 @@ defmodule Supavisor.ClientHandler.StatsTest do
refute_receive {:telemetry, {:db, _, %{tenant: ^external_id}}}
end
end

defp external_id(_ctx, prefix \\ "default") do
external_id =
prefix <> "_" <> String.replace(Ecto.UUID.generate(), "-", "_")

unboxed(fn ->
_ = Repo.query("DROP DATABASE IF EXISTS #{external_id}")
assert {:ok, _} = Repo.query("CREATE DATABASE #{external_id}")
end)

assert {:ok, tenant} =
Supavisor.Tenants.create_tenant(%{
default_parameter_status: %{},
db_host: "localhost",
db_port: 6432,
db_database: external_id,
auth_query: "SELECT rolname, rolpassword FROM pg_authid WHERE rolname=$1;",
external_id: external_id,
users: [
%{
"pool_size" => 15,
"db_user" => "postgres",
"db_password" => "postgres",
"is_manager" => true,
"mode_type" => "session"
}
]
})

on_exit(fn ->
unboxed(fn ->
_ = Repo.query("DROP DATABASE IF EXISTS #{external_id}")
end)
end)

{:ok, %{user: "postgres.#{external_id}", db: tenant.db_database, external_id: external_id}}
end
end
67 changes: 67 additions & 0 deletions test/support/e2e_case.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
defmodule Supavisor.E2ECase do
use ExUnit.CaseTemplate

@repo Supavisor.Repo

using do
quote do
alias unquote(@repo)

import unquote(__MODULE__)
end
end

setup tags do
if tags[:async] do
raise "End to end tests must be run in synchronous mode"
end

Supavisor.DataCase.setup_sandbox(tags)
end

def unboxed(fun) do
Ecto.Adapters.SQL.Sandbox.unboxed_run(@repo, fun)
end

def create_instance(external_id) do
external_id =
external_id
|> List.wrap()
|> Enum.map_join("_", &String.replace(to_string(&1), ~r/\W/, "_"))
|> String.downcase()

unboxed(fn ->
assert {:ok, _} = @repo.query("DROP DATABASE IF EXISTS #{external_id}")
assert {:ok, _} = @repo.query("CREATE DATABASE #{external_id}")
end)

assert {:ok, tenant} =
Supavisor.Tenants.create_tenant(%{
default_parameter_status: %{},
db_host: "localhost",
db_port: 6432,
db_database: external_id,
auth_query: "SELECT rolname, rolpassword FROM pg_authid WHERE rolname=$1;",
external_id: external_id,
users: [
%{
"pool_size" => 15,
"db_user" => "postgres",
"db_password" => "postgres",
"is_manager" => true,
"mode_type" => "session"
}
]
})

on_exit(fn ->
:ok = Supavisor.stop({{:single, external_id}, "postgres", :session, external_id, nil})

unboxed(fn ->
assert {:ok, _} = @repo.query("DROP DATABASE #{external_id}")
end)
end)

{:ok, %{user: "postgres.#{external_id}", db: tenant.db_database, external_id: external_id}}
end
end

0 comments on commit c5b9f20

Please sign in to comment.