aboutsummaryrefslogtreecommitdiff
path: root/kci_test
blob: e10f07c0ee17f27cccb3a3125b50a820bb919e30 (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
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
#!/usr/bin/env python
#
# 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 argparse
import glob
import json
import os
import sys

from kernelci.cli import Args, Command, parse_args
import kernelci.config.lab
import kernelci.config.test
import kernelci.build
import kernelci.lab
import kernelci.test


# -----------------------------------------------------------------------------
# Commands
#

class cmd_list_jobs(Command):
    help = "List all the jobs that need to be run for a given build and lab"
    args = [Args.bmeta_json, Args.dtbs_json, Args.lab]
    opt_args = [Args.user, Args.token, Args.lab_json]

    def __call__(self, test_configs, lab_configs, args):
        bmeta, dtbs = kernelci.build.load_json(args.bmeta_json, args.dtbs_json)

        if bmeta['status'] != "PASS":
            return True

        lab = lab_configs['labs'][args.lab]
        api = kernelci.lab.get_api(lab, args.user, args.token, args.lab_json)

        configs = kernelci.test.match_configs(
            test_configs['test_configs'], bmeta, dtbs, lab)
        for device_type, plan in configs:
            if not api.device_type_online(device_type):
                continue
            print(' '.join([device_type.name, plan.name]))

        return True


class cmd_list_plans(Command):
    help = "List all the existing test plan names"

    def __call__(self, test_configs, lab_configs, args):
        plans = set(plan.name for plan in test_configs['test_plans'].values())
        for plan in sorted(plans):
            print(plan)
        return True


class cmd_list_labs(Command):
    help = "List all the existing lab names"

    def __call__(self, test_configs, lab_configs, args):
        for lab in sorted(lab_configs['labs'].keys()):
            print(lab)
        return True


class cmd_get_lab_info(Command):
    help = "Get the information about a lab into a JSON file"
    args = [Args.lab, Args.lab_json]
    opt_args = [Args.user, Args.token]

    def __call__(self, test_configs, lab_configs, args):
        lab = lab_configs['labs'][args.lab]
        lab_api = kernelci.lab.get_api(lab, args.user, args.token)
        data = {
            'lab': lab.name,
            'lab_type': lab.lab_type,
            'url': lab.url,
            'devices': lab_api.devices,
        }
        with open(args.lab_json, 'w') as json_file:
            json.dump(data, json_file, indent=4, sort_keys=True)
        return True


class cmd_generate(Command):
    help = "Generate the job definition for a given build"
    args = [Args.bmeta_json, Args.dtbs_json, Args.storage, Args.lab]
    opt_args = [Args.plan, Args.target, Args.output,
                Args.lab_json, Args.user, Args.token,
                Args.callback_id, Args.callback_dataset,
                Args.callback_type, Args.callback_url]

    def __call__(self, test_configs, lab_configs, args):
        if args.callback_id and not args.callback_url:
            print("--callback-url is required with --callback-id")
            return False

        bmeta, dtbs = kernelci.build.load_json(args.bmeta_json, args.dtbs_json)

        if bmeta['status'] != "PASS":
            return True

        lab = lab_configs['labs'][args.lab]
        api = kernelci.lab.get_api(lab, args.user, args.token, args.lab_json)

        if args.target and args.plan:
            target = test_configs['device_types'][args.target]
            plan = test_configs['test_plans'][args.plan]
            jobs_list = [(target, plan)]
        else:
            jobs_list = []
            configs = kernelci.test.match_configs(
                test_configs['test_configs'], bmeta, dtbs, lab)
            for device_type, plan in configs:
                if not api.device_type_online(device_type):
                    continue
                if args.target and device_type.name != args.target:
                    continue
                if args.plan and plan.name != args.plan:
                    continue
                jobs_list.append((device_type, plan))

        # ToDo: deal with a JSON file containing a list of builds to iterate
        # over, such as the one produced by "kci_build publish" or saved as a
        # result of a new command "kci_build get_meta" to download meta-data
        # from the backend API.
        callback_opts = {
            'id': args.callback_id,
            'dataset': args.callback_dataset,
            'type': args.callback_type,
            'url': args.callback_url,
        }
        if args.output and not os.path.exists(args.output):
            os.makedirs(args.output)
        for target, plan in jobs_list:
            params = kernelci.test.get_params(
                bmeta, target, plan, args.storage)
            job = api.generate(params, target, plan, callback_opts)
            if args.output:
                file_name = api.job_file_name(params)
                output_file = os.path.join(args.output, file_name)
                print(output_file)
                with open(output_file, 'wb') as output:
                    output.write(job)
            else:
                print("# Job: {}".format(params['name']))
                print(job)
        return True


class cmd_submit(Command):
    help = "Submit job definitions to a lab"
    args = [Args.lab, Args.user, Args.token, Args.jobs]

    def __call__(self, test_configs, lab_configs, args):
        lab = lab_configs['labs'][args.lab]
        lab_api = kernelci.lab.get_api(lab, args.user, args.token)
        job_paths = glob.glob(args.jobs)
        res = True
        for path in job_paths:
            if not os.path.isfile(path):
                continue
            with open(path, 'rb') as job_file:
                job = job_file.read()
                try:
                    job_id = lab_api.submit(job)
                    print("{} {}".format(job_id, path))
                except Exception as e:
                    print("ERROR {}: {}".format(path, e))
                    res = False
        return res


# -----------------------------------------------------------------------------
# Main
#

if __name__ == '__main__':
    parser = argparse.ArgumentParser("kci_test")
    parser.add_argument("--yaml-test-configs", default="test-configs.yaml",
                        help="Path to the YAML test configs file")
    parser.add_argument("--yaml-lab-configs", default="lab-configs.yaml",
                        help="Path to the YAML lab configs file")
    kernelci.cli.add_subparser(parser, globals())
    args = parser.parse_args()
    test_configs = kernelci.config.test.from_yaml(args.yaml_test_configs)
    lab_configs = kernelci.config.lab.from_yaml(args.yaml_lab_configs)
    status = args.func(test_configs, lab_configs, args)
    sys.exit(0 if status is True else 1)