use of com.yahoo.pulsar.common.naming.NamespaceName in project pulsar by yahoo.
the class NamespacesTest method initNamespace.
@BeforeClass
public void initNamespace() throws Exception {
testLocalNamespaces = Lists.newArrayList();
testGlobalNamespaces = Lists.newArrayList();
testLocalNamespaces.add(new NamespaceName(this.testProperty, this.testLocalCluster, "test-namespace-1"));
testLocalNamespaces.add(new NamespaceName(this.testProperty, this.testLocalCluster, "test-namespace-2"));
testLocalNamespaces.add(new NamespaceName(this.testProperty, this.testOtherCluster, "test-other-namespace-1"));
testGlobalNamespaces.add(new NamespaceName(this.testProperty, "global", "test-global-ns1"));
uriField = PulsarWebResource.class.getDeclaredField("uri");
uriField.setAccessible(true);
uriInfo = mock(UriInfo.class);
}
use of com.yahoo.pulsar.common.naming.NamespaceName in project pulsar by yahoo.
the class NamespacesTest method testSplitBundles.
@Test
public void testSplitBundles() throws Exception {
URL localWebServiceUrl = new URL(pulsar.getWebServiceAddress());
String bundledNsLocal = "test-bundled-namespace-1";
BundlesData bundleData = new BundlesData(Lists.newArrayList("0x00000000", "0xffffffff"));
createBundledTestNamespaces(this.testProperty, this.testLocalCluster, bundledNsLocal, bundleData);
final NamespaceName testNs = new NamespaceName(this.testProperty, this.testLocalCluster, bundledNsLocal);
OwnershipCache MockOwnershipCache = spy(pulsar.getNamespaceService().getOwnershipCache());
doNothing().when(MockOwnershipCache).disableOwnership(any(NamespaceBundle.class));
Field ownership = NamespaceService.class.getDeclaredField("ownershipCache");
ownership.setAccessible(true);
ownership.set(pulsar.getNamespaceService(), MockOwnershipCache);
mockWebUrl(localWebServiceUrl, testNs);
// split bundles
try {
namespaces.splitNamespaceBundle(testProperty, testLocalCluster, bundledNsLocal, "0x00000000_0xffffffff", false);
// verify split bundles
BundlesData bundlesData = namespaces.getBundlesData(testProperty, testLocalCluster, bundledNsLocal);
assertNotNull(bundlesData);
assertTrue(bundlesData.boundaries.size() == 3);
assertTrue(bundlesData.boundaries.get(0).equals("0x00000000"));
assertTrue(bundlesData.boundaries.get(1).equals("0x7fffffff"));
assertTrue(bundlesData.boundaries.get(2).equals("0xffffffff"));
} catch (RestException re) {
assertEquals(re.getResponse().getStatus(), Status.PRECONDITION_FAILED.getStatusCode());
}
}
use of com.yahoo.pulsar.common.naming.NamespaceName in project pulsar by yahoo.
the class NamespacesTest method testDeleteNamespaces.
@Test
public void testDeleteNamespaces() throws Exception {
try {
namespaces.deleteNamespace(this.testProperty, this.testLocalCluster, "non-existing-namespace-1", false);
fail("should have failed");
} catch (RestException e) {
assertEquals(e.getResponse().getStatus(), Status.NOT_FOUND.getStatusCode());
}
NamespaceName testNs = this.testLocalNamespaces.get(1);
DestinationName topicName = DestinationName.get(testNs.getPersistentTopicName("my-topic"));
ZkUtils.createFullPathOptimistic(mockZookKeeper, "/managed-ledgers/" + topicName.getPersistenceNamingEncoding(), new byte[0], null, null);
// setup ownership to localhost
URL localWebServiceUrl = new URL(pulsar.getWebServiceAddress());
doReturn(localWebServiceUrl).when(nsSvc).getWebServiceUrl(testNs, false, false, false);
doReturn(true).when(nsSvc).isServiceUnitOwned(testNs);
try {
namespaces.deleteNamespace(testNs.getProperty(), testNs.getCluster(), testNs.getLocalName(), false);
fail("should have failed");
} catch (RestException e) {
// Ok, namespace not empty
assertEquals(e.getResponse().getStatus(), Status.CONFLICT.getStatusCode());
}
testNs = this.testGlobalNamespaces.get(0);
// setup ownership to localhost
doReturn(localWebServiceUrl).when(nsSvc).getWebServiceUrl(testNs, false, false, false);
doReturn(true).when(nsSvc).isServiceUnitOwned(testNs);
namespaces.deleteNamespace(testNs.getProperty(), testNs.getCluster(), testNs.getLocalName(), false);
testNs = this.testLocalNamespaces.get(0);
// setup ownership to localhost
doReturn(localWebServiceUrl).when(nsSvc).getWebServiceUrl(testNs, false, false, false);
doReturn(true).when(nsSvc).isServiceUnitOwned(testNs);
namespaces.deleteNamespace(testNs.getProperty(), testNs.getCluster(), testNs.getLocalName(), false);
List<String> nsList = Lists.newArrayList(this.testLocalNamespaces.get(1).toString(), this.testLocalNamespaces.get(2).toString());
nsList.sort(null);
assertEquals(namespaces.getPropertyNamespaces(this.testProperty), nsList);
testNs = this.testLocalNamespaces.get(1);
try {
namespaces.deleteNamespace(testNs.getProperty(), testNs.getCluster(), testNs.getLocalName(), false);
fail("should have failed");
} catch (RestException e) {
// Ok
}
// delete the topic from ZK
mockZookKeeper.delete("/managed-ledgers/" + topicName.getPersistenceNamingEncoding(), -1);
// ensure refreshed destination list in the cache
pulsar.getLocalZkCacheService().managedLedgerListCache().clearTree();
// setup ownership to localhost
doReturn(localWebServiceUrl).when(nsSvc).getWebServiceUrl(testNs, false, false, false);
doReturn(true).when(nsSvc).isServiceUnitOwned(testNs);
namespaces.deleteNamespace(testNs.getProperty(), testNs.getCluster(), testNs.getLocalName(), false);
}
use of com.yahoo.pulsar.common.naming.NamespaceName in project pulsar by yahoo.
the class NamespacesTest method testUnloadNamespaces.
@Test
public void testUnloadNamespaces() throws Exception {
final NamespaceName testNs = this.testLocalNamespaces.get(1);
URL localWebServiceUrl = new URL(pulsar.getWebServiceAddress());
doReturn(localWebServiceUrl).when(nsSvc).getWebServiceUrl(Mockito.argThat(new Matcher<NamespaceBundle>() {
@Override
public void describeTo(Description description) {
// TODO Auto-generated method stub
}
@Override
public boolean matches(Object item) {
if (item instanceof NamespaceName) {
NamespaceName ns = (NamespaceName) item;
return ns.equals(testNs);
}
return false;
}
@Override
public void _dont_implement_Matcher___instead_extend_BaseMatcher_() {
// TODO Auto-generated method stub
}
}), Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anyBoolean());
doReturn(true).when(nsSvc).isServiceUnitOwned(Mockito.argThat(new Matcher<NamespaceBundle>() {
@Override
public void describeTo(Description description) {
// TODO Auto-generated method stub
}
@Override
public boolean matches(Object item) {
if (item instanceof NamespaceName) {
NamespaceName ns = (NamespaceName) item;
return ns.equals(testNs);
}
return false;
}
@Override
public void _dont_implement_Matcher___instead_extend_BaseMatcher_() {
// TODO Auto-generated method stub
}
}));
doNothing().when(nsSvc).unloadNamespace(testNs);
NamespaceBundle bundle = nsSvc.getNamespaceBundleFactory().getFullBundle(testNs);
doNothing().when(namespaces).validateBundleOwnership(bundle, false, true);
try {
namespaces.unloadNamespace(testNs.getProperty(), testNs.getCluster(), testNs.getLocalName());
fail("should have failed as bydefault bundle is activated and can't be unloaded");
} catch (RestException re) {
// ok
}
verify(nsSvc, times(0)).unloadNamespace(testNs);
}
use of com.yahoo.pulsar.common.naming.NamespaceName in project pulsar by yahoo.
the class NamespacesTest method testGrantAndRevokePermissions.
@Test(enabled = false)
public void testGrantAndRevokePermissions() throws Exception {
Policies expectedPolicies = new Policies();
assertEquals(namespaces.getPolicies(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName()), expectedPolicies);
assertEquals(namespaces.getPermissions(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName()), expectedPolicies.auth_policies.namespace_auth);
namespaces.grantPermissionOnNamespace(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName(), "my-role", EnumSet.of(AuthAction.produce));
expectedPolicies.auth_policies.namespace_auth.put("my-role", EnumSet.of(AuthAction.produce));
assertEquals(namespaces.getPolicies(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName()), expectedPolicies);
assertEquals(namespaces.getPermissions(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName()), expectedPolicies.auth_policies.namespace_auth);
namespaces.grantPermissionOnNamespace(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName(), "other-role", EnumSet.of(AuthAction.consume));
expectedPolicies.auth_policies.namespace_auth.put("other-role", EnumSet.of(AuthAction.consume));
assertEquals(namespaces.getPolicies(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName()), expectedPolicies);
assertEquals(namespaces.getPermissions(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName()), expectedPolicies.auth_policies.namespace_auth);
namespaces.revokePermissionsOnNamespace(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName(), "my-role");
expectedPolicies.auth_policies.namespace_auth.remove("my-role");
assertEquals(namespaces.getPolicies(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName()), expectedPolicies);
assertEquals(namespaces.getPermissions(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName()), expectedPolicies.auth_policies.namespace_auth);
// Non-existing namespaces
try {
namespaces.getPolicies(this.testProperty, this.testLocalCluster, "non-existing-namespace-1");
fail("should have failed");
} catch (RestException e) {
assertEquals(e.getResponse().getStatus(), Status.NOT_FOUND.getStatusCode());
}
try {
namespaces.getPermissions(this.testProperty, this.testLocalCluster, "non-existing-namespace-1");
fail("should have failed");
} catch (RestException e) {
assertEquals(e.getResponse().getStatus(), Status.NOT_FOUND.getStatusCode());
}
try {
namespaces.grantPermissionOnNamespace(this.testProperty, this.testLocalCluster, "non-existing-namespace-1", "my-role", EnumSet.of(AuthAction.produce));
fail("should have failed");
} catch (RestException e) {
assertEquals(e.getResponse().getStatus(), Status.NOT_FOUND.getStatusCode());
}
try {
namespaces.revokePermissionsOnNamespace(this.testProperty, this.testLocalCluster, "non-existing-namespace-1", "my-role");
fail("should have failed");
} catch (RestException e) {
assertEquals(e.getResponse().getStatus(), Status.NOT_FOUND.getStatusCode());
}
NamespaceName testNs = this.testLocalNamespaces.get(1);
mockZookKeeper.failNow(Code.SESSIONEXPIRED);
try {
namespaces.getPolicies(testNs.getProperty(), testNs.getCluster(), testNs.getLocalName());
fail("should have failed");
} catch (RestException e) {
// Ok
}
mockZookKeeper.failNow(Code.SESSIONEXPIRED);
try {
namespaces.getPermissions(testNs.getProperty(), testNs.getCluster(), testNs.getLocalName());
fail("should have failed");
} catch (RestException e) {
// Ok
}
mockZookKeeper.failNow(Code.SESSIONEXPIRED);
try {
namespaces.grantPermissionOnNamespace(testNs.getProperty(), testNs.getCluster(), testNs.getLocalName(), "other-role", EnumSet.of(AuthAction.consume));
fail("should have failed");
} catch (RestException e) {
// Ok
}
mockZookKeeper.failNow(Code.BADVERSION);
try {
namespaces.grantPermissionOnNamespace(testNs.getProperty(), testNs.getCluster(), testNs.getLocalName(), "other-role", EnumSet.of(AuthAction.consume));
fail("should have failed");
} catch (RestException e) {
assertEquals(e.getResponse().getStatus(), Status.CONFLICT.getStatusCode());
}
mockZookKeeper.failNow(Code.BADVERSION);
try {
namespaces.revokePermissionsOnNamespace(testNs.getProperty(), testNs.getCluster(), testNs.getLocalName(), "other-role");
fail("should have failed");
} catch (RestException e) {
assertEquals(e.getResponse().getStatus(), Status.CONFLICT.getStatusCode());
}
mockZookKeeper.failNow(Code.SESSIONEXPIRED);
try {
namespaces.revokePermissionsOnNamespace(testNs.getProperty(), testNs.getCluster(), testNs.getLocalName(), "other-role");
fail("should have failed");
} catch (RestException e) {
// Ok
}
}
Aggregations