test_routines.py 14.8 KB
Newer Older
Paul McCarthy's avatar
Paul McCarthy committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#!/usr/bin/env python

import os
import os.path as op
import shutil
import subprocess as sp
import sys
import textwrap as tw

# py3
try:
    from unittest import mock
# py2
except ImportError:
    import mock

import pytest

from . import onpath, server

import fslinstaller as inst


def test_Version():
    assert inst.Version('1')       == inst.Version('1')
    assert inst.Version('1.2')     == inst.Version('1.2')
    assert inst.Version('1.2.3')   == inst.Version('1.2.3')
    assert inst.Version('1.2.3')   <  inst.Version('1.2.4')
    assert inst.Version('1.2.3')   >  inst.Version('1.2.2')
    assert inst.Version('1.2.3')   >  inst.Version('1.2')
    assert inst.Version('1.2.3')   <  inst.Version('1.2.3.0')
    assert inst.Version('1.2.3.0') >  inst.Version('1.2.3')


def test_Context_identify_plaform():
    tests = [
        [('linux',  'x86_64'), 'linux-64'],
        [('darwin', 'x86_64'), 'macos-64'],
        [('darwin', 'arm64'),  'macos-64'],
    ]

    for info, expected in tests:
        sys, cpu = info
        with mock.patch('platform.system', return_value=sys), \
             mock.patch('platform.machine', return_value=cpu):
            assert inst.Context.identify_platform() == expected


def test_Context_identify_cuda():
    with inst.tempdir() as cwd:
        with onpath(cwd):

            nvidia_smi = tw.dedent("""
            #!/usr/bin/env bash
            echo "{stdout}"
            exit {retcode}
            """).strip()

            # test when nvidia-smi doesn't exist
            # (assuming that it won't be present
            # in any of the mock paths)
            path = op.pathsep.join(('/usr/sbin', '/usr/bin', '/sbin', '/bin'))
            with mock.patch.dict(os.environ, PATH=path):
                assert inst.Context.identify_cuda() is None
                if hasattr(inst.Context.identify_cuda, 'no_cuda'):
                    delattr(inst.Context.identify_cuda, 'no_cuda')

            with open('nvidia-smi', 'wt') as f:
                f.write(nvidia_smi.format(stdout='CUDA Version: 10.1', retcode=0))
            os.chmod('nvidia-smi', 0o755)
            assert  inst.Context.identify_cuda() == 10.1
            if hasattr(inst.Context.identify_cuda, 'no_cuda'):
                delattr(inst.Context.identify_cuda, 'no_cuda')

            with open('nvidia-smi', 'wt') as f:
                f.write(nvidia_smi.format(stdout='CUDA Version: 11.2', retcode=0))
            os.chmod('nvidia-smi', 0o755)
            assert  inst.Context.identify_cuda() == 11.2
            if hasattr(inst.Context.identify_cuda, 'no_cuda'):
                delattr(inst.Context.identify_cuda, 'no_cuda')

            with open('nvidia-smi', 'wt') as f:
                f.write(nvidia_smi.format(stdout='CUDA Version: 11.2', retcode=1))
            os.chmod('nvidia-smi', 0o755)
            assert  inst.Context.identify_cuda() is None
            if hasattr(inst.Context.identify_cuda, 'no_cuda'):
                delattr(inst.Context.identify_cuda, 'no_cuda')


def test_Process_check_call():
    with inst.tempdir() as cwd:
        script_template = tw.dedent("""
        #!/usr/bin/env sh
        touch {semaphore}
        exit {retcode}
        """).strip()

        with open('pass', 'wt') as f:
            f.write(script_template.format(semaphore='passed', retcode=0))
        with open('fail', 'wt') as f:
            f.write(script_template.format(semaphore='failed', retcode=1))
        os.chmod('pass', 0o755)
        os.chmod('fail', 0o755)

        inst.Process.check_call(op.join(cwd, 'pass'))
        assert op.exists('passed')

        with pytest.raises(Exception):
            inst.Process.check_call(op.join(cwd, 'fail'))
        assert op.exists('failed')


def test_Process_check_output():
    with inst.tempdir() as cwd:
        script_template = tw.dedent("""
        #!/usr/bin/env sh
        echo "{stdout}"
        exit {retcode}
        """).strip()

        # (stdout, retcode)
        tests = [
            ('stdout', 0),
            ('stdout', 1),
        ]

        for expect, retcode in tests:
            script = script_template.format(stdout=expect, retcode=retcode)

            with open('script', 'wt') as f:
                f.write(script)
            os.chmod('script', 0o755)

            if retcode == 0:
                got = inst.Process.check_output(op.join(cwd, 'script'))
                assert got.strip() == expect

            else:
                with pytest.raises(Exception):
                    inst.Process.check_output(op.join(cwd, 'script'))


