Skip to content

Commit 4c0d325

Browse files
committed
fix: add integration test
1 parent 103f322 commit 4c0d325

8 files changed

Lines changed: 288 additions & 5 deletions

File tree

.github/workflows/engine-ci.yml

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ jobs:
1010
secrets: inherit
1111
strategy:
1212
matrix:
13-
runtime: [docker, podman]
13+
include:
14+
- runtime: docker
15+
is_uploader: true
16+
- runtime: podman
1417
permissions:
1518
contents: write # for checkout
1619
id-token: write # for authenticating to Google Cloud Platform
@@ -21,3 +24,35 @@ jobs:
2124
install_binary: true
2225
remote_debug: true
2326
remote_debug_user: "fr12k"
27+
upload_artifacts: ${{ matrix.is_uploader }}
28+
29+
integration-test:
30+
name: Integration Test (${{ matrix.os }})
31+
needs: build-go
32+
runs-on: ubuntu-latest
33+
steps:
34+
- name: Checkout code
35+
uses: actions/checkout@v4
36+
37+
- name: Setup Go
38+
uses: actions/setup-go@v5
39+
with:
40+
go-version: '1.25'
41+
42+
- name: Download artifacts
43+
uses: actions/download-artifact@v4
44+
with:
45+
name: binaries
46+
47+
- name: Make binary executable
48+
run: ls -lha
49+
50+
- name: Run integration tests
51+
env:
52+
RUN_INTEGRATION_TESTS: "true"
53+
run: |
54+
# Move binary to expected location
55+
# mv ${{ matrix.binary }} containifyci-java-$(go env GOOS)-$(go env GOARCH)
56+
57+
# Run the integration test
58+
go test -tags=integration -v -timeout 10m ./...

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.25.0
55
// replace github.com/containifyci/engine-ci => ../engine-ci
66

77
require (
8-
github.com/containifyci/engine-ci v0.24.2
8+
github.com/containifyci/engine-ci v0.25.0
99
github.com/stretchr/testify v1.11.1
1010
)
1111

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ github.com/containers/psgo v1.9.0 h1:eJ74jzSaCHnWt26OlKZROSyUyRcGDf+gYBdXnxrMW4g
7070
github.com/containers/psgo v1.9.0/go.mod h1:0YoluUm43Mz2UnBIh1P+6V6NWcbpTL5uRtXyOcH0B5A=
7171
github.com/containers/storage v1.58.0 h1:Q7SyyCCjqgT3wYNgRNIL8o/wUS92heIj2/cc8Sewvcc=
7272
github.com/containers/storage v1.58.0/go.mod h1:w7Jl6oG+OpeLGLzlLyOZPkmUso40kjpzgrHUk5tyBlo=
73-
github.com/containifyci/engine-ci v0.24.2 h1:TTiPVgY0F2NR0OHq8xO9H45lhpkZQL2Da5M8Irt5x5k=
74-
github.com/containifyci/engine-ci v0.24.2/go.mod h1:SWRTTkF3IADlTwY0w20hCukdeJKOsuS8XmbmoWZE1kw=
73+
github.com/containifyci/engine-ci v0.25.0 h1:rRB4+XTzaTe8fcyd4Sy6Xm+05BVuSWoB31lDo8LeNd0=
74+
github.com/containifyci/engine-ci v0.25.0/go.mod h1:SWRTTkF3IADlTwY0w20hCukdeJKOsuS8XmbmoWZE1kw=
7575
github.com/containifyci/engine-ci/protos2 v0.15.2 h1:CjUuYqXt0gAakiUCDWn5JaO+oTesFMeyGj0Vs7A24qc=
7676
github.com/containifyci/engine-ci/protos2 v0.15.2/go.mod h1:k7k6Zego6VwwYFYHM4LcBR34E6NDxY1OO8lgVQzMpQ0=
7777
github.com/containifyci/go-self-update v0.2.2 h1:qbactPBL9Ad90bS6v5dIjulNFwqxj/C+IK8ZdFxe9CM=

