Skip to content

Commit

Permalink
MINOR: more KRaft Metadata Image tests (apache#13724)
Browse files Browse the repository at this point in the history
Adds additional testing for the various KRaft *Image classes. For every image that we create we already test that we can get there by applying all the records corresponding to that image written out as a list of records. This patch adds more tests to confirm that we can get to each such final image with intermediate stops at all possible intermediate images along the way.

Reviewers: Colin P. McCabe <[email protected]>, David Arthur <[email protected]>
  • Loading branch information
rondagostino authored Jul 10, 2023
1 parent 726d277 commit edd64fa
Show file tree
Hide file tree
Showing 10 changed files with 383 additions and 111 deletions.
42 changes: 30 additions & 12 deletions metadata/src/test/java/org/apache/kafka/image/AclsImageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static org.apache.kafka.metadata.authorizer.StandardAclWithIdTest.TEST_ACLS;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -73,31 +74,48 @@ public class AclsImageTest {
}

@Test
public void testEmptyImageRoundTrip() throws Throwable {
testToImageAndBack(AclsImage.EMPTY);
public void testEmptyImageRoundTrip() {
testToImage(AclsImage.EMPTY);
}

@Test
public void testImage1RoundTrip() throws Throwable {
testToImageAndBack(IMAGE1);
public void testImage1RoundTrip() {
testToImage(IMAGE1);
}

@Test
public void testApplyDelta1() throws Throwable {
public void testApplyDelta1() {
assertEquals(IMAGE2, DELTA1.apply());
// check image1 + delta1 = image2, since records for image1 + delta1 might differ from records from image2
List<ApiMessageAndVersion> records = getImageRecords(IMAGE1);
records.addAll(DELTA1_RECORDS);
testToImage(IMAGE2, records);
}

@Test
public void testImage2RoundTrip() throws Throwable {
testToImageAndBack(IMAGE2);
public void testImage2RoundTrip() {
testToImage(IMAGE2);
}

private void testToImageAndBack(AclsImage image) throws Throwable {
private static void testToImage(AclsImage image) {
testToImage(image, Optional.empty());
}

private static void testToImage(AclsImage image, Optional<List<ApiMessageAndVersion>> fromRecords) {
testToImage(image, fromRecords.orElseGet(() -> getImageRecords(image)));
}

private static void testToImage(AclsImage image, List<ApiMessageAndVersion> fromRecords) {
// test from empty image stopping each of the various intermediate images along the way
new RecordTestUtils.TestThroughAllIntermediateImagesLeadingToFinalImageHelper<>(
() -> AclsImage.EMPTY,
AclsDelta::new
).test(image, fromRecords);
}

private static List<ApiMessageAndVersion> getImageRecords(AclsImage image) {
RecordListWriter writer = new RecordListWriter();
image.write(writer, new ImageWriterOptions.Builder().build());
AclsDelta delta = new AclsDelta(AclsImage.EMPTY);
RecordTestUtils.replayAll(delta, writer.records());
AclsImage nextImage = delta.apply();
assertEquals(image, nextImage);
return writer.records();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static org.apache.kafka.common.metadata.MetadataRecordType.CLIENT_QUOTA_RECORD;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -89,31 +90,48 @@ public class ClientQuotasImageTest {
}

@Test
public void testEmptyImageRoundTrip() throws Throwable {
testToImageAndBack(ClientQuotasImage.EMPTY);
public void testEmptyImageRoundTrip() {
testToImage(ClientQuotasImage.EMPTY);
}

@Test
public void testImage1RoundTrip() throws Throwable {
testToImageAndBack(IMAGE1);
public void testImage1RoundTrip() {
testToImage(IMAGE1);
}

@Test
public void testApplyDelta1() throws Throwable {
public void testApplyDelta1() {
assertEquals(IMAGE2, DELTA1.apply());
// check image1 + delta1 = image2, since records for image1 + delta1 might differ from records from image2
List<ApiMessageAndVersion> records = getImageRecords(IMAGE1);
records.addAll(DELTA1_RECORDS);
testToImage(IMAGE2, records);
}

@Test
public void testImage2RoundTrip() throws Throwable {
testToImageAndBack(IMAGE2);
public void testImage2RoundTrip() {
testToImage(IMAGE2);
}

private void testToImageAndBack(ClientQuotasImage image) throws Throwable {
private static void testToImage(ClientQuotasImage image) {
testToImage(image, Optional.empty());
}

private static void testToImage(ClientQuotasImage image, Optional<List<ApiMessageAndVersion>> fromRecords) {
testToImage(image, fromRecords.orElseGet(() -> getImageRecords(image)));
}

private static void testToImage(ClientQuotasImage image, List<ApiMessageAndVersion> fromRecords) {
// test from empty image stopping each of the various intermediate images along the way
new RecordTestUtils.TestThroughAllIntermediateImagesLeadingToFinalImageHelper<>(
() -> ClientQuotasImage.EMPTY,
ClientQuotasDelta::new
).test(image, fromRecords);
}

private static List<ApiMessageAndVersion> getImageRecords(ClientQuotasImage image) {
RecordListWriter writer = new RecordListWriter();
image.write(writer, new ImageWriterOptions.Builder().build());
ClientQuotasDelta delta = new ClientQuotasDelta(ClientQuotasImage.EMPTY);
RecordTestUtils.replayAll(delta, writer.records());
ClientQuotasImage nextImage = delta.apply();
assertEquals(image, nextImage);
return writer.records();
}
}
41 changes: 29 additions & 12 deletions metadata/src/test/java/org/apache/kafka/image/ClusterImageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,31 +127,48 @@ public class ClusterImageTest {
}

@Test
public void testEmptyImageRoundTrip() throws Throwable {
testToImageAndBack(ClusterImage.EMPTY);
public void testEmptyImageRoundTrip() {
testToImage(ClusterImage.EMPTY);
}

@Test
public void testImage1RoundTrip() throws Throwable {
testToImageAndBack(IMAGE1);
public void testImage1RoundTrip() {
testToImage(IMAGE1);
}

@Test
public void testApplyDelta1() throws Throwable {
public void testApplyDelta1() {
assertEquals(IMAGE2, DELTA1.apply());
// check image1 + delta1 = image2, since records for image1 + delta1 might differ from records from image2
List<ApiMessageAndVersion> records = getImageRecords(IMAGE1);
records.addAll(DELTA1_RECORDS);
testToImage(IMAGE2, records);
}

@Test
public void testImage2RoundTrip() throws Throwable {
testToImageAndBack(IMAGE2);
public void testImage2RoundTrip() {
testToImage(IMAGE2);
}

private void testToImageAndBack(ClusterImage image) throws Throwable {
private static void testToImage(ClusterImage image) {
testToImage(image, Optional.empty());
}

private static void testToImage(ClusterImage image, Optional<List<ApiMessageAndVersion>> fromRecords) {
testToImage(image, fromRecords.orElseGet(() -> getImageRecords(image)));
}

private static void testToImage(ClusterImage image, List<ApiMessageAndVersion> fromRecords) {
// test from empty image stopping each of the various intermediate images along the way
new RecordTestUtils.TestThroughAllIntermediateImagesLeadingToFinalImageHelper<>(
() -> ClusterImage.EMPTY,
ClusterDelta::new
).test(image, fromRecords);
}

private static List<ApiMessageAndVersion> getImageRecords(ClusterImage image) {
RecordListWriter writer = new RecordListWriter();
image.write(writer, new ImageWriterOptions.Builder().build());
ClusterDelta delta = new ClusterDelta(ClusterImage.EMPTY);
RecordTestUtils.replayAll(delta, writer.records());
ClusterImage nextImage = delta.apply();
assertEquals(image, nextImage);
return writer.records();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static org.apache.kafka.common.config.ConfigResource.Type.BROKER;
import static org.apache.kafka.common.metadata.MetadataRecordType.CONFIG_RECORD;
Expand Down Expand Up @@ -84,31 +85,48 @@ public class ConfigurationsImageTest {
}

@Test
public void testEmptyImageRoundTrip() throws Throwable {
testToImageAndBack(ConfigurationsImage.EMPTY);
public void testEmptyImageRoundTrip() {
testToImage(ConfigurationsImage.EMPTY);
}

@Test
public void testImage1RoundTrip() throws Throwable {
testToImageAndBack(IMAGE1);
public void testImage1RoundTrip() {
testToImage(IMAGE1);
}

@Test
public void testApplyDelta1() throws Throwable {
public void testApplyDelta1() {
assertEquals(IMAGE2, DELTA1.apply());
// check image1 + delta1 = image2, since records for image1 + delta1 might differ from records from image2
List<ApiMessageAndVersion> records = getImageRecords(IMAGE1);
records.addAll(DELTA1_RECORDS);
testToImage(IMAGE2, records);
}

@Test
public void testImage2RoundTrip() throws Throwable {
testToImageAndBack(IMAGE2);
public void testImage2RoundTrip() {
testToImage(IMAGE2);
}

private void testToImageAndBack(ConfigurationsImage image) throws Throwable {
private static void testToImage(ConfigurationsImage image) {
testToImage(image, Optional.empty());
}

private static void testToImage(ConfigurationsImage image, Optional<List<ApiMessageAndVersion>> fromRecords) {
testToImage(image, fromRecords.orElseGet(() -> getImageRecords(image)));
}

private static void testToImage(ConfigurationsImage image, List<ApiMessageAndVersion> fromRecords) {
// test from empty image stopping each of the various intermediate images along the way
new RecordTestUtils.TestThroughAllIntermediateImagesLeadingToFinalImageHelper<>(
() -> ConfigurationsImage.EMPTY,
ConfigurationsDelta::new
).test(image, fromRecords);
}

private static List<ApiMessageAndVersion> getImageRecords(ConfigurationsImage image) {
RecordListWriter writer = new RecordListWriter();
image.write(writer, new ImageWriterOptions.Builder().build());
ConfigurationsDelta delta = new ConfigurationsDelta(ConfigurationsImage.EMPTY);
RecordTestUtils.replayAll(delta, writer.records());
ConfigurationsImage nextImage = delta.apply();
assertEquals(image, nextImage);
return writer.records();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand Down Expand Up @@ -72,32 +73,49 @@ public class FeaturesImageTest {
}

@Test
public void testEmptyImageRoundTrip() throws Throwable {
testToImageAndBack(FeaturesImage.EMPTY);
public void testEmptyImageRoundTrip() {
testToImage(FeaturesImage.EMPTY);
}

@Test
public void testImage1RoundTrip() throws Throwable {
testToImageAndBack(IMAGE1);
public void testImage1RoundTrip() {
testToImage(IMAGE1);
}

@Test
public void testApplyDelta1() throws Throwable {
public void testApplyDelta1() {
assertEquals(IMAGE2, DELTA1.apply());
// check image1 + delta1 = image2, since records for image1 + delta1 might differ from records from image2
List<ApiMessageAndVersion> records = getImageRecords(IMAGE1);
records.addAll(DELTA1_RECORDS);
testToImage(IMAGE2, records);
}

@Test
public void testImage2RoundTrip() throws Throwable {
testToImageAndBack(IMAGE2);
public void testImage2RoundTrip() {
testToImage(IMAGE2);
}

private void testToImageAndBack(FeaturesImage image) throws Throwable {
private static void testToImage(FeaturesImage image) {
testToImage(image, Optional.empty());
}

private static void testToImage(FeaturesImage image, Optional<List<ApiMessageAndVersion>> fromRecords) {
testToImage(image, fromRecords.orElseGet(() -> getImageRecords(image)));
}

private static void testToImage(FeaturesImage image, List<ApiMessageAndVersion> fromRecords) {
// test from empty image stopping each of the various intermediate images along the way
new RecordTestUtils.TestThroughAllIntermediateImagesLeadingToFinalImageHelper<>(
() -> FeaturesImage.EMPTY,
FeaturesDelta::new
).test(image, fromRecords);
}

private static List<ApiMessageAndVersion> getImageRecords(FeaturesImage image) {
RecordListWriter writer = new RecordListWriter();
image.write(writer, new ImageWriterOptions.Builder().setMetadataVersion(image.metadataVersion()).build());
FeaturesDelta delta = new FeaturesDelta(FeaturesImage.EMPTY);
RecordTestUtils.replayAll(delta, writer.records());
FeaturesImage nextImage = delta.apply();
assertEquals(image, nextImage);
return writer.records();
}

@Test
Expand Down
Loading

0 comments on commit edd64fa

Please sign in to comment.