Search in sources :

Example 6 with Topology

use of org.apache.knox.gateway.topology.Topology in project knox by apache.

the class TopologyRulesModuleTest method testParseServiceParamsInKnoxFormat.

@Test
public void testParseServiceParamsInKnoxFormat() throws IOException, SAXException {
    Digester digester = loader.newDigester();
    String name = "org/apache/knox/gateway/topology/xml/service-param-topology-knox-format.xml";
    URL url = ClassLoader.getSystemResource(name);
    assertThat("Failed to find URL for resource " + name, url, notNullValue());
    File file = new File(url.getFile());
    TopologyBuilder topologyBuilder = digester.parse(url);
    Topology topology = topologyBuilder.build();
    assertThat("Failed to parse resource " + name, topology, notNullValue());
    topology.setTimestamp(file.lastModified());
    assertThat(topology.getName(), is("test-topology-name"));
    assertThat(topology.getTimestamp(), is(file.lastModified()));
    assertThat(topology.getServices().size(), is(1));
    Provider provider = topology.getProviders().iterator().next();
    assertThat(provider, notNullValue());
    assertThat(provider.getRole(), is("test-provider-role"));
    assertThat(provider.getName(), is("test-provider-name"));
    assertThat(provider.getParams(), hasEntry(is("test-provider-param-name-1"), is("test-provider-param-value-1")));
    assertThat(provider.getParams(), hasEntry(is("test-provider-param-name-2"), is("test-provider-param-value-2")));
    Service service = topology.getServices().iterator().next();
    assertThat(service, notNullValue());
    assertThat(service.getRole(), is("test-service-role"));
    assertThat(service.getUrls().size(), is(2));
    assertThat(service.getUrls(), hasItem("test-service-scheme://test-service-host1:42/test-service-path"));
    assertThat(service.getUrls(), hasItem("test-service-scheme://test-service-host2:42/test-service-path"));
    assertThat(service.getName(), is("test-service-name"));
    assertThat(service.getParams(), hasEntry(is("test-service-param-name-1"), is("test-service-param-value-1")));
    assertThat(service.getParams(), hasEntry(is("test-service-param-name-2"), is("test-service-param-value-2")));
    assertThat(service.getVersion(), is(new Version(1, 0, 0)));
}
Also used : TopologyBuilder(org.apache.knox.gateway.topology.builder.TopologyBuilder) Version(org.apache.knox.gateway.topology.Version) Digester(org.apache.commons.digester3.Digester) Service(org.apache.knox.gateway.topology.Service) Topology(org.apache.knox.gateway.topology.Topology) File(java.io.File) URL(java.net.URL) Provider(org.apache.knox.gateway.topology.Provider) Test(org.junit.Test)

Example 7 with Topology

use of org.apache.knox.gateway.topology.Topology in project knox by apache.

the class DeploymentFactoryTest method test_validateNoAppsWithDuplicateUrlsInTopology.

