diff options
Diffstat (limited to 'tests/unit/orchestrator')
-rw-r--r-- | tests/unit/orchestrator/test_heat.py | 70 | ||||
-rw-r--r-- | tests/unit/orchestrator/test_kubernetes.py | 110 |
2 files changed, 144 insertions, 36 deletions
diff --git a/tests/unit/orchestrator/test_heat.py b/tests/unit/orchestrator/test_heat.py index 3b3873301..c127dd0c9 100644 --- a/tests/unit/orchestrator/test_heat.py +++ b/tests/unit/orchestrator/test_heat.py @@ -11,6 +11,7 @@ # Unittest for yardstick.benchmark.orchestrator.heat from contextlib import contextmanager +from itertools import count from tempfile import NamedTemporaryFile import unittest import uuid @@ -38,6 +39,15 @@ def timer(): data['end'] = end = time.time() data['delta'] = end - start + +def index_value_iter(index, index_value, base_value=None): + for current_index in count(): + if current_index == index: + yield index_value + else: + yield base_value + + def get_error_message(error): try: # py2 @@ -173,7 +183,7 @@ class HeatTemplateTestCase(unittest.TestCase): @mock_patch_target_module('op_utils') @mock_patch_target_module('heatclient.client.Client') def test_create_negative(self, mock_heat_client_class, mock_op_utils): - self.template.HEAT_WAIT_LOOP_INTERVAL = interval = 0.2 + self.template.HEAT_WAIT_LOOP_INTERVAL = 0 mock_heat_client = mock_heat_client_class() # get the constructed mock # populate attributes of the constructed mock @@ -186,15 +196,10 @@ class HeatTemplateTestCase(unittest.TestCase): with mock.patch.object(self.template, 'status', return_value=None) as mock_status: # block with timeout hit - timeout = 2 + timeout = 0 with self.assertRaises(RuntimeError) as raised, timer() as time_data: self.template.create(block=True, timeout=timeout) - # ensure runtime is approximately the timeout value - expected_time_low = timeout - interval * 0.2 - expected_time_high = timeout + interval * 0.2 - self.assertTrue(expected_time_low < time_data['delta'] < expected_time_high) - # ensure op_utils was used expected_op_utils_usage += 1 self.assertEqual(mock_op_utils.get_session.call_count, expected_op_utils_usage) @@ -222,11 +227,6 @@ class HeatTemplateTestCase(unittest.TestCase): with self.assertRaises(RuntimeError) as raised, timer() as time_data: self.template.create(block=True, timeout=timeout) - # ensure runtime is approximately two intervals - expected_time_low = interval * 1.8 - expected_time_high = interval * 2.2 - self.assertTrue(expected_time_low < time_data['delta'] < expected_time_high) - # ensure the existing heat_client was used and op_utils was used again self.assertEqual(mock_op_utils.get_session.call_count, expected_op_utils_usage) self.assertEqual(mock_op_utils.get_endpoint.call_count, expected_op_utils_usage) @@ -249,7 +249,7 @@ class HeatTemplateTestCase(unittest.TestCase): @mock_patch_target_module('op_utils') @mock_patch_target_module('heatclient.client.Client') def test_create(self, mock_heat_client_class, mock_op_utils): - self.template.HEAT_WAIT_LOOP_INTERVAL = interval = 0.2 + self.template.HEAT_WAIT_LOOP_INTERVAL = 0.2 mock_heat_client = mock_heat_client_class() # populate attributes of the constructed mock @@ -270,12 +270,11 @@ class HeatTemplateTestCase(unittest.TestCase): expected_op_utils_usage = 0 with mock.patch.object(self.template, 'status') as mock_status: - # no block - with timer() as time_data: - self.assertIsInstance(self.template.create(block=False, timeout=2), heat.HeatStack) + self.template.name = 'no block test' + mock_status.return_value = None - # ensure runtime is much less than one interval - self.assertLess(time_data['delta'], interval * 0.2) + # no block + self.assertIsInstance(self.template.create(block=False, timeout=2), heat.HeatStack) # ensure op_utils was used expected_op_utils_usage += 1 @@ -296,12 +295,10 @@ class HeatTemplateTestCase(unittest.TestCase): self.assertEqual(self.template.outputs, {}) # block with immediate complete - mock_status.return_value = u'CREATE_COMPLETE' - with timer() as time_data: - self.assertIsInstance(self.template.create(block=True, timeout=2), heat.HeatStack) + self.template.name = 'block, immediate complete test' - # ensure runtime is less than one interval - self.assertLess(time_data['delta'], interval * 0.2) + mock_status.return_value = self.template.HEAT_CREATE_COMPLETE_STATUS + self.assertIsInstance(self.template.create(block=True, timeout=2), heat.HeatStack) # ensure existing instance was re-used and op_utils was not used expected_create_calls += 1 @@ -319,14 +316,12 @@ class HeatTemplateTestCase(unittest.TestCase): self.template.outputs = None # block with delayed complete - mock_status.side_effect = iter([None, None, u'CREATE_COMPLETE']) - with timer() as time_data: - self.assertIsInstance(self.template.create(block=True, timeout=2), heat.HeatStack) + self.template.name = 'block, delayed complete test' - # ensure runtime is approximately two intervals - expected_time_low = interval * 1.8 - expected_time_high = interval * 2.2 - self.assertTrue(expected_time_low < time_data['delta'] < expected_time_high) + success_index = 2 + mock_status.side_effect = index_value_iter(success_index, + self.template.HEAT_CREATE_COMPLETE_STATUS) + self.assertIsInstance(self.template.create(block=True, timeout=2), heat.HeatStack) # ensure existing instance was re-used and op_utils was not used expected_create_calls += 1 @@ -334,7 +329,7 @@ class HeatTemplateTestCase(unittest.TestCase): self.assertEqual(mock_heat_client.stacks.create.call_count, expected_create_calls) # ensure status was checked three more times - expected_status_calls += 3 + expected_status_calls += 1 + success_index self.assertEqual(mock_status.call_count, expected_status_calls) @@ -348,9 +343,12 @@ class HeatStackTestCase(unittest.TestCase): # call once and then call again if uuid is not none self.assertGreater(delete_mock.call_count, 1) - def test_delete_all_calls_delete(self): - stack = heat.HeatStack('test') - stack.uuid = 1 - with mock.patch.object(stack, "delete") as delete_mock: + @mock.patch('yardstick.orchestrator.heat.op_utils') + def test_delete_all_calls_delete(self, mock_op): + # we must patch the object before we create an instance + # so we can override delete() in all the instances + with mock.patch.object(heat.HeatStack, "delete") as delete_mock: + stack = heat.HeatStack('test') + stack.uuid = 1 stack.delete_all() - self.assertGreater(delete_mock.call_count, 0) + self.assertGreater(delete_mock.call_count, 0) diff --git a/tests/unit/orchestrator/test_kubernetes.py b/tests/unit/orchestrator/test_kubernetes.py new file mode 100644 index 000000000..51718ab86 --- /dev/null +++ b/tests/unit/orchestrator/test_kubernetes.py @@ -0,0 +1,110 @@ +#!/usr/bin/env python + +############################################################################## +# Copyright (c) 2017 Intel Corporation +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +# Unittest for yardstick.benchmark.orchestrator.heat +import unittest +import mock + +from yardstick.orchestrator.kubernetes import KubernetesObject +from yardstick.orchestrator.kubernetes import KubernetesTemplate + + +class GetTemplateTestCase(unittest.TestCase): + + def test_get_template(self): + output_t = { + "apiVersion": "v1", + "kind": "ReplicationController", + "metadata": { + "name": "host-k8s-86096c30" + }, + "spec": { + "replicas": 1, + "template": { + "metadata": { + "labels": { + "app": "host-k8s-86096c30" + } + }, + "spec": { + "containers": [ + { + "args": [ + "-c", + "chmod 700 ~/.ssh; chmod 600 ~/.ssh/*; \ +service ssh restart;while true ; do sleep 10000; done" + ], + "command": [ + "/bin/bash" + ], + "image": "openretriever/yardstick", + "name": "host-k8s-86096c30-container", + "volumeMounts": [ + { + "mountPath": "/root/.ssh/", + "name": "k8s-86096c30-key" + } + ] + } + ], + "volumes": [ + { + "configMap": { + "name": "k8s-86096c30-key" + }, + "name": "k8s-86096c30-key" + } + ] + } + } + } + } + input_s = { + 'command': '/bin/bash', + 'args': ['-c', 'chmod 700 ~/.ssh; chmod 600 ~/.ssh/*; \ +service ssh restart;while true ; do sleep 10000; done'], + 'ssh_key': 'k8s-86096c30-key' + } + name = 'host-k8s-86096c30' + output_r = KubernetesObject(name, **input_s).get_template() + self.assertEqual(output_r, output_t) + + +class GetRcPodsTestCase(unittest.TestCase): + + @mock.patch('yardstick.orchestrator.kubernetes.k8s_utils.get_pod_list') + def test_get_rc_pods(self, mock_get_pod_list): + servers = { + 'host': { + 'image': 'openretriever/yardstick', + 'command': '/bin/bash', + 'args': ['-c', 'chmod 700 ~/.ssh; chmod 600 ~/.ssh/*; \ +service ssh restart;while true ; do sleep 10000; done'] + }, + 'target': { + 'image': 'openretriever/yardstick', + 'command': '/bin/bash', + 'args': ['-c', 'chmod 700 ~/.ssh; chmod 600 ~/.ssh/*; \ +service ssh restart;while true ; do sleep 10000; done'] + } + } + k8s_template = KubernetesTemplate('k8s-86096c30', servers) + mock_get_pod_list.return_value.items = [] + pods = k8s_template.get_rc_pods() + self.assertEqual(pods, []) + + +def main(): + unittest.main() + + +if __name__ == '__main__': + main() |