Skip to content

Commit

Permalink
Separate loading run artifacts into IRunResult method, fetchRun now o…
Browse files Browse the repository at this point in the history
…nly gets run details

Signed-off-by: Eamonn Mansour <47121388+eamansour@users.noreply.github.com>
  • Loading branch information
eamansour committed Sep 11, 2024
1 parent 8b5652e commit cdbb1db
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private CouchdbRasFileSystemProvider createFileSystemProvider() {
return new CouchdbRasFileSystemProvider(fileStore, store, logFactory);
}

private Path getRunArtifactPath(TestStructureCouchdb ts) throws CouchdbRasException {
public Path getRunArtifactPath(TestStructureCouchdb ts) throws CouchdbRasException {
CouchdbRasFileSystemProvider runProvider = createFileSystemProvider();
if (ts.getArtifactRecordIds() == null || ts.getArtifactRecordIds().isEmpty()) {
return runProvider.getRoot();
Expand Down Expand Up @@ -184,17 +184,7 @@ private Path getRunArtifactPath(TestStructureCouchdb ts) throws CouchdbRasExcept
}

private CouchdbRunResult fetchRun(String id) throws ParseException, IOException, ResultArchiveStoreException {
CouchdbRunResult runResult = fetchRunWithoutArtifacts(id);
TestStructureCouchdb testStructure = (TestStructureCouchdb) runResult.getTestStructure();

// Populate the run's artifacts filesystem
Path runArtifactPath = getRunArtifactPath(testStructure);

runResult = new CouchdbRunResult(store, testStructure, runArtifactPath);
return runResult;
}

private CouchdbRunResult fetchRunWithoutArtifacts(String id) throws ParseException, IOException, CouchdbRasException {
CouchdbRunResult runResult = null;
HttpGet httpGet = requestFactory.getHttpGetRequest(store.getCouchdbUri() + "/" + CouchdbRasStore.RUNS_DB + "/" + id);

try (CloseableHttpResponse response = store.getHttpClient().execute(httpGet)) {
Expand All @@ -207,10 +197,9 @@ private CouchdbRunResult fetchRunWithoutArtifacts(String id) throws ParseExcepti
String responseEntity = EntityUtils.toString(entity);
TestStructureCouchdb ts = store.getGson().fromJson(responseEntity, TestStructureCouchdb.class);

// *** Add this run to the results
CouchdbRunResult cdbrr = new CouchdbRunResult(store, ts, createFileSystemProvider().getRoot());
return cdbrr;
runResult = new CouchdbRunResult(store, ts, logFactory);
}
return runResult;
}

@Override
Expand Down Expand Up @@ -375,11 +364,10 @@ private RasRunResultPage getRunsPageFromCouchdb(HttpPost httpPost, Find query) t

for (TestStructureCouchdb ts : found.docs) {
if (ts.isValid()) {
// Don't load the artifacts for the found runs, just set a root location for artifacts
CouchdbRasFileSystemProvider runProvider = createFileSystemProvider();

// Add this run to the results
runs.add(new CouchdbRunResult(store, ts, runProvider.getRoot()));
// Don't load the artifacts for the found runs, just set a root location for artifacts
// and add this run to the results
runs.add(new CouchdbRunResult(store, ts, logFactory));
}
}

Expand Down Expand Up @@ -442,21 +430,14 @@ private JsonArray buildQuerySortJson(@NotNull RasSortField primarySort) {
return runs;
}

public void discardRun(String id) throws ResultArchiveStoreException {
public void discardRun(@NotNull TestStructureCouchdb runTestStructure) throws ResultArchiveStoreException {
try {
CouchdbRunResult run = fetchRunWithoutArtifacts(id);
if (run == null) {
logger.info("Run with ID " + id + " does not exist or has already been discarded");
} else {
TestStructureCouchdb testStructure = (TestStructureCouchdb) run.getTestStructure();

discardRunLogs(testStructure.getLogRecordIds());
discardRunArtifacts(testStructure.getArtifactRecordIds());

discardRecord(CouchdbRasStore.RUNS_DB, id, testStructure._rev);
}
} catch (CouchdbRasException | ParseException | IOException e) {
throw new ResultArchiveStoreException("Failed to discard run: " + id, e);
discardRunLogs(runTestStructure.getLogRecordIds());
discardRunArtifacts(runTestStructure.getArtifactRecordIds());

discardRecord(CouchdbRasStore.RUNS_DB, runTestStructure._id, runTestStructure._rev);
} catch (CouchdbRasException | ParseException e) {
throw new ResultArchiveStoreException("Failed to discard run: " + runTestStructure._id, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,30 @@

import java.nio.file.Path;

import dev.galasa.extensions.common.api.LogFactory;
import dev.galasa.framework.spi.IRunResult;
import dev.galasa.framework.spi.ResultArchiveStoreException;
import dev.galasa.framework.spi.ras.ResultArchiveStoreFileStore;
import dev.galasa.framework.spi.teststructure.TestStructure;
import dev.galasa.ras.couchdb.internal.pojos.TestStructureCouchdb;

public class CouchdbRunResult implements IRunResult {

private final TestStructureCouchdb testStructure;
private final Path path;
private final CouchdbRasStore store;
private Path path;

public CouchdbRunResult(CouchdbRasStore store, TestStructureCouchdb testStructure, Path path) {
public CouchdbRunResult(CouchdbRasStore store, TestStructureCouchdb testStructure, LogFactory logFactory) {
this.store = store;
if (testStructure == null) {
this.testStructure = new TestStructureCouchdb();
} else {
this.testStructure = testStructure;
}
this.path = path;

// Create an empty artifact filesystem and set the artifacts path to the root of this filesystem
ResultArchiveStoreFileStore fileStore = new ResultArchiveStoreFileStore();
this.path = new CouchdbRasFileSystemProvider(fileStore, store, logFactory).getRoot();
}

@Override
Expand All @@ -46,12 +51,18 @@ public String getLog() throws ResultArchiveStoreException {
@Override
public void discard() throws ResultArchiveStoreException {
CouchdbDirectoryService storeService = (CouchdbDirectoryService) store.getDirectoryServices().get(0);
storeService.discardRun(this.testStructure._id);
storeService.discardRun(this.testStructure);
}

@Override
public String getRunId() {
return "cdb-" + this.testStructure._id;
}

@Override
public void loadArtifacts() throws ResultArchiveStoreException {
CouchdbDirectoryService storeService = (CouchdbDirectoryService) store.getDirectoryServices().get(0);
this.path = storeService.getRunArtifactPath(this.testStructure);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ public void testGetRunsPageWithNilBookmarkReturnsPageWithNoNextCursor() throws E
public void testDiscardRunDeletesRunOk() throws Exception {
// Given...
String runId = "ABC123";
TestStructureCouchdb mockRun1 = createRunTestStructure("run1");
TestStructureCouchdb mockRun1 = createRunTestStructure(runId);

IdRev mockIdRev = new IdRev();
String revision = "this-is-a-revision";
Expand All @@ -584,10 +584,7 @@ public void testDiscardRunDeletesRunOk() throws Exception {
String runDbUri = baseUri + "/" + CouchdbRasStore.RUNS_DB + "/" + runId;
String artifactsDbUri = baseUri + "/" + CouchdbRasStore.ARTIFACTS_DB;
String logsDbUri = baseUri + "/" + CouchdbRasStore.LOG_DB;
List<HttpInteraction> interactions = List.of(
// Fetch the run to be deleted
new GetRunByIdFromCouchdbInteraction(runDbUri, HttpStatus.SC_OK, mockRun1),

List<HttpInteraction> interactions = List.of(
// Start discarding the run's log records
new GetDocumentByIdFromCouchdbInteraction(logsDbUri + "/" + logId1, HttpStatus.SC_OK, mockIdRev),
new DeleteDocumentFromCouchdbInteraction(logsDbUri + "/" + logId1 + "?rev=" + revision, HttpStatus.SC_OK),
Expand All @@ -609,7 +606,7 @@ public void testDiscardRunDeletesRunOk() throws Exception {
CouchdbDirectoryService directoryService = new CouchdbDirectoryService(mockRasStore, mockLogFactory, new HttpRequestFactoryImpl());

// When...
directoryService.discardRun(runId);
directoryService.discardRun(mockRun1);

// Then...
// The assertions in the interactions should not have failed
Expand All @@ -619,7 +616,7 @@ public void testDiscardRunDeletesRunOk() throws Exception {
public void testDiscardRunWithNoArtifactsDeletesRunOk() throws Exception {
// Given...
String runId = "ABC123";
TestStructureCouchdb mockRun1 = createRunTestStructure("run1");
TestStructureCouchdb mockRun1 = createRunTestStructure(runId);

IdRev mockIdRev = new IdRev();
String revision = "this-is-a-revision";
Expand All @@ -636,9 +633,6 @@ public void testDiscardRunWithNoArtifactsDeletesRunOk() throws Exception {
String runDbUri = baseUri + "/" + CouchdbRasStore.RUNS_DB + "/" + runId;
String logsDbUri = baseUri + "/" + CouchdbRasStore.LOG_DB;
List<HttpInteraction> interactions = List.of(
// Fetch the run to be deleted
new GetRunByIdFromCouchdbInteraction(runDbUri, HttpStatus.SC_OK, mockRun1),

// Start discarding the run's log records
new GetDocumentByIdFromCouchdbInteraction(logsDbUri + "/" + logId1, HttpStatus.SC_OK, mockIdRev),
new DeleteDocumentFromCouchdbInteraction(logsDbUri + "/" + logId1 + "?rev=" + revision, HttpStatus.SC_OK),
Expand All @@ -654,7 +648,7 @@ public void testDiscardRunWithNoArtifactsDeletesRunOk() throws Exception {
CouchdbDirectoryService directoryService = new CouchdbDirectoryService(mockRasStore, mockLogFactory, new HttpRequestFactoryImpl());

// When...
directoryService.discardRun(runId);
directoryService.discardRun(mockRun1);

// Then...
// The assertions in the interactions should not have failed
Expand All @@ -664,14 +658,11 @@ public void testDiscardRunWithNoArtifactsDeletesRunOk() throws Exception {
public void testDiscardRunWithNoArtifactsAndLogsDeletesRunOk() throws Exception {
// Given...
String runId = "ABC123";
TestStructureCouchdb mockRun1 = createRunTestStructure("run1");
TestStructureCouchdb mockRun1 = createRunTestStructure(runId);

String baseUri = "http://my.uri";
String runDbUri = baseUri + "/" + CouchdbRasStore.RUNS_DB + "/" + runId;
List<HttpInteraction> interactions = List.of(
// Fetch the run to be deleted
new GetRunByIdFromCouchdbInteraction(runDbUri, HttpStatus.SC_OK, mockRun1),

// Delete the record of the run
new DeleteDocumentFromCouchdbInteraction(runDbUri + "?rev=" + mockRun1._rev, HttpStatus.SC_OK)
);
Expand All @@ -681,7 +672,7 @@ public void testDiscardRunWithNoArtifactsAndLogsDeletesRunOk() throws Exception
CouchdbDirectoryService directoryService = new CouchdbDirectoryService(mockRasStore, mockLogFactory, new HttpRequestFactoryImpl());

// When...
directoryService.discardRun(runId);
directoryService.discardRun(mockRun1);

// Then...
// The assertions in the interactions should not have failed
Expand All @@ -691,14 +682,11 @@ public void testDiscardRunWithNoArtifactsAndLogsDeletesRunOk() throws Exception
public void testDiscardRunWithCouchdbServerErrorThrowsCorrectError() throws Exception {
// Given...
String runId = "ABC123";
TestStructureCouchdb mockRun1 = createRunTestStructure("run1");
TestStructureCouchdb mockRun1 = createRunTestStructure(runId);

String baseUri = "http://my.uri";
String runDbUri = baseUri + "/" + CouchdbRasStore.RUNS_DB + "/" + runId;
List<HttpInteraction> interactions = List.of(
// Fetch the run to be deleted
new GetRunByIdFromCouchdbInteraction(runDbUri, HttpStatus.SC_OK, mockRun1),

// Delete the record of the run
new DeleteDocumentFromCouchdbInteraction(runDbUri + "?rev=" + mockRun1._rev, HttpStatus.SC_INTERNAL_SERVER_ERROR)
);
Expand All @@ -709,36 +697,12 @@ public void testDiscardRunWithCouchdbServerErrorThrowsCorrectError() throws Exce

// When...
ResultArchiveStoreException thrown = catchThrowableOfType(() -> {
directoryService.discardRun(runId);
directoryService.discardRun(mockRun1);
}, ResultArchiveStoreException.class);

// Then...
// The assertions in the interactions should not have failed
assertThat(thrown).isNotNull();
assertThat(thrown.getMessage()).contains("Unable to delete run", runId);
}

@Test
public void testDiscardRunWithNonExistantRunDoesNotThrowError() throws Exception {
// Given...
String runId = "ABC123";
TestStructureCouchdb mockRun1 = createRunTestStructure("run1");

String baseUri = "http://my.uri";
String runDbUri = baseUri + "/" + CouchdbRasStore.RUNS_DB + "/" + runId;
List<HttpInteraction> interactions = List.of(
new GetRunByIdFromCouchdbInteraction(runDbUri, HttpStatus.SC_INTERNAL_SERVER_ERROR, mockRun1)
);

MockLogFactory mockLogFactory = new MockLogFactory();
CouchdbRasStore mockRasStore = fixtures.createCouchdbRasStore(interactions, mockLogFactory);
CouchdbDirectoryService directoryService = new CouchdbDirectoryService(mockRasStore, mockLogFactory, new HttpRequestFactoryImpl());

// When...
directoryService.discardRun(runId);

// Then...
// The assertions in the interactions should not have failed
assertThat(mockLogFactory.toString()).contains(runId, "does not exist or has already been discarded");
}
}

0 comments on commit cdbb1db

Please sign in to comment.