A Python library for code common to TripleO CLI and TripleO UI.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
5.5KB

  1. # Copyright 2016 Red Hat, Inc.
  2. # All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. # not use this file except in compliance with the License. You may obtain
  6. # a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations
  14. # under the License.
  15. import collections
  16. import mock
  17. import yaml
  18. from swiftclient import exceptions as swiftexceptions
  19. from tripleo_common.actions import scale
  20. from tripleo_common import constants
  21. from tripleo_common.tests import base
  22. def mock_stack():
  23. stack = mock.Mock()
  24. stack.name = 'My Stack'
  25. stack.parameters = {'ComputeCount': '2'}
  26. stack.to_dict.return_value = {
  27. 'uuid': 5,
  28. 'name': 'My Stack',
  29. 'parameters': stack.parameters,
  30. }
  31. return stack
  32. class ScaleDownActionTest(base.TestCase):
  33. def setUp(self):
  34. super(ScaleDownActionTest, self).setUp()
  35. self.image = collections.namedtuple('image', ['id'])
  36. @mock.patch('tripleo_common.actions.base.TripleOAction.'
  37. 'cache_delete')
  38. @mock.patch('tripleo_common.actions.base.TripleOAction.'
  39. 'get_orchestration_client')
  40. @mock.patch('heatclient.common.template_utils.'
  41. 'process_multiple_environments_and_files')
  42. @mock.patch('heatclient.common.template_utils.get_template_contents')
  43. @mock.patch('tripleo_common.actions.base.TripleOAction.get_object_client')
  44. def test_run(self, mock_get_object_client,
  45. mock_get_template_contents, mock_env_files,
  46. mock_get_heat_client, mock_cache):
  47. mock_env_files.return_value = ({}, {})
  48. heatclient = mock.MagicMock()
  49. heatclient.resources.list.return_value = [
  50. mock.MagicMock(
  51. links=[{'rel': 'stack',
  52. 'href': 'http://192.0.2.1:8004/v1/'
  53. 'a959ac7d6a4a475daf2428df315c41ef/'
  54. 'stacks/overcloud/123'}],
  55. logical_resource_id='logical_id',
  56. physical_resource_id='resource_id',
  57. resource_type='OS::Heat::ResourceGroup',
  58. resource_name='Compute'
  59. ),
  60. mock.MagicMock(
  61. links=[{'rel': 'stack',
  62. 'href': 'http://192.0.2.1:8004/v1/'
  63. 'a959ac7d6a4a475daf2428df315c41ef/'
  64. 'stacks/overcloud/124'}],
  65. logical_resource_id='node0',
  66. physical_resource_id='123',
  67. resource_type='OS::TripleO::Compute',
  68. parent_resource='Compute',
  69. resource_name='node0',
  70. )
  71. ]
  72. heatclient.stacks.get.return_value = mock_stack()
  73. heatclient.stacks.validate.return_value = {}
  74. mock_get_heat_client.return_value = heatclient
  75. mock_ctx = mock.MagicMock()
  76. swift = mock.MagicMock(url="http://test.com")
  77. mock_env = yaml.safe_dump({
  78. 'name': 'overcloud',
  79. 'temp_environment': 'temp_environment',
  80. 'template': 'template',
  81. 'environments': [{u'path': u'environments/test.yaml'}]
  82. }, default_flow_style=False)
  83. mock_roles = yaml.safe_dump([{"name": "foo"}])
  84. mock_network = yaml.safe_dump([{'enabled': False}])
  85. mock_exclude = yaml.safe_dump({"name": "foo"})
  86. swift.get_object.side_effect = (
  87. ({}, mock_env),
  88. ({}, mock_env),
  89. ({}, mock_roles),
  90. ({}, mock_network),
  91. ({}, mock_exclude),
  92. ({}, mock_env),
  93. ({}, mock_env),
  94. ({}, mock_env),
  95. ({}, mock_roles),
  96. ({}, mock_network),
  97. ({}, mock_exclude),
  98. ({}, mock_env),
  99. ({}, mock_env),
  100. swiftexceptions.ClientException('atest2')
  101. )
  102. def return_container_files(*args):
  103. return ('headers', [{'name': 'foo.role.j2.yaml'}])
  104. swift.get_container = mock.MagicMock(
  105. side_effect=return_container_files)
  106. mock_get_object_client.return_value = swift
  107. env = {
  108. 'resource_registry': {
  109. 'resources': {'*': {'*': {'UpdateDeployment': {'hooks': []}}}}
  110. }
  111. }
  112. mock_get_template_contents.return_value = ({}, {
  113. 'heat_template_version': '2016-04-30'
  114. })
  115. # Test
  116. action = scale.ScaleDownAction(
  117. constants.STACK_TIMEOUT_DEFAULT, ['resource_id'], 'stack')
  118. action.run(mock_ctx)
  119. heatclient.stacks.validate.assert_called_once_with(
  120. environment=env,
  121. files={},
  122. show_nested=True,
  123. template={'heat_template_version': '2016-04-30'}
  124. )
  125. clear_list = list(['ComputeCount', 'ComputeRemovalPolicies',
  126. 'ComputeRemovalPoliciesMode'])
  127. _, kwargs = heatclient.stacks.update.call_args
  128. self.assertEqual(set(kwargs['clear_parameters']), set(clear_list))
  129. self.assertEqual(kwargs['environment'], env)
  130. self.assertEqual(kwargs['existing'], True)
  131. self.assertEqual(kwargs['files'], {})
  132. mock_cache.assert_called_with(
  133. mock_ctx,
  134. "stack",
  135. "tripleo.parameters.get"
  136. )