Пример #1
0
    def test_status_valid(self, mock_docker_client, mock_container,
                          mock_non_match_container):
        # @note when setting the container object using autospec=True, it could not
        # set the properties name and status of the mock object.
        # Thus we resort to using TestContainerSpec as the autospec where these are
        # settable. It should be noted that for the status test it was sufficient to use
        # @mock.patch('docker.models.containers.Container') directly but we are using
        # TestContainerSpec for consistency

        # arrange
        test_status = 'running'
        type(mock_container).status = mock.PropertyMock(
            return_value=test_status)
        type(mock_container).name = mock.PropertyMock(
            return_value=self.TEST_CONTAINER_NAME)
        type(mock_non_match_container).status = mock.PropertyMock(
            return_value='running')
        type(mock_non_match_container).name = mock.PropertyMock(
            return_value='blah')
        mock_docker_client.containers.list.return_value = [
            mock_non_match_container, mock_container
        ]
        client = EdgeDockerClient.create_instance(mock_docker_client)

        # act
        result = client.status(self.TEST_CONTAINER_NAME)

        # assert
        mock_docker_client.containers.list.assert_called_with(all=True)
        self.assertEqual(test_status, result)
Пример #2
0
    def test_create_valid(self, mock_docker_client, mock_docker_api_client):
        # arrange
        type(mock_docker_client).api = mock.PropertyMock(
            return_value=mock_docker_api_client)
        image = 'test_image'
        container_name = 'test_name'
        detach_bool = True
        env_dict = {'test_key_env': 'test_val_env'}
        ports_dict = {'test_key_ports': 'test_val_ports'}
        volume_dict = {
            'test_key_volume': {
                'bind': 'test_val_bind',
                'mode': 'test_val_mode'
            }
        }
        client = EdgeDockerClient.create_instance(mock_docker_client)

        # act
        client.create_container(image,
                                name=container_name,
                                detach=detach_bool,
                                environment=env_dict,
                                ports=ports_dict,
                                volumes=volume_dict)

        # assert
        mock_docker_client.api.create_container.assert_called_with(
            image,
            detach=detach_bool,
            environment=env_dict,
            name=container_name,
            ports=ports_dict,
            volumes=volume_dict)
Пример #3
0
 def test_get_os_valid(self, mock_docker_client):
     # arrange
     os_type = 'TEST_OS'
     mock_docker_client.info.return_value = {'OSType': os_type}
     client = EdgeDockerClient.create_instance(mock_docker_client)
     result = client.get_os_type()
     mock_docker_client.info.assert_called_with()
     self.assertEqual(result, os_type.lower())
Пример #4
0
    def test_stop_by_label_raises_exception(self, mock_docker_client):
        # arrange
        mock_docker_client.containers.list.side_effect = docker.errors.APIError(
            'list failure')
        client = EdgeDockerClient.create_instance(mock_docker_client)

        # act, assert
        with self.assertRaises(EdgeDeploymentError):
            client.stop_remove_by_label(self.TEST_LABEL)
Пример #5
0
    def test_status_raises_exception(self, mock_docker_client):
        # arrange
        mock_docker_client.containers.list.side_effect = docker.errors.APIError(
            'list failure')
        client = EdgeDockerClient.create_instance(mock_docker_client)

        # act, assert
        with self.assertRaises(EdgeDeploymentError):
            client.status(self.TEST_CONTAINER_NAME)
Пример #6
0
    def test_stop_invalid_container_raises_exception(self, mock_docker_client):
        # arrange
        mock_docker_client.containers.get.side_effect = docker.errors.NotFound(
            'invalid image')
        client = EdgeDockerClient.create_instance(mock_docker_client)

        # act, assert
        with self.assertRaises(EdgeDeploymentError):
            client.stop(self.TEST_CONTAINER_NAME)
Пример #7
0
    def test_get_os_fails(self, mock_docker_client):
        # arrange
        mock_docker_client.info.side_effect = docker.errors.APIError(
            'info fails')
        client = EdgeDockerClient.create_instance(mock_docker_client)

        # act, assert
        with self.assertRaises(EdgeError):
            client.get_os_type()
Пример #8
0
    def test_remove_fails_raises_exception(self, mock_docker_client,
                                           mock_container):
        # arrange
        mock_container.remove.side_effect = docker.errors.APIError(
            'remove failure')
        mock_docker_client.containers.get.return_value = mock_container
        client = EdgeDockerClient.create_instance(mock_docker_client)

        # act, assert
        with self.assertRaises(EdgeDeploymentError):
            client.remove(self.TEST_CONTAINER_NAME)
Пример #9
0
    def test_create_raises_except_when_APIError_is_raised(
            self, mock_docker_client, mock_docker_api_client):
        # arrange
        mock_docker_api_client.create_container.side_effect = docker.errors.APIError(
            'image error')
        type(mock_docker_client).api = mock.PropertyMock(
            return_value=mock_docker_api_client)
        client = EdgeDockerClient.create_instance(mock_docker_client)

        # act, assert
        with self.assertRaises(EdgeDeploymentError):
            self._create_common_invocation(client)
