use of org.eclipse.che.api.environment.server.model.CheServiceImpl in project che by eclipse.
the class CheEnvironmentEngineTest method shouldReplaceServiceNameWithContainerNameAndUseAliasInLinks.
@Test
public void shouldReplaceServiceNameWithContainerNameAndUseAliasInLinks() {
//given
final String serviceNameToLink = "service";
final String containerNameToLink = "container";
final String AliasToServiceToLink = "alias";
List<String> links = new ArrayList<>();
links.add(serviceNameToLink + ':' + AliasToServiceToLink);
CheServiceImpl serviceToNormalizeLinks = new CheServiceImpl().withLinks(links);
CheServiceImpl service1 = mock(CheServiceImpl.class);
CheServiceImpl service2 = mock(CheServiceImpl.class);
Map<String, CheServiceImpl> allServices = new HashMap<>();
allServices.put("this", serviceToNormalizeLinks);
allServices.put(serviceNameToLink, service1);
allServices.put("anotherService", service2);
when(service1.getContainerName()).thenReturn(containerNameToLink);
// when
engine.normalizeLinks(serviceToNormalizeLinks, allServices);
// then
assertEquals(serviceToNormalizeLinks.getLinks().size(), 1);
assertEquals(serviceToNormalizeLinks.getLinks().get(0), containerNameToLink + ':' + AliasToServiceToLink);
}
use of org.eclipse.che.api.environment.server.model.CheServiceImpl in project che by eclipse.
the class CheEnvironmentEngineTest method shouldBeAbleToStartEnvironmentWithRecover.
@Test
public void shouldBeAbleToStartEnvironmentWithRecover() throws Exception {
// given
SnapshotImpl snapshot = mock(SnapshotImpl.class);
MachineSourceImpl machineSource = new MachineSourceImpl("image", "registry.com/snapshot123:latest@sha256:abc1234567890", null);
when(snapshotDao.getSnapshot(anyString(), anyString(), anyString())).thenReturn(snapshot);
when(snapshot.getMachineSource()).thenReturn(machineSource);
// given
EnvironmentImpl env = createEnv();
String envName = "env-1";
String workspaceId = "wsId";
List<Instance> expectedMachines = new ArrayList<>();
when(machineProvider.startService(anyString(), eq(workspaceId), eq(envName), anyString(), anyBoolean(), anyString(), any(CheServiceImpl.class), any(LineConsumer.class))).thenAnswer(invocationOnMock -> {
Object[] arguments = invocationOnMock.getArguments();
String machineName = (String) arguments[3];
boolean isDev = (boolean) arguments[4];
CheServiceImpl service = (CheServiceImpl) arguments[6];
Machine machine = createMachine(workspaceId, envName, service, machineName, isDev);
NoOpMachineInstance instance = spy(new NoOpMachineInstance(machine));
expectedMachines.add(instance);
return instance;
});
when(environmentParser.parse(env)).thenReturn(createCheServicesEnv());
// when
List<Instance> machines = engine.start(workspaceId, envName, env, true, messageConsumer);
// then
assertEquals(machines, expectedMachines);
ArgumentCaptor<CheServiceImpl> captor = ArgumentCaptor.forClass(CheServiceImpl.class);
verify(machineProvider).startService(anyString(), anyString(), anyString(), anyString(), eq(false), anyString(), captor.capture(), any(LineConsumer.class));
CheServiceImpl actualService = captor.getValue();
assertEquals(actualService.getImage(), "registry.com/snapshot123:latest");
}
use of org.eclipse.che.api.environment.server.model.CheServiceImpl in project che by eclipse.
the class CheEnvironmentValidatorTest method invalidServicesEnvironmentProvider.
@DataProvider
public static Object[][] invalidServicesEnvironmentProvider() {
// Format of result array:
// [ [InvalidCheServicesEnvironmentObject, ExceptionMessage], ... ]
CheServicesEnvironmentImpl env;
Map.Entry<String, CheServiceImpl> serviceEntry;
CheServiceImpl service;
List<List<Object>> data = new ArrayList<>();
env = createServicesEnv();
env.setServices(null);
data.add(asList(env, "Environment 'env' should contain at least 1 machine"));
env = createServicesEnv();
env.setServices(emptyMap());
data.add(asList(env, "Environment 'env' should contain at least 1 machine"));
env = createServicesEnv();
serviceEntry = getAnyService(env);
env.getServices().put("invalid service name", serviceEntry.getValue());
data.add(asList(env, "Name of machine 'invalid service name' in environment 'env' is invalid"));
env = createServicesEnv();
serviceEntry = getAnyService(env);
service = serviceEntry.getValue();
service.setImage(null);
service.setBuild(null);
data.add(asList(env, format("Field 'image' or 'build.context' is required in machine '%s' in environment 'env'", serviceEntry.getKey())));
env = createServicesEnv();
serviceEntry = getAnyService(env);
service = serviceEntry.getValue();
service.setImage("");
service.setBuild(null);
data.add(asList(env, format("Field 'image' or 'build.context' is required in machine '%s' in environment 'env'", serviceEntry.getKey())));
env = createServicesEnv();
serviceEntry = getAnyService(env);
service = serviceEntry.getValue();
service.setImage(null);
service.setBuild(new CheServiceBuildContextImpl());
data.add(asList(env, format("Field 'image' or 'build.context' is required in machine '%s' in environment 'env'", serviceEntry.getKey())));
env = createServicesEnv();
serviceEntry = getAnyService(env);
service = serviceEntry.getValue();
service.setImage("");
service.setBuild(new CheServiceBuildContextImpl());
data.add(asList(env, format("Field 'image' or 'build.context' is required in machine '%s' in environment 'env'", serviceEntry.getKey())));
env = createServicesEnv();
serviceEntry = getAnyService(env);
service = serviceEntry.getValue();
service.setPorts(new ArrayList<>(singletonList("8080:8080")));
data.add(asList(env, format("Ports binding is forbidden but found in machine '%s' of environment 'env'", serviceEntry.getKey())));
env = createServicesEnv();
serviceEntry = getAnyService(env);
service = serviceEntry.getValue();
service.setVolumes(new ArrayList<>(singletonList("volume")));
data.add(asList(env, format("Volumes binding is forbidden but found in machine '%s' of environment 'env'", serviceEntry.getKey())));
env = createServicesEnv();
serviceEntry = getAnyService(env);
service = serviceEntry.getValue();
service.setNetworks(new ArrayList<>(singletonList("network1")));
data.add(asList(env, format("Networks configuration is forbidden but found in machine '%s' of environment 'env'", serviceEntry.getKey())));
env = createServicesEnv();
serviceEntry = getAnyService(env);
service = serviceEntry.getValue();
service.setImage(null);
service.setBuild(new CheServiceBuildContextImpl(null, "dockerfile", null, null));
data.add(asList(env, format("Field 'image' or 'build.context' is required in machine '%s' in environment 'env'", serviceEntry.getKey())));
env = createServicesEnv();
serviceEntry = getAnyService(env);
service = serviceEntry.getValue();
service.setImage("");
service.setBuild(new CheServiceBuildContextImpl("", "dockerfile", null, null));
data.add(asList(env, format("Field 'image' or 'build.context' is required in machine '%s' in environment 'env'", serviceEntry.getKey())));
env = createServicesEnv();
serviceEntry = getAnyService(env);
service = serviceEntry.getValue();
service.setImage("");
service.setBuild(new CheServiceBuildContextImpl(null, null, null, null));
data.add(asList(env, format("Field 'image' or 'build.context' is required in machine '%s' in environment 'env'", serviceEntry.getKey())));
env = createServicesEnv();
serviceEntry = getAnyService(env);
service = serviceEntry.getValue();
service.setImage("");
service.setBuild(new CheServiceBuildContextImpl("some url", null, "some content", new HashMap<String, String>() {
{
put("argkey", "argvalue");
}
}));
data.add(asList(env, format("Machine '%s' in environment 'env' contains mutually exclusive dockerfile content and build context.", serviceEntry.getKey())));
return data.stream().map(list -> list.toArray(new Object[list.size()])).toArray(value -> new Object[data.size()][]);
}
use of org.eclipse.che.api.environment.server.model.CheServiceImpl in project che by eclipse.
the class EnvironmentParserTest method createCheServicesEnv.
private static CheServicesEnvironmentImpl createCheServicesEnv(Map<String, String> labels, List<String> expose) {
CheServiceImpl cheService = new CheServiceImpl().withLabels(labels).withExpose(expose).withImage(DEFAULT_DOCKER_IMAGE);
Map<String, CheServiceImpl> cheComposeEnvs = new HashMap<>();
cheComposeEnvs.put(DEFAULT_MACHINE_NAME, cheService);
return new CheServicesEnvironmentImpl().withServices(cheComposeEnvs);
}
use of org.eclipse.che.api.environment.server.model.CheServiceImpl in project che by eclipse.
the class EnvironmentParserTest method shouldAddPortsAndLabelsFromExtendedMachineServers.
@Test(dataProvider = "environmentWithServersProvider")
public void shouldAddPortsAndLabelsFromExtendedMachineServers(EnvironmentImpl environment, CheServicesEnvironmentImpl expectedEnv, CheServicesEnvironmentImpl parsedCheEnv) throws Exception {
when(envParser.parse(any(Environment.class))).thenReturn(parsedCheEnv);
// when
CheServicesEnvironmentImpl cheServicesEnvironment = parser.parse(environment);
// then
// prevent failures because of reordered entries of expose field
assertEquals(cheServicesEnvironment.getServices().size(), expectedEnv.getServices().size());
cheServicesEnvironment.getServices().entrySet().forEach(entry -> {
CheServiceImpl actual = entry.getValue();
CheServiceImpl expected = expectedEnv.getServices().get(entry.getKey());
assertNotNull(expected);
assertEqualsNoOrder(actual.getExpose().toArray(), expected.getExpose().toArray(), format("Expose fields differ. Actual:%s. Expected:%s", actual.getExpose(), expected.getExpose()));
expected.setExpose(null);
actual.setExpose(null);
});
assertEquals(cheServicesEnvironment, expectedEnv);
}
Aggregations