Search in sources :

Example 1 with TJob

use of io.elastest.etm.model.TJob in project elastest-torm by elastest.

the class DockerService2 method startDockbeat.

public void startDockbeat(DockerExecution dockerExec) throws TJobStoppedException {
    TJobExecution tJobExec = dockerExec.gettJobexec();
    TJob tJob = tJobExec.getTjob();
    Long execution = dockerExec.getExecutionId();
    String containerName = getDockbeatContainerName(dockerExec);
    // Environment variables
    ArrayList<String> envList = new ArrayList<>();
    String envVar;
    // Get Parameters and insert into Env VarsĀ”
    String lsHostEnvVar = "LOGSTASHHOST" + "=" + logstashHost;
    if (tJob.isSelectedService("ems")) {
        envVar = "FILTER_CONTAINERS" + "=" + "\"^sut\\d*_.*_" + execution + "|^test\\d*_.*_" + execution + "\"";
        envList.add(envVar);
        // envVar = "FILTER_EXCLUDE" + "=" + "\"\"";
        // envList.add(envVar);
        envVar = "LOGSTASHPORT" + "=" + tJobExec.getEnvVars().get("ET_EMS_LSBEATS_PORT");
        envList.add(envVar);
        lsHostEnvVar = "LOGSTASHHOST" + "=" + tJobExec.getEnvVars().get("ET_EMS_LSBEATS_HOST");
    }
    envList.add(lsHostEnvVar);
    // dockerSock
    Volume volume1 = new Volume(dockerSock);
    // Pull Image
    this.pullETExecImage(dockbeatImage, "Dockbeat");
    // Create Container
    logger.debug("Creating Dockbeat Container...");
    CreateContainerResponse container = dockerExec.getDockerClient().createContainerCmd(dockbeatImage).withEnv(envList).withName(containerName).withBinds(new Bind(dockerSock, volume1)).withNetworkMode(dockerExec.getNetwork()).exec();
    dockerExec.getDockerClient().startContainerCmd(container.getId()).exec();
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    }
    this.insertCreatedContainer(container.getId(), containerName);
}
Also used : Bind(com.github.dockerjava.api.model.Bind) Volume(com.github.dockerjava.api.model.Volume) TJobExecution(io.elastest.etm.model.TJobExecution) ArrayList(java.util.ArrayList) CreateContainerResponse(com.github.dockerjava.api.command.CreateContainerResponse) TJob(io.elastest.etm.model.TJob)

Example 2 with TJob

use of io.elastest.etm.model.TJob in project elastest-torm by elastest.

the class DockerService2 method createContainer.

