kwargs = dt_utils.get_value_from_dict('opts', project_cfg)
shell = dt_utils.get_value_from_dict('shell', project_cfg)
if not shell:
- return None
+ return None, "Lacking of key word 'shell' in config file."
env_list = dt_utils.get_value_from_dict('envs', project_cfg)
if env_list:
kwargs['environment'] = \
[env for env in env_list if env is not None]
volume_list = dt_utils.get_value_from_dict('volumes', project_cfg)
kwargs['volumes'] = [vol for vol in volume_list if vol is not None]
+
+ kwargs['mounts'], msg = dt_utils.get_mount_list(project_cfg)
+ if not kwargs['mounts']:
+ return None, msg
+
kwargs['extra_hosts'] = dt_utils.get_hosts_info(self.logger)
try:
self.container = self.client.containers.run(
docker_image, shell, **kwargs)
except (docker.errors.ContainerError, docker.errors.ImageNotFound,
- docker.errors.APIError):
- return None
+ docker.errors.APIError) as e:
+ return None, e
- return self.container.id
+ return self.container.id, 'Successfully to create container.'
def get_image_id(self, image_name):
try:
self.logger.error("Failed to pull the image.")
return
- container_id = container.create(docker_image)
+ container_id, msg = container.create(docker_image)
if not container_id:
self.logger.error('Failed to create container.')
+ self.logger.error(msg)
return
self.logger.debug('container id: {}'.format(container_id))
container_id = 'container_id'
mock_utils.get_value_from_dict.side_effect = [
{'key': 'value'}, 'shell', 'envs', ['volume_one', 'volume_two']]
+ mock_utils.get_mount_list.side_effect = [['mount', 'list'], 'success']
mock_utils.get_hosts_info.return_value = 'host_info'
container_obj = Mock()
container_obj.id = container_id
mock_config.dovetail_config = {'bottlenecks': project_config}
expected = container_id
- result = self.container.create(docker_image)
+ result, msg = self.container.create(docker_image)
mock_utils.get_value_from_dict.assert_has_calls([
call('opts', project_config),
call('volumes', project_config)])
mock_utils.get_hosts_info.assert_called_once_with(self.logger)
self.assertEqual(expected, result)
+ self.assertEqual('Successfully to create container.', msg)
@patch('dovetail.container.dt_utils')
@patch('dovetail.container.dt_cfg')
mock_utils.get_value_from_dict.side_effect = ['opts', None]
mock_utils.get_hosts_info.return_value = 'host_info'
- result = self.container.create(docker_image)
+ result, msg = self.container.create(docker_image)
mock_utils.get_value_from_dict.assert_has_calls([
call('opts', 'value'),
call('shell', 'value')])
self.assertEqual(None, result)
+ self.assertEqual("Lacking of key word 'shell' in config file.", msg)
+
+ @patch('dovetail.container.dt_utils')
+ @patch('dovetail.container.dt_cfg')
+ def test_create_mounts_none(self, mock_config, mock_utils):
+ docker_image = 'docker_image'
+ project_config = {}
+ mock_config.dovetail_config = {'bottlenecks': project_config}
+ mock_utils.get_value_from_dict.side_effect = [
+ {'key': 'value'}, 'shell', ['envs'], ['volume_one']]
+ mock_utils.get_mount_list.side_effect = [[None, 'error']]
+ mock_utils.get_hosts_info.return_value = 'host_info'
+
+ result, msg = self.container.create(docker_image)
+
+ mock_utils.get_value_from_dict.assert_has_calls([
+ call('opts', project_config), call('shell', project_config),
+ call('envs', project_config), call('volumes', project_config)])
+ self.assertEqual(None, result)
+ self.assertEqual('error', msg)
@patch('dovetail.container.dt_utils')
@patch('dovetail.container.dt_cfg')
docker_image = 'docker_image'
mock_utils.get_value_from_dict.side_effect = [
{'key': 'value'}, 'shell', ['envs'], ['volume_one']]
+ mock_utils.get_mount_list.side_effect = [['mount', 'list'], 'success']
mock_utils.get_hosts_info.return_value = 'host_info'
mock_utils.check_https_enabled.return_value = True
self.client.containers.run.side_effect = \
docker.errors.ImageNotFound('error')
project_config = {}
mock_config.dovetail_config = {'bottlenecks': project_config}
- result = self.container.create(docker_image)
+ result, msg = self.container.create(docker_image)
mock_utils.get_value_from_dict.assert_has_calls([
call('opts', project_config),
call('volumes', project_config)])
mock_utils.get_hosts_info.assert_called_once_with(self.logger)
self.assertEqual(None, result)
+ self.assertEqual('error', str(docker.errors.ImageNotFound('error')))
docker_img_obj = Mock()
container_obj.get_docker_image.return_value = docker_img_obj
container_obj.pull_image.return_value = True
- container_obj.create.return_value = False
+ container_obj.create.return_value = [None, 'error']
mock_container.return_value = container_obj
docker_runner.run()
container_obj.get_docker_image.assert_called_once_with()
container_obj.pull_image.assert_called_once_with(docker_img_obj)
container_obj.create.assert_called_once_with(docker_img_obj)
- docker_runner.logger.error.assert_called_once_with(
- 'Failed to create container.')
+ docker_runner.logger.error.assert_has_calls([
+ call('Failed to create container.'), call('error')])
@patch('dovetail.test_runner.dt_utils')
@patch('dovetail.test_runner.dt_cfg')
container_obj.get_docker_image.return_value = docker_img_obj
container_obj.pull_image.return_value = True
container_id = '12345'
- container_obj.create.return_value = container_id
+ container_msg = 'Successfully to create container.'
+ container_obj.create.return_value = [container_id, container_msg]
mock_container.return_value = container_obj
self.testcase.pre_condition.return_value = ['cmd']
self.testcase.prepare_cmd.return_value = False
container_obj.get_docker_image.return_value = docker_img_obj
container_obj.pull_image.return_value = True
container_id = '12345'
- container_obj.create.return_value = container_id
+ container_msg = 'Successfully to create container.'
+ container_obj.create.return_value = [container_id, container_msg]
mock_container.return_value = container_obj
self.testcase.pre_condition.return_value = ['cmd']
self.testcase.prepare_cmd.return_value = True
logger.debug.assert_not_called()
logger.exception.assert_called_once_with(
"The results cannot be pushed to DB.")
+
+ def test_get_mount_list_error_mount(self):
+ project_cfg = {'mounts': ['aaa']}
+ res, msg = dovetail_utils.get_mount_list(project_cfg)
+ self.assertEqual(None, res)
+ self.assertEqual('Error mount aaa.', msg)
+
+ def test_get_mount_list_keyerror_exception(self):
+ project_cfg = {'mounts': ['aaa=a,bbb=b', '']}
+ res, msg = dovetail_utils.get_mount_list(project_cfg)
+ self.assertEqual(None, res)
+ self.assertEqual("'target'", str(msg))
+
+ def test_get_mount_list(self):
+ project_cfg = {'mounts': ['target=a,source=b', '']}
+ res, msg = dovetail_utils.get_mount_list(project_cfg)
+ expected = [{'Source': 'b', 'Type': 'bind', 'ReadOnly': False,
+ 'Target': 'a'}]
+ self.assertEqual(expected, res)
+ self.assertEqual('Successfully to get mount list.', msg)
import yaml
import python_hosts
import docker
+from docker.types import Mount
from dovetail import constants
from dovetail.utils.dovetail_config import DovetailConfig as dt_cfg
except Exception:
logger.exception('The results cannot be pushed to DB.')
return False
+
+
+def get_mount_list(project_cfg):
+ mount_list = []
+ mounts = get_value_from_dict('mounts', project_cfg)
+ for mount in mounts:
+ if mount:
+ param_dict = {}
+ for param in mount.split(','):
+ key_word = param.split('=')
+
+ if len(key_word) != 2:
+ return None, 'Error mount {}.'.format(mount)
+
+ param_dict[key_word[0]] = key_word[1]
+ try:
+ mount_list.append(Mount(target=param_dict['target'],
+ source=param_dict['source'],
+ type='bind'))
+ except Exception as e:
+ return None, e
+
+ return mount_list, 'Successfully to get mount list.'
{% set build_tag = build_tag or '' %}
{% set cacert_volume = '' %}
{% if cacert %}
- {% set cacert_volume = cacert + ':' + cacert %}
+ {% set cacert_volume = 'source=' + cacert + ',target=' + cacert %}
{% endif %}
{% set openrc_file = '/tmp/admin_rc.sh' %}
{% set result_dir = '/home/opnfv/bottlenecks/results' %}
- 'CI_DEBUG={{debug}}'
- 'BUILD_TAG={{build_tag}}-{{testcase}}'
volumes:
- - '/var/run/docker.sock:/var/run/docker.sock'
- '{{dovetail_home}}/results/bottlenecks:/tmp'
- - '{{dovetail_home}}/pre_config/env_config.sh:{{openrc_file}}'
- - {{cacert_volume}}
- '{{dovetail_home}}/images:{{images_dir}}'
- '{{dovetail_home}}/results:{{result_dir}}'
+ mounts:
+ - 'source=/var/run/docker.sock,target=/var/run/docker.sock'
+ - 'source={{dovetail_home}}/pre_config/env_config.sh,target={{openrc_file}}'
+ - {{cacert_volume}}
pre_condition:
- 'cp {{images_dir}}/ubuntu-16.04-server-cloudimg-amd64-disk1.img {{image_file}}'
cmds:
- 'CI_DEBUG={{debug}}'
- 'BUILD_TAG={{build_tag}}-{{testcase}}'
volumes:
- - '{{dovetail_home}}/pre_config/k8.creds:{{openrc_file}}'
- - '{{dovetail_home}}/pre_config/admin.conf:{{kube_file}}'
- '{{dovetail_home}}/results/:{{result_dir}}'
+ mounts:
+ - 'source={{dovetail_home}}/pre_config/k8.creds,target={{openrc_file}}'
+ - 'source={{dovetail_home}}/pre_config/admin.conf,target={{kube_file}}'
pre_condition:
- 'echo test for precondition in functest'
cmds:
{% set build_tag = build_tag or '' %}
{% set cacert_volume = '' %}
{% if cacert %}
- {% set cacert_volume = cacert + ':' + cacert %}
+ {% set cacert_volume = 'source=' + cacert + ',target=' + cacert %}
{% endif %}
{% set openrc_file = '/home/opnfv/functest/conf/env_file' %}
{% set result_dir = '/home/opnfv/functest/results' %}
- 'CI_DEBUG={{debug}}'
- 'BUILD_TAG={{build_tag}}-{{testcase}}'
volumes:
- - '{{dovetail_home}}/pre_config/env_config.sh:{{openrc_file}}'
- - {{cacert_volume}}
- - '{{dovetail_home}}/pre_config:/home/opnfv/pre_config'
- '{{dovetail_home}}/userconfig:{{userconfig_dir}}'
- '{{dovetail_home}}/patches:{{patches_dir}}'
- '{{dovetail_home}}/results:{{result_dir}}'
- '{{dovetail_home}}/images:{{images_dir}}'
+ mounts:
+ - 'source={{dovetail_home}}/pre_config/env_config.sh,target={{openrc_file}}'
+ - 'source={{dovetail_home}}/pre_config,target=/home/opnfv/pre_config'
+ - {{cacert_volume}}
patches_dir: {{patches_dir}}
pre_condition:
- 'echo test for precondition in functest'
{% set build_tag = build_tag or '' %}
{% set cacert_volume = '' %}
{% if cacert %}
- {% set cacert_volume = cacert + ':' + cacert %}
+ {% set cacert_volume = 'source=' + cacert + ',target=' + cacert %}
{% endif %}
{% set openrc_file = '/etc/yardstick/openstack.creds' %}
{% set pod_file = '/etc/yardstick/pod.yaml' %}
- 'CI_DEBUG={{debug}}'
- 'BUILD_TAG={{build_tag}}-{{testcase}}"'
volumes:
- - '{{dovetail_home}}/pre_config/env_config.sh:{{openrc_file}}'
- - {{cacert_volume}}
- - '{{dovetail_home}}/pre_config/pod.yaml:{{pod_file}}'
- '{{dovetail_home}}/images:/home/opnfv/images'
- '{{dovetail_home}}/results:{{result_dir}}'
- - '{{dovetail_home}}/pre_config:{{dovetail_home}}/pre_config'
+ mounts:
+ - 'source={{dovetail_home}}/pre_config,target={{dovetail_home}}/pre_config'
+ - 'source={{dovetail_home}}/pre_config/env_config.sh,target={{openrc_file}}'
+ - 'source={{dovetail_home}}/pre_config/pod.yaml,target={{pod_file}}'
+ - {{cacert_volume}}
pre_condition:
- 'echo this is pre_condition'
cmds: