Add different version support for docker-py

As https://review.openstack.org/#/c/186610/
mentioned one point that distros packaging may have old
docker-py version, in order to have wide support about container
api, this change add more version support for execute deprecated
issue.

Closes-Bug: #1460976
Change-Id: I0d30afa95cb64a5761a829a47eb01af45fea42e7
This commit is contained in:
Kai Qiang Wu(Kennan) 2015-06-03 18:03:18 +08:00
parent 7b204c61db
commit 5c6fa972c4
3 changed files with 59 additions and 4 deletions

View File

@ -11,6 +11,8 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import docker
from docker.utils import utils
def parse_docker_image(image_id):
@ -23,3 +25,9 @@ def parse_docker_image(image_id):
image_tag = image_parts[1]
return image_repo, image_tag
def is_docker_library_version_atleast(version):
if utils.compare_version(docker.version, version) <= 0:
return True
return False

View File

@ -228,10 +228,14 @@ class Handler(object):
docker = self.get_docker_client(context, container_uuid)
try:
docker_id = self._find_container_by_name(docker, container_uuid)
create_res = docker.exec_create(docker_id, command, True,
True, False)
return {'output': docker.exec_start(create_res, False,
False, False)}
if docker_utils.is_docker_library_version_atleast('1.2.0'):
create_res = docker.exec_create(docker_id, command, True,
True, False)
exec_output = docker.exec_start(create_res, False, False,
False)
else:
exec_output = docker.execute(docker_id, command)
return {'output': exec_output}
except errors.APIError as api_error:
raise exception.ContainerException(
"Docker API Error : %s" % str(api_error))

View File

@ -11,6 +11,7 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import docker
from docker import errors
import mock
from oslo_config import cfg
@ -615,6 +616,7 @@ class TestDockerConductor(base.BaseTestCase):
mock_get_docker_client.return_value = mock_docker
mock_container_uuid = 'd545a92d-609a-428f-8edb-16b02ad20ca1'
mock_docker_id = '2703ef2b705d'
docker.version = '1.2.2'
mock_find_container.return_value = mock_docker_id
mock_create_res = mock.MagicMock()
mock_docker.exec_create.return_value = mock_create_res
@ -627,6 +629,23 @@ class TestDockerConductor(base.BaseTestCase):
mock_find_container.assert_called_once_with(mock_docker,
mock_container_uuid)
@patch.object(docker_conductor.Handler, '_find_container_by_name')
@mock.patch.object(docker_conductor.Handler, 'get_docker_client')
def test_container_execute_deprecated(self, mock_get_docker_client,
mock_find_container):
mock_docker = mock.MagicMock()
mock_get_docker_client.return_value = mock_docker
mock_container_uuid = 'd545a92d-609a-428f-8edb-16b02ad20ca1'
mock_docker_id = '2703ef2b705d'
docker.version = '0.7.0'
mock_find_container.return_value = mock_docker_id
mock_create_res = mock.MagicMock()
mock_docker.exec_create.return_value = mock_create_res
self.conductor.container_execute(None, mock_container_uuid, 'ls')
mock_docker.execute.assert_called_once_with(mock_docker_id, 'ls')
mock_find_container.assert_called_once_with(mock_docker,
mock_container_uuid)
@patch.object(docker_conductor.Handler, '_find_container_by_name')
@mock.patch.object(docker_conductor.Handler, 'get_docker_client')
def test_container_execute_with_failure(self,
@ -636,6 +655,7 @@ class TestDockerConductor(base.BaseTestCase):
mock_get_docker_client.return_value = mock_docker
mock_container_uuid = 'd545a92d-609a-428f-8edb-16b02ad20ca1'
mock_docker_id = '2703ef2b705d'
docker.version = '1.2.2'
mock_find_container.return_value = mock_docker_id
with patch.object(errors.APIError, '__str__',
return_value='hit error') as mock_init:
@ -651,6 +671,29 @@ class TestDockerConductor(base.BaseTestCase):
mock_container_uuid)
mock_init.assert_called_once_with()
@patch.object(docker_conductor.Handler, '_find_container_by_name')
@mock.patch.object(docker_conductor.Handler, 'get_docker_client')
def test_container_execute_deprecated_with_failure(self,
mock_get_docker_client,
mock_find_container):
mock_docker = mock.MagicMock()
mock_get_docker_client.return_value = mock_docker
mock_container_uuid = 'd545a92d-609a-428f-8edb-16b02ad20ca1'
mock_docker_id = '2703ef2b705d'
docker.version = '0.7.0'
mock_find_container.return_value = mock_docker_id
with patch.object(errors.APIError, '__str__',
return_value='hit error') as mock_init:
mock_docker.execute = mock.Mock(
side_effect=errors.APIError('Error', '', ''))
self.assertRaises(exception.ContainerException,
self.conductor.container_execute,
None, mock_container_uuid, 'ls')
mock_docker.execute.assert_called_once_with(mock_docker_id, 'ls')
mock_find_container.assert_called_once_with(mock_docker,
mock_container_uuid)
mock_init.assert_called_once_with()
@patch.object(docker_conductor.Handler, '_find_container_by_name')
@mock.patch.object(docker_conductor.Handler, 'get_docker_client')
def test_container_logs(self, mock_get_docker_client,