Skip to content

Commit

Permalink
fix gdal mismatch issue and add test (#581)
Browse files Browse the repository at this point in the history
* fix gdal mismatch issue and add test for publishing vrt using gdal plugin
  • Loading branch information
NyakudyaA committed Nov 1, 2023
1 parent 46f543c commit acd96d7
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 1 deletion.
27 changes: 27 additions & 0 deletions scenario_tests/stores/docker-compose-gdal.yml
Original file line number Diff line number Diff line change
@@ -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


24 changes: 24 additions & 0 deletions scenario_tests/stores/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
67 changes: 67 additions & 0 deletions scenario_tests/stores/tests/test_gdal.py
Original file line number Diff line number Diff line change
@@ -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'''
<coverageStore>
<name>{self.store_name}</name>
<workspace>{self.geo_workspace_name}</workspace>
<enabled>true</enabled>
<type>VRT</type>
</coverageStore>
'''
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()
19 changes: 18 additions & 1 deletion scripts/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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-<version>.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}')
Expand Down Expand Up @@ -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

0 comments on commit acd96d7

Please sign in to comment.