aboutsummaryrefslogtreecommitdiffstats
path: root/functest/opnfv_tests/openstack/refstack/refstack.py
blob: 87932020bf177ad06d42f776268201c60ec3ce98 (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
#!/usr/bin/env python

# Copyright (c) 2017 Huawei Technologies Co.,Ltd 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

"""Refstack testcase implementation."""

import logging
import os
import re
import subprocess
import yaml

from functest.opnfv_tests.openstack.tempest import tempest
from functest.utils import config


class Refstack(tempest.TempestCommon):
    """Refstack testcase implementation class."""

    __logger = logging.getLogger(__name__)

    def _extract_refstack_data(self, refstack_list):
        yaml_data = ""
        with open(refstack_list, encoding='utf-8') as def_file:
            for line in def_file:
                try:
                    grp = re.search(r'^([^\[]*)(\[.*\])\n*$', line)
                    yaml_data = f"{yaml_data}\n{grp.group(1)}: {grp.group(2)}"
                except Exception:  # pylint: disable=broad-except
                    self.__logger.warning("Cannot parse %s", line)
        return yaml.full_load(yaml_data)

    def _extract_tempest_data(self):
        olddir = os.getcwd()
        try:
            os.chdir(self.verifier_repo_dir)
            cmd = ['stestr', 'list', '^tempest.']
            output = subprocess.check_output(cmd)
        except subprocess.CalledProcessError as cpe:
            self.__logger.error(
                "Exception when listing tempest tests: %s\n%s",
                cpe.cmd, cpe.output.decode("utf-8"))
            raise
        finally:
            os.chdir(olddir)
        yaml_data2 = ""
        for line in output.splitlines():
            try:
                grp = re.search(r'^([^\[]*)(\[.*\])\n*$', line.decode("utf-8"))
                yaml_data2 = f"{yaml_data2}\n{grp.group(1)}: {grp.group(2)}"
            except Exception:  # pylint: disable=broad-except
                self.__logger.warning("Cannot parse %s. skipping it", line)
        return yaml.full_load(yaml_data2)

    def generate_test_list(self, **kwargs):
        refstack_list = os.path.join(
            getattr(config.CONF, 'dir_refstack_data'),
            f"{kwargs.get('target', 'compute')}.txt")
        self.backup_tempest_config(self.conf_file, '/etc')
        refstack_data = self._extract_refstack_data(refstack_list)
        tempest_data = self._extract_tempest_data()
        with open(self.list, 'w', encoding='utf-8') as ref_file:
            for key in refstack_data.keys():
                try:
                    for data in tempest_data[key]:
                        if data == refstack_data[key][0]:
                            break
                    else:
                        self.__logger.info("%s: ids differ. skipping it", key)
                        continue
                    value = str(tempest_data[key]).replace(
                        "'", "").replace(", ", ",")
                    ref_file.write(f"{key}{value}\n")
                except Exception:  # pylint: disable=broad-except
                    self.__logger.info("%s: not found. skipping it", key)
                    continue