Beispiel #1
0
def test_cleanup_images(mock_client, now):
    max_image_age = now
    mock_client.images.return_value = images = [
        {
            'Id': 'abcd'
        },
        {
            'Id': 'abbb'
        },
    ]
    mock_images = [
        {
            'Id': 'abcd',
            'Created': '2014-01-01T01:01:01Z'
        },
        {
            'Id': 'abbb',
            'Created': '2014-01-01T01:01:01Z'
        },
    ]
    mock_client.inspect_image.side_effect = iter(mock_images)

    docker_gc.cleanup_images(mock_client, max_image_age, False, set())
    assert mock_client.remove_image.mock_calls == [
        mock.call(image=image['Id']) for image in reversed(images)
    ]
def test_cleanup_images_max_tags_count(mock_client,
                                       max_image_age, expected_remove_calls):
    mock_client.images.return_value = [
        {'Id': 'abcd', 'RepoTags': ['user/one:latest', 'user/one:abcd']},
        {'Id': 'abce', 'RepoTags': ['user/two:latest', 'user/one:efgh']},
        {'Id': 'abcf', 'RepoTags': ['user/two:aaaa', 'user/one:bbbb']},
    ]
    mock_images = [
        {
            'Id': 'abcd',
            'Created': '2014-01-01T01:01:01Z'
        },
        {
            'Id': 'abce',
            'Created': '2014-01-01T01:01:01Z'
        },
        {
            'Id': 'abcf',
            'Created': '2014-01-01T01:01:01Z'
        },
    ]
    mock_client.inspect_image.side_effect = iter(mock_images)
    max_tags_count = 1
    docker_gc.cleanup_images(
        mock_client, max_image_age, max_tags_count, False, set())
    # Keep at least max_tags_count tags for each repository.
    assert mock_client.remove_image.mock_calls == expected_remove_calls
def test_cleanup_images(mock_client, now):
    max_image_age = now
    mock_client.images.return_value = images = [
        dict(Id='abcd'),
        dict(Id='abbb'),
    ]
    mock_images = [
        dict(Id='abcd', Created='2014-01-01T01:01:01Z'),
        dict(Id='abbb', Created='2014-01-01T01:01:01Z'),
    ]
    mock_client.inspect_image.side_effect = iter(mock_images)

    docker_gc.cleanup_images(mock_client, max_image_age, False, set())
    assert mock_client.remove_image.mock_calls == [
        mock.call(image['Id']) for image in reversed(images)
    ]
def test_filter_images_in_use_by_id(mock_client, now):
    mock_client._version = '1.21'
    mock_client.containers.return_value = [
        {'Id': 'abcd', 'ImageID': '1'},
        {'Id': 'abbb', 'ImageID': '2'},
    ]
    mock_containers = [
        {
            'Id': 'abcd',
            'Name': 'one',
            'State': {
                'Running': False,
                'FinishedAt': '2014-01-01T01:01:01Z'
            }
        },
        {
            'Id': 'abbb',
            'Name': 'two',
            'State': {
                'Running': True,
                'FinishedAt': '2014-01-01T01:01:01Z'
            }
        }
    ]
    mock_client.inspect_container.side_effect = iter(mock_containers)
    mock_client.images.return_value = [
        {'Id': '1', 'Created': '2014-01-01T01:01:01Z'},
        {'Id': '2', 'Created': '2014-01-01T01:01:01Z'},
        {'Id': '3', 'Created': '2014-01-01T01:01:01Z'},
        {'Id': '4', 'Created': '2014-01-01T01:01:01Z'},
        {'Id': '5', 'Created': '2014-01-01T01:01:01Z'},
        {'Id': '6', 'Created': '2014-01-01T01:01:01Z'},
    ]
    mock_client.inspect_image.side_effect = lambda image: {
        'Id': image,
        'Created': '2014-01-01T01:01:01Z'
    }
    docker_gc.cleanup_images(mock_client, now, None, False, set())
    assert mock_client.remove_image.mock_calls == [
        mock.call(image=id_) for id_ in ['3', '4', '5', '6']
    ]