aboutsummaryrefslogtreecommitdiff
path: root/kernelci/config/lab.py
blob: 91c0267ba5d570f43202034183ea2c125a745b7e (plain)
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
# Copyright (C) 2019 Collabora Limited
# Author: Guillaume Tucker <guillaume.tucker@collabora.com>
#
# This module is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 2.1 of the License, or (at your option)
# any later version.
#
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

import yaml

from kernelci.config import FilterFactory, YAMLObject


class Lab(YAMLObject):
    """Test lab model."""

    def __init__(self, name, lab_type, url, filters=None):
        """A lab object contains all the information relative to a test lab.

        *name* is the name used to refer to the lab in meta-data.
        *lab_type* is the name of the type of lab, essentially indicating the
                   type of software used by the lab.
        *url* is the URL to reach the lab API.
        *filters* is a list of Filter objects associated with this lab.
        """
        self._name = name
        self._lab_type = lab_type
        self._url = url
        self._filters = filters or list()

    @classmethod
    def from_yaml(cls, lab, kw):
        return cls(**kw)

    @property
    def name(self):
        return self._name

    @property
    def lab_type(self):
        return self._lab_type

    @property
    def url(self):
        return self._url

    def match(self, data):
        return all(f.match(**data) for f in self._filters)


class Lab_LAVA(Lab):

    def __init__(self, priority='medium', *args, **kwargs):
        super(Lab_LAVA, self).__init__(*args, **kwargs)
        self._priority = priority

    @property
    def priority(self):
        return self._priority

    @classmethod
    def from_yaml(cls, lab, kw):
        priority = lab.get('priority')
        if priority:
            kw['priority'] = priority
        return cls(**kw)


class LabFactory(YAMLObject):
    """Factory to create lab objects from YAML data."""

    _lab_types = {
        'lava': Lab_LAVA,
    }

    @classmethod
    def from_yaml(cls, name, lab):
        lab_type = lab.get('lab_type')
        kw = cls._kw_from_yaml(lab, ['url'])
        kw.update({
            'name': name,
            'lab_type': lab_type,
            'filters': FilterFactory.from_data(lab),
        })
        lab_cls = cls._lab_types[lab_type] if lab_type else Lab
        return lab_cls.from_yaml(lab, kw)


def from_yaml(yaml_path):
    with open(yaml_path) as f:
        data = yaml.load(f)

    labs = {
        name: LabFactory.from_yaml(name, lab)
        for name, lab in data['labs'].iteritems()
    }

    config_data = {
        'labs': labs,
    }

    return config_data