def test_read_fslversion():
    with inst.tempdir() as cwd:
        os.mkdir('etc')
        assert inst.read_fslversion(cwd) is None

        with open(op.join('etc', 'fslversion'), 'wt') as f:
            f.write('abcde')
        assert inst.read_fslversion(cwd) == 'abcde'

        with open(op.join('etc', 'fslversion'), 'wt') as f:
            f.write('abcde:fghij')
        assert inst.read_fslversion(cwd) == 'abcde'


def test_download_file():

    with inst.tempdir() as cwd:
        with open('file', 'wt') as f:
            f.write('hello\n')
        with server(cwd) as srv:

            url = 'http://localhost:{}/file'.format(srv.port)

            inst.download_file(url, 'copy')

            with open('copy', 'rt') as f:
                 assert f.read() == 'hello\n'


def test_patch_file():

    content = tw.dedent("""
    line1
    line2
    line3
    line4
    """).strip()

    with inst.tempdir():

        with open('file', 'wt') as f:
            f.write(content)
        inst.patch_file('file', 'line2', 1, 'newline2')
        expect = content.replace('line2', 'newline2')
        with open('file', 'rt') as f:
            assert f.read().strip() == expect

        with open('file', 'wt') as f:
            f.write(content)
        inst.patch_file('file', 'line2', 2, 'newline2')
        expect = content.replace('line2\nline3', 'newline2')
        with open('file', 'rt') as f:
            assert f.read().strip() == expect

        with open('file', 'wt') as f:
            f.write(content)
        inst.patch_file('file', 'noline', 1, 'newline')
        expect = content + '\n\nnewline'
        with open('file', 'rt') as f:
            assert f.read().strip() == expect


def test_configure_shell():

    template = tw.dedent("""
    # FSL Setup
    FSLDIR={}
    PATH=${{FSLDIR}}/share/fsl/bin:${{PATH}}
    export FSLDIR PATH
    . ${{FSLDIR}}/etc/fslconf/fsl.sh
    """).strip()

    with inst.tempdir() as homedir:

        # no profile file exists
        inst.configure_shell('bash', homedir, '/fsl')
        with open('.bash_profile', 'rt') as f:
            assert f.read().strip() == template.format('/fsl')

        # existing profile with FSL config already present
        inst.configure_shell('bash', homedir, '/fsl_new')
        with open('.bash_profile', 'rt') as f:
            assert f.read().strip() == template.format('/fsl_new')

        # existing profile without FSL config
        config = tw.dedent("""
        line1
        line2
        line3
        """).strip()
        with open('.bash_profile', 'wt') as f:
            f.write(config)
        inst.configure_shell('bash', homedir, '/fsl')

        expect = config + '\n\n' + template.format('/fsl')
        with open('.bash_profile', 'rt') as f:
            assert f.read().strip() == expect


def test_configure_matlab():

    template = tw.dedent("""
    % FSL Setup
    setenv( 'FSLDIR', '{}' );
    setenv('FSLOUTPUTTYPE', 'NIFTI_GZ');
    fsldir = getenv('FSLDIR');
    fsldirmpath = sprintf('%s/etc/matlab',fsldir);
    path(path, fsldirmpath);
    clear fsldir fsldirmpath;
    """).strip()

    with inst.tempdir() as homedir:

        os.makedirs(op.join('Documents', 'MATLAB'))
        startupm = op.join('Documents', 'MATLAB', 'startup.m')

        # no startup.m exists
        inst.configure_matlab(homedir, '/fsl')
        with open(startupm, 'rt') as f:
            assert f.read().strip() == template.format('/fsl')

        # existing startup.m with FSL config already present
        inst.configure_matlab(homedir, '/fsl_new')
        with open(startupm, 'rt') as f:
            assert f.read().strip() == template.format('/fsl_new')

        # existing startup.m without FSL config
        config = tw.dedent("""
        line1
        line2
        line3
        """).strip()
        with open(startupm, 'wt') as f:
            f.write(config)
        inst.configure_matlab(homedir, '/fsl')

        expect = config + '\n\n' + template.format('/fsl')
        with open(startupm, 'rt') as f:
            assert f.read().strip() == expect


def test_list_available_versions():
    manifest = {'versions' : {'latest' : '6.1.0',
                              '6.1.0' : [{'platform'    : 'linux-64',
                                          'environment' : 'http://env.yml'},
                                         {'platform'    : 'macos-64',
                                          'environment' : 'http://env.yml'},
                                         {'platform'    : 'linux-64',
                                          'cuda'        : '10.2',
                                          'environment' : 'http://env.yml'}],
                              '6.2.0' : [{'platform'    : 'linux-64',
                                          'environment' : 'http://env.yml'},
                                         {'platform'    : 'macos-64',
                                          'environment' : 'http://env.yml'},
                                         {'platform'    : 'linux-64',
                                          'cuda'        : '10.2',
                                          'environment' : 'http://env.yml'}]}}
    inst.list_available_versions(manifest)