public CreateContainerResponse createContainer(DockerExecution dockerExec, String type) throws TJobStoppedException {
    TJobExecution tJobExec = dockerExec.gettJobexec();
    TJob tJob = tJobExec.getTjob();
    SutSpecification sut = tJob.getSut();
    String image = "";
    String commands = null;
    List<Parameter> parametersList = new ArrayList<Parameter>();
    String prefix = "";
    String suffix = "";
    String containerName = "";
    int logPort = 5000;
    String sutHost = null;
    String sutPath = null;
    if ("sut".equals(type.toLowerCase())) {
        parametersList = sut.getParameters();
        commands = sut.getCommands();
        image = sut.getSpecification();
        prefix = "sut_";
        if (sut.isSutInNewContainer()) {
            suffix = sut.getSutInContainerAuxLabel();
        }
        containerName = getSutName(dockerExec);
        sutPath = filesService.buildFilesPath(tJobExec, ElastestConstants.SUT_FOLDER);
        filesService.createExecFilesFolder(sutPath);
    } else if ("tjob".equals(type.toLowerCase())) {
        parametersList = tJobExec.getParameters();
        commands = tJob.getCommands();
        image = tJob.getImageName();
        prefix = "test_";
        containerName = getTestName(dockerExec);
        if (dockerExec.isWithSut()) {
            sutHost = dockerExec.getSutExec().getIp();
        }
    }
    // Environment variables (optional)
    ArrayList<String> envList = new ArrayList<>();
    String envVar;
    // Get TJob Exec Env Vars
    for (Map.Entry<String, String> entry : dockerExec.gettJobexec().getEnvVars().entrySet()) {
        envVar = entry.getKey() + "=" + entry.getValue();
        envList.add(envVar);
    }
    // Get Parameters and insert into Env Vars
    for (Parameter parameter : parametersList) {
        envVar = parameter.getName() + "=" + parameter.getValue();
        envList.add(envVar);
    }
    if (sutHost != null) {
        envList.add("ET_SUT_HOST=" + dockerExec.getSutExec().getIp());
    }
    // Commands (optional)
    ArrayList<String> cmdList = new ArrayList<>();
    ArrayList<String> entrypointList = new ArrayList<>();
    if (commands != null && !commands.isEmpty()) {
        cmdList.add("-c");
        if (sut != null) {
            if (sut.isSutInNewContainer()) {
                commands = sutPath != null ? ("cd " + sutPath + ";" + commands) : commands;
            }
        } else {
            commands = "export ET_SUT_HOST=$(" + this.getThisContainerIpCmd + ") || echo;" + commands;
        }
        cmdList.add(commands);
        entrypointList.add("/bin/sh");
    }
    // Load Log Config
    LogConfig logConfig = null;
    if (tJob.isSelectedService("ems")) {
        try {
            logConfig = getEMSLogConfig(type, prefix, suffix, dockerExec);
        } catch (Exception e) {
            logger.error("", e);
        }
    } else {
        logConfig = getDefaultLogConfig(logPort, prefix, suffix, dockerExec);
    }
    // Pull Image
    this.pullETExecImage(image, type);
    // Create docker sock volume
    Volume dockerSockVolume = new Volume(dockerSock);
    CreateContainerCmd containerCmd = dockerExec.getDockerClient().createContainerCmd(image).withEnv(envList).withLogConfig(logConfig).withName(containerName).withCmd(cmdList).withEntrypoint(entrypointList).withNetworkMode(dockerExec.getNetwork());
    Volume sharedDataVolume = null;
    if (dockerExec.gettJobexec().getTjob().getSut() != null && dockerExec.gettJobexec().getTjob().getSut().isSutInNewContainer()) {
        sharedDataVolume = new Volume(sharedFolder);
    }
    if (sharedDataVolume != null) {
        containerCmd = containerCmd.withVolumes(dockerSockVolume, sharedDataVolume).withBinds(new Bind(dockerSock, dockerSockVolume), new Bind(sharedFolder, sharedDataVolume));
    } else {
        containerCmd = containerCmd.withVolumes(dockerSockVolume).withBinds(new Bind(dockerSock, dockerSockVolume));
    }
    // Create Container
    return containerCmd.exec();
}
Also used : Bind(com.github.dockerjava.api.model.Bind) TJobExecution(io.elastest.etm.model.TJobExecution) ArrayList(java.util.ArrayList) SutSpecification(io.elastest.etm.model.SutSpecification) TJob(io.elastest.etm.model.TJob) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NotModifiedException(com.github.dockerjava.api.exception.NotModifiedException) DockerClientException(com.github.dockerjava.api.exception.DockerClientException) InternalServerErrorException(com.github.dockerjava.api.exception.InternalServerErrorException) NotFoundException(com.github.dockerjava.api.exception.NotFoundException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) CreateContainerCmd(com.github.dockerjava.api.command.CreateContainerCmd) Volume(com.github.dockerjava.api.model.Volume) Parameter(io.elastest.etm.model.Parameter) Map(java.util.Map) HashMap(java.util.HashMap) LogConfig(com.github.dockerjava.api.model.LogConfig)

Example 3 with TJob

use of io.elastest.etm.model.TJob in project elastest-torm by elastest.

the class ExternalService method createElasTestEntitiesForExtJob.

