|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# --- BEGIN_HEADER --- |
| 4 | +# |
| 5 | +# Copyright (C) 2003-2024 The MiG Project by the Science HPC Center at UCPH |
| 6 | +# |
| 7 | +# This file is part of MiG. |
| 8 | +# |
| 9 | +# MiG is free software: you can redistribute it and/or modify |
| 10 | +# it under the terms of the GNU General Public License as published by |
| 11 | +# the Free Software Foundation; either version 2 of the License, or |
| 12 | +# (at your option) any later version. |
| 13 | +# |
| 14 | +# MiG is distributed in the hope that it will be useful, |
| 15 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | +# GNU General Public License for more details. |
| 18 | +# |
| 19 | +# You should have received a copy of the GNU General Public License |
| 20 | +# along with this program; if not, write to the Free Software |
| 21 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
| 22 | +# USA. |
| 23 | +# |
| 24 | +# --- END_HEADER --- |
| 25 | +# |
| 26 | + |
| 27 | +"""Unit tests for the MiG WSGI glue""" |
| 28 | + |
| 29 | +from configparser import ConfigParser |
| 30 | +import importlib |
| 31 | +import os |
| 32 | +import stat |
| 33 | +import sys |
| 34 | + |
| 35 | +from tests.support import MIG_BASE, MigTestCase, testmain |
| 36 | + |
| 37 | + |
| 38 | +def create_output_returner(arranged=None): |
| 39 | + def test_format_output(*args): |
| 40 | + return arranged |
| 41 | + return test_format_output |
| 42 | + |
| 43 | +def create_wsgi_environ(config_file, env_http_host=None, env_path_info=None): |
| 44 | + environ = {} |
| 45 | + environ['MIG_CONF'] = config_file |
| 46 | + environ['HTTP_HOST'] = env_http_host |
| 47 | + environ['PATH_INFO'] = env_path_info |
| 48 | + environ['SCRIPT_URI'] = ''.join(('http://', environ['HTTP_HOST'], environ['PATH_INFO'])) |
| 49 | + return environ |
| 50 | + |
| 51 | + |
| 52 | +def noop(*args): |
| 53 | + pass |
| 54 | + |
| 55 | + |
| 56 | +from tests.support import PY2, is_path_within |
| 57 | +from mig.shared.base import client_id_dir, client_dir_id, get_short_id, \ |
| 58 | + invisible_path, allow_script, brief_list |
| 59 | + |
| 60 | + |
| 61 | +_LOCAL_MIG_BASE = '/usr/src/app' if PY2 else MIG_BASE # account for execution in container |
| 62 | +_PYTHON_MAJOR = '2' if PY2 else '3' |
| 63 | +_TEST_CONF_DIR = os.path.join(MIG_BASE, "envhelp/output/testconfs-py%s" % (_PYTHON_MAJOR,)) |
| 64 | +_TEST_CONF_FILE = os.path.join(_TEST_CONF_DIR, "MiGserver.conf") |
| 65 | +_TEST_CONF_SYMLINK = os.path.join(MIG_BASE, "envhelp/output/testconfs") |
| 66 | + |
| 67 | + |
| 68 | +def _assert_local_config(): |
| 69 | + try: |
| 70 | + link_stat = os.lstat(_TEST_CONF_SYMLINK) |
| 71 | + assert stat.S_ISLNK(link_stat.st_mode) |
| 72 | + configdir_stat = os.stat(_TEST_CONF_DIR) |
| 73 | + assert stat.S_ISDIR(configdir_stat.st_mode) |
| 74 | + config = ConfigParser() |
| 75 | + config.read([_TEST_CONF_FILE]) |
| 76 | + return config |
| 77 | + except Exception as exc: |
| 78 | + raise AssertionError('local configuration invalid or missing: %s' % (str(exc),)) |
| 79 | + |
| 80 | + |
| 81 | +def _assert_local_config_global_values(config): |
| 82 | + config_global_values = dict(config.items('GLOBAL')) |
| 83 | + |
| 84 | + for path in ('mig_path', 'certs_path', 'state_path'): |
| 85 | + path_value = config_global_values.get(path) |
| 86 | + if not is_path_within(path_value, start=_LOCAL_MIG_BASE): |
| 87 | + raise AssertionError('local config contains bad path: %s=%s' % (path, path_value)) |
| 88 | + |
| 89 | + return config_global_values |
| 90 | + |
| 91 | + |
| 92 | +_WSGI_BIN = os.path.join(MIG_BASE, 'mig/wsgi-bin') |
| 93 | + |
| 94 | +def _import_migwsgi(): |
| 95 | + sys.path.append(_WSGI_BIN) |
| 96 | + migwsgi = importlib.import_module('migwsgi') |
| 97 | + sys.path.pop(-1) |
| 98 | + return migwsgi |
| 99 | + |
| 100 | + |
| 101 | +migwsgi = _import_migwsgi() |
| 102 | + |
| 103 | + |
| 104 | +class MigSharedConfiguration(MigTestCase): |
| 105 | + def test_xxx(self): |
| 106 | + config = _assert_local_config() |
| 107 | + config_global_values = _assert_local_config_global_values(config) |
| 108 | + |
| 109 | + def fake_start_response(status, headers, exc=None): |
| 110 | + fake_start_response.calls.append((status, headers, exc)) |
| 111 | + fake_start_response.calls = [] |
| 112 | + |
| 113 | + def fake_set_environ(value): |
| 114 | + fake_set_environ.value = value |
| 115 | + fake_set_environ.value = None |
| 116 | + |
| 117 | + wsgi_environ = create_wsgi_environ( |
| 118 | + _TEST_CONF_FILE, |
| 119 | + env_http_host='localhost', |
| 120 | + env_path_info='/' |
| 121 | + ) |
| 122 | + |
| 123 | + test_output_returner = create_output_returner(b'') |
| 124 | + |
| 125 | + yielder = migwsgi._application(wsgi_environ, fake_start_response, |
| 126 | + _format_output=test_output_returner, |
| 127 | + _set_environ=fake_set_environ, |
| 128 | + _wrap_wsgi_errors=noop, |
| 129 | + _config_file=_TEST_CONF_FILE, |
| 130 | + _skip_log=True, |
| 131 | + ) |
| 132 | + chunks = list(yielder) |
| 133 | + |
| 134 | + self.assertGreater(len(chunks), 0) |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | +if __name__ == '__main__': |
| 139 | + testmain() |
0 commit comments