From: Olivier Lamy Date: Sun, 22 Dec 2013 12:25:43 +0000 (+0000) Subject: [MRM-1750] Add remote repository health check X-Git-Tag: archiva-2.0.0-RC1~69 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=0c9fab3b3bc6d439e1660a8e1ef06b85de8889b9;p=archiva.git [MRM-1750] Add remote repository health check add unit test git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1552982 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml index e58c40a36..99e37c8ee 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml @@ -402,18 +402,26 @@ ${tomcat7Version} test + org.apache.archiva archiva-metadata-consumer test + + javax.servlet + javax.servlet-api + provided + + diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRemoteRepositoriesService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRemoteRepositoriesService.java index b48bccfb2..38afc6758 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRemoteRepositoriesService.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRemoteRepositoriesService.java @@ -37,6 +37,8 @@ import org.apache.maven.wagon.authorization.AuthorizationException; import org.apache.maven.wagon.providers.http.AbstractHttpClientWagon; import org.apache.maven.wagon.providers.http.HttpConfiguration; import org.apache.maven.wagon.providers.http.HttpMethodConfiguration; +import org.apache.maven.wagon.providers.http.HttpWagon; +import org.apache.maven.wagon.repository.Repository; import org.springframework.stereotype.Service; import javax.inject.Inject; @@ -170,7 +172,7 @@ public class DefaultRemoteRepositoriesService wagon.setReadTimeout( remoteRepository.getRemoteDownloadTimeout() * 1000 ); wagon.setTimeout( remoteRepository.getTimeout() * 1000 ); - + HttpWagon foo; if ( wagon instanceof AbstractHttpClientWagon ) { HttpConfiguration httpConfiguration = new HttpConfiguration(); @@ -181,37 +183,14 @@ public class DefaultRemoteRepositoriesService AbstractHttpClientWagon.class.cast( wagon ).setHttpConfiguration( httpConfiguration ); } + wagon.connect( new Repository( remoteRepository.getId(), remoteRepository.getUrl() ) ); + // we only check connectivity as remote repo can be empty wagon.getFileList( "/" ); return Boolean.TRUE; } - catch ( RepositoryAdminException e ) - { - throw new ArchivaRestServiceException( e.getMessage(), - Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e ); - } - catch ( MalformedURLException e ) - { - throw new ArchivaRestServiceException( e.getMessage(), - Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e ); - } - catch ( WagonFactoryException e ) - { - throw new ArchivaRestServiceException( e.getMessage(), - Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e ); - } - catch ( TransferFailedException e ) - { - throw new ArchivaRestServiceException( e.getMessage(), - Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e ); - } - catch ( ResourceDoesNotExistException e ) - { - throw new ArchivaRestServiceException( e.getMessage(), - Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e ); - } - catch ( AuthorizationException e ) + catch ( Exception e ) { throw new ArchivaRestServiceException( e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e ); diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ManagedRepositoriesServiceTest.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ManagedRepositoriesServiceTest.java index 38e12302b..a49995457 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ManagedRepositoriesServiceTest.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ManagedRepositoriesServiceTest.java @@ -167,4 +167,6 @@ public class ManagedRepositoriesServiceTest deleteTestRepo( testRepoId ); } + + } diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/RemoteRepositoriesServiceTest.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/RemoteRepositoriesServiceTest.java index c2d0a0d77..4d7d78c21 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/RemoteRepositoriesServiceTest.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/RemoteRepositoriesServiceTest.java @@ -152,5 +152,4 @@ public class RemoteRepositoriesServiceTest "cool repo" ); } - } diff --git a/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/DownloadSnapshotTest.java b/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/DownloadSnapshotTest.java deleted file mode 100644 index 4da67a6e5..000000000 --- a/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/DownloadSnapshotTest.java +++ /dev/null @@ -1,137 +0,0 @@ -package org.apache.archiva; - -/* - * 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. - */ - -import org.apache.archiva.admin.model.beans.ManagedRepository; -import org.apache.archiva.redback.rest.api.services.RoleManagementService; -import org.apache.archiva.remotedownload.AbstractDownloadTest; -import org.apache.archiva.rest.api.services.ManagedRepositoriesService; -import org.apache.archiva.security.common.ArchivaRoleConstants; -import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner; -import org.apache.commons.io.FileUtils; -import org.apache.maven.wagon.providers.http.HttpWagon; -import org.apache.maven.wagon.repository.Repository; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.File; -import java.util.List; -import java.util.zip.ZipEntry; -import java.util.zip.ZipFile; - -/** - * @author Olivier Lamy - */ -@RunWith( ArchivaBlockJUnit4ClassRunner.class ) -public class DownloadSnapshotTest - extends AbstractDownloadTest -{ - protected Logger log = LoggerFactory.getLogger( getClass() ); - - @BeforeClass - public static void setAppServerBase() - { - previousAppServerBase = System.getProperty( "appserver.base" ); - System.setProperty( "appserver.base", "target/" + DownloadSnapshotTest.class.getName() ); - } - - - @AfterClass - public static void resetAppServerBase() - { - System.setProperty( "appserver.base", previousAppServerBase ); - } - - protected String getSpringConfigLocation() - { - return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-artifacts-download.xml"; - } - - - @Test - public void downloadSNAPSHOT() - throws Exception - { - - File tmpIndexDir = new File( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex" ); - if ( tmpIndexDir.exists() ) - { - FileUtils.deleteDirectory( tmpIndexDir ); - } - String id = Long.toString( System.currentTimeMillis() ); - ManagedRepository managedRepository = new ManagedRepository(); - managedRepository.setId( id ); - managedRepository.setName( "name of " + id ); - managedRepository.setLocation( System.getProperty( "basedir" ) + "/src/test/repositories/snapshot-repo" ); - managedRepository.setIndexDirectory( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex/" + id ); - - ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService(); - - if ( managedRepositoriesService.getManagedRepository( id ) != null ) - { - managedRepositoriesService.deleteManagedRepository( id, false ); - } - - getManagedRepositoriesService().addManagedRepository( managedRepository ); - - RoleManagementService roleManagementService = getRoleManagementService( authorizationHeader ); - - if ( !roleManagementService.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, - id ) ) - { - roleManagementService.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, id ); - } - - getUserService( authorizationHeader ).createGuestUser(); - roleManagementService.assignRole( ArchivaRoleConstants.TEMPLATE_GUEST, "guest" ); - - roleManagementService.assignTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, id, - "guest" ); - - getUserService( authorizationHeader ).removeFromCache( "guest" ); - - File file = new File( "target/archiva-model-1.4-M4-SNAPSHOT.jar" ); - if ( file.exists() ) - { - file.delete(); - } - - HttpWagon httpWagon = new HttpWagon(); - httpWagon.connect( new Repository( "foo", "http://localhost:" + port ) ); - - httpWagon.get( "/repository/"+ id +"/org/apache/archiva/archiva-model/1.4-M4-SNAPSHOT/archiva-model-1.4-M4-SNAPSHOT.jar", file ); - - ZipFile zipFile = new ZipFile( file ); - List entries = getZipEntriesNames( zipFile ); - ZipEntry zipEntry = zipFile.getEntry( "org/apache/archiva/model/ArchivaArtifact.class" ); - assertNotNull( "cannot find zipEntry org/apache/archiva/model/ArchivaArtifact.class, entries: " + entries + ", content is: " - + FileUtils.readFileToString( file ), zipEntry ); - zipFile.close(); - file.deleteOnExit(); - - - - } - -} diff --git a/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/DownloadArtifactsTest.java b/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/DownloadArtifactsTest.java index 105c6d035..4775a88d5 100644 --- a/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/DownloadArtifactsTest.java +++ b/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/DownloadArtifactsTest.java @@ -124,8 +124,6 @@ public class DownloadArtifactsTest this.redirectPort = redirectServer.getConnectors()[0].getLocalPort(); log.info( "redirect server port {}", redirectPort ); - - } @After diff --git a/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/DownloadSnapshotTest.java b/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/DownloadSnapshotTest.java new file mode 100644 index 000000000..9b969ede9 --- /dev/null +++ b/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/DownloadSnapshotTest.java @@ -0,0 +1,136 @@ +package org.apache.archiva.remotedownload; + +/* + * 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. + */ + +import org.apache.archiva.admin.model.beans.ManagedRepository; +import org.apache.archiva.redback.rest.api.services.RoleManagementService; +import org.apache.archiva.rest.api.services.ManagedRepositoriesService; +import org.apache.archiva.security.common.ArchivaRoleConstants; +import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner; +import org.apache.commons.io.FileUtils; +import org.apache.maven.wagon.providers.http.HttpWagon; +import org.apache.maven.wagon.repository.Repository; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.util.List; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +/** + * @author Olivier Lamy + */ +@RunWith( ArchivaBlockJUnit4ClassRunner.class ) +public class DownloadSnapshotTest + extends AbstractDownloadTest +{ + protected Logger log = LoggerFactory.getLogger( getClass() ); + + @BeforeClass + public static void setAppServerBase() + { + previousAppServerBase = System.getProperty( "appserver.base" ); + System.setProperty( "appserver.base", "target/" + DownloadSnapshotTest.class.getName() ); + } + + + @AfterClass + public static void resetAppServerBase() + { + System.setProperty( "appserver.base", previousAppServerBase ); + } + + protected String getSpringConfigLocation() + { + return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-artifacts-download.xml"; + } + + + @Test + public void downloadSNAPSHOT() + throws Exception + { + + File tmpIndexDir = new File( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex" ); + if ( tmpIndexDir.exists() ) + { + FileUtils.deleteDirectory( tmpIndexDir ); + } + String id = Long.toString( System.currentTimeMillis() ); + ManagedRepository managedRepository = new ManagedRepository(); + managedRepository.setId( id ); + managedRepository.setName( "name of " + id ); + managedRepository.setLocation( System.getProperty( "basedir" ) + "/src/test/repositories/snapshot-repo" ); + managedRepository.setIndexDirectory( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex/" + id ); + + ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService(); + + if ( managedRepositoriesService.getManagedRepository( id ) != null ) + { + managedRepositoriesService.deleteManagedRepository( id, false ); + } + + getManagedRepositoriesService().addManagedRepository( managedRepository ); + + RoleManagementService roleManagementService = getRoleManagementService( authorizationHeader ); + + if ( !roleManagementService.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, + id ) ) + { + roleManagementService.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, id ); + } + + getUserService( authorizationHeader ).createGuestUser(); + roleManagementService.assignRole( ArchivaRoleConstants.TEMPLATE_GUEST, "guest" ); + + roleManagementService.assignTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, id, + "guest" ); + + getUserService( authorizationHeader ).removeFromCache( "guest" ); + + File file = new File( "target/archiva-model-1.4-M4-SNAPSHOT.jar" ); + if ( file.exists() ) + { + file.delete(); + } + + HttpWagon httpWagon = new HttpWagon(); + httpWagon.connect( new Repository( "foo", "http://localhost:" + port ) ); + + httpWagon.get( "/repository/"+ id +"/org/apache/archiva/archiva-model/1.4-M4-SNAPSHOT/archiva-model-1.4-M4-SNAPSHOT.jar", file ); + + ZipFile zipFile = new ZipFile( file ); + List entries = getZipEntriesNames( zipFile ); + ZipEntry zipEntry = zipFile.getEntry( "org/apache/archiva/model/ArchivaArtifact.class" ); + assertNotNull( "cannot find zipEntry org/apache/archiva/model/ArchivaArtifact.class, entries: " + entries + ", content is: " + + FileUtils.readFileToString( file ), zipEntry ); + zipFile.close(); + file.deleteOnExit(); + + + + } + +} diff --git a/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/RemoteRepositoryConnectivityCheckTest.java b/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/RemoteRepositoryConnectivityCheckTest.java new file mode 100644 index 000000000..c882d8816 --- /dev/null +++ b/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/RemoteRepositoryConnectivityCheckTest.java @@ -0,0 +1,142 @@ +package org.apache.archiva; + +import com.google.common.io.Files; +import org.apache.archiva.admin.model.beans.RemoteRepository; +import org.apache.archiva.remotedownload.AbstractDownloadTest; +import org.apache.archiva.rest.api.services.RemoteRepositoriesService; +import org.apache.catalina.startup.Tomcat; +import org.apache.commons.io.FileUtils; +import org.apache.cxf.jaxrs.client.WebClient; +import org.eclipse.jetty.server.Handler; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.handler.ContextHandler; +import org.eclipse.jetty.server.handler.ContextHandlerCollection; +import org.eclipse.jetty.server.handler.DefaultHandler; +import org.eclipse.jetty.server.handler.HandlerList; +import org.eclipse.jetty.server.handler.ResourceHandler; +import org.eclipse.jetty.servlet.DefaultServlet; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.servlet.ServletHolder; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.File; + +/** + * @author Olivier Lamy + */ +public class RemoteRepositoryConnectivityCheckTest + extends AbstractDownloadTest +{ + + @BeforeClass + public static void setAppServerBase() + { + previousAppServerBase = System.getProperty( "appserver.base" ); + System.setProperty( "appserver.base", "target/" + RemoteRepositoryConnectivityCheckTest.class.getName() ); + } + + + @AfterClass + public static void resetAppServerBase() + { + System.setProperty( "appserver.base", previousAppServerBase ); + } + + protected String getSpringConfigLocation() + { + return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-artifacts-download.xml"; + } + + @Test + public void checkRemoteConnectivity() + throws Exception + { + + Server repoServer = + buildStaticServer( new File( System.getProperty( "basedir" ) + "/src/test/repositories/test-repo" ) ); + repoServer.start(); + + RemoteRepositoriesService service = getRemoteRepositoriesService(); + + WebClient.client( service ).header( "Authorization", authorizationHeader ); + + try + { + + int repoServerPort = repoServer.getConnectors()[0].getLocalPort(); + + RemoteRepository repo = getRemoteRepository(); + + repo.setUrl( "http://localhost:" + repoServerPort ); + + service.addRemoteRepository( repo ); + + service.checkRemoteConnectivity( repo.getId() ); + } + finally + { + service.deleteRemoteRepository( "id-new" ); + repoServer.stop(); + } + } + + @Test + public void checkRemoteConnectivityEmptyRemote() + throws Exception + { + + File tmpDir = Files.createTempDir(); + Server repoServer = buildStaticServer( tmpDir ); + repoServer.start(); + + RemoteRepositoriesService service = getRemoteRepositoriesService(); + + WebClient.client( service ).header( "Authorization", authorizationHeader ); + + try + { + + int repoServerPort = repoServer.getConnectors()[0].getLocalPort(); + + RemoteRepository repo = getRemoteRepository(); + + repo.setUrl( "http://localhost:" + repoServerPort ); + + service.addRemoteRepository( repo ); + + service.checkRemoteConnectivity( repo.getId() ); + } + finally + { + service.deleteRemoteRepository( "id-new" ); + FileUtils.deleteQuietly( tmpDir ); + repoServer.stop(); + } + } + + protected Server buildStaticServer( File path ) + { + Server repoServer = new Server( 0 ); + + ResourceHandler resourceHandler = new ResourceHandler(); + resourceHandler.setDirectoriesListed( true ); + resourceHandler.setWelcomeFiles( new String[]{ "index.html" } ); + resourceHandler.setResourceBase( path.getAbsolutePath() ); + + HandlerList handlers = new HandlerList(); + handlers.setHandlers( new Handler[]{ resourceHandler, new DefaultHandler() } ); + repoServer.setHandler( handlers ); + + return repoServer; + } + + + RemoteRepository getRemoteRepository() + { + return new RemoteRepository( "id-new", "new one", "http://foo.com", "default", "foo", "foopassword", 120, + "cool repo" ); + } + +}