@Test(timeout = 300000)
  public void testClusterPortAssignment() throws Exception {
    boolean retry = false;
    do {
      int masterPort = HBaseTestingUtil.randomFreePort();
      int masterInfoPort = HBaseTestingUtil.randomFreePort();
      int rsPort = HBaseTestingUtil.randomFreePort();
      int rsInfoPort = HBaseTestingUtil.randomFreePort();
      TEST_UTIL.getConfiguration().setBoolean(LocalHBaseCluster.ASSIGN_RANDOM_PORTS, false);
      TEST_UTIL.getConfiguration().setBoolean(HConstants.REGIONSERVER_INFO_PORT_AUTO, false);
      TEST_UTIL.getConfiguration().setBoolean("fs.hdfs.impl.disable.cache", true);
      TEST_UTIL.getConfiguration().setInt(HConstants.MASTER_PORT, masterPort);
      TEST_UTIL.getConfiguration().setInt(HConstants.MASTER_INFO_PORT, masterInfoPort);
      TEST_UTIL.getConfiguration().setInt(HConstants.REGIONSERVER_PORT, rsPort);
      TEST_UTIL.getConfiguration().setInt(HConstants.REGIONSERVER_INFO_PORT, rsInfoPort);
      LOG.info("Ports: {}, {}, {}, {}", masterPort, masterInfoPort, rsPort, rsInfoPort);
      try {
        SingleProcessHBaseCluster cluster = TEST_UTIL.startMiniCluster();
        assertTrue("Cluster failed to come up", cluster.waitForActiveAndReadyMaster(30000));
        retry = false;
        assertEquals("Master RPC port is incorrect", masterPort,
          cluster.getMaster().getRpcServer().getListenerAddress().getPort());
        assertEquals("Master info port is incorrect", masterInfoPort,
          cluster.getMaster().getInfoServer().getPort());
        assertEquals("RS RPC port is incorrect", rsPort,
          cluster.getRegionServer(0).getRpcServer().getListenerAddress().getPort());
        assertEquals("RS info port is incorrect", rsInfoPort,
          cluster.getRegionServer(0).getInfoServer().getPort());
      } catch (Exception e) {
        Throwable rootCause = ExceptionUtils.getRootCause(e);
        if (rootCause instanceof BindException) {
          LOG.info("Failed bind, need to retry", e);
          retry = true;
        } else {
          LOG.error("Failed to start mini cluster", e);
          retry = false;
          fail("Failed to start mini cluster with assigned ports.");
        }
      } finally {
        TEST_UTIL.shutdownMiniCluster();
      }
    } while (retry);
  }
Check that we can start an HBase cluster specifying a custom set of RPC and infoserver ports.
hbase-server/src/test/java/org/apache/hadoop/hbase/TestClusterPortAssignment.java