def test_do_update(self): profile = stack.StackProfile('t', self.spec) # Check New Stack Path test_stack = mock.Mock() test_stack.physical_id = None new_profile = mock.Mock() self.assertTrue(profile.do_update(test_stack, new_profile)) # New Profile new_spec = { 'type': 'os.heat.stack', 'version': '1.0', 'properties': { 'template': {"Template": "data update"}, 'context': {}, 'parameters': {'new': 'params'}, 'files': {}, 'timeout': 60, 'disable_rollback': True, 'environment': {} } } new_profile = stack.StackProfile('u', new_spec) # Check Update Stack Path test_stack.physical_id = 'ce8ae86c-9810-4cb1-8888-7fb53bc523bf' profile.hc = mock.MagicMock() profile._check_action_complete = mock.MagicMock(return_value=True) profile.hc.stack_update = mock.MagicMock() self.assertTrue(profile.do_update(test_stack, new_profile)) self.assertTrue(profile.hc.stack_update.called) self.assertTrue(profile._check_action_complete.called)
def test_do_update_no_change(self): profile = stack.StackProfile('t', self.spec) hc = mock.Mock() self.patchobject(profile, 'heat', return_value=hc) stack_obj = mock.Mock() stack_obj.physical_id = 'STACKID' new_spec = copy.deepcopy(self.spec) new_profile = stack.StackProfile('u', new_spec) res = profile.do_update(stack_obj, new_profile) self.assertTrue(res)
def test_do_update(self): profile = stack.StackProfile('t', self.spec) oc = mock.Mock() profile._orchestrationclient = oc test_stack = mock.Mock(physical_id='FAKE_ID') new_spec = { 'type': 'os.heat.stack', 'version': '1.0', 'properties': { 'template': { "Template": "data update" }, 'context': {}, 'parameters': { 'new': 'params' }, 'files': { 'file1': 'new_content' }, 'timeout': 123, 'disable_rollback': False, 'environment': { 'foo': 'bar' } } } new_profile = stack.StackProfile('u', new_spec) # do it res = profile.do_update(test_stack, new_profile) # assertions self.assertTrue(res) kwargs = { 'template': { 'Template': 'data update' }, 'parameters': { 'new': 'params' }, 'timeout_mins': 123, 'disable_rollback': False, 'files': { 'file1': 'new_content' }, 'environment': { 'foo': 'bar' }, } oc.stack_update.assert_called_once_with('FAKE_ID', **kwargs) oc.wait_for_stack.assert_called_once_with('FAKE_ID', 'UPDATE_COMPLETE', timeout=3600)
def test_do_update_no_change(self): profile = stack.StackProfile('t', self.spec) oc = mock.Mock() profile._orchestrationclient = oc stack_obj = mock.Mock(physical_id='FAKE_ID') new_spec = copy.deepcopy(self.spec) new_profile = stack.StackProfile('u', new_spec) res = profile.do_update(stack_obj, new_profile) self.assertTrue(res) self.assertEqual(0, oc.stack_update.call_count)
def test_do_update_failed(self): profile = stack.StackProfile('t', self.spec) hc = mock.Mock() self.patchobject(profile, 'heat', return_value=hc) hc.stack_update.side_effect = Exception('Stack update failed.') stack_obj = mock.Mock() stack_obj.physical_id = 'STACKID' new_spec = copy.deepcopy(self.spec) new_spec['properties']['environment'] = {"new": "env1"} new_profile = stack.StackProfile('u', new_spec) res = profile.do_update(stack_obj, new_profile) hc.stack_update.assert_called_once_with('STACKID', environment={"new": "env1"}) self.assertFalse(res)
def test_do_update_timeout(self): profile = stack.StackProfile('t', self.spec) hc = mock.Mock() self.patchobject(profile, 'heat', return_value=hc) mock_check = self.patchobject(profile, '_check_action_complete', return_value=True) stack_obj = mock.Mock() stack_obj.physical_id = 'STACKID' new_spec = copy.deepcopy(self.spec) new_spec['properties']['timeout'] = 120 new_profile = stack.StackProfile('u', new_spec) profile.do_update(stack_obj, new_profile) hc.stack_update.assert_called_once_with('STACKID', timeout_mins=120) mock_check.assert_called_once_with(stack_obj, 'UPDATE')
def test_do_create_failed_create(self): oc = mock.Mock() profile = stack.StackProfile('t', self.spec) node = mock.Mock(id='NODE_ID', cluster_id='CLUSTER_ID', index=123) node.name = 'test_node' err = exc.InternalError(code=400, message='Too Bad') oc.stack_create = mock.Mock(side_effect=err) profile._orchestrationclient = oc # do it ex = self.assertRaises(exc.EResourceCreation, profile.do_create, node) # assertions self.assertEqual('Failed in creating stack: Too Bad.', six.text_type(ex)) call_args = { 'stack_name': mock.ANY, 'template': self.spec['properties']['template'], 'template_url': self.spec['properties']['template_url'], 'timeout_mins': self.spec['properties']['timeout'], 'disable_rollback': self.spec['properties']['disable_rollback'], 'parameters': self.spec['properties']['parameters'], 'files': self.spec['properties']['files'], 'environment': self.spec['properties']['environment'], 'tags': ",".join(['cluster_node_id=NODE_ID', 'cluster_id=CLUSTER_ID', 'cluster_node_index=123']) } oc.stack_create.assert_called_once_with(**call_args) self.assertEqual(0, oc.wait_for_stack.call_count)
def test_do_check(self): profile = stack.StackProfile('t', self.spec) heat_client = mock.Mock() test_stack = mock.Mock() test_stack.physical_id = 'ce8ae86c-9810-4cb1-8888-7fb53bc523bf' fake_stack = mock.Mock() fake_stack_complete = mock.Mock() fake_stack_complete.status = 'CHECK_COMPLETE' # Setup side effect of mock call to handle checking status # within while loop. 3rd call in while loop results in # fake_stack_complete mock object with a "complete" status. side_effect = [fake_stack, fake_stack, fake_stack_complete] # Check path where stack status can't be checked fake_stack.check = mock.MagicMock(side_effect=Exception()) heat_client.stack_get = mock.MagicMock(side_effect=side_effect) profile.heat = mock.MagicMock(return_value=heat_client) self.assertFalse(profile.do_check(test_stack)) self.assertTrue(profile.heat.called) self.assertTrue(heat_client.stack_get.called) self.assertTrue(fake_stack.check.called) # Check normal status path fake_stack.check = mock.MagicMock() fake_stack.status = 'CHECK_IN_PROGRESS' self.assertTrue(profile.do_check(test_stack)) self.assertEqual(2, profile.heat.call_count) self.assertEqual(3, heat_client.stack_get.call_count) self.assertTrue(fake_stack.check.called)
def test_do_update_only_params(self): profile = stack.StackProfile('t', self.spec) oc = mock.Mock() profile._orchestrationclient = oc stack_obj = mock.Mock(physical_id='FAKE_ID') new_spec = copy.deepcopy(self.spec) new_spec['properties']['parameters'] = {"new": "params"} new_profile = stack.StackProfile('u', new_spec) res = profile.do_update(stack_obj, new_profile) self.assertTrue(res) oc.stack_update.assert_called_once_with( 'FAKE_ID', parameters={"new": "params"}) oc.wait_for_stack.assert_called_once_with( 'FAKE_ID', 'UPDATE_COMPLETE', timeout=3600)
def test_do_create(self): oc = mock.Mock() profile = stack.StackProfile('t', self.spec) profile._orchestrationclient = oc node = mock.Mock(id='NODE_ID', cluster_id='CLUSTER_ID', index=123) node.name = 'test_node' fake_stack = mock.Mock(id='FAKE_ID') oc.stack_create = mock.Mock(return_value=fake_stack) # do it res = profile.do_create(node) # assertions kwargs = { 'stack_name': mock.ANY, 'template': self.spec['properties']['template'], 'template_url': self.spec['properties']['template_url'], 'timeout_mins': self.spec['properties']['timeout'], 'disable_rollback': self.spec['properties']['disable_rollback'], 'parameters': self.spec['properties']['parameters'], 'files': self.spec['properties']['files'], 'environment': self.spec['properties']['environment'], 'tags': ",".join(['cluster_node_id=NODE_ID', 'cluster_id=CLUSTER_ID', 'cluster_node_index=123']) } self.assertEqual('FAKE_ID', res) oc.stack_create.assert_called_once_with(**kwargs) oc.wait_for_stack.assert_called_once_with('FAKE_ID', 'CREATE_COMPLETE', timeout=3600)
def test_do_create_failed_wait(self): spec = copy.deepcopy(self.spec) del spec['properties']['timeout'] profile = stack.StackProfile('t', spec) oc = mock.Mock() stack_node = mock.Mock() stack_node.name = 'test_stack' fake_stack = mock.Mock(id='FAKE_ID') oc.stack_create = mock.Mock(return_value=fake_stack) err = exc.InternalError(code=400, message='Timeout') oc.wait_for_stack = mock.Mock(side_effect=err) profile._orchestrationclient = oc # do it ex = self.assertRaises(exc.EResourceCreation, profile.do_create, stack_node) # assertions self.assertEqual('Failed in creating stack: Timeout.', six.text_type(ex)) kwargs = { 'stack_name': mock.ANY, 'template': self.spec['properties']['template'], 'template_url': self.spec['properties']['template_url'], 'timeout_mins': None, 'disable_rollback': self.spec['properties']['disable_rollback'], 'parameters': self.spec['properties']['parameters'], 'files': self.spec['properties']['files'], 'environment': self.spec['properties']['environment'], } oc.stack_create.assert_called_once_with(**kwargs) oc.wait_for_stack.assert_called_once_with('FAKE_ID', 'CREATE_COMPLETE', timeout=None)
def test_refresh_tags_with_contents_no_add(self): profile = stack.StackProfile('t', self.spec) node = mock.Mock() res = profile._refresh_tags(['foo'], node, False) self.assertEqual(('foo', False), res)
def test_do_get_details_no_physical_id(self): profile = stack.StackProfile('t', self.spec) node_obj = mock.Mock(physical_id=None) res = profile.do_get_details(node_obj) self.assertEqual({}, res)
def test_refresh_tags_deleted_no_add(self): profile = stack.StackProfile('t', self.spec) node = mock.Mock() res = profile._refresh_tags(['cluster_id=FOO', 'bar'], node, False) self.assertEqual(('bar', True), res)
def test_do_validate_fails(self): oc = mock.Mock() profile = stack.StackProfile('t', self.spec) profile._orchestrationclient = oc err = exc.InternalError(code=400, message='Boom') oc.stack_create = mock.Mock(side_effect=err) node_obj = mock.Mock() node_obj.name = 'stack_node' ex = self.assertRaises(exc.InvalidSpec, profile.do_validate, node_obj) props = self.spec['properties'] call_args = { 'stack_name': mock.ANY, 'template': props['template'], 'template_url': props['template_url'], 'parameters': props['parameters'], 'files': props['files'], 'environment': props['environment'], 'preview': True, } oc.stack_create.assert_called_once_with(**call_args) self.assertEqual('Failed in validating template: Boom', six.text_type(ex))
def test_do_check_no_physical_id(self): node_obj = mock.Mock(physical_id=None) profile = stack.StackProfile('t', self.spec) res = profile.do_check(node_obj) self.assertFalse(res)
def test_do_leave_no_physical_id(self): profile = stack.StackProfile('t', self.spec) node = mock.Mock(physical_id=None) res = profile.do_leave(node) self.assertFalse(res)
def test_do_join_no_physical_id(self): profile = stack.StackProfile('t', self.spec) node = mock.Mock(physical_id=None) res = profile.do_join(node, 'CLUSTER_ID') self.assertFalse(res)
def test_do_create(self): oc = mock.Mock() profile = stack.StackProfile('t', self.spec) profile._orchestrationclient = oc test_stack = mock.Mock() test_stack.name = 'test_stack' fake_stack = mock.Mock(id='FAKE_ID') oc.stack_create = mock.Mock(return_value=fake_stack) # do it res = profile.do_create(test_stack) # assertions kwargs = { 'stack_name': mock.ANY, 'template': self.spec['properties']['template'], 'template_url': self.spec['properties']['template_url'], 'timeout_mins': self.spec['properties']['timeout'], 'disable_rollback': self.spec['properties']['disable_rollback'], 'parameters': self.spec['properties']['parameters'], 'files': self.spec['properties']['files'], 'environment': self.spec['properties']['environment'], } self.assertEqual('FAKE_ID', res) oc.stack_create.assert_called_once_with(**kwargs) oc.wait_for_stack.assert_called_once_with('FAKE_ID', 'CREATE_COMPLETE', timeout=3600)
def test_do_adopt_with_overrides(self): profile = stack.StackProfile('t', self.spec) x_stack = mock.Mock( parameters={'p1': 'v1', 'OS::stack_id': 'FAKE_ID'}, timeout_mins=123, disable_rollback=False ) oc = mock.Mock() oc.stack_get = mock.Mock(return_value=x_stack) oc.stack_get_template = mock.Mock(return_value={'foo': 'bar'}) oc.stack_get_environment = mock.Mock(return_value={'ke': 've'}) oc.stack_get_files = mock.Mock(return_value={'fn': 'content'}) profile._orchestrationclient = oc node_obj = mock.Mock(physical_id='FAKE_ID') overrides = {'environment': {'ENV': 'SETTING'}} res = profile.do_adopt(node_obj, overrides=overrides) expected = { 'environment': {'ENV': 'SETTING'}, 'files': {'fn': 'content'}, 'template': {'foo': 'bar'}, 'parameters': {'p1': 'v1'}, 'timeout': 123, 'disable_rollback': False } self.assertEqual(expected, res) oc.stack_get.assert_called_once_with('FAKE_ID') oc.stack_get_template.assert_called_once_with('FAKE_ID')
def test_refresh_tags_empty_no_add(self): profile = stack.StackProfile('t', self.spec) node = mock.Mock() res = profile._refresh_tags([], node, False) self.assertEqual(("", False), res)
def test_do_update_no_physical_stack(self): profile = stack.StackProfile('t', self.spec) test_stack = mock.Mock(physical_id=None) new_profile = mock.Mock() res = profile.do_update(test_stack, new_profile) self.assertFalse(res)
def test_do_validate(self): profile = stack.StackProfile('t', self.spec) profile.hc = mock.MagicMock() test_stack = mock.Mock() test_stack.name = 'test_stack' self.assertTrue(profile.do_validate(test_stack)) self.assertTrue(profile.hc.stacks.validate.called)
def test_do_update_with_timeout_value(self): profile = stack.StackProfile('t', self.spec) oc = mock.Mock() profile._orchestrationclient = oc stack_obj = mock.Mock(physical_id='FAKE_ID') new_spec = copy.deepcopy(self.spec) new_spec['properties']['timeout'] = 120 new_profile = stack.StackProfile('u', new_spec) # do it res = profile.do_update(stack_obj, new_profile) # assertions self.assertTrue(res) oc.stack_update.assert_called_once_with('FAKE_ID', timeout_mins=120) oc.wait_for_stack.assert_called_once_with( 'FAKE_ID', 'UPDATE_COMPLETE', timeout=3600)
def test__refresh_tags_empty_and_add(self): profile = stack.StackProfile('t', self.spec) node = mock.Mock(id='NODE_ID', cluster_id='CLUSTER_ID', index=123) res = profile._refresh_tags("", node, True) expected = ",".join(['cluster_id=CLUSTER_ID', 'cluster_node_id=NODE_ID', 'cluster_node_index=123']) self.assertEqual((expected, True), res)
def test_do_update_failed_update(self): profile = stack.StackProfile('t', self.spec) oc = mock.Mock() profile._orchestrationclient = oc oc.stack_update = mock.Mock( side_effect=exc.InternalError(code=400, message='Failed')) stack_obj = mock.Mock(physical_id='FAKE_ID') new_spec = copy.deepcopy(self.spec) new_spec['properties']['environment'] = {"new": "env1"} new_profile = stack.StackProfile('u', new_spec) ex = self.assertRaises(exc.EResourceUpdate, profile.do_update, stack_obj, new_profile) oc.stack_update.assert_called_once_with('FAKE_ID', environment={"new": "env1"}) self.assertEqual(0, oc.wait_for_stack.call_count) self.assertEqual("Failed in updating stack 'FAKE_ID': " "Failed.", str(ex))
def test_do_get_details_failed_retrieval(self): profile = stack.StackProfile('t', self.spec) node_obj = mock.Mock(physical_id='STACK_ID') oc = mock.Mock() oc.stack_get.side_effect = exc.InternalError(message='BOOM') profile._orchestrationclient = oc res = profile.do_get_details(node_obj) self.assertEqual({'Error': {'code': 500, 'message': 'BOOM'}}, res) oc.stack_get.assert_called_once_with('STACK_ID')
def test_do_delete(self): profile = stack.StackProfile('t', self.spec) test_stack = mock.Mock() test_stack.physical_id = 'ce8ae86c-9810-4cb1-8888-7fb53bc523bf' profile.hc = mock.MagicMock() profile._check_action_complete = mock.MagicMock(return_value=True) profile.hc.stack_delete = mock.MagicMock() self.assertTrue(profile.do_delete(test_stack)) self.assertTrue(profile.hc.stack_delete.called) self.assertTrue(profile.hc.wait_for_stack_delete.called)
def test_do_join_failed_get_stack(self): profile = stack.StackProfile('t', self.spec) oc = mock.Mock() profile._orchestrationclient = oc err = exc.InternalError(code=400, message='Boom') oc.stack_get.side_effect = err node = mock.Mock(physical_id='STACK_ID') res = profile.do_join(node, 'CLUSTER_ID') self.assertFalse(res) oc.stack_get.assert_called_once_with('STACK_ID')
def test_do_adopt_failed_get(self): profile = stack.StackProfile('t', self.spec) oc = mock.Mock() oc.stack_get.side_effect = exc.InternalError(message='BOOM') profile._orchestrationclient = oc node_obj = mock.Mock(physical_id='FAKE_ID') res = profile.do_adopt(node_obj) expected = {'Error': {'code': 500, 'message': 'BOOM'}} self.assertEqual(expected, res) oc.stack_get.assert_called_once_with('FAKE_ID')