@Test(timeout = TestUtils.SHORT_TIMEOUT)
public void test_validateNoAppsWithDuplicateUrlsInTopology() {
    DeploymentFactory.validateNoAppsWithDuplicateUrlsInTopology(null);
    Topology topology = new Topology();
    topology.setName("test-topology");
    DeploymentFactory.validateNoAppsWithDuplicateUrlsInTopology(topology);
    Application application;
    topology = new Topology();
    topology.setName("test-topology");
    application = new Application();
    application.setName("test-application-1");
    topology.addApplication(application);
    DeploymentFactory.validateNoAppsWithDuplicateUrlsInTopology(topology);
    topology = new Topology();
    topology.setName("test-topology");
    application = new Application();
    application.setName("test-application-1");
    topology.addApplication(application);
    application = new Application();
    application.setName("test-application-2");
    topology.addApplication(application);
    DeploymentFactory.validateNoAppsWithDuplicateUrlsInTopology(topology);
    topology = new Topology();
    topology.setName("test-topology");
    application = new Application();
    application.setName("test-application-1");
    topology.addApplication(application);
    application = new Application();
    application.setName("test-application-2");
    application.addUrl("/test-application-2");
    topology.addApplication(application);
    DeploymentFactory.validateNoAppsWithDuplicateUrlsInTopology(topology);
    topology = new Topology();
    topology.setName("test-topology");
    application = new Application();
    application.setName("test-application-1");
    topology.addApplication(application);
    application = new Application();
    application.setName("test-application-2");
    application.addUrl("/");
    topology.addApplication(application);
    DeploymentFactory.validateNoAppsWithDuplicateUrlsInTopology(topology);
    topology = new Topology();
    topology.setName("test-topology");
    application = new Application();
    application.setName("test-application-1");
    topology.addApplication(application);
    application = new Application();
    application.setName("test-application-2");
    application.addUrl("/");
    topology.addApplication(application);
    DeploymentFactory.validateNoAppsWithDuplicateUrlsInTopology(topology);
    topology = new Topology();
    topology.setName("test-topology");
    application = new Application();
    application.setName("test-application-1");
    application.addUrl("/test-application-1");
    topology.addApplication(application);
    application = new Application();
    application.setName("test-application-2");
    application.addUrl("/test-application-2");
    topology.addApplication(application);
    DeploymentFactory.validateNoAppsWithDuplicateUrlsInTopology(topology);
    topology = new Topology();
    topology.setName("test-topology");
    application = new Application();
    application.setName("test-application-1");
    application.addUrl("/test-application-dup");
    topology.addApplication(application);
    application = new Application();
    application.setName("test-application-2");
    application.addUrl("/test-application-dup");
    topology.addApplication(application);
    try {
        DeploymentFactory.validateNoAppsWithDuplicateUrlsInTopology(topology);
        fail("Expected DeploymentException");
    } catch (DeploymentException e) {
    // Expected.
    }
    topology = new Topology();
    topology.setName("test-topology");
    application = new Application();
    application.setName("test-application-1");
    application.addUrl("/");
    topology.addApplication(application);
    application = new Application();
    application.setName("test-application-2");
    application.addUrl("/");
    topology.addApplication(application);
    try {
        DeploymentFactory.validateNoAppsWithDuplicateUrlsInTopology(topology);
        fail("Expected DeploymentException");
    } catch (DeploymentException e) {
    // Expected.
    }
    topology = new Topology();
    topology.setName("test-topology");
    application = new Application();
    application.setName("test-application-1");
    application.addUrl("");
    topology.addApplication(application);
    application = new Application();
    application.setName("test-application-2");
    application.addUrl("/");
    topology.addApplication(application);
    try {
        DeploymentFactory.validateNoAppsWithDuplicateUrlsInTopology(topology);
        fail("Expected DeploymentException");
    } catch (DeploymentException e) {
    // Expected.
    }
    topology = new Topology();
    topology.setName("test-topology");
    application = new Application();
    application.setName("test-application-1");
    topology.addApplication(application);
    application = new Application();
    application.setName("test-application-2");
    application.addUrl("/test-application-1");
    topology.addApplication(application);
    try {
        DeploymentFactory.validateNoAppsWithDuplicateUrlsInTopology(topology);
        fail("Expected DeploymentException");
    } catch (DeploymentException e) {
    // Expected.
    }
    topology = new Topology();
    topology.setName("test-topology");
    application = new Application();
    application.setName("test-application-1");
    topology.addApplication(application);
    application = new Application();
    application.setName("test-application-1");
    topology.addApplication(application);
    try {
        DeploymentFactory.validateNoAppsWithDuplicateUrlsInTopology(topology);
        fail("Expected DeploymentException");
    } catch (DeploymentException e) {
    // Expected.
    }
    topology = new Topology();
    topology.setName("test-topology");
    application = new Application();
    application.setName("test-application-1");
    application.addUrl("/test-application-1");
    application.addUrl("/test-application-3");
    topology.addApplication(application);
    application = new Application();
    application.setName("test-application-2");
    application.addUrl("/test-application-2");
    application.addUrl("/test-application-3");
    topology.addApplication(application);
    try {
        DeploymentFactory.validateNoAppsWithDuplicateUrlsInTopology(topology);
        fail("Expected DeploymentException");
    } catch (DeploymentException e) {
    // Expected.
    }
}
Also used : Topology(org.apache.knox.gateway.topology.Topology) Application(org.apache.knox.gateway.topology.Application) Test(org.junit.Test)

