From: Olivier Lamy Date: Sun, 17 Apr 2022 11:31:02 +0000 (+1000) Subject: use testcontainers to run cassandra tests (#79) X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=e6a08c75177ac7fa05b21ddccc90e0036d69e55d;p=archiva.git use testcontainers to run cassandra tests (#79) * use testcontainers for cassandra test Signed-off-by: Olivier Lamy --- diff --git a/archiva-cli/pom.xml b/archiva-cli/pom.xml index 9359d81bb..88f3c27e0 100644 --- a/archiva-cli/pom.xml +++ b/archiva-cli/pom.xml @@ -91,6 +91,7 @@ shade + false apache-archiva-cli-${project.version} diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/test/test-settings.xml b/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/test/test-settings.xml index ed41e84d5..fcbb3649e 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/test/test-settings.xml +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/test/test-settings.xml @@ -24,7 +24,7 @@ under the License. xmlns="http://maven.apache.org/SETTINGS/1.0.0"> - external:* + external:*,!central local-mirror local-mirror @localRepositoryUrl@ @@ -60,6 +60,16 @@ under the License. true + + central + https://repo.maven.apache.org/maven2 + + true + + + false + + @@ -72,6 +82,16 @@ under the License. true + + central + https://repo.maven.apache.org/maven2 + + true + + + false + + diff --git a/archiva-modules/metadata/metadata-repository-api/src/test/java/org/apache/archiva/metadata/repository/AbstractMetadataRepositoryTest.java b/archiva-modules/metadata/metadata-repository-api/src/test/java/org/apache/archiva/metadata/repository/AbstractMetadataRepositoryTest.java index 52eb42195..4c351447b 100644 --- a/archiva-modules/metadata/metadata-repository-api/src/test/java/org/apache/archiva/metadata/repository/AbstractMetadataRepositoryTest.java +++ b/archiva-modules/metadata/metadata-repository-api/src/test/java/org/apache/archiva/metadata/repository/AbstractMetadataRepositoryTest.java @@ -234,20 +234,14 @@ public abstract class AbstractMetadataRepositoryTest { try ( RepositorySession session = getSessionFactory( ).createSession( ) ) { - tryAssert( ( ) -> { - assertThat( getRepository( ).getRootNamespaces( session, TEST_REPO_ID ) ).isNotNull( ).isEmpty( ); - } ); + tryAssert( ( ) -> assertThat( getRepository( ).getRootNamespaces( session, TEST_REPO_ID ) ).isNotNull( ).isEmpty( )); getRepository( ).updateNamespace( session, TEST_REPO_ID, TEST_NAMESPACE ); - tryAssert( ( ) -> { - assertThat( getRepository( ).getRootNamespaces( session, TEST_REPO_ID ) ).isNotNull( ).isNotEmpty( ).contains( - TEST_NAMESPACE ).hasSize( 1 ); - } ); + tryAssert( ( ) -> assertThat( getRepository( ).getRootNamespaces( session, TEST_REPO_ID ) ).isNotNull( ).isNotEmpty( ) + .contains( TEST_NAMESPACE ).hasSize( 1 )); getRepository( ).removeNamespace( session, TEST_REPO_ID, TEST_NAMESPACE ); - tryAssert( ( ) -> { - assertThat( getRepository( ).getRootNamespaces( session, TEST_REPO_ID ) ).isNotNull( ).isEmpty( ); - } ); + tryAssert( ( ) -> assertThat( getRepository( ).getRootNamespaces( session, TEST_REPO_ID ) ).isNotNull( ).isEmpty( )); } } diff --git a/archiva-modules/metadata/metadata-store-provider/metadata-store-cassandra/pom.xml b/archiva-modules/metadata/metadata-store-provider/metadata-store-cassandra/pom.xml index 4aee87311..fcdce8a99 100644 --- a/archiva-modules/metadata/metadata-store-provider/metadata-store-cassandra/pom.xml +++ b/archiva-modules/metadata/metadata-store-provider/metadata-store-cassandra/pom.xml @@ -44,12 +44,6 @@ org.apache.archiva metadata-repository-api - - org.apache.archiva - metadata-repository-api - tests - test - org.apache.archiva archiva-common @@ -79,7 +73,6 @@ metadata-model - jakarta.annotation jakarta.annotation-api @@ -119,11 +112,11 @@ ${datastax.driver.version} - - org.junit.jupiter - junit-jupiter-api + org.apache.archiva + metadata-repository-api + tests test @@ -165,158 +158,32 @@ org.apache.logging.log4j - log4j-1.2-api + log4j-core ${log4j.version} test - org.junit.jupiter - junit-jupiter-engine + org.testcontainers + cassandra + 1.17.1 test - - - - src/test/filtered-resources - true - - - - - - org.codehaus.mojo - build-helper-maven-plugin - - - reserve-ports - process-test-resources - - reserve-network-port - - - - cassandra.rpcPort - cassandra.storagePort - cassandra.stopPort - cassandra.jmxPort - cassandra.nativeTransportPort - - - - - - - org.codehaus.mojo - cassandra-maven-plugin - 3.7-SNAPSHOT - - - start-cassandra - pre-integration-test - - start - - - ${cassandra.nativeTransportPort} - ${cassandra.rpcPort} - ${cassandra.storagePort} - ${cassandra.stopPort} - ${cassandra.jmxPort} - false - false - 500 - true - INFO - false - - ${cassandra.jmxPort} - - - - - - stop-cassandra - post-integration-test - - stop - - - - - - org.slf4j - slf4j-simple - ${slf4j.version} - - - net.java.dev.jna - jna - 4.2.2 - - - org.apache.cassandra - cassandra-all - ${cassandraVersion} - - - - - org.apache.maven.plugins - maven-failsafe-plugin - 3.0.0-M5 - - - - integration-test - verify - - - - - - **/*Test.java - - - ${cassandra.nativeTransportPort} - 127.0.0.1 - cassandra - ${project.build.directory}/appserver-base - true - - false - false - ${project.build.outputDirectory} - - - - org.junit.jupiter - junit-jupiter-engine - ${junit.jupiter.version} - - - - - org.apache.maven.plugins maven-surefire-plugin - - - true + + cassandra + ${project.build.directory}/appserver-base + ${cassandraVersion} + @@ -326,7 +193,6 @@ num_tokens: 1 src/cassandra/** - src/test/resources/cassandra-test.yaml @@ -334,15 +200,4 @@ num_tokens: 1 - - - jdk9+ - - [1.9,) - - - true - - - diff --git a/archiva-modules/metadata/metadata-store-provider/metadata-store-cassandra/src/main/java/org/apache/archiva/metadata/repository/cassandra/DefaultCassandraArchivaManager.java b/archiva-modules/metadata/metadata-store-provider/metadata-store-cassandra/src/main/java/org/apache/archiva/metadata/repository/cassandra/DefaultCassandraArchivaManager.java index df53f600b..21df2ca15 100644 --- a/archiva-modules/metadata/metadata-store-provider/metadata-store-cassandra/src/main/java/org/apache/archiva/metadata/repository/cassandra/DefaultCassandraArchivaManager.java +++ b/archiva-modules/metadata/metadata-store-provider/metadata-store-cassandra/src/main/java/org/apache/archiva/metadata/repository/cassandra/DefaultCassandraArchivaManager.java @@ -182,7 +182,6 @@ public class DefaultCassandraArchivaManager hostNames.add( cassandraHost + ":" + cassandraPort ); configLoader = DriverConfigLoader.programmaticBuilder( ) - .withStringList( DefaultDriverOption.CONTACT_POINTS, hostNames ) .withInt( DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE, maxActive ) .withInt( DefaultDriverOption.CONNECTION_POOL_REMOTE_SIZE, maxActive ) diff --git a/archiva-modules/metadata/metadata-store-provider/metadata-store-cassandra/src/test/filtered-resources/META-INF/spring-context.xml b/archiva-modules/metadata/metadata-store-provider/metadata-store-cassandra/src/test/filtered-resources/META-INF/spring-context.xml deleted file mode 100644 index 49f80f869..000000000 --- a/archiva-modules/metadata/metadata-store-provider/metadata-store-cassandra/src/test/filtered-resources/META-INF/spring-context.xml +++ /dev/null @@ -1,29 +0,0 @@ - - - - - - - - - - \ No newline at end of file diff --git a/archiva-modules/metadata/metadata-store-provider/metadata-store-cassandra/src/test/java/org/apache/archiva/metadata/repository/cassandra/CassandraMetadataRepositoryTest.java b/archiva-modules/metadata/metadata-store-provider/metadata-store-cassandra/src/test/java/org/apache/archiva/metadata/repository/cassandra/CassandraMetadataRepositoryTest.java index c1e07a07a..ce85af8ad 100644 --- a/archiva-modules/metadata/metadata-store-provider/metadata-store-cassandra/src/test/java/org/apache/archiva/metadata/repository/cassandra/CassandraMetadataRepositoryTest.java +++ b/archiva-modules/metadata/metadata-store-provider/metadata-store-cassandra/src/test/java/org/apache/archiva/metadata/repository/cassandra/CassandraMetadataRepositoryTest.java @@ -27,13 +27,19 @@ import org.apache.archiva.metadata.repository.MetadataService; import org.apache.archiva.metadata.repository.RepositorySession; import org.apache.archiva.metadata.repository.RepositorySessionFactory; import org.apache.archiva.metadata.repository.cassandra.model.ProjectVersionMetadataModel; +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.extension.ExtendWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.testcontainers.containers.CassandraContainer; +import org.testcontainers.containers.output.Slf4jLogConsumer; +import org.testcontainers.utility.DockerImageName; import javax.inject.Inject; import javax.inject.Named; @@ -57,9 +63,13 @@ import static org.mockito.Mockito.when; */ @ExtendWith( SpringExtension.class ) @TestInstance( TestInstance.Lifecycle.PER_CLASS ) +@ContextConfiguration( locations = {"classpath*:/META-INF/spring-context.xml"} ) public class CassandraMetadataRepositoryTest extends AbstractMetadataRepositoryTest { + + private static final Logger LOGGER = LoggerFactory.getLogger( CassandraMetadataRepositoryTest.class ); + @Inject @Named(value = "archivaEntityManagerFactory#cassandra") CassandraArchivaManager cassandraArchivaManager; @@ -70,6 +80,19 @@ public class CassandraMetadataRepositoryTest RepositorySession session; + private static final CassandraContainer CASSANDRA = + new CassandraContainer(DockerImageName.parse("cassandra") + .withTag(System.getProperty("cassandraVersion","3.11.2"))); + + // because of @ExtendWith( SpringExtension.class ) @BeforeAll will not be executed before spring resolution so need to use this... + static { + LOGGER.info("initCassandra"); + CASSANDRA.withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger("org.apache.archiva.metadata.repository.cassandra.logs"))); + CASSANDRA.start(); + System.setProperty("cassandra.host", CASSANDRA.getHost()); + System.setProperty("cassandra.port", CASSANDRA.getMappedPort(9042).toString()); + } + long cTime; int testNum = 0; final AtomicBoolean clearedTables = new AtomicBoolean( false ); @@ -87,8 +110,15 @@ public class CassandraMetadataRepositoryTest return cmr; } + @AfterAll + public static void stopCassandra() + throws Exception { + CASSANDRA.close(); + } + @BeforeEach - public void setUp( TestInfo testInfo ) + @Override + public void setUp() throws Exception { cTime = System.currentTimeMillis( ); @@ -152,7 +182,7 @@ public class CassandraMetadataRepositoryTest @AfterEach - public void shutdown(TestInfo testInfo) + public void shutdown() throws Exception { clearReposAndNamespace( cassandraArchivaManager, clearedTables ); @@ -177,7 +207,8 @@ public class CassandraMetadataRepositoryTest cassandraArchivaManager.getLicenseFamilyName( ), cassandraArchivaManager.getDependencyFamilyName( ) ); - CompletableFuture.allOf( tables.stream( ).map( table -> session.executeAsync( truncate( table ).build( ) ) ) + CompletableFuture.allOf(tables.stream() + .map(table -> session.executeAsync(truncate(table).build())) .map( CompletionStage::toCompletableFuture ).collect( Collectors.toList( ) ).toArray( new CompletableFuture[0] ) ) .whenComplete( ( c, e ) -> { if ( clearedFlag != null ) clearedFlag.set( true ); diff --git a/archiva-modules/metadata/metadata-store-provider/metadata-store-cassandra/src/test/java/org/apache/archiva/metadata/repository/cassandra/RepositoriesNamespaceTest.java b/archiva-modules/metadata/metadata-store-provider/metadata-store-cassandra/src/test/java/org/apache/archiva/metadata/repository/cassandra/RepositoriesNamespaceTest.java index 7bcc68758..c2d92ad15 100644 --- a/archiva-modules/metadata/metadata-store-provider/metadata-store-cassandra/src/test/java/org/apache/archiva/metadata/repository/cassandra/RepositoriesNamespaceTest.java +++ b/archiva-modules/metadata/metadata-store-provider/metadata-store-cassandra/src/test/java/org/apache/archiva/metadata/repository/cassandra/RepositoriesNamespaceTest.java @@ -22,7 +22,9 @@ package org.apache.archiva.metadata.repository.cassandra; import org.apache.archiva.metadata.model.ProjectMetadata; import org.apache.archiva.metadata.repository.cassandra.model.Namespace; import org.apache.archiva.metadata.repository.cassandra.model.Repository; +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -30,6 +32,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.testcontainers.containers.CassandraContainer; +import org.testcontainers.containers.output.Slf4jLogConsumer; +import org.testcontainers.utility.DockerImageName; import javax.inject.Inject; import javax.inject.Named; @@ -40,19 +45,37 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Olivier Lamy */ @ExtendWith( SpringExtension.class ) -@ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } ) +@ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml" } ) public class RepositoriesNamespaceTest { - private Logger logger = LoggerFactory.getLogger( getClass() ); + private static final Logger LOGGER = LoggerFactory.getLogger( RepositoriesNamespaceTest.class ); + + private static final CassandraContainer CASSANDRA = + new CassandraContainer(DockerImageName.parse("cassandra") + .withTag(System.getProperty("cassandraVersion","3.11.2"))); @Inject @Named( value = "archivaEntityManagerFactory#cassandra" ) CassandraArchivaManager cassandraArchivaManager; - CassandraMetadataRepository cmr; + @BeforeAll + public static void initCassandra() + throws Exception { + CASSANDRA.withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger("org.apache.archiva.metadata.repository.cassandra.logs"))); + CASSANDRA.start(); + System.setProperty("cassandra.host", CASSANDRA.getHost()); + System.setProperty("cassandra.port", CASSANDRA.getMappedPort(9042).toString()); + } + + @AfterAll + public static void stopCassandra() + throws Exception { + CASSANDRA.close(); + } + @BeforeEach public void setup() throws Exception @@ -131,7 +154,7 @@ public class RepositoriesNamespaceTest } catch ( Exception e ) { - logger.error( e.getMessage(), e ); + LOGGER.error( e.getMessage(), e ); throw e; } finally diff --git a/archiva-modules/metadata/metadata-store-provider/metadata-store-cassandra/src/test/resources/META-INF/spring-context.xml b/archiva-modules/metadata/metadata-store-provider/metadata-store-cassandra/src/test/resources/META-INF/spring-context.xml new file mode 100644 index 000000000..49f80f869 --- /dev/null +++ b/archiva-modules/metadata/metadata-store-provider/metadata-store-cassandra/src/test/resources/META-INF/spring-context.xml @@ -0,0 +1,29 @@ + + + + + + + + + + \ No newline at end of file diff --git a/archiva-modules/metadata/metadata-store-provider/metadata-store-cassandra/src/test/resources/log4j2-test.xml b/archiva-modules/metadata/metadata-store-provider/metadata-store-cassandra/src/test/resources/log4j2-test.xml index 6e871d967..6a8992150 100644 --- a/archiva-modules/metadata/metadata-store-provider/metadata-store-cassandra/src/test/resources/log4j2-test.xml +++ b/archiva-modules/metadata/metadata-store-provider/metadata-store-cassandra/src/test/resources/log4j2-test.xml @@ -19,23 +19,16 @@ --> - - - - + - - - - - - - + + + diff --git a/archiva-modules/metadata/metadata-store-provider/oak-jcr/metadata-store-jcr/src/test/resources/log4j2-test.xml b/archiva-modules/metadata/metadata-store-provider/oak-jcr/metadata-store-jcr/src/test/resources/log4j2-test.xml index b9911476e..7d0a7bfdb 100644 --- a/archiva-modules/metadata/metadata-store-provider/oak-jcr/metadata-store-jcr/src/test/resources/log4j2-test.xml +++ b/archiva-modules/metadata/metadata-store-provider/oak-jcr/metadata-store-jcr/src/test/resources/log4j2-test.xml @@ -19,7 +19,7 @@ --> - + diff --git a/archiva-modules/metadata/metadata-store-provider/oak-jcr/oak-jcr-lucene/pom.xml b/archiva-modules/metadata/metadata-store-provider/oak-jcr/oak-jcr-lucene/pom.xml index 961fe0d16..633914610 100644 --- a/archiva-modules/metadata/metadata-store-provider/oak-jcr/oak-jcr-lucene/pom.xml +++ b/archiva-modules/metadata/metadata-store-provider/oak-jcr/oak-jcr-lucene/pom.xml @@ -182,7 +182,6 @@ org.apache.maven.plugins maven-shade-plugin - 3.2.4 @@ -191,6 +190,7 @@ shade + false *:* diff --git a/pom.xml b/pom.xml index e3cbef7b4..50ff533ab 100644 --- a/pom.xml +++ b/pom.xml @@ -1824,28 +1824,36 @@ - - org.junit.platform - junit-platform-launcher - test - - - org.junit.platform - junit-platform-console-standalone - test - org.junit.jupiter - junit-jupiter-engine + junit-jupiter-api test - + org.junit.vintage junit-vintage-engine test - - + + + + + + + + + + + + + + + + + + + +