Skip to content

Commit

Permalink
CURATOR-714: Assert that namespace paths are created using CuratorFra…
Browse files Browse the repository at this point in the history
…mework's ACLProvider (#505)

CURATOR-221 reported that namespace paths are created with no ACL.
Though, I belive that it should be fixed by CURATOR-222. It should still
be a good to write tests to assert that.
  • Loading branch information
kezhuw authored Sep 11, 2024
1 parent eeee4c6 commit 782696a
Showing 1 changed file with 131 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,75 @@ public void processResult(CuratorFramework client, CuratorEvent event) throws Ex
}
}

@Test
public void testCreateWithParentsWithAclApplyToParentsInNamespace() throws Exception {
CuratorFramework client = createClient(new DefaultACLProvider());
try {
client.start();

// given: a namespace
CuratorFramework bar = client.usingNamespace("bar");

// when: create a path with custom ACL and applyToParents option open in that namespace
List<ACL> acl =
Collections.singletonList(new ACL(ZooDefs.Perms.CREATE | ZooDefs.Perms.READ, ANYONE_ID_UNSAFE));
bar.create().creatingParentsIfNeeded().withACL(acl, true).forPath("/foo/boo");

// then: the created path has custom ACL
List<ACL> actual_bar_foo_boo = client.getACL().forPath("/bar/foo/boo");
assertEquals(actual_bar_foo_boo, acl);

// then: the parent path has custom ACL
List<ACL> actual_bar_foo = client.getACL().forPath("/bar/foo");
assertEquals(actual_bar_foo, acl);

// then: but the namespace path inherits ACL from CuratorFramework
List<ACL> actual_bar = client.getACL().forPath("/bar");
assertEquals(actual_bar, ZooDefs.Ids.OPEN_ACL_UNSAFE);
} finally {
CloseableUtils.closeQuietly(client);
}
}

@Test
public void testCreateWithParentsWithAclApplyToParentsInNamespaceBackground() throws Exception {
CuratorFramework client = createClient(new DefaultACLProvider());
try {
client.start();

final CountDownLatch latch = new CountDownLatch(1);
BackgroundCallback callback = (client1, event) -> latch.countDown();

List<ACL> acl =
Collections.singletonList(new ACL(ZooDefs.Perms.CREATE | ZooDefs.Perms.READ, ANYONE_ID_UNSAFE));

// given: a namespace
CuratorFramework bar = client.usingNamespace("bar");

// when: create a path with custom ACL and applyToParents option open in that namespace in background
bar.create()
.creatingParentsIfNeeded()
.withACL(acl, true)
.inBackground(callback)
.forPath("/foo/boo");
assertTrue(latch.await(2000, TimeUnit.MILLISECONDS), "Callback not invoked");

// then: the created path has custom ACL
List<ACL> actual_bar_foo_boo = client.getACL().forPath("/bar/foo/boo");
assertEquals(actual_bar_foo_boo, acl);

// then: the parent path has custom ACL
List<ACL> actual_bar_foo = client.getACL().forPath("/bar/foo");
assertEquals(actual_bar_foo, acl);

// then: but the namespace path inherits ACL from CuratorFramework
List<ACL> actual_bar = client.getACL().forPath("/bar");
assertEquals(actual_bar, ZooDefs.Ids.OPEN_ACL_UNSAFE);
} finally {
CloseableUtils.closeQuietly(client);
}
}

/**
* Tests that if no ACL list provided to the create builder, then the ACL list is created based on the client's ACLProvider.
*/
Expand Down Expand Up @@ -235,6 +304,68 @@ public void processResult(CuratorFramework client, CuratorEvent event) throws Ex
}
}

/**
* Tests that if no ACL list provided to the create builder, then the ACL list is created based on the client's ACLProvider.
*/
@Test
public void testCreateWithParentsWithoutAclInNamespace() throws Exception {
CuratorFramework client = createClient(testACLProvider);
try {
client.start();

// given: a namespace
CuratorFramework bar = client.usingNamespace("bar");

// when: create a path in that namespace
bar.create().creatingParentsIfNeeded().forPath("/foo/boo");

// then: all created paths inherit ACLs from CuratorFramework
List<ACL> actual_bar_foo_boo = client.getACL().forPath("/bar/foo/boo");
assertEquals(actual_bar_foo_boo, ZooDefs.Ids.OPEN_ACL_UNSAFE);
List<ACL> actual_bar_foo = client.getACL().forPath("/bar/foo");
assertEquals(actual_bar_foo, READ_CREATE_WRITE);

// then: also the namespace path inherits ACL from CuratorFramework
List<ACL> actual_bar = client.getACL().forPath("/bar");
assertEquals(actual_bar, READ_CREATE);
} finally {
CloseableUtils.closeQuietly(client);
}
}

/**
* Tests that if no ACL list provided to the create builder, then the ACL list is created based on the client's ACLProvider.
*/
@Test
public void testCreateWithParentsWithoutAclInNamespaceBackground() throws Exception {
CuratorFramework client = createClient(testACLProvider);
try {
client.start();

final CountDownLatch latch = new CountDownLatch(1);
BackgroundCallback callback = (client1, event) -> latch.countDown();

// given: a namespace
CuratorFramework bar = client.usingNamespace("bar");

// when: create a path in that namespace in background
bar.create().creatingParentsIfNeeded().inBackground(callback).forPath("/foo/boo");
assertTrue(latch.await(2000, TimeUnit.MILLISECONDS), "Callback not invoked");

// then: all created paths inherit ACLs from CuratorFramework
List<ACL> actual_bar_foo_boo = client.getACL().forPath("/bar/foo/boo");
assertEquals(actual_bar_foo_boo, ZooDefs.Ids.OPEN_ACL_UNSAFE);
List<ACL> actual_bar_foo = client.getACL().forPath("/bar/foo");
assertEquals(actual_bar_foo, READ_CREATE_WRITE);

// then: also the namespace path inherits ACL from CuratorFramework
List<ACL> actual_bar = client.getACL().forPath("/bar");
assertEquals(actual_bar, READ_CREATE);
} finally {
CloseableUtils.closeQuietly(client);
}
}

private void check(
CuratorFramework client, CreateBuilderMain builder, String path, byte[] data, boolean expectedSuccess)
throws Exception {
Expand Down

0 comments on commit 782696a

Please sign in to comment.