def _test_container_exec_with_failure(self,
                                       mock_find_container,
                                       docker_version='1.2.2',
                                       deprecated=False):
     mock_container_uuid = 'd545a92d-609a-428f-8edb-16b02ad20ca1'
     mock_docker_id = '2703ef2b705d'
     docker.version = docker_version
     mock_find_container.return_value = mock_docker_id
     with mock.patch.object(errors.APIError,
                            '__str__',
                            return_value='hit error') as mock_init:
         if deprecated:
             self.mock_docker.execute = mock.Mock(
                 side_effect=errors.APIError('Error', '', ''))
         else:
             self.mock_docker.exec_create = mock.Mock(
                 side_effect=errors.APIError('Error', '', ''))
         self.assertRaises(exception.ContainerException,
                           self.conductor.container_exec, None,
                           mock_container_uuid, 'ls')
         if deprecated:
             self.mock_docker.execute.assert_called_once_with(
                 mock_docker_id, 'ls')
         else:
             self.mock_docker.exec_create.assert_called_once_with(
                 mock_docker_id, 'ls', True, True, False)
         mock_find_container.assert_called_once_with(
             self.mock_docker, mock_container_uuid)
         mock_init.assert_called_with()
    def _test_container_show_with_failure(self,
                                          mock_find_container,
                                          mock_get_by_uuid,
                                          error,
                                          assert_raise=True,
                                          expected_status=None):
        mock_container = mock.MagicMock()
        mock_get_by_uuid.return_value = mock_container
        mock_container_uuid = 'd545a92d-609a-428f-8edb-1d6b02ad20ca1'
        mock_docker_id = '2703ef2b705d'
        mock_find_container.return_value = mock_docker_id
        with mock.patch.object(errors.APIError, '__str__',
                               return_value=error) as mock_init:
            self.mock_docker.inspect_container = mock.Mock(
                side_effect=errors.APIError('Error', '', ''))

            if assert_raise:
                self.assertRaises(exception.ContainerException,
                                  self.conductor.container_show, None,
                                  mock_container_uuid)
            else:
                self.conductor.container_show(None, mock_container_uuid)

            self.mock_docker.inspect_container.assert_called_once_with(
                mock_docker_id)
            mock_find_container.assert_called_once_with(
                self.mock_docker, mock_container_uuid)
            mock_init.assert_called_with()
            if expected_status is not None:
                self.assertEqual(expected_status, mock_container.status)
Пример #3
0
 def test_search_image_apierror(self):
     with mock.patch.object(errors.APIError, '__str__',
                            return_value='hit error') as mock_init:
         self.mock_docker.search = mock.Mock(
             side_effect=errors.APIError('Error', '', ''))
         self.assertRaises(exception.ZunException, self.driver.search_image,
                           None, 'test_image', None, False)
         self.mock_docker.search.assert_called_once_with('test_image')
         self.assertEqual(1, mock_init.call_count)
Пример #4
0
 def test_find_container_by_name_not_found(self):
     mock_docker = mock.MagicMock()
     fake_response = mock.MagicMock()
     fake_response.content = 'not_found'
     fake_response.status_code = 404
     mock_docker.list_instances.side_effect = errors.APIError(
         'not_found', fake_response)
     ret = self.conductor._find_container_by_name(mock_docker, '1')
     self.assertEqual({}, ret)
Пример #5
0
 def test_delete_fail_no_result(self):
     with mock.patch.object(errors.APIError, '__str__',
                            return_value='404 Not Found') as mock_init:
         self.mock_docker.remove_container = mock.Mock(
             side_effect=errors.APIError('Error', '', ''))
         mock_container = mock.MagicMock()
         self.driver.delete(mock_container, True)
         self.mock_docker.remove_container.assert_called_once_with(
             mock_container.container_id, force=True)
         self.assertEqual(1, mock_init.call_count)
Пример #6
0
 def test_container_list_with_failure(self):
     with patch.object(errors.APIError, '__str__',
                       return_value='hit error') as mock_init:
         self.mock_client.containers = mock.Mock(side_effect=
                               errors.APIError('Error', '', ''))
         self.assertRaises(exception.ContainerException,
                           self.conductor.container_list,
                           None)
         self.mock_client.containers.assert_called_once_with()
         mock_init.assert_called_once_with()
