@BeforeClass protected void init() { if (api.getImageApi().inspectImage(ALPINE_IMAGE_TAG) == null) { CreateImageOptions options = CreateImageOptions.Builder.fromImage(ALPINE_IMAGE_TAG); InputStream createImageStream = api.getImageApi().createImage(options); consumeStream(createImageStream); } image = api.getImageApi().inspectImage(ALPINE_IMAGE_TAG); assertNotNull(image); }
@Test(dependsOnMethods = "testTagImage") public void testListImages() { List<ImageSummary> listImages = api().listImages(); assertNotNull(listImages); Optional<ImageSummary> summary = Iterables.tryFind(listImages, new Predicate<ImageSummary>() { @Override public boolean apply(ImageSummary input) { return input.repoTags().contains("jclouds:testTag"); } }); assertThat(summary).isPresent(); }
@Test(dependsOnMethods = "testListImages", alwaysRun = true) public void testDeleteImage() { consumeStream(api().deleteImage(String.format("%s:%s", DEFAULT_IMAGE, DEFAULT_TAG))); assertNull(api().inspectImage(String.format("%s:%s", DEFAULT_IMAGE, DEFAULT_TAG))); assertNotNull(api().inspectImage(image.id()), "Image should should still exist after removing original tag. There is a newly added tag referencing it."); consumeStream(api().deleteImage("jclouds:testTag")); assertNull(api().inspectImage("jclouds:testTag")); }
@Test(dependsOnMethods = "testInspectImage") public void testTagImage() { api.getImageApi().tagImage(image.id(), "jclouds", "testTag", true); Image taggedImage = api.getImageApi().inspectImage("jclouds:testTag"); assertEquals(taggedImage.id(), image.id(), "Newly added image tag should point to the same image ID."); }
/** * Method based on {@link org.jclouds.docker.features.ImageApi#listImages()}. It retrieves additional * information by inspecting each image. * * @see org.jclouds.compute.ComputeServiceAdapter#listImages() */ @Override public Set<Image> listImages() { Set<Image> images = Sets.newHashSet(); for (ImageSummary imageSummary : api.getImageApi().listImages()) { // less efficient than just listImages but returns richer json that needs repoTags coming from listImages Image inspected = api.getImageApi().inspectImage(imageSummary.id()); inspected = Image.create(inspected.id(), inspected.author(), inspected.comment(), inspected.config(), inspected.containerConfig(), inspected.parent(), inspected.created(), inspected.container(), inspected.dockerVersion(), inspected.architecture(), inspected.os(), inspected.size(), inspected.virtualSize(), imageSummary.repoTags()); images.add(inspected); } return images; }
@Override public Image getImage(final String imageIdOrName) { checkNotNull(imageIdOrName); if (imageIdOrName.startsWith("sha256")) { // less efficient than just inspectImage but listImages return repoTags return find(listImages(), new Predicate<Image>() { @Override public boolean apply(Image input) { // Only attempt match on id as we should try to pull again anyway if using name return input.id().equals(imageIdOrName); } }, null); } // Image is not cached or getting image by name so try to pull it api.getImageApi().createImage(CreateImageOptions.Builder.fromImage(imageIdOrName)); // as above this ensure repotags are returned return find(listImages(), createPredicateMatchingRepoTags(imageIdOrName), null); }
@Test(dependsOnMethods = "testCreateImage") public void testInspectImage() { image = api.getImageApi().inspectImage(String.format("%s:%s", DEFAULT_IMAGE, DEFAULT_TAG)); assertNotNull(image); }
@AfterClass @Override protected void tearDownContext() { super.tearDownContext(); if (defaultImage != null) { imageApi().deleteImage(SSHABLE_IMAGE + ":" + SSHABLE_IMAGE_TAG, DeleteImageOptions.Builder.force(true)); } }
public void testTagImage() throws Exception { MockWebServer server = mockWebServer(new MockResponse().setResponseCode(201)); ImageApi api = api(DockerApi.class, server.getUrl("/").toString()).getImageApi(); try { api.tagImage("633fcd11259e8d6bccfbb59a4086b95b0d0fb44edfc3912000ef1f70e8a7bfc6", "jclouds", "testTag", true); assertSent(server, "POST", "/images/633fcd11259e8d6bccfbb59a4086b95b0d0fb44edfc3912000ef1f70e8a7bfc6/tag?repo=jclouds&tag=testTag&force=true"); } finally { server.shutdown(); } }
ImageApi api = api(DockerApi.class, server.getUrl("/").toString()).getImageApi(); try { assertEquals(api.getHistory("ubuntu"), new HistoryParseTest().expected()); assertSent(server, "GET", "/images/ubuntu/history"); assertEquals(api.getHistory("fcf9d588ee9ab46c5a888e67f892fac66e6396eb195a743e50c0c5f9a4710e66"), ImmutableList.of( ImageHistory.create("sha256:fcf9d588ee9ab46c5a888e67f892fac66e6396eb195a743e50c0c5f9a4710e66", List<ImageHistory> historyList = api.getHistory("missing-image"); assertNotNull(historyList); assertTrue(historyList.isEmpty());
/** * Method based on {@link org.jclouds.docker.features.ImageApi#listImages()}. It retrieves additional * information by inspecting each image. * * @see org.jclouds.compute.ComputeServiceAdapter#listImages() */ @Override public Set<Image> listImages() { Set<Image> images = Sets.newHashSet(); for (ImageSummary imageSummary : api.getImageApi().listImages()) { // less efficient than just listImages but returns richer json that needs repoTags coming from listImages Image inspected = api.getImageApi().inspectImage(imageSummary.id()); inspected = Image.create(inspected.id(), inspected.author(), inspected.comment(), inspected.config(), inspected.containerConfig(), inspected.parent(), inspected.created(), inspected.container(), inspected.dockerVersion(), inspected.architecture(), inspected.os(), inspected.size(), inspected.virtualSize(), imageSummary.repoTags()); images.add(inspected); } return images; }
@Override public Image getImage(final String imageIdOrName) { checkNotNull(imageIdOrName); if (imageIdOrName.startsWith("sha256")) { // less efficient than just inspectImage but listImages return repoTags return find(listImages(), new Predicate<Image>() { @Override public boolean apply(Image input) { // Only attempt match on id as we should try to pull again anyway if using name return input.id().equals(imageIdOrName); } }, null); } // Image is not cached or getting image by name so try to pull it api.getImageApi().createImage(CreateImageOptions.Builder.fromImage(imageIdOrName)); // as above this ensure repotags are returned return find(listImages(), createPredicateMatchingRepoTags(imageIdOrName), null); }
/** * Build a new image with 2 tags on it in the test preparation phase. * * @see org.jclouds.apis.BaseContextLiveTest#setupContext() */ @Override @BeforeClass(groups = { "integration", "live" }) public void setupContext() { super.setupContext(); final String tag = toTag(IMAGE_REPOSITORY, IMAGE_TAG_1); removeImageIfExists(api(), tag); removeImageIfExists(api(), toTag(IMAGE_REPOSITORY, IMAGE_TAG_2)); BuildOptions options = BuildOptions.Builder.tag(tag).verbose(false).nocache(false); InputStream buildImageStream; try { buildImageStream = api().getMiscApi().build(BaseDockerApiLiveTest.tarredDockerfile(), options); consumeStreamSilently(buildImageStream); } catch (IOException e) { throw new RuntimeException("Error occured during building Docker image.", e); } image = api().getImageApi().inspectImage(tag); api().getImageApi().tagImage(image.id(), IMAGE_REPOSITORY, IMAGE_TAG_2, true); }
public void testGetImageNotHiddenByCache() { // Ensure image to be tested is unknown to jclouds and docker and that // cache is warm assertNull(findImageFromListImages(CHUANWEN_COWSAY)); assertNull(api.getImageApi().inspectImage(CHUANWEN_COWSAY)); // Get new image adapter.getImage(CHUANWEN_COWSAY); assertNotNull(findImageFromListImages(CHUANWEN_COWSAY), "New image is not available from listImages presumably due to caching"); }
@AfterClass protected void tearDown() { if (container != null) { if (api.getContainerApi().inspectContainer(container.id()) != null) { api.getContainerApi().removeContainer(container.id(), RemoveContainerOptions.Builder.force(true)); } } if (image != null) { api.getImageApi().deleteImage(ALPINE_IMAGE_TAG); } }
@BeforeClass protected void init() { if (api.getImageApi().inspectImage(ALPINE_IMAGE_TAG) == null) { CreateImageOptions options = CreateImageOptions.Builder.fromImage(ALPINE_IMAGE_TAG); InputStream createImageStream = api.getImageApi().createImage(options); consumeStream(createImageStream); } image = api.getImageApi().inspectImage(ALPINE_IMAGE_TAG); assertNotNull(image); Config containerConfig = Config.builder().image(image.id()) .cmd(ImmutableList.of("/bin/sh", "-c", "touch hello; while true; do echo hello world; sleep 1; done")) .build(); container = api.getContainerApi().createContainer("miscApiTest", containerConfig); assertNotNull(container); api.getContainerApi().startContainer(container.id()); assertTrue(api.getContainerApi().inspectContainer(container.id()).state().running()); }
/** * Method based on {@link org.jclouds.docker.features.ImageApi#listImages()}. It retrieves additional * information by inspecting each image. * * @see org.jclouds.compute.ComputeServiceAdapter#listImages() */ @Override public Set<Image> listImages() { Set<Image> images = Sets.newHashSet(); for (ImageSummary imageSummary : api.getImageApi().listImages()) { // less efficient than just listImages but returns richer json that needs repoTags coming from listImages Image inspected = api.getImageApi().inspectImage(imageSummary.id()); inspected = Image.create(inspected.id(), inspected.author(), inspected.comment(), inspected.config(), inspected.containerConfig(), inspected.parent(), inspected.created(), inspected.container(), inspected.dockerVersion(), inspected.architecture(), inspected.os(), inspected.size(), inspected.virtualSize(), imageSummary.repoTags()); images.add(inspected); } return images; }
@Override public Image getImage(final String imageIdOrName) { checkNotNull(imageIdOrName); if (imageIdOrName.startsWith("sha256")) { // less efficient than just inspectImage but listImages return repoTags return find(listImages(), new Predicate<Image>() { @Override public boolean apply(Image input) { // Only attempt match on id as we should try to pull again anyway if using name return input.id().equals(imageIdOrName); } }, null); } // Image is not cached or getting image by name so try to pull it api.getImageApi().createImage(CreateImageOptions.Builder.fromImage(imageIdOrName)); // as above this ensure repotags are returned return find(listImages(), createPredicateMatchingRepoTags(imageIdOrName), null); }
/** * Removes Docker image if it's present on the Docker host. Docker Image API * is used to inspect and remove image (({@link ImageApi#deleteImage(String)} * method). * * @param dockerApi * DockerApi instance (must be not-<code>null</code>) * @param imageName * image to be deleted (must be not-<code>null</code>) */ public static void removeImageIfExists(DockerApi dockerApi, String imageName) { Preconditions.checkNotNull(dockerApi, "DockerApi instance has to be provided"); Preconditions.checkNotNull(imageName, "Docker image name has to be provided"); final ImageApi imageApi = dockerApi.getImageApi(); if (null != imageApi.inspectImage(imageName)) { consumeStreamSilently(imageApi.deleteImage(imageName, DeleteImageOptions.Builder.force(true))); } } }
/** * Asserts that the new image exists and tags were created successfully in * the test preparation phase ({@link #setupContext()} method). */ @Test public void testImageCreated() { assertNotNull(image); final String imageId = image.id(); assertNotNull(imageId); List<ImageSummary> listImages = api().getImageApi().listImages(); assertNotNull(listImages); ImageSummary testImage = Iterables.find(listImages, new Predicate<ImageSummary>() { @Override public boolean apply(ImageSummary input) { return imageId.equals(input.id()); } }); assertEquals(testImage.repoTags().size(), 2, "Unexpected number of tags on the image."); assertThat(testImage.repoTags()).contains(toTag(IMAGE_REPOSITORY, IMAGE_TAG_1), toTag(IMAGE_REPOSITORY, IMAGE_TAG_2)); }