Skip to content

Commit e40c2de

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

File tree

6 files changed

+130
-66
lines changed

6 files changed

+130
-66
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, :test_auth_profiles,
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+
auth_profiles_conf = Application.get_env(:supavisor, :test_auth_profiles)
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+
auth_profiles_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 = auth_profiles_conf[:"scram-sha-256"]
44+
4045
{:ok, _} =
4146
%{
4247
db_host: db_conf[:hostname],

test/supavisor/monitoring/tenant_test.exs

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,37 +35,39 @@ 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, auth_config} <- list_authentication_configs() do
39+
describe "execute_client_connections_lifetime when authentication method is #{key}" do
40+
setup ctx do
41+
create_instance([__MODULE__, ctx.line], unquote(auth_config))
42+
end
4243

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

test/support/e2e_case.ex

Lines changed: 16 additions & 4 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, config \\ %{port: 6432, hostname: "localhost"}) do
3131
external_id =
3232
external_id
3333
|> List.wrap()
@@ -42,8 +42,8 @@ defmodule Supavisor.E2ECase do
4242
assert {:ok, tenant} =
4343
Supavisor.Tenants.create_tenant(%{
4444
default_parameter_status: %{},
45-
db_host: "localhost",
46-
db_port: 6432,
45+
db_host: config[:hostname],
46+
db_port: config[:port],
4747
db_database: external_id,
4848
auth_query: "SELECT rolname, rolpassword FROM pg_authid WHERE rolname=$1;",
4949
external_id: external_id,
@@ -67,6 +67,18 @@ defmodule Supavisor.E2ECase do
6767
end)
6868
end)
6969

70-
{:ok, %{user: "postgres.#{external_id}", db: tenant.db_database, external_id: external_id}}
70+
{:ok,
71+
%{
72+
user: "postgres.#{external_id}",
73+
db: tenant.db_database,
74+
external_id: external_id,
75+
auth_config: config
76+
}}
77+
end
78+
79+
def list_authentication_configs do
80+
:supavisor
81+
|> Application.get_env(:test_auth_profiles)
82+
|> Macro.escape()
7183
end
7284
end

0 commit comments

Comments
 (0)