test_davinci_script.py 4.16 KB
Newer Older
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
##############################################################################
# (c) Copyright 2021 CERN for the benefit of the LHCb Collaboration           #
#                                                                             #
# This software is distributed under the terms of the GNU General Public      #
# Licence version 3 (GPL Version 3), copied verbatim in the file "COPYING".   #
#                                                                             #
# In applying this licence, CERN does not waive the privileges and immunities #
# granted to it by virtue of its status as an Intergovernmental Organization  #
# or submit itself to any jurisdiction.                                       #
###############################################################################

import subprocess, os
import pytest


def test_default():
    """
    Check that the simple `davinci` command works.
    """
    result = subprocess.run("davinci", shell=True)
    assert result.returncode == 0


def test_invalid_option():
    """
    Check that an unknown `davinci` option results in an exit code error.
    """
    with pytest.raises(subprocess.CalledProcessError):
        _ = subprocess.run("davinci --unknown", shell=True, check=True)


def test_override_job_option():
    """
    Verify the overriding of a DaVinci option on the fly.
    No need to actually run the job, hence the dry run.
    """
37
    cmd = 'davinci --export test_override_job_option.opts --dry-run run-mc --override_data_options --process "Turbo" --inputfiledb Upgrade_Bd2KstarMuMu -'
38
39
40
41
42
43
44
    result = subprocess.run(cmd, shell=True)

    # Just be maniac - the command should work ;-)
    assert result.returncode == 0

    # Inspect the produced .opts file and check for the option value overridden
    with open("test_override_job_option.opts") as f:
45
        assert any('.process = "Turbo";' in line for line in f.readlines())
46
47

    os.remove("test_override_job_option.opts")
48
49
50
51
52
53
54
55
56


def test_override_job_option_bis():
    """
    Verify the overriding of a DaVinci option on the fly,
    no matter the order of the --override_data_options option
    (see related test "test_override_job_option").
    No need to actually run the job, hence the dry run.
    """
57
    cmd = 'davinci --export test_override_job_option_bis.opts --dry-run run-mc  --process "Turbo" --override_data_options --inputfiledb Upgrade_Bd2KstarMuMu -'
58
59
60
61
62
63
64
    result = subprocess.run(cmd, shell=True)

    # Just be maniac - the command should work ;-)
    assert result.returncode == 0

    # Inspect the produced .opts file and check for the option value overridden
    with open("test_override_job_option_bis.opts") as f:
65
        assert any('.process = "Turbo";' in line for line in f.readlines())
66
67

    os.remove("test_override_job_option_bis.opts")
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87


def test_run_option_shortcut():
    """
    Verify that option values passed to davinci command are accepted also 
    when not using the full name (e.g. --inputfiledb ==> -i).
    """
    cmd = 'davinci --dry-run -o test_run_option_shortcut.opts run-mc -i Upgrade_Bd2KstarMuMu -'
    result = subprocess.run(cmd, shell=True)

    # Just be maniac - the command should work ;-)
    assert result.returncode == 0

    # Inspect the .opts file and check if the input files option is filled correctly.
    with open("test_run_option_shortcut.opts") as f:
        assert not any('.input_files = [];' in line for line in f.readlines())

    os.remove("test_run_option_shortcut.opts")


Davide Fazzini's avatar
Davide Fazzini committed
88
def test_create_options_templates():
89
    """
Davide Fazzini's avatar
Davide Fazzini committed
90
    Verify that the 'create_options_templates' command produces a template for the inputfiledb and joboptfileoption files.
91
    """
Davide Fazzini's avatar
Davide Fazzini committed
92
    cmd = 'davinci create-options-templates -f test_inputdb_template.yaml test_jobopt_template.yaml'
93
94
95
96
97
    result = subprocess.run(cmd, shell=True)

    # Just be maniac - the command should work ;-)
    assert result.returncode == 0

Davide Fazzini's avatar
Davide Fazzini committed
98
    import yaml
Gitlab CI's avatar
Gitlab CI committed
99
100
    with open(
            os.path.expandvars("test_inputdb_template.yaml")) as inputdb_file:
Davide Fazzini's avatar
Davide Fazzini committed
101
        assert yaml.safe_load(inputdb_file)
Gitlab CI's avatar
Gitlab CI committed
102

Davide Fazzini's avatar
Davide Fazzini committed
103
104
    with open(os.path.expandvars("test_inputdb_template.yaml")) as jobopt_file:
        assert yaml.safe_load(jobopt_file)
105
106

    os.remove('test_inputdb_template.yaml')
Davide Fazzini's avatar
Davide Fazzini committed
107
    os.remove('test_jobopt_template.yaml')