"""Test template form module.""" import re from pathlib import Path from glicid_spawner import form from glicid_spawner.form import options_attrs, options_form, options_from_form from glicid_spawner.micromamba import MicromambaEnv from glicid_spawner.slurm import sinfo_from_file from pytest import fixture DATA = Path(__file__).parent / 'data' SINFO = sinfo_from_file(DATA / 'sinfo.txt') SLURM_SINGLE_CLUSTER = {'N/A': SINFO.pop('N/A')} SLURM_MULTI_CLUSTER = SINFO @fixture def mock_python_envs(monkeypatch): """Mock python environments list.""" monkeypatch.setattr( form, 'get_envs', lambda username: [ MicromambaEnv('USER', 'foo', f'/{username}/envs/foo'), MicromambaEnv('USER', 'bar', f'/{username}/envs/bar'), MicromambaEnv('GLOBAL', 'baz', '/global/envs/baz'), ], ) @fixture def mock_cluster(monkeypatch, mock_python_envs): """Mock multi cluster configuration (default).""" monkeypatch.setattr(form, 'sinfo', lambda _: SLURM_MULTI_CLUSTER) @fixture def mock_single_cluster(monkeypatch, mock_python_envs): """Mock multi cluster configuration.""" monkeypatch.setattr(form, 'sinfo', lambda _: SLURM_SINGLE_CLUSTER) def test_options_attrs(mock_cluster): """Test form options attributes.""" options = options_attrs('john-doe') assert options['username'] == 'john-doe' assert options['folders'] == [ '/home/john-doe', '/scratch/nautilus/users/john-doe', '/scratch/waves/users/john-doe', '/scratch/nautilus/projects', '/scratch/waves/projects', '/LAB-DATA/', ] assert [env.path for env in options['envs']] == [ '/john-doe/envs/foo', '/john-doe/envs/bar', '/global/envs/baz', ] cpu = options['cpus'] assert cpu[1] == 24 assert cpu[24] == 1 mem = options['mems'] assert mem[4] == 24 assert mem[96] == 1 gpu = options['gpus'] assert gpu['None'] == 24 assert gpu['A100'] == 1 # Multi cluster configuration (default) sinfo = options['sinfo'] assert 'N/A' not in sinfo assert 'nautilus' in sinfo assert 'waves' in sinfo node = sinfo['nautilus']['gpu']['gnode1'] assert node == 'gnode1' assert node.cpu.idle == 92 assert node.gpu.name == 'A100' def test_options_attrs_single_cluster(mock_single_cluster): """Test form options attributes in single cluster configuration.""" options = options_attrs('john-doe') # Single cluster configuration sinfo = options['sinfo'] assert 'N/A' in sinfo assert 'nautilus' not in sinfo assert 'waves' not in sinfo node = sinfo['N/A']['Devel']['nazare001'] assert node == 'nazare001' assert node.cpu.idle == 20 assert node.gpu.name == 'None' def test_options_form_resources(mock_cluster): """Test options form render.""" html = re.sub(r'\n\s+', ' ', options_form('john-doe')) # trim line breaks # Username assert '