Skip to content
Snippets Groups Projects
Verified Commit 78f58c70 authored by David Hoese's avatar David Hoese
Browse files

Add initial Postgres initialization to unit tests

parent 77db69bd
No related branches found
No related tags found
No related merge requests found
......@@ -15,6 +15,39 @@ fi
image_url=$1
image_tag=$2
MINIO_SERVER_NAME="test-minio-server"
export AWS_ACCESS_KEY_ID="minioadmin"
export AWS_SECRET_ACCESS_KEY="minioadmin"
S3_PORT=9000
PG_PORT=5432
PG_SERVER_NAME="postgres_db"
POSTGRES_USER="postgres"
POSTGRES_PASSWORD="1234"
NETWORK_NAME=`basename "$0"`
NETWORK_NAME=${NETWORK_NAME/.sh/}
base_tmp_dir="__TOBECREATED__"
setup_test() {
base_tmp_dir=$(mktemp -d --suffix="-geotiffs")
cd "${base_tmp_dir}"
echo "Temporary directory: ${base_tmp_dir}"
docker network create ${NETWORK_NAME}
}
teardown_test() {
kill_test_container || echo "Could not kill test container"
kill_postgres || echo "Could not kill postgres container"
if [ -d $base_tmp_dir ]; then
rm -rf $base_tmp_dir
fi
docker network rm ${NETWORK_NAME} > /dev/null || echo "Could not remove docker network"
}
graceful_exit() {
debug "Graceful exit"
teardown_test
}
add_shapefile_content() {
debug "Creating fake shapefile directory for C01"
......@@ -24,9 +57,30 @@ add_shapefile_content() {
docker exec test python3 -c "import fiona; fiona.open('/data/tiles/g16/abi/radf/C01/C01.shp', 'w', driver='ESRI Shapefile', schema={'geometry': 'Polygon', 'properties': {'location': 'str', 'time': 'str:19'}})"
}
add_postgres_content() {
debug "Creating PostGIS table"
docker exec -i ${PG_SERVER_NAME} psql -U ${POSTGRES_USER} <<EOF
CREATE TABLE IF NOT EXISTS g16_abi_radf_l1b_c01 (
gid SERIAL PRIMARY KEY,
start_time CHAR(19) NOT NULL UNIQUE,
location VARCHAR(255) NOT NULL,
bbox_geometry geometry(POLYGON, 930916)
)
EOF
}
add_postgres_projections() {
# copied from tile gen
debug "Creating PostGIS projections"
docker exec -i ${PG_SERVER_NAME} psql -U ${POSTGRES_USER} <<EOF
INSERT into spatial_ref_sys (srid, auth_name, auth_srid, srtext, proj4text)
values (930916, 'EPSG', 4269, 'PROJCRS["GOES-16 ABI Fixed Grid",BASEGEOGCRS["GOES-16 ABI Fixed Grid",DATUM["North American Datum 1983",ELLIPSOID["GRS 1980",6378137,298.257222101,LENGTHUNIT["metre",1]],ID["EPSG",6269]],PRIMEM["Greenwich",0,ANGLEUNIT["degree",0.0174532925199433],ID["EPSG",8901]]],CONVERSION["unknown",METHOD["Geostationary Satellite (Sweep X)"],PARAMETER["Longitude of natural origin",-75,ANGLEUNIT["degree",0.0174532925199433],ID["EPSG",8802]],PARAMETER["Satellite Height",35786023,LENGTHUNIT["metre",1,ID["EPSG",9001]]],PARAMETER["False easting",0,LENGTHUNIT["metre",1],ID["EPSG",8806]],PARAMETER["False northing",0,LENGTHUNIT["metre",1],ID["EPSG",8807]]],CS[Cartesian,2],AXIS["(E)",east,ORDER[1],LENGTHUNIT["metre",1,ID["EPSG",9001]]],AXIS["(N)",north,ORDER[2],LENGTHUNIT["metre",1,ID["EPSG",9001]]]]', '+proj=geos +sweep=x +lon_0=-75 +h=35786023 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs +type=crs') ON CONFLICT (srid) DO NOTHING
EOF
}
start_test_container() {
debug "Starting test docker container (${image_url}:${image_tag})..."
docker run --rm -d --name test -p 8888:80 $@ ${image_url}:${image_tag}
docker run --rm -d --network ${NETWORK_NAME} --name test -p 8888:80 $@ ${image_url}:${image_tag}
start_status=$?
# just wait a bit to let the server start
sleep 2
......@@ -34,12 +88,39 @@ start_test_container() {
return $start_status
}
start_pg_test_container() {
mkdir pg_secrets
echo "${POSTGRES_PASSWORD}" > pg_secrets/fake_file
start_test_container -v "$(pwd)"/pg_secrets:/secrets -e POSTGRES_HOST=${PG_SERVER_NAME} -e POSTGRES_PORT=${PG_PORT} -e POSTGRES_PASSWORD_FILE="/secrets/fake_file"
}
# start_minio() {
# base_dir=$1
# docker run -d --rm --name ${MINIO_SERVER_NAME} --user ${UID}:${UID} -p ${S3_PORT}:9000 -p 9001:9001 -v ${base_dir}:/data minio/minio server /data --console-address ":9001"
# }
start_postgres() {
debug "Starting Postgres database..."
docker run --rm -d --network ${NETWORK_NAME} --name ${PG_SERVER_NAME} -e POSTGRES_PASSWORD="${POSTGRES_PASSWORD}" postgis/postgis
debug "Sleeping for 5 seconds for DB to start up..."
sleep 5
}
kill_test_container() {
debug "Killing docker container..."
debug "-----------------------------------------"
docker logs test
debug "-----------------------------------------"
docker kill test >/dev/null
debug "Done killing docker container."
}
kill_postgres() {
debug "Killing postgres container..."
docker kill ${PG_SERVER_NAME} >/dev/null
debug "Done killing postgres container."
}
curl_index() {
debug "Starting curl basic request..."
curl --fail -sS --max-time 5 "http://localhost:8888/" >/dev/null
......@@ -48,6 +129,7 @@ curl_index() {
curl_layer_times() {
debug "Starting curl basic time request..."
# NOTE: The time doesn't actually exist and no image data is available. A blank image should be returned
# TODO: Verify that the expected time is returned
curl --fail -sS "http://localhost:8888/wms_times/g16/abi/radf/C01" >/dev/null
}
......@@ -58,30 +140,39 @@ curl_empty_tile() {
}
run_basic_shapefile_tests() {
sf_exit_status=0
set -e
setup_test
debug "Starting shapefile tests..."
start_test_container || sf_exit_status=1
curl_index || sf_exit_status=1
curl_layer_times || sf_exit_status=1
curl_empty_tile || sf_exit_status=1
kill_test_container
return $sf_exit_status
start_test_container
curl_index
curl_layer_times
curl_empty_tile
teardown_test
debug "SUCCESS: Shapefile test completed successfully"
set +e
}
run_basic_postgres_tests() {
pg_exit_status=0
set -e
setup_test
debug "Starting postgres tests..."
start_test_container -e POSTGRES_HOST=localhost -e POSTGRES_PASSWORD_FILE="/secrets/fake_file" || pg_exit_status=1
start_postgres
start_pg_test_container
add_shapefile_content
curl_index || pg_exit_status=1
# curl_layer_times || sf_exit_status=1
curl_empty_tile || pg_exit_status=1
kill_test_container
return $pg_exit_status
add_postgres_projections
add_postgres_content
curl_index
curl_layer_times
curl_empty_tile
debug "SUCCESS: Postgres test completed successfully"
teardown_test
set +e
}
trap graceful_exit EXIT
exit_status=0
run_basic_shapefile_tests || exit_status=1
echo "#######"
......@@ -90,6 +181,7 @@ echo "#######"
if [[ $exit_status -eq 0 ]]; then
debug "SUCCESS"
trap - EXIT # tests should have cleared this already, otherwise produces extra output
else
debug "FAIL"
fi
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment