use of org.jboss.pnc.spi.builddriver.BuildDriverResult in project pnc by project-ncl.
the class BuildDriverMock method complete.
protected void complete(BuildExecutionSession buildExecutionSession, final RunningEnvironment runningEnvironment, Consumer<CompletedBuild> onComplete) throws InterruptedException {
Thread.sleep(RandomUtils.randInt(100, 300));
setBuildStatus(buildExecutionSession.getBuildExecutionConfiguration().getBuildScript());
onComplete.accept(new CompletedBuild() {
@Override
public BuildDriverResult getBuildResult() throws BuildDriverException {
return getBuildResultMock(runningEnvironment);
}
@Override
public RunningEnvironment getRunningEnvironment() {
return runningEnvironment;
}
});
}
use of org.jboss.pnc.spi.builddriver.BuildDriverResult in project pnc by project-ncl.
the class BuildResultMock method mock.
public static BuildResult mock(BuildStatus status) {
BuildExecutionConfiguration buildExecutionConfig = BuildExecutionConfigurationMock.mockConfig();
BuildDriverResult buildDriverResult = BuildDriverResultMock.mockResult(status);
RepositoryManagerResult repositoryManagerResult = RepositoryManagerResultMock.mockResult();
ExecutorException exception = buildException();
CompletionStatus completionStatus;
if (status.completedSuccessfully()) {
completionStatus = CompletionStatus.SUCCESS;
} else {
completionStatus = CompletionStatus.FAILED;
}
return new BuildResult(completionStatus, Optional.of(new ProcessException("Test Exception.")), "", Optional.ofNullable(buildExecutionConfig), Optional.ofNullable(buildDriverResult), Optional.ofNullable(repositoryManagerResult), Optional.of(EnvironmentDriverResultMock.mock()), Optional.of(RepourResultMock.mock()));
}
use of org.jboss.pnc.spi.builddriver.BuildDriverResult in project pnc by project-ncl.
the class BuildExecutorMock method mockBuild.
private Boolean mockBuild(BuildExecutionSession buildExecutionSession) {
log.debug("Building {}.", buildExecutionSession.getId());
BuildDriverResult driverResult;
Boolean buildPassed;
if (TestProjectConfigurationBuilder.FAIL.equals(buildExecutionSession.getBuildExecutionConfiguration().getBuildScript())) {
log.debug("Marking build {} as Failed.", buildExecutionSession.getId());
driverResult = BuildDriverResultMock.mockResult(BuildStatus.FAILED);
buildExecutionSession.setStatus(BuildExecutionStatus.BUILD_COMPLETED_WITH_ERROR);
buildPassed = false;
} else if (TestProjectConfigurationBuilder.FAIL_WITH_DELAY.equals(buildExecutionSession.getBuildExecutionConfiguration().getBuildScript())) {
log.debug("Waiting for a while for a build {}.", buildExecutionSession.getId());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
log.warn("Build mock has been interrupted.", e);
}
log.debug("Marking build {} as Failed.", buildExecutionSession.getId());
driverResult = BuildDriverResultMock.mockResult(BuildStatus.FAILED);
buildPassed = false;
} else if (TestProjectConfigurationBuilder.CANCEL.equals(buildExecutionSession.getBuildExecutionConfiguration().getBuildScript())) {
log.debug("Waiting for a while for a build {} to be canceled.", buildExecutionSession.getId());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
log.warn("Build mock has been interrupted.", e);
}
driverResult = BuildDriverResultMock.mockResult(BuildStatus.CANCELLED);
buildPassed = false;
} else {
log.debug("Marking build {} as Success.", buildExecutionSession.getId());
driverResult = BuildDriverResultMock.mockResult(BuildStatus.SUCCESS);
RepositoryManagerResult repositoryManagerResult = RepositoryManagerResultMock.mockResult();
buildExecutionSession.setRepositoryManagerResult(repositoryManagerResult);
buildPassed = true;
}
buildExecutionSession.setBuildDriverResult(driverResult);
return buildPassed;
}
use of org.jboss.pnc.spi.builddriver.BuildDriverResult in project pnc by project-ncl.
the class BuildTaskEndpointTest method shouldAcceptCompletionResultAsSingleJson.
@Test
public void shouldAcceptCompletionResultAsSingleJson() throws RemoteResourceException {
// given
BuildDriverResult buildDriverResult = new DefaultBuildDriverResult("The log!", BuildStatus.SYSTEM_ERROR, java.util.Optional.of("12345"));
BuildDriverResultRest buildDriverResultRest = new BuildDriverResultRest(buildDriverResult);
BuildResultRest buildResultRest = new BuildResultRest();
buildResultRest.setBuildDriverResult(buildDriverResultRest);
// when
HttpPost request = new HttpPost(url + BASE_REST_PATH + "/build-tasks/42/completed");
request.addHeader(Credentials.USER.createAuthHeader(BasicHeader::new));
request.addHeader("Content-type", MediaType.APPLICATION_JSON);
String jsonBody = JsonOutputConverterMapper.apply(buildResultRest);
log.debug("Json body: {}.", jsonBody);
request.setEntity(new StringEntity(jsonBody, ContentType.APPLICATION_JSON));
// then
int statusCode = -1;
try (CloseableHttpClient httpClient = HttpUtils.getPermissiveHttpClient()) {
try (CloseableHttpResponse response = httpClient.execute(request)) {
statusCode = response.getStatusLine().getStatusCode();
Assert.assertEquals("Received error response code. Response: " + printEntity(response), // validation failure is expected; 500 when deserialization fails
400, statusCode);
}
} catch (IOException e) {
Assertions.fail("Cannot invoke remote endpoint.", e);
}
}
use of org.jboss.pnc.spi.builddriver.BuildDriverResult in project pnc by project-ncl.
the class DatastoreAdapter method storeResult.
public BuildRecord storeResult(BuildTask buildTask, BuildResult buildResult) throws DatastoreException {
try {
BuildStatus buildRecordStatus = NEW;
BuildRecord.Builder buildRecordBuilder = initBuildRecordBuilder(buildTask);
buildRecordBuilder.buildContentId(buildTask.getContentId());
if (buildResult.getRepourResult().isPresent()) {
RepourResult repourResult = buildResult.getRepourResult().get();
buildRecordBuilder.repourLog(repourResult.getLog());
buildRecordBuilder.executionRootName(repourResult.getExecutionRootName());
buildRecordBuilder.executionRootVersion(repourResult.getExecutionRootVersion());
CompletionStatus repourCompletionStatus = repourResult.getCompletionStatus();
if (repourCompletionStatus != null) {
switch(repourCompletionStatus) {
case SUCCESS:
case NO_REBUILD_REQUIRED:
break;
case FAILED:
buildRecordBuilder.appendLog("\nBuild failed during the alignment phase, please check the 'Alignment Log' tab for more information.\n");
buildRecordStatus = FAILED;
break;
case CANCELLED:
buildRecordBuilder.appendLog("\nBuild cancelled during alignment phase.\n");
buildRecordStatus = CANCELLED;
break;
case TIMED_OUT:
buildRecordBuilder.appendLog("\nBuild timed-out during alignment phase.\n");
buildRecordStatus = SYSTEM_ERROR;
break;
case SYSTEM_ERROR:
buildRecordBuilder.appendLog("\nBuild failed with SYSTEM_ERROR during the alignment phase, " + "please check the 'Alignment Log' tab for more information.\n");
buildRecordStatus = SYSTEM_ERROR;
break;
default:
buildRecordBuilder.appendLog("\nInvalid status during the alignment phase, failing with SYSTEM_ERROR.\n");
buildRecordStatus = SYSTEM_ERROR;
break;
}
}
} else {
userLog.warn("Missing Repour Result!");
log.warn("[BuildTask:" + buildTask.getId() + "] Missing RepourResult.");
}
if (buildResult.getBuildDriverResult().isPresent()) {
BuildDriverResult buildDriverResult = buildResult.getBuildDriverResult().get();
buildRecordBuilder.appendLog(buildDriverResult.getBuildLog());
buildDriverResult.getOutputChecksum().ifPresent(buildRecordBuilder::buildOutputChecksum);
// TODO buildRecord should use CompletionStatus
buildRecordStatus = buildDriverResult.getBuildStatus();
} else if (!buildResult.hasFailed()) {
return storeResult(buildTask, Optional.of(buildResult), new BuildCoordinationException("Trying to store success build with incomplete result. Missing BuildDriverResult."));
}
if (buildResult.getEnvironmentDriverResult().isPresent()) {
EnvironmentDriverResult environmentDriverResult = buildResult.getEnvironmentDriverResult().get();
buildRecordBuilder.appendLog(environmentDriverResult.getLog());
environmentDriverResult.getSshCredentials().ifPresent(c -> {
buildRecordBuilder.sshCommand(c.getCommand());
buildRecordBuilder.sshPassword(c.getPassword());
});
if (environmentDriverResult.getCompletionStatus() != null) {
switch(environmentDriverResult.getCompletionStatus()) {
case SUCCESS:
case NO_REBUILD_REQUIRED:
break;
case FAILED:
buildRecordStatus = FAILED;
break;
case CANCELLED:
buildRecordStatus = CANCELLED;
break;
case TIMED_OUT:
case SYSTEM_ERROR:
buildRecordStatus = SYSTEM_ERROR;
break;
default:
buildRecordBuilder.appendLog("\nInvalid status during the environment setup phase, failing with SYSTEM_ERROR.\n");
buildRecordStatus = SYSTEM_ERROR;
break;
}
}
}
List<Artifact> builtArtifacts = Collections.emptyList();
List<Artifact> dependencies = Collections.emptyList();
if (buildResult.getRepositoryManagerResult().isPresent()) {
RepositoryManagerResult repositoryManagerResult = buildResult.getRepositoryManagerResult().get();
buildRecordBuilder.appendLog(repositoryManagerResult.getLog());
if (repositoryManagerResult.getCompletionStatus() != null) {
switch(// TODO, do not mix statuses
repositoryManagerResult.getCompletionStatus()) {
case SUCCESS:
case NO_REBUILD_REQUIRED:
break;
case FAILED:
buildRecordStatus = FAILED;
break;
case CANCELLED:
buildRecordStatus = CANCELLED;
break;
case TIMED_OUT:
case SYSTEM_ERROR:
buildRecordStatus = SYSTEM_ERROR;
break;
default:
buildRecordBuilder.appendLog("\nInvalid status during the promotion phase, failing with SYSTEM_ERROR.\n");
buildRecordStatus = SYSTEM_ERROR;
break;
}
}
builtArtifacts = repositoryManagerResult.getBuiltArtifacts();
Map<Artifact, String> builtConflicts = datastore.checkForBuiltArtifacts(builtArtifacts);
if (builtConflicts.size() > 0) {
return storeResult(buildTask, Optional.of(buildResult), new BuildCoordinationException("Trying to store success build with invalid repository manager result. Conflicting artifact data found: " + builtConflicts.toString()));
}
dependencies = repositoryManagerResult.getDependencies();
} else if (!buildResult.hasFailed()) {
return storeResult(buildTask, Optional.of(buildResult), new BuildCoordinationException("Trying to store success build with incomplete result. Missing RepositoryManagerResult."));
}
if (NEW.equals(buildRecordStatus)) {
switch(buildResult.getCompletionStatus()) {
case SUCCESS:
case NO_REBUILD_REQUIRED:
case FAILED:
case SYSTEM_ERROR:
break;
case CANCELLED:
buildRecordStatus = CANCELLED;
break;
case TIMED_OUT:
buildRecordStatus = SYSTEM_ERROR;
buildRecordBuilder.appendLog("-- Operation TIMED-OUT --");
userLog.warn("Operation TIMED-OUT.");
break;
default:
buildRecordBuilder.appendLog("\nInvalid status detected in the final completion status, failing with SYSTEM_ERROR.\n");
break;
}
}
log.debug("Setting status " + buildRecordStatus.toString() + " to buildRecord.");
buildRecordBuilder.status(buildRecordStatus);
if (buildResult.getBuildExecutionConfiguration().isPresent()) {
BuildExecutionConfiguration buildExecutionConfig = buildResult.getBuildExecutionConfiguration().get();
buildRecordBuilder.scmRepoURL(buildExecutionConfig.getScmRepoURL());
buildRecordBuilder.scmRevision(buildExecutionConfig.getScmRevision());
buildRecordBuilder.scmTag(buildExecutionConfig.getScmTag());
} else if (!buildResult.hasFailed()) {
return storeResult(buildTask, Optional.of(buildResult), new BuildCoordinationException("Trying to store success build with incomplete result. Missing BuildExecutionConfiguration."));
}
log.debug("Storing results of buildTask [{}] to datastore.", buildTask.getId());
userLog.info("Successfully completed.");
return datastore.storeCompletedBuild(buildRecordBuilder, builtArtifacts, dependencies);
} catch (Exception e) {
return storeResult(buildTask, Optional.of(buildResult), e);
}
}
Aggregations