def test_download_install_miniconda():
    class MockObject(object):
        pass

    installer = tw.dedent("""
    #!/usr/bin/env bash
    # is run like <miniconda.sh> -b -p <prefix>
    mkdir -p $3
    touch $3/installed
    """)

    def gen_manifest(platform, port, sha256):
        return {
            'miniconda' : {
                platform : {
                    'url'    : 'http://localhost:{}/remote.sh'.format(port),
                    'sha256' : sha256,
                }
            }
        }

    with inst.tempdir() as cwd:
        os.mkdir('remote')
        with open(op.join('remote', 'remote.sh'), 'wt') as f:
            f.write(installer)
        sha256 = inst.sha256(op.join('remote', 'remote.sh'))

        with server('remote') as srv:

            destdir              = op.join(cwd, 'miniconda')
            ctx                  = MockObject()
            ctx.args             = MockObject()
            ctx.destdir          = destdir
            ctx.need_admin       = False
            ctx.args.no_checksum = False
            ctx.platform         = 'linux'
            ctx.manifest         = gen_manifest('linux', srv.port, sha256)

            inst.download_miniconda(ctx)
            inst.install_miniconda(ctx)

            assert op.exists(destdir)
            assert op.exists(op.join(destdir, 'installed'))
            assert op.exists(op.join(destdir, '.condarc'))

            shutil.rmtree(destdir)

            ctx.manifest = gen_manifest('linux', srv.port, 'bad')
            with pytest.raises(Exception):
                inst.download_miniconda(ctx)

            ctx.args.no_checksum = True
            inst.download_miniconda(ctx)

            with open(op.join('remote', 'remote.sh'), 'wt') as f:
                f.write(installer)
            sha256 = inst.sha256(op.join('remote', 'remote.sh'))


def test_self_update():

    ver    = inst.__version__
    newver = '{}.0.0'.format(int(ver.split('.')[0]) + 1)
    oldver = '{}.0.0'.format(int(ver.split('.')[0]) - 1)

    new_installer = tw.dedent("""
    #!/usr/bin/env python
    from __future__ import print_function
    print("new version")
    """).strip()

    script_template = tw.dedent("""
    #!/usr/bin/env python
    from __future__ import print_function

    manifest = {{
        'installer' : {{
            'version' : '{ver}',
            'url'     : 'http://localhost:{port}/new_installer.py',
            'sha256'  : '{checksum}'
        }}
    }}

    import fslinstaller as inst
    inst.self_update(manifest, '.', {check_checksum})
    print('old version')
    """).strip()

    with inst.tempdir() as cwd:
        with server() as srv:

            shutil.copyfile(inst.__absfile__, 'fslinstaller.py')
            with open('new_installer.py', 'wt') as f:
                f.write(new_installer)
            checksum = inst.sha256('new_installer.py')

            # new version available
            with open('script.py', 'wt') as f:
                f.write(script_template.format(ver=newver,
                                               port=srv.port,
                                               checksum=checksum,
                                               check_checksum=True))
            got = sp.check_output([sys.executable, 'script.py'])
            assert got.decode('utf-8').strip() == 'new version'

            # new version available, bad checksum
            with open('script.py', 'wt') as f:
                f.write(script_template.format(ver=newver,
                                               port=srv.port,
                                               checksum='bad',
                                               check_checksum=True))
            got = sp.check_output([sys.executable, 'script.py'])
            # drop warning messages
            got = got.decode('utf-8').strip().split('\n')[-1]
            assert got == 'old version'

            # new version available, bad checksum, but skip checksum
            with open('script.py', 'wt') as f:
                f.write(script_template.format(ver=newver,
                                               port=srv.port,
                                               checksum='bad',
                                               check_checksum=False))
            got = sp.check_output([sys.executable, 'script.py'])
            got = got.decode('utf-8').strip().split('\n')[-1]
            assert got == 'new version'

            # same version available
            with open('script.py', 'wt') as f:
                f.write(script_template.format(ver=ver,
                                               port=srv.port,
                                               checksum=checksum,
                                               check_checksum=True))
            got = sp.check_output([sys.executable, 'script.py'])
            assert got.decode('utf-8').strip() == 'old version'

            # old version available
            with open('script.py', 'wt') as f:
                f.write(script_template.format(ver=oldver,
                                               port=srv.port,
                                               checksum=checksum,
                                               check_checksum=True))
            got = sp.check_output([sys.executable, 'script.py'])
            assert got.decode('utf-8').strip() == 'old version'