Projet

Général

Profil

0001-tests-move-run_wcs_script-to-utils-module-30752.patch

Emmanuel Cazenave, 22 février 2019 15:41

Télécharger (4,32 ko)

Voir les différences:

Subject: [PATCH 1/2] tests: move run_wcs_script to utils module (#30752)

 tests/conftest.py | 53 ++++++++++++++++++++---------------------------
 tests/utils.py    | 17 +++++++++++++++
 2 files changed, 39 insertions(+), 31 deletions(-)
 create mode 100644 tests/utils.py
tests/conftest.py
11 11
from collections import namedtuple
12 12

  
13 13
import psycopg2
14

  
15 14
import pytest
16 15

  
16
import utils
17

  
18

  
17 19
Wcs = namedtuple('Wcs', ['url', 'appdir', 'pid'])
18 20

  
19 21

  
......
123 125
}
124 126

  
125 127

  
126
@pytest.fixture(scope='session')
127
def wcs(tmp_path_factory):
128
@pytest.fixture
129
def wcs_dir(tmp_path_factory):
130
    return tmp_path_factory.mktemp('wcs')
131

  
132

  
133
@pytest.fixture
134
def wcs(tmp_path_factory, wcs_dir):
128 135
    '''Session scoped wcs fixture, so read-only.'''
129
    if 'WCSCTL' not in os.environ or not os.path.exists(os.environ['WCSCTL']):
130
        pytest.skip('WCSCTL not defined in environment')
131
    WCSCTL = os.environ.get('WCSCTL')
132
    WCS_DIR = tmp_path_factory.mktemp('wcs')
133
    HOSTNAME = '127.0.0.1'
134 136
    PORT = 8899
135 137
    ADDRESS = '0.0.0.0'
136 138
    WCS_PID = None
137 139

  
138
    def run_wcs_script(script, hostname):
139
        '''Run python script inside w.c.s. environment'''
140

  
141
        script_path = WCS_DIR / (script + '.py')
142
        with script_path.open('w') as fd:
143
            fd.write(WCS_SCRIPTS[script])
144

  
145
        subprocess.check_call(
146
            [WCSCTL, 'runscript', '--app-dir', str(WCS_DIR), '--vhost', hostname,
147
             str(script_path)])
148

  
149
    tenant_dir = WCS_DIR / HOSTNAME
140
    tenant_dir = wcs_dir / utils.HOSTNAME
150 141
    tenant_dir.mkdir()
151 142

  
152
    run_wcs_script('setup-auth', HOSTNAME)
153
    run_wcs_script('create-user', HOSTNAME)
154
    run_wcs_script('create-data', HOSTNAME)
143
    utils.run_wcs_script(wcs_dir, WCS_SCRIPTS['setup-auth'], 'setup-auth')
144
    utils.run_wcs_script(wcs_dir, WCS_SCRIPTS['create-user'], 'create-user')
145
    utils.run_wcs_script(wcs_dir, WCS_SCRIPTS['create-data'], 'create-data')
155 146

  
156 147
    with (tenant_dir / 'site-options.cfg').open('w') as fd:
157 148
        fd.write(u'''[api-secrets]
158 149
olap = olap
159 150
''')
160 151

  
161
    with (WCS_DIR / 'wcs.cfg').open('w') as fd:
152
    with (wcs_dir / 'wcs.cfg').open('w') as fd:
162 153
        fd.write(u'''[main]
163
app_dir = %s\n''' % WCS_DIR)
154
app_dir = %s\n''' % wcs_dir)
164 155

  
165
    with (WCS_DIR / 'local_settings.py').open('w') as fd:
156
    with (wcs_dir / 'local_settings.py').open('w') as fd:
166 157
        fd.write(u'''
167 158
WCS_LEGACY_CONFIG_FILE = '%s/wcs.cfg'
168 159
THEMES_DIRECTORY = '/'
169 160
ALLOWED_HOSTS = ['%s']
170
''' % (WCS_DIR, HOSTNAME))
161
''' % (wcs_dir, utils.HOSTNAME))
171 162

  
172 163
    # launch a Django worker for running w.c.s.
173 164
    WCS_PID = os.fork()
174 165
    if not WCS_PID:
175
        os.chdir(os.path.dirname(WCSCTL))
166
        os.chdir(os.path.dirname(utils.WCSCTL))
176 167
        os.environ['DJANGO_SETTINGS_MODULE'] = 'wcs.settings'
177
        os.environ['WCS_SETTINGS_FILE'] = str(WCS_DIR / 'local_settings.py')
168
        os.environ['WCS_SETTINGS_FILE'] = str(wcs_dir / 'local_settings.py')
178 169
        os.execvp('python', ['python', 'manage.py', 'runserver', '--noreload', '%s:%s' % (ADDRESS, PORT)])
179 170
        sys.exit(0)
180 171

  
......
197 188
    if pid:
198 189
        assert False, 'w.c.s. stopped with exit-code %s' % exit_code
199 190

  
200
    yield Wcs(url='http://%s:%s/' % (HOSTNAME, PORT), appdir=WCS_DIR, pid=WCS_PID)
191
    yield Wcs(url='http://%s:%s/' % (utils.HOSTNAME, PORT), appdir=wcs_dir, pid=WCS_PID)
201 192
    os.kill(WCS_PID, 9)
202
    shutil.rmtree(str(WCS_DIR))
193
    shutil.rmtree(str(wcs_dir))
203 194

  
204 195

  
205 196
@pytest.fixture
tests/utils.py
1
import os
2
import subprocess
3

  
4

  
5
HOSTNAME = '127.0.0.1'
6
WCSCTL = os.environ.get('WCSCTL')
7

  
8

  
9
def run_wcs_script(wcs_dir, script, script_name):
10
    '''Run python script inside w.c.s. environment'''
11
    script_path = wcs_dir / (script_name + '.py')
12
    with script_path.open('w') as fd:
13
        fd.write(script)
14

  
15
    subprocess.check_call(
16
        [WCSCTL, 'runscript', '--app-dir', str(wcs_dir), '--vhost', HOSTNAME,
17
         str(script_path)])
0
-