Search in sources :

Example 6 with BundlesData

use of com.yahoo.pulsar.common.policies.data.BundlesData in project pulsar by yahoo.

the class NamespacesTest method setup.

@Override
@BeforeMethod
public void setup() throws Exception {
    super.internalSetup();
    namespaces = spy(new Namespaces());
    namespaces.setServletContext(new MockServletContext());
    namespaces.setPulsar(pulsar);
    doReturn(mockZookKeeper).when(namespaces).globalZk();
    doReturn(mockZookKeeper).when(namespaces).localZk();
    doReturn(pulsar.getConfigurationCache().propertiesCache()).when(namespaces).propertiesCache();
    doReturn(pulsar.getConfigurationCache().policiesCache()).when(namespaces).policiesCache();
    doReturn(false).when(namespaces).isRequestHttps();
    doReturn("test").when(namespaces).clientAppId();
    doReturn(Sets.newTreeSet(Lists.newArrayList("use", "usw", "usc", "global"))).when(namespaces).clusters();
    doNothing().when(namespaces).validateAdminAccessOnProperty("my-property");
    doNothing().when(namespaces).validateAdminAccessOnProperty("other-property");
    doNothing().when(namespaces).validateAdminAccessOnProperty("new-property");
    admin.clusters().createCluster("use", new ClusterData("http://broker-use.com:" + BROKER_WEBSERVICE_PORT));
    admin.clusters().createCluster("usw", new ClusterData("http://broker-usw.com:" + BROKER_WEBSERVICE_PORT));
    admin.clusters().createCluster("usc", new ClusterData("http://broker-usc.com:" + BROKER_WEBSERVICE_PORT));
    admin.properties().createProperty(this.testProperty, new PropertyAdmin(Lists.newArrayList("role1", "role2"), Sets.newHashSet("use", "usc", "usw")));
    createTestNamespaces(this.testProperty, this.testLocalNamespaces, new BundlesData());
    createGlobalTestNamespaces(this.testProperty, this.testGlobalNamespaces.get(0).getLocalName(), new BundlesData());
    nsSvc = pulsar.getNamespaceService();
}
Also used : ClusterData(com.yahoo.pulsar.common.policies.data.ClusterData) PropertyAdmin(com.yahoo.pulsar.common.policies.data.PropertyAdmin) BundlesData(com.yahoo.pulsar.common.policies.data.BundlesData) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 7 with BundlesData

use of com.yahoo.pulsar.common.policies.data.BundlesData in project pulsar by yahoo.

the class NamespacesTest method testUnloadNamespaceWithBundles.

@Test
public void testUnloadNamespaceWithBundles() throws Exception {
    URL localWebServiceUrl = new URL(pulsar.getWebServiceAddress());
    String bundledNsLocal = "test-bundled-namespace-1";
    BundlesData bundleData = new BundlesData(Lists.newArrayList("0x00000000", "0x80000000", "0xffffffff"));
    createBundledTestNamespaces(this.testProperty, this.testLocalCluster, bundledNsLocal, bundleData);
    final NamespaceName testNs = new NamespaceName(this.testProperty, this.testLocalCluster, bundledNsLocal);
    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 NamespaceBundle) {
                NamespaceBundle bundle = (NamespaceBundle) item;
                return bundle.getNamespaceObject().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 NamespaceBundle) {
                NamespaceBundle bundle = (NamespaceBundle) item;
                return bundle.getNamespaceObject().equals(testNs);
            }
            return false;
        }

        @Override
        public void _dont_implement_Matcher___instead_extend_BaseMatcher_() {
        // TODO Auto-generated method stub
        }
    }));
    NamespaceBundles nsBundles = nsSvc.getNamespaceBundleFactory().getBundles(testNs, bundleData);
    NamespaceBundle testBundle = nsBundles.getBundles().get(0);
    // make one bundle owned
    doReturn(localWebServiceUrl).when(nsSvc).getWebServiceUrl(testBundle, false, true, false);
    doReturn(true).when(nsSvc).isServiceUnitOwned(testBundle);
    doNothing().when(nsSvc).unloadNamespaceBundle(testBundle);
    namespaces.unloadNamespaceBundle(testProperty, testLocalCluster, bundledNsLocal, "0x00000000_0x80000000", false);
    verify(nsSvc, times(1)).unloadNamespaceBundle(testBundle);
    try {
        namespaces.unloadNamespaceBundle(testProperty, testLocalCluster, bundledNsLocal, "0x00000000_0x88000000", false);
        fail("should have failed");
    } catch (RestException re) {
    // ok
    }
}
Also used : NamespaceBundle(com.yahoo.pulsar.common.naming.NamespaceBundle) NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) Description(org.hamcrest.Description) Matcher(org.hamcrest.Matcher) NamespaceBundles(com.yahoo.pulsar.common.naming.NamespaceBundles) 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 8 with BundlesData

