diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1a9238ef2d..a4a7af360b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -25,6 +25,20 @@ jobs: ${{ runner.os }}-maven- - name: Fetch ES data run: wget -O src/test/resources/isaac-test-es-data.tar.gz https://cdn.isaaccomputerscience.org/isaac/test/isaac-test-es-data.tar.gz + - name: Start test containers + run: docker compose -f compose-test-deps.yml up -d + - name: Wait for PostgreSQL to be ready + run: | + until docker compose -f compose-test-deps.yml exec -T postgres pg_isready -U rutherford; do + echo "Waiting for PostgreSQL..." + sleep 2 + done + - name: Wait for Elasticsearch to be ready + run: | + until curl -s -u elastic:elastic http://localhost:9200/_cluster/health | grep -q '"status"'; do + echo "Waiting for Elasticsearch..." + sleep 2 + done - name: Build with Maven run: mvn -B package -DskipTests --file pom.xml - name: Test with Maven diff --git a/compose-test-deps.yml b/compose-test-deps.yml new file mode 100644 index 0000000000..949cf08c73 --- /dev/null +++ b/compose-test-deps.yml @@ -0,0 +1,34 @@ +version: '2' + +services: + postgres: + image: postgres:14-alpine + environment: + POSTGRES_HOST_AUTH_METHOD: trust + POSTGRES_USER: rutherford + ports: + - "5432:5432" + volumes: + - ./src/test/resources/db_scripts/postgres-rutherford-create-script.sql:/docker-entrypoint-initdb.d/00-isaac-create.sql + - ./src/test/resources/db_scripts/postgres-rutherford-functions.sql:/docker-entrypoint-initdb.d/01-isaac-functions.sql + - ./src/test/resources/db_scripts/quartz_scheduler_create_script.sql:/docker-entrypoint-initdb.d/02-isaac-quartz.sql + - ./src/test/resources/test-postgres-rutherford-data-dump.sql:/docker-entrypoint-initdb.d/03-data-dump.sql + + elasticsearch: + image: docker.elastic.co/elasticsearch/elasticsearch:7.17.6 + environment: + - cluster.name=isaac + - node.name=localhost + - http.max_content_length=512mb + - xpack.security.enabled=true + - ELASTIC_PASSWORD=elastic + - ingest.geoip.downloader.enabled=false + - ES_JAVA_OPTS=-XX:+IgnoreUnrecognizedVMOptions -XX:-UseCGroupMemoryLimitForHeap -Djdk.internal.platform.cgroups.enabled=false + - JAVA_TOOL_OPTIONS=-Dcom.sun.management.jmxremote=false + ports: + - "9200:9200" + - "9300:9300" + entrypoint: /usr/local/bin/docker-entrypoint.sh + volumes: + - ./src/test/resources/isaac-test-es-docker-entrypoint.sh:/usr/local/bin/docker-entrypoint.sh:ro + - ./src/test/resources/isaac-test-es-data.tar.gz:/usr/share/elasticsearch/isaac-test-es-data.tar.gz diff --git a/src/test/java/uk/ac/cam/cl/dtg/isaac/api/IsaacIntegrationTest.java b/src/test/java/uk/ac/cam/cl/dtg/isaac/api/IsaacIntegrationTest.java index e96b55b9eb..a6ccb1f833 100644 --- a/src/test/java/uk/ac/cam/cl/dtg/isaac/api/IsaacIntegrationTest.java +++ b/src/test/java/uk/ac/cam/cl/dtg/isaac/api/IsaacIntegrationTest.java @@ -33,7 +33,6 @@ import java.net.UnknownHostException; import java.security.NoSuchAlgorithmException; import java.security.spec.InvalidKeySpecException; -import java.time.Duration; import java.time.Instant; import java.time.format.DateTimeFormatter; import java.util.HashMap; @@ -41,12 +40,7 @@ import org.apache.commons.lang3.SystemUtils; import org.easymock.Capture; import org.eclipse.jgit.api.Git; -import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; -import org.testcontainers.containers.PostgreSQLContainer; -import org.testcontainers.elasticsearch.ElasticsearchContainer; -import org.testcontainers.utility.DockerImageName; -import org.testcontainers.utility.MountableFile; import uk.ac.cam.cl.dtg.isaac.api.managers.AssignmentManager; import uk.ac.cam.cl.dtg.isaac.api.managers.EventBookingManager; import uk.ac.cam.cl.dtg.isaac.api.managers.GameManager; @@ -127,8 +121,6 @@ public abstract class IsaacIntegrationTest { protected static HttpSession httpSession; - protected static PostgreSQLContainer postgres; - protected static ElasticsearchContainer elasticsearch; protected static PropertiesLoader properties; protected static Map globalTokens; protected static PostgresSqlDb postgresSqlDb; @@ -152,6 +144,7 @@ public abstract class IsaacIntegrationTest { protected static GitContentManager contentManager; protected static UserBadgeManager userBadgeManager; protected static UserAssociationManager userAssociationManager; + protected static CompetitionEntryService competitionEntryService; protected static AssignmentManager assignmentManager; protected static QuestionManager questionManager; protected static QuizManager quizManager; @@ -167,57 +160,13 @@ public abstract class IsaacIntegrationTest { protected static PgAnonymousUsers pgAnonymousUsers; protected static ContentMapperUtils contentMapperUtils; - protected static CompetitionEntryService competitionEntryService; - // Services protected static AssignmentService assignmentService; @BeforeAll public static void setUpClass() { - postgres = new PostgreSQLContainer<>("postgres:14-alpine") - .withEnv("POSTGRES_HOST_AUTH_METHOD", "trust") - .withUsername("rutherford") - .withCopyFileToContainer( - MountableFile.forClasspathResource("db_scripts/postgres-rutherford-create-script.sql"), - "/docker-entrypoint-initdb.d/00-isaac-create.sql" - ) - .withCopyFileToContainer( - MountableFile.forClasspathResource("db_scripts/postgres-rutherford-functions.sql"), - "/docker-entrypoint-initdb.d/01-isaac-functions.sql" - ) - .withCopyFileToContainer( - MountableFile.forClasspathResource("db_scripts/quartz_scheduler_create_script.sql"), - "/docker-entrypoint-initdb.d/02-isaac-quartz.sql" - ) - .withCopyFileToContainer( - MountableFile.forClasspathResource("test-postgres-rutherford-data-dump.sql"), - "/docker-entrypoint-initdb.d/03-data-dump.sql" - ); - - elasticsearch = new ElasticsearchContainer( - DockerImageName.parse("docker.elastic.co/elasticsearch/elasticsearch:7.17.6")) - .withCopyFileToContainer( - MountableFile.forClasspathResource("isaac-test-es-data.tar.gz"), - "/usr/share/elasticsearch/isaac-test-es-data.tar.gz" - ) - .withCopyFileToContainer( - MountableFile.forClasspathResource("isaac-test-es-docker-entrypoint.sh", 0100775), - "/usr/local/bin/docker-entrypoint.sh" - ) - .withExposedPorts(9200, 9300) - .withEnv("cluster.name", "isaac") - .withEnv("node.name", "localhost") - .withEnv("http.max_content_length", "512mb") - .withEnv("xpack.security.enabled", "true") - .withEnv("ELASTIC_PASSWORD", "elastic") - .withEnv("ingest.geoip.downloader.enabled", "false") - .withStartupTimeout(Duration.ofSeconds(120)); - - postgres.start(); - elasticsearch.start(); - postgresSqlDb = new PostgresSqlDb( - postgres.getJdbcUrl(), + "jdbc:postgresql://localhost:5432/rutherford", "rutherford", "somerandompassword" ); // user/pass are irrelevant because POSTGRES_HOST_AUTH_METHOD is set to "trust" @@ -226,7 +175,7 @@ public static void setUpClass() { elasticSearchProvider = new ElasticSearchProvider(ElasticSearchProvider.getClient( "localhost", - elasticsearch.getMappedPort(9200), + 9200, "elastic", "elastic" ) @@ -378,11 +327,6 @@ public static void setUpClass() { */ } - @AfterAll - static void tearDownClass() { - postgres.stop(); - elasticsearch.stop(); - } protected LoginResult loginAs(final HttpSession httpSession, final String username, final String password) throws NoCredentialsAvailableException, SegueDatabaseException, AuthenticationProviderMappingException,