summaryrefslogtreecommitdiffstats
path: root/dovetail/run.py
blob: 8d4b7f8ddff37122c1acc5b16ca933773677a71a (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
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
#!/usr/bin/env python

##############################################################################
# Copyright (c) 2017 grakiss.wanglei@huawei.com and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################


import copy
import os
import time
import uuid

import click

from container import Container
from dovetail import constants
from parser import Parser
from report import BottlenecksChecker, FunctestChecker, YardstickChecker
from report import VnftestChecker
from report import BottlenecksCrawler, FunctestCrawler, YardstickCrawler
from report import VnftestCrawler
from report import Report
from test_runner import DockerRunner, ShellRunner
from testcase import Testcase
from testcase import Testsuite
from utils.dovetail_config import DovetailConfig as dt_cfg
import utils.dovetail_logger as dt_logger
import utils.dovetail_utils as dt_utils

EXIT_RUN_FAILED = 2


def load_testsuite(testsuite):
    Testsuite.load()
    return Testsuite.get(testsuite)


def run_test(testcase_list, logger):
    duration = 0
    if not testcase_list:
        logger.warning("No test case will be executed.")
        return duration

    start_time = time.time()
    for testcase_name in testcase_list:
        logger.info('>>[testcase]: {}'.format(testcase_name))
        testcase = Testcase.get(testcase_name)
        run_testcase = True

        # if testcase.exceed_max_retry_times():
        #    run_testcase = False

        # if testcase.script_result_acquired():
        #    run_testcase = False

        if run_testcase:
            testcase.run()

        stop_on_fail = Report.check_tc_result(testcase)
        try:
            if (not stop_on_fail or stop_on_fail['criteria'] == "FAIL") \
                and dt_cfg.dovetail_config['stop']:
                return "stop_on_fail"
        except KeyError as e:
            logger.error("There is no key {}.".format(e))

    end_time = time.time()
    duration = end_time - start_time
    return duration


def filter_config(input_dict, logger):
    cli_dict = dt_cfg.dovetail_config['cli']
    configs = {}
    for key in cli_dict:
        if not cli_dict[key]:
            continue
        try:
            cli_config = cli_dict[key]['config']
            if cli_config is None:
                continue
        except KeyError:
            continue
        for key, value in input_dict.items():
            for config_key, config_value in cli_config.items():
                value_dict = {}
                value_dict['value'] = value
                try:
                    value_dict['path'] = config_value['path']
                    if key == config_key:
                        configs[key] = value_dict
                        break
                    if key.upper() == config_key:
                        configs[key.upper()] = value_dict
                        break
                except KeyError as e:
                    logger.exception('KeyError {}.'.format(e))
                    raise SystemExit(EXIT_RUN_FAILED)
    if not configs:
        return None
    return configs


def create_logs():
    Container.create_log()
    Parser.create_log()
    Report.create_log()
    FunctestCrawler.create_log()
    YardstickCrawler.create_log()
    VnftestCrawler.create_log()
    BottlenecksCrawler.create_log()
    FunctestChecker.create_log()
    YardstickChecker.create_log()
    VnftestChecker.create_log()
    BottlenecksChecker.create_log()
    Testcase.create_log()
    Testsuite.create_log()
    DockerRunner.create_log()
    ShellRunner.create_log()


def clean_results_dir():
    result_path = dt_cfg.dovetail_config['result_dir']
    if os.path.exists(result_path):
        if os.path.isdir(result_path):
            cmd = 'sudo rm -rf %s/*' % (result_path)
            dt_utils.exec_cmd(cmd, exit_on_error=False, exec_msg_on=False)
        else:
            print("result_dir in dovetail_config.yml is not a directory.")
            raise SystemExit(EXIT_RUN_FAILED)


def get_result_path():
    try:
        dovetail_home = os.environ["DOVETAIL_HOME"]
    except Exception:
        print("ERROR: mandatory env variable 'DOVETAIL_HOME' is not found, "
              "please set in env_config.sh and source this file before "
              "running.")
        return None
    result_path = os.path.join(dovetail_home, 'results')
    dt_cfg.dovetail_config['result_dir'] = result_path
    dt_cfg.dovetail_config['images_dir'] = os.path.join(dovetail_home,
                                                        'images')
    pre_config_path = os.path.join(dovetail_home, 'pre_config')
    patch_set_path = os.path.join(dovetail_home, 'patches')
    dt_cfg.dovetail_config['config_dir'] = pre_config_path
    dt_cfg.dovetail_config['patch_dir'] = patch_set_path
    return dovetail_home


def copy_userconfig_files(logger):
    pre_config_path = dt_cfg.dovetail_config['config_dir']
    if not os.path.isdir(pre_config_path):
        os.makedirs(pre_config_path)
    cmd = 'sudo cp -r %s/* %s' % (constants.USERCONF_PATH, pre_config_path)
    dt_utils.exec_cmd(cmd, logger, exit_on_error=False)


def copy_patch_files(logger):
    patch_set_path = dt_cfg.dovetail_config['patch_dir']
    if not os.path.isdir(patch_set_path):
        os.makedirs(patch_set_path)
    cmd = 'sudo cp -a -r %s/* %s' % (constants.PATCH_PATH, patch_set_path)
    dt_utils.exec_cmd(cmd, logger, exit_on_error=False)


# env_init can source some env variable used in dovetail, such as
# when https+credential used, OS_CACERT
def env_init(logger):
    openrc = os.path.join(dt_cfg.dovetail_config['config_dir'],
                          dt_cfg.dovetail_config['env_file'])
    if not os.path.isfile(openrc):
        logger.error("File {} does not exist.".format(openrc))
    dt_utils.source_env(openrc)


def update_deploy_scenario(logger, **kwargs):
    if 'deploy_scenario' in kwargs and kwargs['deploy_scenario'] is not None:
        os.environ['DEPLOY_SCENARIO'] = kwargs['deploy_scenario']
        logger.info("DEPLOY_SCENARIO : %s", os.environ['DEPLOY_SCENARIO'])


def check_hosts_file(logger):
    hosts_file = os.path.join(dt_cfg.dovetail_config['config_dir'],
                              'hosts.yaml')
    if not os.path.isfile(hosts_file):
        logger.warn("There is no hosts file {}, may be some issues with "
                    "domain name resolution.".format(hosts_file))


def parse_cli(logger=None, **kwargs):
    configs = filter_config(kwargs, logger)
    if configs is not None:
        dt_cfg.update_config(configs)
    dt_cfg.dovetail_config['offline'] = kwargs['offline']
    dt_cfg.dovetail_config['noclean'] = kwargs['no_clean']
    dt_cfg.dovetail_config['stop'] = kwargs['stop']
    dt_cfg.dovetail_config['mandatory'] = kwargs['mandatory']
    dt_cfg.dovetail_config['optional'] = kwargs['optional']
    if kwargs['no_api_validation']:
        dt_cfg.dovetail_config['no_api_validation'] = True
        logger.warning('Strict API response validation DISABLED.')
    else:
        dt_cfg.dovetail_config['no_api_validation'] = False


def check_testcase_list(testcase_list, logger=None):
    if testcase_list:
        for tc in testcase_list:
            if tc not in Testcase.testcase_list:
                logger.error('Test case {} is not defined.'.format(tc))
                return None
        return testcase_list
    logger.error("There is no test case to be executed.")
    return None


def get_testcase_list(logger=None, **kwargs):
    Testcase.load()
    testcase_list = kwargs['testcase']

    # If specify 'testcase' on the CLI, ignore 'testsuite' and 'testarea'. In
    # this case, all test cases are marked as mandatory=false in the result
    # file because there is no testsuite to relate to.
    # If 'testcase' is not specified on the CLI, check the combination of
    # 'testsuite' and 'testarea'
    if testcase_list:
        return check_testcase_list(testcase_list, logger)

    testsuite_validation = False
    testsuite = kwargs['testsuite']
    if testsuite in dt_cfg.dovetail_config['testsuite_supported']:
        testsuite_validation = True
    origin_testarea = kwargs['testarea']
    testarea_validation, testarea = Testcase.check_testarea(origin_testarea)

    if testsuite_validation and testarea_validation:
        testsuite_yaml = load_testsuite(testsuite)
        testcase_list = Testcase.get_testcases_for_testsuite(testsuite_yaml,
                                                             testarea)
        return check_testcase_list(testcase_list, logger)
    elif not testsuite_validation:
        logger.error('Test suite {} is not defined.'.format(testsuite))
    else:
        logger.error('Test area {} is not defined.'.format(origin_testarea))
    return None


def main(*args, **kwargs):
    """Dovetail compliance test entry!"""
    build_tag = "daily-master-%s" % str(uuid.uuid1())
    dt_cfg.dovetail_config['build_tag'] = build_tag
    if not get_result_path():
        return
    clean_results_dir()
    if kwargs['debug']:
        os.environ['DEBUG'] = 'true'
    create_logs()
    logger = dt_logger.Logger('run').getLogger()

    logger.info('================================================')
    logger.info('Dovetail compliance: {}!'.format(kwargs['testsuite']))
    logger.info('================================================')
    logger.info('Build tag: {}'.format(dt_cfg.dovetail_config['build_tag']))
    parse_cli(logger, **kwargs)
    update_deploy_scenario(logger, **kwargs)
    env_init(logger)
    copy_userconfig_files(logger)
    copy_patch_files(logger)
    dt_utils.check_docker_version(logger)
    dt_utils.get_openstack_endpoint(logger)
    check_hosts_file(logger)
    dt_utils.get_hardware_info(logger)

    testcase_list = get_testcase_list(logger, **kwargs)
    if not testcase_list:
        raise SystemExit(EXIT_RUN_FAILED)

    duration = run_test(testcase_list, logger)
    if (duration != "stop_on_fail"):
        Report.generate(testcase_list, duration)
        if (kwargs['report']):
            Report.save_logs()


dt_cfg.load_config_files(constants.CONF_PATH)
dovetail_config = copy.deepcopy(dt_cfg.dovetail_config)
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
if dovetail_config['cli']['options'] is not None:
    for key, value in dovetail_config['cli']['options'].items():
        if value is not None:
            for k, v in value.items():
                flags = v['flags']
                v.pop('flags')
                v.pop('path', None)
                main = click.option(*flags, **v)(main)
if dovetail_config['cli']['arguments'] is not None:
    for key, value in dovetail_config['cli']['arguments'].items():
        if value is not None:
            for k, v in value.items():
                flags = v['flags']
                v.pop('flags')
                v.pop('path', None)
                main = click.argument(flags, **v)(main)
main = click.command(context_settings=CONTEXT_SETTINGS)(main)


if __name__ == '__main__':
    main()