forked from docker-java/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListImagesCmdIT.java
More file actions
79 lines (67 loc) · 3.13 KB
/
Copy pathListImagesCmdIT.java
File metadata and controls
79 lines (67 loc) · 3.13 KB
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
package com.github.dockerjava.cmd;
import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.exception.DockerException;
import com.github.dockerjava.api.model.Image;
import com.github.dockerjava.api.model.Info;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import static com.github.dockerjava.utils.TestUtils.isNotSwarm;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.emptyArray;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.emptyString;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertTrue;
public class ListImagesCmdIT extends CmdIT {
public static final Logger LOG = LoggerFactory.getLogger(ListImagesCmdIT.class);
@Test
public void listImages() throws DockerException {
List<Image> images = dockerRule.getClient().listImagesCmd().withShowAll(true).exec();
assertThat(images, notNullValue());
LOG.info("Images List: {}", images);
Info info = dockerRule.getClient().infoCmd().exec();
if (isNotSwarm(dockerRule.getClient())) {
assertThat(images.size(), equalTo(info.getImages()));
}
Image img = images.get(0);
assertThat(img.getCreated(), is(greaterThan(0L)));
assertThat(img.getVirtualSize(), is(greaterThan(0L)));
assertThat(img.getId(), not(is(emptyString())));
assertThat(img.getRepoTags(), not(emptyArray()));
}
@Test
public void listImagesWithDanglingFilter() throws DockerException {
String imageId = createDanglingImage();
List<Image> images = dockerRule.getClient().listImagesCmd().withDanglingFilter(true).withShowAll(true)
.exec();
assertThat(images, notNullValue());
LOG.info("Images List: {}", images);
assertThat(images.size(), is(greaterThan(0)));
Boolean imageInFilteredList = isImageInFilteredList(images, imageId);
assertTrue(imageInFilteredList);
}
private boolean isImageInFilteredList(List<Image> images, String expectedImageId) {
for (Image image : images) {
if (expectedImageId.equals(image.getId())) {
return true;
}
}
return false;
}
private String createDanglingImage() {
CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox").withCmd("sleep", "9999").exec();
LOG.info("Created container: {}", container.toString());
assertThat(container.getId(), not(is(emptyString())));
dockerRule.getClient().startContainerCmd(container.getId()).exec();
LOG.info("Committing container {}", container.toString());
String imageId = dockerRule.getClient().commitCmd(container.getId()).exec();
dockerRule.getClient().stopContainerCmd(container.getId()).exec();
dockerRule.getClient().removeContainerCmd(container.getId()).exec();
return imageId;
}
}