From 371b48d45c66aacc1488074b26215e5dd41abcb7 Mon Sep 17 00:00:00 2001 From: Youngjun Date: Wed, 13 Mar 2024 11:53:53 +0900 Subject: [PATCH] refectoring: Eliminate repetitive code - Removed to avoid repeatedly specifying dictionaries. Change-Id: Ia0bcae7934646e01eea8e866b92b9b2b7d79f542 Signed-off-by: Youngjun --- .../common/services/gnocchi/metric.py | 6 +++--- .../common/services/image/glance_v2.py | 3 +-- .../task/scenarios/senlin/utils.py | 5 ++--- .../common/services/gnocchi/test_metric.py | 20 +++++++++---------- .../unit/task/scenarios/manila/test_shares.py | 4 ++-- 5 files changed, 18 insertions(+), 20 deletions(-) diff --git a/rally_openstack/common/services/gnocchi/metric.py b/rally_openstack/common/services/gnocchi/metric.py index 9424a4ff..2c4dc1d1 100644 --- a/rally_openstack/common/services/gnocchi/metric.py +++ b/rally_openstack/common/services/gnocchi/metric.py @@ -58,9 +58,9 @@ class GnocchiService(service.Service): :param metric_pattern: Wildcard of metric name to match :param archive_policy_name: Archive policy name """ - archive_policy_rule = {"name": name} - archive_policy_rule["metric_pattern"] = metric_pattern - archive_policy_rule["archive_policy_name"] = archive_policy_name + archive_policy_rule = {"name": name, + "metric_pattern": metric_pattern, + "archive_policy_name": archive_policy_name} return self._clients.gnocchi().archive_policy_rule.create( archive_policy_rule) diff --git a/rally_openstack/common/services/image/glance_v2.py b/rally_openstack/common/services/image/glance_v2.py index 3a1162a0..b79e802c 100644 --- a/rally_openstack/common/services/image/glance_v2.py +++ b/rally_openstack/common/services/image/glance_v2.py @@ -132,8 +132,7 @@ class GlanceV2Service(service.Service, glance_common.GlanceMixin): :param visibility: Filter in images for the specified visibility :param owner: Filter in images for tenant ID """ - filters = {} - filters["status"] = status + filters = {"status": status} if visibility: filters["visibility"] = visibility if owner: diff --git a/rally_openstack/task/scenarios/senlin/utils.py b/rally_openstack/task/scenarios/senlin/utils.py index 0cf48644..6e06bc1a 100644 --- a/rally_openstack/task/scenarios/senlin/utils.py +++ b/rally_openstack/task/scenarios/senlin/utils.py @@ -127,9 +127,8 @@ class SenlinScenario(scenario.OpenStackScenario): :returns: object of profile created """ - attrs = {} - attrs["spec"] = spec - attrs["name"] = self.generate_random_name() + attrs = {"spec": spec, + "name": self.generate_random_name()} if metadata: attrs["metadata"] = metadata diff --git a/tests/unit/common/services/gnocchi/test_metric.py b/tests/unit/common/services/gnocchi/test_metric.py index 4087f0c7..77ca1821 100644 --- a/tests/unit/common/services/gnocchi/test_metric.py +++ b/tests/unit/common/services/gnocchi/test_metric.py @@ -34,9 +34,9 @@ class GnocchiServiceTestCase(test.TestCase): definition = [{"granularity": "0:00:01", "timespan": "1:00:00"}] aggregation_methods = [ "std", "count", "95pct", "min", "max", "sum", "median", "mean"] - archive_policy = {"name": "fake_name"} - archive_policy["definition"] = definition - archive_policy["aggregation_methods"] = aggregation_methods + archive_policy = {"name": "fake_name", + "definition": definition, + "aggregation_methods": aggregation_methods} self.assertEqual( self.service.create_archive_policy( @@ -65,9 +65,9 @@ class GnocchiServiceTestCase(test.TestCase): "gnocchi.list_archive_policy") def test__create_archive_policy_rule(self): - archive_policy_rule = {"name": "fake_name"} - archive_policy_rule["metric_pattern"] = "cpu_*" - archive_policy_rule["archive_policy_name"] = "low" + archive_policy_rule = {"name": "fake_name", + "metric_pattern": "cpu_*", + "archive_policy_name": "low"} self.assertEqual( self.service.create_archive_policy_rule( @@ -129,10 +129,10 @@ class GnocchiServiceTestCase(test.TestCase): "gnocchi.get_measures") def test__create_metric(self): - param = {"name": "fake_name"} - param["archive_policy_name"] = "fake_archive_policy" - param["unit"] = "fake_unit" - param["resource_id"] = "fake_resource_id" + param = {"name": "fake_name", + "archive_policy_name": "fake_archive_policy", + "unit": "fake_unit", + "resource_id": "fake_resource_id"} self.assertEqual( self.service.create_metric( name="fake_name", diff --git a/tests/unit/task/scenarios/manila/test_shares.py b/tests/unit/task/scenarios/manila/test_shares.py index f10fcf80..7d75c3b7 100644 --- a/tests/unit/task/scenarios/manila/test_shares.py +++ b/tests/unit/task/scenarios/manila/test_shares.py @@ -551,8 +551,8 @@ class ManilaSharesTestCase(test.ScenarioTestCase): def test_set_and_delete_metadata(self, params, iteration, share_number): scenario = shares.SetAndDeleteMetadata() share_list = [{"id": "fake_share_%s_id" % d} for d in range(3)] - scenario.context = {"tenant": {"shares": share_list}} - scenario.context["iteration"] = iteration + scenario.context = {"tenant": {"shares": share_list}, + "iteration": iteration} scenario._set_metadata = mock.MagicMock() scenario._delete_metadata = mock.MagicMock() expected_set_params = {