Search in sources :

Example 1 with NamespaceName

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);
}
Also used : NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) PulsarWebResource(com.yahoo.pulsar.broker.web.PulsarWebResource) UriInfo(javax.ws.rs.core.UriInfo) BeforeClass(org.testng.annotations.BeforeClass)

Example 2 with NamespaceName

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());
    }
}
Also used : NamespaceBundle(com.yahoo.pulsar.common.naming.NamespaceBundle) NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) Field(java.lang.reflect.Field) OwnershipCache(com.yahoo.pulsar.broker.namespace.OwnershipCache) BundlesData(com.yahoo.pulsar.common.policies.data.BundlesData) RestException(com.yahoo.pulsar.broker.web.RestException) URL(java.net.URL) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 3 with NamespaceName

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);
}
Also used : NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) RestException(com.yahoo.pulsar.broker.web.RestException) DestinationName(com.yahoo.pulsar.common.naming.DestinationName) URL(java.net.URL) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 4 with NamespaceName

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);
}
Also used : NamespaceBundle(com.yahoo.pulsar.common.naming.NamespaceBundle) NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) Description(org.hamcrest.Description) Matcher(org.hamcrest.Matcher) RestException(com.yahoo.pulsar.broker.web.RestException) URL(java.net.URL) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 5 with NamespaceName

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
    }
}
Also used : NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) Policies(com.yahoo.pulsar.common.policies.data.Policies) RetentionPolicies(com.yahoo.pulsar.common.policies.data.RetentionPolicies) RestException(com.yahoo.pulsar.broker.web.RestException) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Aggregations

NamespaceName (com.yahoo.pulsar.common.naming.NamespaceName)93 NamespaceBundle (com.yahoo.pulsar.common.naming.NamespaceBundle)41 Test (org.testng.annotations.Test)40 PulsarAdminException (com.yahoo.pulsar.client.admin.PulsarAdminException)38 RestException (com.yahoo.pulsar.broker.web.RestException)25 Policies (com.yahoo.pulsar.common.policies.data.Policies)18 ApiOperation (io.swagger.annotations.ApiOperation)17 ApiResponses (io.swagger.annotations.ApiResponses)17 Path (javax.ws.rs.Path)17 KeeperException (org.apache.zookeeper.KeeperException)17 PulsarServerException (com.yahoo.pulsar.broker.PulsarServerException)16 MockedPulsarServiceBaseTest (com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)14 RetentionPolicies (com.yahoo.pulsar.common.policies.data.RetentionPolicies)14 NamespaceBundles (com.yahoo.pulsar.common.naming.NamespaceBundles)13 Field (java.lang.reflect.Field)13 URL (java.net.URL)13 WebApplicationException (javax.ws.rs.WebApplicationException)13 PersistencePolicies (com.yahoo.pulsar.common.policies.data.PersistencePolicies)12 NoNodeException (org.apache.zookeeper.KeeperException.NoNodeException)10 SubscriptionBusyException (com.yahoo.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException)9