1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Tests for tools.exec_utils module.
10 from pathlib import Path
15 import nominatim.tools.exec_utils as exec_utils
17 class TestRunLegacyScript:
19 @pytest.fixture(autouse=True)
20 def setup_nominatim_env(self, tmp_path, def_config):
21 tmp_phplib_dir = tmp_path / 'phplib'
22 tmp_phplib_dir.mkdir()
23 (tmp_phplib_dir / 'admin').mkdir()
27 phplib_dir = tmp_phplib_dir
28 data_dir = Path('data')
29 project_dir = Path('.')
30 sqllib_dir = Path('lib-sql')
31 config_dir = Path('settings')
33 osm2pgsql_path = 'osm2pgsql'
35 self.testenv = _NominatimEnv
38 def mk_script(self, code):
39 codefile = self.testenv.phplib_dir / 'admin' / 't.php'
40 codefile.write_text('<?php\n' + code + '\n')
45 @pytest.mark.parametrize("return_code", (0, 1, 15, 255))
46 def test_run_legacy_return_exit_code(self, return_code):
47 fname = self.mk_script('exit({});'.format(return_code))
48 assert return_code == \
49 exec_utils.run_legacy_script(fname, nominatim_env=self.testenv)
52 def test_run_legacy_return_throw_on_fail(self):
53 fname = self.mk_script('exit(11);')
54 with pytest.raises(subprocess.CalledProcessError):
55 exec_utils.run_legacy_script(fname, nominatim_env=self.testenv,
59 def test_run_legacy_return_dont_throw_on_success(self):
60 fname = self.mk_script('exit(0);')
61 assert exec_utils.run_legacy_script(fname, nominatim_env=self.testenv,
62 throw_on_fail=True) == 0
64 def test_run_legacy_use_given_module_path(self):
65 fname = self.mk_script("exit($_SERVER['NOMINATIM_DATABASE_MODULE_PATH'] == '' ? 0 : 23);")
67 assert exec_utils.run_legacy_script(fname, nominatim_env=self.testenv) == 0
70 def test_run_legacy_do_not_overwrite_module_path(self, monkeypatch):
71 monkeypatch.setenv('NOMINATIM_DATABASE_MODULE_PATH', 'other')
72 fname = self.mk_script(
73 "exit($_SERVER['NOMINATIM_DATABASE_MODULE_PATH'] == 'other' ? 0 : 1);")
75 assert exec_utils.run_legacy_script(fname, nominatim_env=self.testenv) == 0
78 def test_run_legacy_default_osm2pgsql_binary(self, monkeypatch):
79 fname = self.mk_script("exit($_SERVER['NOMINATIM_OSM2PGSQL_BINARY'] == 'osm2pgsql' ? 0 : 23);")
81 assert exec_utils.run_legacy_script(fname, nominatim_env=self.testenv) == 0
84 def test_run_legacy_override_osm2pgsql_binary(self, monkeypatch):
85 monkeypatch.setenv('NOMINATIM_OSM2PGSQL_BINARY', 'somethingelse')
87 fname = self.mk_script("exit($_SERVER['NOMINATIM_OSM2PGSQL_BINARY'] == 'somethingelse' ? 0 : 23);")
89 assert exec_utils.run_legacy_script(fname, nominatim_env=self.testenv) == 0
92 class TestRunApiScript:
95 @pytest.fixture(autouse=True)
96 def setup_project_dir(tmp_path):
97 webdir = tmp_path / 'website'
99 (webdir / 'test.php').write_text("<?php\necho 'OK\n';")
103 def test_run_api(tmp_path):
104 assert exec_utils.run_api_script('test', tmp_path) == 0
107 def test_run_api_execution_error(tmp_path):
108 assert exec_utils.run_api_script('badname', tmp_path) != 0
111 def test_run_api_with_extra_env(tmp_path):
112 extra_env = dict(SCRIPT_FILENAME=str(tmp_path / 'website' / 'test.php'))
113 assert exec_utils.run_api_script('badname', tmp_path, extra_env=extra_env) == 0
116 def test_custom_phpcgi(tmp_path, capfd):
117 assert exec_utils.run_api_script('test', tmp_path, phpcgi_bin='env',
118 params={'q' : 'Berlin'}) == 0
119 captured = capfd.readouterr()
121 assert '?q=Berlin' in captured.out
124 def test_fail_on_error_output(tmp_path):
125 # Starting PHP 8 the PHP CLI no longer has STDERR defined as constant
128 if(!defined('STDERR')) define('STDERR', fopen('php://stderr', 'wb'));
129 fwrite(STDERR, 'WARNING'.PHP_EOL);
131 (tmp_path / 'website' / 'bad.php').write_text(php)
133 assert exec_utils.run_api_script('bad', tmp_path) == 1
137 def test_run_osm2pgsql(osm2pgsql_options):
138 osm2pgsql_options['append'] = False
139 osm2pgsql_options['import_file'] = 'foo.bar'
140 osm2pgsql_options['tablespaces']['slim_data'] = 'extra'
141 exec_utils.run_osm2pgsql(osm2pgsql_options)
144 def test_run_osm2pgsql_disable_jit(osm2pgsql_options):
145 osm2pgsql_options['append'] = True
146 osm2pgsql_options['import_file'] = 'foo.bar'
147 osm2pgsql_options['disable_jit'] = True
148 exec_utils.run_osm2pgsql(osm2pgsql_options)