|
| 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 | +} |
0 commit comments