Skip to content

Commit

Permalink
Simplify maintenance tests (#905)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkEWaite authored Sep 14, 2022
1 parent 9f64c6b commit 853f2df
Showing 1 changed file with 53 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.rules.ErrorCollector;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
Expand Down Expand Up @@ -60,6 +61,10 @@ public class GitClientMaintenanceTest {

@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();

@Rule
public ErrorCollector collector = new ErrorCollector();

private File repoRoot = null;
private LogHandler handler;

Expand Down Expand Up @@ -134,9 +139,9 @@ public void setGitClient() throws IOException, InterruptedException {
TaskListener listener = newListener(handler);
gitClient = Git.with(listener, new EnvVars()).in(repoRoot).using(gitImplName).getClient();
File gitDir = gitClient.withRepository((repo, channel) -> repo.getDirectory());
assertThat("Premature " + gitDir, gitDir, is(not(anExistingDirectory())));
collector.checkThat("Premature " + gitDir, gitDir, is(not(anExistingDirectory())));
gitClient.init_().workspace(repoRoot.getAbsolutePath()).execute();
assertThat("Missing " + gitDir, gitDir, is(anExistingDirectory()));
collector.checkThat("Missing " + gitDir, gitDir, is(anExistingDirectory()));
gitClient.setRemoteUrl("origin", srcRepoDir.getAbsolutePath());
CliGitCommand gitCmd = new CliGitCommand(gitClient);
gitCmd.run("config", "user.name", "Vojtěch GitClientMaintenanceTest Zweibrücken-Šafařík");
Expand All @@ -150,12 +155,10 @@ public void setGitClient() throws IOException, InterruptedException {
looseObjectsSupported = false;
}
if (!cliGitClient.isAtLeastVersion(2, 19, 0, 0)) {
incrementalRepackSupported = false;
prefetchSupported = false;
looseObjectsSupported = false;
}
if (!cliGitClient.isAtLeastVersion(2, 25, 0, 0)) {
incrementalRepackSupported = false;
prefetchSupported = false;
looseObjectsSupported = false;
}
Expand Down Expand Up @@ -202,7 +205,7 @@ private ObjectId commitFile(final String path, final String content, final Strin
gitClient.add(path);
gitClient.commit(commitMessage);
List<ObjectId> headList = gitClient.revList(Constants.HEAD);
assertThat(headList.size(), is(greaterThan(0)));
collector.checkThat(headList.size(), is(greaterThan(0)));
return headList.get(0);
}

Expand All @@ -219,19 +222,13 @@ private void createFile(String path, String content) {
}
}

@Test
public void test_commit_graph_maintenance() throws Exception {
if (!commitGraphSupported) {
return;
private String getExpectedMessage(String maintenanceTask, boolean expectedResult) {
if (gitImplName.startsWith("jgit")) {
return "JGIT doesn't support git maintenance. Use CLIGIT to execute maintenance tasks.";
}

commitSeveralFiles();

boolean isExecuted = gitClient.maintenance("commit-graph");

String expectedMessage = "Git maintenance task commit-graph finished";
assertThat(handler.getMessages(), hasItem(startsWith(expectedMessage)));
assertThat(isExecuted,is(true));
return expectedResult
? "Git maintenance task " + maintenanceTask + " finished"
: "Error executing " + maintenanceTask + " maintenance task";
}

@Test
Expand All @@ -248,124 +245,90 @@ public void test_loose_objects_maintenance() throws Exception {

// Assert loose objects are in the objects directory
String looseObjects[] = objectsPath.list();
assertThat(Arrays.asList(looseObjects), hasItems(expectedDirList));
assertThat("Missing expected loose objects in objects dir, only found " + Arrays.stream(looseObjects).collect(Collectors.joining(",")),
collector.checkThat(Arrays.asList(looseObjects), hasItems(expectedDirList));
collector.checkThat("Missing expected loose objects in objects dir, only found " + Arrays.stream(looseObjects).collect(Collectors.joining(",")),
looseObjects.length, is(greaterThan(2))); // Initially loose objects are present

// Run the loose objects maintenance task, will create loose-objects pack file
boolean isExecuted = gitClient.maintenance("loose-objects");
// Check if maintenance has executed successfully.
collector.checkThat(isExecuted, is(true));

// Confirm loose-object pack file is present in the pack directory
File looseObjectPackFilePath = new File(objectsPath.getAbsolutePath(), "pack");
String[] looseObjectPackFile = looseObjectPackFilePath.list((dir1, name) -> name.startsWith("loose-"));
assertThat("Missing expected loose objects in git dir, only found " + Arrays.stream(looseObjectPackFile).collect(Collectors.joining(",")),
collector.checkThat("Missing expected loose objects in git dir, only found " + Arrays.stream(looseObjectPackFile).collect(Collectors.joining(",")),
looseObjectPackFile.length, is(2)); // Contains loose-${hash}.pack and loose-${hash}.idx

// Check if maintenance has executed successfully.
assertThat(isExecuted,is(true));

// Clean the loose objects present in the repo.
isExecuted = gitClient.maintenance("loose-objects");

// Assert that loose objects are no longer in the objects directory
assertThat(objectsPath.list(), is(arrayContainingInAnyOrder(expectedDirList)));
collector.checkThat(objectsPath.list(), is(arrayContainingInAnyOrder(expectedDirList)));

assertThat(isExecuted,is(true));
collector.checkThat(isExecuted, is(true));
}

@Test
public void test_incremental_repack_maintenance() throws Exception {
if (!incrementalRepackSupported) {
return;
}
String maintenanceTask = "incremental-repack";

commitSeveralFiles();

// Run incremental repack maintenance task
boolean isExecuted = gitClient.maintenance("gc"); // Need to create pack files to use incremental repack
assertThat(isExecuted,is(true));
isExecuted = gitClient.maintenance("incremental-repack"); // Running incremental repack on the pack-files
assertThat(isExecuted,is(true));
// Need to create pack files to use incremental repack
collector.checkThat(gitClient.maintenance("gc"), is(!gitImplName.startsWith("jgit"))); // No gc on JGit maintenace

String expectedMessage = "Git maintenance task incremental-repack finished";
assertThat(handler.getMessages(), hasItem(startsWith(expectedMessage)));
collector.checkThat(gitClient.maintenance(maintenanceTask), is(incrementalRepackSupported));

String expectedMessage = getExpectedMessage(maintenanceTask, incrementalRepackSupported);
collector.checkThat(handler.getMessages(), hasItem(startsWith(expectedMessage)));
}

@Test
public void test_gc_maintenance() throws Exception {
if (!garbageCollectionSupported) {
return;
}
public void test_commit_graph_maintenance() throws Exception {
String maintenanceTask = "commit-graph";

commitSeveralFiles();

boolean isExecuted = gitClient.maintenance("gc");
collector.checkThat(gitClient.maintenance(maintenanceTask), is(commitGraphSupported));

String expectedMessage = "Git maintenance task gc finished";
assertThat(handler.getMessages(), hasItem(startsWith(expectedMessage)));
assertThat(isExecuted,is(true));
String expectedMessage = getExpectedMessage(maintenanceTask, commitGraphSupported);
collector.checkThat(handler.getMessages(), hasItem(startsWith(expectedMessage)));
}

@Test
public void test_prefetch_maintenance() throws Exception {
if (!prefetchSupported) {
return;
}
public void test_gc_maintenance() throws Exception {
String maintenanceTask = "gc";

commitSeveralFiles();

boolean isExecuted = gitClient.maintenance("prefetch");
collector.checkThat(gitClient.maintenance("gc"), is(garbageCollectionSupported));

// Can improve the test by checking prefetch dir in .git/refs directory.
String expectedMessage = "Git maintenance task prefetch finished";
assertThat(handler.getMessages(), hasItem(startsWith(expectedMessage)));
assertThat(isExecuted,is(true));
String expectedMessage = getExpectedMessage(maintenanceTask, garbageCollectionSupported);
collector.checkThat(handler.getMessages(), hasItem(startsWith(expectedMessage)));
}

@Test
public void test_error_reported_by_invalid_maintenance_task() throws Exception {
if (gitImplName.startsWith("jgit")) {
return;
}
public void test_prefetch_maintenance() throws Exception {
String maintenanceTask = "prefetch";

boolean isExecuted = gitClient.maintenance("invalid-maintenance-task");
collector.checkThat(gitClient.maintenance("prefetch"), is(prefetchSupported));

String expectedMessage = "Error executing invalid-maintenance-task maintenance task";
assertThat(handler.getMessages(), hasItem(expectedMessage));
assertThat(isExecuted,is(false));
}

private Map<String, Boolean> getExpectedTaskResults() throws Exception {
Map<String, Boolean> expectedTaskResults = new HashMap<>();
expectedTaskResults.put("gc", true);
expectedTaskResults.put("incremental-repack", incrementalRepackSupported);
expectedTaskResults.put("commit-graph", commitGraphSupported);
expectedTaskResults.put("prefetch", prefetchSupported);
expectedTaskResults.put("loose-objects", looseObjectsSupported);
return expectedTaskResults;
String expectedMessage = getExpectedMessage(maintenanceTask, prefetchSupported);
collector.checkThat(handler.getMessages(), hasItem(startsWith(expectedMessage)));
}

@Test
public void test_legacy_maintenance() throws Exception {
if (gitImplName.startsWith("jgit")) {
return;
}

commitSeveralFiles();
gitClient.maintenance("gc"); // Need to create pack files to use incremental repack
commitSeveralFiles();

Map<String, Boolean> expectedTaskResults = getExpectedTaskResults();

for (Map.Entry<String, Boolean> entry : expectedTaskResults.entrySet()) {
String maintenanceTask = entry.getKey();
boolean expectedResult = entry.getValue();
public void test_error_reported_by_invalid_maintenance_task() throws Exception {
String maintenanceTask = "invalid-maintenance-task";

boolean isExecuted = gitClient.maintenance(maintenanceTask);
// Should always fail to execute
collector.checkThat(gitClient.maintenance(maintenanceTask), is(false));

String expectedMessage = expectedResult
? "Git maintenance task " + maintenanceTask + " finished"
: "Error executing " + maintenanceTask + " maintenance task";
assertThat(handler.getMessages(), hasItem(containsString(expectedMessage)));
assertThat(isExecuted,is(expectedResult));
}
String expectedMessage = gitImplName.startsWith("jgit")
? "JGIT doesn't support git maintenance. Use CLIGIT to execute maintenance tasks."
: "Error executing invalid-maintenance-task maintenance task";
collector.checkThat(handler.getMessages(), hasItem(expectedMessage));
}
}

0 comments on commit 853f2df

Please sign in to comment.