Example 8 with Topology

use of org.apache.knox.gateway.topology.Topology in project knox by apache.

the class DeploymentFactoryTest method test_validateNoAppsWithRootUrlsInServicesTopology.

@Test(timeout = TestUtils.SHORT_TIMEOUT)
public void test_validateNoAppsWithRootUrlsInServicesTopology() {
    DeploymentFactory.validateNoAppsWithRootUrlsInServicesTopology(null);
    Topology topology = new Topology();
    topology.setName("test-topology");
    DeploymentFactory.validateNoAppsWithRootUrlsInServicesTopology(topology);
    Service service;
    Application application;
    topology = new Topology();
    topology.setName("test-topology");
    service = new Service();
    service.setName("test-service");
    service.setRole("test-service");
    topology.addService(service);
    application = new Application();
    application.setName("test-application");
    topology.addApplication(application);
    topology = new Topology();
    topology.setName("test-topology");
    service = new Service();
    service.setName("test-service");
    service.setRole("test-service");
    topology.addService(service);
    application = new Application();
    application.setName("test-application");
    application.addUrl("");
    topology.addApplication(application);
    try {
        DeploymentFactory.validateNoAppsWithRootUrlsInServicesTopology(topology);
        fail("Expected DeploymentException");
    } catch (DeploymentException e) {
    // Expected.
    }
    topology = new Topology();
    topology.setName("test-topology");
    service = new Service();
    service.setName("test-service");
    service.setRole("test-service");
    topology.addService(service);
    application = new Application();
    application.setName("test-application");
    application.addUrl("/");
    topology.addApplication(application);
    try {
        DeploymentFactory.validateNoAppsWithRootUrlsInServicesTopology(topology);
        fail("Expected DeploymentException");
    } catch (DeploymentException e) {
    // Expected.
    }
    topology = new Topology();
    topology.setName("test-topology");
    service = new Service();
    service.setName("test-service");
    service.setRole("test-service");
    topology.addService(service);
    application = new Application();
    application.setName("test-application");
    application.addUrl("/");
    topology.addApplication(application);
    try {
        DeploymentFactory.validateNoAppsWithRootUrlsInServicesTopology(topology);
        fail("Expected DeploymentException");
    } catch (DeploymentException e) {
    // Expected.
    }
    topology = new Topology();
    topology.setName("test-topology");
    service = new Service();
    service.setName("test-service");
    service.setRole("test-service");
    topology.addService(service);
    application = new Application();
    application.setName("test-application");
    application.addUrl("/test-application");
    application.addUrl("/");
    topology.addApplication(application);
    try {
        DeploymentFactory.validateNoAppsWithRootUrlsInServicesTopology(topology);
        fail("Expected DeploymentException");
    } catch (DeploymentException e) {
    // Expected.
    }
}
Also used : Service(org.apache.knox.gateway.topology.Service) Topology(org.apache.knox.gateway.topology.Topology) Application(org.apache.knox.gateway.topology.Application) Test(org.junit.Test)

Example 9 with Topology

use of org.apache.knox.gateway.topology.Topology in project knox by apache.

the class ServiceDefinitionDeploymentContributorTest method testServiceAttributeUseTwoWaySSLParamOverride.

/**
 * Test that service param useTwoWaySsl in topologies overrides the corresponding custom dispatch property.
 */
