diff --git a/.github/actions/ubuntu-prerequisites/action.yml b/.github/actions/ubuntu-prerequisites/action.yml index 8a7eb0c9..b162627c 100644 --- a/.github/actions/ubuntu-prerequisites/action.yml +++ b/.github/actions/ubuntu-prerequisites/action.yml @@ -32,7 +32,7 @@ runs: python3-psycopg2 \ python3-setuptools \ zlib1g-dev - pip3 install behave + pip3 install behave osmium if [ "$CC" = clang-8 ]; then sudo apt-get install -yq --no-install-suggests --no-install-recommends clang-8; fi shell: bash diff --git a/.github/actions/win-install/action.yml b/.github/actions/win-install/action.yml index 7bcb20ec..008aac25 100644 --- a/.github/actions/win-install/action.yml +++ b/.github/actions/win-install/action.yml @@ -19,5 +19,5 @@ runs: zlib:x64-windows shell: bash - name: Install psycopg2 and behave - run: python -m pip install psycopg2 behave + run: python -m pip install psycopg2 behave osmium shell: bash diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 45a1bcde..4ded343d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,7 +15,7 @@ jobs: - name: Install prerequisites run: | brew install lua boost postgis pandoc cimg potrace - pip3 install psycopg2 behave + pip3 install psycopg2 behave osmium pg_ctl -D /usr/local/var/postgres init pg_ctl -D /usr/local/var/postgres start # Work around homebrew postgresql installation screw-up, see diff --git a/tests/bdd/command-line/replication.feature b/tests/bdd/command-line/replication.feature new file mode 100644 index 00000000..cb61a1f2 --- /dev/null +++ b/tests/bdd/command-line/replication.feature @@ -0,0 +1,32 @@ +Feature: Tests for the osm2pgsql-replication script + + Scenario: Replication can be initialised + + Given the OSM data + """ + n34 Tamenity=restaurant x77 y45.3 + """ + When running osm2pgsql pgsql with parameters + | --slim | + + And running osm2pgsql-replication + | init | --osm-file={TEST_DATA_DIR}/liechtenstein-2013-08-03.osm.pbf | + + Then table planet_osm_replication_status has 1 row + + Scenario: Replication can be initialised in different schema + + Given the database schema foobar + And the OSM data + """ + n34 Tamenity=restaurant x77 y45.3 + """ + When running osm2pgsql pgsql with parameters + | --slim | + + And running osm2pgsql-replication + | init | + | --osm-file={TEST_DATA_DIR}/liechtenstein-2013-08-03.osm.pbf | + | --middle-schema=foobar | + + Then table foobar.planet_osm_replication_status has 1 row diff --git a/tests/bdd/environment.py b/tests/bdd/environment.py index f05b5a88..f37b77d2 100644 --- a/tests/bdd/environment.py +++ b/tests/bdd/environment.py @@ -23,6 +23,7 @@ TEST_BASE_DIR = (Path(__file__) / '..' / '..').resolve() # behave -DBINARY=/tmp/my-builddir/osm2pgsql -DKEEP_TEST_DB USER_CONFIG = { 'BINARY': (TEST_BASE_DIR / '..' / 'build' / 'osm2pgsql').resolve(), + 'REPLICATION_SCRIPT': (TEST_BASE_DIR / '..' / 'scripts' / 'osm2pgsql-replication').resolve(), 'TEST_DATA_DIR': TEST_BASE_DIR / 'data', 'SRC_DIR': (TEST_BASE_DIR / '..').resolve(), 'KEEP_TEST_DB': False, diff --git a/tests/bdd/steps/steps_execute.py b/tests/bdd/steps/steps_execute.py index 7d782f5c..432a447d 100644 --- a/tests/bdd/steps/steps_execute.py +++ b/tests/bdd/steps/steps_execute.py @@ -9,6 +9,7 @@ Steps for executing osm2pgsql. """ from io import StringIO from pathlib import Path +import os import subprocess def get_import_file(context): @@ -38,8 +39,9 @@ def run_osm2pgsql(context, output): cmdline.extend(('-O', output)) cmdline.extend(context.osm2pgsql_params) + # convert table items to CLI arguments and inject constants to placeholders if context.table: - cmdline.extend(f for f in context.table.headings if f) + cmdline.extend(f.format(**context.config.userdata) for f in context.table.headings if f) for row in context.table: cmdline.extend(f.format(**context.config.userdata) for f in row if f) @@ -72,6 +74,31 @@ def run_osm2pgsql(context, output): return proc.returncode + +def run_osm2pgsql_replication(context): + cmdline = [str(Path(context.config.userdata['REPLICATION_SCRIPT']).resolve())] + # convert table items to CLI arguments and inject constants to placeholders + if context.table: + cmdline.extend(f.format(**context.config.userdata) for f in context.table.headings if f) + for row in context.table: + cmdline.extend(f.format(**context.config.userdata) for f in row if f) + + if '-d' not in cmdline and '--database' not in cmdline: + cmdline.extend(('-d', context.config.userdata['TEST_DB'])) + + # on Windows execute script directly with python, because shebang is not recognised + if os.name == 'nt': + cmdline.insert(0, "python") + + proc = subprocess.Popen(cmdline, cwd=str(context.workdir), + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + + _, errs = proc.communicate() + + return proc.returncode, errs.decode('utf-8') + + @given("no lua tagtransform") def do_not_setup_tagtransform(context): pass @@ -106,7 +133,7 @@ def setup_style_file(context, style): @when("running osm2pgsql (?P\w+)(?: with parameters)?") -def execute_osm2pgsql_sucessfully(context, output): +def execute_osm2pgsql_successfully(context, output): returncode = run_osm2pgsql(context, output) if context.scenario.status == "skipped": @@ -127,6 +154,15 @@ def execute_osm2pgsql_with_failure(context, output): assert returncode != 0, "osm2pgsql unexpectedly succeeded" +@when("running osm2pgsql-replication") +def execute_osm2pgsql_replication_successfully(context): + returncode, errs = run_osm2pgsql_replication(context) + + assert returncode == 0,\ + f"osm2pgsql-replication failed with error code {returncode}.\n"\ + f"Errors:\n{errs}" + + @then("the (?P\w+) output contains") def check_program_output(context, kind): if kind == 'error': diff --git a/tests/data/liechtenstein-2013-08-03.osm.pbf b/tests/data/liechtenstein-2013-08-03.osm.pbf index 9032c1c6..54bc92b7 100644 Binary files a/tests/data/liechtenstein-2013-08-03.osm.pbf and b/tests/data/liechtenstein-2013-08-03.osm.pbf differ