You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@knox.apache.org by km...@apache.org on 2013/11/27 00:16:37 UTC
git commit: KNOX-210: Create a functional test template
Updated Branches:
refs/heads/master ef3ec3546 -> 184ee06e1
KNOX-210: Create a functional test template
Project: http://git-wip-us.apache.org/repos/asf/incubator-knox/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-knox/commit/184ee06e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-knox/tree/184ee06e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-knox/diff/184ee06e
Branch: refs/heads/master
Commit: 184ee06e10def9be34be9feaa2e864080edb92b0
Parents: ef3ec35
Author: Kevin Minder <ke...@hortonworks.com>
Authored: Tue Nov 26 18:16:29 2013 -0500
Committer: Kevin Minder <ke...@hortonworks.com>
Committed: Tue Nov 26 18:16:29 2013 -0500
----------------------------------------------------------------------
.../apache/hadoop/gateway/GatewayServer.java | 10 +-
.../hadoop/gateway/GatewaySampleFuncTest.java | 211 +++++++++++++++++++
.../TestProviderDeploymentContributor.java | 46 ++++
.../TestServiceDeploymentContributor.java | 49 +++++
.../gateway/TestServiceDispatchFilter.java | 44 ++++
...gateway.deploy.ProviderDeploymentContributor | 19 ++
....gateway.deploy.ServiceDeploymentContributor | 19 ++
.../gateway/GatewaySampleFuncTest/users.ldif | 36 ++++
8 files changed, 431 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/184ee06e/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayServer.java
----------------------------------------------------------------------
diff --git a/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayServer.java b/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayServer.java
index e2f4f36..5db8ff5 100644
--- a/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayServer.java
+++ b/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayServer.java
@@ -175,9 +175,13 @@ public class GatewayServer {
log.startingGateway();
server = new GatewayServer( config );
synchronized ( server ) {
- if (services == null) {
- services = svcs;
- }
+ //KM[ Commented this out because is causes problems with
+ // multiple services instance used in a single test process.
+ // I'm not sure what drive including this check though.
+ //if (services == null) {
+ services = svcs;
+ //}
+ //KM]
services.start();
DeploymentFactory.setGatewayServices(services);
server.start();
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/184ee06e/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewaySampleFuncTest.java
----------------------------------------------------------------------
diff --git a/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewaySampleFuncTest.java b/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewaySampleFuncTest.java
new file mode 100644
index 0000000..c55fd0a
--- /dev/null
+++ b/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewaySampleFuncTest.java
@@ -0,0 +1,211 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.gateway;
+
+import com.mycila.xmltool.XMLDoc;
+import com.mycila.xmltool.XMLTag;
+import org.apache.commons.io.FileUtils;
+import org.apache.hadoop.gateway.config.GatewayConfig;
+import org.apache.hadoop.gateway.security.EmbeddedApacheDirectoryServer;
+import org.apache.hadoop.gateway.services.DefaultGatewayServices;
+import org.apache.hadoop.gateway.services.ServiceLifecycleException;
+import org.apache.hadoop.test.log.NoOpAppender;
+import org.apache.http.HttpStatus;
+import org.apache.log4j.Appender;
+import org.hamcrest.MatcherAssert;
+import org.hamcrest.Matchers;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.ServerSocket;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+
+import static com.jayway.restassured.RestAssured.given;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.junit.Assert.assertThat;
+
+public class GatewaySampleFuncTest {
+
+ private static Class RESOURCE_BASE_CLASS = GatewaySampleFuncTest.class;
+ private static Logger LOG = LoggerFactory.getLogger( GatewaySampleFuncTest.class );
+
+ public static Enumeration<Appender> appenders;
+ public static GatewayConfig config;
+ public static GatewayServer gateway;
+ public static String gatewayUrl;
+ public static String clusterUrl;
+ public static EmbeddedApacheDirectoryServer ldap;
+
+ @BeforeClass
+ public static void setupSuite() throws Exception {
+ //appenders = NoOpAppender.setUp();
+ setupLdap();
+ setupGateway();
+ }
+
+ @AfterClass
+ public static void cleanupSuite() throws Exception {
+ gateway.stop();
+ ldap.stop();
+ //FileUtils.deleteQuietly( new File( config.getGatewayHomeDir() ) );
+ //NoOpAppender.tearDown( appenders );
+ }
+
+ public static void setupLdap() throws Exception {
+ URL usersUrl = getResourceUrl( "users.ldif" );
+ int port = findFreePort();
+ ldap = new EmbeddedApacheDirectoryServer( "dc=hadoop,dc=apache,dc=org", null, port );
+ ldap.start();
+ ldap.loadLdif( usersUrl );
+ LOG.info( "LDAP port = " + ldap.getTransport().getPort() );
+ }
+
+ public static void setupGateway() throws IOException {
+
+ File targetDir = new File( System.getProperty( "user.dir" ), "target" );
+ File gatewayDir = new File( targetDir, "gateway-home-" + UUID.randomUUID() );
+ gatewayDir.mkdirs();
+
+ GatewayTestConfig testConfig = new GatewayTestConfig();
+ config = testConfig;
+ testConfig.setGatewayHomeDir( gatewayDir.getAbsolutePath() );
+ testConfig.setDeploymentDir( "clusters" );
+
+ File deployDir = new File( gatewayDir, testConfig.getDeploymentDir() );
+ deployDir.mkdirs();
+
+ File descriptor = new File( deployDir, "test-cluster.xml" );
+ FileOutputStream stream = new FileOutputStream( descriptor );
+ createTopology().toStream( stream );
+ stream.close();
+
+ DefaultGatewayServices srvcs = new DefaultGatewayServices();
+ Map<String,String> options = new HashMap<String,String>();
+ options.put( "persist-master", "false" );
+ options.put( "master", "password" );
+ try {
+ srvcs.init( testConfig, options );
+ } catch ( ServiceLifecycleException e ) {
+ e.printStackTrace(); // I18N not required.
+ }
+ gateway = GatewayServer.startGateway( testConfig, srvcs );
+ MatcherAssert.assertThat( "Failed to start gateway.", gateway, notNullValue() );
+
+ LOG.info( "Gateway port = " + gateway.getAddresses()[ 0 ].getPort() );
+
+ gatewayUrl = "http://localhost:" + gateway.getAddresses()[0].getPort() + "/" + config.getGatewayPath();
+ clusterUrl = gatewayUrl + "/test-cluster";
+ }
+
+ private static XMLTag createTopology() {
+ XMLTag xml = XMLDoc.newDocument( true )
+ .addRoot( "topology" )
+ .addTag( "gateway" )
+ .addTag( "provider" )
+ .addTag( "role" ).addText( "authentication" )
+ .addTag( "enabled" ).addText( "true" )
+ .addTag( "param" )
+ .addTag( "name" ).addText( "main.ldapRealm" )
+ .addTag( "value" ).addText( "org.apache.shiro.realm.ldap.JndiLdapRealm" ).gotoParent()
+ .addTag( "param" )
+ .addTag( "name" ).addText( "main.ldapRealm.userDnTemplate" )
+ .addTag( "value" ).addText( "uid={0},ou=people,dc=hadoop,dc=apache,dc=org" ).gotoParent()
+ .addTag( "param" )
+ .addTag( "name" ).addText( "main.ldapRealm.contextFactory.url" )
+ .addTag( "value" ).addText( "ldap://localhost:" + ldap.getTransport().getPort() ).gotoParent()
+ .addTag( "param" )
+ .addTag( "name" ).addText( "main.ldapRealm.contextFactory.authenticationMechanism" )
+ .addTag( "value" ).addText( "simple" ).gotoParent()
+ .addTag( "param" )
+ .addTag( "name" ).addText( "urls./**" )
+ .addTag( "value" ).addText( "authcBasic" ).gotoParent().gotoParent()
+ .addTag( "provider" )
+ .addTag( "role" ).addText( "identity-assertion" )
+ .addTag( "enabled" ).addText( "true" )
+ .addTag( "name" ).addText( "Pseudo" ).gotoParent()
+ .addTag( "provider" )
+ .gotoRoot()
+ .addTag( "service" )
+ .addTag( "role" ).addText( "test-service-role" )
+ .gotoRoot();
+ // System.out.println( "GATEWAY=" + xml.toString() );
+ return xml;
+ }
+
+ private static int findFreePort() throws IOException {
+ ServerSocket socket = new ServerSocket(0);
+ int port = socket.getLocalPort();
+ socket.close();
+ return port;
+ }
+
+ public static InputStream getResourceStream( String resource ) throws IOException {
+ return getResourceUrl( resource ).openStream();
+ }
+
+ public static URL getResourceUrl( String resource ) {
+ URL url = ClassLoader.getSystemResource( getResourceName( resource ) );
+ assertThat( "Failed to find test resource " + resource, url, Matchers.notNullValue() );
+ return url;
+ }
+
+ public static String getResourceName( String resource ) {
+ return getResourceBaseName() + resource;
+ }
+
+ public static String getResourceBaseName() {
+ return RESOURCE_BASE_CLASS.getName().replaceAll( "\\.", "/" ) + "/";
+ }
+
+ @Ignore
+ @Test
+ public void waitForManualTesting() throws IOException {
+ System.in.read();
+ }
+
+ @Test
+ public void testTestService() throws ClassNotFoundException {
+
+ String username = "guest";
+ String password = "guest-password";
+ String serviceUrl = clusterUrl + "/test-service-path/test-service-resource";
+ given()
+ //.log().all()
+ .auth().preemptive().basic( username, password )
+ .expect()
+ //.log().all()
+ .statusCode( HttpStatus.SC_OK )
+ .contentType( "text/plain" )
+ .body( is( "test-service-response" ) )
+ .when().get( serviceUrl );
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/184ee06e/gateway-test/src/test/java/org/apache/hadoop/gateway/TestProviderDeploymentContributor.java
----------------------------------------------------------------------
diff --git a/gateway-test/src/test/java/org/apache/hadoop/gateway/TestProviderDeploymentContributor.java b/gateway-test/src/test/java/org/apache/hadoop/gateway/TestProviderDeploymentContributor.java
new file mode 100644
index 0000000..9a47816
--- /dev/null
+++ b/gateway-test/src/test/java/org/apache/hadoop/gateway/TestProviderDeploymentContributor.java
@@ -0,0 +1,46 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.gateway;
+
+import org.apache.hadoop.gateway.deploy.DeploymentContext;
+import org.apache.hadoop.gateway.deploy.ProviderDeploymentContributorBase;
+import org.apache.hadoop.gateway.descriptor.FilterParamDescriptor;
+import org.apache.hadoop.gateway.descriptor.ResourceDescriptor;
+import org.apache.hadoop.gateway.topology.Provider;
+import org.apache.hadoop.gateway.topology.Service;
+
+import java.util.List;
+
+public class TestProviderDeploymentContributor extends ProviderDeploymentContributorBase {
+
+ @Override
+ public String getRole() {
+ return "test-provider-role";
+ }
+
+ @Override
+ public String getName() {
+ return "test-provider-name";
+ }
+
+ @Override
+ public void contributeFilter( DeploymentContext context, Provider provider, Service service, ResourceDescriptor resource, List<FilterParamDescriptor> params ) {
+ resource.addFilter().name( getName() ).role( getRole() ).impl( TestServiceDispatchFilter.class.getName() );
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/184ee06e/gateway-test/src/test/java/org/apache/hadoop/gateway/TestServiceDeploymentContributor.java
----------------------------------------------------------------------
diff --git a/gateway-test/src/test/java/org/apache/hadoop/gateway/TestServiceDeploymentContributor.java b/gateway-test/src/test/java/org/apache/hadoop/gateway/TestServiceDeploymentContributor.java
new file mode 100644
index 0000000..cad32ee
--- /dev/null
+++ b/gateway-test/src/test/java/org/apache/hadoop/gateway/TestServiceDeploymentContributor.java
@@ -0,0 +1,49 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.gateway;
+
+import org.apache.hadoop.gateway.deploy.DeploymentContext;
+import org.apache.hadoop.gateway.deploy.ServiceDeploymentContributorBase;
+import org.apache.hadoop.gateway.descriptor.ResourceDescriptor;
+import org.apache.hadoop.gateway.topology.Service;
+
+public class TestServiceDeploymentContributor extends ServiceDeploymentContributorBase {
+
+ @Override
+ public String getRole() {
+ return "test-service-role";
+ }
+
+ @Override
+ public String getName() {
+ return "test-service-name";
+ }
+
+ @Override
+ public void contributeService( DeploymentContext context, Service service ) throws Exception {
+ ResourceDescriptor resource = context.getGatewayDescriptor().addResource();
+ resource.role( service.getRole() );
+ resource.pattern( "test-service-path/**?**" );
+ addAuthenticationFilter( context, service, resource );
+ addIdentityAssertionFilter( context, service, resource );
+ addAuthorizationFilter( context, service, resource );
+ //addRewriteFilter( context, service, resource, null ); // This shouldn't be included for in-processes services.
+ context.contributeFilter( service, resource, "test-provider-role", "test-provider-name", null );
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/184ee06e/gateway-test/src/test/java/org/apache/hadoop/gateway/TestServiceDispatchFilter.java
----------------------------------------------------------------------
diff --git a/gateway-test/src/test/java/org/apache/hadoop/gateway/TestServiceDispatchFilter.java b/gateway-test/src/test/java/org/apache/hadoop/gateway/TestServiceDispatchFilter.java
new file mode 100644
index 0000000..62ca915
--- /dev/null
+++ b/gateway-test/src/test/java/org/apache/hadoop/gateway/TestServiceDispatchFilter.java
@@ -0,0 +1,44 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.gateway;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import java.io.IOException;
+
+public class TestServiceDispatchFilter implements Filter {
+
+ @Override
+ public void init( FilterConfig filterConfig ) throws ServletException {
+ }
+
+ @Override
+ public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain ) throws IOException, ServletException {
+ response.setContentType( "text/plain" );
+ response.getWriter().write( "test-service-response" );
+ }
+
+ @Override
+ public void destroy() {
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/184ee06e/gateway-test/src/test/resources/META-INF/services/org.apache.hadoop.gateway.deploy.ProviderDeploymentContributor
----------------------------------------------------------------------
diff --git a/gateway-test/src/test/resources/META-INF/services/org.apache.hadoop.gateway.deploy.ProviderDeploymentContributor b/gateway-test/src/test/resources/META-INF/services/org.apache.hadoop.gateway.deploy.ProviderDeploymentContributor
new file mode 100644
index 0000000..fba3834
--- /dev/null
+++ b/gateway-test/src/test/resources/META-INF/services/org.apache.hadoop.gateway.deploy.ProviderDeploymentContributor
@@ -0,0 +1,19 @@
+##########################################################################
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##########################################################################
+
+org.apache.hadoop.gateway.TestProviderDeploymentContributor
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/184ee06e/gateway-test/src/test/resources/META-INF/services/org.apache.hadoop.gateway.deploy.ServiceDeploymentContributor
----------------------------------------------------------------------
diff --git a/gateway-test/src/test/resources/META-INF/services/org.apache.hadoop.gateway.deploy.ServiceDeploymentContributor b/gateway-test/src/test/resources/META-INF/services/org.apache.hadoop.gateway.deploy.ServiceDeploymentContributor
new file mode 100644
index 0000000..14e16ee
--- /dev/null
+++ b/gateway-test/src/test/resources/META-INF/services/org.apache.hadoop.gateway.deploy.ServiceDeploymentContributor
@@ -0,0 +1,19 @@
+##########################################################################
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##########################################################################
+
+org.apache.hadoop.gateway.TestServiceDeploymentContributor
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/184ee06e/gateway-test/src/test/resources/org/apache/hadoop/gateway/GatewaySampleFuncTest/users.ldif
----------------------------------------------------------------------
diff --git a/gateway-test/src/test/resources/org/apache/hadoop/gateway/GatewaySampleFuncTest/users.ldif b/gateway-test/src/test/resources/org/apache/hadoop/gateway/GatewaySampleFuncTest/users.ldif
new file mode 100644
index 0000000..fdd0296
--- /dev/null
+++ b/gateway-test/src/test/resources/org/apache/hadoop/gateway/GatewaySampleFuncTest/users.ldif
@@ -0,0 +1,36 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+version: 1
+
+# entry for a sample people container
+# please replace with site specific values
+dn: ou=people,dc=hadoop,dc=apache,dc=org
+objectclass:top
+objectclass:organizationalUnit
+ou: people
+
+# entry for a sample end user
+# please replace with site specific values
+dn: uid=guest,ou=people,dc=hadoop,dc=apache,dc=org
+objectclass:top
+objectclass:person
+objectclass:organizationalPerson
+objectclass:inetOrgPerson
+cn: Guest
+sn: User
+uid: guest
+userPassword:guest-password
\ No newline at end of file