@Test
public void testServiceAttributeUseTwoWaySSLParamOverride() throws Exception {
    final String TEST_SERVICE_ROLE = "Test";
    final String USE_TWO_WAY_SSL_PARAM = "useTwoWaySsl";
    UrlRewriteRulesDescriptor clusterRules = EasyMock.createNiceMock(UrlRewriteRulesDescriptor.class);
    EasyMock.replay(clusterRules);
    UrlRewriteRulesDescriptor svcRules = EasyMock.createNiceMock(UrlRewriteRulesDescriptor.class);
    EasyMock.replay(svcRules);
    ServiceDefinition svcDef = EasyMock.createNiceMock(ServiceDefinition.class);
    EasyMock.expect(svcDef.getRole()).andReturn(TEST_SERVICE_ROLE).anyTimes();
    List<Route> svcRoutes = new ArrayList<>();
    Route route = EasyMock.createNiceMock(Route.class);
    List<Rewrite> filters = new ArrayList<>();
    EasyMock.expect(route.getRewrites()).andReturn(filters).anyTimes();
    svcRoutes.add(route);
    EasyMock.replay(route);
    EasyMock.expect(svcDef.getRoutes()).andReturn(svcRoutes).anyTimes();
    CustomDispatch cd = EasyMock.createNiceMock(CustomDispatch.class);
    EasyMock.expect(cd.getClassName()).andReturn("TestDispatch").anyTimes();
    EasyMock.expect(cd.getHaClassName()).andReturn("TestHADispatch").anyTimes();
    EasyMock.expect(cd.getHaContributorName()).andReturn(null).anyTimes();
    // Let useTwoWaySsl be FALSE by default
    EasyMock.expect(cd.getUseTwoWaySsl()).andReturn(false).anyTimes();
    EasyMock.replay(cd);
    EasyMock.expect(svcDef.getDispatch()).andReturn(cd).anyTimes();
    EasyMock.replay(svcDef);
    ServiceDefinitionDeploymentContributor sddc = new ServiceDefinitionDeploymentContributor(svcDef, svcRules);
    DeploymentContext context = EasyMock.createNiceMock(DeploymentContext.class);
    EasyMock.expect(context.getDescriptor("rewrite")).andReturn(clusterRules).anyTimes();
    GatewayConfig gc = EasyMock.createNiceMock(GatewayConfig.class);
    EasyMock.expect(gc.isXForwardedEnabled()).andReturn(false).anyTimes();
    EasyMock.expect(gc.isCookieScopingToPathEnabled()).andReturn(false).anyTimes();
    EasyMock.replay(gc);
    EasyMock.expect(context.getGatewayConfig()).andReturn(gc).anyTimes();
    // Configure the HaProvider
    Topology topology = EasyMock.createNiceMock(Topology.class);
    List<Provider> providers = new ArrayList<>();
    Provider haProvider = EasyMock.createNiceMock(Provider.class);
    EasyMock.expect(haProvider.getRole()).andReturn("ha").anyTimes();
    EasyMock.expect(haProvider.isEnabled()).andReturn(true).anyTimes();
    Map<String, String> providerParams = new HashMap<>();
    providerParams.put(TEST_SERVICE_ROLE, "whatever");
    EasyMock.expect(haProvider.getParams()).andReturn(providerParams).anyTimes();
    EasyMock.replay(haProvider);
    providers.add(haProvider);
    EasyMock.expect(topology.getProviders()).andReturn(providers).anyTimes();
    EasyMock.replay(topology);
    EasyMock.expect(context.getTopology()).andReturn(topology).anyTimes();
    TestGatewayDescriptor gd = new TestGatewayDescriptor();
    EasyMock.expect(context.getGatewayDescriptor()).andReturn(gd).anyTimes();
    EasyMock.replay(context);
    // Configure the service with the useTwoWaySsl param to OVERRIDE the value in the service definition
    Service service = EasyMock.createNiceMock(Service.class);
    Map<String, String> svcParams = new HashMap<>();
    svcParams.put(USE_TWO_WAY_SSL_PARAM, "true");
    EasyMock.expect(service.getParams()).andReturn(svcParams).anyTimes();
    EasyMock.replay(service);
    sddc.contributeService(context, service);
    List<ResourceDescriptor> resources = gd.resources();
    assertEquals(1, gd.resources().size());
    ResourceDescriptor res = gd.resources().get(0);
    assertNotNull(res);
    List<FilterDescriptor> filterList = res.filters();
    assertEquals(1, filterList.size());
    FilterDescriptor f = filterList.get(0);
    assertNotNull(f);
    assertEquals("dispatch", f.role());
    List<FilterParamDescriptor> fParams = f.params();
    assertNotNull(fParams);
    // Collect the values of filter params named useTwoWaySsl
    List<String> useTwoWaySslFilterParamValues = new ArrayList<>();
    for (FilterParamDescriptor param : fParams) {
        if (param.name().equals(USE_TWO_WAY_SSL_PARAM)) {
            useTwoWaySslFilterParamValues.add(param.value());
        }
    }
    assertEquals("Expected only a single filter param named " + USE_TWO_WAY_SSL_PARAM, 1, useTwoWaySslFilterParamValues.size());
    assertEquals("Expected the service param to override the service definition value for " + USE_TWO_WAY_SSL_PARAM, "true", useTwoWaySslFilterParamValues.get(0));
}
Also used : CustomDispatch(org.apache.knox.gateway.service.definition.CustomDispatch) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) FilterDescriptor(org.apache.knox.gateway.descriptor.FilterDescriptor) ServiceDefinition(org.apache.knox.gateway.service.definition.ServiceDefinition) Route(org.apache.knox.gateway.service.definition.Route) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) FilterParamDescriptor(org.apache.knox.gateway.descriptor.FilterParamDescriptor) Service(org.apache.knox.gateway.topology.Service) Topology(org.apache.knox.gateway.topology.Topology) Provider(org.apache.knox.gateway.topology.Provider) DeploymentContext(org.apache.knox.gateway.deploy.DeploymentContext) Rewrite(org.apache.knox.gateway.service.definition.Rewrite) ResourceDescriptor(org.apache.knox.gateway.descriptor.ResourceDescriptor) Test(org.junit.Test)