Пример #7
0
 def test_show_fail_api_error(self):
     with mock.patch.object(errors.APIError, '__str__',
                            return_value='test') as mock_init:
         self.mock_docker.inspect_container = mock.Mock(
             side_effect=errors.APIError('Error', '', ''))
         mock_container = mock.MagicMock()
         self.assertRaises(errors.APIError, self.driver.show,
                           self.context, mock_container)
         self.mock_docker.inspect_container.assert_called_once_with(
             mock_container.container_id)
         self.assertEqual(1, mock_init.call_count)
Пример #8
0
 def test_pull_image_raises_API_error(self, mock_should_pull_image,
                                      mock_search, mock_parse_image):
     mock_should_pull_image.return_value = True
     mock_search.return_value = {'image': 'nginx', 'path': 'xyz'}
     mock_parse_image.return_value = ('repo', 'tag')
     self.mock_docker.pull = mock.Mock(
         side_effect=errors.APIError('Error', '', ''))
     self.assertRaises(exception.ZunException, self.driver.pull_image,
                       None, 'repo', 'tag', 'always', None)
     self.mock_docker.pull.assert_called_once_with(
         'repo', tag='tag', auth_config=None)
Пример #9
0
 def test_delete_fail_raise_error(self):
     with mock.patch.object(errors.APIError, '__str__',
                            return_value='test') as mock_init:
         self.mock_docker.remove_container = mock.Mock(
             side_effect=errors.APIError('Error', '', ''))
         mock_container = mock.MagicMock()
         self.assertRaises(errors.APIError, self.driver.delete,
                           self.context, mock_container, True)
         self.mock_docker.remove_container.assert_called_once_with(
             mock_container.container_id, force=True)
         self.assertEqual(2, mock_init.call_count)
Пример #10
0
 def test_show_fail_container_status_error(self):
     with mock.patch.object(errors.APIError, '__str__',
                            return_value='404 Not Found') as mock_init:
         self.mock_docker.inspect_container = mock.Mock(
             side_effect=errors.APIError('Error', '', ''))
         mock_container = mock.MagicMock()
         result_container = self.driver.show(self.context, mock_container)
         self.mock_docker.inspect_container.assert_called_once_with(
             mock_container.container_id)
         self.assertEqual(result_container.status,
                          consts.ERROR)
         self.assertEqual(2, mock_init.call_count)
Пример #11
0
 def test_pull_image_raises_API_error(self, mock_should_pull_image,
                                      mock_search, mock_parse_image):
     mock_should_pull_image.return_value = True
     mock_search.return_value = {'image': 'nginx', 'path': 'xyz'}
     mock_parse_image.return_value = ('repo', 'tag')
     with mock.patch.object(errors.APIError,
                            '__str__',
                            return_value='404 Not Found') as mock_init:
         self.mock_docker.pull = mock.Mock(
             side_effect=errors.APIError('Error', '', ''))
         self.assertRaises(exception.ZunException, self.driver.pull_image,
                           None, 'repo', 'tag', 'always')
         self.mock_docker.pull.assert_called_once_with('repo', tag='tag')
         self.assertEqual(1, mock_init.call_count)
Пример #12
0
 def test_container_logs_with_failure(self, mock_find_container, mock_init):
     mock_container_uuid = 'd545a92d-609a-428f-8edb-16b02ad20ca1'
     mock_docker_id = '2703ef2b705d'
     mock_find_container.return_value = mock_docker_id
     mock_init.return_value = 'hit error'
     self.mock_docker.logs = mock.Mock(
         side_effect=errors.APIError('Error', '', ''))
     self.assertRaises(exception.ContainerException,
                       self.conductor.container_logs, None,
                       mock_container_uuid)
     self.mock_docker.logs.assert_called_once_with(mock_docker_id)
     mock_find_container.assert_called_once_with(self.mock_docker,
                                                 mock_container_uuid)
     mock_init.assert_called_with()
