Repository: apache/kafka
File: core/src/test/scala/unit/kafka/tools/StorageToolTest.scala (Scala)

Code before:
    properties.setProperty("log.dirs", availableDirs.mkString(","))
    val stream = new ByteArrayOutputStream()
    assertEquals(0, runFormatCommand(stream, properties, Seq(
      "--release-version", "3.6-IV0",
      "--feature", "kraft.version=0")))
    assertTrue(stream.toString().contains("3.6-IV0"),
      "Failed to find content in output: " + stream.toString())
  }

  @Test
  def testFormatWithStandaloneFlagOnBrokerFails(): Unit = {
    val availableDirs = Seq(TestUtils.tempDir())
    val properties = new Properties()
    properties.putAll(defaultStaticQuorumProperties)
    properties.remove("controller.quorum.voters")
    properties.put("controller.quorum.bootstrap.servers", "localhost:9093")
    properties.setProperty("log.dirs", availableDirs.mkString(","))
    val stream = new ByteArrayOutputStream()
    val arguments = ListBuffer[String]("--release-version", "3.9-IV0", "--standalone")
    assertEquals("You can only use --standalone on a controller.",
      assertThrows(classOf[TerseFailure],
        () => runFormatCommand(stream, properties, arguments.toSeq)).getMessage)
  }

  @Test
  def testFormatWithStandaloneAndInitialControllersFailsWithVotersConfig(): Unit = {
    val availableDirs = Seq(TestUtils.tempDir())
    val properties = new Properties()
    properties.putAll(defaultDynamicQuorumProperties)
    properties.setProperty(QuorumConfig.QUORUM_VOTERS_CONFIG, "0@localhost:8020")
    properties.setProperty("log.dirs", availableDirs.mkString(","))
    val stream = new ByteArrayOutputStream()
    val args1 = ListBuffer[String]("--release-version", "3.9-IV0", "--standalone")
    assertEquals("You cannot specify controller.quorum.voters and " +
      "format the node with --initial-controllers or --standalone. If you " +
      "want to use dynamic quorum, please remove controller.quorum.voters and " +
      "specify controller.quorum.bootstrap.servers instead.",
      assertThrows(classOf[TerseFailure],
        () => runFormatCommand(stream, properties, args1.toSeq)).getMessage
    )
    val args2 = ListBuffer[String](
      "--release-version", "3.9-IV0",
      "--initial-controllers",
      "0@localhost:8020:K90IZ-0DRNazJ49kCZ1EMQ,"
    )
    assertEquals("You cannot specify controller.quorum.voters and " +
      "format the node with --initial-controllers or --standalone. If you " +
      "want to use dynamic quorum, please remove controller.quorum.voters and " +
      "specify controller.quorum.bootstrap.servers instead.",
      assertThrows(classOf[TerseFailure],
        () => runFormatCommand(stream, properties, args2.toSeq)).getMessage

Review comment (bug):
nit: break into two tests?
`testFormatWithStandaloneFailsWithStaticVotersConfig`
`testFormatWithInitialControllersFailsWithStaticVotersConfig`

Code after:
    properties.setProperty("log.dirs", availableDirs.mkString(","))
    val stream = new ByteArrayOutputStream()
    assertEquals(0, runFormatCommand(stream, properties, Seq(
      "--release-version", "3.6-IV0",
      "--feature", "kraft.version=0")))
    assertTrue(stream.toString().contains("3.6-IV0"),
      "Failed to find content in output: " + stream.toString())
  }

  @Test
  def testFormatWithStandaloneFlagOnBrokerFails(): Unit = {
    val availableDirs = Seq(TestUtils.tempDir())
    val properties = new Properties()
    properties.setProperty("process.roles", "broker")
    properties.setProperty("node.id", "0")
    properties.setProperty("controller.listener.names", "CONTROLLER")
    properties.setProperty("controller.quorum.bootstrap.servers", "localhost:9093")
    properties.setProperty("log.dirs", availableDirs.mkString(","))
    val stream = new ByteArrayOutputStream()
    val arguments = ListBuffer[String]("--release-version", "3.9-IV0", "--standalone")
    assertEquals("You can only use --standalone on a controller.",
      assertThrows(classOf[TerseFailure],
        () => runFormatCommand(stream, properties, arguments.toSeq)).getMessage)
  }

  @Test
  def testFormatWithStandaloneFailsWithStaticVotersConfig(): Unit = {
    val availableDirs = Seq(TestUtils.tempDir())
    val properties = new Properties()
    properties.putAll(defaultDynamicQuorumProperties)
    properties.setProperty(QuorumConfig.QUORUM_VOTERS_CONFIG, "0@localhost:8020")
    properties.setProperty("log.dirs", availableDirs.mkString(","))
    val stream = new ByteArrayOutputStream()
    val arguments = ListBuffer[String]("--release-version", "3.9-IV0", "--standalone")
    assertEquals("You cannot specify controller.quorum.voters and " +
      "format the node with --initial-controllers or --standalone. If you " +
      "want to use dynamic quorum, please remove controller.quorum.voters and " +
      "specify controller.quorum.bootstrap.servers instead.",
      assertThrows(classOf[TerseFailure],
        () => runFormatCommand(stream, properties, arguments.toSeq)).getMessage
    )
  }

  @Test
  def testFormatWithInitialControllersFailsWithStaticVotersConfig(): Unit = {
    val availableDirs = Seq(TestUtils.tempDir())
    val properties = new Properties()
    properties.putAll(defaultDynamicQuorumProperties)
    properties.setProperty(QuorumConfig.QUORUM_VOTERS_CONFIG, "0@localhost:8020")
    properties.setProperty("log.dirs", availableDirs.mkString(","))
    val stream = new ByteArrayOutputStream()