summaryrefslogtreecommitdiffstats
path: root/docker-api/src/test/java/com/yahoo/vespa/hosted/dockerapi/DockerImageGarbageCollectionTest.java
blob: e83270b5949ccbca9ade431375518066a6810796 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.dockerapi;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.dockerjava.api.model.Image;
import org.junit.Test;

import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

/**
 * @author freva
 */
public class DockerImageGarbageCollectionTest {
    @Test
    public void noImagesMeansNoUnusedImages() throws Exception {
        new ImageGcTester(0)
                .withExistingImages()
                .expectUnusedImages();
    }

    @Test
    public void singleImageWithoutContainersIsUnused() throws Exception {
        new ImageGcTester(0)
                .withExistingImages(new ImageBuilder("image-1"))
                .expectUnusedImages("image-1");
    }

    @Test
    public void singleImageWithContainerIsUsed() throws Exception {
        new ImageGcTester(0)
                .withExistingImages(ImageBuilder.forId("image-1"))
                .andExistingContainers(ContainerBuilder.forId("container-1").withImageId("image-1"))
                .expectUnusedImages();
    }

    @Test
    public void multipleUnusedImagesAreIdentified() throws Exception {
        new ImageGcTester(0)
                .withExistingImages(
                        ImageBuilder.forId("image-1"),
                        ImageBuilder.forId("image-2"))
                .expectUnusedImages("image-1", "image-2");
    }

    @Test
    public void multipleUnusedLeavesAreIdentified() throws Exception {
        new ImageGcTester(0)
                .withExistingImages(
                        ImageBuilder.forId("parent-image"),
                        ImageBuilder.forId("image-1").withParentId("parent-image"),
                        ImageBuilder.forId("image-2").withParentId("parent-image"))
                .expectUnusedImages("image-1", "image-2", "parent-image");
    }

    @Test
    public void unusedLeafWithUsedSiblingIsIdentified() throws Exception {
        new ImageGcTester(0)
                .withExistingImages(
                        ImageBuilder.forId("parent-image"),
                        ImageBuilder.forId("image-1").withParentId("parent-image").withTags("latest"),
                        ImageBuilder.forId("image-2").withParentId("parent-image").withTags("1.24"))
                .andExistingContainers(ContainerBuilder.forId("vespa-node-1").withImageId("image-1"))
                .expectUnusedImages("1.24");
    }

    @Test
    public void unusedImagesWithMultipleTags() throws Exception {
        new ImageGcTester(0)
                .withExistingImages(
                        ImageBuilder.forId("parent-image"),
                        ImageBuilder.forId("image-1").withParentId("parent-image")
                                .withTags("vespa-6", "vespa-6.28", "vespa:latest"))
                .expectUnusedImages("vespa-6", "vespa-6.28", "vespa:latest", "parent-image");
    }

    @Test
    public void taggedImageWithNoContainersIsUnused() throws Exception {
        new ImageGcTester(0)
                .withExistingImages(ImageBuilder.forId("image-1").withTags("vespa-6"))
                .expectUnusedImages("vespa-6");
    }

    @Test
    public void unusedImagesWithSimpleImageGc() throws Exception {
        new ImageGcTester(20)
                .withExistingImages(
                        ImageBuilder.forId("parent-image").withLastUsedMinutesAgo(25),
                        ImageBuilder.forId("image-1").withParentId("parent-image").withLastUsedMinutesAgo(5))
                .expectUnusedImages();
    }

    @Test
    public void unusedImagesWithImageGc() throws Exception {
        new ImageGcTester(20)
                .withExistingImages(
                        ImageBuilder.forId("parent-1").withLastUsedMinutesAgo(40),
                        ImageBuilder.forId("parent-2").withTags("p-tag:1").withLastUsedMinutesAgo(10),
                        ImageBuilder.forId("image-1-1").withParentId("parent-1").withTags("i-tag:1", "i-tag:2", "i-tag-3").withLastUsedMinutesAgo(5),
                        ImageBuilder.forId("image-1-2").withParentId("parent-1").withLastUsedMinutesAgo(25),
                        ImageBuilder.forId("image-2-1").withParentId("parent-2").withTags("i-tag:4").withLastUsedMinutesAgo(30))
                .andExistingContainers(
                        ContainerBuilder.forId("cont-1").withImageId("image-1-1"))
                .expectUnusedImages("image-1-2", "i-tag:4");
    }