use of com.yahoo.pulsar.common.policies.data.BundlesData in project pulsar by yahoo.

the class AdminTest method properties.

@Test
void properties() throws Exception {
    assertEquals(properties.getProperties(), Lists.newArrayList());
    verify(properties, times(1)).validateSuperUserAccess();
    Set<String> allowedClusters = Sets.newHashSet();
    PropertyAdmin propertyAdmin = new PropertyAdmin(Lists.newArrayList("role1", "role2"), allowedClusters);
    properties.createProperty("test-property", propertyAdmin);
    verify(properties, times(2)).validateSuperUserAccess();
    assertEquals(properties.getProperties(), Lists.newArrayList("test-property"));
    verify(properties, times(3)).validateSuperUserAccess();
    assertEquals(properties.getPropertyAdmin("test-property"), propertyAdmin);
    verify(properties, times(4)).validateSuperUserAccess();
    PropertyAdmin newPropertyAdmin = new PropertyAdmin(Lists.newArrayList("role1", "other-role"), allowedClusters);
    properties.updateProperty("test-property", newPropertyAdmin);
    verify(properties, times(5)).validateSuperUserAccess();
    // Wait for updateProperty to take effect
    Thread.sleep(100);
    assertEquals(properties.getPropertyAdmin("test-property"), newPropertyAdmin);
    assertNotSame(properties.getPropertyAdmin("test-property"), propertyAdmin);
    verify(properties, times(7)).validateSuperUserAccess();
    // Check creating existing property
    try {
        properties.createProperty("test-property", propertyAdmin);
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.CONFLICT.getStatusCode());
    }
    // Check non-existing property
    try {
        properties.getPropertyAdmin("non-existing");
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.NOT_FOUND.getStatusCode());
    }
    try {
        properties.updateProperty("xxx-non-existing", newPropertyAdmin);
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.NOT_FOUND.getStatusCode());
    }
    // Check deleting non-existing property
    try {
        properties.deleteProperty("non-existing");
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.NOT_FOUND.getStatusCode());
    }
    // Test zk failures
    mockZookKeeper.failNow(Code.SESSIONEXPIRED);
    try {
        properties.getProperties();
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }
    mockZookKeeper.failNow(Code.SESSIONEXPIRED);
    try {
        properties.getPropertyAdmin("my-property");
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }
    mockZookKeeper.failNow(Code.SESSIONEXPIRED);
    try {
        properties.updateProperty("my-property", newPropertyAdmin);
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }
    mockZookKeeper.failNow(Code.SESSIONEXPIRED);
    try {
        properties.createProperty("test", propertyAdmin);
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }
    mockZookKeeper.failNow(Code.SESSIONEXPIRED);
    try {
        properties.deleteProperty("my-property");
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }
    properties.createProperty("error-property", propertyAdmin);
    mockZookKeeper.failAfter(2, Code.SESSIONEXPIRED);
    try {
        properties.deleteProperty("error-property");
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }
    properties.deleteProperty("test-property");
    properties.deleteProperty("error-property");
    assertEquals(properties.getProperties(), Lists.newArrayList());
    // Create a namespace to test deleting a non-empty property
    clusters.createCluster("use", new ClusterData());
    newPropertyAdmin = new PropertyAdmin(Lists.newArrayList("role1", "other-role"), Sets.newHashSet("use"));
    properties.createProperty("my-property", newPropertyAdmin);
    namespaces.createNamespace("my-property", "use", "my-namespace", new BundlesData());
    try {
        properties.deleteProperty("my-property");
        fail("should have failed");
    } catch (RestException e) {
    // Ok
    }
    // Check name validation
    try {
        properties.createProperty("test&", propertyAdmin);
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.PRECONDITION_FAILED.getStatusCode());
    }
    namespaces.deleteNamespace("my-property", "use", "my-namespace", false);
    properties.deleteProperty("my-property");
}
Also used : ClusterData(com.yahoo.pulsar.common.policies.data.ClusterData) PropertyAdmin(com.yahoo.pulsar.common.policies.data.PropertyAdmin) RestException(com.yahoo.pulsar.broker.web.RestException) BundlesData(com.yahoo.pulsar.common.policies.data.BundlesData) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 9 with BundlesData

use of com.yahoo.pulsar.common.policies.data.BundlesData in project pulsar by yahoo.

the class NamespacesTest method testCreateNamespaces.

