@Test
  @SuppressWarnings("unchecked")
  public void testInitCredentialsForCluster4() throws Exception {
    HBaseTestingUtil util1 = new HBaseTestingUtil();
    // Assume util1 is insecure cluster
    // Do not start util1 because cannot boot secured mini cluster and insecure mini cluster at once

    HBaseTestingUtil util2 = new HBaseTestingUtil();
    File keytab = new File(util2.getDataTestDir("keytab").toUri().getPath());
    MiniKdc kdc = util2.setupMiniKdc(keytab);
    try {
      String username = UserGroupInformation.getLoginUser().getShortUserName();
      String userPrincipal = username + "/localhost";
      kdc.createPrincipal(keytab, userPrincipal, HTTP_PRINCIPAL);
      loginUserFromKeytab(userPrincipal + '@' + kdc.getRealm(), keytab.getAbsolutePath());

      try (Closeable ignored2 = startSecureMiniCluster(util2, kdc, userPrincipal)) {
        Configuration conf1 = util1.getConfiguration();
        Job job = Job.getInstance(conf1);

        TableMapReduceUtil.initCredentialsForCluster(job, util2.getConfiguration());

        Credentials credentials = job.getCredentials();
        Collection<Token<? extends TokenIdentifier>> tokens = credentials.getAllTokens();
        assertEquals(1, tokens.size());

        String clusterId = ZKClusterId.readClusterIdZNode(util2.getZooKeeperWatcher());
        Token<AuthenticationTokenIdentifier> tokenForCluster =
          (Token<AuthenticationTokenIdentifier>) credentials.getToken(new Text(clusterId));
        assertEquals(userPrincipal + '@' + kdc.getRealm(),
          tokenForCluster.decodeIdentifier().getUsername());
      }
    } finally {
      kdc.stop();
    }
  }
Test different variants of initTableMapperJob method
hbase-mapreduce/src/test/java/org/apache/hadoop/hbase/mapreduce/TestTableMapReduceUtil.java