Skip to content

Commit

Permalink
test: bats boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
bodymindarts committed May 2, 2024
1 parent 9bab2c0 commit 71a7be7
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 19 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ Cargo.lock
*.pdb

.direnv/

-
.e2e-logs
.cala
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,11 @@ check-code: sdl
SQLX_OFFLINE=true cargo clippy --all-features
SQLX_OFFLINE=true cargo audit

build:
SQLX_OFFLINE=true cargo build --locked

e2e: clean-deps start-deps build
bats -t bats

sdl:
SQLX_OFFLINE=true cargo run --bin write_sdl > cala-server/schema.graphql
17 changes: 17 additions & 0 deletions bats/examples.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bats

load "helpers"

setup_file() {
reset_pg
start_server
}

teardown_file() {
stop_server
}


@test "rust: entities sync to server" {
cargo run --bin cala-ledger-example-rust
}
74 changes: 74 additions & 0 deletions bats/helpers.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
REPO_ROOT=$(git rev-parse --show-toplevel)
COMPOSE_PROJECT_NAME="${COMPOSE_PROJECT_NAME:-${REPO_ROOT##*/}}"

CALA_HOME="${CALA_HOME:-.cala}"

reset_pg() {
docker exec "${COMPOSE_PROJECT_NAME}-server-pg-1" psql $PG_CON -c "DROP SCHEMA public CASCADE"
docker exec "${COMPOSE_PROJECT_NAME}-server-pg-1" psql $PG_CON -c "CREATE SCHEMA public"
docker exec "${COMPOSE_PROJECT_NAME}-examples-pg-1" psql $PG_CON -c "DROP SCHEMA public CASCADE"
docker exec "${COMPOSE_PROJECT_NAME}-examples-pg-1" psql $PG_CON -c "CREATE SCHEMA public"
}

server_cmd() {
server_location=${REPO_ROOT}/target/debug/cala-server
if [[ ! -z ${CARGO_TARGET_DIR} ]] ; then
server_location=${CARGO_TARGET_DIR}/debug/cala-server
fi

${server_location} $@
}

start_server() {
background server_cmd > .e2e-logs
for i in {1..20}
do
if head .e2e-logs | grep -q 'Starting graphql server on port'; then
break
else
sleep 1
fi
done
}

stop_server() {
if [[ -f ${CALA_HOME}/server-pid ]]; then
kill -9 $(cat ${CALA_HOME}/server-pid) || true
fi
}

# Run the given command in the background. Useful for starting a
# node and then moving on with commands that exercise it for the
# test.
#
# Ensures that BATS' handling of file handles is taken into account;
# see
# https://github.com/bats-core/bats-core#printing-to-the-terminal
# https://github.com/sstephenson/bats/issues/80#issuecomment-174101686
# for details.
background() {
"$@" 3>- &
echo $!
}

# Taken from https://github.com/docker/swarm/blob/master/test/integration/helpers.bash
# Retry a command $1 times until it succeeds. Wait $2 seconds between retries.
retry() {
local attempts=$1
shift
local delay=$1
shift
local i

for ((i=0; i < attempts; i++)); do
run "$@"
if [[ "$status" -eq 0 ]] ; then
return 0
fi
sleep "$delay"
done

echo "Command \"$*\" failed $attempts times. Output: $output"
false
}

27 changes: 24 additions & 3 deletions cala-server/src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
pub mod config;
mod db;

use anyhow::Context;
use clap::Parser;
use std::path::PathBuf;
use std::{fs, path::PathBuf};

use self::config::{Config, EnvOverride};

Expand All @@ -17,6 +18,13 @@ struct Cli {
value_name = "FILE"
)]
config: PathBuf,
#[clap(
long,
env = "CALA_HOME",
default_value = ".cala",
value_name = "DIRECTORY"
)]
cala_home: String,
#[clap(env = "PG_CON")]
pg_con: String,
}
Expand All @@ -26,18 +34,31 @@ pub async fn run() -> anyhow::Result<()> {

let config = Config::from_path(cli.config, EnvOverride { db_con: cli.pg_con })?;

run_cmd(config).await?;
run_cmd(&cli.cala_home, config).await?;

Ok(())
}

async fn run_cmd(config: Config) -> anyhow::Result<()> {
async fn run_cmd(cala_home: &str, config: Config) -> anyhow::Result<()> {
use cala_ledger::{CalaLedger, CalaLedgerConfig};
cala_tracing::init_tracer(config.tracing)?;
store_server_pid(cala_home, std::process::id())?;
let pool = db::init_pool(&config.db).await?;
let ledger_config = CalaLedgerConfig::builder().pool(pool.clone()).build()?;
let ledger = CalaLedger::init(ledger_config).await?;
let app = crate::app::CalaApp::new(pool, ledger);
crate::server::run(config.server, app).await?;
Ok(())
}

pub fn store_server_pid(cala_home: &str, pid: u32) -> anyhow::Result<()> {
create_cala_dir(cala_home)?;
let _ = fs::remove_file(format!("{cala_home}/server-pid"));
fs::write(format!("{cala_home}/server-pid"), pid.to_string()).context("Writing PID file")?;
Ok(())
}

fn create_cala_dir(bria_home: &str) -> anyhow::Result<()> {
let _ = fs::create_dir(bria_home);
Ok(())
}
8 changes: 0 additions & 8 deletions docker-compose.override.yml

This file was deleted.

18 changes: 16 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ services:
integration-deps:
image: busybox
depends_on:
- postgres
- server-pg
- examples-pg
- otel-agent
postgres:
server-pg:
image: postgres:14.1
ports:
- "5432:5432"
Expand All @@ -18,6 +19,19 @@ services:
interval: 5s
timeout: 5s
retries: 5
examples-pg:
image: postgres:14.1
ports:
- "5433:5432"
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=password
- POSTGRES_DB=pg
healthcheck:
test: [ "CMD-SHELL", "pg_isready" ]
interval: 5s
timeout: 5s
retries: 5
otel-agent:
image: otel/opentelemetry-collector-contrib:0.57.2
command: [ "--config=/etc/otel-agent-config.yaml" ]
Expand Down
7 changes: 3 additions & 4 deletions examples/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ version = "0.1.0"
edition = "2021"
workspace = "../../"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0"
cala-ledger = { path = "../../cala-ledger" }
tokio = { version = "1.34", features = ["rt-multi-thread", "macros"] }

anyhow = { workspace = true }
tokio = { workspace = true }
sqlx = { workspace = true }
3 changes: 1 addition & 2 deletions examples/rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use cala_ledger::{account::*, journal::*, migrate::IncludeMigrations, query::*,

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let pg_host = std::env::var("PG_HOST").unwrap_or("localhost".to_string());
let pg_con = format!("postgres://user:password@{pg_host}:5432/pg");
let pg_con = format!("postgres://user:password@localhost:5433/pg");
let pool = sqlx::postgres::PgPoolOptions::new()
.max_connections(20)
.connect(&pg_con)
Expand Down

0 comments on commit 71a7be7

Please sign in to comment.