integration_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//go:build integration
2+
// +build integration
3+
4+
package main
5+
6+
import (
7+
"fmt"
8+
"os"
9+
"os/exec"
10+
"path/filepath"
11+
"runtime"
12+
"testing"
13+
14+
"github.com/stretchr/testify/assert"
15+
"github.com/stretchr/testify/require"
16+
)
17+
18+
// TestBuildHelloWorldServlet tests that the containifyci-java binary can successfully
19+
// build a simple Hello World Tomcat servlet project
20+
func TestBuildHelloWorldServlet(t *testing.T) {
21+
// Skip if not running integration tests
22+
if os.Getenv("RUN_INTEGRATION_TESTS") != "true" {
23+
t.Skip("Skipping integration test. Set RUN_INTEGRATION_TESTS=true to run")
24+
}
25+
26+
// Get the current working directory
27+
cwd, err := os.Getwd()
28+
require.NoError(t, err, "Failed to get current working directory")
29+
30+
// Determine binary name based on OS and architecture
31+
binaryName := fmt.Sprintf("containifyci-java-%s-%s", runtime.GOOS, runtime.GOARCH)
32+
binaryPath := filepath.Join(cwd, binaryName)
33+
34+
// Check if pre-built binary exists, if not build it
35+
if _, err := os.Stat(binaryPath); os.IsNotExist(err) {
36+
t.Logf("Binary %s not found, building it...", binaryName)
37+
cmd := exec.Command("go", "build", "-o", binaryPath, ".")
38+
err = cmd.Run()
39+
require.NoError(t, err, "Failed to build binary")
40+
}
41+
42+
// Path to test project
43+
testProjectPath := filepath.Join(cwd, "testdata", "hello-world-servlet")
44+
45+
// Ensure test project exists
46+
pomPath := filepath.Join(testProjectPath, "pom.xml")
47+
require.FileExists(t, pomPath, "Test project pom.xml not found")
48+
49+
// Clean any previous build artifacts
50+
targetDir := filepath.Join(testProjectPath, "target")
51+
os.RemoveAll(targetDir)
52+
defer os.RemoveAll(targetDir)
53+
54+
// Change to project directory
55+
originalDir, err := os.Getwd()
56+
require.NoError(t, err)
57+
defer os.Chdir(originalDir)
58+
59+
err = os.Chdir(testProjectPath)
60+
require.NoError(t, err)
61+
62+
// Run the binary to build the test project
63+
t.Log("Running containifyci-java to build Hello World servlet...")
64+
cmd := exec.Command(binaryPath, "run")
65+
cmd.Stdout = os.Stdout
66+
cmd.Stderr = os.Stderr
67+
cmd.Env = append(os.Environ(), "CONTAINIFYCI_FILE=.containifyci/containifyci.go")
68+
69+
// Run command and check exit code
70+
err = cmd.Run()
71+
assert.NoError(t, err, "Build command should exit with code 0")
72+
73+
t.Log("Build completed successfully with exit code 0")
74+
}

pkg/maven/maven.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,8 @@ func (c *MavenContainer) Prod() error {
276276
os.Exit(1)
277277
}
278278

