def test_timestamp_parsing(): assert parse_arguments(['containers', '--created', '1h']).created == timedelta(hours=1) assert parse_arguments(['containers', '--created', '23m']).created == timedelta(minutes=23) assert parse_arguments(['containers', '--created', '2d']).created == timedelta(days=2) assert parse_arguments(['containers', '--created', '0h']).created == timedelta() assert parse_arguments(['containers', '--created', '0']).created == timedelta()
def test_negative_tag_on_different_name_filter(): images = [ image_entry(IID1, mins_ago(200), "foo:v1.1", "bar:latest"), image_entry(IID2, mins_ago(190), "foo:v1.2"), image_entry(IID3, mins_ago(180), "foo:v1.3"), image_entry(IID4, mins_ago(170), "foo:v1.4"), ] args = parse_arguments(['images', '--keep', '2', '--tag', '~latest']) result = determine_images_to_remove(images, [], args) _assert_ids(result, IID2)
def test_keep_multiname(): images = [ image_entry(IID1, mins_ago(200), "foo:v1.1", "bar:v1"), image_entry(IID2, mins_ago(190), "foo:v1.2"), image_entry(IID3, mins_ago(180), "foo:v1.3"), image_entry(IID4, mins_ago(170), "foo:v1.4"), ] args = parse_arguments(['images', '--keep', '2']) result = determine_images_to_remove(images, [], args) _assert_ids(result, IID2)
def test_keep_multiname_with_filter(): """Even though we're only deleting records with name "foo", "keep" trumps filters""" images = [ image_entry(IID1, mins_ago(200), "foo:v1.1", "bar:v1"), image_entry(IID2, mins_ago(190), "foo:v1.2"), image_entry(IID3, mins_ago(180), "foo:v1.3"), image_entry(IID4, mins_ago(170), "foo:v1.4"), ] args = parse_arguments(['images', '--keep', '2', '--name', 'foo']) result = determine_images_to_remove(images, [], args) _assert_ids(result, IID2)
def test_partial_name_doesnt_match(): images = [ image_entry(IID1, mins_ago(200), "somewhere.org/someorg/foo:v1.1"), image_entry(IID2, mins_ago(190), "someorg/foo:v1.2"), image_entry(IID3, mins_ago(180), "someorg/foo:v1.3"), image_entry(IID4, mins_ago(210), "someorg/foo_monitor:R1"), image_entry(IID4, mins_ago(195), "somewhere.org/someorg/foo_monitor:R2"), image_entry(IID4, mins_ago(165), "someorg/foo_monitor:R3"), ] args = parse_arguments(['images', '--keep', '2', '--name', 'someorg/foo']) result = determine_images_to_remove(images, [], args) _assert_ids(result, IID1)
def test_positive_name_filter(): images = [ image_entry(IID1, mins_ago(200), "somewhere.org/someorg/foo:v1.1"), image_entry(IID2, mins_ago(190), "someorg/foo:v1.2"), image_entry(IID3, mins_ago(180), "someorg/foo:v1.3"), image_entry(IID4, mins_ago(210), "someorg/bar:R1"), image_entry(IID4, mins_ago(195), "somewhere.org/someorg/bar:R2"), image_entry(IID4, mins_ago(165), "someorg/bar:R3"), ] args = parse_arguments(['images', '--keep', '2', '--name', 'someorg/foo']) result = determine_images_to_remove(images, [], args) _assert_ids(result, IID1)
def test_dead(): containers = [ dead_container_entry(CID1, IID, mins_ago(2465)), dead_container_entry(CID2, IID, mins_ago(61)), dead_container_entry(CID3, IID, mins_ago(25)), running_container_entry(CID4, IID, mins_ago(2465)), created_container_entry(CID5, IID, mins_ago(2001)), exited_container_entry(CID6, IID, mins_ago(2011)) ] args = parse_arguments(['containers', '--dead', '1h']) _mock_containers(containers, args) result = determine_containers_to_remove(args) _assert_ids(result, CID1, CID2)
def test_keep_with_active_and_filter(): images = [ image_entry(IID1, mins_ago(200), "foo:v1.1"), image_entry(IID2, mins_ago(190), "foo:v1.2"), image_entry(IID3, mins_ago(180), "foo:v1.3"), image_entry(IID4, mins_ago(170), "foo:v1.4", "foo:latest"), ] containers = [ created_container_entry(CID1, IID2, mins_ago(5)), created_container_entry(CID2, IID3, mins_ago(5))] args = parse_arguments(['images', '--keep', '2', '--name', 'foo']) result = determine_images_to_remove(images, containers_result(containers), args) _assert_ids(result, IID1)
def test_keep_zero(): images = [ image_entry(IID1, mins_ago(200), "foo:v1.1"), image_entry(IID2, mins_ago(190), "foo:v1.2"), image_entry(IID3, mins_ago(180), "foo:v1.3"), image_entry(IID4, mins_ago(170), "foo:v1.4", "foo:latest"), ] args = parse_arguments(['images', '--keep', '0']) ids_to_keep = find_image_ids_to_keep(images, args) assert ids_to_keep == set() result = determine_images_to_remove(images, [], args) _assert_ids(result, IID1, IID2, IID3, IID4)
def test_keep_with_active_and_filter(): images = [ image_entry(IID1, mins_ago(200), "foo:v1.1"), image_entry(IID2, mins_ago(190), "foo:v1.2"), image_entry(IID3, mins_ago(180), "foo:v1.3"), image_entry(IID4, mins_ago(170), "foo:v1.4", "foo:latest"), ] containers = [ created_container_entry(CID1, IID2, mins_ago(5)), created_container_entry(CID2, IID3, mins_ago(5)) ] args = parse_arguments(['images', '--keep', '2', '--name', 'foo']) result = determine_images_to_remove(images, containers_result(containers), args) _assert_ids(result, IID1)
def test_regexes(): images = [ # should be deleted image_entry(IID1, mins_ago(200), "somewhere.org/someorg/foo:v1.1"), image_entry(IID2, mins_ago(190), "someorg/foo:v1.2"), # should be skipped image_entry(IID3, mins_ago(190), "awesomeorg/foo:v1.2"), image_entry(IID4, mins_ago(180), "someorg/notfoo:v1.3"), image_entry(IID5, mins_ago(180), "someorg/Foo:v1.3"), image_entry(IID6, mins_ago(180), "someorg/foo_nope:v1.3"), image_entry(IID7, mins_ago(180), "someorg/nope/foo:v1.3"), ] args = parse_arguments(['images', '--keep', '0', '--name', 'someorg/[^/]*', '--name', '[^/]*/foo']) result = determine_images_to_remove(images, [], args) _assert_ids(result, IID1, IID2)
def test_regexes(): images = [ # should be deleted image_entry(IID1, mins_ago(200), "somewhere.org/someorg/foo:v1.1"), image_entry(IID2, mins_ago(190), "someorg/foo:v1.2"), # should be skipped image_entry(IID3, mins_ago(190), "awesomeorg/foo:v1.2"), image_entry(IID4, mins_ago(180), "someorg/notfoo:v1.3"), image_entry(IID5, mins_ago(180), "someorg/Foo:v1.3"), image_entry(IID6, mins_ago(180), "someorg/foo_nope:v1.3"), image_entry(IID7, mins_ago(180), "someorg/nope/foo:v1.3"), ] args = parse_arguments([ 'images', '--keep', '0', '--name', 'someorg/[^/]*', '--name', '[^/]*/foo' ]) result = determine_images_to_remove(images, [], args) _assert_ids(result, IID1, IID2)