-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cargo make + docker for e2e tests
- Loading branch information
1 parent
cc51eb6
commit 4133b3c
Showing
14 changed files
with
132 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
[tasks.docker-up] | ||
script = [ | ||
"docker compose up --detach", | ||
"sleep 3", | ||
] | ||
|
||
[tasks.run-server] | ||
script = [ | ||
"cargo run &", | ||
"echo $! > server.pid", | ||
] | ||
|
||
[tasks.run-tests] | ||
script = [ | ||
"cargo test", | ||
"sleep 1" | ||
] | ||
|
||
[tasks.wait-for-server] | ||
script = [ | ||
"until nc -z -v -w5 localhost 8080; do", | ||
" echo 'Waiting for server to start on port 8080...'", | ||
" sleep 1", | ||
"done" | ||
] | ||
|
||
[tasks.kill-server] | ||
script = [ | ||
"kill $(cat server.pid)", | ||
"rm server.pid" | ||
] | ||
|
||
[tasks.docker-down] | ||
script = [ | ||
"docker compose down" | ||
] | ||
|
||
[tasks.full-test] | ||
dependencies = [ | ||
"docker-up", | ||
"run-server", | ||
"wait-for-server", | ||
"run-tests", | ||
"kill-server", | ||
"docker-down" | ||
] | ||
|
||
[tasks.full-clean] | ||
script = [ | ||
"docker rmi ratings-postgres && docker rmi redis", | ||
"cargo clean" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
FROM postgres:latest | ||
|
||
COPY init.sh /docker-entrypoint-initdb.d/ | ||
|
||
RUN chmod +x /docker-entrypoint-initdb.d/init.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
# Create users and databases | ||
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "postgres" <<-EOSQL | ||
CREATE USER $MIGRATION_USER WITH PASSWORD '$MIGRATION_PASSWORD'; | ||
CREATE USER $SERVICE_USER WITH PASSWORD '$SERVICE_PASSWORD'; | ||
CREATE DATABASE $RATINGS_DB; | ||
EOSQL | ||
|
||
# Set permissions on the new database | ||
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$RATINGS_DB" <<-EOSQL | ||
GRANT CONNECT ON DATABASE $RATINGS_DB TO $MIGRATION_USER; | ||
GRANT USAGE, CREATE ON SCHEMA public TO $MIGRATION_USER; | ||
EOSQL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,11 @@ | ||
use std::sync::{Arc, Once}; | ||
|
||
use once_cell::sync::Lazy; | ||
use ratings::utils::Migrator; | ||
use tokio::sync::Mutex; | ||
use std::sync::Once; | ||
|
||
static INIT: Once = Once::new(); | ||
static TEST_COUNTER: Lazy<Arc<Mutex<i32>>> = Lazy::new(|| Arc::new(Mutex::new(0))); | ||
|
||
pub async fn before_all(migrator: Migrator) { | ||
pub async fn before_all() { | ||
INIT.call_once(|| { | ||
tracing_subscriber::fmt().init(); | ||
}); | ||
if let Err(e) = migrator.run().await { | ||
panic!("{}", e) | ||
} | ||
|
||
let counter = Arc::clone(&*TEST_COUNTER); | ||
let mut test_counter = counter.lock().await; | ||
*test_counter += 1; | ||
} | ||
|
||
pub async fn after_all(migrator: Migrator) { | ||
let counter = Arc::clone(&*TEST_COUNTER); | ||
let mut test_counter = counter.lock().await; | ||
*test_counter -= 1; | ||
if *test_counter == 0 { | ||
if let Err(e) = migrator.revert().await { | ||
panic!("{}", e) | ||
} | ||
} | ||
} | ||
pub async fn after_all() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
use std::future::Future; | ||
|
||
use crate::helpers::hooks::{after_all, before_all}; | ||
use ratings::utils::Migrator; | ||
|
||
pub async fn with_lifecycle<F>(f: F, migrator: Migrator) | ||
pub async fn with_lifecycle<F>(f: F) | ||
where | ||
F: Future<Output = ()>, | ||
{ | ||
before_all(migrator.clone()).await; | ||
before_all().await; | ||
f.await; | ||
after_all(migrator.clone()).await; | ||
after_all().await; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,42 @@ | ||
use ratings::utils::{Config, Migrator}; | ||
use ratings::utils::Config; | ||
use tonic::Code; | ||
|
||
use super::super::helpers::{client_user::UserClient, with_lifecycle::with_lifecycle}; | ||
|
||
#[tokio::test] | ||
async fn blank() -> Result<(), Box<dyn std::error::Error>> { | ||
let config = Config::load()?; | ||
let migrator = Migrator::new(&config.migration_postgres_uri).await?; | ||
|
||
with_lifecycle( | ||
async { | ||
let id = ""; | ||
let client = UserClient::new(&config.socket()); | ||
with_lifecycle(async { | ||
let id = ""; | ||
let client = UserClient::new(&config.socket()); | ||
|
||
match client.register(id).await { | ||
Ok(response) => panic!("expected Err but got Ok: {response:?}"), | ||
Err(status) => { | ||
assert_eq!(status.code(), Code::InvalidArgument) | ||
} | ||
match client.register(id).await { | ||
Ok(response) => panic!("expected Err but got Ok: {response:?}"), | ||
Err(status) => { | ||
assert_eq!(status.code(), Code::InvalidArgument) | ||
} | ||
}, | ||
migrator, | ||
) | ||
} | ||
}) | ||
.await; | ||
Ok(()) | ||
} | ||
|
||
#[tokio::test] | ||
async fn wrong_length() -> Result<(), Box<dyn std::error::Error>> { | ||
let config = Config::load()?; | ||
let migrator = Migrator::new(&config.migration_postgres_uri).await?; | ||
|
||
with_lifecycle( | ||
async { | ||
let client_hash = "foobarbazbun"; | ||
let client = UserClient::new(&config.socket()); | ||
with_lifecycle(async { | ||
let client_hash = "foobarbazbun"; | ||
let client = UserClient::new(&config.socket()); | ||
|
||
match client.register(client_hash).await { | ||
Ok(response) => panic!("expected Err but got Ok: {response:?}"), | ||
Err(status) => { | ||
assert_eq!(status.code(), Code::InvalidArgument) | ||
} | ||
match client.register(client_hash).await { | ||
Ok(response) => panic!("expected Err but got Ok: {response:?}"), | ||
Err(status) => { | ||
assert_eq!(status.code(), Code::InvalidArgument) | ||
} | ||
}, | ||
migrator, | ||
) | ||
} | ||
}) | ||
.await; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters