From fa5336ca3615c725cd1b9883f56bc1e081ae6f59 Mon Sep 17 00:00:00 2001 From: Nolan Brubaker Date: Tue, 6 Jun 2017 21:26:27 -0400 Subject: [PATCH] Fix Python 3 dictionary incompatibilities There were two instances of abusing old Python 2 behaviors around dictionary keys that Python 3 no longer allows. These are both fixed. Change-Id: I5865ee3becf52fca590a1c0c897bcd968210ac9d --- osa_toolkit/generate.py | 14 +++++++++++--- tests/test_inventory.py | 7 ++++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/osa_toolkit/generate.py b/osa_toolkit/generate.py index ddcf91f4a5..5bb966f405 100755 --- a/osa_toolkit/generate.py +++ b/osa_toolkit/generate.py @@ -829,14 +829,22 @@ def _parse_global_variables(user_cidr, inventory, user_defined_config): ) logger.debug("Applied global_overrides") - kept_vars = user_defined_config['global_overrides'].keys() + # NOTE (palendae): wrapped in a list to support python3, + # which uses `dict_keys` objects that can't be appended + kept_vars = list(user_defined_config['global_overrides'].keys()) kept_vars.append('container_cidr') # Remove global overrides that were deleted from inventory, too + # We use the to_delete list due to Python 3 disallowing dict + # size mutation during iteration + to_delete = [] for key in inventory['all']['vars'].keys(): if key not in kept_vars: - logger.debug("Deleting key %s from inventory", key) - del inventory['all']['vars'][key] + to_delete.append(key) + + for key in to_delete: + logger.debug("Deleting key %s from inventory", key) + del inventory['all']['vars'][key] def _check_same_ip_to_multiple_host(config): diff --git a/tests/test_inventory.py b/tests/test_inventory.py index 1d9f7b8fe9..5d0a3e1244 100644 --- a/tests/test_inventory.py +++ b/tests/test_inventory.py @@ -988,9 +988,14 @@ class TestOverridingEnvVars(OverridingEnvBase): # a partial override file vol = self.cinder_config['container_skel']['cinder_volumes_container'] + keys = vol.keys() + to_delete = [] for key in vol.keys(): if not key == 'properties': - del vol[key] + to_delete.append(key) + + for key in to_delete: + del vol[key] self.write_override_env()