Example 10 with Topology

use of org.apache.knox.gateway.topology.Topology in project knox by apache.

the class DefaultTopologyServiceTest method testGetTopologies.

@Test
public void testGetTopologies() throws Exception {
    File dir = createDir();
    File topologyDir = new File(dir, "topologies");
    File descriptorsDir = new File(dir, "descriptors");
    descriptorsDir.mkdirs();
    File sharedProvidersDir = new File(dir, "shared-providers");
    sharedProvidersDir.mkdirs();
    long time = topologyDir.lastModified();
    try {
        createFile(topologyDir, "one.xml", "org/apache/knox/gateway/topology/file/topology-one.xml", time);
        TestTopologyListener topoListener = new TestTopologyListener();
        FileAlterationMonitor monitor = new FileAlterationMonitor(Long.MAX_VALUE);
        TopologyService provider = new DefaultTopologyService();
        Map<String, String> c = new HashMap<>();
        GatewayConfig config = EasyMock.createNiceMock(GatewayConfig.class);
        EasyMock.expect(config.getGatewayTopologyDir()).andReturn(topologyDir.getAbsolutePath()).anyTimes();
        EasyMock.expect(config.getGatewayConfDir()).andReturn(descriptorsDir.getParentFile().getAbsolutePath()).anyTimes();
        EasyMock.expect(config.getGatewayProvidersConfigDir()).andReturn(sharedProvidersDir.getAbsolutePath()).anyTimes();
        EasyMock.expect(config.getGatewayDescriptorsDir()).andReturn(descriptorsDir.getAbsolutePath()).anyTimes();
        EasyMock.replay(config);
        provider.init(config, c);
        provider.addTopologyChangeListener(topoListener);
        provider.reloadTopologies();
        Collection<Topology> topologies = provider.getTopologies();
        assertThat(topologies, notNullValue());
        assertThat(topologies.size(), is(1));
        Topology topology = topologies.iterator().next();
        assertThat(topology.getName(), is("one"));
        assertThat(topology.getTimestamp(), is(time));
        assertThat(topoListener.events.size(), is(1));
        topoListener.events.clear();
        // Add a file to the directory.
        File two = createFile(topologyDir, "two.xml", "org/apache/knox/gateway/topology/file/topology-two.xml", 1L);
        provider.reloadTopologies();
        topologies = provider.getTopologies();
        assertThat(topologies.size(), is(2));
        Set<String> names = new HashSet<>(Arrays.asList("one", "two"));
        Iterator<Topology> iterator = topologies.iterator();
        topology = iterator.next();
        assertThat(names, hasItem(topology.getName()));
        names.remove(topology.getName());
        topology = iterator.next();
        assertThat(names, hasItem(topology.getName()));
        names.remove(topology.getName());
        assertThat(names.size(), is(0));
        assertThat(topoListener.events.size(), is(1));
        List<TopologyEvent> events = topoListener.events.get(0);
        assertThat(events.size(), is(1));
        TopologyEvent event = events.get(0);
        assertThat(event.getType(), is(TopologyEvent.Type.CREATED));
        assertThat(event.getTopology(), notNullValue());
        // Update a file in the directory.
        two = createFile(topologyDir, "two.xml", "org/apache/knox/gateway/topology/file/topology-three.xml", 2L);
        provider.reloadTopologies();
        topologies = provider.getTopologies();
        assertThat(topologies.size(), is(2));
        names = new HashSet<>(Arrays.asList("one", "two"));
        iterator = topologies.iterator();
        topology = iterator.next();
        assertThat(names, hasItem(topology.getName()));
        names.remove(topology.getName());
        topology = iterator.next();
        assertThat(names, hasItem(topology.getName()));
        names.remove(topology.getName());
        assertThat(names.size(), is(0));
        // Remove a file from the directory.
        two.delete();
        provider.reloadTopologies();
        topologies = provider.getTopologies();
        assertThat(topologies.size(), is(1));
        topology = topologies.iterator().next();
        assertThat(topology.getName(), is("one"));
        assertThat(topology.getTimestamp(), is(time));
    } finally {
        FileUtils.deleteQuietly(dir);
    }
}
Also used : FileAlterationMonitor(org.apache.commons.io.monitor.FileAlterationMonitor) DefaultTopologyService(org.apache.knox.gateway.services.topology.impl.DefaultTopologyService) HashMap(java.util.HashMap) TopologyEvent(org.apache.knox.gateway.topology.TopologyEvent) Topology(org.apache.knox.gateway.topology.Topology) DefaultTopologyService(org.apache.knox.gateway.services.topology.impl.DefaultTopologyService) File(java.io.File) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Topology (org.apache.knox.gateway.topology.Topology)52 Test (org.junit.Test)36 HashMap (java.util.HashMap)23 Service (org.apache.knox.gateway.topology.Service)22 File (java.io.File)20 Provider (org.apache.knox.gateway.topology.Provider)20 GatewayConfig (org.apache.knox.gateway.config.GatewayConfig)17 DefaultGatewayServices (org.apache.knox.gateway.services.DefaultGatewayServices)10 DeploymentContext (org.apache.knox.gateway.deploy.DeploymentContext)9 ServiceLifecycleException (org.apache.knox.gateway.services.ServiceLifecycleException)9 WebArchive (org.jboss.shrinkwrap.api.spec.WebArchive)9 IOException (java.io.IOException)8 GatewayTestConfig (org.apache.knox.gateway.GatewayTestConfig)8 URL (java.net.URL)7 TopologyService (org.apache.knox.gateway.services.topology.TopologyService)7 Application (org.apache.knox.gateway.topology.Application)7 Param (org.apache.knox.gateway.topology.Param)7 EnterpriseArchive (org.jboss.shrinkwrap.api.spec.EnterpriseArchive)7 Document (org.w3c.dom.Document)7 Digester (org.apache.commons.digester3.Digester)6