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 class TestRunApiScript:
 
  75     @pytest.fixture(autouse=True)
 
  76     def setup_project_dir(tmp_path):
 
  77         webdir = tmp_path / 'website'
 
  79         (webdir / 'test.php').write_text("<?php\necho 'OK\n';")
 
  83     def test_run_api(tmp_path):
 
  84         assert exec_utils.run_api_script('test', tmp_path) == 0
 
  87     def test_run_api_execution_error(tmp_path):
 
  88         assert exec_utils.run_api_script('badname', tmp_path) != 0
 
  91     def test_run_api_with_extra_env(tmp_path):
 
  92         extra_env = dict(SCRIPT_FILENAME=str(tmp_path / 'website' / 'test.php'))
 
  93         assert exec_utils.run_api_script('badname', tmp_path, extra_env=extra_env) == 0
 
  98 def test_run_osm2pgsql(osm2pgsql_options):
 
  99     osm2pgsql_options['append'] = False
 
 100     osm2pgsql_options['import_file'] = 'foo.bar'
 
 101     osm2pgsql_options['tablespaces']['osm_data'] = 'extra'
 
 102     exec_utils.run_osm2pgsql(osm2pgsql_options)
 
 105 def test_run_osm2pgsql_disable_jit(osm2pgsql_options):
 
 106     osm2pgsql_options['append'] = True
 
 107     osm2pgsql_options['import_file'] = 'foo.bar'
 
 108     osm2pgsql_options['disable_jit'] = True
 
 109     exec_utils.run_osm2pgsql(osm2pgsql_options)