use of org.eclipse.che.plugin.docker.client.json.ContainerConfig in project che by eclipse.
the class DockerProcessTest method setUp.
@BeforeMethod
public void setUp() throws Exception {
dockerConnectorConfiguration = new DockerConnectorConfiguration(new InitialAuthConfig(), new DefaultNetworkFinder());
docker = new DockerConnector(dockerConnectorConfiguration, new DockerConnectionFactory(dockerConnectorConfiguration), new DockerRegistryAuthResolver(null, null), new DockerApiVersionPathPrefixProvider("1.18"));
final ContainerCreated containerCreated = docker.createContainer(CreateContainerParams.create(new ContainerConfig().withImage("ubuntu").withCmd("tail", "-f", "/dev/null")));
container = containerCreated.getId();
docker.startContainer(StartContainerParams.create(containerCreated.getId()));
when(dockerConnectorProvider.get()).thenReturn(docker);
}
use of org.eclipse.che.plugin.docker.client.json.ContainerConfig in project che by eclipse.
the class OpenShiftConnectorTest method shouldGetWorkspaceIDWhenAValidOneIsProvidedInCreateContainerParams.
@Test
public void shouldGetWorkspaceIDWhenAValidOneIsProvidedInCreateContainerParams() throws IOException {
//Given
String expectedWorkspaceID = "abcd1234";
ContainerConfig containerConfig = mock(ContainerConfig.class);
CreateContainerParams createContainerParams = CreateContainerParams.create(containerConfig);
when(containerConfig.getEnv()).thenReturn(CONTAINER_ENV_VARIABLES);
//When
openShiftConnector = new OpenShiftConnector(dockerConnectorConfiguration, dockerConnectionFactory, authManager, dockerApiVersionPathPrefixProvider, CHE_DEFAULT_OPENSHIFT_PROJECT_NAME, CHE_DEFAULT_OPENSHIFT_SERVICEACCOUNT, OPENSHIFT_LIVENESS_PROBE_DELAY, OPENSHIFT_LIVENESS_PROBE_TIMEOUT);
String workspaceID = openShiftConnector.getCheWorkspaceId(createContainerParams);
//Then
assertEquals(workspaceID, expectedWorkspaceID);
}
use of org.eclipse.che.plugin.docker.client.json.ContainerConfig in project che by eclipse.
the class DockerConnectorTest method shouldBeAbleToCreateContainer.
@Test
public void shouldBeAbleToCreateContainer() throws IOException, JsonParseException {
CreateContainerParams createContainerParams = CreateContainerParams.create(new ContainerConfig());
ContainerCreated containerCreated = mock(ContainerCreated.class);
when(dockerResponse.getStatus()).thenReturn(RESPONSE_CREATED_CODE);
doReturn(containerCreated).when(dockerConnector).parseResponseStreamAndClose(inputStream, ContainerCreated.class);
ContainerCreated returnedContainerCreated = dockerConnector.createContainer(createContainerParams);
verify(dockerConnectionFactory).openConnection(any(URI.class));
verify(dockerConnection).method(REQUEST_METHOD_POST);
verify(dockerConnection).path("/containers/create");
verify(dockerConnection).header("Content-Type", MediaType.APPLICATION_JSON);
verify(dockerConnection).header(eq("Content-Length"), anyInt());
verify(dockerConnection).entity(any(byte[].class));
verify(dockerConnection).request();
verify(dockerResponse).getStatus();
verify(dockerResponse).getInputStream();
assertEquals(returnedContainerCreated, containerCreated);
}
use of org.eclipse.che.plugin.docker.client.json.ContainerConfig in project che by eclipse.
the class OpenShiftConnector method createContainerInfo.
/**
* Collects the relevant information from a Service, an ImageInfo, and a Pod into
* a docker ContainerInfo JSON object. The returned object is what would be returned
* by executing {@code docker inspect <container>}, with fields filled as available.
* @param svc
* @param imageInfo
* @param pod
* @param containerId
* @return
*/
private ContainerInfo createContainerInfo(Service svc, ImageInfo imageInfo, Pod pod, String containerId) {
// In Che on OpenShift, we only have one container per pod.
Container container = pod.getSpec().getContainers().get(0);
ContainerConfig imageContainerConfig = imageInfo.getContainerConfig();
// HostConfig
HostConfig hostConfig = new HostConfig();
hostConfig.setBinds(new String[0]);
hostConfig.setMemory(imageInfo.getConfig().getMemory());
// Env vars
List<String> imageEnv = Arrays.asList(imageContainerConfig.getEnv());
List<String> containerEnv = container.getEnv().stream().map(e -> String.format("%s=%s", e.getName(), e.getValue())).collect(Collectors.toList());
String[] env = Stream.concat(imageEnv.stream(), containerEnv.stream()).toArray(String[]::new);
// Exposed Ports
Map<String, List<PortBinding>> ports = getCheServicePorts(svc);
Map<String, Map<String, String>> exposedPorts = new HashMap<>();
for (String key : ports.keySet()) {
exposedPorts.put(key, Collections.emptyMap());
}
// Labels
Map<String, String> annotations = KubernetesLabelConverter.namesToLabels(svc.getMetadata().getAnnotations());
Map<String, String> containerLabels = imageInfo.getConfig().getLabels();
Map<String, String> labels = Stream.concat(annotations.entrySet().stream(), containerLabels.entrySet().stream()).filter(e -> e.getKey().startsWith(KubernetesLabelConverter.getCheServerLabelPrefix())).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
// ContainerConfig
ContainerConfig config = imageContainerConfig;
config.setHostname(svc.getMetadata().getName());
config.setEnv(env);
config.setExposedPorts(exposedPorts);
config.setLabels(labels);
config.setImage(container.getImage());
// NetworkSettings
NetworkSettings networkSettings = new NetworkSettings();
networkSettings.setIpAddress(svc.getSpec().getClusterIP());
networkSettings.setGateway(svc.getSpec().getClusterIP());
networkSettings.setPorts(ports);
// Make final ContainerInfo
ContainerInfo info = new ContainerInfo();
info.setId(containerId);
info.setConfig(config);
info.setNetworkSettings(networkSettings);
info.setHostConfig(hostConfig);
info.setImage(imageInfo.getConfig().getImage());
return info;
}
use of org.eclipse.che.plugin.docker.client.json.ContainerConfig in project che by eclipse.
the class MachineProviderImplTest method shouldAddLinksToContainerOnCreation.
@Test
public void shouldAddLinksToContainerOnCreation() throws Exception {
// given
String[] links = new String[] { "container1", "container2:alias" };
CheServiceImpl service = createService();
service.setLinks(asList(links));
// when
createInstanceFromRecipe(service, true);
// then
ArgumentCaptor<CreateContainerParams> argumentCaptor = ArgumentCaptor.forClass(CreateContainerParams.class);
verify(dockerConnector).createContainer(argumentCaptor.capture());
ContainerConfig containerConfig = argumentCaptor.getValue().getContainerConfig();
assertEquals(containerConfig.getHostConfig().getLinks(), links);
assertEquals(containerConfig.getNetworkingConfig().getEndpointsConfig().get(NETWORK_NAME).getLinks(), links);
}
Aggregations