diff options
author | Ben Nemec <bnemec@redhat.com> | 2019-10-23 15:36:42 +0000 |
---|---|---|
committer | Ben Nemec <bnemec@redhat.com> | 2020-01-15 16:48:41 +0000 |
commit | 686aa238f921e8b6dff814d001690e15fa8ccea6 (patch) | |
tree | 504b35a62362d8c54beb06bb0c3e11da8e6fb54b | |
parent | 0f7e144d013155f27f74b0eb91b7ae0f1530a86b (diff) | |
download | oslo-policy-686aa238f921e8b6dff814d001690e15fa8ccea6.tar.gz |
Initialize global config object in cli tools
Currently, passing --config-file to a tool like oslopolicy-list-redundant
is ineffective because the projects pass an empty cli arg list to the
conf object when they initialize it. By registering our cli args on the
global conf object, the projects can safely parse cli args in their
call to the conf object so things like --config-file won't be ignored.
This didn't work before because oslo.policy recognizes cli args like
--namespace that aren't recognized by the consuming projects.
This will require followup changes in each project to stop passing an
empty cli arg list to the conf object initialization. In the meantime,
everything should continue to work as it did before.
Change-Id: Iacd257fc6c351582de45476768e3fd1775317d3c
Closes-Bug: 1849518
-rw-r--r-- | oslo_policy/generator.py | 16 | ||||
-rw-r--r-- | oslo_policy/tests/test_generator.py | 13 |
2 files changed, 18 insertions, 11 deletions
diff --git a/oslo_policy/generator.py b/oslo_policy/generator.py index 9e283d9..fad156e 100644 --- a/oslo_policy/generator.py +++ b/oslo_policy/generator.py @@ -334,9 +334,11 @@ def on_load_failure_callback(*args, **kwargs): raise -def generate_sample(args=None): +def generate_sample(args=None, conf=None): logging.basicConfig(level=logging.WARN) - conf = cfg.ConfigOpts() + # Allow the caller to pass in a local conf object for unit testing + if conf is None: + conf = cfg.CONF conf.register_cli_opts(GENERATOR_OPTS + RULE_OPTS) conf.register_opts(GENERATOR_OPTS + RULE_OPTS) conf(args) @@ -345,7 +347,7 @@ def generate_sample(args=None): def generate_policy(args=None): logging.basicConfig(level=logging.WARN) - conf = cfg.ConfigOpts() + conf = cfg.CONF conf.register_cli_opts(GENERATOR_OPTS + ENFORCER_OPTS) conf.register_opts(GENERATOR_OPTS + ENFORCER_OPTS) conf(args) @@ -367,9 +369,11 @@ def _upgrade_policies(policies, default_policies): 'new_name': rule_default.name}) -def upgrade_policy(args=None): +def upgrade_policy(args=None, conf=None): logging.basicConfig(level=logging.WARN) - conf = cfg.ConfigOpts() + # Allow the caller to pass in a local conf object for unit testing + if conf is None: + conf = cfg.CONF conf.register_cli_opts(GENERATOR_OPTS + RULE_OPTS + UPGRADE_OPTS) conf.register_opts(GENERATOR_OPTS + RULE_OPTS + UPGRADE_OPTS) conf(args) @@ -396,7 +400,7 @@ def upgrade_policy(args=None): def list_redundant(args=None): logging.basicConfig(level=logging.WARN) - conf = cfg.ConfigOpts() + conf = cfg.CONF conf.register_cli_opts(ENFORCER_OPTS) conf.register_opts(ENFORCER_OPTS) conf(args) diff --git a/oslo_policy/tests/test_generator.py b/oslo_policy/tests/test_generator.py index a0beeae..147ef2d 100644 --- a/oslo_policy/tests/test_generator.py +++ b/oslo_policy/tests/test_generator.py @@ -500,8 +500,9 @@ class GeneratorRaiseErrorTestCase(testtools.TestCase): def test_generator_call_with_no_arguments_raises_error(self): testargs = ['oslopolicy-sample-generator'] with mock.patch('sys.argv', testargs): + local_conf = cfg.ConfigOpts() self.assertRaises(cfg.RequiredOptError, generator.generate_sample, - []) + [], local_conf) class GeneratePolicyTestCase(base.PolicyBaseTestCase): @@ -655,6 +656,8 @@ class UpgradePolicyTestCase(base.PolicyBaseTestCase): plugin=None, obj=[self.new_policy]) self.extensions.append(ext) + # Just used for cli opt parsing + self.local_conf = cfg.ConfigOpts() def test_upgrade_policy_json_file(self): test_mgr = stevedore.named.NamedExtensionManager.make_test_instance( @@ -669,7 +672,7 @@ class UpgradePolicyTestCase(base.PolicyBaseTestCase): self.get_config_file_fullname('new_policy.json'), '--format', 'json'] with mock.patch('sys.argv', testargs): - generator.upgrade_policy() + generator.upgrade_policy(conf=self.local_conf) new_file = self.get_config_file_fullname('new_policy.json') new_policy = jsonutils.loads(open(new_file, 'r').read()) self.assertIsNotNone(new_policy.get('new_policy_name')) @@ -688,7 +691,7 @@ class UpgradePolicyTestCase(base.PolicyBaseTestCase): self.get_config_file_fullname('new_policy.yaml'), '--format', 'yaml'] with mock.patch('sys.argv', testargs): - generator.upgrade_policy() + generator.upgrade_policy(conf=self.local_conf) new_file = self.get_config_file_fullname('new_policy.yaml') new_policy = yaml.safe_load(open(new_file, 'r')) self.assertIsNotNone(new_policy.get('new_policy_name')) @@ -706,7 +709,7 @@ class UpgradePolicyTestCase(base.PolicyBaseTestCase): '--namespace', 'test_upgrade', '--format', 'json'] with mock.patch('sys.argv', testargs): - generator.upgrade_policy() + generator.upgrade_policy(conf=self.local_conf) expected = '''{ "new_policy_name": "rule:admin" }''' @@ -724,7 +727,7 @@ class UpgradePolicyTestCase(base.PolicyBaseTestCase): '--namespace', 'test_upgrade', '--format', 'yaml'] with mock.patch('sys.argv', testargs): - generator.upgrade_policy() + generator.upgrade_policy(conf=self.local_conf) expected = '''new_policy_name: rule:admin ''' self.assertEqual(expected, stdout.getvalue()) |