    private static class ImageGcTester {
        private static DockerImageGarbageCollector imageGC;

        private List<Image> existingImages = Collections.emptyList();
        private List<com.github.dockerjava.api.model.Container> existingContainers = Collections.emptyList();

        private ImageGcTester(int imageGcMinTimeInMinutes) {
            imageGC = new DockerImageGarbageCollector(Duration.ofMinutes(imageGcMinTimeInMinutes));
        }

        private ImageGcTester withExistingImages(final ImageBuilder... images) {
            this.existingImages = Arrays.stream(images)
                    .map(ImageBuilder::toImage)
                    .collect(Collectors.toList());
            return this;
        }

        private ImageGcTester andExistingContainers(final ContainerBuilder... containers) {
            this.existingContainers = Arrays.stream(containers)
                    .map(ContainerBuilder::toContainer)
                    .collect(Collectors.toList());
            return this;
        }

        private void expectUnusedImages(final String... imageIds) {
            final List<DockerImage> expectedUnusedImages = Arrays.stream(imageIds)
                    .map(DockerImage::new)
                    .collect(Collectors.toList());

            assertThat(imageGC.getUnusedDockerImages(existingImages, existingContainers), is(expectedUnusedImages));
        }
    }

    /**
     * Serializes object to a JSON string using Jackson, then deserializes it to an instance of toClass
     * (again using Jackson). This can be used to create Jackson classes with no public constructors.
     * @throws IllegalArgumentException if Jackson fails to serialize or deserialize.
     */
    private static <T> T createFrom(Class<T> toClass, Object object) throws IllegalArgumentException {
        final String serialized;
        try {
            serialized = new ObjectMapper().writeValueAsString(object);
        } catch (JsonProcessingException e) {
            throw new IllegalArgumentException("Failed to serialize object " + object + " to "
                    + toClass + " with Jackson: " + e, e);
        }
        try {
            return new ObjectMapper().readValue(serialized, toClass);
        } catch (IOException e) {
            throw new IllegalArgumentException("Failed to convert " + serialized + " to "
                    + toClass + " with Jackson: " + e, e);
        }
    }

    // Workaround for Image class that can't be instantiated directly in Java (instantiate via Jackson instead).
    private static class ImageBuilder {
        // Json property names must match exactly the property names in the Image class.
        @JsonProperty("Id")
        private final String id;

        @JsonProperty("ParentId")
        private String parentId = ""; // docker-java returns empty string and not null if the parent is not present

        @JsonProperty("RepoTags")
        private String[] repoTags = null;

        private ImageBuilder(String id) { this.id = id; }

        private static ImageBuilder forId(String id) { return new ImageBuilder(id); }
        private ImageBuilder withParentId(String parentId) { this.parentId = parentId; return this; }
        private ImageBuilder withTags(String... tags) { this.repoTags = tags; return this; }
        private ImageBuilder withLastUsedMinutesAgo(int minutesAgo) {
            ImageGcTester.imageGC.updateLastUsedTimeFor(id, Instant.now().minus(Duration.ofMinutes(minutesAgo)));
            return this;
        }

        private Image toImage() { return createFrom(Image.class, this); }
    }

    // Workaround for Container class that can't be instantiated directly in Java (instantiate via Jackson instead).
    private static class ContainerBuilder {
        // Json property names must match exactly the property names in the Container class.
        @JsonProperty("Id")
        private final String id;

        @JsonProperty("ImageID")
        private String imageId;

        private ContainerBuilder(String id) { this.id = id; }
        private static ContainerBuilder forId(final String id) { return new ContainerBuilder(id); }
        private ContainerBuilder withImageId(String imageId) { this.imageId = imageId; return this; }

        private com.github.dockerjava.api.model.Container toContainer() {
            return createFrom(com.github.dockerjava.api.model.Container.class, this);
        }
    }
}