Skip to content

Commit a1fdf31

Browse files
committed
Test deployment on server
1 parent 35c65cd commit a1fdf31

4 files changed

Lines changed: 45 additions & 29 deletions

File tree

images/server/7.1.0/docs/content.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ Follow these steps:
5555
docker run -p 8080:8080 -p 4848:4848 -v /deployments:/deploy @docker.glassfish.repository@
5656
```
5757

58+
Alternatively, you can mount a specific WAR file directly:
59+
60+
```
61+
docker run -p 8080:8080 -p 4848:4848 -v /deployment/application.war:/deploy/application.war @docker.glassfish.repository@
62+
```
63+
64+
**Note**: GlassFish Server deploys applications using the WAR filename as the context path (e.g., `application.war` becomes accessible at `/application/`).
65+
5866
Then you can open the application in the browser with:
5967

6068
* http://localhost:9080/application

src/test/java/org/glassfish/main/distributions/docker/embedded/EmbeddedDeploymentIT.java

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import org.testcontainers.junit.jupiter.Container;
2727
import org.testcontainers.junit.jupiter.Testcontainers;
2828

29-
import static org.glassfish.main.distributions.docker.testutils.HttpUtilities.getEmbeddedApplication;
29+
import static org.glassfish.main.distributions.docker.testutils.HttpUtilities.getApplication;
3030
import static org.junit.jupiter.api.Assertions.assertEquals;
3131
import static org.junit.jupiter.api.Assertions.assertTrue;
3232

@@ -43,29 +43,8 @@ public class EmbeddedDeploymentIT {
4343

4444
@Test
4545
void deployedApplicationIsAccessible() throws Exception {
46-
// Wait for the GLASSFISH STARTED message in logs
47-
long startTime = System.currentTimeMillis();
48-
boolean glassfishStarted = false;
49-
while (!glassfishStarted && (System.currentTimeMillis() - startTime) < 120000) { // 2 minutes
50-
String logs = server.getLogs();
51-
if (logs.contains("GLASSFISH STARTED")) {
52-
glassfishStarted = true;
53-
} else {
54-
Thread.sleep(1000); // Wait 1 second before checking again
55-
}
56-
}
57-
58-
if (!glassfishStarted) {
59-
System.err.println("Full logs: " + server.getLogs());
60-
throw new RuntimeException("GlassFish did not start within 2 minutes");
61-
}
62-
63-
System.err.println("GlassFish started successfully, checking application deployment...");
64-
6546
// Verify the application is deployed by checking if it's accessible
66-
final HttpResponse<String> appResponse = getEmbeddedApplication(server, "/index.html");
67-
System.err.println("Response status: " + appResponse.statusCode());
68-
System.err.println("Response body: " + appResponse.body());
47+
final HttpResponse<String> appResponse = getApplication(server, "/index.html");
6948
assertEquals(200, appResponse.statusCode(), "Application response status code");
7049
assertTrue(appResponse.body().contains("Hello from test app"), "Application should return Hello message");
7150
}

src/test/java/org/glassfish/main/distributions/docker/server/StartServIT.java renamed to src/test/java/org/glassfish/main/distributions/docker/server/StartAndDeployIT.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,27 @@
1919
import java.net.http.HttpResponse;
2020

2121
import org.junit.jupiter.api.Test;
22+
import org.testcontainers.containers.BindMode;
2223
import org.testcontainers.containers.GenericContainer;
2324
import org.testcontainers.junit.jupiter.Container;
2425
import org.testcontainers.junit.jupiter.Testcontainers;
2526

2627
import static org.glassfish.main.distributions.docker.testutils.HttpUtilities.getServerDefaultRoot;
28+
import static org.glassfish.main.distributions.docker.testutils.HttpUtilities.getApplication;
2729
import static org.hamcrest.MatcherAssert.assertThat;
2830
import static org.hamcrest.Matchers.stringContainsInOrder;
2931
import static org.junit.jupiter.api.Assertions.assertEquals;
32+
import static org.junit.jupiter.api.Assertions.assertTrue;
3033

3134
@Testcontainers
32-
public class StartServIT {
35+
public class StartAndDeployIT {
3336

3437
@SuppressWarnings({"rawtypes", "resource"})
3538
@Container
3639
private final GenericContainer server = new GenericContainer<>(System.getProperty("server.docker.glassfish.image"))
37-
.withCommand("startserv").withExposedPorts(8080)
40+
.withCommand("startserv")
41+
.withExposedPorts(8080)
42+
.withFileSystemBind("target/test-classes/application-test.war", "/deploy/application-test.war", BindMode.READ_ONLY)
3843
.withLogConsumer(o -> System.err.print("GF: " + o.getUtf8String()));
3944

4045
@Test
@@ -43,4 +48,11 @@ void rootResourceGivesOkWithDefaultResponse() throws Exception {
4348
assertEquals(200, defaultRootResponse.statusCode(), "Response status code");
4449
assertThat(defaultRootResponse.body(), stringContainsInOrder("Eclipse GlassFish", "index.html", "production-quality"));
4550
}
51+
52+
@Test
53+
void deployedApplicationIsAccessible() throws Exception {
54+
final HttpResponse<String> appResponse = getApplication(server, "/application-test/index.html");
55+
assertEquals(200, appResponse.statusCode(), "Application response status code");
56+
assertTrue(appResponse.body().contains("Hello from test app"), "Application should return Hello message");
57+
}
4658
}

src/test/java/org/glassfish/main/distributions/docker/testutils/HttpUtilities.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.security.KeyManagementException;
2424
import java.security.NoSuchAlgorithmException;
2525
import java.security.SecureRandom;
26+
import java.time.Duration;
2627
import java.util.Base64;
2728

2829
import javax.net.ssl.SSLContext;
@@ -33,6 +34,7 @@
3334
import org.testcontainers.containers.GenericContainer;
3435

3536
import static java.net.http.HttpResponse.BodyHandlers.ofString;
37+
import static java.time.Duration.ofSeconds;
3638

3739
/**
3840
*
@@ -59,12 +61,27 @@ public static HttpResponse<String> getEmbeddedDefaultRoot(GenericContainer serve
5961
}
6062
}
6163

62-
public static HttpResponse<String> getEmbeddedApplication(GenericContainer server, String appPath) throws Exception {
64+
public static HttpResponse<String> getApplication(GenericContainer server, String appPath) throws Exception {
6365
URI uri = URI.create("http://localhost:" + server.getMappedPort(8080) + appPath);
64-
try (HttpClient client = newInsecureHttpClient()) {
65-
final HttpRequest request = HttpRequest.newBuilder(uri).build();
66-
return client.send(request, ofString(StandardCharsets.UTF_8));
66+
67+
// Wait up to 30 seconds for the application to become available
68+
long startTime = System.currentTimeMillis();
69+
HttpResponse<String> response = null;
70+
71+
while ((System.currentTimeMillis() - startTime) < Duration.ofSeconds(30).toMillis()) {
72+
try (HttpClient client = newInsecureHttpClient()) {
73+
final HttpRequest request = HttpRequest.newBuilder(uri).build();
74+
response = client.send(request, ofString(StandardCharsets.UTF_8));
75+
76+
if (response.statusCode() == 200) {
77+
return response;
78+
}
79+
80+
Thread.sleep(ofSeconds(1)); // Wait 1 second before retrying
81+
}
6782
}
83+
84+
return response; // Return the last response (likely 404)
6885
}
6986

7087
public static HttpResponse<String> getAdminResource(GenericContainer server, String resourcePath, UserPassword userPass) throws Exception {

0 commit comments

Comments
 (0)