Skip to content

Commit

Permalink
build: add a ci job to run the tests using postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
etorreborre committed Jun 26, 2024
1 parent 28e050d commit bb62eaa
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
42 changes: 42 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,49 @@ jobs:
uses: ./.github/actions/nix_upload_store
if: ${{ steps.nix-installer.outputs.cache-hit != 'true' }}

test_postgres:
name: Rust - test_postgres${{ matrix.test_projects != 'stable' && format('_{0}', matrix.test_projects) || '' }}
runs-on: ubuntu-22.04
services:
postgres:
image: postgres:latest
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
POSTGRES_DB: test
ports:
- 5433:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

defaults:
run:
shell: nix develop ./tools/nix#rust${{ matrix.test_projects == 'nightly' && '_nightly' || '' }} --keep CI --ignore-environment --command bash {0}
strategy:
fail-fast: false
matrix:
test_projects:
- stable
steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332
with:
ref: ${{ github.event.inputs.commit_sha }}

- name: Install Nix
uses: ./.github/actions/cache_nix
with:
cache-unique-id: ${{ matrix.test_projects }}
id: nix-installer

- uses: ./.github/actions/cache_rust
with:
job_name: "${{ github.job }}-${{ matrix.test_projects }}"

- name: Run postgres test on ${{ matrix.test_projects }}
run: make -f implementations/rust/Makefile test_postgres

- name: Nix Upload Store
uses: ./.github/actions/nix_upload_store
if: ${{ steps.nix-installer.outputs.cache-hit != 'true' }}

check:
name: Rust - check_${{ matrix.check_projects }}
Expand Down
7 changes: 7 additions & 0 deletions implementations/rust/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ nextest:
nextest_%:
cargo --locked nextest --config-file $(ROOT_DIR)/tools/nextest/.config/nextest.toml run -E 'package($*)' --no-fail-fast
cargo --locked test --doc
test_postgres:
export OCKAM_POSTGRES_HOST=localhost
export OCKAM_POSTGRES_PORT=5433
export OCKAM_POSTGRES_DATABASE_NAME=test
export OCKAM_POSTGRES_USER=postgres
export OCKAM_POSTGRES_PASSWORD=password
cargo --locked nextest --config-file $(ROOT_DIR)/tools/nextest/.config/nextest.toml run -E 'test(sql) or test(cli_state)' --no-fail-fast --test-threads 1

lint: lint_cargo_fmt_check lint_cargo_deny lint_cargo_clippy
lint_cargo_fmt_check:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ use crate::database::database_configuration::DatabaseConfiguration;
use crate::database::migrations::application_migration_set::ApplicationMigrationSet;
use crate::database::migrations::node_migration_set::NodeMigrationSet;
use crate::database::migrations::MigrationSet;
use crate::database::DatabaseType;
use crate::database::{
DatabaseType, OCKAM_POSTGRES_DATABASE_NAME, OCKAM_POSTGRES_HOST, OCKAM_POSTGRES_PASSWORD,
OCKAM_POSTGRES_PORT, OCKAM_POSTGRES_USER,
};
use ockam_core::compat::rand::random_string;
use ockam_core::compat::sync::Arc;
use ockam_core::env::get_env;
use ockam_core::{Error, Result};

/// The SqlxDatabase struct is used to create a database:
Expand Down Expand Up @@ -321,8 +325,20 @@ where
let db = SqlxDatabase::create_sqlite(db_file.path()).await?;
rethrow("SQLite on disk", f(db)).await?;

let host: Option<String> = get_env(OCKAM_POSTGRES_HOST)?;
let port: Option<u16> = get_env(OCKAM_POSTGRES_PORT)?;
let database_name: String =
get_env(OCKAM_POSTGRES_DATABASE_NAME)?.unwrap_or("postgres".to_string());
let user: Option<String> = get_env(OCKAM_POSTGRES_USER)?;
let password: Option<String> = get_env(OCKAM_POSTGRES_PASSWORD)?;
println!("boom: {host}:{port}/{database_name}/{user:?}/{password:?}");

// only run the postgres tests if the OCKAM_POSTGRES_* environment variables are set
if let Ok(db) = SqlxDatabase::create_new_postgres().await {

if 2 == 1 + 1 {
panic!("boom: {host}:{port}/{database_name}/{user:?}/{password:?}");
};
rethrow("Postgres local", f(db.clone())).await?;
db.drop_all_postgres_tables().await?;
};
Expand Down

0 comments on commit bb62eaa

Please sign in to comment.