Skip to content

Commit

Permalink
chore: upgrade gh runner (#226)
Browse files Browse the repository at this point in the history
* chore: upgrade gh runner
* remove psql from tests
  • Loading branch information
abc3 authored Dec 19, 2023
1 parent 5c0c7cc commit 1fd9222
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 46 deletions.
25 changes: 16 additions & 9 deletions .github/workflows/prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,28 @@ on:
branches:
- release
env:
INCLUDE_ERTS: false
INCLUDE_ERTS: true
MIX_ENV: prod
jobs:
publish:
runs-on: ubuntu-latest
runs-on: u22-arm-runner
timeout-minutes: 15
permissions:
contents: write
packages: write
id-token: write
steps:
- uses: actions/checkout@v4
- name: Setup elixir
id: beam
uses: erlef/setup-beam@v1
- name: Setup Elixir
run: |
. ~/.asdf/asdf.sh
echo 'elixir v1.14.5-otp-25' >> ~/.tool-versions
echo 'erlang 25.3.2.7' >> ~/.tool-versions
elixir -v
- name: Set up Rust
uses: ATiltedTree/setup-rust@v1
with:
otp-version: 25.3.2 # Define the OTP version [required]
elixir-version: 1.14.5 # Define the elixir version [required]
version-type: strict
rust-version: stable
- name: Cache Mix
uses: actions/cache@v3
with:
Expand All @@ -31,7 +35,10 @@ jobs:
restore-keys: |
${{ runner.os }}-mix-
- name: Install dependencies
run: mix deps.get
run: |
mix local.hex --force
mix local.rebar --force
mix deps.get
- name: Make release
run: mix release supavisor
- name: Create tarball
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish_docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Release on Dockerhub
on:
push:
branches:
- main_v0.9
- main
paths:
- ".github/workflows/publish_docker.yml"
- "VERSION"
Expand Down
27 changes: 17 additions & 10 deletions .github/workflows/stage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,30 @@ name: Publish upgrade artifacts to staging
on:
push:
branches:
- main_v0.9
- main
env:
INCLUDE_ERTS: false
INCLUDE_ERTS: true
MIX_ENV: prod
jobs:
publish:
runs-on: ubuntu-latest
runs-on: u22-arm-runner
timeout-minutes: 15
permissions:
contents: write
packages: write
id-token: write
steps:
- uses: actions/checkout@v4
- name: Setup elixir
id: beam
uses: erlef/setup-beam@v1
- name: Setup Elixir
run: |
. ~/.asdf/asdf.sh
echo 'elixir v1.14.5-otp-25' >> ~/.tool-versions
echo 'erlang 25.3.2.7' >> ~/.tool-versions
elixir -v
- name: Set up Rust
uses: ATiltedTree/setup-rust@v1
with:
otp-version: 25.3.2 # Define the OTP version [required]
elixir-version: 1.14.5 # Define the elixir version [required]
version-type: strict
rust-version: stable
- name: Cache Mix
uses: actions/cache@v3
with:
Expand All @@ -31,7 +35,10 @@ jobs:
restore-keys: |
${{ runner.os }}-mix-
- name: Install dependencies
run: mix deps.get
run: |
mix local.hex --force
mix local.rebar --force
mix deps.get
- name: Make release
run: mix release supavisor
- name: Create tarball
Expand Down
22 changes: 14 additions & 8 deletions .github/workflows/staging_linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,32 @@ env:
jobs:
format:
name: Formatting Checks
runs-on: ubuntu-latest
runs-on: u22-arm-runner

steps:
- uses: actions/checkout@v4
- name: Setup elixir
id: beam
uses: erlef/setup-beam@v1
- name: Setup Elixir
run: |
. ~/.asdf/asdf.sh
echo 'elixir v1.14.5-otp-25' >> ~/.tool-versions
echo 'erlang 25.3.2.7' >> ~/.tool-versions
elixir -v
- name: Set up Rust
uses: ATiltedTree/setup-rust@v1
with:
otp-version: 25.x # Define the OTP version [required]
elixir-version: 1.14.x # Define the elixir version [required]
rust-version: stable
- name: Cache Mix
uses: actions/cache@v3
with:
path: deps
key: ${{ runner.os }}-mix-${{ hashFiles(format('{0}{1}', github.workspace, '/mix.lock')) }}
restore-keys: |
${{ runner.os }}-mix-
- name: Install dependencies
run: mix deps.get
run: |
mix local.hex --force
mix local.rebar --force
mix deps.get
- name: Set up Postgres
run: docker-compose -f ./docker-compose.db.yml up -d
- name: Run main database migrations
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.5
1.0.6
81 changes: 64 additions & 17 deletions test/integration/proxy_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Supavisor.Integration.ProxyTest do
require Logger
use Supavisor.DataCase, async: true
alias Postgrex, as: P

Expand Down Expand Up @@ -29,33 +30,39 @@ defmodule Supavisor.Integration.ProxyTest do
end

test "prepared statement", %{proxy: proxy} do
prepare_sql =
"PREPARE tenant (text) AS SELECT id, external_id FROM _supavisor.tenants WHERE external_id = $1;"

db_conf = Application.get_env(:supavisor, Repo)

url = """
postgresql://#{db_conf[:username] <> "." <> @tenant}:#{db_conf[:password]}\
@#{db_conf[:hostname]}:#{Application.get_env(:supavisor, :proxy_port_transaction)}/postgres
"""
{:ok, pid} =
Keyword.merge(db_conf, username: db_conf[:username] <> "." <> @tenant)
|> single_connection(Application.get_env(:supavisor, :proxy_port_transaction))

prepare_sql =
"PREPARE tenant (text) AS SELECT id, external_id FROM _supavisor.tenants WHERE external_id = $1;"
assert [%Postgrex.Result{command: :prepare}] =
P.SimpleConnection.call(pid, {:query, prepare_sql})

{result, _} = System.cmd("psql", [url, "-c", prepare_sql], stderr_to_stdout: true)
assert result =~ "PREPARE"
:timer.sleep(500)

{result, _} =
System.cmd("psql", [url, "-c", "EXECUTE tenant('#{@tenant}');"], stderr_to_stdout: true)
assert {_, %Postgrex.Result{command: :select}} =
Postgrex.query(proxy, "EXECUTE tenant('#{@tenant}');", [])

assert result =~ "#{@tenant}\n(1 row)"
:gen_statem.stop(pid)
end

test "the wrong password" do
Process.flag(:trap_exit, true)
db_conf = Application.get_env(:supavisor, Repo)

url =
"postgresql://#{db_conf[:username] <> "." <> @tenant}:no_pass@#{db_conf[:hostname]}:#{Application.get_env(:supavisor, :proxy_port_transaction)}/postgres"

{result, _} = System.cmd("psql", [url], stderr_to_stdout: true)
assert result =~ "error received from server in SCRAM exchange: Wrong password"
assert {:error,
{_,
{:stop,
%Postgrex.Error{
message: "error received in SCRAM server final message: \"Wrong password\""
}, _}}} = parse_uri(url) |> single_connection()
end

test "insert", %{proxy: proxy, origin: origin} do
Expand Down Expand Up @@ -172,7 +179,7 @@ defmodule Supavisor.Integration.ProxyTest do
url =
"postgresql://transaction.proxy_tenant:#{db_conf[:password]}@#{db_conf[:hostname]}:#{Application.get_env(:supavisor, :proxy_port_transaction)}/postgres"

psql_pid = spawn(fn -> System.cmd("psql", [url]) end)
{:ok, pid} = parse_uri(url) |> single_connection()

:timer.sleep(500)

Expand All @@ -185,16 +192,56 @@ defmodule Supavisor.Integration.ProxyTest do
{state, %{db_pid: db_pid}} = :sys.get_state(client_pid)

assert {:idle, nil} = {state, db_pid}
Process.exit(psql_pid, :kill)
:gen_statem.stop(pid)
end

test "limit client connections" do
Process.flag(:trap_exit, true)
db_conf = Application.get_env(:supavisor, Repo)

url =
"postgresql://max_clients.proxy_tenant:#{db_conf[:password]}@#{db_conf[:hostname]}:#{Application.get_env(:supavisor, :proxy_port_transaction)}/postgres?sslmode=disable"

{result, _} = System.cmd("psql", [url], stderr_to_stdout: true)
assert result =~ "FATAL: Max client connections reached"
assert =
{:error,
{_,
{:stop,
%Postgrex.Error{
postgres: %{
code: :internal_error,
message: "Max client connections reached",
pg_code: "XX000",
severity: "FATAL",
unknown: "FATAL"
}
}, _}}} = parse_uri(url) |> single_connection()
end

defp single_connection(db_conf, c_port \\ nil) when is_list(db_conf) do
port = c_port || db_conf[:port]

[
hostname: db_conf[:hostname],
port: port,
database: db_conf[:database],
password: db_conf[:password],
username: db_conf[:username],
pool_size: 1
]
|> SingleConnection.connect()
end

defp parse_uri(uri) do
%URI{
userinfo: userinfo,
host: host,
port: port,
path: path
} = URI.parse(uri)

[username, pass] = String.split(userinfo, ":")
database = String.replace(path, "/", "")

[hostname: host, port: port, database: database, password: pass, username: username]
end
end
34 changes: 34 additions & 0 deletions test/support/fixtures/single_connection.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
defmodule SingleConnection do
alias Postgrex, as: P
@behaviour P.SimpleConnection

def connect(conf) do
P.SimpleConnection.start_link(__MODULE__, [], conf)
end

@impl true
def init(_args) do
{:ok, %{from: nil}}
end

@impl true
def handle_call({:query, query}, from, state) do
{:query, query, %{state | from: from}}
end

def handle_result(results, state) when is_list(results) do
IO.inspect({213, results})
P.SimpleConnection.reply(state.from, results)
{:noreply, state}
end

@impl true
def handle_result(%Postgrex.Error{} = error, state) do
IO.inspect({213, error})
P.SimpleConnection.reply(state.from, error)
{:noreply, state}
end

@impl true
def notify(_, _, _), do: :ok
end

0 comments on commit 1fd9222

Please sign in to comment.