org.testcontainers.DockerClientFactory Java Examples
The following examples show how to use
org.testcontainers.DockerClientFactory.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ContainerState.java From testcontainers-java with MIT License | 6 votes |
/** * Streams a file which resides inside the container * * @param containerPath path to file which is copied from container * @param function function that takes InputStream of the copied file */ @SneakyThrows default <T> T copyFileFromContainer(String containerPath, ThrowingFunction<InputStream, T> function) { if (!isCreated()) { throw new IllegalStateException("copyFileFromContainer can only be used when the Container is created."); } DockerClient dockerClient = DockerClientFactory.instance().client(); try ( InputStream inputStream = dockerClient.copyArchiveFromContainerCmd(getContainerId(), containerPath).exec(); TarArchiveInputStream tarInputStream = new TarArchiveInputStream(inputStream) ) { tarInputStream.getNextTarEntry(); return function.apply(tarInputStream); } }
Example #2
Source File: EtcdContainer.java From etcd4j with Apache License 2.0 | 6 votes |
private WaitStrategy waitStrategy() { return new AbstractWaitStrategy() { @Override protected void waitUntilReady() { final DockerClient client = DockerClientFactory.instance().client(); final WaitingConsumer waitingConsumer = new WaitingConsumer(); LogUtils.followOutput(client, waitStrategyTarget.getContainerId(), waitingConsumer); try { waitingConsumer.waitUntil( f -> f.getUtf8String().contains("etcdserver: published"), startupTimeout.getSeconds(), TimeUnit.SECONDS, 1 ); } catch (TimeoutException e) { throw new ContainerLaunchException("Timed out"); } } }; }
Example #3
Source File: ApplicationContainer.java From microshed-testing with Apache License 2.0 | 6 votes |
public DefaultServerAdapter() { if (isHollow) { defaultHttpPort = -1; } else { InspectImageResponse imageData = DockerClientFactory.instance().client().inspectImageCmd(getDockerImageName()).exec(); LOG.info("Found exposed ports: " + Arrays.toString(imageData.getContainerConfig().getExposedPorts())); int bestChoice = -1; for (ExposedPort exposedPort : imageData.getContainerConfig().getExposedPorts()) { int port = exposedPort.getPort(); // If any ports end with 80, assume they are HTTP ports if (Integer.toString(port).endsWith("80")) { bestChoice = port; break; } else if (bestChoice == -1) { // if no ports match *80, then pick the first port bestChoice = port; } } defaultHttpPort = bestChoice; LOG.info("Automatically selecting default HTTP port: " + defaultHttpPort); } }
Example #4
Source File: AuthenticatedImagePullTest.java From testcontainers-java with MIT License | 6 votes |
@BeforeClass public static void setUp() throws InterruptedException { originalAuthLocatorSingleton = RegistryAuthLocator.instance(); client = DockerClientFactory.instance().client(); String testRegistryAddress = authenticatedRegistry.getHost() + ":" + authenticatedRegistry.getFirstMappedPort(); testImageName = testRegistryAddress + "/alpine"; testImageNameWithTag = testImageName + ":latest"; final DockerImageName expectedName = new DockerImageName(testImageNameWithTag); final AuthConfig authConfig = new AuthConfig() .withUsername("testuser") .withPassword("notasecret") .withRegistryAddress("http://" + testRegistryAddress); // Replace the RegistryAuthLocator singleton with our mock, for the duration of this test final RegistryAuthLocator mockAuthLocator = Mockito.mock(RegistryAuthLocator.class); RegistryAuthLocator.setInstance(mockAuthLocator); when(mockAuthLocator.lookupAuthConfig(eq(expectedName), any())) .thenReturn(authConfig); // a push will use the auth locator for authentication, although that isn't the goal of this test putImageInRegistry(); }
Example #5
Source File: DockerCassandra.java From james-project with Apache License 2.0 | 6 votes |
public DockerCassandra(String imageName, AdditionalDockerFileStep additionalSteps) { client = DockerClientFactory.instance().client(); boolean doNotDeleteImageAfterUsage = false; cassandraContainer = new GenericContainer<>( new ImageFromDockerfile(imageName,doNotDeleteImageAfterUsage) .withDockerfileFromBuilder(builder -> additionalSteps.applyStep(builder .from("cassandra:3.11.3") .env("ENV CASSANDRA_CONFIG", "/etc/cassandra") .run("echo \"-Xms" + CASSANDRA_MEMORY + "M\" >> " + JVM_OPTIONS) .run("echo \"-Xmx" + CASSANDRA_MEMORY + "M\" >> " + JVM_OPTIONS) .run("sed", "-i", "s/auto_snapshot: true/auto_snapshot: false/g", "/etc/cassandra/cassandra.yaml") .run("echo 'authenticator: PasswordAuthenticator' >> /etc/cassandra/cassandra.yaml") .run("echo 'authorizer: org.apache.cassandra.auth.CassandraAuthorizer' >> /etc/cassandra/cassandra.yaml")) .build())) .withTmpFs(ImmutableMap.of("/var/lib/cassandra", "rw,noexec,nosuid,size=200m")) .withExposedPorts(CASSANDRA_PORT) .withLogConsumer(DockerCassandra::displayDockerLog); cassandraContainer .waitingFor(new CassandraWaitStrategy(cassandraContainer)); }
Example #6
Source File: ContainerState.java From testcontainers-java with MIT License | 6 votes |
/** * * Copies a file to the container. * * @param transferable file which is copied into the container * @param containerPath destination path inside the container */ @SneakyThrows(IOException.class) default void copyFileToContainer(Transferable transferable, String containerPath) { if (!isCreated()) { throw new IllegalStateException("copyFileToContainer can only be used with created / running container"); } try ( ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); TarArchiveOutputStream tarArchive = new TarArchiveOutputStream(byteArrayOutputStream) ) { tarArchive.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX); transferable.transferTo(tarArchive, containerPath); tarArchive.finish(); DockerClientFactory.instance().client() .copyArchiveToContainerCmd(getContainerId()) .withTarInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray())) .withRemotePath("/") .exec(); } }
Example #7
Source File: NetworkTest.java From testcontainers-java with MIT License | 6 votes |
@Test public void testReusability() throws Exception { try (Network network = Network.newNetwork()) { String firstId = network.getId(); assertNotNull( "Network exists", DockerClientFactory.instance().client().inspectNetworkCmd().withNetworkId(firstId).exec() ); network.close(); assertNotEquals( "New network created", firstId, DockerClientFactory.instance().client().inspectNetworkCmd().withNetworkId(network.getId()).exec().getId() ); } }
Example #8
Source File: ReusabilityUnitTests.java From testcontainers-java with MIT License | 6 votes |
@Test public void shouldSetLabelsIfEnvironmentDoesNotSupportReuse() { Mockito.doReturn(false).when(TestcontainersConfiguration.getInstance()).environmentSupportsReuse(); AtomicReference<CreateContainerCmd> commandRef = new AtomicReference<>(); String containerId = randomContainerId(); when(client.createContainerCmd(any())).then(createContainerAnswer(containerId, commandRef::set)); when(client.startContainerCmd(containerId)).then(startContainerAnswer()); when(client.inspectContainerCmd(containerId)).then(inspectContainerAnswer()); container.start(); assertThat(commandRef) .isNotNull() .satisfies(command -> { assertThat(command.get().getLabels()) .containsKeys(DockerClientFactory.TESTCONTAINERS_SESSION_ID_LABEL); }); }
Example #9
Source File: ImagePullPolicyTest.java From testcontainers-java with MIT License | 6 votes |
@BeforeClass public static void beforeClass() throws Exception { String testRegistryAddress = registry.getHost() + ":" + registry.getFirstMappedPort(); String testImageName = testRegistryAddress + "/image-pull-policy-test"; String tag = UUID.randomUUID().toString(); imageName = testImageName + ":" + tag; DockerClient client = DockerClientFactory.instance().client(); String dummySourceImage = "hello-world:latest"; client.pullImageCmd(dummySourceImage).exec(new PullImageResultCallback()).awaitCompletion(); String dummyImageId = client.inspectImageCmd(dummySourceImage).exec().getId(); // push the image to the registry client.tagImageCmd(dummyImageId, testImageName, tag).exec(); client.pushImageCmd(imageName) .exec(new PushImageResultCallback()) .awaitCompletion(1, TimeUnit.MINUTES); }
Example #10
Source File: LocalstackContainerTest.java From testcontainers-java with MIT License | 6 votes |
@Test public void sqsTestOverBridgeNetwork() { AmazonSQS sqs = AmazonSQSClientBuilder.standard() .withEndpointConfiguration(localstack.getEndpointConfiguration(SQS)) .withCredentials(localstack.getDefaultCredentialsProvider()) .build(); CreateQueueResult queueResult = sqs.createQueue("baz"); String fooQueueUrl = queueResult.getQueueUrl(); assertThat("Created queue has external hostname URL", fooQueueUrl, containsString("http://" + DockerClientFactory.instance().dockerHostIpAddress() + ":" + localstack.getMappedPort(SQS.getPort()))); sqs.sendMessage(fooQueueUrl, "test"); final long messageCount = sqs.receiveMessage(fooQueueUrl).getMessages().stream() .filter(message -> message.getBody().equals("test")) .count(); assertEquals("the sent message can be received", 1L, messageCount); }
Example #11
Source File: Network.java From testcontainers-java with MIT License | 6 votes |
private String create() { CreateNetworkCmd createNetworkCmd = DockerClientFactory.instance().client().createNetworkCmd(); createNetworkCmd.withName(name); createNetworkCmd.withCheckDuplicate(true); if (enableIpv6 != null) { createNetworkCmd.withEnableIpv6(enableIpv6); } if (driver != null) { createNetworkCmd.withDriver(driver); } for (Consumer<CreateNetworkCmd> consumer : createNetworkCmdModifiers) { consumer.accept(createNetworkCmd); } Map<String, String> labels = createNetworkCmd.getLabels(); labels = new HashMap<>(labels != null ? labels : Collections.emptyMap()); labels.putAll(DockerClientFactory.DEFAULT_LABELS); createNetworkCmd.withLabels(labels); return createNetworkCmd.exec().getId(); }
Example #12
Source File: SesResource.java From quarkus-quickstarts with Apache License 2.0 | 5 votes |
@Override public Map<String, String> start() { DockerClientFactory.instance().client(); try { services = new LocalStackContainer("0.11.1").withServices(Service.SES); services.start(); StaticCredentialsProvider staticCredentials = StaticCredentialsProvider .create(AwsBasicCredentials.create("accesskey", "secretKey")); client = SesClient.builder() .endpointOverride(new URI(endpoint())) .credentialsProvider(staticCredentials) .httpClientBuilder(UrlConnectionHttpClient.builder()) .region(Region.US_EAST_1).build(); client.verifyEmailIdentity(req -> req.emailAddress(FROM_EMAIL)); client.verifyEmailIdentity(req -> req.emailAddress(TO_EMAIL)); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("Could not start localstack server", e); } Map<String, String> properties = new HashMap<>(); properties.put("quarkus.ses.endpoint-override", endpoint()); properties.put("quarkus.ses.aws.region", "us-east-1"); properties.put("quarkus.ses.aws.credentials.type", "static"); properties.put("quarkus.ses.aws.credentials.static-provider.access-key-id", "accessKey"); properties.put("quarkus.ses.aws.credentials.static-provider.secret-access-key", "secretKey"); return properties; }
Example #13
Source File: DockerComposeContainer.java From testcontainers-java with MIT License | 5 votes |
private void pullImages() { // Pull images using our docker client rather than compose itself, // (a) as a workaround for https://github.com/docker/compose/issues/5854, which prevents authenticated image pulls being possible when credential helpers are in use // (b) so that credential helper-based auth still works when compose is running from within a container parsedComposeFiles.stream() .flatMap(it -> it.getDependencyImageNames().stream()) .forEach(imageName -> { try { log.info("Preemptively checking local images for '{}', referenced via a compose file or transitive Dockerfile. If not available, it will be pulled.", imageName); DockerClientFactory.instance().checkAndPullImage(dockerClient, imageName); } catch (Exception e) { log.warn("Unable to pre-fetch an image ({}) depended upon by Docker Compose build - startup will continue but may fail. Exception message was: {}", imageName, e.getMessage()); } }); }
Example #14
Source File: NetworkTest.java From testcontainers-java with MIT License | 5 votes |
@Test public void testModifiers() throws Exception { try ( Network network = Network.builder() .createNetworkCmdModifier(cmd -> cmd.withDriver("macvlan")) .build(); ) { String id = network.getId(); assertEquals( "Flag is set", "macvlan", DockerClientFactory.instance().client().inspectNetworkCmd().withNetworkId(id).exec().getDriver() ); } }
Example #15
Source File: KmsResource.java From quarkus-quickstarts with Apache License 2.0 | 5 votes |
@Override public Map<String, String> start() { DockerClientFactory.instance().client(); String masterKeyId; try { services = new KmsContainer(); services.start(); StaticCredentialsProvider staticCredentials = StaticCredentialsProvider .create(AwsBasicCredentials.create("accesskey", "secretKey")); client = KmsClient.builder() .endpointOverride(new URI(endpoint())) .credentialsProvider(staticCredentials) .httpClientBuilder(UrlConnectionHttpClient.builder()) .region(Region.US_EAST_1).build(); masterKeyId = client.createKey().keyMetadata().keyId(); client.generateDataKey(r -> r.keyId(masterKeyId).keySpec(DataKeySpec.AES_256)); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("Could not start localstack server", e); } Map<String, String> properties = new HashMap<>(); properties.put("quarkus.kms.endpoint-override", endpoint()); properties.put("quarkus.kms.aws.region", "us-east-1"); properties.put("quarkus.kms.aws.credentials.type", "static"); properties.put("quarkus.kms.aws.credentials.static-provider.access-key-id", "accessKey"); properties.put("quarkus.kms.aws.credentials.static-provider.secret-access-key", "secretKey"); properties.put("key.arn", masterKeyId); return properties; }
Example #16
Source File: SnsResource.java From quarkus-quickstarts with Apache License 2.0 | 5 votes |
@Override public Map<String, String> start() { DockerClientFactory.instance().client(); String topicArn; try { services = new LocalStackContainer("0.11.1").withServices(Service.SNS); services.start(); StaticCredentialsProvider staticCredentials = StaticCredentialsProvider .create(AwsBasicCredentials.create("accesskey", "secretKey")); client = SnsClient.builder() .endpointOverride(new URI(endpoint())) .credentialsProvider(staticCredentials) .httpClientBuilder(UrlConnectionHttpClient.builder()) .region(Region.US_EAST_1).build(); topicArn = client.createTopic(t -> t.name(TOPIC_NAME)).topicArn(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("Could not start ocalstack server", e); } Map<String, String> properties = new HashMap<>(); properties.put("quarkus.sns.endpoint-override", endpoint()); properties.put("quarkus.sns.aws.region", "us-east-1"); properties.put("quarkus.sns.aws.credentials.type", "static"); properties.put("quarkus.sns.aws.credentials.static-provider.access-key-id", "accessKey"); properties.put("quarkus.sns.aws.credentials.static-provider.secret-access-key", "secretKey"); properties.put("topic.arn", topicArn); return properties; }
Example #17
Source File: S3Resource.java From quarkus-quickstarts with Apache License 2.0 | 5 votes |
@Override public Map<String, String> start() { DockerClientFactory.instance().client(); try { s3 = new LocalStackContainer().withServices(Service.S3); s3.start(); client = S3Client.builder() .endpointOverride(new URI(endpoint())) .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("accesskey", "secretKey"))) .httpClientBuilder(UrlConnectionHttpClient.builder()) .region(Region.US_EAST_1).build(); client.createBucket(b -> b.bucket(BUCKET_NAME)); } catch (Exception e) { throw new RuntimeException("Could not start S3 localstack server", e); } Map<String, String> properties = new HashMap<>(); properties.put("quarkus.s3.endpoint-override", endpoint()); properties.put("quarkus.s3.aws.region", "us-east-1"); properties.put("quarkus.s3.aws.credentials.type", "static"); properties.put("quarkus.s3.aws.credentials.static-provider.access-key-id", "accessKey"); properties.put("quarkus.s3.aws.credentials.static-provider.secret-access-key", "secretKey"); properties.put("bucket.name", BUCKET_NAME); return properties; }
Example #18
Source File: SqsResource.java From quarkus-quickstarts with Apache License 2.0 | 5 votes |
@Override public Map<String, String> start() { DockerClientFactory.instance().client(); String queueUrl; try { services = new LocalStackContainer("0.11.1").withServices(Service.SQS); services.start(); StaticCredentialsProvider staticCredentials = StaticCredentialsProvider .create(AwsBasicCredentials.create("accesskey", "secretKey")); client = SqsClient.builder() .endpointOverride(new URI(endpoint())) .credentialsProvider(staticCredentials) .httpClientBuilder(UrlConnectionHttpClient.builder()) .region(Region.US_EAST_1).build(); queueUrl = client.createQueue(q -> q.queueName(QUEUE_NAME)).queueUrl(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("Could not start localstack server", e); } Map<String, String> properties = new HashMap<>(); properties.put("quarkus.sqs.endpoint-override", endpoint()); properties.put("quarkus.sqs.aws.region", "us-east-1"); properties.put("quarkus.sqs.aws.credentials.type", "static"); properties.put("quarkus.sqs.aws.credentials.static-provider.access-key-id", "accessKey"); properties.put("quarkus.sqs.aws.credentials.static-provider.secret-access-key", "secretKey"); properties.put("queue.url", queueUrl); return properties; }
Example #19
Source File: TestcontainersExtension.java From testcontainers-java with MIT License | 5 votes |
boolean isDockerAvailable() { try { DockerClientFactory.instance().client(); return true; } catch (Throwable ex) { return false; } }
Example #20
Source File: ImageFromDockerfile.java From testcontainers-java with MIT License | 5 votes |
private void prePullDependencyImages(Set<String> imagesToPull) { final DockerClient dockerClient = DockerClientFactory.instance().client(); imagesToPull.forEach(imageName -> { try { log.info("Pre-emptively checking local images for '{}', referenced via a Dockerfile. If not available, it will be pulled.", imageName); DockerClientFactory.instance().checkAndPullImage(dockerClient, imageName); } catch (Exception e) { log.warn("Unable to pre-fetch an image ({}) depended upon by Dockerfile - image build will continue but may fail. Exception message was: {}", imageName, e.getMessage()); } }); }
Example #21
Source File: DockerComposeContainerWithBuildTest.java From testcontainers-java with MIT License | 5 votes |
private String imageNameForRunningContainer(final String containerNameSuffix) { return DockerClientFactory.instance().client().listContainersCmd().exec().stream() .filter(it -> Stream.of(it.getNames()).anyMatch(name -> name.endsWith(containerNameSuffix))) .findFirst() .map(Container::getImage) .orElseThrow(IllegalStateException::new); }
Example #22
Source File: NetworkTest.java From testcontainers-java with MIT License | 5 votes |
@Test public void testBuilder() throws Exception { try ( Network network = Network.builder() .driver("macvlan") .build(); ) { String id = network.getId(); assertEquals( "Flag is set", "macvlan", DockerClientFactory.instance().client().inspectNetworkCmd().withNetworkId(id).exec().getDriver() ); } }
Example #23
Source File: Environments.java From presto with Apache License 2.0 | 5 votes |
public static void pruneEnvironment() { log.info("Shutting down previous containers"); try (DockerClient dockerClient = DockerClientFactory.lazyClient()) { killContainers( dockerClient, listContainersCmd -> listContainersCmd.withLabelFilter(ImmutableMap.of(PRODUCT_TEST_LAUNCHER_STARTED_LABEL_NAME, PRODUCT_TEST_LAUNCHER_STARTED_LABEL_VALUE))); removeNetworks( dockerClient, listNetworksCmd -> listNetworksCmd.withNameFilter(PRODUCT_TEST_LAUNCHER_NETWORK)); } catch (IOException e) { throw new UncheckedIOException(e); } }
Example #24
Source File: ImageFromDockerfileTest.java From testcontainers-java with MIT License | 5 votes |
@Test public void shouldAddDefaultLabels() { ImageFromDockerfile image = new ImageFromDockerfile() .withDockerfileFromBuilder(it -> it.from("scratch")); String imageId = image.resolve(); DockerClient dockerClient = DockerClientFactory.instance().client(); InspectImageResponse inspectImageResponse = dockerClient.inspectImageCmd(imageId).exec(); assertThat(inspectImageResponse.getConfig().getLabels()) .containsAllEntriesOf(DockerClientFactory.DEFAULT_LABELS); }
Example #25
Source File: ImagePullPolicyTest.java From testcontainers-java with MIT License | 5 votes |
@AfterClass public static void afterClass() { try { DockerClientFactory.instance().client().removeImageCmd(imageName).withForce(true).exec(); } catch (NotFoundException ignored) { } }
Example #26
Source File: ImagePullPolicyTest.java From testcontainers-java with MIT License | 5 votes |
@Before public void setUp() { // Clean up local cache try { DockerClientFactory.instance().client().removeImageCmd(imageName).withForce(true).exec(); } catch (NotFoundException ignored) { } LocalImagesCache.INSTANCE.cache.remove(new DockerImageName(imageName)); }
Example #27
Source File: ImagePullPolicyTest.java From testcontainers-java with MIT License | 5 votes |
@Test public void shouldAlwaysPull() { try ( GenericContainer<?> container = new GenericContainer<>(imageName) .withStartupCheckStrategy(new OneShotStartupCheckStrategy()) ) { container.start(); } DockerClientFactory.instance().client().removeImageCmd(imageName).withForce(true).exec(); try ( GenericContainer<?> container = new GenericContainer<>(imageName) .withStartupCheckStrategy(new OneShotStartupCheckStrategy()) ) { expectToFailWithNotFoundException(container); } try ( // built_in_image_pull_policy { GenericContainer<?> container = new GenericContainer<>(imageName) .withImagePullPolicy(PullPolicy.alwaysPull()) // } ) { container.withStartupCheckStrategy(new OneShotStartupCheckStrategy()); container.start(); } }
Example #28
Source File: EventStreamTest.java From testcontainers-java with MIT License | 5 votes |
/** * Test that docker events can be streamed from the client. */ @Test public void test() throws IOException, InterruptedException { CountDownLatch latch = new CountDownLatch(1); try ( GenericContainer container = new GenericContainer<>() .withCommand("true") .withStartupCheckStrategy(new OneShotStartupCheckStrategy()) ) { container.start(); String createdAt = container.getContainerInfo().getCreated(); // Request all events between startTime and endTime for the container try ( EventsResultCallback response = DockerClientFactory.instance().client().eventsCmd() .withContainerFilter(container.getContainerId()) .withEventFilter("create") .withSince(Instant.parse(createdAt).getEpochSecond() + "") .exec(new EventsResultCallback() { @Override public void onNext(@NotNull Event event) { // Check that a create event for the container is received if (event.getId().equals(container.getContainerId()) && event.getStatus().equals("create")) { latch.countDown(); } } }) ) { response.awaitStarted(5, TimeUnit.SECONDS); latch.await(5, TimeUnit.SECONDS); } } }
Example #29
Source File: DockerContainer.java From james-project with Apache License 2.0 | 5 votes |
private static void disableDockerTestsIfDockerUnavailable() { try { DockerClientFactory.instance().client(); } catch (IllegalStateException e) { LOGGER.error("Cannot connect to docker service", e); throw new AssumptionViolatedException("Skipping all docker tests as no Docker environment was found"); } }
Example #30
Source File: SvnTestHelper.java From git-as-svn with GNU General Public License v2.0 | 5 votes |
public static void skipTestIfDockerUnavailable() { try { Assert.assertNotNull(DockerClientFactory.instance().client()); } catch (IllegalStateException e) { throw new SkipException("Docker is not available", e); } }