Merge "Move CooldownMixin tests to group test modules"

This commit is contained in:
Zuul 2018-02-06 23:36:07 +00:00 committed by Gerrit Code Review
commit 9a403e225c
4 changed files with 241 additions and 195 deletions

View File

@ -10,9 +10,11 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
import json
import mock
from oslo_utils import timeutils
import six
from heat.common import exception
@ -664,3 +666,122 @@ class IncorrectUpdatePolicyTest(common.HeatTestCase):
exc = self.assertRaises(exception.StackValidationFailed,
stack.validate)
self.assertIn('Unknown Property RollingUpdate', six.text_type(exc))
class TestCooldownMixin(common.HeatTestCase):
def setUp(self):
super(TestCooldownMixin, self).setUp()
t = template_format.parse(inline_templates.as_heat_template)
self.stack = utils.parse_stack(t, params=inline_templates.as_params)
self.stack.store()
self.group = self.stack['my-group']
self.group.state_set('CREATE', 'COMPLETE')
def test_cooldown_is_in_progress_toosoon(self):
cooldown_end = timeutils.utcnow() + datetime.timedelta(seconds=60)
previous_meta = {'cooldown_end': {
cooldown_end.isoformat(): 'change_in_capacity : 1'}}
self.patchobject(self.group, 'metadata_get',
return_value=previous_meta)
self.assertRaises(resource.NoActionRequired,
self.group._check_scaling_allowed,
60)
def test_cooldown_is_in_progress_toosoon_legacy(self):
now = timeutils.utcnow()
previous_meta = {'cooldown': {
now.isoformat(): 'change_in_capacity : 1'}}
self.patchobject(self.group, 'metadata_get',
return_value=previous_meta)
self.assertRaises(resource.NoActionRequired,
self.group._check_scaling_allowed,
60)
def test_cooldown_is_in_progress_scaling_unfinished(self):
previous_meta = {'scaling_in_progress': True}
self.patchobject(self.group, 'metadata_get',
return_value=previous_meta)
self.assertRaises(resource.NoActionRequired,
self.group._check_scaling_allowed,
60)
def test_cooldown_not_in_progress_legacy(self):
awhile_ago = timeutils.utcnow() - datetime.timedelta(seconds=100)
previous_meta = {
'cooldown': {
awhile_ago.isoformat(): 'change_in_capacity : 1'
},
'scaling_in_progress': False
}
self.patchobject(self.group, 'metadata_get',
return_value=previous_meta)
self.assertIsNone(self.group._check_scaling_allowed(60))
def test_cooldown_not_in_progress(self):
awhile_after = timeutils.utcnow() + datetime.timedelta(seconds=60)
previous_meta = {
'cooldown_end': {
awhile_after.isoformat(): 'change_in_capacity : 1'
},
'scaling_in_progress': False
}
timeutils.set_time_override()
timeutils.advance_time_seconds(100)
self.patchobject(self.group, 'metadata_get',
return_value=previous_meta)
self.assertIsNone(self.group._check_scaling_allowed(60))
timeutils.clear_time_override()
def test_scaling_policy_cooldown_zero(self):
now = timeutils.utcnow()
previous_meta = {'cooldown_end': {
now.isoformat(): 'change_in_capacity : 1'}}
self.patchobject(self.group, 'metadata_get',
return_value=previous_meta)
self.assertIsNone(self.group._check_scaling_allowed(0))
def test_scaling_policy_cooldown_none(self):
now = timeutils.utcnow()
previous_meta = {'cooldown_end': {
now.isoformat(): 'change_in_capacity : 1'}}
self.patchobject(self.group, 'metadata_get',
return_value=previous_meta)
self.assertIsNone(self.group._check_scaling_allowed(None))
def test_no_cooldown_no_scaling_in_progress(self):
# no cooldown entry in the metadata
awhile_ago = timeutils.utcnow() - datetime.timedelta(seconds=100)
previous_meta = {'scaling_in_progress': False,
awhile_ago.isoformat(): 'change_in_capacity : 1'}
self.patchobject(self.group, 'metadata_get',
return_value=previous_meta)
self.assertIsNone(self.group._check_scaling_allowed(60))
def test_metadata_is_written(self):
nowish = timeutils.utcnow()
reason = 'cool as'
meta_set = self.patchobject(self.group, 'metadata_set')
self.patchobject(timeutils, 'utcnow', return_value=nowish)
self.group._finished_scaling(60, reason)
cooldown_end = nowish + datetime.timedelta(seconds=60)
meta_set.assert_called_once_with(
{'cooldown_end': {cooldown_end.isoformat(): reason},
'scaling_in_progress': False})
def test_metadata_is_written_update(self):
nowish = timeutils.utcnow()
reason = 'cool as'
prev_cooldown_end = nowish + datetime.timedelta(seconds=100)
previous_meta = {
'cooldown_end': {
prev_cooldown_end.isoformat(): 'change_in_capacity : 1'
}
}
self.patchobject(self.group, 'metadata_get',
return_value=previous_meta)
meta_set = self.patchobject(self.group, 'metadata_set')
self.patchobject(timeutils, 'utcnow', return_value=nowish)
self.group._finished_scaling(60, reason)
meta_set.assert_called_once_with(
{'cooldown_end': {prev_cooldown_end.isoformat(): reason},
'scaling_in_progress': False})

