Search in sources :

Example 56 with CheServiceImpl

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);
}
Also used : CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.testng.annotations.Test)

Example 57 with CheServiceImpl

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");
}
Also used : SnapshotImpl(org.eclipse.che.api.machine.server.model.impl.SnapshotImpl) Instance(org.eclipse.che.api.machine.server.spi.Instance) CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) ArrayList(java.util.ArrayList) MachineSourceImpl(org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl) CheServicesEnvironmentImpl(org.eclipse.che.api.environment.server.model.CheServicesEnvironmentImpl) Matchers.anyString(org.mockito.Matchers.anyString) ExtendedMachine(org.eclipse.che.api.core.model.workspace.ExtendedMachine) Machine(org.eclipse.che.api.core.model.machine.Machine) LineConsumer(org.eclipse.che.api.core.util.LineConsumer) Test(org.testng.annotations.Test)

Example 58 with CheServiceImpl

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()][]);
}
Also used : Listeners(org.testng.annotations.Listeners) MachineInstanceProviders(org.eclipse.che.api.machine.server.MachineInstanceProviders) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl) DataProvider(org.testng.annotations.DataProvider) Mock(org.mockito.Mock) DtoConverter(org.eclipse.che.api.workspace.server.DtoConverter) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) HashMap(java.util.HashMap) ExtendedMachineImpl(org.eclipse.che.api.workspace.server.model.impl.ExtendedMachineImpl) ServerConfDto(org.eclipse.che.api.machine.shared.dto.ServerConfDto) Mockito.spy(org.mockito.Mockito.spy) ServerConf2Impl(org.eclipse.che.api.workspace.server.model.impl.ServerConf2Impl) CheServicesEnvironmentImpl(org.eclipse.che.api.environment.server.model.CheServicesEnvironmentImpl) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) EnvironmentDto(org.eclipse.che.api.workspace.shared.dto.EnvironmentDto) MachineConfigDto(org.eclipse.che.api.machine.shared.dto.MachineConfigDto) Collections.singleton(java.util.Collections.singleton) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) CheServiceBuildContextImpl(org.eclipse.che.api.environment.server.model.CheServiceBuildContextImpl) Collections.singletonMap(java.util.Collections.singletonMap) InjectMocks(org.mockito.InjectMocks) Collections.emptyMap(java.util.Collections.emptyMap) EnvironmentRecipeImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentRecipeImpl) MockitoTestNGListener(org.mockito.testng.MockitoTestNGListener) MachineSourceDto(org.eclipse.che.api.machine.shared.dto.MachineSourceDto) Collections.emptyList(java.util.Collections.emptyList) Assert.fail(org.testng.Assert.fail) BeforeMethod(org.testng.annotations.BeforeMethod) DtoFactory.newDto(org.eclipse.che.dto.server.DtoFactory.newDto) ExtendedMachineDto(org.eclipse.che.api.workspace.shared.dto.ExtendedMachineDto) Mockito.when(org.mockito.Mockito.when) String.format(java.lang.String.format) Matchers.any(org.mockito.Matchers.any) List(java.util.List) Environment(org.eclipse.che.api.core.model.workspace.Environment) ServerException(org.eclipse.che.api.core.ServerException) CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) Joiner(com.google.common.base.Joiner) CheServiceBuildContextImpl(org.eclipse.che.api.environment.server.model.CheServiceBuildContextImpl) CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CheServicesEnvironmentImpl(org.eclipse.che.api.environment.server.model.CheServicesEnvironmentImpl) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) Collections.emptyMap(java.util.Collections.emptyMap) DataProvider(org.testng.annotations.DataProvider)

Example 59 with CheServiceImpl

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);
}
Also used : CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) HashMap(java.util.HashMap) CheServicesEnvironmentImpl(org.eclipse.che.api.environment.server.model.CheServicesEnvironmentImpl)

Example 60 with CheServiceImpl

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);
}
Also used : CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) Environment(org.eclipse.che.api.core.model.workspace.Environment) CheServicesEnvironmentImpl(org.eclipse.che.api.environment.server.model.CheServicesEnvironmentImpl) Test(org.testng.annotations.Test)

Aggregations

CheServiceImpl (org.eclipse.che.api.environment.server.model.CheServiceImpl)93 Test (org.testng.annotations.Test)63 CheServicesEnvironmentImpl (org.eclipse.che.api.environment.server.model.CheServicesEnvironmentImpl)46 Matchers.anyString (org.mockito.Matchers.anyString)32 LineConsumer (org.eclipse.che.api.core.util.LineConsumer)28 ArrayList (java.util.ArrayList)24 HashMap (java.util.HashMap)24 Map (java.util.Map)18 Machine (org.eclipse.che.api.core.model.machine.Machine)17 CreateContainerParams (org.eclipse.che.plugin.docker.client.params.CreateContainerParams)17 ServerException (org.eclipse.che.api.core.ServerException)16 EnvironmentImpl (org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl)16 IOException (java.io.IOException)14 List (java.util.List)14 ServerConf (org.eclipse.che.api.core.model.machine.ServerConf)14 Collections.singletonMap (java.util.Collections.singletonMap)13 Set (java.util.Set)13 CheServiceBuildContextImpl (org.eclipse.che.api.environment.server.model.CheServiceBuildContextImpl)13 Instance (org.eclipse.che.api.machine.server.spi.Instance)13 EnvironmentContext (org.eclipse.che.commons.env.EnvironmentContext)13