Search in sources :

Example 6 with ContainerCreateConfig

use of io.fabric8.maven.docker.access.ContainerCreateConfig in project docker-maven-plugin by fabric8io.

the class RunServiceTest method retryIfInsufficientPortBindingInformation.

@Test
public void retryIfInsufficientPortBindingInformation(@Mocked Container container, @Mocked ImageConfiguration imageConfiguration, @Mocked PortMapping portMapping) throws DockerAccessException {
    new Expectations() {

        {
            docker.createContainer(withAny(new ContainerCreateConfig("img")), anyString);
            result = "containerId";
            portMapping.needsPropertiesUpdate();
            result = true;
            queryService.getMandatoryContainer("containerId");
            result = container;
            times = 2;
            container.isRunning();
            result = true;
            container.getPortBindings();
            result = new PortBindingException("5432/tcp", new Gson().fromJson("{\"5432/tcp\": []}", JsonObject.class));
            returns(ImmutableMap.of("5432/tcp", new Container.PortBinding(56741, "0.0.0.0")), ImmutableMap.of("5432/tcp", new Container.PortBinding(56741, "0.0.0.0")));
        }
    };
    String containerId = runService.createAndStartContainer(imageConfiguration, portMapping, new GavLabel("Im:A:Test"), properties, getBaseDirectory(), "blah", new Date());
    new Verifications() {

        {
            assertEquals("containerId", containerId);
        }
    };
}
Also used : Expectations(mockit.Expectations) GavLabel(io.fabric8.maven.docker.util.GavLabel) ContainerCreateConfig(io.fabric8.maven.docker.access.ContainerCreateConfig) Gson(com.google.gson.Gson) PortBindingException(io.fabric8.maven.docker.model.PortBindingException) Verifications(mockit.Verifications) Date(java.util.Date) Test(org.junit.Test)

Example 7 with ContainerCreateConfig

use of io.fabric8.maven.docker.access.ContainerCreateConfig in project docker-maven-plugin by fabric8io.

the class DockerAccessWithHcClient method createContainer.

@Override
public String createContainer(ContainerCreateConfig containerConfig, String containerName) throws DockerAccessException {
    String createJson = containerConfig.toJson();
    log.debug("Container create config: %s", createJson);
    try {
        String url = urlBuilder.createContainer(containerName);
        log.verbose(Logger.LogVerboseCategory.API, API_LOG_FORMAT_POST_WITH_REQUEST, url, createJson);
        String response = delegate.post(url, createJson, new ApacheHttpClientDelegate.BodyResponseHandler(), HTTP_CREATED);
        JsonObject json = JsonFactory.newJsonObject(response);
        logWarnings(json);
        // only need first 12 to id a container
        return json.get("Id").getAsString().substring(0, 12);
    } catch (IOException e) {
        throw new DockerAccessException(e, "Unable to create container for [%s]", containerConfig.getImageName());
    }
}
Also used : DockerAccessException(io.fabric8.maven.docker.access.DockerAccessException) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException)

Example 8 with ContainerCreateConfig

use of io.fabric8.maven.docker.access.ContainerCreateConfig in project docker-maven-plugin by fabric8io.

the class StartContainerExecutorTest method testStartContainers.

@Test
public void testStartContainers(@Mocked ServiceHub hub, @Mocked DockerAccess dockerAccess, @Mocked ContainerTracker containerTracker, @Mocked Logger log) throws IOException, ExecException {
    // Given
    new Expectations() {

        {
            dockerAccess.createContainer((ContainerCreateConfig) any, anyString);
            result = "container-name";
            dockerAccess.getContainer(anyString);
            result = new ContainerDetails(JsonFactory.newJsonObject("{\"NetworkSettings\":{\"IPAddress\":\"192.168.1.2\"}}"));
            QueryService queryService = new QueryService(dockerAccess);
            hub.getQueryService();
            result = queryService;
            hub.getRunService();
            result = new RunService(dockerAccess, queryService, containerTracker, new LogOutputSpecFactory(true, true, null), log);
        }
    };
    Properties projectProps = new Properties();
    StartContainerExecutor startContainerExecutor = new StartContainerExecutor.Builder().serviceHub(hub).projectProperties(projectProps).portMapping(new PortMapping(Collections.emptyList(), projectProps)).gavLabel(new GavLabel("io.fabric8:test:0.1.0")).basedir(new File("/tmp/foo")).containerNamePattern("test-").buildTimestamp(new Date()).exposeContainerProps("docker.container").imageConfig(new ImageConfiguration.Builder().name("name").alias("alias").runConfig(new RunImageConfiguration.Builder().build()).build()).build();
    // When
    String containerId = startContainerExecutor.startContainer();
    // Then
    assertEquals("container-name", containerId);
    assertEquals("container-name", projectProps.getProperty("docker.container.alias.id"));
    assertEquals("192.168.1.2", projectProps.getProperty("docker.container.alias.ip"));
}
Also used : Expectations(mockit.Expectations) RunService(io.fabric8.maven.docker.service.RunService) LogOutputSpecFactory(io.fabric8.maven.docker.log.LogOutputSpecFactory) Properties(java.util.Properties) Date(java.util.Date) GavLabel(io.fabric8.maven.docker.util.GavLabel) QueryService(io.fabric8.maven.docker.service.QueryService) PortMapping(io.fabric8.maven.docker.access.PortMapping) File(java.io.File) RunImageConfiguration(io.fabric8.maven.docker.config.RunImageConfiguration) ContainerDetails(io.fabric8.maven.docker.model.ContainerDetails) Test(org.junit.Test)

Aggregations

ContainerCreateConfig (io.fabric8.maven.docker.access.ContainerCreateConfig)6 PortMapping (io.fabric8.maven.docker.access.PortMapping)3 GavLabel (io.fabric8.maven.docker.util.GavLabel)3 Date (java.util.Date)3 Properties (java.util.Properties)3 Expectations (mockit.Expectations)3 Test (org.junit.Test)3 Gson (com.google.gson.Gson)2 ContainerHostConfig (io.fabric8.maven.docker.access.ContainerHostConfig)2 Arguments (io.fabric8.maven.docker.config.Arguments)2 RunImageConfiguration (io.fabric8.maven.docker.config.RunImageConfiguration)2 PortBindingException (io.fabric8.maven.docker.model.PortBindingException)2 JsonObject (com.google.gson.JsonObject)1 ContainerNetworkingConfig (io.fabric8.maven.docker.access.ContainerNetworkingConfig)1 DockerAccessException (io.fabric8.maven.docker.access.DockerAccessException)1 NetworkConfig (io.fabric8.maven.docker.config.NetworkConfig)1 RunVolumeConfiguration (io.fabric8.maven.docker.config.RunVolumeConfiguration)1 LogOutputSpecFactory (io.fabric8.maven.docker.log.LogOutputSpecFactory)1 Container (io.fabric8.maven.docker.model.Container)1 ContainerDetails (io.fabric8.maven.docker.model.ContainerDetails)1