View File

@ -11,10 +11,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
import mock
from oslo_utils import timeutils
import six
from heat.common import exception
@ -152,104 +149,6 @@ class TestAutoScalingPolicy(common.HeatTestCase):
self.assertEqual('convg_xyz', rsrc.FnGetRefId())
class TestCooldownMixin(common.HeatTestCase):
def create_scaling_group(self, t, stack, resource_name):
stack.store()
rsrc = stack[resource_name]
rsrc.state_set('CREATE', 'COMPLETE')
return rsrc
def test_cooldown_is_in_progress_toosoon(self):
t = template_format.parse(as_template)
stack = utils.parse_stack(t, params=as_params)
group = self.create_scaling_group(t, stack, 'my-group')
now = timeutils.utcnow()
previous_meta = {'cooldown': {
now.isoformat(): 'change_in_capacity : 1'}}
self.patchobject(group, 'metadata_get', return_value=previous_meta)
ex = self.assertRaises(resource.NoActionRequired,
group._check_scaling_allowed,
60)
self.assertIn('due to cooldown', six.text_type(ex))
def test_cooldown_is_in_progress_scaling_unfinished(self):
t = template_format.parse(as_template)
stack = utils.parse_stack(t, params=as_params)
group = self.create_scaling_group(t, stack, 'my-group')
previous_meta = {'scaling_in_progress': True}
self.patchobject(group, 'metadata_get', return_value=previous_meta)
ex = self.assertRaises(resource.NoActionRequired,
group._check_scaling_allowed,
60)
self.assertIn('due to scaling activity', six.text_type(ex))
def test_cooldown_not_in_progress(self):
t = template_format.parse(as_template)
stack = utils.parse_stack(t, params=as_params)
group = self.create_scaling_group(t, stack, 'my-group')
awhile_ago = timeutils.utcnow() - datetime.timedelta(seconds=100)
previous_meta = {
'cooldown': {
awhile_ago.isoformat(): 'change_in_capacity : 1'
},
'scaling_in_progress': False
}
self.patchobject(group, 'metadata_get', return_value=previous_meta)
self.assertIsNone(group._check_scaling_allowed(60))
def test_scaling_policy_cooldown_zero(self):
t = template_format.parse(as_template)
stack = utils.parse_stack(t, params=as_params)
group = self.create_scaling_group(t, stack, 'my-group')
now = timeutils.utcnow()
previous_meta = {'cooldown': {
now.isoformat(): 'change_in_capacity : 1'}}
self.patchobject(group, 'metadata_get', return_value=previous_meta)
self.assertIsNone(group._check_scaling_allowed(0))
def test_scaling_policy_cooldown_none(self):
t = template_format.parse(as_template)
stack = utils.parse_stack(t, params=as_params)
group = self.create_scaling_group(t, stack, 'my-group')
now = timeutils.utcnow()
previous_meta = {'cooldown': {
now.isoformat(): 'change_in_capacity : 1'}}
self.patchobject(group, 'metadata_get', return_value=previous_meta)
self.assertIsNone(group._check_scaling_allowed(None))
def test_no_cooldown_no_scaling_in_progress(self):
t = template_format.parse(as_template)
stack = utils.parse_stack(t, params=as_params)
group = self.create_scaling_group(t, stack, 'my-group')
# no cooldown entry in the metadata
awhile_ago = timeutils.utcnow() - datetime.timedelta(seconds=100)
previous_meta = {'scaling_in_progress': False,
awhile_ago.isoformat(): 'change_in_capacity : 1'}
self.patchobject(group, 'metadata_get', return_value=previous_meta)
self.assertIsNone(group._check_scaling_allowed(60))
def test_metadata_is_written(self):
t = template_format.parse(as_template)
stack = utils.parse_stack(t, params=as_params)
group = self.create_scaling_group(t, stack, 'my-group')
nowish = timeutils.utcnow()
reason = 'cool as'
meta_set = self.patchobject(group, 'metadata_set')
self.patchobject(timeutils, 'utcnow', return_value=nowish)
group._finished_scaling(0, reason, size_changed=True)
meta_set.assert_called_once_with(
{'cooldown_end': {nowish.isoformat(): reason},
'scaling_in_progress': False})
class ScalingPolicyAttrTest(common.HeatTestCase):
def setUp(self):
super(ScalingPolicyAttrTest, self).setUp()

