use of org.apache.knox.gateway.topology.Topology in project knox by apache.
the class DefaultTopologyServiceTest method testSimpleDescriptorsTopologyGeneration.
/**
* KNOX-1014
*
* Test the lifecycle relationship between simple descriptors and topology files.
*
* N.B. This test depends on the DummyServiceDiscovery extension being configured:
* org.apache.knox.gateway.topology.discovery.test.extension.DummyServiceDiscovery
*/
@Test
public void testSimpleDescriptorsTopologyGeneration() throws Exception {
File dir = createDir();
File topologyDir = new File(dir, "topologies");
topologyDir.mkdirs();
File descriptorsDir = new File(dir, "descriptors");
descriptorsDir.mkdirs();
File sharedProvidersDir = new File(dir, "shared-providers");
sharedProvidersDir.mkdirs();
try {
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.replay(config);
provider.init(config, c);
provider.addTopologyChangeListener(topoListener);
provider.reloadTopologies();
// Add a simple descriptor to the descriptors dir to verify topology generation and loading (KNOX-1006)
AliasService aliasService = EasyMock.createNiceMock(AliasService.class);
EasyMock.expect(aliasService.getPasswordFromAliasForGateway(anyObject(String.class))).andReturn(null).anyTimes();
EasyMock.replay(aliasService);
DefaultTopologyService.DescriptorsMonitor dm = new DefaultTopologyService.DescriptorsMonitor(config, topologyDir, aliasService);
// Listener to simulate the topologies directory monitor, to notice when a topology has been deleted
provider.addTopologyChangeListener(new TestTopologyDeleteListener((DefaultTopologyService) provider));
// Write out the referenced provider config first
File provCfgFile = createFile(sharedProvidersDir, "ambari-cluster-policy.xml", "org/apache/knox/gateway/topology/file/ambari-cluster-policy.xml", System.currentTimeMillis());
try {
// Create the simple descriptor in the descriptors dir
File simpleDesc = createFile(descriptorsDir, "four.json", "org/apache/knox/gateway/topology/file/simple-topology-four.json", System.currentTimeMillis());
// Trigger the topology generation by noticing the simple descriptor
dm.onFileChange(simpleDesc);
// Load the generated topology
provider.reloadTopologies();
Collection<Topology> topologies = provider.getTopologies();
assertThat(topologies.size(), is(1));
Iterator<Topology> iterator = topologies.iterator();
Topology topology = iterator.next();
assertThat("four", is(topology.getName()));
int serviceCount = topology.getServices().size();
assertEquals("Expected the same number of services as are declared in the simple dscriptor.", 10, serviceCount);
// Overwrite the simple descriptor with a different set of services, and check that the changes are
// propagated to the associated topology
simpleDesc = createFile(descriptorsDir, "four.json", "org/apache/knox/gateway/topology/file/simple-descriptor-five.json", System.currentTimeMillis());
dm.onFileChange(simpleDesc);
provider.reloadTopologies();
topologies = provider.getTopologies();
topology = topologies.iterator().next();
assertNotEquals(serviceCount, topology.getServices().size());
assertEquals(6, topology.getServices().size());
// Delete the simple descriptor, and make sure that the associated topology file is deleted
simpleDesc.delete();
dm.onFileDelete(simpleDesc);
provider.reloadTopologies();
topologies = provider.getTopologies();
assertTrue(topologies.isEmpty());
// Delete a topology file, and make sure that the associated simple descriptor is deleted
// Overwrite the simple descriptor with a different set of services, and check that the changes are
// propagated to the associated topology
simpleDesc = createFile(descriptorsDir, "deleteme.json", "org/apache/knox/gateway/topology/file/simple-descriptor-five.json", System.currentTimeMillis());
dm.onFileChange(simpleDesc);
provider.reloadTopologies();
topologies = provider.getTopologies();
assertFalse(topologies.isEmpty());
topology = topologies.iterator().next();
assertEquals("deleteme", topology.getName());
File topologyFile = new File(topologyDir, topology.getName() + ".xml");
assertTrue(topologyFile.exists());
topologyFile.delete();
provider.reloadTopologies();
assertFalse("Simple descriptor should have been deleted because the associated topology was.", simpleDesc.exists());
} finally {
provCfgFile.delete();
}
} finally {
FileUtils.deleteQuietly(dir);
}
}
use of org.apache.knox.gateway.topology.Topology in project knox by apache.
the class PropertyTopologyBuilderTest method testBuildSuccessfulForTopologyProperty.
@Test
public void testBuildSuccessfulForTopologyProperty() {
PropertyTopologyBuilder propertyTopologyBuilder = new PropertyTopologyBuilder();
propertyTopologyBuilder.addProperty(new Property("topology.name", "topology"));
Topology topology = propertyTopologyBuilder.build();
assertThat(topology, notNullValue());
}
use of org.apache.knox.gateway.topology.Topology in project knox by apache.
the class TopologyResourceTest method testTopologyURLMethods.
@Test
public void testTopologyURLMethods() {
// Note: had to change method signature due to these tests. Changed methods to public and added
// HttpServletRequest argument.
HttpServletRequest request = EasyMock.createNiceMock(HttpServletRequest.class);
setDefaultExpectations(request);
setMockRequestHeader(request, X_Forwarded_Host, host);
setMockRequestHeader(request, X_Forwarded_Port, port);
setMockRequestHeader(request, X_Forwarded_Proto, proto);
setMockRequestHeader(request, X_Forwarded_Context, fullContext);
setMockRequestHeader(request, X_Forwarded_Server, server);
Topology t = EasyMock.createNiceMock(Topology.class);
EasyMock.expect(t.getName()).andReturn(topologyName).anyTimes();
GatewayConfig conf = EasyMock.createNiceMock(GatewayConfig.class);
EasyMock.expect(conf.getGatewayPath()).andReturn(gatewayPath).anyTimes();
EasyMock.replay(request, t, conf);
TopologiesResource res = new TopologiesResource();
String href = res.buildHref(t, request);
String uri = res.buildURI(t, conf, request);
assertThat(uri, containsString(proto));
assertThat(uri, containsString(host));
assertThat(uri, containsString(server));
assertThat(uri, containsString(port));
assertThat(uri, containsString(topologyName));
assert (uri.equals(expectedURI));
assertThat(href, containsString(proto));
assertThat(href, containsString(host));
assertThat(href, containsString(server));
assertThat(href, containsString(port));
assertThat(href, containsString(fullContext));
assert (href.equals(expectedHref));
// Test 2 - No Protocol Header
EasyMock.reset(request);
setDefaultExpectations(request);
setMockRequestHeader(request, X_Forwarded_Host, host);
setMockRequestHeader(request, X_Forwarded_Port, port);
setMockRequestHeader(request, X_Forwarded_Context, fullContext);
setMockRequestHeader(request, X_Forwarded_Server, server);
EasyMock.replay(request);
String test2URI = expectedURI.replace(proto, reqProto);
String test2href = expectedHref.replace(proto, reqProto);
assert (res.buildURI(t, conf, request).equals(test2URI));
assert (res.buildHref(t, request).equals(test2href));
// Test 3 - No port in host Header
EasyMock.reset(request);
setDefaultExpectations(request);
setMockRequestHeader(request, X_Forwarded_Host, server);
setMockRequestHeader(request, X_Forwarded_Port, port);
setMockRequestHeader(request, X_Forwarded_Context, fullContext);
setMockRequestHeader(request, X_Forwarded_Server, server);
setMockRequestHeader(request, X_Forwarded_Proto, proto);
EasyMock.replay(request);
assert (res.buildURI(t, conf, request).equals(expectedURI));
assert (res.buildHref(t, request).equals(expectedHref));
// Test 4 - server & no host Header
EasyMock.reset(request);
setDefaultExpectations(request);
setMockRequestHeader(request, X_Forwarded_Server, server);
setMockRequestHeader(request, X_Forwarded_Port, port);
setMockRequestHeader(request, X_Forwarded_Context, fullContext);
setMockRequestHeader(request, X_Forwarded_Proto, proto);
EasyMock.replay(request);
assert (res.buildURI(t, conf, request).equals(expectedURI));
assert (res.buildHref(t, request).equals(expectedHref));
// Test 5 - no server & no host Header
EasyMock.reset(request);
setDefaultExpectations(request);
setMockRequestHeader(request, X_Forwarded_Port, port);
setMockRequestHeader(request, X_Forwarded_Context, fullContext);
setMockRequestHeader(request, X_Forwarded_Proto, proto);
EasyMock.replay(request);
String test5URI = expectedURI.replace(server, reqServer);
String test5href = expectedHref.replace(server, reqServer);
assertEquals(res.buildURI(t, conf, request), test5URI);
assertEquals(res.buildHref(t, request), test5href);
// Test 6 - no port, no server & no host Header
EasyMock.reset(request);
setDefaultExpectations(request);
setMockRequestHeader(request, X_Forwarded_Context, fullContext);
setMockRequestHeader(request, X_Forwarded_Proto, proto);
EasyMock.replay(request);
String test6URI = expectedURI.replace(server, reqServer).replace(port, reqPort);
String test6href = expectedHref.replace(server, reqServer).replace(port, reqPort);
assertEquals(res.buildURI(t, conf, request), (test6URI));
assertEquals(res.buildHref(t, request), (test6href));
// Test 7 - No Context
EasyMock.reset(request);
setDefaultExpectations(request);
setMockRequestHeader(request, X_Forwarded_Host, host);
setMockRequestHeader(request, X_Forwarded_Port, port);
setMockRequestHeader(request, X_Forwarded_Proto, proto);
setMockRequestHeader(request, X_Forwarded_Server, server);
EasyMock.replay(request);
String test7URI = expectedURI.replace(startContext, "");
String test7href = expectedHref.replace(startContext, "");
assertEquals(res.buildURI(t, conf, request), test7URI);
assertEquals(res.buildHref(t, request), test7href);
}
use of org.apache.knox.gateway.topology.Topology in project knox by apache.
the class HaProviderDeploymentContributorTest method testServiceLevelParamOverrides_SubsetProviderParams.
/**
* Verify a mixture of provider-level params and service-level params.
*/
@Test
public void testServiceLevelParamOverrides_SubsetProviderParams() throws Exception {
// Define some provider params
Map<String, String> providerParams = new HashMap<>();
// Specify all the possible params at the HaProvider level for TestRoleTwo
providerParams.put("TestRoleOne", "enabled=false;" + "maxRetryAttempts=5;" + "maxFailoverAttempts=4;" + "failoverSleep=40");
Provider haProvider = createHaProvider(providerParams);
// Define the topology content (e.g., services)
Collection<Service> topologyServices = new HashSet<>();
// Specify all the possible params in the TestRoleOne service level
Map<String, String> testRoleOneParams = new HashMap<>();
testRoleOneParams.put("enabled", "true");
testRoleOneParams.put("retrySleep", "60");
testRoleOneParams.put("zookeeperNamespace", "testRoleOneOverride");
testRoleOneParams.put("zookeeperEnsemble", "http://host3:2181,http://host4:2181");
// A service with all the params overriden
Service testRoleOneService = EasyMock.createNiceMock(Service.class);
EasyMock.expect(testRoleOneService.getRole()).andReturn("TestRoleOne").anyTimes();
EasyMock.expect(testRoleOneService.getName()).andReturn("TestRoleOneService").anyTimes();
EasyMock.expect(testRoleOneService.getParams()).andReturn(testRoleOneParams).anyTimes();
EasyMock.replay(testRoleOneService);
topologyServices.add(testRoleOneService);
Topology topology = EasyMock.createNiceMock(Topology.class);
EasyMock.expect(topology.getServices()).andReturn(topologyServices).anyTimes();
EasyMock.replay(topology);
WebArchive war = EasyMock.createNiceMock(WebArchive.class);
EasyMock.replay(war);
DeploymentContext context = new DescriptorCaptureDeploymentContext(topology, war);
// Invoke the contributor
HaProviderDeploymentContributor haPDC = new HaProviderDeploymentContributor();
haPDC.contributeProvider(context, haProvider);
HaDescriptor descriptor = context.getDescriptor("ha.provider.descriptor");
assertNotNull(descriptor);
assertEquals(1, descriptor.getServiceConfigs().size());
validateServiceHaConfig(descriptor.getServiceConfig("TestRoleOne"), true, 40, 4, 60, 5, "testRoleOneOverride", "http://host3:2181,http://host4:2181");
}
use of org.apache.knox.gateway.topology.Topology in project knox by apache.
the class HaProviderDeploymentContributorTest method testProviderLevelParams.
/**
* Basically, a backward-compatibility test to ensure that HaProvider service params specified ONLY at the provider
* level still work.
*/
@Test
public void testProviderLevelParams() throws Exception {
// Define some provider params
Map<String, String> providerParams = new HashMap<>();
// Specify all the possible params at the HaProvider level for TestRoleTwo
providerParams.put("TestRoleOne", "enabled=false;" + "maxRetryAttempts=5;" + "retrySleep=50;" + "maxFailoverAttempts=4;" + "failoverSleep=40;" + "zookeeperNamespace=testRoleOne;" + "zookeeperEnsemble=http://host1:2181,http://host2:2181");
Provider haProvider = createHaProvider(providerParams);
// Define the topology content (e.g., services)
Collection<Service> topologyServices = new HashSet<>();
// A service with no param overrides
Service testRoleOneService = EasyMock.createNiceMock(Service.class);
EasyMock.expect(testRoleOneService.getRole()).andReturn("TestRoleOne").anyTimes();
EasyMock.expect(testRoleOneService.getName()).andReturn("TestRoleOneService").anyTimes();
EasyMock.expect(testRoleOneService.getParams()).andReturn(Collections.emptyMap()).anyTimes();
EasyMock.replay(testRoleOneService);
topologyServices.add(testRoleOneService);
Topology topology = EasyMock.createNiceMock(Topology.class);
EasyMock.expect(topology.getServices()).andReturn(topologyServices).anyTimes();
EasyMock.replay(topology);
WebArchive war = EasyMock.createNiceMock(WebArchive.class);
EasyMock.replay(war);
DeploymentContext context = new DescriptorCaptureDeploymentContext(topology, war);
// Invoke the contributor
HaProviderDeploymentContributor haPDC = new HaProviderDeploymentContributor();
haPDC.contributeProvider(context, haProvider);
HaDescriptor descriptor = context.getDescriptor("ha.provider.descriptor");
assertNotNull(descriptor);
assertEquals(1, descriptor.getServiceConfigs().size());
validateServiceHaConfig(descriptor.getServiceConfig("TestRoleOne"), false, 40, 4, 50, 5, "testRoleOne", "http://host1:2181,http://host2:2181");
}
Aggregations