mirror of https://github.com/apache/kafka.git
KAFKA-18128 Fix failed test MetadataSchemaCheckerToolTest.testVerifyEvolutionGit in PR (#17996)
Reviewers: Chia-Ping Tsai <chia7712@gmail.com>
This commit is contained in:
parent
45c094ed23
commit
c3d22180d4
|
@ -35,6 +35,7 @@ import org.eclipse.jgit.treewalk.filter.PathFilter;
|
|||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
@ -122,13 +123,18 @@ class CheckerUtils {
|
|||
/**
|
||||
* Read a MessageSpec file from remote git repo.
|
||||
*
|
||||
* @param filePath The file to read from remote git repo.
|
||||
* @return The file contents.
|
||||
* @param filePath The file to read from remote git repo.
|
||||
* @param ref The specific git reference to be used for testing.
|
||||
* @return The file contents.
|
||||
*/
|
||||
static String GetDataFromGit(String filePath, Path gitPath) throws IOException {
|
||||
static String getDataFromGit(String filePath, Path gitPath, String ref) throws IOException {
|
||||
Git git = Git.open(new File(gitPath + "/.git"));
|
||||
Repository repository = git.getRepository();
|
||||
Ref head = git.getRepository().getRefDatabase().firstExactRef("refs/heads/trunk");
|
||||
Ref head = repository.getRefDatabase().findRef(ref);
|
||||
if (head == null) {
|
||||
throw new IllegalStateException("Cannot find " + ref + " in the repository.");
|
||||
}
|
||||
|
||||
try (RevWalk revWalk = new RevWalk(repository)) {
|
||||
RevCommit commit = revWalk.parseCommit(head.getObjectId());
|
||||
RevTree tree = commit.getTree();
|
||||
|
@ -141,7 +147,7 @@ class CheckerUtils {
|
|||
}
|
||||
ObjectId objectId = treeWalk.getObjectId(0);
|
||||
ObjectLoader loader = repository.open(objectId);
|
||||
return new String(loader.getBytes(), "UTF-8");
|
||||
return new String(loader.getBytes(), StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,10 +25,11 @@ import net.sourceforge.argparse4j.inf.Subparsers;
|
|||
import net.sourceforge.argparse4j.internal.HelpScreenException;
|
||||
|
||||
import java.io.PrintStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import static org.apache.kafka.message.checker.CheckerUtils.GetDataFromGit;
|
||||
import static org.apache.kafka.message.checker.CheckerUtils.getDataFromGit;
|
||||
|
||||
public class MetadataSchemaCheckerTool {
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
@ -65,6 +66,10 @@ public class MetadataSchemaCheckerTool {
|
|||
evolutionGitVerifierParser.addArgument("--file", "-3").
|
||||
required(true).
|
||||
help("The edited JSON file");
|
||||
evolutionGitVerifierParser.addArgument("--ref", "-4")
|
||||
.required(false)
|
||||
.setDefault("refs/heads/trunk")
|
||||
.help("Optional Git reference to be used for testing. Defaults to 'refs/heads/trunk' if not specified.");
|
||||
Namespace namespace;
|
||||
if (args.length == 0) {
|
||||
namespace = argumentParser.parseArgs(new String[] {"--help"});
|
||||
|
@ -93,13 +98,13 @@ public class MetadataSchemaCheckerTool {
|
|||
case "verify-evolution-git": {
|
||||
String filePath = "/metadata/src/main/resources/common/metadata/" + namespace.getString("file");
|
||||
Path rootKafkaDirectory = Paths.get("").toAbsolutePath();
|
||||
while (!rootKafkaDirectory.endsWith("kafka")) {
|
||||
while (!Files.exists(rootKafkaDirectory.resolve(".git"))) {
|
||||
rootKafkaDirectory = rootKafkaDirectory.getParent();
|
||||
if (rootKafkaDirectory == null) {
|
||||
throw new RuntimeException("Invalid directory, need to be within the kafka directory");
|
||||
throw new RuntimeException("Invalid directory, need to be within a Git repository");
|
||||
}
|
||||
}
|
||||
String gitContent = GetDataFromGit(filePath, rootKafkaDirectory);
|
||||
String gitContent = getDataFromGit(filePath, rootKafkaDirectory, namespace.getString("ref"));
|
||||
EvolutionVerifier verifier = new EvolutionVerifier(
|
||||
CheckerUtils.readMessageSpecFromFile(rootKafkaDirectory + filePath),
|
||||
CheckerUtils.readMessageSpecFromString(gitContent));
|
||||
|
|
|
@ -29,7 +29,12 @@ public class MetadataSchemaCheckerToolTest {
|
|||
@Test
|
||||
public void testVerifyEvolutionGit() throws Exception {
|
||||
try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
|
||||
MetadataSchemaCheckerTool.run(new String[]{"verify-evolution-git", "--file", "AbortTransactionRecord.json"}, new PrintStream(stream));
|
||||
MetadataSchemaCheckerTool.run(
|
||||
// In the CI environment because the CI fetch command only creates HEAD and refs/remotes/pull/... references.
|
||||
// Since there may not be other branches like refs/heads/trunk in CI, HEAD serves as the baseline reference.
|
||||
new String[]{"verify-evolution-git", "--file", "AbortTransactionRecord.json", "--ref", "HEAD"},
|
||||
new PrintStream(stream)
|
||||
);
|
||||
assertEquals("Successfully verified evolution of file: AbortTransactionRecord.json",
|
||||
stream.toString().trim());
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue