Fix role processing in Patrole
[functest.git] / functest / opnfv_tests / openstack / patrole / patrole.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2018 Orange 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 # pylint: disable=missing-docstring
11
12 import logging
13 import os
14
15 from functest.opnfv_tests.openstack.tempest import conf_utils
16 from functest.opnfv_tests.openstack.tempest import tempest
17 from functest.utils import config
18
19
20 class Patrole(tempest.TempestCommon):
21
22     __logger = logging.getLogger(__name__)
23
24     def __init__(self, **kwargs):
25         if "case_name" not in kwargs:
26             kwargs["case_name"] = 'patrole'
27         super(Patrole, self).__init__(**kwargs)
28         self.res_dir = os.path.join(
29             getattr(config.CONF, 'dir_results'), 'patrole')
30         self.list = os.path.join(self.res_dir, 'tempest-list.txt')
31
32     def apply_tempest_blacklist(self):
33         pass
34
35     def configure(self, **kwargs):
36         super(Patrole, self).configure(**kwargs)
37         rconfig = conf_utils.ConfigParser.RawConfigParser()
38         rconfig.read(self.conf_file)
39         rconfig.add_section('rbac')
40         rconfig.set('rbac', 'enable_rbac', True)
41         rconfig.set('rbac', 'rbac_test_role', kwargs.get('role', 'admin'))
42         with open(self.conf_file, 'wb') as config_file:
43             rconfig.write(config_file)
44         self.backup_tempest_config(self.conf_file, self.res_dir)
45
46     def generate_test_list(self):
47         self.backup_tempest_config(self.conf_file, '/etc')
48         super(Patrole, self).generate_test_list()
49         os.remove('/etc/tempest.conf')
50
51     def run(self, **kwargs):
52         for exclude in kwargs.get('exclude', []):
53             self.mode = "{}(?!.*{})".format(self.mode, exclude)
54         self.mode = "'{}(?=patrole_tempest_plugin.tests.api.({}))'".format(
55             self.mode, '|'.join(kwargs.get('services', [])))
56         return super(Patrole, self).run(**kwargs)