org.testcontainers.containers.ContainerState Java Examples

The following examples show how to use org.testcontainers.containers.ContainerState. 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: AuthenticatedImagePullTest.java    From testcontainers-java with MIT License 6 votes vote down vote up
@Test
public void testThatAuthLocatorIsUsedForDockerComposePull() throws IOException {
    // Prepare a simple temporary Docker Compose manifest which requires our custom private image
    Path tempFile = getLocalTempFile(".docker-compose.yml");
    @Language("yaml") String composeFileContent =
        "version: '2.0'\n" +
            "services:\n" +
            "  privateservice:\n" +
            "      command: /bin/sh -c 'sleep 60'\n" +
            "      image: " + testImageNameWithTag;
    Files.write(tempFile, composeFileContent.getBytes());

    // Start the docker compose project, which will require an authenticated pull
    try (final DockerComposeContainer<?> compose = new DockerComposeContainer<>(tempFile.toFile())) {
        compose.start();

        assertTrue("container started following an authenticated pull",
            compose
                .getContainerByServiceName("privateservice_1")
                .map(ContainerState::isRunning)
                .orElse(false)
        );
    }
}
 
Example #2
Source File: DockerComposeContainerTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Test
public void shouldRetrieveContainerByServiceName() {
    String existingServiceName = "db_1";
    Optional<ContainerState> result = environment.getContainerByServiceName(existingServiceName);
    assertTrue(format("Container should be found by service name %s", existingServiceName), result.isPresent());
    assertEquals("Mapped port for result container was wrong, probably wrong container found", result.get().getExposedPorts(), singletonList(3306));
}
 
Example #3
Source File: DockerComposePassthroughTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Test
public void testContainerInstanceProperties() {
    final ContainerState container = waitStrategy.getContainer();

    //check environment variable was set
    assertThat("Environment variable set correctly", Arrays.asList(Objects.requireNonNull(container.getContainerInfo()
        .getConfig().getEnv())), hasItem("bar=bar"));

    //check other container properties
    assertNotNull("Container id is not null", container.getContainerId());
    assertNotNull("Port mapped", container.getMappedPort(3000));
    assertThat("Exposed Ports", container.getExposedPorts(), hasItem(3000));

}
 
Example #4
Source File: SkyWalkingAnnotations.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static void initDockerContainers(final Object testClass,
                                         final DockerComposeContainer<?> compose) throws Exception {
    final List<Field> containerFields = Stream.of(testClass.getClass().getDeclaredFields())
                                              .filter(SkyWalkingAnnotations::isAnnotatedWithDockerContainer)
                                              .collect(Collectors.toList());
    if (containerFields.isEmpty()) {
        return;
    }

    final Field serviceMap = DockerComposeContainer.class.getDeclaredField("serviceInstanceMap");
    serviceMap.setAccessible(true);
    final Map<String, ContainerState> serviceInstanceMap = (Map<String, ContainerState>) serviceMap.get(compose);

    for (final Field containerField : containerFields) {
        if (containerField.getType() != ContainerState.class) {
            throw new IllegalArgumentException(
                "@DockerContainer can only be annotated on fields of type " + ContainerState.class.getName()
                    + " but was " + containerField.getType() + "; field \"" + containerField.getName() + "\""
            );
        }
        final DockerContainer dockerContainer = containerField.getAnnotation(DockerContainer.class);
        final String serviceName = dockerContainer.value();
        final Optional<ContainerState> container =
            serviceInstanceMap.entrySet()
                              .stream()
                              .filter(e -> e.getKey().startsWith(serviceName + "_"))
                              .findFirst()
                              .map(Map.Entry::getValue);
        containerField.setAccessible(true);
        containerField.set(
            testClass,
            container.orElseThrow(
                () -> new NoSuchElementException("cannot find container with name " + serviceName)
            )
        );
    }
}
 
Example #5
Source File: DockerComposeContainerTest.java    From testcontainers-java with MIT License 4 votes vote down vote up
@Test
public void shouldReturnEmptyResultOnNoneExistingService() {
    String notExistingServiceName = "db_256";
    Optional<ContainerState> result = environment.getContainerByServiceName(notExistingServiceName);
    assertFalse(format("No container should be found under service name %s", notExistingServiceName), result.isPresent());
}
 
Example #6
Source File: DockerComposePassthroughTest.java    From testcontainers-java with MIT License 4 votes vote down vote up
@SuppressWarnings("unchecked")
public ContainerState getContainer() {
    return this.waitStrategyTarget;
}