]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tools/test_exec_utils.py
custom country config loads correctly
[nominatim.git] / test / python / tools / test_exec_utils.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Tests for tools.exec_utils module.
9 """
10 from pathlib import Path
11 import subprocess
12
13 import pytest
14
15 import nominatim.tools.exec_utils as exec_utils
16
17 class TestRunLegacyScript:
18
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()
24
25         class _NominatimEnv:
26             config = def_config
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')
32             module_dir = 'module'
33             osm2pgsql_path = 'osm2pgsql'
34
35         self.testenv = _NominatimEnv
36
37
38     def mk_script(self, code):
39         codefile = self.testenv.phplib_dir / 'admin' / 't.php'
40         codefile.write_text('<?php\n' + code + '\n')
41
42         return 't.php'
43
44
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)
50
51
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,
56                                          throw_on_fail=True)
57
58
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
63
64     def test_run_legacy_use_given_module_path(self):
65         fname = self.mk_script("exit($_SERVER['NOMINATIM_DATABASE_MODULE_PATH'] == '' ? 0 : 23);")
66
67         assert exec_utils.run_legacy_script(fname, nominatim_env=self.testenv) == 0
68
69
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);")
74
75         assert exec_utils.run_legacy_script(fname, nominatim_env=self.testenv) == 0
76
77
78     def test_run_legacy_default_osm2pgsql_binary(self, monkeypatch):
79         fname = self.mk_script("exit($_SERVER['NOMINATIM_OSM2PGSQL_BINARY'] == 'osm2pgsql' ? 0 : 23);")
80
81         assert exec_utils.run_legacy_script(fname, nominatim_env=self.testenv) == 0
82
83
84     def test_run_legacy_override_osm2pgsql_binary(self, monkeypatch):
85         monkeypatch.setenv('NOMINATIM_OSM2PGSQL_BINARY', 'somethingelse')
86
87         fname = self.mk_script("exit($_SERVER['NOMINATIM_OSM2PGSQL_BINARY'] == 'somethingelse' ? 0 : 23);")
88
89         assert exec_utils.run_legacy_script(fname, nominatim_env=self.testenv) == 0
90
91
92 class TestRunApiScript:
93
94     @staticmethod
95     @pytest.fixture(autouse=True)
96     def setup_project_dir(tmp_path):
97         webdir = tmp_path / 'website'
98         webdir.mkdir()
99         (webdir / 'test.php').write_text("<?php\necho 'OK\n';")
100
101
102     @staticmethod
103     def test_run_api(tmp_path):
104         assert exec_utils.run_api_script('test', tmp_path) == 0
105
106     @staticmethod
107     def test_run_api_execution_error(tmp_path):
108         assert exec_utils.run_api_script('badname', tmp_path) != 0
109
110     @staticmethod
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
114
115     @staticmethod
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()
120
121         assert '?q=Berlin' in captured.out
122
123     @staticmethod
124     def test_fail_on_error_output(tmp_path):
125         (tmp_path / 'website' / 'bad.php').write_text("<?php\nfwrite(STDERR, 'WARNING'.PHP_EOL);")
126
127         assert exec_utils.run_api_script('bad', tmp_path) == 1
128
129 ### run_osm2pgsql
130
131 def test_run_osm2pgsql(osm2pgsql_options):
132     osm2pgsql_options['append'] = False
133     osm2pgsql_options['import_file'] = 'foo.bar'
134     osm2pgsql_options['tablespaces']['slim_data'] = 'extra'
135     exec_utils.run_osm2pgsql(osm2pgsql_options)
136
137
138 def test_run_osm2pgsql_disable_jit(osm2pgsql_options):
139     osm2pgsql_options['append'] = True
140     osm2pgsql_options['import_file'] = 'foo.bar'
141     osm2pgsql_options['disable_jit'] = True
142     exec_utils.run_osm2pgsql(osm2pgsql_options)