blob: b7717eafbb0e44c50fdb8d4c6e87a546602d5fa1 [file] [log] [blame]
DavidPurcellb25f93d2017-01-27 12:46:27 -05001# Copyright 2017 AT&T Corporation.
DavidPurcell663aedf2017-01-03 10:01:14 -05002# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
Sean Pryor7f8993f2017-08-14 12:53:17 -040016import logging
DavidPurcell663aedf2017-01-03 10:01:14 -050017import os
18
Sean Pryor7f8993f2017-08-14 12:53:17 -040019from oslo_concurrency import lockutils
20
DavidPurcell663aedf2017-01-03 10:01:14 -050021from tempest import config
22from tempest.test_discover import plugins
23
24from patrole_tempest_plugin import config as project_config
25
Sean Pryor7f8993f2017-08-14 12:53:17 -040026RBACLOG = logging.getLogger('rbac_reporting')
27
DavidPurcell663aedf2017-01-03 10:01:14 -050028
29class PatroleTempestPlugin(plugins.TempestPlugin):
Sean Pryor7f8993f2017-08-14 12:53:17 -040030
DavidPurcell663aedf2017-01-03 10:01:14 -050031 def load_tests(self):
32 base_path = os.path.split(os.path.dirname(
33 os.path.abspath(__file__)))[0]
Felipe Monteiro1461ddc2017-05-16 18:37:13 +010034 test_dir = "patrole_tempest_plugin/tests/api"
DavidPurcell663aedf2017-01-03 10:01:14 -050035 full_test_dir = os.path.join(base_path, test_dir)
36 return full_test_dir, base_path
37
Sean Pryor7f8993f2017-08-14 12:53:17 -040038 @lockutils.synchronized('_reset_log_file')
39 def _reset_log_file(self, logfile):
40 try:
41 os.remove(logfile)
42 except OSError:
43 pass
44
45 def _configure_per_test_logging(self, conf):
46 # Separate log handler for rbac reporting
47 RBACLOG.setLevel(level=logging.INFO)
48 # Set up proper directory handling
49 report_abs_path = os.path.abspath(conf.patrole_log.report_log_path)
50 report_path = os.path.join(
51 report_abs_path, conf.patrole_log.report_log_name)
52
53 # Remove the log file if it exists
54 self._reset_log_file(report_path)
55
56 # Delay=True so that we don't end up creating an empty file if we
57 # never log to it.
58 rbac_report_handler = logging.FileHandler(
59 filename=report_path, delay=True, mode='a')
60 rbac_report_handler.setFormatter(
61 fmt=logging.Formatter(fmt='%(message)s'))
62 RBACLOG.addHandler(rbac_report_handler)
63
DavidPurcell663aedf2017-01-03 10:01:14 -050064 def register_opts(self, conf):
Felipe Monteirof6eb8622017-08-06 06:08:02 +010065 # TODO(fmontei): Remove ``rbac_group`` in a future release as it is
66 # currently deprecated.
DavidPurcell663aedf2017-01-03 10:01:14 -050067 config.register_opt_group(
68 conf,
69 project_config.rbac_group,
Felipe Monteirof6eb8622017-08-06 06:08:02 +010070 project_config.PatroleGroup)
71 config.register_opt_group(
72 conf,
73 project_config.patrole_group,
74 project_config.PatroleGroup)
Sean Pryor7f8993f2017-08-14 12:53:17 -040075 config.register_opt_group(
76 conf,
77 project_config.patrole_log_group,
78 project_config.PatroleLogGroup)
79
80 if conf.patrole_log.enable_reporting:
81 self._configure_per_test_logging(conf)
DavidPurcell663aedf2017-01-03 10:01:14 -050082
83 def get_opt_lists(self):
Felipe Monteirof6eb8622017-08-06 06:08:02 +010084 return [(project_config.patrole_group.name,
85 project_config.PatroleGroup)]