Пример #10
0
    def test_remove_valid(self, mock_docker_client, mock_container):
        # arrange
        mock_docker_client.containers.get.return_value = mock_container
        client = EdgeDockerClient.create_instance(mock_docker_client)

        # act
        client.remove(self.TEST_CONTAINER_NAME)

        # assert
        mock_docker_client.containers.get.assert_called_with(
            self.TEST_CONTAINER_NAME)
        mock_container.remove.assert_called_with()
Пример #11
0
    def test_get_local_image_sha_id_fails(self, mock_docker_client,
                                          mock_docker_api_client):
        # arrange
        mock_docker_api_client.inspect_image.side_effect = docker.errors.APIError(
            'inspect fails')
        type(mock_docker_client).api = mock.PropertyMock(
            return_value=mock_docker_api_client)
        client = EdgeDockerClient.create_instance(mock_docker_client)
        image = 'test_image'

        # act
        result = client.get_local_image_sha_id(image)

        # assert
        mock_docker_api_client.inspect_image.assert_called_with(image)
        self.assertEqual(result, None)
Пример #12
0
    def test_get_local_image_sha_id_valid(self, mock_docker_client,
                                          mock_docker_api_client):
        # arrange
        test_id = '1234'
        mock_docker_api_client.inspect_image.return_value = {'Id': test_id}
        type(mock_docker_client).api = mock.PropertyMock(
            return_value=mock_docker_api_client)
        client = EdgeDockerClient.create_instance(mock_docker_client)
        image = 'test_image'

        # act
        result = client.get_local_image_sha_id(image)

        # assert
        mock_docker_api_client.inspect_image.assert_called_with(image)
        self.assertEqual(result, test_id)
Пример #13
0
    def test_pull_raises_exception(self, mock_docker_client,
                                   mock_docker_api_client, mock_get_local_id):
        # arrange
        test_id = '1234'
        mock_get_local_id.return_value = None
        mock_docker_api_client.inspect_image.return_value = {'Id': test_id}
        type(mock_docker_client).api = mock.PropertyMock(
            return_value=mock_docker_api_client)
        mock_docker_client.images.pull.side_effect = docker.errors.APIError(
            'docker unavailable')
        client = EdgeDockerClient.create_instance(mock_docker_client)
        image = 'test_image'
        username = "******"
        password = "******"

        # act, assert
        with self.assertRaises(EdgeDeploymentError):
            client.pull(image, username, password)
Пример #14
0
    def test_stop_by_label_valid(self, mock_docker_client, mock_container1,
                                 mock_container2):
        # @note when setting multiple container mocks autospec=True failed which is
        # why we resort to using TestContainerSpec as the autospec class

        # arrange
        mock_docker_client.containers.list.return_value = [
            mock_container1, mock_container2
        ]
        client = EdgeDockerClient.create_instance(mock_docker_client)
        filter_dict = {'label': self.TEST_LABEL}

        # act
        client.stop_remove_by_label(self.TEST_LABEL)

        # assert
        mock_docker_client.containers.list.assert_called_with(
            all=True, filters=filter_dict)
        mock_container1.stop.assert_called_with()
        mock_container2.stop.assert_called_with()
Пример #15
0
    def test_pull_image_exists_locally_with_newer_image_no_credentials_valid(
            self, mock_docker_client, mock_docker_api_client,
            mock_get_local_id):
        # arrange
        test_id = '1234'
        mock_get_local_id.return_value = '1000'
        mock_docker_api_client.inspect_image.return_value = {'Id': test_id}
        type(mock_docker_client).api = mock.PropertyMock(
            return_value=mock_docker_api_client)
        client = EdgeDockerClient.create_instance(mock_docker_client)
        image = 'test_image'
        auth_dict = None

        # act
        result = client.pull(image, None, None)

        # assert
        mock_get_local_id.assert_called_with(image)
        mock_docker_api_client.inspect_image.assert_called_with(image)
        mock_docker_client.images.pull.assert_called_with(
            image, auth_config=auth_dict)
        self.assertTrue(result)
Пример #16
0
    def test_pull_image_exists_locally_with_no_newer_image_valid(
            self, mock_docker_client, mock_docker_api_client,
            mock_get_local_id):
        # arrange
        test_id = '1234'
        mock_get_local_id.return_value = test_id
        mock_docker_api_client.inspect_image.return_value = {'Id': test_id}
        type(mock_docker_client).api = mock.PropertyMock(
            return_value=mock_docker_api_client)
        client = EdgeDockerClient.create_instance(mock_docker_client)
        image = 'test_image'
        username = "******"
        password = "******"
        auth_dict = {'username': username, 'password': password}

        # act
        result = client.pull(image, username, password)

        # assert
        mock_get_local_id.assert_called_with(image)
        mock_docker_api_client.inspect_image.assert_called_with(image)
        mock_docker_client.images.pull.assert_called_with(
            image, auth_config=auth_dict)
        self.assertFalse(result)