aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/container/image/ContainerImagePrunerTest.java
blob: f6d941c429977ff96e1e85203d83859d7b32e4cd (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright Verizon Media. 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 com.yahoo.vespa.hosted.node.admin.container.image.ContainerImagePruner;
import com.yahoo.vespa.hosted.node.admin.container.image.Image;
import org.junit.Test;

import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

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 multipleUnusedLeavesAreIdentified() {
        tester.withExistingImages(image("parent-image"),
                                  image("image-1", "parent-image"),
                                  image("image-2", "parent-image"))
              .expectDeletedImages("image-1", "image-2", "parent-image");
    }


    @Test
    public void unusedLeafWithUsedSiblingIsIdentified() {
        tester.withExistingImages(image("parent-image"),
                                  image("image-1", "parent-image", "latest"),
                                  image("image-2", "parent-image", "1.24"))
              .withExistingContainers(container("vespa-node-1", "image-1"))
              .expectDeletedImages("1.24"); // Deleting the only tag will delete the image
    }


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


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


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


    @Test
    public void unusedImagesWithSimpleImageGc() {
        tester.withExistingImages(image("parent-image"))
              .expectDeletedImagesAfterMinutes(30)
              .withExistingImages(image("parent-image"),
                                    image("image-1", "parent-image"))
              .expectDeletedImagesAfterMinutes(0)
              .expectDeletedImagesAfterMinutes(30)
              // At this point, parent-image has been unused for 1h, but image-1 depends on parent-image and it has
              // only been unused for 30m, so we cannot delete parent-image yet. 30 mins later both can be removed
              .expectDeletedImagesAfterMinutes(30, "image-1", "parent-image");
    }


    @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", null, "image-1", "my-tag"))
              .expectDeletedImages("image-1", "my-tag") // After 1h we delete image
              .expectDeletedImagesAfterMinutes(0) // image is immediately re-downloaded, but is not deleted
              .expectDeletedImagesAfterMinutes(10)
              .expectDeletedImages("image-1", "my-tag"); // 1h after re-download it is deleted again
    }

    /** Same scenario as in {@link #multipleUnusedImagesAreIdentified()} */
    @Test
    public void doesNotDeleteExcludedByIdImages() {
        tester.withExistingImages(image("parent-image"),
                                  image("image-1", "parent-image"),
                                  image("image-2", "parent-image"))
              // Normally, image-1 and parent-image should also be deleted, but because we exclude image-1
              // we cannot delete parent-image, so 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("parent-image", "rhel-6"),
                                  image("image-1", "parent-image", "vespa:6.288.16"),
                                  image("image-2", "parent-image", "vespa:6.289.94"))
              .expectDeletedImages(List.of("vespa:6.288.16"), "vespa:6.289.94");
    }

    @Test
    public void exludingNotDownloadedImageIsNoop() {
        tester.withExistingImages(image("parent-image", "rhel-6"),
                                  image("image-1", "parent-image", "vespa:6.288.16"),
                                  image("image-2", "parent-image", "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) {
        return image(id, null);
    }

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

    private static Container container(String name, String imageId) {
        return new Container(new ContainerId("id-of-" + name), new ContainerName(name),
                             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, Collections.emptyList(), 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));

            Arrays.stream(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;
        }
    }

}