@Test
public void testCreateNamespaces() throws Exception {
    try {
        namespaces.createNamespace("my-property", "other-colo", "my-namespace", new BundlesData());
        fail("should have failed");
    } catch (RestException e) {
    // Ok, cluster doesn't exist
    }
    List<NamespaceName> nsnames = Lists.newArrayList();
    nsnames.add(new NamespaceName("my-property", "use", "create-namespace-1"));
    nsnames.add(new NamespaceName("my-property", "use", "create-namespace-2"));
    nsnames.add(new NamespaceName("my-property", "usc", "create-other-namespace-1"));
    createTestNamespaces("my-property", nsnames, new BundlesData());
    try {
        namespaces.createNamespace("my-property", "use", "create-namespace-1", new BundlesData());
        fail("should have failed");
    } catch (RestException e) {
    // Ok, namespace already exists
    }
    try {
        namespaces.createNamespace("other-property", "use", "create-namespace-1", new BundlesData());
        fail("should have failed");
    } catch (RestException e) {
    // Ok, property doesn't exist
    }
    try {
        namespaces.createNamespace("my-property", "use", "create-namespace-#", new BundlesData());
        fail("should have failed");
    } catch (RestException e) {
        // Ok, invalid namespace name
        assertEquals(e.getResponse().getStatus(), Status.PRECONDITION_FAILED.getStatusCode());
    }
    mockZookKeeper.failNow(Code.SESSIONEXPIRED);
    try {
        namespaces.createNamespace("my-property", "use", "my-namespace-3", new BundlesData());
        fail("should have failed");
    } catch (RestException e) {
    // Ok
    }
}
Also used : NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) BundlesData(com.yahoo.pulsar.common.policies.data.BundlesData) RestException(com.yahoo.pulsar.broker.web.RestException) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 10 with BundlesData

use of com.yahoo.pulsar.common.policies.data.BundlesData in project pulsar by yahoo.

the class NamespacesTest method testValidateDestinationOwnership.

@Test
public void testValidateDestinationOwnership() throws Exception {
    try {
        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);
        DestinationName topicName = DestinationName.get(testNs.getPersistentTopicName("my-topic"));
        PersistentTopics topics = spy(new PersistentTopics());
        topics.setServletContext(new MockServletContext());
        topics.setPulsar(pulsar);
        doReturn(false).when(topics).isRequestHttps();
        doReturn("test").when(topics).clientAppId();
        mockWebUrl(localWebServiceUrl, testNs);
        try {
            topics.validateAdminOperationOnDestination(topicName, false);
        } catch (RestException e) {
            fail("validateAdminAccessOnProperty failed");
        }
        try {
            topics.validateAdminOperationOnDestination(DestinationName.get(""), false);
            fail("validateAdminAccessOnProperty failed");
        } catch (Exception e) {
        // OK
        }
    } catch (RestException e) {
        fail("validateAdminAccessOnProperty failed");
    }
}
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) DestinationName(com.yahoo.pulsar.common.naming.DestinationName) RestException(com.yahoo.pulsar.broker.web.RestException) URL(java.net.URL) RestException(com.yahoo.pulsar.broker.web.RestException) ClientErrorException(javax.ws.rs.ClientErrorException) WebApplicationException(javax.ws.rs.WebApplicationException) PulsarAdminException(com.yahoo.pulsar.client.admin.PulsarAdminException) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Aggregations

BundlesData (com.yahoo.pulsar.common.policies.data.BundlesData)13 RestException (com.yahoo.pulsar.broker.web.RestException)10 Test (org.testng.annotations.Test)10 MockedPulsarServiceBaseTest (com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)9 NamespaceName (com.yahoo.pulsar.common.naming.NamespaceName)8 NamespaceBundle (com.yahoo.pulsar.common.naming.NamespaceBundle)6 URL (java.net.URL)6 OwnershipCache (com.yahoo.pulsar.broker.namespace.OwnershipCache)4 Field (java.lang.reflect.Field)4 NamespaceBundles (com.yahoo.pulsar.common.naming.NamespaceBundles)3 PulsarAdminException (com.yahoo.pulsar.client.admin.PulsarAdminException)2 ClusterData (com.yahoo.pulsar.common.policies.data.ClusterData)2 PropertyAdmin (com.yahoo.pulsar.common.policies.data.PropertyAdmin)2 ClientErrorException (javax.ws.rs.ClientErrorException)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 KeeperException (org.apache.zookeeper.KeeperException)2 Description (org.hamcrest.Description)2 Matcher (org.hamcrest.Matcher)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 NamespaceEphemeralData (com.yahoo.pulsar.broker.namespace.NamespaceEphemeralData)1