Skip to content
Permalink
Browse files

40: Fix failing tests after remote locking

Reviewed-by: ehelin
  • Loading branch information
Robin Westberg
Robin Westberg committed Jul 4, 2019
1 parent 0cfec57 commit 7c68b3b485a6e4483ee34ac2c92af48308a1c830
@@ -34,15 +34,17 @@

import java.io.*;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.*;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class BridgeBotTests {

private List<String> runHgCommand(Repository repository, String... params) throws IOException {
List<String> finalParams = new ArrayList<>();
finalParams.add("hg");
@@ -112,6 +114,14 @@ void setup() throws IOException {
try {
var localRepo = Repository.materialize(sourceFolder.path(), URIBuilder.base("http://hg.openjdk.java.net/code-tools/jtreg").build(), "default");
runHgCommand(localRepo, "strip", "-r", "b2511c725d81");

// Create a lockfile in the mercurial repo, as it will overwrite the existing lock in the remote git repo
runHgCommand(localRepo, "update", "null");
runHgCommand(localRepo, "branch", "testlock");
var lockFile = localRepo.root().resolve("lock.txt");
Files.writeString(lockFile, ZonedDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME), StandardCharsets.UTF_8);
localRepo.add(lockFile);
localRepo.commit("Lock", "duke", "Duke <duke@openjdk.java.net>");
} catch (IOException e) {
Assumptions.assumeTrue(false, "Failed to connect to hg.openjdk.java.net - skipping tests");
}
@@ -132,6 +142,7 @@ void bridgeTest(TestInfo testInfo) throws IOException {
var storageFolder2 = new TemporaryDirectory()) {
// Export a partial version of a hg repository
var localHgRepo = Repository.materialize(hgFolder.path(), source, "default");
localHgRepo.fetch(source, "testlock");
var destinationRepo = credentials.getHostedRepository();
var config = new TestExporterConfig(localHgRepo.root().toUri(), destinationRepo);
var bridge = new JBridgeBot(config, storageFolder.path());
@@ -256,8 +267,9 @@ void bridgeReuseMarks(TestInfo testInfo) throws IOException {
var localGitCommits = getCommitHashes(localGitRepo);
assertTrue(localGitCommits.contains("9cb6a5b843c0e9f6d45273a1a6f5c98979ab0766"));

// Push something else to overwrite it
// Push something else to overwrite it (but retain the lock)
var localRepo = CheckableRepository.init(gitFolder2.path(), destinationRepo.getRepositoryType());
credentials.commitLock(localRepo);
localRepo.pushAll(destinationRepo.getUrl());

// Materialize it again and ensure that the known commit is now gone
@@ -344,6 +356,7 @@ void filterUnreachable(TestInfo testInfo) throws IOException {
var storageFolder2 = new TemporaryDirectory()) {
// Export a hg repository with unreachable commits
var localHgRepo = Repository.materialize(hgFolder.path(), source, "default");
localHgRepo.fetch(source, "testlock");
var destinationRepo = credentials.getHostedRepository();
var config = new TestExporterConfig(localHgRepo.root().toUri(), destinationRepo);
var bridge = new JBridgeBot(config, storageFolder.path());
@@ -65,7 +65,7 @@ void testJsonUpdaterBranch(TestInfo testInfo) throws IOException {
var jsonFolder = new TemporaryDirectory()) {
var repo = credentials.getHostedRepository();
var localRepo = CheckableRepository.init(tempFolder.path(), repo.getRepositoryType());
localRepo.fetch(repo.getUrl(), "testlock:testlock");
credentials.commitLock(localRepo);
localRepo.pushAll(repo.getUrl());

var tagStorage = createTagStorage(repo);
@@ -99,7 +99,7 @@ void testJsonUpdaterTag(TestInfo testInfo) throws IOException {
var jsonFolder = new TemporaryDirectory()) {
var repo = credentials.getHostedRepository();
var localRepo = CheckableRepository.init(tempFolder.path(), repo.getRepositoryType());
localRepo.fetch(repo.getUrl(), "testlock:testlock");
credentials.commitLock(localRepo);
var masterHash = localRepo.resolve("master").orElseThrow();
localRepo.tag(masterHash, "jdk-12+1", "Added tag 1", "Duke", "duke@openjdk.java.net");
localRepo.pushAll(repo.getUrl());
@@ -197,7 +197,7 @@ private boolean getLock(HostedRepository repo) throws IOException {
}

if (Files.exists(lockFile)) {
var currentLock = Files.readString(lockFile, StandardCharsets.UTF_8);
var currentLock = Files.readString(lockFile, StandardCharsets.UTF_8).strip();
var lockTime = ZonedDateTime.parse(currentLock, DateTimeFormatter.ISO_DATE_TIME);
if (lockTime.isBefore(ZonedDateTime.now().minus(Duration.ofMinutes(10)))) {
log.info("Stale lock encountered - overwriting it");
@@ -208,9 +208,7 @@ private boolean getLock(HostedRepository repo) throws IOException {
}

// The lock either doesn't exist or is stale, try to grab it
Files.writeString(lockFile, ZonedDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME), StandardCharsets.UTF_8);
localRepo.add(lockFile);
var lockHash = localRepo.commit("Lock", "test", "test@test.test");
var lockHash = commitLock(localRepo);
localRepo.push(lockHash, repo.getUrl(), "testlock");
log.info("Obtained credentials lock");

@@ -231,6 +229,15 @@ private void releaseLock(HostedRepository repo) throws IOException {
}
}

public Hash commitLock(Repository localRepo) throws IOException {
var lockFile = localRepo.root().resolve("lock.txt");
Files.writeString(lockFile, ZonedDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME), StandardCharsets.UTF_8);
localRepo.add(lockFile);
var lockHash = localRepo.commit("Lock", "test", "test@test.test");
localRepo.branch(lockHash, "testlock");
return lockHash;
}

public HostedRepository getHostedRepository() {
var host = getHost();
var repo = credentials.getHostedRepository(host);

0 comments on commit 7c68b3b

Please sign in to comment.
You can’t perform that action at this time.