Manage block_migration in Rally
[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.full_load(yaml_data)
41
42     def _extract_tempest_data(self):
43         olddir = os.getcwd()
44         try:
45             os.chdir(self.verifier_repo_dir)
46             cmd = ['stestr', 'list', '^tempest.']
47             output = subprocess.check_output(cmd)
48         except subprocess.CalledProcessError as cpe:
49             self.__logger.error(
50                 "Exception when listing tempest tests: %s\n%s",
51                 cpe.cmd, cpe.output)
52             raise
53         finally:
54             os.chdir(olddir)
55         yaml_data2 = ""
56         for line in output.splitlines():
57             try:
58                 grp = re.search(r'^([^\[]*)(\[.*\])\n*$', line)
59                 yaml_data2 = "{}\n{}: {}".format(
60                     yaml_data2, grp.group(1), grp.group(2))
61             except Exception:  # pylint: disable=broad-except
62                 self.__logger.warning("Cannot parse %s. skipping it", line)
63         return yaml.full_load(yaml_data2)
64
65     def generate_test_list(self, **kwargs):
66         self.backup_tempest_config(self.conf_file, '/etc')
67         refstack_data = self._extract_refstack_data()
68         tempest_data = self._extract_tempest_data()
69         with open(self.list, 'w') as ref_file:
70             for key in refstack_data.keys():
71                 try:
72                     for data in tempest_data[key]:
73                         if data == refstack_data[key][0]:
74                             break
75                     else:
76                         self.__logger.info("%s: ids differ. skipping it", key)
77                         continue
78                     ref_file.write("{}{}\n".format(
79                         key, str(tempest_data[key]).replace(
80                             "'", "").replace(", ", ",")))
81                 except Exception:  # pylint: disable=broad-except
82                     self.__logger.info("%s: not found. skipping it", key)
83                     continue