diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 5badb58..1c00c76 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -25,6 +25,6 @@ "postStartCommand": "bash -i .devcontainer/post-start.sh", "postCreateCommand": "bash -i .devcontainer/post-create.sh", "mounts": [ - "source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind" + "source=/var/run/docker.sock.raw,target=/var/run/docker.sock,type=bind" ] } diff --git a/.devcontainer/docker-compose.yml b/.devcontainer/docker-compose.yml index c8a656b..9891eae 100644 --- a/.devcontainer/docker-compose.yml +++ b/.devcontainer/docker-compose.yml @@ -11,6 +11,8 @@ services: volumes: - ../..:/workspaces:cached environment: + DOCKER_NETWORK: training-log-pro_devcontainer_default + TESTCONTAINERS_RYUK_DISABLED: true POSTGRES_HOSTNAME: db POSTGRES_PORT: 5432 POSTGRES_DB: training-log @@ -25,12 +27,6 @@ services: # Overrides default command so things don't shut down after the process ends. command: sleep infinity - # Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function. - network_mode: service:db - - # Use "forwardPorts" in **devcontainer.json** to forward an app port locally. - # (Adding the "ports" property to this file will not forward from a Codespace.) - db: image: postgres:15.2-bullseye restart: unless-stopped @@ -38,6 +34,3 @@ services: POSTGRES_DB: training-log POSTGRES_PASSWORD: postgres POSTGRES_USER: postgres - - # Add "forwardPorts": ["5432"] to **devcontainer.json** to forward PostgreSQL locally. - # (Adding the "ports" property to this file will not forward from a Codespace.) diff --git a/server/src/test/java/mucsi96/traininglog/BaseIntegrationTest.java b/server/src/test/java/mucsi96/traininglog/BaseIntegrationTest.java index 333e842..7254ee0 100644 --- a/server/src/test/java/mucsi96/traininglog/BaseIntegrationTest.java +++ b/server/src/test/java/mucsi96/traininglog/BaseIntegrationTest.java @@ -1,6 +1,8 @@ package mucsi96.traininglog; import org.junit.jupiter.api.BeforeAll; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; @@ -9,8 +11,27 @@ import org.springframework.test.context.DynamicPropertyRegistry; import org.springframework.test.context.DynamicPropertySource; import org.springframework.test.web.servlet.MockMvc; +import org.testcontainers.containers.Network; import org.testcontainers.containers.PostgreSQLContainer; +class DevContainerNetwork implements Network { + + @Override + public String getId() { + return System.getenv("DOCKER_NETWORK"); + } + + @Override + public void close() { + } + + @Override + public Statement apply(Statement base, Description description) { + return null; + } + +}; + @ActiveProfiles("test") @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @AutoConfigureMockMvc @@ -37,6 +58,12 @@ public static void setUp() { } dbMock = new PostgreSQLContainer<>("postgres:15.3-alpine3.18"); + + if (System.getenv("DOCKER_NETWORK") != null) { + Network network = new DevContainerNetwork(); + dbMock.withNetwork(network); + } + dbMock.start(); }