Skip to content

Commit a237c6b

Browse files
committed
inline go script and run go mod commands in container
1 parent 4d8a7e2 commit a237c6b

File tree

1 file changed

+62
-11
lines changed

1 file changed

+62
-11
lines changed

internal/test/compilecheck/compile_check_test.go

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package main
88

99
import (
10+
"bytes"
1011
"context"
1112
"fmt"
1213
"io"
@@ -21,6 +22,24 @@ import (
2122
"github.com/testcontainers/testcontainers-go"
2223
)
2324

25+
const mainGo = `package main
26+
27+
import (
28+
"fmt"
29+
30+
"go.mongodb.org/mongo-driver/v2/bson"
31+
"go.mongodb.org/mongo-driver/v2/mongo"
32+
"go.mongodb.org/mongo-driver/v2/mongo/options"
33+
)
34+
35+
func main() {
36+
_, _ = mongo.Connect(options.Client())
37+
fmt.Println(bson.D{{Key: "key", Value: "value"}})
38+
}
39+
`
40+
41+
var architectures = []string{"386", "arm", "arm64", "ppc64le", "s390x"}
42+
2443
func getVersions(t *testing.T) []string {
2544
t.Helper()
2645

@@ -49,16 +68,15 @@ func TestCompileCheck(t *testing.T) {
4968
req := testcontainers.ContainerRequest{
5069
Image: image,
5170
Cmd: []string{"tail", "-f", "/dev/null"},
52-
WorkingDir: "/workspace",
71+
WorkingDir: "/app",
5372
HostConfigModifier: func(hostConfig *container.HostConfig) {
54-
hostConfig.Binds = []string{fmt.Sprintf("%s:/workspace", rootDir)}
73+
hostConfig.Binds = []string{fmt.Sprintf("%s:/driver", rootDir)}
5574
},
56-
Env: map[string]string{
57-
"GC": "go",
58-
// Compilation modules are not part of the workspace as testcontainers requires
59-
// a version of klauspost/compress not supported by the Go Driver / other modules
60-
// in the workspace.
61-
"GOWORK": "off",
75+
Files: []testcontainers.ContainerFile{
76+
{
77+
ContainerFilePath: "/app/main.go",
78+
Reader: bytes.NewReader([]byte(mainGo)),
79+
},
6280
},
6381
}
6482

@@ -75,14 +93,47 @@ func TestCompileCheck(t *testing.T) {
7593
require.NoError(t, err)
7694
}()
7795

78-
exitCode, outputReader, err := container.Exec(context.Background(), []string{"bash", "etc/compile_check.sh"})
96+
// Initialize go module and set up replace directive to use local driver.
97+
setupCmds := [][]string{
98+
{"go", "mod", "init", "app"},
99+
{"go", "mod", "edit", "-replace", "go.mongodb.org/mongo-driver/v2=/driver"},
100+
{"go", "mod", "tidy"},
101+
}
102+
103+
for _, cmd := range setupCmds {
104+
exitCode, outputReader, err := container.Exec(context.Background(), cmd)
105+
require.NoError(t, err)
106+
107+
output, err := io.ReadAll(outputReader)
108+
require.NoError(t, err)
109+
110+
require.Equal(t, 0, exitCode, "command %v failed: %s", cmd, output)
111+
}
112+
113+
// Standard build.
114+
exitCode, outputReader, err := container.Exec(context.Background(), []string{"go", "build", "./..."})
79115
require.NoError(t, err)
80116

81117
output, err := io.ReadAll(outputReader)
82118
require.NoError(t, err)
83119

84-
t.Logf("output: %s", output)
85-
assert.Equal(t, 0, exitCode)
120+
require.Equal(t, 0, exitCode, "standard build failed: %s", output)
121+
122+
// Check build with various architectures.
123+
for _, arch := range architectures {
124+
exitCode, outputReader, err := container.Exec(
125+
context.Background(),
126+
[]string{"sh", "-c", fmt.Sprintf("GOOS=linux GOARCH=%s go build ./...", arch)},
127+
)
128+
require.NoError(t, err)
129+
130+
output, err := io.ReadAll(outputReader)
131+
require.NoError(t, err)
132+
133+
assert.Equal(t, 0, exitCode, "build for GOARCH=%s failed: %s", arch, output)
134+
}
135+
136+
t.Logf("compilation checks passed for %s", image)
86137
})
87138
}
88139
}

0 commit comments

Comments
 (0)