diff options
-rw-r--r-- | yardstick/common/exceptions.py | 4 | ||||
-rw-r--r-- | yardstick/orchestrator/kubernetes.py | 16 | ||||
-rw-r--r-- | yardstick/tests/unit/orchestrator/test_kubernetes.py | 28 |
3 files changed, 38 insertions, 10 deletions
diff --git a/yardstick/common/exceptions.py b/yardstick/common/exceptions.py index 1f749adc1..cbb294989 100644 --- a/yardstick/common/exceptions.py +++ b/yardstick/common/exceptions.py @@ -267,6 +267,10 @@ class KubernetesContainerWrongImagePullPolicy(YardstickException): message = 'Image pull policy must be "Always", "IfNotPresent" or "Never"' +class KubernetesContainerCommandType(YardstickException): + message = '"args" and "command" must be string or list of strings' + + class ScenarioCreateNetworkError(YardstickException): message = 'Create Neutron Network Scenario failed' diff --git a/yardstick/orchestrator/kubernetes.py b/yardstick/orchestrator/kubernetes.py index bee4d4a78..3e2572fcc 100644 --- a/yardstick/orchestrator/kubernetes.py +++ b/yardstick/orchestrator/kubernetes.py @@ -11,6 +11,7 @@ import copy import re from oslo_serialization import jsonutils +import six from yardstick.common import constants from yardstick.common import exceptions @@ -22,7 +23,7 @@ class ContainerObject(object): SSH_MOUNT_PATH = '/tmp/.ssh/' IMAGE_DEFAULT = 'openretriever/yardstick' - COMMAND_DEFAULT = '/bin/bash' + COMMAND_DEFAULT = ['/bin/bash', '-c'] RESOURCES = ('requests', 'limits') PORT_OPTIONS = ('containerPort', 'hostIP', 'hostPort', 'name', 'protocol') IMAGE_PULL_POLICY = ('Always', 'IfNotPresent', 'Never') @@ -31,8 +32,9 @@ class ContainerObject(object): self._name = name self._ssh_key = ssh_key self._image = kwargs.get('image', self.IMAGE_DEFAULT) - self._command = [kwargs.get('command', self.COMMAND_DEFAULT)] - self._args = kwargs.get('args', []) + self._command = self._parse_commands( + kwargs.get('command', self.COMMAND_DEFAULT)) + self._args = self._parse_commands(kwargs.get('args', [])) self._volume_mounts = kwargs.get('volumeMounts', []) self._security_context = kwargs.get('securityContext') self._env = kwargs.get('env', []) @@ -40,6 +42,14 @@ class ContainerObject(object): self._ports = kwargs.get('ports', []) self._image_pull_policy = kwargs.get('imagePullPolicy') + @staticmethod + def _parse_commands(command): + if isinstance(command, six.string_types): + return [command] + elif isinstance(command, list): + return command + raise exceptions.KubernetesContainerCommandType() + def _create_volume_mounts(self): """Return all "volumeMounts" items per container""" volume_mounts_items = [self._create_volume_mounts_item(vol) diff --git a/yardstick/tests/unit/orchestrator/test_kubernetes.py b/yardstick/tests/unit/orchestrator/test_kubernetes.py index 1317308cc..be9c2ae24 100644 --- a/yardstick/tests/unit/orchestrator/test_kubernetes.py +++ b/yardstick/tests/unit/orchestrator/test_kubernetes.py @@ -300,7 +300,7 @@ class ContainerObjectTestCase(base.BaseUnitTestCase): 'cname', ssh_key='fake_sshkey', volumeMount=[volume_mount], args=args) expected = {'args': args, - 'command': [kubernetes.ContainerObject.COMMAND_DEFAULT], + 'command': kubernetes.ContainerObject.COMMAND_DEFAULT, 'image': kubernetes.ContainerObject.IMAGE_DEFAULT, 'name': 'cname-container', 'volumeMounts': container_obj._create_volume_mounts()} @@ -314,7 +314,7 @@ class ContainerObjectTestCase(base.BaseUnitTestCase): 'cname', ssh_key='fake_sshkey', volumeMount=[volume_mount], args=args, securityContext={'key': 'value'}) expected = {'args': args, - 'command': [kubernetes.ContainerObject.COMMAND_DEFAULT], + 'command': kubernetes.ContainerObject.COMMAND_DEFAULT, 'image': kubernetes.ContainerObject.IMAGE_DEFAULT, 'name': 'cname-container', 'volumeMounts': container_obj._create_volume_mounts(), @@ -330,7 +330,7 @@ class ContainerObjectTestCase(base.BaseUnitTestCase): args=args, env=[{'name': 'fake_var_name', 'value': 'fake_var_value'}]) expected = {'args': args, - 'command': [kubernetes.ContainerObject.COMMAND_DEFAULT], + 'command': kubernetes.ContainerObject.COMMAND_DEFAULT, 'image': kubernetes.ContainerObject.IMAGE_DEFAULT, 'name': 'cname-container', 'volumeMounts': container_obj._create_volume_mounts(), @@ -351,8 +351,7 @@ class ContainerObjectTestCase(base.BaseUnitTestCase): 'invalid_varible': 'fakeinvalid_varible', 'hostIP': 'fake_port_number'}]) expected = {'args': args, - 'command': [ - kubernetes.ContainerObject.COMMAND_DEFAULT], + 'command': kubernetes.ContainerObject.COMMAND_DEFAULT, 'image': kubernetes.ContainerObject.IMAGE_DEFAULT, 'name': 'cname-container', 'volumeMounts': container_obj._create_volume_mounts(), @@ -387,7 +386,7 @@ class ContainerObjectTestCase(base.BaseUnitTestCase): 'cname', ssh_key='fake_sshkey', volumeMount=[volume_mount], args=args, resources=resources) expected = {'args': args, - 'command': [kubernetes.ContainerObject.COMMAND_DEFAULT], + 'command': kubernetes.ContainerObject.COMMAND_DEFAULT, 'image': kubernetes.ContainerObject.IMAGE_DEFAULT, 'name': 'cname-container', 'volumeMounts': container_obj._create_volume_mounts(), @@ -399,13 +398,28 @@ class ContainerObjectTestCase(base.BaseUnitTestCase): container_obj = kubernetes.ContainerObject( 'cname', ssh_key='fake_sshkey', imagePullPolicy='Always') expected = {'args': [], - 'command': [kubernetes.ContainerObject.COMMAND_DEFAULT], + 'command': kubernetes.ContainerObject.COMMAND_DEFAULT, 'image': kubernetes.ContainerObject.IMAGE_DEFAULT, 'name': 'cname-container', 'volumeMounts': container_obj._create_volume_mounts(), 'imagePullPolicy':'Always'} self.assertEqual(expected, container_obj.get_container_item()) + def test__parse_commands_string(self): + container_obj = kubernetes.ContainerObject('cname', 'fake_sshkey') + self.assertEqual(['fake command'], + container_obj._parse_commands('fake command')) + + def test__parse_commands_list(self): + container_obj = kubernetes.ContainerObject('cname', 'fake_sshkey') + self.assertEqual(['cmd1', 'cmd2'], + container_obj._parse_commands(['cmd1', 'cmd2'])) + + def test__parse_commands_exception(self): + container_obj = kubernetes.ContainerObject('cname', 'fake_sshkey') + with self.assertRaises(exceptions.KubernetesContainerCommandType): + container_obj._parse_commands({}) + class CustomResourceDefinitionObjectTestCase(base.BaseUnitTestCase): |