DavidPurcell | b25f93d | 2017-01-27 12:46:27 -0500 | [diff] [blame] | 1 | # Copyright 2017 AT&T Corporation. |
DavidPurcell | 663aedf | 2017-01-03 10:01:14 -0500 | [diff] [blame] | 2 | # 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 Pryor | 7f8993f | 2017-08-14 12:53:17 -0400 | [diff] [blame] | 16 | import logging |
DavidPurcell | 663aedf | 2017-01-03 10:01:14 -0500 | [diff] [blame] | 17 | import os |
| 18 | |
Sean Pryor | 7f8993f | 2017-08-14 12:53:17 -0400 | [diff] [blame] | 19 | from oslo_concurrency import lockutils |
| 20 | |
DavidPurcell | 663aedf | 2017-01-03 10:01:14 -0500 | [diff] [blame] | 21 | from tempest import config |
| 22 | from tempest.test_discover import plugins |
| 23 | |
Felipe Monteiro | 739041f | 2018-03-25 00:24:03 -0400 | [diff] [blame] | 24 | from patrole_tempest_plugin import config as pconfig |
DavidPurcell | 663aedf | 2017-01-03 10:01:14 -0500 | [diff] [blame] | 25 | |
Sean Pryor | 7f8993f | 2017-08-14 12:53:17 -0400 | [diff] [blame] | 26 | RBACLOG = logging.getLogger('rbac_reporting') |
| 27 | |
DavidPurcell | 663aedf | 2017-01-03 10:01:14 -0500 | [diff] [blame] | 28 | |
| 29 | class PatroleTempestPlugin(plugins.TempestPlugin): |
Sean Pryor | 7f8993f | 2017-08-14 12:53:17 -0400 | [diff] [blame] | 30 | |
DavidPurcell | 663aedf | 2017-01-03 10:01:14 -0500 | [diff] [blame] | 31 | def load_tests(self): |
| 32 | base_path = os.path.split(os.path.dirname( |
| 33 | os.path.abspath(__file__)))[0] |
Felipe Monteiro | 1461ddc | 2017-05-16 18:37:13 +0100 | [diff] [blame] | 34 | test_dir = "patrole_tempest_plugin/tests/api" |
DavidPurcell | 663aedf | 2017-01-03 10:01:14 -0500 | [diff] [blame] | 35 | full_test_dir = os.path.join(base_path, test_dir) |
| 36 | return full_test_dir, base_path |
| 37 | |
Sean Pryor | 7f8993f | 2017-08-14 12:53:17 -0400 | [diff] [blame] | 38 | @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 | |
DavidPurcell | 663aedf | 2017-01-03 10:01:14 -0500 | [diff] [blame] | 64 | def register_opts(self, conf): |
Felipe Monteiro | f6eb862 | 2017-08-06 06:08:02 +0100 | [diff] [blame] | 65 | config.register_opt_group( |
| 66 | conf, |
Felipe Monteiro | 739041f | 2018-03-25 00:24:03 -0400 | [diff] [blame] | 67 | pconfig.patrole_group, |
| 68 | pconfig.PatroleGroup) |
Sean Pryor | 7f8993f | 2017-08-14 12:53:17 -0400 | [diff] [blame] | 69 | config.register_opt_group( |
| 70 | conf, |
Felipe Monteiro | 739041f | 2018-03-25 00:24:03 -0400 | [diff] [blame] | 71 | pconfig.patrole_log_group, |
| 72 | pconfig.PatroleLogGroup) |
| 73 | config.register_opt_group( |
| 74 | conf, |
| 75 | pconfig.policy_feature_enabled, |
| 76 | pconfig.PolicyFeatureEnabledGroup) |
Sean Pryor | 7f8993f | 2017-08-14 12:53:17 -0400 | [diff] [blame] | 77 | |
| 78 | if conf.patrole_log.enable_reporting: |
| 79 | self._configure_per_test_logging(conf) |
DavidPurcell | 663aedf | 2017-01-03 10:01:14 -0500 | [diff] [blame] | 80 | |
| 81 | def get_opt_lists(self): |
Felipe Monteiro | 739041f | 2018-03-25 00:24:03 -0400 | [diff] [blame] | 82 | return [ |
| 83 | (pconfig.patrole_group.name, pconfig.PatroleGroup), |
| 84 | (pconfig.policy_feature_enabled.name, |
| 85 | pconfig.PolicyFeatureEnabledGroup) |
| 86 | ] |