private TJob createElasTestEntitiesForExtJob(ExternalJob externalJob) throws Exception {
    logger.info("Creating external job entities.");
    try {
        logger.debug("Creating Project.");
        Project project = projectService.getProjectByName(externalJob.getJobName());
        if (project == null) {
            project = new Project();
            project.setId(0L);
            project.setName(externalJob.getJobName());
            project = projectService.createProject(project);
        }
        logger.debug("Creating TJob.");
        TJob tJob = tJobService.getTJobByName(externalJob.getJobName());
        if (tJob == null) {
            tJob = new TJob();
            tJob.setName(externalJob.getJobName());
            tJob.setProject(project);
            tJob.setExternal(true);
            tJob = tJobService.createTJob(tJob);
        }
        if (externalJob.getTSServices() != null && externalJob.getTSServices().size() > 0) {
            tJob.setSelectedServices("[");
            for (TestSupportServices tSService : externalJob.getTSServices()) {
                tJob.setSelectedServices(tJob.getSelectedServices() + tSService.toJsonString());
            }
            tJob.setSelectedServices(tJob.getSelectedServices() + "]");
        }
        return tJob;
    } catch (Exception e) {
        e.printStackTrace();
        logger.error("Error message: " + e.getMessage());
        throw e;
    }
}
Also used : ExternalProject(io.elastest.etm.model.external.ExternalProject) Project(io.elastest.etm.model.Project) TestSupportServices(io.elastest.etm.api.model.TestSupportServices) TJob(io.elastest.etm.model.TJob) ExternalTJob(io.elastest.etm.model.external.ExternalTJob) HTTPException(javax.xml.ws.http.HTTPException)

Example 4 with TJob

use of io.elastest.etm.model.TJob in project elastest-torm by elastest.

the class EtmApiItTest method createTJob.

protected TJob createTJob(long projectId, long sutId) {
    String requestJson = "{" + "\"id\": 0," + "\"imageName\": \"elastest/test-etm-test1\"," + "\"name\": \"testApp1\"," + "\"parameters\": [{\"Param1\":\"Value1\"}]," + "\"resultsPath\": \"/app1TestJobsJenkins/target/surefire-reports/\"," + "\"project\": { \"id\":" + projectId + "}" + (sutId == -1 ? "" : ", \"sut\":{ \"id\":" + sutId + "}") + "}";
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity<String> entity = new HttpEntity<String>(requestJson, headers);
    log.info("POST /api/tjob");
    ResponseEntity<TJob> response = httpClient.postForEntity("/api/tjob", entity, TJob.class);
    if (response.getStatusCode() != HttpStatus.OK) {
        log.warn("Error creating TJob: " + response);
    }
    log.info("TJob created:" + response.getBody());
    return response.getBody();
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) TJob(io.elastest.etm.model.TJob)

Example 5 with TJob

use of io.elastest.etm.model.TJob in project elastest-torm by elastest.

the class TJobApiItTest method testCreateTJob.

@Test
public void testCreateTJob() {
    log.info("Start the test testCreateTJob");
    String requestJson = "{" + "\"id\": 0," + "\"imageName\": \"elastest/test-etm-test1\"," + "\"name\": \"testApp1\"," + "\"resultsPath\": \"/app1TestJobsJenkins/target/surefire-reports/\"," + "\"project\": { \"id\":" + projectId + "}" + "}";
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity<String> entity = new HttpEntity<String>(requestJson, headers);
    log.info("POST /api/tjob");
    ResponseEntity<TJob> response = httpClient.postForEntity("/api/tjob", entity, TJob.class);
    log.info("TJob created:" + response.getBody());
    deleteTJob(response.getBody().getId());
    assertAll("Validating tJob Properties", () -> assertTrue(response.getBody().getName().equals("testApp1")), () -> assertNotNull(response.getBody().getId()));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) TJob(io.elastest.etm.model.TJob) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

TJob (io.elastest.etm.model.TJob)17 TJobExecution (io.elastest.etm.model.TJobExecution)6 Test (org.junit.jupiter.api.Test)6 Project (io.elastest.etm.model.Project)5 SutSpecification (io.elastest.etm.model.SutSpecification)4 ArrayList (java.util.ArrayList)4 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4 HttpEntity (org.springframework.http.HttpEntity)4 HttpHeaders (org.springframework.http.HttpHeaders)4 Bind (com.github.dockerjava.api.model.Bind)2 Volume (com.github.dockerjava.api.model.Volume)2 Parameter (io.elastest.etm.model.Parameter)2 ExternalTJob (io.elastest.etm.model.external.ExternalTJob)2 HashMap (java.util.HashMap)2 HTTPException (javax.xml.ws.http.HTTPException)2 JsonView (com.fasterxml.jackson.annotation.JsonView)1 CreateContainerCmd (com.github.dockerjava.api.command.CreateContainerCmd)1 CreateContainerResponse (com.github.dockerjava.api.command.CreateContainerResponse)1 DockerClientException (com.github.dockerjava.api.exception.DockerClientException)1 InternalServerErrorException (com.github.dockerjava.api.exception.InternalServerErrorException)1