From acd96d71c4a3dd55532811cb780f696ee029438e Mon Sep 17 00:00:00 2001 From: mazano Date: Sat, 21 Oct 2023 09:35:23 +0200 Subject: [PATCH] fix gdal mismatch issue and add test (#581) * fix gdal mismatch issue and add test for publishing vrt using gdal plugin --- scenario_tests/stores/docker-compose-gdal.yml | 27 ++++++++ scenario_tests/stores/test.sh | 24 +++++++ scenario_tests/stores/tests/test_gdal.py | 67 +++++++++++++++++++ scripts/setup.sh | 19 +++++- 4 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 scenario_tests/stores/docker-compose-gdal.yml create mode 100644 scenario_tests/stores/tests/test_gdal.py diff --git a/scenario_tests/stores/docker-compose-gdal.yml b/scenario_tests/stores/docker-compose-gdal.yml new file mode 100644 index 00000000..5b9d2e4e --- /dev/null +++ b/scenario_tests/stores/docker-compose-gdal.yml @@ -0,0 +1,27 @@ + +version: '2.1' + +volumes: + geoserver-data-dir: + +services: + + geoserver: + image: 'kartoza/geoserver:${TAG:-manual-build}' + restart: 'always' + volumes: + - geoserver-data-dir:/opt/geoserver/data_dir + - ./tests:/tests + environment: + GEOSERVER_ADMIN_PASSWORD: myawesomegeoserver + GEOSERVER_ADMIN_USER: admin + TEST_CLASS: test_gdal.TestGeoServerGDAL + ports: + - "8080" + healthcheck: + test: "curl --fail --silent --write-out 'HTTP CODE : %{http_code}\n' --output /dev/null -u admin:'myawesomegeoserver' http://localhost:8080/geoserver/rest/about/version.xml" + interval: 1m30s + timeout: 10s + retries: 3 + + diff --git a/scenario_tests/stores/test.sh b/scenario_tests/stores/test.sh index bdfb544c..ac17e0d5 100755 --- a/scenario_tests/stores/test.sh +++ b/scenario_tests/stores/test.sh @@ -12,6 +12,7 @@ if [[ $(dpkg -l | grep "docker-compose") > /dev/null ]];then VERSION='docker compose' fi +# JNDI store ${VERSION} -f docker-compose-postgis-jndi.yml up -d if [[ -n "${PRINT_TEST_LOGS}" ]]; then @@ -33,3 +34,26 @@ for service in "${services[@]}"; do done ${VERSION} -f docker-compose-postgis-jndi.yml down -v + +# Run GDAL +${VERSION} -f docker-compose-gdal.yml up -d + +if [[ -n "${PRINT_TEST_LOGS}" ]]; then + ${VERSION} -f docker-compose-gdal.yml logs -f & +fi + +sleep 60 + + +services=("geoserver") + +for service in "${services[@]}"; do + + # Execute tests + sleep 60 + echo "Execute test for $service" + ${VERSION} -f docker-compose-gdal.yml exec $service /bin/bash /tests/test.sh + +done + +${VERSION} -f docker-compose-gdal.yml down -v diff --git a/scenario_tests/stores/tests/test_gdal.py b/scenario_tests/stores/tests/test_gdal.py new file mode 100644 index 00000000..231d7162 --- /dev/null +++ b/scenario_tests/stores/tests/test_gdal.py @@ -0,0 +1,67 @@ +import unittest +from os import environ, chown +from subprocess import check_call +from requests import get, post, exceptions +from requests.auth import HTTPBasicAuth +from geo.Geoserver import Geoserver + + +class TestGeoServerGDAL(unittest.TestCase): + + def setUp(self): + # Define the GeoServer URL + self.gs_url = 'http://localhost:8080/geoserver' + self.store_name = 'sfdem' + self.geo_username = environ.get('GEOSERVER_ADMIN_USER', 'admin') + self.geo_password = environ.get('GEOSERVER_ADMIN_PASSWORD', 'myawesomegeoserver') + self.geo_workspace_name = 'kartoza' + self.geo_data_dir = '/usr/local/tomcat/data/data/sf' + + def test_publish_gdal_store(self): + # Generate tiles + built_vrt = f'gdalbuildvrt {self.geo_data_dir}/sfdem.vrt {self.geo_data_dir}/sfdem.tif' + check_call(built_vrt, shell=True) + vrt_path = f'{self.geo_data_dir}/sfdem.vrt' + chown(vrt_path, 1000, 1000) + + geo = Geoserver(self.gs_url, username=self.geo_username, password=self.geo_password) + + auth = HTTPBasicAuth(self.geo_username, self.geo_password) + + # Create a workspace if it doesn't exist + try: + rest_url = f'{self.gs_url}/rest/workspaces/{self.geo_workspace_name}.json' + response = get(rest_url, auth=auth) + response.raise_for_status() + except exceptions.HTTPError: + geo.create_workspace(workspace=self.geo_workspace_name) + geo.set_default_workspace(self.geo_workspace_name) + + # Create a GDAL store + store_data = f''' + + {self.store_name} + {self.geo_workspace_name} + true + VRT + + ''' + store_url = f'{self.gs_url}/rest/workspaces/{self.geo_workspace_name}/coveragestores' + response = post(store_url, data=store_data, auth=auth, headers={'Content-Type': 'application/xml'}) + self.assertEqual(response.status_code, 201) + + # Upload the VRT file to the store + vrt_upload_url = f'{self.gs_url}/rest/workspaces/{self.geo_workspace_name}/coveragestores/{self.store_name}/external.vrt?configure=first&coverageName={self.store_name}' + curl_command = f'curl -u {self.geo_username}:{self.geo_password} -v -XPUT -H "Content-type: text/plain" -d "file:{vrt_path}" {vrt_upload_url}' + check_call(curl_command, shell=True) + + # Check that the published layer exists + layer_source_url = f'{self.gs_url}/{self.geo_workspace_name}/wms?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&FORMAT=image/jpeg&TRANSPARENT=true&STYLES&LAYERS={self.geo_workspace_name}:{self.store_name}&exceptions=application/vnd.ogc.se_inimage&SRS=EPSG:26713&WIDTH=768&HEIGHT=578&BBOX=594739.7048312925,4919224.415741393,602069.4450795503,4924731.264860202' + response = get(layer_source_url) + + # Check that the response has a status code of 200 (OK) + self.assertEqual(response.status_code, 200) + + +if __name__ == '__main__': + unittest.main() diff --git a/scripts/setup.sh b/scripts/setup.sh index cd8004cb..ab64c3de 100644 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -109,6 +109,23 @@ if ls /tmp/resources/plugins/*.zip >/dev/null 2>&1; then done fi +lib_dir="${GEOSERVER_INSTALL_DIR}/webapps/${GEOSERVER_CONTEXT_ROOT}/WEB-INF/lib" + +# Search for gdal-.jar files in the lib directory +for jar_file in "$lib_dir"/gdal-*.jar; do + if [[ -f "$jar_file" ]]; then + # Extract the version number + version=$(basename "$jar_file" | sed 's/gdal-\(.*\)\.jar/\1/') + break + fi +done + +GDAL_VERSION=$(gdalinfo --version | head -n1 | cut -d" " -f2 | tr -d ,,) + +if [[ ${GDAL_VERSION} != ${version} ]];then + rm ${lib_dir}/gdal-${version}.jar + wget https://repo1.maven.org/maven2/org/gdal/gdal/${GDAL_VERSION:0:3}.0/gdal-${GDAL_VERSION:0:3}.0.jar -O ${lib_dir}/gdal-${GDAL_VERSION:0:3}.0.jar +fi # Install Marlin render https://www.geocat.net/docs/geoserver-enterprise/2020.5/install/production/marlin.html JAVA_VERSION=$(java -version 2>&1 | awk -F '"' '/version/ {print $2}') @@ -166,4 +183,4 @@ rm -rf /tmp/resources # Delete resources which will be setup on first run delete_file "${CATALINA_HOME}"/conf/tomcat-users.xml -delete_file "${CATALINA_HOME}"/conf/web.xml +delete_file "${CATALINA_HOME}"/conf/web.xml \ No newline at end of file