diff options
author | Gordon Chung <chungg@ca.ibm.com> | 2013-09-25 15:26:57 -0400 |
---|---|---|
committer | Gordon Chung <chungg@ca.ibm.com> | 2013-09-25 16:25:01 -0400 |
commit | 1f2ec374ea27a6ce4db4009b0a36097286908cef (patch) | |
tree | c6e6de6d71d1cfbf43db6083be2fd499e207bb6e | |
parent | e538cd562d2e2dafdf90bbe672c64b12877e583d (diff) | |
download | pycadf-1f2ec374ea27a6ce4db4009b0a36097286908cef.tar.gz |
fix conf file settings0.2
- add test for default_target_endpoint_type setting
- do not fail when missing optional conf options.
Change-Id: If09267c17db73e8adfdf3cc5d63156a0408d2edc
Fixes-Bug: #1226870
Fixes-Bug: #1228199
-rw-r--r-- | pycadf/audit/api.py | 11 | ||||
-rw-r--r-- | pycadf/tests/audit/test_api.py | 35 | ||||
-rw-r--r-- | pycadf/tests/base.py | 5 |
3 files changed, 47 insertions, 4 deletions
diff --git a/pycadf/audit/api.py b/pycadf/audit/api.py index 9d2cfbc..c947d42 100644 --- a/pycadf/audit/api.py +++ b/pycadf/audit/api.py @@ -92,20 +92,23 @@ class OpenStackAuditApi(object): try: paths = audit_map.get('DEFAULT', 'api_paths') self._api_paths = paths.lstrip().split('\n') - self._default_target_endpoint_type = \ - audit_map.get('DEFAULT', 'target_endpoint_type') + try: + self._default_target_endpoint_type = \ + audit_map.get('DEFAULT', 'target_endpoint_type') + except ConfigParser.NoOptionError: + pass except ConfigParser.NoSectionError: pass try: self._body_actions = dict(audit_map.items('body_actions')) - except ConfigParser.NoSectionError: + except ConfigParser.Error: pass try: self._service_endpoints = \ dict(audit_map.items('service_endpoints')) - except ConfigParser.NoSectionError: + except ConfigParser.Error: pass except ConfigParser.ParsingError as err: raise PycadfAuditApiConfigError( diff --git a/pycadf/tests/audit/test_api.py b/pycadf/tests/audit/test_api.py index 20fb275..7e07b4d 100644 --- a/pycadf/tests/audit/test_api.py +++ b/pycadf/tests/audit/test_api.py @@ -107,6 +107,28 @@ class TestAuditApi(base.TestCase): self.assertEqual(payload['target']['id'], 'unknown') self.assertEqual(payload['target']['typeURI'], 'unknown') + def test_get_unknown_endpoint_default_set(self): + tmpfile = self.temp_config_file_path() + with open(tmpfile, "w") as f: + f.write("[DEFAULT]\n") + f.write("target_endpoint_type = compute \n") + f.write("api_paths = servers\n\n") + f.write("[service_endpoints]\n") + f.write("compute = service/compute") + cfg.CONF.set_override('api_audit_map', tmpfile, group='audit') + self.audit_api = api.OpenStackAuditApi() + + self.assertEqual(self.audit_api._default_target_endpoint_type, + 'compute') + req = self.api_request('GET', + 'http://unknown:8774/v2/public/servers/') + payload = req.environ['CADF_EVENT'] + self.assertEqual(payload['action'], 'list') + self.assertEqual(payload['outcome'], 'pending') + self.assertEqual(payload['target']['name'], 'nova') + self.assertEqual(payload['target']['id'], 'resource_id') + self.assertEqual(payload['target']['typeURI'], 'service/compute') + def test_put(self): req = self.api_request('PUT', 'http://host:8774/v2/public/servers') payload = req.environ['CADF_EVENT'] @@ -191,3 +213,16 @@ class TestAuditApi(base.TestCase): self.assertEqual(payload['reason']['reasonCode'], '200') self.assertEqual(payload['observer'], 'target') self.assertNotIn('reporterchain', payload) + + +class TestAuditApiConf(base.TestCase): + def test_missing_default_option(self): + tmpfile = self.temp_config_file_path() + # NOTE(gordc): ensure target_endpoint_type is not in conf file + with open(tmpfile, "w") as f: + f.write("[DEFAULT]\n") + f.write("api_paths = servers\n\n") + f.write("[service_endpoints]\n") + f.write("compute = service/compute") + cfg.CONF.set_override('api_audit_map', tmpfile, group='audit') + self.audit_api = api.OpenStackAuditApi() diff --git a/pycadf/tests/base.py b/pycadf/tests/base.py index 729886a..a231b41 100644 --- a/pycadf/tests/base.py +++ b/pycadf/tests/base.py @@ -16,6 +16,7 @@ """Test base classes. """ +import fixtures import os.path from oslo.config import cfg import testtools @@ -25,6 +26,7 @@ class TestCase(testtools.TestCase): def setUp(self): super(TestCase, self).setUp() + self.tempdir = self.useFixture(fixtures.TempDir()) cfg.CONF([], project='pycadf') def path_get(self, project_file=None): @@ -38,6 +40,9 @@ class TestCase(testtools.TestCase): else: return root + def temp_config_file_path(self, name='api_audit_map.conf'): + return os.path.join(self.tempdir.path, name) + def tearDown(self): cfg.CONF.reset() super(TestCase, self).tearDown() |