| 
27 | 27 | import org.apache.curator.framework.CuratorFramework;  | 
28 | 28 | import org.apache.curator.framework.CuratorFrameworkFactory;  | 
29 | 29 | import org.apache.curator.framework.api.ACLProvider;  | 
 | 30 | +import org.apache.curator.framework.api.CreateBuilder;  | 
 | 31 | +import org.apache.curator.framework.api.ProtectACLCreateModeStatPathAndBytesable;  | 
30 | 32 | import org.apache.curator.retry.ExponentialBackoffRetry;  | 
31 | 33 | import org.apache.curator.test.TestingServer;  | 
32 | 34 | import org.apache.hadoop.conf.Configuration;  | 
 | 
37 | 39 | import org.apache.hadoop.security.token.delegation.web.DelegationTokenIdentifier;  | 
38 | 40 | import org.apache.hadoop.security.token.delegation.web.DelegationTokenManager;  | 
39 | 41 | import org.apache.hadoop.test.GenericTestUtils;  | 
 | 42 | +import org.apache.hadoop.test.LambdaTestUtils;  | 
 | 43 | +import org.apache.zookeeper.KeeperException;  | 
40 | 44 | import org.apache.zookeeper.ZooDefs;  | 
41 | 45 | import org.apache.zookeeper.data.ACL;  | 
42 | 46 | import org.apache.zookeeper.data.Id;  | 
 | 47 | +import org.apache.zookeeper.data.Stat;  | 
43 | 48 | import org.apache.zookeeper.server.auth.DigestAuthenticationProvider;  | 
44 | 49 | import org.junit.After;  | 
45 | 50 | import org.junit.Assert;  | 
@@ -506,4 +511,65 @@ public Boolean get() {  | 
506 | 511 |       }  | 
507 | 512 |     }, 1000, 5000);  | 
508 | 513 |   }  | 
 | 514 | + | 
 | 515 | +  @Test  | 
 | 516 | +  public void testCreatingParentContainersIfNeeded() throws Exception {  | 
 | 517 | + | 
 | 518 | +    String connectString = zkServer.getConnectString();  | 
 | 519 | +    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);  | 
 | 520 | +    Configuration conf = getSecretConf(connectString);  | 
 | 521 | +    CuratorFramework curatorFramework =  | 
 | 522 | +        CuratorFrameworkFactory.builder()  | 
 | 523 | +        .connectString(connectString)  | 
 | 524 | +        .retryPolicy(retryPolicy)  | 
 | 525 | +        .build();  | 
 | 526 | +    curatorFramework.start();  | 
 | 527 | +    ZKDelegationTokenSecretManager.setCurator(curatorFramework);  | 
 | 528 | +    DelegationTokenManager tm1 = new DelegationTokenManager(conf, new Text("foo"));  | 
 | 529 | + | 
 | 530 | +    // When the init method is called,  | 
 | 531 | +    // the ZKDelegationTokenSecretManager#startThread method will be called,  | 
 | 532 | +    // and the creatingParentContainersIfNeeded will be called to create the nameSpace.  | 
 | 533 | +    tm1.init();  | 
 | 534 | + | 
 | 535 | +    String workingPath = "/" + conf.get(ZKDelegationTokenSecretManager.ZK_DTSM_ZNODE_WORKING_PATH,  | 
 | 536 | +        ZKDelegationTokenSecretManager.ZK_DTSM_ZNODE_WORKING_PATH_DEAFULT) + "/ZKDTSMRoot";  | 
 | 537 | + | 
 | 538 | +    // Check if the created NameSpace exists.  | 
 | 539 | +    Stat stat = curatorFramework.checkExists().forPath(workingPath);  | 
 | 540 | +    Assert.assertNotNull(stat);  | 
 | 541 | + | 
 | 542 | +    tm1.destroy();  | 
 | 543 | +    curatorFramework.close();  | 
 | 544 | +  }  | 
 | 545 | + | 
 | 546 | +  @Test  | 
 | 547 | +  public void testCreateNameSpaceRepeatedly() throws Exception {  | 
 | 548 | + | 
 | 549 | +    String connectString = zkServer.getConnectString();  | 
 | 550 | +    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);  | 
 | 551 | +    Configuration conf = getSecretConf(connectString);  | 
 | 552 | +    CuratorFramework curatorFramework =  | 
 | 553 | +        CuratorFrameworkFactory.builder().  | 
 | 554 | +        connectString(connectString).  | 
 | 555 | +        retryPolicy(retryPolicy).  | 
 | 556 | +        build();  | 
 | 557 | +    curatorFramework.start();  | 
 | 558 | + | 
 | 559 | +    String workingPath = "/" + conf.get(ZKDelegationTokenSecretManager.ZK_DTSM_ZNODE_WORKING_PATH,  | 
 | 560 | +        ZKDelegationTokenSecretManager.ZK_DTSM_ZNODE_WORKING_PATH_DEAFULT) + "/ZKDTSMRoot-Test";  | 
 | 561 | +    CreateBuilder createBuilder = curatorFramework.create();  | 
 | 562 | +    ProtectACLCreateModeStatPathAndBytesable<String> createModeStat =  | 
 | 563 | +        createBuilder.creatingParentContainersIfNeeded();  | 
 | 564 | +    createModeStat.forPath(workingPath);  | 
 | 565 | + | 
 | 566 | +    // Check if the created NameSpace exists.  | 
 | 567 | +    Stat stat = curatorFramework.checkExists().forPath(workingPath);  | 
 | 568 | +    Assert.assertNotNull(stat);  | 
 | 569 | + | 
 | 570 | +    // Repeated creation will throw NodeExists exception  | 
 | 571 | +    LambdaTestUtils.intercept(KeeperException.class,  | 
 | 572 | +        "KeeperErrorCode = NodeExists for "+workingPath,  | 
 | 573 | +        () -> createModeStat.forPath(workingPath));  | 
 | 574 | +  }  | 
509 | 575 | }  | 
0 commit comments