blob: 8af6a69d42f44098eb27004e816102d6507b8e42 [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
Felipe Monteiro739041f2018-03-25 00:24:03 -040024from patrole_tempest_plugin import config as pconfig
DavidPurcell663aedf2017-01-03 10:01:14 -050025
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 config.register_opt_group(
66 conf,
Felipe Monteiro739041f2018-03-25 00:24:03 -040067 pconfig.patrole_group,
68 pconfig.PatroleGroup)
Sean Pryor7f8993f2017-08-14 12:53:17 -040069 config.register_opt_group(
70 conf,
Felipe Monteiro739041f2018-03-25 00:24:03 -040071 pconfig.patrole_log_group,
72 pconfig.PatroleLogGroup)
73 config.register_opt_group(
74 conf,
75 pconfig.policy_feature_enabled,
76 pconfig.PolicyFeatureEnabledGroup)
Sean Pryor7f8993f2017-08-14 12:53:17 -040077
78 if conf.patrole_log.enable_reporting:
79 self._configure_per_test_logging(conf)
DavidPurcell663aedf2017-01-03 10:01:14 -050080
81 def get_opt_lists(self):
Felipe Monteiro739041f2018-03-25 00:24:03 -040082 return [
83 (pconfig.patrole_group.name, pconfig.PatroleGroup),
84 (pconfig.policy_feature_enabled.name,
85 pconfig.PolicyFeatureEnabledGroup)
86 ]