Accept strings and lists as container "args" and "commands" 47/59947/4
authorRodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com>
Wed, 18 Jul 2018 15:39:17 +0000 (16:39 +0100)
committerRodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com>
Thu, 19 Jul 2018 11:03:34 +0000 (12:03 +0100)
Accept strings and list of strings as "args" and "commands"
in a Kubernetes container.

JIRA: YARDSTICK-1329

Change-Id: I56470741072fb7f9a62d695c51fcb0cc3f3ff1b9
Signed-off-by: Rodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com>
yardstick/common/exceptions.py
yardstick/orchestrator/kubernetes.py
yardstick/tests/unit/orchestrator/test_kubernetes.py

index 1f749ad..cbb2949 100644 (file)
@@ -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'
 
index bee4d4a..3e2572f 100644 (file)
@@ -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)
index 1317308..be9c2ae 100644 (file)
@@ -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):