Skip to content

Commit 77f8a8b

Browse files
committed
test: add tests for different auth methods (scram-sha-256, password, md5)
1 parent e1a6779 commit 77f8a8b

File tree

7 files changed

+137
-68
lines changed

7 files changed

+137
-68
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,5 @@ priv/native/*
4141
/.pre-commit-config.yaml
4242
*.coverdata
4343
/tmp
44+
45+
.devenv/

config/test.exs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,29 @@ config :supavisor, Supavisor.Repo,
4141
pool_size: 10,
4242
port: 6432
4343

44+
config :supavisor, :tenants,
45+
"scram-sha-256": [
46+
hostname: "localhost",
47+
port: 6432,
48+
database: "supavisor_test",
49+
username: "postgres",
50+
password: "postgres"
51+
],
52+
md5: [
53+
hostname: "localhost",
54+
port: 6433,
55+
database: "supavisor_test",
56+
username: "postgres",
57+
password: "postgres"
58+
],
59+
password: [
60+
hostname: "localhost",
61+
port: 6434,
62+
database: "supavisor_test",
63+
username: "postgres",
64+
password: "postgres"
65+
]
66+
4467
# We don't run a server during test. If one is required,
4568
# you can enable the server option below.
4669
config :supavisor, SupavisorWeb.Endpoint,

docker-compose.db.yml

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,35 @@ services:
88
- "6432:5432"
99
volumes:
1010
- ./dev/postgres:/docker-entrypoint-initdb.d/
11-
# Uncomment to set MD5 authentication method on uninitialized databases
12-
# - ./dev/postgres/md5/etc/postgresql/pg_hba.conf:/etc/postgresql/pg_hba.conf
13-
# Uncomment to set password authentication method on uninitialized databases
14-
# - ./dev/postgres/password/etc/postgresql/pg_hba.conf:/etc/postgresql/pg_hba.conf
1511
command: postgres -c config_file=/etc/postgresql/postgresql.conf -c max_prepared_transactions=2000
1612
environment:
1713
POSTGRES_HOST: /var/run/postgresql
1814
POSTGRES_PASSWORD: postgres
19-
# Uncomment to set MD5 authentication method on uninitialized databases
20-
# POSTGRES_INITDB_ARGS: --auth-host=md5
21-
# Uncomment to set password authentication method on uninitialized databases
22-
# POSTGRES_INITDB_ARGS: --auth-host=password
15+
16+
db_md5:
17+
image: supabase/postgres:14.1.0.106
18+
container_name: supavisor-db-md5
19+
ports:
20+
- "6433:5432"
21+
volumes:
22+
- ./dev/postgres:/docker-entrypoint-initdb.d/
23+
- ./dev/postgres/md5/etc/postgresql/pg_hba.conf:/etc/postgresql/pg_hba.conf
24+
command: postgres -c config_file=/etc/postgresql/postgresql.conf -c max_prepared_transactions=2000
25+
environment:
26+
POSTGRES_HOST: /var/run/postgresql
27+
POSTGRES_PASSWORD: postgres
28+
POSTGRES_INITDB_ARGS: --auth-host=md5
29+
30+
db_password:
31+
image: supabase/postgres:14.1.0.106
32+
container_name: supavisor-db-password
33+
ports:
34+
- "6434:5432"
35+
volumes:
36+
- ./dev/postgres:/docker-entrypoint-initdb.d/
37+
- ./dev/postgres/password/etc/postgresql/pg_hba.conf:/etc/postgresql/pg_hba.conf
38+
command: postgres -c config_file=/etc/postgresql/postgresql.conf -c max_prepared_transactions=2000
39+
environment:
40+
POSTGRES_HOST: /var/run/postgresql
41+
POSTGRES_PASSWORD: postgres
42+
POSTGRES_INITDB_ARGS: --auth-host=password

priv/repo/seeds_after_migration.exs

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,46 @@ alias Supavisor.Tenants
22
alias Supavisor.Repo
33
import Ecto.Adapters.SQL, only: [query: 3]
44

5-
db_conf = Application.get_env(:supavisor, Repo)
5+
tenants_conf = Application.get_env(:supavisor, :tenants)
66

77
tenant_name = "dev_tenant"
88

99
if Tenants.get_tenant_by_external_id(tenant_name) do
1010
Tenants.delete_tenant_by_external_id(tenant_name)
1111
end
1212

13-
if !Tenants.get_tenant_by_external_id("is_manager") do
14-
{:ok, _} =
15-
%{
16-
db_host: db_conf[:hostname],
17-
db_port: db_conf[:port],
18-
db_database: db_conf[:database],
19-
default_parameter_status: %{},
20-
external_id: "is_manager",
21-
require_user: false,
22-
auth_query: "SELECT rolname, rolpassword FROM pg_authid WHERE rolname=$1;",
23-
users: [
24-
%{
25-
"db_user" => db_conf[:username],
26-
"db_password" => db_conf[:password],
27-
"pool_size" => 2,
28-
"mode_type" => "transaction",
29-
"is_manager" => true,
30-
"pool_checkout_timeout" => 1000
31-
}
32-
]
33-
}
34-
|> Tenants.create_tenant()
35-
end
13+
tenants_conf
14+
|> Enum.each(fn {key, db_conf} ->
15+
if !Tenants.get_tenant_by_external_id("is_manager_#{key}") do
16+
{:ok, _} =
17+
%{
18+
db_host: db_conf[:hostname],
19+
db_port: db_conf[:port],
20+
db_database: db_conf[:database],
21+
default_parameter_status: %{},
22+
external_id: "is_manager_#{key}",
23+
require_user: false,
24+
auth_query: "SELECT rolname, rolpassword FROM pg_authid WHERE rolname=$1;",
25+
users: [
26+
%{
27+
"db_user" => db_conf[:username],
28+
"db_password" => db_conf[:password],
29+
"pool_size" => 2,
30+
"mode_type" => "transaction",
31+
"is_manager" => true,
32+
"pool_checkout_timeout" => 1000
33+
}
34+
]
35+
}
36+
|> Tenants.create_tenant()
37+
end
38+
end)
3639

3740
["proxy_tenant1", "syn_tenant", "prom_tenant", "max_pool_tenant", "metrics_tenant"]
3841
|> Enum.each(fn tenant ->
3942
if !Tenants.get_tenant_by_external_id(tenant) do
43+
db_conf = tenants_conf[:"scram-sha-256"]
44+
4045
{:ok, _} =
4146
%{
4247
db_host: db_conf[:hostname],
Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
import Ecto.Adapters.SQL, only: [query: 3]
22

3-
[
4-
"create schema if not exists _supavisor"
5-
]
6-
|> Enum.each(&query(Supavisor.Repo, &1, []))
3+
tenants_conf = Application.get_env(:supavisor, :tenants)
4+
5+
defmodule MD5Repo do
6+
use Ecto.Repo,
7+
otp_app: :supavisor,
8+
adapter: Ecto.Adapters.Postgres
9+
end
10+
11+
MD5Repo.start_link(tenants_conf[:md5])
12+
13+
defmodule PasswordRepo do
14+
use Ecto.Repo,
15+
otp_app: :supavisor,
16+
adapter: Ecto.Adapters.Postgres
17+
end
18+
19+
PasswordRepo.start_link(tenants_conf[:password])
20+
21+
[MD5Repo, PasswordRepo, Supavisor.Repo]
22+
|> Enum.each(fn repo -> query(repo, "create schema if not exists _supavisor", []) end)

test/supavisor/monitoring/tenant_test.exs

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,37 +35,40 @@ defmodule Supavisor.PromEx.Plugins.TenantTest do
3535
end
3636
end
3737

38-
describe "execute_client_connections_lifetime" do
39-
setup ctx do
40-
create_instance([__MODULE__, ctx.line])
41-
end
38+
# for {key, port} <- [{"scram-sha-256", 6432}, {"md5", 6433},{"password", 6434}] do
39+
for {key, port} <- [{"scram-sha-256", 6432}, {"password", 6434}] do
40+
describe "execute_client_connections_lifetime when authentication method is #{key}" do
41+
setup ctx do
42+
create_instance([__MODULE__, ctx.line], unquote(port))
43+
end
4244

43-
test "emits event for active client connections", ctx do
44-
start_supervised!(
45-
{SingleConnection,
46-
hostname: "localhost",
47-
port: Application.fetch_env!(:supavisor, :proxy_port_transaction),
48-
database: ctx.db,
49-
username: ctx.user,
50-
password: "postgres"}
51-
)
52-
53-
ref = attach_handler([:supavisor, :client, :connection, :lifetime])
54-
assert :ok = Tenant.execute_client_connections_lifetime()
55-
56-
assert_receive {^ref, {[:supavisor, :client, :connection, :lifetime], measurement, meta}}
57-
58-
assert %{lifetime: lifetime} = measurement
59-
assert lifetime >= 0
60-
61-
assert meta == %{
62-
tenant: ctx.db,
63-
user: String.split(ctx.user, ".") |> List.first(),
64-
mode: :transaction,
65-
type: :single,
66-
db_name: ctx.db,
67-
search_path: nil
68-
}
45+
test "emits event for active client connections", ctx do
46+
start_supervised!(
47+
{SingleConnection,
48+
hostname: "localhost",
49+
port: Application.fetch_env!(:supavisor, :proxy_port_transaction),
50+
database: ctx.db,
51+
username: ctx.user,
52+
password: "postgres"}
53+
)
54+
55+
ref = attach_handler([:supavisor, :client, :connection, :lifetime])
56+
assert :ok = Tenant.execute_client_connections_lifetime()
57+
58+
assert_receive {^ref, {[:supavisor, :client, :connection, :lifetime], measurement, meta}}
59+
60+
assert %{lifetime: lifetime} = measurement
61+
assert lifetime >= 0
62+
63+
assert meta == %{
64+
tenant: ctx.db,
65+
user: String.split(ctx.user, ".") |> List.first(),
66+
mode: :transaction,
67+
type: :single,
68+
db_name: ctx.db,
69+
search_path: nil
70+
}
71+
end
6972
end
7073
end
7174

test/support/e2e_case.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ defmodule Supavisor.E2ECase do
2727
Ecto.Adapters.SQL.Sandbox.unboxed_run(@repo, fun)
2828
end
2929

30-
def create_instance(external_id) do
30+
def create_instance(external_id, port \\ 6432) do
3131
external_id =
3232
external_id
3333
|> List.wrap()
@@ -43,7 +43,7 @@ defmodule Supavisor.E2ECase do
4343
Supavisor.Tenants.create_tenant(%{
4444
default_parameter_status: %{},
4545
db_host: "localhost",
46-
db_port: 6432,
46+
db_port: port,
4747
db_database: external_id,
4848
auth_query: "SELECT rolname, rolpassword FROM pg_authid WHERE rolname=$1;",
4949
external_id: external_id,

0 commit comments

Comments
 (0)