22e09bc820bd331f1c6cf6f7168b1ae88b133fea
[functest.git] / functest / opnfv_tests / openstack / refstack / refstack.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 """Refstack testcase implementation."""
11
12 import logging
13 import os
14 import re
15 import subprocess
16 import yaml
17
18 from functest.opnfv_tests.openstack.tempest import tempest
19 from functest.utils import config
20
21
22 class Refstack(tempest.TempestCommon):
23     """Refstack testcase implementation class."""
24
25     __logger = logging.getLogger(__name__)
26
27     defcorelist = os.path.join(
28         getattr(config.CONF, 'dir_refstack_data'), 'defcore.txt')
29
30     def _extract_refstack_data(self):
31         yaml_data = ""
32         with open(self.defcorelist) as def_file:
33             for line in def_file:
34                 try:
35                     grp = re.search(r'^([^\[]*)(\[.*\])\n*$', line)
36                     yaml_data = "{}\n{}: {}".format(
37                         yaml_data, grp.group(1), grp.group(2))
38                 except Exception:  # pylint: disable=broad-except
39                     self.__logger.warning("Cannot parse %s", line)
40         return yaml.load(yaml_data)
41
42     def _extract_tempest_data(self):
43         try:
44             cmd = ['stestr', '--here', self.verifier_repo_dir, 'list',
45                    '^tempest.']
46             output = subprocess.check_output(cmd)
47         except subprocess.CalledProcessError as cpe:
48             self.__logger.error(
49                 "Exception when listing tempest tests: %s\n%s",
50                 cpe.cmd, cpe.output)
51             raise
52         yaml_data2 = ""
53         for line in output.splitlines():
54             try:
55                 grp = re.search(r'^([^\[]*)(\[.*\])\n*$', line)
56                 yaml_data2 = "{}\n{}: {}".format(
57                     yaml_data2, grp.group(1), grp.group(2))
58             except Exception:  # pylint: disable=broad-except
59                 self.__logger.warning("Cannot parse %s. skipping it", line)
60         return yaml.load(yaml_data2)
61
62     def generate_test_list(self, **kwargs):
63         self.backup_tempest_config(self.conf_file, '/etc')
64         refstack_data = self._extract_refstack_data()
65         tempest_data = self._extract_tempest_data()
66         with open(self.list, 'w') as ref_file:
67             for key in refstack_data.keys():
68                 try:
69                     for data in tempest_data[key]:
70                         if data == refstack_data[key][0]:
71                             break
72                     else:
73                         self.__logger.info("%s: ids differ. skipping it", key)
74                         continue
75                     ref_file.write("{}{}\n".format(
76                         key, str(tempest_data[key]).replace(
77                             "'", "").replace(", ", ",")))
78                 except Exception:  # pylint: disable=broad-except
79                     self.__logger.info("%s: not found. skipping it", key)
80                     continue