Skip to content

Commit

Permalink
rename testcontainer into docker
Browse files Browse the repository at this point in the history
  • Loading branch information
jetoile committed Feb 24, 2019
1 parent 09efcc1 commit 9cf47f7
Show file tree
Hide file tree
Showing 22 changed files with 164 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,14 @@ public class HadoopUnitClientConfig {
public static final String CONFLUENT_KSQL_PORT_KEY = "confluent.ksql.port";


//testcontainer
public static final String TESTCONTAINER_IMAGENAME_KEY = "testcontainer.imagename";
public static final String TESTCONTAINER_EXPOSEDPORTS_KEY = "testcontainer.exposedports";
public static final String TESTCONTAINER_ENVS_KEY = "testcontainer.envs";
public static final String TESTCONTAINER_LABELS_KEY = "testcontainer.labels";
public static final String TESTCONTAINER_COMMAND_KEY = "testcontainer.command";
public static final String TESTCONTAINER_FIXED_EXPOSEDPORTS_KEY = "testcontainer.fixed.exposedports";
public static final String TESTCONTAINER_CLASSPATH_RESOURCES_MAPPING_KEY = "testcontainer.classpath.resources.mapping";
//docker
public static final String DOCKER_IMAGENAME_KEY = "docker.imagename";
public static final String DOCKER_EXPOSEDPORTS_KEY = "docker.exposedports";
public static final String DOCKER_ENVS_KEY = "docker.envs";
public static final String DOCKER_LABELS_KEY = "docker.labels";
public static final String DOCKER_COMMAND_KEY = "docker.command";
public static final String DOCKER_FIXED_EXPOSEDPORTS_KEY = "docker.fixed.exposedports";
public static final String DOCKER_CLASSPATH_RESOURCES_MAPPING_KEY = "docker.classpath.resources.mapping";

private HadoopUnitClientConfig() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>hadoop-unit-testcontainer</artifactId>
<artifactId>hadoop-unit-docker</artifactId>

<properties>
<junit.version>4.12</junit.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,30 @@
import org.testcontainers.containers.BindMode;
import org.testcontainers.containers.FixedHostPortGenericContainer;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.output.OutputFrame;
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.containers.output.ToStringConsumer;

import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;

public class TestContainerBootstrap implements Bootstrap {
static final private Logger LOGGER = LoggerFactory.getLogger(TestContainerBootstrap.class);
public class DockerBootstrap implements Bootstrap {
static final private Logger LOGGER = LoggerFactory.getLogger(DockerBootstrap.class);

private State state = State.STOPPED;
private Configuration configuration;

private FixedHostPortGenericContainer container;

private String imageName;
private List<Integer> exposedPorts;
private Map<String, String> envs;
private Map<String, String> labels;
private List<String> command;
private Map<Integer, Integer> fixedExposedPorts;
private Map<String, String> classpathResourceMappings;
private List<Integer> exposedPorts = new ArrayList<>();
private Map<String, String> envs = new HashMap<>();
private Map<String, String> labels = new HashMap<>();
private List<String> command = new ArrayList<>();
private Map<Integer, Integer> fixedExposedPorts = new HashMap<>();
private Map<String, String> classpathResourceMappings = new HashMap<>();


public TestContainerBootstrap() {
public DockerBootstrap() {
try {
configuration = HadoopUtils.INSTANCE.loadConfigFile(null);
loadConfig();
Expand All @@ -60,7 +56,7 @@ public TestContainerBootstrap() {
}
}

public TestContainerBootstrap(URL url) {
public DockerBootstrap(URL url) {
try {
configuration = HadoopUtils.INSTANCE.loadConfigFile(url);
loadConfig();
Expand All @@ -71,7 +67,7 @@ public TestContainerBootstrap(URL url) {

@Override
public ComponentMetadata getMetadata() {
return new TestContainerMetadata();
return new DockerMetadata();
}

@Override
Expand All @@ -86,66 +82,66 @@ public String getProperties() {
}

private void loadConfig() {
imageName = configuration.getString(TestContainerConfig.TESTCONTAINER_IMAGENAME_KEY);
imageName = configuration.getString(DockerConfig.DOCKER_IMAGENAME_KEY);

String[] portsAsString = configuration.getStringArray(TestContainerConfig.TESTCONTAINER_EXPOSEDPORTS_KEY);
String[] portsAsString = configuration.getStringArray(DockerConfig.DOCKER_EXPOSEDPORTS_KEY);
exposedPorts = Arrays.asList(portsAsString).stream()
.map(Integer::valueOf)
.collect(Collectors.toList());

String[] commandAsString = configuration.getStringArray(TestContainerConfig.TESTCONTAINER_COMMAND_KEY);
String[] commandAsString = configuration.getStringArray(DockerConfig.DOCKER_COMMAND_KEY);
command = Arrays.asList(commandAsString).stream()
.collect(Collectors.toList());

String[] envsAsString = configuration.getStringArray(TestContainerConfig.TESTCONTAINER_ENVS_KEY);
String[] envsAsString = configuration.getStringArray(DockerConfig.DOCKER_ENVS_KEY);
envs = Arrays.asList(envsAsString).stream().collect(Collectors.toMap(c -> c.split(":")[0], c -> c.split(":")[1]));

String[] labelsAsString = configuration.getStringArray(TestContainerConfig.TESTCONTAINER_LABELS_KEY);
String[] labelsAsString = configuration.getStringArray(DockerConfig.DOCKER_LABELS_KEY);
labels = Arrays.asList(labelsAsString).stream().collect(Collectors.toMap(c -> c.split(":")[0], c -> c.split(":")[1]));

String[] fixedExposedPortsString = configuration.getStringArray(TestContainerConfig.TESTCONTAINER_FIXED_EXPOSEDPORTS_KEY);
String[] fixedExposedPortsString = configuration.getStringArray(DockerConfig.DOCKER_FIXED_EXPOSEDPORTS_KEY);
fixedExposedPorts = Arrays.asList(fixedExposedPortsString).stream().collect(Collectors.toMap(c -> Integer.valueOf(c.split(":")[0]), c -> Integer.valueOf(c.split(":")[1])));

String[] classpathResourceMappingsList = configuration.getStringArray(TestContainerConfig.TESTCONTAINER_CLASSPATH_RESOURCES_MAPPING_KEY);
String[] classpathResourceMappingsList = configuration.getStringArray(DockerConfig.DOCKER_CLASSPATH_RESOURCES_MAPPING_KEY);
classpathResourceMappings = Arrays.asList(classpathResourceMappingsList).stream().collect(Collectors.toMap(c -> c.split(":")[0], c -> c.substring(c.indexOf(":") + 1)));
}


@Override
public void loadConfig(Map<String, String> configs) {
if (StringUtils.isNotEmpty(configs.get(TestContainerConfig.TESTCONTAINER_IMAGENAME_KEY))) {
imageName = configs.get(TestContainerConfig.TESTCONTAINER_IMAGENAME_KEY);
if (StringUtils.isNotEmpty(configs.get(DockerConfig.DOCKER_IMAGENAME_KEY))) {
imageName = configs.get(DockerConfig.DOCKER_IMAGENAME_KEY);
}
if (StringUtils.isNotEmpty(configs.get(TestContainerConfig.TESTCONTAINER_EXPOSEDPORTS_KEY))) {
String ports = configs.get(TestContainerConfig.TESTCONTAINER_EXPOSEDPORTS_KEY);
if (StringUtils.isNotEmpty(configs.get(DockerConfig.DOCKER_EXPOSEDPORTS_KEY))) {
String ports = configs.get(DockerConfig.DOCKER_EXPOSEDPORTS_KEY);
String[] portsAsString = ports.split(",");
exposedPorts = Arrays.asList(portsAsString).stream()
.map(Integer::valueOf)
.collect(Collectors.toList());
}
if (StringUtils.isNotEmpty(configs.get(TestContainerConfig.TESTCONTAINER_COMMAND_KEY))) {
String commandList = configs.get(TestContainerConfig.TESTCONTAINER_COMMAND_KEY);
if (StringUtils.isNotEmpty(configs.get(DockerConfig.DOCKER_COMMAND_KEY))) {
String commandList = configs.get(DockerConfig.DOCKER_COMMAND_KEY);
String[] commandAsString = commandList.split(",");
command = Arrays.asList(commandAsString).stream()
.collect(Collectors.toList());
}
if (StringUtils.isNotEmpty(configs.get(TestContainerConfig.TESTCONTAINER_ENVS_KEY))) {
String envsList = configs.get(TestContainerConfig.TESTCONTAINER_ENVS_KEY);
if (StringUtils.isNotEmpty(configs.get(DockerConfig.DOCKER_ENVS_KEY))) {
String envsList = configs.get(DockerConfig.DOCKER_ENVS_KEY);
String[] envsAsString = envsList.split(",");
envs = Arrays.asList(envsAsString).stream().collect(Collectors.toMap(c -> c.split(":")[0], c -> c.split(":")[1]));
}
if (StringUtils.isNotEmpty(configs.get(TestContainerConfig.TESTCONTAINER_LABELS_KEY))) {
String labelsList = configs.get(TestContainerConfig.TESTCONTAINER_LABELS_KEY);
if (StringUtils.isNotEmpty(configs.get(DockerConfig.DOCKER_LABELS_KEY))) {
String labelsList = configs.get(DockerConfig.DOCKER_LABELS_KEY);
String[] labelsAsString = labelsList.split(",");
labels = Arrays.asList(labelsAsString).stream().collect(Collectors.toMap(c -> c.split(":")[0], c -> c.split(":")[1]));
}
if (StringUtils.isNotEmpty(configs.get(TestContainerConfig.TESTCONTAINER_FIXED_EXPOSEDPORTS_KEY))) {
String fixedExposedPortsList = configs.get(TestContainerConfig.TESTCONTAINER_FIXED_EXPOSEDPORTS_KEY);
if (StringUtils.isNotEmpty(configs.get(DockerConfig.DOCKER_FIXED_EXPOSEDPORTS_KEY))) {
String fixedExposedPortsList = configs.get(DockerConfig.DOCKER_FIXED_EXPOSEDPORTS_KEY);
String[] fixedExposedPortsAsString = fixedExposedPortsList.split(",");
fixedExposedPorts = Arrays.asList(fixedExposedPortsAsString).stream().collect(Collectors.toMap(c -> Integer.valueOf(c.split(":")[0]), c -> Integer.valueOf(c.split(":")[1])));
}
if (StringUtils.isNotEmpty(configs.get(TestContainerConfig.TESTCONTAINER_CLASSPATH_RESOURCES_MAPPING_KEY))) {
String classpathResourceMappingsList = configs.get(TestContainerConfig.TESTCONTAINER_CLASSPATH_RESOURCES_MAPPING_KEY);
if (StringUtils.isNotEmpty(configs.get(DockerConfig.DOCKER_CLASSPATH_RESOURCES_MAPPING_KEY))) {
String classpathResourceMappingsList = configs.get(DockerConfig.DOCKER_CLASSPATH_RESOURCES_MAPPING_KEY);
String[] classpathResourceMappingsAsString = classpathResourceMappingsList.split(",");
classpathResourceMappings = Arrays.asList(classpathResourceMappingsAsString).stream().collect(Collectors.toMap(c -> c.split(":")[0], c -> c.substring(c.indexOf(":") + 1)));
}
Expand Down Expand Up @@ -191,7 +187,7 @@ public Bootstrap start() {
container.start();
container.followOutput(new Slf4jLogConsumer(LOGGER));
} catch (Throwable e) {
LOGGER.error("unable to add testcontainer", e);
LOGGER.error("unable to add docker", e);
}
state = State.STARTED;
LOGGER.info("{} is started", this.getClass().getName());
Expand All @@ -209,7 +205,7 @@ public Bootstrap stop() {
try {
container.stop();
} catch (Exception e) {
LOGGER.error("unable to stop testcontainer", e);
LOGGER.error("unable to stop docker", e);
}
state = State.STOPPED;
LOGGER.info("{} is stopped", this.getClass().getName());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package fr.jetoile.hadoopunit.component;

public class DockerConfig {

public static final String DOCKER_IMAGENAME_KEY = "docker.imagename";
public static final String DOCKER_EXPOSEDPORTS_KEY = "docker.exposedports";
public static final String DOCKER_ENVS_KEY = "docker.envs";
public static final String DOCKER_LABELS_KEY = "docker.labels";
public static final String DOCKER_COMMAND_KEY = "docker.command";
public static final String DOCKER_FIXED_EXPOSEDPORTS_KEY = "docker.fixed.exposedports";
public static final String DOCKER_CLASSPATH_RESOURCES_MAPPING_KEY = "docker.classpath.resources.mapping";


private DockerConfig() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package fr.jetoile.hadoopunit.component;

import fr.jetoile.hadoopunit.ComponentMetadata;

import java.util.Collections;
import java.util.List;

public class DockerMetadata extends ComponentMetadata {
@Override
public String getName() {
return "DOCKER";
}

@Override
public List<String> getDependencies() {
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fr.jetoile.hadoopunit.component.DockerBootstrap
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
import static org.assertj.core.api.Assertions.assertThat;


public class TestContainerBootstrapTest {
public class DockerBootstrapTest {


private static final Logger LOGGER = LoggerFactory.getLogger(TestContainerBootstrapTest.class);
private static final Logger LOGGER = LoggerFactory.getLogger(DockerBootstrapTest.class);

static private Configuration configuration;

Expand All @@ -62,7 +62,7 @@ public static void tearDown() {

@Test
public void testContainerShouldStart() throws NotFoundServiceException, IOException {
TestContainerBootstrap testcontainer = (TestContainerBootstrap) HadoopBootstrap.INSTANCE.getService("TESTCONTAINER");
DockerBootstrap testcontainer = (DockerBootstrap) HadoopBootstrap.INSTANCE.getService("DOCKER");

String containerIpAddress = testcontainer.getContainer().getContainerIpAddress();
Integer firstMappedPort = testcontainer.getContainer().getFirstMappedPort();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#docker.imagename=couchbase:4.5.0
#docker.exposedports=8091,8092,8093,8094,11207,11210,11211,18091,18092,18093
docker.imagename=alpine:3.2
docker.exposedports=80
docker.envs=MAGIC_NUMBER:42
docker.labels=MAGIC_NUMBER:42
docker.command=/bin/sh, -c, while true; do echo \"$MAGIC_NUMBER\" | nc -l -p 80; done
docker.fixed.exposedports=21300:80
docker.classpath.resources.mapping=hadoop-unit-default.properties:/hadoop-unit-default.properties:READ_ONLY
19 changes: 19 additions & 0 deletions hadoop-unit-standalone/src/main/resources/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: '2'
services:
spark-master:
image: bde2020/spark-master:2.4.0-hadoop2.7
container_name: spark-master
ports:
- "8080:8080"
- "7077:7077"
environment:
- INIT_DAEMON_STEP=setup_spark
spark-worker-1:
image: bde2020/spark-worker:2.4.0-hadoop2.7
container_name: spark-worker-1
depends_on:
- spark-master
ports:
- "8081:8081"
environment:
- "SPARK_MASTER=spark://spark-master:7077"
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ confluent_kafka_rest.artifactId=fr.jetoile.hadoop:hadoop-unit-confluent-rest:3.2
confluent_kafka_rest.mainClass=fr.jetoile.hadoopunit.component.ConfluentKafkaRestBootstrap
confluent_kafka_rest.metadataClass=fr.jetoile.hadoopunit.component.ConfluentKafkaRestMetadata

testcontainer.artifactId=fr.jetoile.hadoop:hadoop-unit-testcontainer:3.2-SNAPSHOT
testcontainer.mainClass=fr.jetoile.hadoopunit.component.TestContainerBootstrap
testcontainer.metadataClass=fr.jetoile.hadoopunit.component.TestContainerMetadata
docker.artifactId=fr.jetoile.hadoop:hadoop-unit-docker:3.2-SNAPSHOT
docker.mainClass=fr.jetoile.hadoopunit.component.DockerBootstrap
docker.metadataClass=fr.jetoile.hadoopunit.component.DockerMetadata

maven.central.repo=https://repo.maven.apache.org/maven2/
maven.local.repo=~/.m2/repository
Expand Down Expand Up @@ -306,10 +306,16 @@ confluent.ksql.port=8083



# Test Container
testcontainer.imagename=alpine:3.2
testcontainer.exposedports=80
testcontainer.envs=MAGIC_NUMBER:42
testcontainer.labels=MAGIC_NUMBER:42
testcontainer.command=/bin/sh, -c, while true; do echo \"$MAGIC_NUMBER\" | nc -l -p 80; done
testcontainer.fixed.exposedports=21300:80
# Docker
docker.imagename=alpine:3.2
docker.exposedports=80
docker.envs=MAGIC_NUMBER:42
docker.labels=MAGIC_NUMBER:42
docker.command=/bin/sh, -c, while true; do echo \"$MAGIC_NUMBER\" | nc -l -p 80; done
docker.fixed.exposedports=21300:80
#docker.classpath.resources.mapping=hadoop-unit-default.properties:/hadoop-unit-default.properties:READ_ONLY

# Docker compose
dockercompose.filename=conf/docker-compose.yml
#dockercompose.exposedports=zoo:2181,resourcemanager:8088
dockercompose.local=false
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ hiveserver2=true
#confluent_schemaregistry=true
#confluent_kafka=true
#confluent_ksql_rest=true
testcontainer=true
docker=true

This file was deleted.

Loading

0 comments on commit 9cf47f7

Please sign in to comment.