From 080ace0054682494134a5bca5921fe937abdbe99 Mon Sep 17 00:00:00 2001 From: Kanagaraj Manickam Date: Tue, 12 Apr 2016 16:50:40 +0530 Subject: [PATCH] Fixes cooldown logic throws ValueError Change-Id: I2835e8b2b570a82625761aa55ec34e0df4d6ecd5 Closes-bug: #1569273 --- heat/scaling/cooldown.py | 26 ++++++++++++------- .../autoscaling/test_heat_scaling_policy.py | 10 +++++++ 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/heat/scaling/cooldown.py b/heat/scaling/cooldown.py index b4517c1f64..95cc2807fd 100644 --- a/heat/scaling/cooldown.py +++ b/heat/scaling/cooldown.py @@ -33,16 +33,22 @@ class CooldownMixin(object): # If not specified, it will be None, same as cooldown == 0 cooldown = 0 - if 'cooldown' not in metadata: - # Note: this is for supporting old version cooldown checking - if metadata and cooldown != 0: - last_adjust = next(six.iterkeys(metadata)) - if not timeutils.is_older_than(last_adjust, cooldown): - return False - elif cooldown != 0: - last_adjust = next(six.iterkeys(metadata['cooldown'])) - if not timeutils.is_older_than(last_adjust, cooldown): - return False + if cooldown != 0: + try: + if 'cooldown' not in metadata: + # Note: this is for supporting old version cooldown logic + if metadata: + last_adjust = next(six.iterkeys(metadata)) + if not timeutils.is_older_than(last_adjust, cooldown): + return False + else: + last_adjust = next(six.iterkeys(metadata['cooldown'])) + if not timeutils.is_older_than(last_adjust, cooldown): + return False + except ValueError: + # occurs when metadata has only {scaling_in_progress: False} + pass + # Assumes _finished_scaling is called # after the scaling operation completes metadata['scaling_in_progress'] = True diff --git a/heat/tests/autoscaling/test_heat_scaling_policy.py b/heat/tests/autoscaling/test_heat_scaling_policy.py index 244fc2b665..bf0de919a5 100644 --- a/heat/tests/autoscaling/test_heat_scaling_policy.py +++ b/heat/tests/autoscaling/test_heat_scaling_policy.py @@ -250,6 +250,16 @@ class TestCooldownMixin(common.HeatTestCase): self.patchobject(pol, 'metadata_get', return_value=previous_meta) self.assertTrue(pol._is_scaling_allowed()) + def test_no_cooldown_no_scaling_in_progress(self): + t = template_format.parse(as_template) + stack = utils.parse_stack(t, params=as_params) + pol = self.create_scaling_policy(t, stack, 'my-policy') + + # no cooldown entry in the metadata + previous_meta = {'scaling_in_progress': False} + self.patchobject(pol, 'metadata_get', return_value=previous_meta) + self.assertTrue(pol._is_scaling_allowed()) + def test_metadata_is_written(self): t = template_format.parse(as_template) stack = utils.parse_stack(t, params=as_params)