mwesterby commented on code in PR #15733:
URL: https://github.com/apache/kafka/pull/15733#discussion_r1567699963


##########
core/src/test/scala/unit/kafka/tools/StorageToolTest.scala:
##########
@@ -192,6 +192,66 @@ Found problem:
     } finally Utils.delete(tempDir)
   }
 
+  private def runFormatCommand(stream: ByteArrayOutputStream, directories: 
Seq[String]): Int = {
+    val metaProperties = new MetaProperties.Builder().
+      setVersion(MetaPropertiesVersion.V1).
+      setClusterId("XcZZOzUqS4yHOjhMQB6JLQ").
+      setNodeId(2).
+      build()
+    val bootstrapMetadata = 
StorageTool.buildBootstrapMetadata(MetadataVersion.latestTesting(), None, "test 
format command")
+    StorageTool.formatCommand(new PrintStream(stream), directories, 
metaProperties, bootstrapMetadata, MetadataVersion.latestTesting(), 
ignoreFormatted = false)
+  }
+
+  @Test
+  def testFormatSucceedsIfAllDirectoriesAreAvailable(): Unit = {
+    val availableDir1 = TestUtils.tempDir()
+    val availableDir2 = TestUtils.tempDir()
+    try {
+      val stream = new ByteArrayOutputStream()
+      assertEquals(0, runFormatCommand(stream, Seq(availableDir1.toString, 
availableDir2.toString)))
+      assertTrue(stream.toString().contains("Formatting 
%s".format(availableDir1)))
+      assertTrue(stream.toString().contains("Formatting 
%s".format(availableDir2)))
+    } finally {
+      Utils.delete(availableDir1)
+      Utils.delete(availableDir2)
+    }
+  }
+
+  @Test
+  def testFormatSucceedsIfAtLeastOneDirectoryIsAvailable(): Unit = {
+    val availableDir1 = TestUtils.tempDir()
+    val unavailableDir1 = TestUtils.tempFile()
+    try {
+      val stream = new ByteArrayOutputStream()
+      assertEquals(0, runFormatCommand(stream, Seq(availableDir1.toString, 
unavailableDir1.toString)))
+      assertTrue(stream.toString().contains("I/O error trying to read log 
directory %s. Ignoring...".format(unavailableDir1)))
+      assertTrue(stream.toString().contains("Formatting 
%s".format(availableDir1)))
+      assertFalse(stream.toString().contains("Formatting 
%s".format(unavailableDir1)))
+    } finally {
+      Utils.delete(availableDir1)
+      Utils.delete(unavailableDir1)
+    }
+  }
+
+  @Test
+  def testFormatFailsIfAllDirectoriesAreUnavailable(): Unit = {
+    val unavailableDir1 = TestUtils.tempFile()
+    val unavailableDir2 = TestUtils.tempFile()
+    try {
+      val stream = new ByteArrayOutputStream()
+      try {
+        assertEquals(1, runFormatCommand(stream, Seq(unavailableDir1.toString, 
unavailableDir2.toString)))
+      } catch {
+        case e: TerseFailure => assertEquals("No available log directories to 
format.", e.getMessage)

Review Comment:
   Much cleaner, I'll refactor this assertion too.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to