aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/container/image/ContainerImagePrunerTest.java
blob: a7307b5d1dec66cd56f9474fdf215b6441e073ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.container.image;

import com.yahoo.config.provision.DockerImage;
import com.yahoo.test.ManualClock;
import com.yahoo.vespa.hosted.node.admin.component.TaskContext;
import com.yahoo.vespa.hosted.node.admin.component.TestTaskContext;
import com.yahoo.vespa.hosted.node.admin.container.Container;
import com.yahoo.vespa.hosted.node.admin.container.ContainerEngineMock;
import com.yahoo.vespa.hosted.node.admin.container.ContainerId;
import com.yahoo.vespa.hosted.node.admin.container.ContainerName;
import com.yahoo.vespa.hosted.node.admin.container.ContainerResources;
import org.junit.Test;

import java.time.Duration;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertTrue;

/**
 * @author freva
 * @author mpolden
 */
public class ContainerImagePrunerTest {

    private final Tester tester = new Tester();

    @Test
    public void noImagesMeansNoUnusedImages() {
        tester.withExistingImages()
              .expectDeletedImages();
    }

    @Test
    public void singleImageWithoutContainersIsUnused() {
        tester.withExistingImages(image("image-1"))
              // Even though nothing is using the image, we will keep it for at least 1h
              .expectDeletedImagesAfterMinutes(0)
              .expectDeletedImagesAfterMinutes(30)
              .expectDeletedImagesAfterMinutes(30, "image-1");
    }

    @Test
    public void singleImageWithContainerIsUsed() {
        tester.withExistingImages(image("image-1"))
              .withExistingContainers(container("container-1", "image-1"))
              .expectDeletedImages();
    }

    @Test
    public void multipleUnusedImagesAreIdentified() {
        tester.withExistingImages(image("image-1"), image("image-2"))
              .expectDeletedImages("image-1", "image-2");
    }

    @Test
    public void unusedImagesWithMultipleTags() {
        tester.withExistingImages(image("image-1", "vespa-6", "vespa-6.28", "vespa:latest"))
              .expectDeletedImages("vespa-6", "vespa-6.28", "vespa:latest");
    }

    @Test
    public void unusedImagesWithMultipleUntagged() {
        tester.withExistingImages(image("image1", "<none>:<none>"),
                                  image("image2", "<none>:<none>"))
              .expectDeletedImages("image1", "image2");
    }

    @Test
    public void taggedImageWithNoContainersIsUnused() {
        tester.withExistingImages(image("image-1", "vespa-6"))
              .expectDeletedImages("vespa-6");
    }

    @Test
    public void reDownloadingImageIsNotImmediatelyDeleted() {
        tester.withExistingImages(image("image"))
              .expectDeletedImages("image") // After 1h we delete image
              .expectDeletedImagesAfterMinutes(0) // image is immediately re-downloaded, but is not deleted
              .expectDeletedImagesAfterMinutes(10)
              .expectDeletedImages("image"); // 1h after re-download it is deleted again
    }

    @Test
    public void reDownloadingImageIsNotImmediatelyDeletedWhenDeletingByTag() {
        tester.withExistingImages(image("image", "my-tag"))
              .expectDeletedImages("my-tag") // After 1h we delete image
              .expectDeletedImagesAfterMinutes(0) // image is immediately re-downloaded, but is not deleted
              .expectDeletedImagesAfterMinutes(10)
              .expectDeletedImages("my-tag"); // 1h after re-download it is deleted again
    }

    /** Same scenario as in {@link #multipleUnusedImagesAreIdentified()} */
    @Test
    public void doesNotDeleteExcludedByIdImages() {
        tester.withExistingImages(image("image-1"), image("image-2"))
              // Normally, image-1 should also be deleted, but because we exclude image-1 only image-2 is deleted
              .expectDeletedImages(List.of("image-1"), "image-2");
    }

    /** Same as in {@link #doesNotDeleteExcludedByIdImages()} but with tags */
    @Test
    public void doesNotDeleteExcludedByTagImages() {
        tester.withExistingImages(image("image-1", "vespa:6.288.16"), image("image-2", "vespa:6.289.94"))
              .expectDeletedImages(List.of("vespa:6.288.16"), "vespa:6.289.94");
    }

    @Test
    public void excludingNotDownloadedImageIsNoop() {
        tester.withExistingImages(image("image-1", "vespa:6.288.16"),
                                  image("image-2", "vespa:6.289.94"))
              .expectDeletedImages(List.of("vespa:6.300.1"), "vespa:6.288.16", "vespa:6.289.94", "rhel-6");
    }

    private static Image image(String id, String... tags) {
        return new Image(id, List.of(tags));
    }

    private static Container container(String name, String imageId) {
        return new Container(new ContainerId("id-of-" + name), new ContainerName(name), Instant.EPOCH,
                             Container.State.running, imageId, DockerImage.EMPTY, Map.of(),
                             42, 43, name + ".example.com", ContainerResources.UNLIMITED,
                             List.of(), true);
    }

    private static class Tester {

        private final ContainerEngineMock containerEngine = new ContainerEngineMock();
        private final TaskContext context = new TestTaskContext();
        private final ManualClock clock = new ManualClock();
        private final ContainerImagePruner pruner = new ContainerImagePruner(containerEngine, clock);
        private final Map<String, Integer> removalCountByImageId = new HashMap<>();

        private boolean initialized = false;

        private Tester withExistingImages(Image... images) {
            containerEngine.setImages(List.of(images));
            return this;
        }

        private Tester withExistingContainers(Container... containers) {
            containerEngine.addContainers(List.of(containers));
            return this;
        }

        private Tester expectDeletedImages(String... imageIds) {
            return expectDeletedImagesAfterMinutes(60, imageIds);
        }

        private Tester expectDeletedImages(List<String> excludedRefs, String... imageIds) {
            return expectDeletedImagesAfterMinutes(60, excludedRefs, imageIds);
        }

        private Tester expectDeletedImagesAfterMinutes(int minutesAfter, String... imageIds) {
            return expectDeletedImagesAfterMinutes(minutesAfter, List.of(), imageIds);
        }

        private Tester expectDeletedImagesAfterMinutes(int minutesAfter, List<String> excludedRefs, String... imageIds) {
            if (!initialized) {
                // Run once with a very long expiry to initialize internal state of existing images
                pruner.removeUnusedImages(context, List.of(), Duration.ofDays(999));
                initialized = true;
            }

            clock.advance(Duration.ofMinutes(minutesAfter));

            pruner.removeUnusedImages(context, excludedRefs, Duration.ofHours(1).minusSeconds(1));

            List.of(imageIds)
                  .forEach(imageId -> {
                      int newValue = removalCountByImageId.getOrDefault(imageId, 0) + 1;
                      removalCountByImageId.put(imageId, newValue);

                      assertTrue("Image " + imageId + " removed",
                                 containerEngine.listImages(context).stream().noneMatch(image -> image.id().equals(imageId)));
                  });
            return this;
        }
    }

}