2 Tests for tools.exec_utils module.
4 from pathlib import Path
9 import nominatim.tools.exec_utils as exec_utils
11 class TestRunLegacyScript:
13 @pytest.fixture(autouse=True)
14 def setup_nominatim_env(self, tmp_path, def_config):
15 tmp_phplib_dir = tmp_path / 'phplib'
16 tmp_phplib_dir.mkdir()
17 (tmp_phplib_dir / 'admin').mkdir()
21 phplib_dir = tmp_phplib_dir
22 data_dir = Path('data')
23 project_dir = Path('.')
24 sqllib_dir = Path('lib-sql')
25 config_dir = Path('settings')
27 osm2pgsql_path = 'osm2pgsql'
29 self.testenv = _NominatimEnv
32 def mk_script(self, code):
33 codefile = self.testenv.phplib_dir / 'admin' / 't.php'
34 codefile.write_text('<?php\n' + code + '\n')
39 @pytest.mark.parametrize("return_code", (0, 1, 15, 255))
40 def test_run_legacy_return_exit_code(self, return_code):
41 fname = self.mk_script('exit({});'.format(return_code))
42 assert return_code == \
43 exec_utils.run_legacy_script(fname, nominatim_env=self.testenv)
46 def test_run_legacy_return_throw_on_fail(self):
47 fname = self.mk_script('exit(11);')
48 with pytest.raises(subprocess.CalledProcessError):
49 exec_utils.run_legacy_script(fname, nominatim_env=self.testenv,
53 def test_run_legacy_return_dont_throw_on_success(self):
54 fname = self.mk_script('exit(0);')
55 assert exec_utils.run_legacy_script(fname, nominatim_env=self.testenv,
56 throw_on_fail=True) == 0
58 def test_run_legacy_use_given_module_path(self):
59 fname = self.mk_script("exit($_SERVER['NOMINATIM_DATABASE_MODULE_PATH'] == '' ? 0 : 23);")
61 assert exec_utils.run_legacy_script(fname, nominatim_env=self.testenv) == 0
64 def test_run_legacy_do_not_overwrite_module_path(self, monkeypatch):
65 monkeypatch.setenv('NOMINATIM_DATABASE_MODULE_PATH', 'other')
66 fname = self.mk_script(
67 "exit($_SERVER['NOMINATIM_DATABASE_MODULE_PATH'] == 'other' ? 0 : 1);")
69 assert exec_utils.run_legacy_script(fname, nominatim_env=self.testenv) == 0
72 def test_run_legacy_default_osm2pgsql_binary(self, monkeypatch):
73 fname = self.mk_script("exit($_SERVER['NOMINATIM_OSM2PGSQL_BINARY'] == 'osm2pgsql' ? 0 : 23);")
75 assert exec_utils.run_legacy_script(fname, nominatim_env=self.testenv) == 0
78 def test_run_legacy_override_osm2pgsql_binary(self, monkeypatch):
79 monkeypatch.setenv('NOMINATIM_OSM2PGSQL_BINARY', 'somethingelse')
81 fname = self.mk_script("exit($_SERVER['NOMINATIM_OSM2PGSQL_BINARY'] == 'somethingelse' ? 0 : 23);")
83 assert exec_utils.run_legacy_script(fname, nominatim_env=self.testenv) == 0
86 class TestRunApiScript:
89 @pytest.fixture(autouse=True)
90 def setup_project_dir(tmp_path):
91 webdir = tmp_path / 'website'
93 (webdir / 'test.php').write_text("<?php\necho 'OK\n';")
97 def test_run_api(tmp_path):
98 assert exec_utils.run_api_script('test', tmp_path) == 0
101 def test_run_api_execution_error(tmp_path):
102 assert exec_utils.run_api_script('badname', tmp_path) != 0
105 def test_run_api_with_extra_env(tmp_path):
106 extra_env = dict(SCRIPT_FILENAME=str(tmp_path / 'website' / 'test.php'))
107 assert exec_utils.run_api_script('badname', tmp_path, extra_env=extra_env) == 0
110 def test_custom_phpcgi(tmp_path, capfd):
111 assert exec_utils.run_api_script('test', tmp_path, phpcgi_bin='env',
112 params={'q' : 'Berlin'}) == 0
113 captured = capfd.readouterr()
115 assert '?q=Berlin' in captured.out
118 def test_fail_on_error_output(tmp_path):
119 (tmp_path / 'website' / 'bad.php').write_text("<?php\nfwrite(STDERR, 'WARNING'.PHP_EOL);")
121 assert exec_utils.run_api_script('bad', tmp_path) == 1
125 def test_run_osm2pgsql(osm2pgsql_options):
126 osm2pgsql_options['append'] = False
127 osm2pgsql_options['import_file'] = 'foo.bar'
128 osm2pgsql_options['tablespaces']['slim_data'] = 'extra'
129 exec_utils.run_osm2pgsql(osm2pgsql_options)
132 def test_run_osm2pgsql_disable_jit(osm2pgsql_options):
133 osm2pgsql_options['append'] = True
134 osm2pgsql_options['import_file'] = 'foo.bar'
135 osm2pgsql_options['disable_jit'] = True
136 exec_utils.run_osm2pgsql(osm2pgsql_options)