Skip to content

Commit

Permalink
Handle SIGINT signals
Browse files Browse the repository at this point in the history
Without this commit, you have to provide the --init argument to Docker.
See moby/moby#2838 (comment)

This also adds a test that verifies that this works.
  • Loading branch information
schra committed Oct 9, 2019
1 parent 485a234 commit 6e2f4d7
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 1 deletion.
27 changes: 27 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ serde = { version = "1.0", features = ["derive"] } # For (de-)serialization supp
serde_json = "1.0" # For (de-)serializing JSON
slippy_map_tilenames = "0.2" # For calculating the coordinates of map tiles
tree_magic = { version = "0.2", features = ["staticmime"] } # For determining MIME types based on content
signal-hook = "0.1" # For correct signal handling if we have pid = 1

[profile.release]
lto = true
Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,16 @@ like `https://your.domain/my-images/8efbfe48-9a8a-41a8-8b2d-307b8cfffff4.jpg` to

You can find the API documentation [here](API.md).

## Testing the API
## Tests

Our integration tests are based on [bats](https://github.com/bats-core/bats-core).
To run them use:

```
bats tests
```

## Interactively testing the API

Compile the backend with the `testpages` feature to have easy access to rudimentary testpages at `/testpages/`.

Expand Down
7 changes: 7 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ fn testpage_routes() -> Option<Vec<Route>> {
fn main() {
check_required_configuration();

unsafe {
signal_hook::register(signal_hook::SIGINT, || {
println!("Received SIGINT signal.");
std::process::exit(0);
}).expect("signal handler could not be set");
}

let mut rocket = rocket::ignite()
.attach(DatabaseConnection::fairing())
.mount("/facilities", facilites_routes())
Expand Down
45 changes: 45 additions & 0 deletions tests/framework.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Common functions used for our tests

# use this instead of echo inside tests
log() {
local BLUE='\033[0;34m'
local RESET='\033[0m'
echo -e "$BLUE"[test] "$*""$RESET"
}

container-running() {
[ "$(docker inspect -f '{{.State.Running}}' "$1")" = "true" ]
}

container-stop() {
# ignore if there is an error (which means that the container is already stopped)
docker stop "$1" 2>&1>/dev/null || true
}

containers-stop() {
[ -n "${TONARI:-}" ] && container-stop "$TONARI"
[ -n "${MONGO:-}" ] && container-stop "$MONGO"
}

containers-run() {
export TONARI_SOURCE_ID=${TONARI_SOURCE_ID:-00000000000000000000000000000000}
export TONARI_IMAGE_URL_PREFIX=${TONARI_IMAGE_URL_PREFIX:-https://tonari.app/api/images/}
export TONARI_IMAGE_PATH=${TONARI_IMAGE_PATH:-/images}
export TONARI_DATABASE_NAME=${TONARI_DATABASE_NAME:-tonari}
export ROCKET_DATABASES=${ROCKET_DATABASES:-'{sanitary_facilities={url="mongodb://localhost:27017/sanitary_facilities"}}'}
export ROCKET_PORT=${ROCKET_PORT:-8000}
ROCKET_SECRET_KEY_DEFAULT=$(openssl rand -base64 32)
export ROCKET_SECRET_KEY=${ROCKET_SECRET_KEY:-$ROCKET_SECRET_KEY_DEFAULT}
export MONGO=$(docker run -d -p 27017:27017 mongo --wiredTigerCacheSizeGB 1.5)
export TONARI=$(docker run -d -eTONARI_{SOURCE_ID,IMAGE_URL_PREFIX,IMAGE_PATH} -eROCKET_{DATABASES,PORT} --network=host tonari/backend)
}

# bats setup
setup() {
containers-run
}

# bats teardown
teardown() {
containers-stop
}
23 changes: 23 additions & 0 deletions tests/sigint-stops-docker-container.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bats

load framework

# Test if a SIGINT signal stops the Docker container
@test "SIGINT stops Docker container" {
# wait so that the container is stopped if there are any startup problems
sleep 1

if ! container-running $TONARI; then
log Tonari failed to start:
docker logs $TONARI
echo Tonari is running > /dev/null
return 1
fi

docker kill --signal=SIGINT $TONARI > /dev/null
sleep 4
if container-running $TONARI; then
log Tonari failed to be killed
false
fi
}

0 comments on commit 6e2f4d7

Please sign in to comment.