@@ -19,8 +19,7 @@ import (
1919 "github.com/testcontainers/testcontainers-go"
2020)
2121
22- const (
23- mainGo = `package main
22+ const mainGo = `package main
2423
2524import (
2625 "fmt"
@@ -35,30 +34,21 @@ func main() {
3534 fmt.Println(bson.D{{Key: "key", Value: "value"}})
3635}
3736`
38- awsauthGo = `package main
3937
40- import (
41- "context"
42-
43- "github.com/aws/aws-sdk-go-v2/aws"
44- "go.mongodb.org/mongo-driver/ext/awsauth"
45- "go.mongodb.org/mongo-driver/v2/mongo"
46- "go.mongodb.org/mongo-driver/v2/mongo/options"
47- )
48-
49- func main() {
50- provider := aws.CredentialsProviderFunc(func(context.Context) (aws.Credentials, error) {
51- return aws.Credentials{}, nil
52- })
53- awsCredentialProvider := awsauth.NewCredentialsProvider(provider)
54- credential := options.Credential{
55- AuthMechanism: "MONGODB-AWS",
56- AWSCredentialsProvider: awsCredentialProvider,
57- }
58- _, _ = mongo.Connect(options.Client().SetAuth(credential))
38+ // goVersions is the list of Go versions to test compilation against.
39+ // To run tests for specific version(s), use the -run flag:
40+ //
41+ // go test -v -run '^TestCompileCheck/go:1.19$'
42+ // go test -v -run '^TestCompileCheck/go:1\.(19|20)$'
43+ var goVersions = []string {
44+ "1.19" , // Minimum supported Go version for mongo-driver v2
45+ "1.20" ,
46+ "1.21" ,
47+ "1.22" ,
48+ "1.23" ,
49+ "1.24" ,
50+ "1.25" , // Test suite Go Version
5951}
60- `
61- )
6252
6353var architectures = []string {
6454 "386" ,
@@ -100,8 +90,7 @@ func execContainer(t *testing.T, c testcontainers.Container, cmd string) string
10090 return s
10191}
10292
103- // execGo runs a Go command, trying GOTOOLCHAIN=goX.Y.0 first, then the local
104- // toolchain.
93+ // execGo runs a Go command, trying GOTOOLCHAIN=goX.Y.0 first, then goX.Y.
10594func execGo (t * testing.T , c testcontainers.Container , cfg * goExecConfig , args ... string ) string {
10695 t .Helper ()
10796
@@ -119,7 +108,7 @@ func execGo(t *testing.T, c testcontainers.Container, cfg *goExecConfig, args ..
119108 var cmd string
120109 if cfg .version != "" {
121110 primaryCmd := fmt .Sprintf ("%s GOTOOLCHAIN=go%s.0 go %s 2>&1" , envStr , cfg .version , goArgs )
122- fallbackCmd := fmt .Sprintf ("%s GOTOOLCHAIN=local go %s 2>&1" , envStr , goArgs )
111+ fallbackCmd := fmt .Sprintf ("%s GOTOOLCHAIN=go%s go %s 2>&1" , envStr , cfg . version , goArgs )
123112 cmd = fmt .Sprintf ("%s || %s" , primaryCmd , fallbackCmd )
124113 } else {
125114 cmd = fmt .Sprintf ("%s go %s 2>&1" , envStr , goArgs )
@@ -129,56 +118,6 @@ func execGo(t *testing.T, c testcontainers.Container, cfg *goExecConfig, args ..
129118}
130119
131120func TestCompileCheck (t * testing.T ) {
132- testCase := []struct {
133- name string
134- FileStr string
135-
136- // GoVersions is the list of Go versions to test compilation against.
137- // These toolchains are pre-downloaded by the Dockerfile (the image's
138- // bundled 1.25.x differs from the requested go1.25.0 patch, so it's
139- // pre-fetched too); keep that RUN in sync with the versions here.
140- //
141- // To run tests for specific version(s), use the -run flag:
142- //
143- // go test -v -run '^TestCompileCheck/aws_auth/go:1.19$'
144- // go test -v -run '^TestCompileCheck/main_driver/go:1\.(19|20)$'
145- GoVersions []string
146- }{
147- {
148- name : "main driver" ,
149- FileStr : mainGo ,
150- GoVersions : []string {
151- "1.19" , // Minimum supported Go version for mongo-driver v2
152- "1.20" ,
153- "1.21" ,
154- "1.22" ,
155- "1.23" ,
156- "1.24" ,
157- "1.25" , // Test suite Go Version
158- },
159- },
160- {
161- name : "aws auth" ,
162- FileStr : awsauthGo ,
163- GoVersions : []string {
164- "1.20" , // Minimum supported Go version for awsauth package
165- "1.21" ,
166- "1.22" ,
167- "1.23" ,
168- "1.24" ,
169- "1.25" , // Test suite Go Version
170- },
171- },
172- }
173- for _ , tc := range testCase {
174- t .Run (tc .name , func (t * testing.T ) {
175- t .Parallel ()
176- compileCheck (t , tc .FileStr , tc .GoVersions )
177- })
178- }
179- }
180-
181- func compileCheck (t * testing.T , fileStr string , goVersions []string ) {
182121 cwd , err := os .Getwd ()
183122 require .NoError (t , err )
184123
@@ -193,20 +132,11 @@ func compileCheck(t *testing.T, fileStr string, goVersions []string) {
193132 },
194133 Files : []testcontainers.ContainerFile {
195134 {
196- Reader : strings .NewReader (fileStr ),
135+ Reader : strings .NewReader (mainGo ),
197136 ContainerFilePath : "/workspace/main.go" ,
198137 FileMode : 0o644 ,
199138 },
200139 },
201- // Share the Go build cache across both compileCheck containers (the
202- // "main driver" and "aws auth" cases run in parallel, each in its own
203- // container). Without this, the per-GOARCH standard-library and driver
204- // compilation is done twice. Only GOCACHE is shared: do NOT mount over
205- // /go/pkg/mod, which would hide the Go toolchains baked into the image.
206- // The Go build cache is safe for concurrent cross-process use.
207- Mounts : testcontainers.ContainerMounts {
208- testcontainers .VolumeMount ("compilecheck-gocache" , "/root/.cache/go-build" ),
209- },
210140 // Entrypoint is set to "tail -f /dev/null" so the container stays running and available to execute multiple shell commands as needed during tests.
211141 // This keeps the container alive and ready for exec calls, rather than immediately exiting.
212142 Entrypoint : []string {"tail" , "-f" , "/dev/null" },
@@ -227,14 +157,6 @@ func compileCheck(t *testing.T, fileStr string, goVersions []string) {
227157 // Initialize Go module and download dependencies using the test suite Go version.
228158 _ = execGo (t , container , & goExecConfig {version : testSuiteVersion }, "mod" , "init" , "compilecheck" )
229159 _ = execGo (t , container , nil , "mod" , "edit" , "-replace=go.mongodb.org/mongo-driver/v2=/mongo-go-driver" )
230- _ = execGo (t , container , nil , "mod" , "edit" , "-replace=go.mongodb.org/mongo-driver/ext/awsauth=/mongo-go-driver/ext/awsauth" )
231-
232- // Pin aws-sdk-go-v2 to the version declared by ext/awsauth/go.mod. Without an
233- // explicit requirement, "go mod tidy" would add the latest release, which
234- // requires a newer Go than the minimum this compile check targets. Keep this
235- // in sync with ext/awsauth/go.mod. Unused for the main-driver case, where
236- // "go mod tidy" drops it.
237- _ = execGo (t , container , nil , "mod" , "edit" , "-require=github.com/aws/aws-sdk-go-v2@v1.28.0" )
238160 _ = execGo (t , container , & goExecConfig {version : testSuiteVersion }, "mod" , "tidy" )
239161
240162 // Set minimum Go version to what the driver claims (first version in our test list).
0 commit comments