Пример #13
0
    def test_remove_image_exception_500(self):
        resp = mock.MagicMock()
        resp.status_code = 500
        docker_except = docker_error.APIError('test error', resp)
        self.dw = get_DockerWorker({
            'image': 'myregistrydomain.com:5000/ubuntu:16.04',
            'action': 'remove_image'
        })
        self.dw.dc.images.return_value = self.fake_data['images']
        self.dw.dc.remove_image.side_effect = docker_except

        self.assertRaises(docker_error.APIError, self.dw.remove_image)
        self.assertTrue(self.dw.changed)
        self.dw.module.fail_json.assert_called_once_with(failed=True,
                                                         msg=("Server error"))
Пример #14
0
    def test_container_create_with_failure(self, mock_init):
        mock_container = mock.MagicMock()
        mock_container.image = 'test_image:some_tag'
        mock_init.return_value = 'hit error'
        self.mock_docker.pull = mock.Mock(
            side_effect=errors.APIError('Error', '', ''))

        self.assertRaises(exception.ContainerException,
                          self.conductor.container_create, None,
                          mock_container)
        self.mock_docker.pull.assert_called_once_with('test_image',
                                                      tag='some_tag')
        self.assertFalse(self.mock_docker.create_container.called)
        mock_init.assert_called_with()
        self.assertEqual(fields.ContainerStatus.ERROR, mock_container.status)
Пример #15
0
    def test_remove_volume_exception(self):
        resp = mock.MagicMock()
        resp.status_code = 409
        docker_except = docker_error.APIError('test error', resp)
        self.dw = get_DockerWorker({
            'name': 'nova_compute',
            'action': 'remove_volume'
        })
        self.dw.dc.volumes.return_value = self.volumes
        self.dw.dc.remove_volume.side_effect = docker_except

        self.assertRaises(docker_error.APIError, self.dw.remove_volume)
        self.assertTrue(self.dw.changed)
        self.dw.module.fail_json.assert_called_once_with(
            failed=True, msg="Volume named 'nova_compute' is currently in-use")
Пример #16
0
 def test_container_delete_with_failure(self, mock_find_container):
     mock_container_uuid = 'd545a92d-609a-428f-8edb-16b02ad20ca1'
     mock_docker_id = '2703ef2b705d'
     mock_find_container.return_value = mock_docker_id
     with patch.object(errors.APIError, '__str__',
                       return_value='hit error') as mock_init:
         self.mock_client.remove_container = mock.Mock(side_effect=
                               errors.APIError('Error', '', ''))
         self.assertRaises(exception.ContainerException,
                           self.conductor.container_delete,
                           None, mock_container_uuid)
         self.mock_client.remove_container.assert_called_once_with(
                                                    mock_docker_id)
         mock_find_container.assert_called_once_with(mock_container_uuid)
         mock_init.assert_called_once_with()
Пример #17
0
 def test_kill_fail_container_status_error(self):
     with mock.patch.object(errors.APIError,
                            '__str__',
                            return_value='404 Not Found') as mock_init:
         self.mock_docker.kill = mock.Mock()
         self.mock_docker.inspect_container = mock.Mock(
             side_effect=errors.APIError('Error', '', ''))
         mock_container = mock.MagicMock()
         result_container = self.driver.kill(mock_container, signal='test')
         self.mock_docker.kill.assert_called_once_with(
             mock_container.container_id, 'test')
         self.mock_docker.inspect_container.assert_called_once_with(
             mock_container.container_id)
         self.assertEqual(result_container.status,
                          fields.ContainerStatus.ERROR)
         self.assertEqual(1, mock_init.call_count)
Пример #18
0
 def _test_container_with_failure(self, action, docker_func_name,
                                  mock_find_container, mock_init):
     mock_container_uuid = 'd545a92d-609a-428f-8edb-16b02ad20ca1'
     mock_docker_id = '2703ef2b705d'
     mock_find_container.return_value = mock_docker_id
     mock_init.return_value = 'hit error'
     setattr(self.mock_docker, docker_func_name,
             mock.Mock(side_effect=errors.APIError('Error', '', '')))
     self.assertRaises(exception.ContainerException,
                       getattr(self.conductor, action), None,
                       mock_container_uuid)
     docker_func = getattr(self.mock_docker, docker_func_name)
     docker_func.assert_called_once_with(mock_docker_id)
     mock_find_container.assert_called_once_with(self.mock_docker,
                                                 mock_container_uuid)
     mock_init.assert_called_with()
