diff options
author | Rodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com> | 2018-06-25 11:19:04 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@opnfv.org> | 2018-06-25 11:19:04 +0000 |
commit | b935c3228523c30dacdaba551ecee3703ff78168 (patch) | |
tree | 4f24f8430239c8cb378518dc81855d04b70be609 /yardstick/tests | |
parent | 9d84f5ad73e9689d3131e4ce09fe7262697c5817 (diff) | |
parent | 55d5d9245c615436921167e24c5e49a59c53f590 (diff) |
Merge "Add "volumes" parameter in Kubernetes context"
Diffstat (limited to 'yardstick/tests')
-rw-r--r-- | yardstick/tests/unit/benchmark/contexts/test_kubernetes.py | 26 | ||||
-rw-r--r-- | yardstick/tests/unit/orchestrator/test_kubernetes.py | 61 |
2 files changed, 71 insertions, 16 deletions
diff --git a/yardstick/tests/unit/benchmark/contexts/test_kubernetes.py b/yardstick/tests/unit/benchmark/contexts/test_kubernetes.py index 0e11a53e1..0d698c5bd 100644 --- a/yardstick/tests/unit/benchmark/contexts/test_kubernetes.py +++ b/yardstick/tests/unit/benchmark/contexts/test_kubernetes.py @@ -12,9 +12,10 @@ import unittest from yardstick.benchmark.contexts import base from yardstick.benchmark.contexts import kubernetes +from yardstick.orchestrator import kubernetes as orchestrator_kubernetes -context_cfg = { +CONTEXT_CFG = { 'type': 'Kubernetes', 'name': 'k8s', 'task_id': '1234567890', @@ -22,14 +23,14 @@ context_cfg = { 'host': { 'image': 'openretriever/yardstick', 'command': '/bin/bash', - 'args': ['-c', 'chmod 700 ~/.ssh; chmod 600 ~/.ssh/*; \ -service ssh restart;while true ; do sleep 10000; done'] + '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'] + 'args': ['-c', 'chmod 700 ~/.ssh; chmod 600 ~/.ssh/*; ' + 'service ssh restart;while true ; do sleep 10000; done'] } } } @@ -42,7 +43,7 @@ class KubernetesTestCase(unittest.TestCase): def setUp(self): self.k8s_context = kubernetes.KubernetesContext() self.addCleanup(self._remove_contexts) - self.k8s_context.init(context_cfg) + self.k8s_context.init(CONTEXT_CFG) @staticmethod def _remove_contexts(): @@ -68,7 +69,8 @@ class KubernetesTestCase(unittest.TestCase): @mock.patch.object(kubernetes.KubernetesContext, '_create_services') @mock.patch.object(kubernetes.KubernetesContext, '_wait_until_running') - @mock.patch.object(kubernetes.KubernetesTemplate, 'get_rc_pods') + @mock.patch.object(orchestrator_kubernetes.KubernetesTemplate, + 'get_rc_pods') @mock.patch.object(kubernetes.KubernetesContext, '_create_rcs') @mock.patch.object(kubernetes.KubernetesContext, '_set_ssh_key') def test_deploy(self, @@ -170,3 +172,13 @@ class KubernetesTestCase(unittest.TestCase): def test_delete_services(self, mock_delete): self.k8s_context._delete_services() mock_delete.assert_called() + + def test_init(self): + self.k8s_context._delete_context() + with mock.patch.object(orchestrator_kubernetes, 'KubernetesTemplate', + return_value='fake_template') as mock_k8stemplate: + self.k8s_context = kubernetes.KubernetesContext() + self.k8s_context.init(CONTEXT_CFG) + mock_k8stemplate.assert_called_once_with(self.k8s_context.name, + CONTEXT_CFG) + self.assertEqual('fake_template', self.k8s_context.template) diff --git a/yardstick/tests/unit/orchestrator/test_kubernetes.py b/yardstick/tests/unit/orchestrator/test_kubernetes.py index 58971f515..e3e5516ca 100644 --- a/yardstick/tests/unit/orchestrator/test_kubernetes.py +++ b/yardstick/tests/unit/orchestrator/test_kubernetes.py @@ -7,15 +7,15 @@ # 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 +from yardstick.common import exceptions +from yardstick.common import kubernetes_utils +from yardstick.orchestrator import kubernetes +from yardstick.tests.unit import base -class GetTemplateTestCase(unittest.TestCase): +class GetTemplateTestCase(base.BaseUnitTestCase): def test_get_template(self): output_t = { @@ -73,14 +73,15 @@ service ssh restart;while true ; do sleep 10000; done" 'args': ['-c', 'chmod 700 ~/.ssh; chmod 600 ~/.ssh/*; \ service ssh restart;while true ; do sleep 10000; done'], 'ssh_key': 'k8s-86096c30-key', - 'nodeSelector': {'kubernetes.io/hostname': 'node-01'} + 'nodeSelector': {'kubernetes.io/hostname': 'node-01'}, + 'volumes': [] } name = 'host-k8s-86096c30' - output_r = KubernetesObject(name, **input_s).get_template() + output_r = kubernetes.KubernetesObject(name, **input_s).get_template() self.assertEqual(output_r, output_t) -class GetRcPodsTestCase(unittest.TestCase): +class GetRcPodsTestCase(base.BaseUnitTestCase): @mock.patch('yardstick.orchestrator.kubernetes.k8s_utils.get_pod_list') def test_get_rc_pods(self, mock_get_pod_list): @@ -98,7 +99,49 @@ service ssh restart;while true ; do sleep 10000; done'] service ssh restart;while true ; do sleep 10000; done'] } } - k8s_template = KubernetesTemplate('k8s-86096c30', servers) + k8s_template = kubernetes.KubernetesTemplate('k8s-86096c30', servers) mock_get_pod_list.return_value.items = [] pods = k8s_template.get_rc_pods() self.assertEqual(pods, []) + + +class KubernetesObjectTestCase(base.BaseUnitTestCase): + + def test__add_volumes(self): + volume1 = {'name': 'fake_sshkey', + 'configMap': {'name': 'fake_sshkey'}} + volume2 = {'name': 'volume2', + 'configMap': 'data'} + k8s_obj = kubernetes.KubernetesObject('name', ssh_key='fake_sshkey', + volumes=[volume2]) + k8s_obj._add_volumes() + volumes = k8s_obj.template['spec']['template']['spec']['volumes'] + self.assertEqual(sorted([volume1, volume2], key=lambda k: k['name']), + sorted(volumes, key=lambda k: k['name'])) + + def test__add_volumes_no_volumes(self): + volume1 = {'name': 'fake_sshkey', + 'configMap': {'name': 'fake_sshkey'}} + k8s_obj = kubernetes.KubernetesObject('name', ssh_key='fake_sshkey') + k8s_obj._add_volumes() + volumes = k8s_obj.template['spec']['template']['spec']['volumes'] + self.assertEqual([volume1], volumes) + + def test__create_ssh_key_volume(self): + expected = {'name': 'fake_sshkey', + 'configMap': {'name': 'fake_sshkey'}} + k8s_obj = kubernetes.KubernetesObject('name', ssh_key='fake_sshkey') + self.assertEqual(expected, k8s_obj._create_ssh_key_volume()) + + def test__create_volume(self): + for vol_type in kubernetes_utils.get_volume_types(): + volume = {'name': 'vol_name', + vol_type: 'data'} + self.assertEqual( + volume, kubernetes.KubernetesObject._create_volume(volume)) + + def test__create_volume_invalid_type(self): + volume = {'name': 'vol_name', + 'invalid_type': 'data'} + with self.assertRaises(exceptions.KubernetesTemplateInvalidVolumeType): + kubernetes.KubernetesObject._create_volume(volume) |