KAFKA-18128 Fix failed test MetadataSchemaCheckerToolTest.testVerifyEvolutionGit in PR (#17996)

Reviewers: Chia-Ping Tsai <chia7712@gmail.com>
This commit is contained in:
TengYao Chi 2024-12-02 16:19:18 +08:00 committed by GitHub
parent 45c094ed23
commit c3d22180d4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 26 additions and 10 deletions

View File

@ -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;
@ -123,12 +124,17 @@ class CheckerUtils {
* Read a MessageSpec file from remote git repo.
*
* @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);
}
}
}

View File

@ -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));

View File

@ -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());
}