Пример #19
0
    def test_container_create_with_failure(self):
        mock_container = mock.MagicMock()
        mock_container.image_id = 'test_image:some_tag'
        with patch.object(errors.APIError, '__str__',
                          return_value='hit error') as mock_init:
            self.mock_client.pull = mock.Mock(side_effect=
                                  errors.APIError('Error', '', ''))

            self.assertRaises(exception.ContainerException,
                              self.conductor.container_create,
                              None, 'some-name', 'some-uuid', mock_container)
            self.mock_client.pull.assert_called_once_with(
                                          'test_image',
                                          tag='some_tag')
            self.assertFalse(self.mock_client.create_container.called)
            mock_init.assert_called_once_with()
Пример #20
0
def test_docker_image_exists_with_api_error(mocker, mock_client, caplog):
    mock_images = mocker.Mock()
    mock_images.get = mocker.Mock()

    images_mock = mocker.PropertyMock(return_value=mock_images)

    mock_client.images = images_mock

    image_tag = "gcr.io/sigint/test-image-name"

    mock_client.images.get.side_effect = docker_errors.APIError("pew")

    with pytest.raises(SystemExit):
        docker_utils.docker_image_exists(image_tag, mock_client)

    assert 1 == len(caplog.records)
Пример #21
0
    def test_container_create_with_failure(self, mock_get_docker_client):
        mock_docker = mock.MagicMock()
        mock_get_docker_client.return_value = mock_docker
        mock_container = mock.MagicMock()
        mock_container.image = 'test_image:some_tag'
        with patch.object(errors.APIError, '__str__',
                          return_value='hit error') as mock_init:
            mock_docker.pull = mock.Mock(
                side_effect=errors.APIError('Error', '', ''))

            self.assertRaises(exception.ContainerException,
                              self.conductor.container_create, None,
                              mock_container)
            mock_docker.pull.assert_called_once_with('test_image',
                                                     tag='some_tag')
            self.assertFalse(mock_docker.create_container.called)
            mock_init.assert_called_once_with()
            self.assertEqual(obj_container.ERROR, mock_container.status)
Пример #22
0
 def test_container_show_with_not_found(self, mock_find_container,
                                        mock_get_by_uuid):
     mock_container = mock.MagicMock()
     mock_get_by_uuid.return_value = mock_container
     mock_container_uuid = 'd545a92d-609a-428f-8edb-1d6b02ad20ca1'
     mock_docker_id = '2703ef2b705d'
     mock_find_container.return_value = mock_docker_id
     with patch.object(errors.APIError, '__str__',
                       return_value='404 error') as mock_init:
         self.mock_docker.inspect_container = mock.Mock(
             side_effect=errors.APIError('Error', '', ''))
         self.conductor.container_show(None, mock_container_uuid)
         self.mock_docker.inspect_container.assert_called_once_with(
             mock_docker_id)
         mock_find_container.assert_called_once_with(
             self.mock_docker, mock_container_uuid)
         mock_init.assert_called_once_with()
         self.assertEqual(fields.ContainerStatus.ERROR,
                          mock_container.status)
Пример #23
0
 def test_container_exec_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_exec, 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()
Пример #24
0
 def _raise_for_status(self, response, explanation=None):
     """Raises stored :class:`APIError`, if one occurred."""
     try:
         response.raise_for_status()
     except requests.exceptions.HTTPError as e:
         raise errors.APIError(e, response, explanation=explanation)
Пример #25
0
    return "/test/dir"


@pytest.fixture
def mock_os_environ(mocker):
    return mocker.patch.dict("os.environ", {"HOME": "/home"})


def test_check_docker_connection(mock_docker_client):
    docker_utils.check_docker_connection(mock_docker_client)
    mock_docker_client.ping.assert_called_once_with()


@pytest.mark.parametrize(
    "error",
    [docker_errors.APIError("msg"),
     requests_exceptions.ConnectionError()],
)
def test_check_docker_connection_with_errors(mock_docker_client, error,
                                             caplog):
    mock_docker_client.ping.side_effect = error
    with pytest.raises(SystemExit):
        docker_utils.check_docker_connection(mock_docker_client)

    assert 2 == len(caplog.records)


@pytest.mark.parametrize("path_exists", [True, False])
def test_check_dockerfile_present(monkeypatch, path_exists, caplog):
    monkeypatch.setattr(os.path, "exists", lambda x: path_exists)