Skip to content

Commit

Permalink
Improve the way to copy postgres library (#54)
Browse files Browse the repository at this point in the history
* mention Dockerfile in the guide

* improve to copy the postgres library

* simpify the instructions

* update the start pom.xml

* use maven latest image

* use maven latest image

* use maven latest image

* improve the test script

* fix typo

* Update Dockerfile

* Update pom.xml

* Update pom.xml

* Update Dockerfile

* Update Dockerfile

* Update dependabot.yml

* Update add-pr-to-project.yml

* downgrading LMP to 3.10 to avoid devc issue

---------

Co-authored-by: Morgan Chang <[email protected]>
Co-authored-by: Ruilin Ma <[email protected]>
  • Loading branch information
3 people authored Mar 7, 2024
1 parent 8500333 commit ee67cc4
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 97 deletions.
7 changes: 6 additions & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
version: 2
updates:
- package-ecosystem: maven
directory: "/"
directory: "/finish"
schedule:
interval: monthly
open-pull-requests-limit: 50
- package-ecosystem: maven
directory: "/start"
schedule:
interval: monthly
open-pull-requests-limit: 50
1 change: 0 additions & 1 deletion .github/workflows/add-pr-to-project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ name: Add PRs to Dependabot PRs dashboard
on:
pull_request:
types:
- opened
- labeled

jobs:
Expand Down
45 changes: 29 additions & 16 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,7 @@ endif::[]

The `finish` directory in the root of this guide contains the finished application. Give it a try before you proceed.

To try out the test, first go to the `finish` directory and run the `mvn package` command to build and package the application, which places the `.war` file in the `target` directory and the `.jar` PostgreSQL JDBC driver file in the `target/liberty/wlp/usr/shared/resources` directory:

[role='command']
```
cd finish
mvn package
```

Now, run the `mvn verify` command, which compiles the Java files, starts the containers, runs the tests, and then stops the containers.
To try out the test, first go to the `finish` directory and run the following Maven goal that builds the application, starts the containers, runs the tests, and then stops the containers:

ifndef::cloud-hosted[]
[.tab_link.windows_link]
Expand All @@ -94,6 +86,7 @@ ifndef::cloud-hosted[]
--
[role='command']
```
cd finish
mvn verify
```
--
Expand All @@ -102,6 +95,7 @@ mvn verify
[role='command']
```
export TESTCONTAINERS_RYUK_DISABLED=true
cd finish
mvn verify
```
--
Expand Down Expand Up @@ -145,7 +139,14 @@ cd /home/project/guide-testcontainers/postgres
```
endif::[]

This guide uses Docker to run an instance of the PostgreSQL database for a fast installation and setup. A Dockerfile file is provided for you. Run the following command to use the Dockerfile to build the image:
// file 0
postgres/Dockerfile
[source, Dockerfile, linenums, role="code_column"]
----
include::postgres/Dockerfile[]
----

This guide uses Docker to run an instance of the PostgreSQL database for a fast installation and setup. A [hotspot file=0]`Dockerfile` file is provided for you. Run the following command to use the Dockerfile to build the image:

[role='command']
```
Expand Down Expand Up @@ -208,7 +209,7 @@ Wait a moment for dev mode to start. After you see the following message, your L
* ...
* Container network information:
* Container name: [ liberty-dev ]
* IP address [ 172.17.0.3 ] on container network [ bridge ]
* IP address [ 172.17.0.2 ] on container network [ bridge ]
* ...
----

Expand Down Expand Up @@ -322,7 +323,21 @@ LibertyContainer.java
include::finish/src/test/java/it/io/openliberty/guides/inventory/LibertyContainer.java[]
----

Construct the [hotspot=postgresImage file=0]`postgresImage` and [hotspot=invImage file=0]`invImage` using the `ImageFromDockerfile` class, which allows Testcontainers to build Docker images from a Dockerfile during the test runtime. For these instances, the provided Dockerfiles at the specified paths `../postgres/Dockerfile` and `./Dockerfile` are used to generate the respective `postgres-sample` and `inventory:1.0-SNAPSHOT` images.
// file 2
postgres/Dockerfile
[source, Dockerfile, linenums, role="code_column"]
----
include::postgres/Dockerfile[]
----

// file 3
Dockerfile
[source, Dockerfile, linenums, role="code_column"]
----
include::finish/Dockerfile[]
----

Construct the [hotspot=postgresImage file=0]`postgresImage` and [hotspot=invImage file=0]`invImage` using the `ImageFromDockerfile` class, which allows Testcontainers to build Docker images from a Dockerfile during the test runtime. For these instances, the provided Dockerfiles at the specified paths [hotspot file=2]`../postgres/Dockerfile` and [hotspot file=3]`./Dockerfile` are used to generate the respective `postgres-sample` and `inventory:1.0-SNAPSHOT` images.

Use [hotspot=GenericContainer file=0]`GenericContainer` class to create the [hotspot=postgresContainer file=0]`postgresContainer` test container to start up the `postgres-sample` Docker image, and use the [hotspot=LibertyContainer file=0]`LibertyContainer` custom class to create the [hotspot=inventoryContainer file=0]`inventoryContainer` test container to start up the `inventory:1.0-SNAPSHOT` Docker image.

Expand Down Expand Up @@ -384,8 +399,6 @@ include::finish/pom.xml[]

Add the required `dependency` for Testcontainers and Log4J libraries with `test` scope. The [hotspot=testcontainers file=0]`testcontainers` dependency offers a general-purpose API for managing container-based test environments. The [hotspot=slf4j file=0]`slf4j-reload4j` and [hotspot=slf4j-api file=0]`slf4j-api` dependencies enable the Simple Logging Facade for Java (SLF4J) API for trace logging during test execution and facilitates debugging and test performance tracking.

The Maven `pom.xml` file contains a configuration for the [hotspot=maven-dependency-plugin file=0]`maven-dependency-plugin` to copy the PostgreSQL JDBC driver into the Liberty configuration's shared resources directory. This setup occurs during the `prepare-package` phase. As a result, running the `mvn package` command ensures the PostgreSQL driver is prepared and accessible for your application when it runs on the Liberty.

Also, add and configure the [hotspot=failsafe file=0]`maven-failsafe-plugin` plugin, so that the integration test can be run by the `mvn verify` command.

When you started Open Liberty in dev mode, all the changes were automatically picked up. You can run the tests by pressing the `enter/return` key from the command-line session where you started dev mode. You see the following output:
Expand Down Expand Up @@ -435,15 +448,15 @@ Now, use the following Maven goal to run the tests from a cold start outside of
--
[role='command']
```
mvn verify
mvn clean verify
```
--
[.tab_content.linux_section]
--
[role='command']
```
export TESTCONTAINERS_RYUK_DISABLED=true
mvn verify
mvn clean verify
```
--

Expand Down
13 changes: 9 additions & 4 deletions finish/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# tag::from[]
FROM maven:3.9.6 as staging

WORKDIR /work
RUN mvn dependency:copy \
-Dartifact=org.postgresql:postgresql:42.7.2 \
-DoutputDirectory=/work

FROM icr.io/appcafe/open-liberty:kernel-slim-java11-openj9-ubi
# end::from[]

ARG VERSION=1.0
ARG REVISION=SNAPSHOT
Expand Down Expand Up @@ -30,8 +35,8 @@ COPY --chown=1001:0 \
target/inventory.war \
/config/apps

COPY --chown=1001:0 \
target/liberty/wlp/usr/shared/resources/*.jar \
COPY --chown=1001:0 --from=staging \
/work/postgresql-*.jar \
/opt/ol/wlp/usr/shared/resources/

USER 1001
Expand Down
29 changes: 4 additions & 25 deletions finish/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.7.1</version>
<version>42.7.2</version>
<scope>provided</scope>
</dependency>

Expand Down Expand Up @@ -76,23 +76,23 @@
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.19.4</version>
<version>1.19.7</version>
<scope>test</scope>
</dependency>
<!-- end::testcontainers[] -->
<!-- tag::slf4j[] -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-reload4j</artifactId>
<version>2.0.11</version>
<version>2.0.12</version>
<scope>test</scope>
</dependency>
<!-- end::slf4j[] -->
<!-- tag::slf4j-api[] -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.11</version>
<version>2.0.12</version>
</dependency>
<!-- end::slf4j-api[] -->
<!-- end::testDependenies[] -->
Expand All @@ -106,27 +106,6 @@
<artifactId>maven-war-plugin</artifactId>
<version>3.4.0</version>
</plugin>
<!-- tag::maven-dependency-plugin[] -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.6.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeGroupIds>org.postgresql</includeGroupIds>
<includeArtifactIds>postgresql</includeArtifactIds>
<outputDirectory>${project.build.directory}/liberty/wlp/usr/shared/resources</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- end::maven-dependency-plugin[] -->
<plugin>
<groupId>io.openliberty.tools</groupId>
<artifactId>liberty-maven-plugin</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion postgres/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM postgres:16.1
FROM postgres:16.2

# set env variable for password to docker
ENV POSTGRES_PASSWORD adminpwd
Expand Down
8 changes: 1 addition & 7 deletions scripts/testApp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,15 @@
set -euxo pipefail

# TEST 1: Running the test by using Testcontainers
mvn -ntp -q clean package

docker pull -q icr.io/appcafe/open-liberty:kernel-slim-java11-openj9-ubi

mvn -ntp verify
mvn -ntp clean verify

# TEST 2: Running the test by local runtime
cd ../postgres

docker build -t postgres-sample .

docker run --name postgres-container -p 5432:5432 -d postgres-sample

cd ../finish

mvn -ntp -Dhttp.keepAlive=false \
-Dmaven.wagon.http.pool=false \
-Dmaven.wagon.httpconnectionManager.ttlSeconds=120 \
Expand Down
11 changes: 9 additions & 2 deletions start/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
FROM maven:3.9.6 as staging

WORKDIR /work
RUN mvn dependency:copy \
-Dartifact=org.postgresql:postgresql:42.7.2 \
-DoutputDirectory=/work

# tag::from[]
FROM icr.io/appcafe/open-liberty:kernel-slim-java11-openj9-ubi
# end::from[]
Expand Down Expand Up @@ -30,8 +37,8 @@ COPY --chown=1001:0 \
target/inventory.war \
/config/apps

COPY --chown=1001:0 \
target/liberty/wlp/usr/shared/resources/*.jar \
COPY --chown=1001:0 --from=staging \
/work/postgresql-*.jar \
/opt/ol/wlp/usr/shared/resources/

USER 1001
Expand Down
41 changes: 1 addition & 40 deletions start/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.7.1</version>
<version>42.7.2</version>
<scope>provided</scope>
</dependency>

Expand Down Expand Up @@ -81,25 +81,6 @@
<artifactId>maven-war-plugin</artifactId>
<version>3.4.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.6.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeGroupIds>org.postgresql</includeGroupIds>
<includeArtifactIds>postgresql</includeArtifactIds>
<outputDirectory>${project.build.directory}/liberty/wlp/usr/shared/resources</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>io.openliberty.tools</groupId>
<artifactId>liberty-maven-plugin</artifactId>
Expand All @@ -116,26 +97,6 @@
</configuration>
<version>3.10</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<systemPropertyVariables>
<http.port>${liberty.var.http.port}</http.port>
<https.port>${liberty.var.https.port}</https.port>
<context.root>${liberty.var.context.root}</context.root>
</systemPropertyVariables>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

0 comments on commit ee67cc4

Please sign in to comment.