279-
err = c.CopyFileTo(c.File.Host(), "/usr/local/tomcat/webapps/jpetstore.war")
279+
fileName := filepath.Base(c.File.Host())
280+
err = c.CopyFileTo(c.File.Host(), "/usr/local/tomcat/webapps/"+fileName)
280281
if err != nil {
281282
slog.Error("Failed to copy file to container", "error", err, "file", c.File)
282283
os.Exit(1)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
5+
http://maven.apache.org/xsd/maven-4.0.0.xsd">
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<groupId>com.example</groupId>
9+
<artifactId>hello-world-servlet</artifactId>
10+
<version>1.0.0</version>
11+
<packaging>war</packaging>
12+
13+
<name>Hello World Servlet</name>
14+
<description>A simple Hello World Tomcat Servlet for integration testing</description>
15+
16+
<properties>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
<maven.compiler.source>11</maven.compiler.source>
19+
<maven.compiler.target>11</maven.compiler.target>
20+
<servlet.api.version>4.0.1</servlet.api.version>
21+
<junit.version>4.13.2</junit.version>
22+
</properties>
23+
24+
<dependencies>
25+
<!-- Servlet API -->
26+
<dependency>
27+
<groupId>javax.servlet</groupId>
28+
<artifactId>javax.servlet-api</artifactId>
29+
<version>${servlet.api.version}</version>
30+
<scope>provided</scope>
31+
</dependency>
32+
33+
<!-- JUnit for testing -->
34+
<dependency>
35+
<groupId>junit</groupId>
36+
<artifactId>junit</artifactId>
37+
<version>${junit.version}</version>
38+
<scope>test</scope>
39+
</dependency>
40+
</dependencies>
41+
42+
<build>
43+
<finalName>hello-world-servlet</finalName>
44+
<plugins>
45+
<plugin>
46+
<groupId>org.apache.maven.plugins</groupId>
47+
<artifactId>maven-compiler-plugin</artifactId>
48+
<version>3.11.0</version>
49+
<configuration>
50+
<source>11</source>
51+
<target>11</target>
52+
</configuration>
53+
</plugin>
54+
<plugin>
55+
<groupId>org.apache.maven.plugins</groupId>
56+
<artifactId>maven-war-plugin</artifactId>
57+
<version>3.3.2</version>
58+
<configuration>
59+
<failOnMissingWebXml>true</failOnMissingWebXml>
60+
</configuration>
61+
</plugin>
62+
<!-- Tomcat Maven Plugin for running the application -->
63+
<plugin>
64+
<groupId>org.apache.tomcat.maven</groupId>
65+
<artifactId>tomcat7-maven-plugin</artifactId>
66+
<version>2.2</version>
67+
<configuration>
68+
<port>8080</port>
69+
<path>/</path>
70+
</configuration>
71+
</plugin>
72+
</plugins>
73+
</build>
74+
</project>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.example;
2+
3+
import javax.servlet.ServletException;
4+
import javax.servlet.http.HttpServlet;
5+
import javax.servlet.http.HttpServletRequest;
6+
import javax.servlet.http.HttpServletResponse;
7+
import java.io.IOException;
8+
import java.io.PrintWriter;
9+
10+
/**
11+
* Simple Hello World Servlet for integration testing
12+
*/
13+
public class HelloWorldServlet extends HttpServlet {
14+
15+
private static final long serialVersionUID = 1L;
16+
private String message;
17+
18+
@Override
19+
public void init() throws ServletException {
20+
message = "Hello World from Tomcat Servlet!";
21+
}
22+
23+
@Override
24+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
25+
throws ServletException, IOException {
26+
27+
response.setContentType("text/html;charset=UTF-8");
28+
29+
try (PrintWriter out = response.getWriter()) {
30+
out.println("<!DOCTYPE html>");
31+
out.println("<html>");
32+
out.println("<head>");
33+
out.println("<title>Hello World Servlet</title>");
34+
out.println("</head>");
35+
out.println("<body>");
36+
out.println("<h1>" + message + "</h1>");
37+
out.println("<p>This servlet is working correctly!</p>");
38+
out.println("<p>Request URI: " + request.getRequestURI() + "</p>");
39+
out.println("<p>Servlet Path: " + request.getServletPath() + "</p>");
40+
out.println("</body>");
41+
out.println("</html>");
42+
}
43+
}
44+
45+
@Override
46+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
47+
throws ServletException, IOException {
48+
doGet(request, response);
49+
}
50+
51+
@Override
52+
public void destroy() {
53+
// Clean up resources if needed
54+
}
55+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
5+
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
6+
version="4.0">
7+
8+
<display-name>Hello World Servlet Application</display-name>
9+
10+
<description>
11+
This is a simple Hello World servlet application for integration testing
12+
</description>
13+
14+
<!-- Servlet Declaration -->
15+
<servlet>
16+
<servlet-name>HelloWorldServlet</servlet-name>
17+
<servlet-class>com.example.HelloWorldServlet</servlet-class>
18+
<load-on-startup>1</load-on-startup>
19+
</servlet>
20+
21+
<!-- Servlet Mapping -->
22+
<servlet-mapping>
23+
<servlet-name>HelloWorldServlet</servlet-name>
24+
<url-pattern>/hello</url-pattern>
25+
</servlet-mapping>
26+
27+
<!-- Default servlet mapping for root -->
28+
<servlet-mapping>
29+
<servlet-name>HelloWorldServlet</servlet-name>
30+
<url-pattern>/</url-pattern>
31+
</servlet-mapping>
32+
33+
<!-- Welcome file -->
34+
<welcome-file-list>
35+
<welcome-file>index.html</welcome-file>
36+
<welcome-file>index.jsp</welcome-file>
37+
</welcome-file-list>
38+
39+
<!-- Session configuration -->
40+
<session-config>
41+
<session-timeout>30</session-timeout>
42+
</session-config>
43+
44+
</web-app>

0 commit comments

Comments
 (0)