View File

@ -11,9 +11,11 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
import json
import mock
from oslo_utils import timeutils
import six
from heat.common import exception
@ -725,3 +727,121 @@ class RollingUpdatePolicyDiffTest(common.HeatTestCase):
def test_update_policy_removed(self):
self.validate_update_policy_diff(asg_tmpl_with_updt_policy(),
inline_templates.as_template)
class TestCooldownMixin(common.HeatTestCase):
def setUp(self):
super(TestCooldownMixin, self).setUp()
t = template_format.parse(inline_templates.as_template)
self.stack = utils.parse_stack(t, params=inline_templates.as_params)
self.stack.store()
self.group = self.stack['WebServerGroup']
self.group.state_set('CREATE', 'COMPLETE')
def test_cooldown_is_in_progress_toosoon(self):
cooldown_end = timeutils.utcnow() + datetime.timedelta(seconds=60)
previous_meta = {'cooldown_end': {
cooldown_end.isoformat(): 'ChangeInCapacity : 1'}}
self.patchobject(self.group, 'metadata_get',
return_value=previous_meta)
self.assertRaises(resource.NoActionRequired,
self.group._check_scaling_allowed,
60)
def test_cooldown_is_in_progress_toosoon_legacy(self):
now = timeutils.utcnow()
previous_meta = {'cooldown': {
now.isoformat(): 'ChangeInCapacity : 1'}}
self.patchobject(self.group, 'metadata_get',
return_value=previous_meta)
self.assertRaises(resource.NoActionRequired,
self.group._check_scaling_allowed,
60)
def test_cooldown_is_in_progress_scaling_unfinished(self):
previous_meta = {'scaling_in_progress': True}
self.patchobject(self.group, 'metadata_get',
return_value=previous_meta)
self.assertRaises(resource.NoActionRequired,
self.group._check_scaling_allowed,
60)
def test_scaling_not_in_progress_legacy(self):
awhile_ago = timeutils.utcnow() - datetime.timedelta(seconds=100)
previous_meta = {
'cooldown': {
awhile_ago.isoformat(): 'ChangeInCapacity : 1'
},
'scaling_in_progress': False
}
self.patchobject(self.group, 'metadata_get',
return_value=previous_meta)
self.assertIsNone(self.group._check_scaling_allowed(60))
def test_scaling_not_in_progress(self):
awhile_after = timeutils.utcnow() + datetime.timedelta(seconds=60)
previous_meta = {
'cooldown_end': {
awhile_after.isoformat(): 'ChangeInCapacity : 1'
},
'scaling_in_progress': False
}
timeutils.set_time_override()
timeutils.advance_time_seconds(100)
self.patchobject(self.group, 'metadata_get',
return_value=previous_meta)
self.assertIsNone(self.group._check_scaling_allowed(60))
timeutils.clear_time_override()
def test_scaling_policy_cooldown_zero(self):
now = timeutils.utcnow()
previous_meta = {
'cooldown_end': {
now.isoformat(): 'ChangeInCapacity : 1'
},
'scaling_in_progress': False
}
self.patchobject(self.group, 'metadata_get',
return_value=previous_meta)
self.assertIsNone(self.group._check_scaling_allowed(60))
def test_scaling_policy_cooldown_none(self):
now = timeutils.utcnow()
previous_meta = {
'cooldown_end': {
now.isoformat(): 'ChangeInCapacity : 1'
},
'scaling_in_progress': False
}
self.patchobject(self.group, 'metadata_get',
return_value=previous_meta)
self.assertIsNone(self.group._check_scaling_allowed(None))
def test_metadata_is_written(self):
nowish = timeutils.utcnow()
reason = 'cool as'
meta_set = self.patchobject(self.group, 'metadata_set')
self.patchobject(timeutils, 'utcnow', return_value=nowish)
self.group._finished_scaling(60, reason)
cooldown_end = nowish + datetime.timedelta(seconds=60)
meta_set.assert_called_once_with(
{'cooldown_end': {cooldown_end.isoformat(): reason},
'scaling_in_progress': False})
def test_metadata_is_written_update(self):
nowish = timeutils.utcnow()
reason = 'cool as'
prev_cooldown_end = nowish + datetime.timedelta(seconds=100)
previous_meta = {
'cooldown_end': {
prev_cooldown_end.isoformat(): 'ChangeInCapacity : 1'
}
}
self.patchobject(self.group, 'metadata_get',
return_value=previous_meta)
meta_set = self.patchobject(self.group, 'metadata_set')
self.patchobject(timeutils, 'utcnow', return_value=nowish)
self.group._finished_scaling(60, reason)
meta_set.assert_called_once_with(
{'cooldown_end': {prev_cooldown_end.isoformat(): reason},
'scaling_in_progress': False})

