This is an automated email from the ASF dual-hosted git repository.

manikumar pushed a commit to branch 3.7
in repository https://gitbox.apache.org/repos/asf/kafka.git


The following commit(s) were added to refs/heads/3.7 by this push:
     new 3de23774123 KAFKA-16473: Use correct cluster ID when formatting log 
dir. (#15658)
3de23774123 is described below

commit 3de23774123bef3e49d133866d091ca23756de48
Author: Sebastian Marsching <smarsch...@users.noreply.github.com>
AuthorDate: Fri Apr 12 08:00:27 2024 +0200

    KAFKA-16473: Use correct cluster ID when formatting log dir. (#15658)
    
    This fixes an issue that when starting a Docker container for the first 
time, the cluster ID used when formatting the log dir would not be $CLUSTER_ID 
but Some($CLUSTER_ID) (KAFKA-16473).
    
    In order to be able to test the formatStorageCmd method which contained the 
bug, the method has been made package private.
    
    Reviewers: cooper.ts...@suse.com, Vedarth Sharma 
<142404391+vedarthconflu...@users.noreply.github.com>, Manikumar Reddy 
<manikumar.re...@gmail.com>
---
 .../main/scala/kafka/docker/KafkaDockerWrapper.scala  |  8 ++++++--
 .../unit/kafka/docker/KafkaDockerWrapperTest.scala    | 19 +++++++++++++++++--
 2 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/core/src/main/scala/kafka/docker/KafkaDockerWrapper.scala 
b/core/src/main/scala/kafka/docker/KafkaDockerWrapper.scala
index 248fc72b0b9..04067e8d560 100644
--- a/core/src/main/scala/kafka/docker/KafkaDockerWrapper.scala
+++ b/core/src/main/scala/kafka/docker/KafkaDockerWrapper.scala
@@ -87,8 +87,12 @@ object KafkaDockerWrapper {
     parser.parseArgsOrFail(args)
   }
 
-  private def formatStorageCmd(configsPath: Path, env: Map[String, String]): 
Array[String] = {
-    Array("format", "--cluster-id=" + env.get("CLUSTER_ID"), "-c", 
s"${configsPath.toString}/server.properties")
+  private[docker] def formatStorageCmd(configsPath: Path, env: Map[String, 
String]): Array[String] = {
+    val clusterId = env.get("CLUSTER_ID") match {
+      case Some(str) => str
+      case None => throw new RuntimeException("CLUSTER_ID environment variable 
is not set.")
+    }
+    Array("format", "--cluster-id=" + clusterId, "-c", 
s"${configsPath.toString}/server.properties")
   }
 
   private def prepareConfigs(defaultConfigsPath: Path, mountedConfigsPath: 
Path, finalConfigsPath: Path): Unit = {
diff --git a/core/src/test/scala/unit/kafka/docker/KafkaDockerWrapperTest.scala 
b/core/src/test/scala/unit/kafka/docker/KafkaDockerWrapperTest.scala
index 0da481a4b2d..409c5dccdbb 100644
--- a/core/src/test/scala/unit/kafka/docker/KafkaDockerWrapperTest.scala
+++ b/core/src/test/scala/unit/kafka/docker/KafkaDockerWrapperTest.scala
@@ -16,11 +16,11 @@
  */
 package kafka.docker
 
-import org.junit.jupiter.api.Assertions.assertEquals
+import org.junit.jupiter.api.Assertions.{assertArrayEquals, assertEquals, 
assertThrows}
 import org.junit.jupiter.api.Test
 
 import java.nio.charset.StandardCharsets
-import java.nio.file.{Files, Path}
+import java.nio.file.{Files, Path, Paths}
 
 class KafkaDockerWrapperTest {
   @Test
@@ -127,6 +127,21 @@ class KafkaDockerWrapperTest {
     assertEquals(expected, actual)
   }
 
+  @Test
+  def testFormatStorageCmd(): Unit = {
+    val configsPath = Paths.get("/path/to/configs")
+    val envVars = Map("CLUSTER_ID" -> "MYwKGPhXQZidgd0qMv8Mkw")
+
+    val expected = Array("format", "--cluster-id=MYwKGPhXQZidgd0qMv8Mkw", 
"-c", "/path/to/configs/server.properties")
+    val actual = KafkaDockerWrapper.formatStorageCmd(configsPath, envVars)
+
+    assertArrayEquals(expected.toArray[Object], actual.toArray[Object])
+
+    assertThrows(classOf[RuntimeException], () => {
+      KafkaDockerWrapper.formatStorageCmd(configsPath, Map())
+    })
+  }
+
   @Test
   def testGetLog4jConfigsFromEnv(): Unit = {
     val envVars = Map(

Reply via email to