Skip to content
Snippets Groups Projects
test_mapserver_image.sh 7.39 KiB
#!/usr/bin/env bash

debug() {
    echo >&2 "DEBUG: $@"
}

error() {
    echo >&2 "ERROR: $@"
}

if [[ $# -ne 2 ]]; then
    error "Usage: ./test_mapserver_image.sh <image_url> <image_tag>"
    exit 1
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/}
C01_GTIFF_NAME="GOES-16_ABI_RadF_C01_20220302_194032_GOES-West.tif"
C01_ISOTIME="2022-03-02T19:40:32"
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 "FAIL"
    debug "Graceful exit"
    teardown_test
}

add_shapefile_content() {
    debug "Creating fake shapefile directory for C01"
    docker exec test mkdir -p /data/tiles/g16/abi/radf/C01
    debug "Creating fake shapefile file for C01"
    docker exec -i test python3 <<EOF
import fiona
from shapely.geometry import mapping, box

s_file = fiona.open('/data/tiles/g16/abi/radf/C01/C01.shp',
                    'w',
                    driver='ESRI Shapefile',
                    schema={'geometry': 'Polygon', 'properties': {'location': 'str', 'time': 'str:19'}})
bbox = mapping(box(-50000, -50000, 50000, 50000))
s_file.write({
    "geometry": bbox,
    "properties": {
        "location": "/data/tiles/g16/abi/radf/${C01_GTIFF_NAME}",
        "time": "${C01_ISOTIME}",
    },
})
EOF
}

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
#     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 --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
    debug "Container started."
    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
}

curl_layer_times() {
    expected_result=$1
    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
    time_result=$(curl --fail -sS "http://localhost:8888/wms_times/g16/abi/radf/C01")
    time_status=$?
    if [[ $time_status -ne 0 ]]; then
        return $time_status
    fi

    # []
    # ["2020-12-25T15:29:25"]
    if [[ "${time_result}" != "${expected_result}" ]]; then
        error "Time result (${time_result}) was not equal to the expected ${expected_result}"
        return 1
    fi
    debug "Time result (${time_result}) was equal to the expected ${expected_result}"
    return $time_status
}

curl_empty_tile() {
    debug "Starting curl basic mapfile request..."
    # NOTE: The time doesn't actually exist and no image data is available. A blank image should be returned
    curl --fail -sS "http://localhost:8888/wms/g16/abi/radf/l1b?VERSION=1.1.1&REQUEST=GetMap&SERVICE=WMS&STYLES=&BBOX=-1330667.479176%2c-2773559.926648%2c2773559.926648%2c1330667.479176&WIDTH=256&HEIGHT=256&FORMAT=rgba&SRS=EPSG%3a930916&LAYERS=C01&TIME=2022-04-20T16:00:21Z" >/dev/null || pg_exit_status=1
}

run_basic_shapefile_tests() {
    setup_test
    debug "Starting shapefile tests..."
    start_test_container
    add_shapefile_content

    curl_index
    curl_layer_times "[\"${C01_ISOTIME}\"]"
    curl_empty_tile
    teardown_test
    debug "SUCCESS: Shapefile test completed successfully"
}

run_basic_postgres_tests() {
    setup_test
    debug "Starting postgres tests..."
    start_postgres
    start_pg_test_container
    add_shapefile_content
    add_postgres_projections
    add_postgres_content

    curl_index
    curl_layer_times "[]"
    curl_empty_tile
    debug "SUCCESS: Postgres test completed successfully"
    teardown_test
}


trap graceful_exit EXIT

set -e
run_basic_shapefile_tests
echo "#######"
run_basic_postgres_tests
echo "#######"

trap - EXIT  # tests should have cleared this already, otherwise produces extra output
debug "SUCCESS"