View File

@ -11,10 +11,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
import mock
from oslo_utils import timeutils
import six
from heat.common import exception
@ -87,9 +84,7 @@ class TestAutoScalingPolicy(common.HeatTestCase):
group = stack['WebServerGroup']
self.patchobject(group, 'adjust',
side_effect=resource.NoActionRequired())
mock_fin_scaling = self.patchobject(group, '_finished_scaling')
self.assertRaises(resource.NoActionRequired, up_policy.handle_signal)
self.assertEqual(0, mock_fin_scaling.call_count)
def test_scaling_policy_adjust_size_changed(self):
t = template_format.parse(as_template)
@ -166,95 +161,6 @@ class TestAutoScalingPolicy(common.HeatTestCase):
self.assertEqual('http://convg_signed_url', rsrc.FnGetRefId())
class TestCooldownMixin(common.HeatTestCase):
def create_scaling_group(self, t, stack, resource_name):
stack.store()
rsrc = stack[resource_name]
rsrc.state_set('CREATE', 'COMPLETE')
return rsrc
def test_cooldown_is_in_progress_toosoon(self):
t = template_format.parse(as_template)
stack = utils.parse_stack(t, params=as_params)
group = self.create_scaling_group(t, stack, 'WebServerGroup')
now = timeutils.utcnow()
previous_meta = {'cooldown': {
now.isoformat(): 'ChangeInCapacity : 1'}}
self.patchobject(group, 'metadata_get', return_value=previous_meta)
ex = self.assertRaises(resource.NoActionRequired,
group._check_scaling_allowed,
60)
self.assertIn('due to cooldown', six.text_type(ex))
def test_cooldown_is_in_progress_scaling_unfinished(self):
t = template_format.parse(as_template)
stack = utils.parse_stack(t, params=as_params)
group = self.create_scaling_group(t, stack, 'WebServerGroup')
previous_meta = {'scaling_in_progress': True}
self.patchobject(group, 'metadata_get', return_value=previous_meta)
ex = self.assertRaises(resource.NoActionRequired,
group._check_scaling_allowed, 60)
self.assertIn('due to scaling activity', six.text_type(ex))
def test_cooldown_not_in_progress(self):
t = template_format.parse(as_template)
stack = utils.parse_stack(t, params=as_params)
group = self.create_scaling_group(t, stack, 'WebServerGroup')
awhile_ago = timeutils.utcnow() - datetime.timedelta(seconds=100)
previous_meta = {
'cooldown': {
awhile_ago.isoformat(): 'ChangeInCapacity : 1'
},
'scaling_in_progress': False
}
self.patchobject(group, 'metadata_get', return_value=previous_meta)
self.assertIsNone(group._check_scaling_allowed(60))
def test_scaling_policy_cooldown_zero(self):
t = template_format.parse(as_template)
stack = utils.parse_stack(t, params=as_params)
group = self.create_scaling_group(t, stack, 'WebServerGroup')
now = timeutils.utcnow()
previous_meta = {now.isoformat(): 'ChangeInCapacity : 1'}
self.patchobject(group, 'metadata_get', return_value=previous_meta)
self.assertIsNone(group._check_scaling_allowed(0))
def test_scaling_policy_cooldown_none(self):
t = template_format.parse(as_template)
# Create the scaling policy no Cooldown property, should behave the
# same as when Cooldown==0
properties = t['Resources']['WebServerScaleUpPolicy']['Properties']
del properties['Cooldown']
stack = utils.parse_stack(t, params=as_params)
group = self.create_scaling_group(t, stack, 'WebServerGroup')
now = timeutils.utcnow()
previous_meta = {now.isoformat(): 'ChangeInCapacity : 1'}
self.patchobject(group, 'metadata_get', return_value=previous_meta)
self.assertIsNone(group._check_scaling_allowed(None))
def test_metadata_is_written(self):
t = template_format.parse(as_template)
stack = utils.parse_stack(t, params=as_params)
group = self.create_scaling_group(t, stack, 'WebServerGroup')
nowish = timeutils.utcnow()
reason = 'cool as'
meta_set = self.patchobject(group, 'metadata_set')
self.patchobject(timeutils, 'utcnow', return_value=nowish)
group._finished_scaling(0, reason)
meta_set.assert_called_once_with(
{'cooldown_end': {nowish.isoformat(): reason},
'scaling_in_progress': False})
class ScalingPolicyAttrTest(common.HeatTestCase):
def setUp(self):
super(ScalingPolicyAttrTest, self).setUp()