Resource optimization service for OpenStack.
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.

119 lines
4.8KB

  1. # -*- encoding: utf-8 -*-
  2. # Copyright (c) 2015 Intel Corp
  3. #
  4. # Authors: Zhenzan Zhou <zhenzan.zhou@intel.com>
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  15. # implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. #
  19. import collections
  20. import mock
  21. from watcher.applier.loading import default
  22. from watcher.common import utils
  23. from watcher.decision_engine.strategy import strategies
  24. from watcher.tests.decision_engine.model import ceilometer_metrics
  25. from watcher.tests.decision_engine.model import gnocchi_metrics
  26. from watcher.tests.decision_engine.strategy.strategies.test_base \
  27. import TestBaseStrategy
  28. class TestOutletTempControl(TestBaseStrategy):
  29. scenarios = [
  30. ("Ceilometer",
  31. {"datasource": "ceilometer",
  32. "fake_datasource_cls": ceilometer_metrics.FakeCeilometerMetrics}),
  33. ("Gnocchi",
  34. {"datasource": "gnocchi",
  35. "fake_datasource_cls": gnocchi_metrics.FakeGnocchiMetrics}),
  36. ]
  37. def setUp(self):
  38. super(TestOutletTempControl, self).setUp()
  39. # fake metrics
  40. self.fake_metrics = self.fake_datasource_cls()
  41. p_datasource = mock.patch.object(
  42. strategies.OutletTempControl, 'datasource_backend',
  43. new_callable=mock.PropertyMock)
  44. self.m_datasource = p_datasource.start()
  45. self.addCleanup(p_datasource.stop)
  46. self.m_datasource.return_value = mock.Mock(
  47. statistic_aggregation=self.fake_metrics.mock_get_statistics,
  48. NAME=self.fake_metrics.NAME)
  49. self.strategy = strategies.OutletTempControl(
  50. config=mock.Mock(datasource=self.datasource))
  51. self.strategy.input_parameters = utils.Struct()
  52. self.strategy.input_parameters.update({'threshold': 34.3})
  53. self.strategy.threshold = 34.3
  54. def test_group_hosts_by_outlet_temp(self):
  55. model = self.fake_c_cluster.generate_scenario_3_with_2_nodes()
  56. self.m_c_model.return_value = model
  57. n1, n2 = self.strategy.group_hosts_by_outlet_temp()
  58. self.assertEqual("af69c544-906b-4a6a-a9c6-c1f7a8078c73",
  59. n1[0]['compute_node'].uuid)
  60. self.assertEqual("fa69c544-906b-4a6a-a9c6-c1f7a8078c73",
  61. n2[0]['compute_node'].uuid)
  62. def test_choose_instance_to_migrate(self):
  63. model = self.fake_c_cluster.generate_scenario_3_with_2_nodes()
  64. self.m_c_model.return_value = model
  65. n1, n2 = self.strategy.group_hosts_by_outlet_temp()
  66. instance_to_mig = self.strategy.choose_instance_to_migrate(n1)
  67. self.assertEqual('af69c544-906b-4a6a-a9c6-c1f7a8078c73',
  68. instance_to_mig[0].uuid)
  69. self.assertEqual('a4cab39b-9828-413a-bf88-f76921bf1517',
  70. instance_to_mig[1].uuid)
  71. def test_filter_dest_servers(self):
  72. model = self.fake_c_cluster.generate_scenario_3_with_2_nodes()
  73. self.m_c_model.return_value = model
  74. n1, n2 = self.strategy.group_hosts_by_outlet_temp()
  75. instance_to_mig = self.strategy.choose_instance_to_migrate(n1)
  76. dest_hosts = self.strategy.filter_dest_servers(n2, instance_to_mig[1])
  77. self.assertEqual(1, len(dest_hosts))
  78. self.assertEqual("fa69c544-906b-4a6a-a9c6-c1f7a8078c73",
  79. dest_hosts[0]['compute_node'].uuid)
  80. def test_execute_no_workload(self):
  81. model = self.fake_c_cluster.\
  82. generate_scenario_4_with_1_node_no_instance()
  83. self.m_c_model.return_value = model
  84. solution = self.strategy.execute()
  85. self.assertEqual([], solution.actions)
  86. def test_execute(self):
  87. model = self.fake_c_cluster.generate_scenario_3_with_2_nodes()
  88. self.m_c_model.return_value = model
  89. solution = self.strategy.execute()
  90. actions_counter = collections.Counter(
  91. [action.get('action_type') for action in solution.actions])
  92. num_migrations = actions_counter.get("migrate", 0)
  93. self.assertEqual(1, num_migrations)
  94. def test_check_parameters(self):
  95. model = self.fake_c_cluster.generate_scenario_3_with_2_nodes()
  96. self.m_c_model.return_value = model
  97. solution = self.strategy.execute()
  98. loader = default.DefaultActionLoader()
  99. for action in solution.actions:
  100. loaded_action = loader.load(action['action_type'])
  101. loaded_action.input_parameters = action['input_parameters']
  102. loaded_action.validate_parameters()