diff --git a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/ozoneimpl/ContainerScanError.java b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/ozoneimpl/ContainerScanError.java index a5dfe5bb8e21..e0d8da354267 100644 --- a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/ozoneimpl/ContainerScanError.java +++ b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/ozoneimpl/ContainerScanError.java @@ -31,6 +31,8 @@ public class ContainerScanError { /** * Represents the reason a container scan failed and a container should * be marked unhealthy. + * Note: When adding a new FailureType, ensure a corresponding test is added to + * TestContainerCorruptions, or explicitly exclude in getExcludedFailureTypes(). */ public enum FailureType { MISSING_CONTAINER_DIR, diff --git a/hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/keyvalue/TestContainerCorruptions.java b/hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/keyvalue/TestContainerCorruptions.java index 979e8c172797..7e7f7edc8f7c 100644 --- a/hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/keyvalue/TestContainerCorruptions.java +++ b/hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/keyvalue/TestContainerCorruptions.java @@ -185,4 +185,16 @@ public static File getBlock(Container container, long blockID) { assertTrue(blockFile.exists()); return blockFile; } + + /** + * Set of {@link ContainerScanError.FailureType} values that are excluded from testing. + * When adding a FailureType to this set, add a comment explaining why it's excluded. + */ + public static Set getExcludedFailureTypes() { + return EnumSet.of( + ContainerScanError.FailureType.WRITE_FAILURE, + ContainerScanError.FailureType.INACCESSIBLE_DB, + ContainerScanError.FailureType.INCONSISTENT_CHUNK_LENGTH + ); + } } diff --git a/hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/keyvalue/TestFailureTypeCoverage.java b/hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/keyvalue/TestFailureTypeCoverage.java new file mode 100644 index 000000000000..9ec60f09b9a1 --- /dev/null +++ b/hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/keyvalue/TestFailureTypeCoverage.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.ozone.container.keyvalue; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.EnumSet; +import java.util.Set; +import org.apache.hadoop.ozone.container.ozoneimpl.ContainerScanError; +import org.junit.jupiter.api.Test; + +/** + * Validates test coverage for all {@link ContainerScanError.FailureType} values. + */ +public class TestFailureTypeCoverage { + + @Test + public void testEveryFailureTypeIsTestedOrExcluded() { + Set testedFailureTypes = EnumSet.noneOf(ContainerScanError.FailureType.class); + + for (TestContainerCorruptions types : TestContainerCorruptions.values()) { + ContainerScanError.FailureType failureType = types.getExpectedResult(); + assertNotNull(failureType); + testedFailureTypes.add(failureType); + } + + Set allFailureTypes = EnumSet.allOf(ContainerScanError.FailureType.class); + Set failureTypesMissingTests = EnumSet.copyOf(allFailureTypes); + + //Remove types that are tested + failureTypesMissingTests.removeAll(testedFailureTypes); + //Remove explicitly excluded types + failureTypesMissingTests.removeAll(TestContainerCorruptions.getExcludedFailureTypes()); + + assertTrue(failureTypesMissingTests.isEmpty(), + "The following FailureType values do not have corresponding test corruptions in " + + "TestContainerCorruptions: " + failureTypesMissingTests + ". Either add test cases for these " + + "failure types or exclude them with an explanation (via getExcludedFailureTypes())."); + } +} +