@@ -2,120 +2,71 @@ package mongodb_test
2
2
3
3
import (
4
4
"context"
5
- "fmt"
6
5
"log"
7
- "strings "
6
+ "testing "
8
7
9
8
"go.mongodb.org/mongo-driver/mongo"
10
9
"go.mongodb.org/mongo-driver/mongo/options"
11
10
12
11
"github.com/testcontainers/testcontainers-go"
13
12
"github.com/testcontainers/testcontainers-go/modules/mongodb"
14
- "github.com/testcontainers/testcontainers-go/wait"
15
13
)
16
14
17
- func ExampleRunContainer () {
18
- // runMongoDBContainer {
19
- ctx := context .Background ()
20
-
21
- mongodbContainer , err := mongodb .RunContainer (ctx , testcontainers .WithImage ("mongo:6" ))
22
- if err != nil {
23
- log .Fatalf ("failed to start container: %s" , err )
24
- }
25
-
26
- // Clean up the container
27
- defer func () {
28
- if err := mongodbContainer .Terminate (ctx ); err != nil {
29
- log .Fatalf ("failed to terminate container: %s" , err )
30
- }
31
- }()
32
- // }
33
-
34
- state , err := mongodbContainer .State (ctx )
35
- if err != nil {
36
- log .Fatalf ("failed to get container state: %s" , err ) // nolint:gocritic
37
- }
38
-
39
- fmt .Println (state .Running )
40
-
41
- // Output:
42
- // true
43
- }
44
-
45
- func ExampleRunContainer_connect () {
46
- // connectToMongo {
47
- ctx := context .Background ()
48
-
49
- mongodbContainer , err := mongodb .RunContainer (ctx , testcontainers .WithImage ("mongo:6" ))
50
- if err != nil {
51
- log .Fatalf ("failed to start container: %s" , err )
52
- }
53
-
54
- // Clean up the container
55
- defer func () {
56
- if err := mongodbContainer .Terminate (ctx ); err != nil {
57
- log .Fatalf ("failed to terminate container: %s" , err )
58
- }
59
- }()
60
-
61
- endpoint , err := mongodbContainer .ConnectionString (ctx )
62
- if err != nil {
63
- log .Fatalf ("failed to get connection string: %s" , err ) // nolint:gocritic
64
- }
65
-
66
- mongoClient , err := mongo .Connect (ctx , options .Client ().ApplyURI (endpoint ))
67
- if err != nil {
68
- log .Fatalf ("failed to connect to MongoDB: %s" , err )
69
- }
70
- // }
71
-
72
- err = mongoClient .Ping (ctx , nil )
73
- if err != nil {
74
- log .Fatalf ("failed to ping MongoDB: %s" , err )
75
- }
76
-
77
- fmt .Println (mongoClient .Database ("test" ).Name ())
78
-
79
- // Output:
80
- // test
81
- }
82
-
83
- func ExampleRunContainer_withCredentials () {
84
- ctx := context .Background ()
85
-
86
- container , err := mongodb .RunContainer (ctx ,
87
- testcontainers .WithImage ("mongo:6" ),
88
- mongodb .WithUsername ("root" ),
89
- mongodb .WithPassword ("password" ),
90
- testcontainers .WithWaitStrategy (wait .ForLog ("Waiting for connections" )),
91
- )
92
- if err != nil {
93
- log .Fatalf ("failed to start container: %s" , err )
15
+ func TestMongoDB (t * testing.T ) {
16
+ type tests struct {
17
+ name string
18
+ image string
94
19
}
95
-
96
- // Clean up the container
97
- defer func () {
98
- if err := container .Terminate (ctx ); err != nil {
99
- log .Fatalf ("failed to terminate container: %s" , err )
100
- }
101
- }()
102
-
103
- connStr , err := container .ConnectionString (ctx )
104
- if err != nil {
105
- log .Fatalf ("failed to get connection string: %s" , err ) // nolint:gocritic
20
+ testCases := []tests {
21
+ {
22
+ name : "From Docker Hub" ,
23
+ image : "mongo:6" ,
24
+ },
25
+ {
26
+ name : "Community Server" ,
27
+ image : "mongodb/mongodb-community-server:7.0.2-ubi8" ,
28
+ },
29
+ {
30
+ name : "Enterprise Server" ,
31
+ image : "mongodb/mongodb-enterprise-server:7.0.0-ubi8" ,
32
+ },
106
33
}
107
34
108
- mongoClient , err := mongo .Connect (ctx , options .Client ().ApplyURI (connStr ))
109
- if err != nil {
110
- log .Fatalf ("failed to connect to MongoDB: %s" , err )
35
+ for _ , tc := range testCases {
36
+ t .Run (tc .image , func (t * testing.T ) {
37
+ t .Parallel ()
38
+
39
+ ctx := context .Background ()
40
+
41
+ mongodbContainer , err := mongodb .RunContainer (ctx , testcontainers .WithImage (tc .image ))
42
+ if err != nil {
43
+ t .Fatalf ("failed to start container: %s" , err )
44
+ }
45
+
46
+ defer func () {
47
+ if err := mongodbContainer .Terminate (ctx ); err != nil {
48
+ t .Fatalf ("failed to terminate container: %s" , err )
49
+ }
50
+ }()
51
+
52
+ endpoint , err := mongodbContainer .ConnectionString (ctx )
53
+ if err != nil {
54
+ t .Fatalf ("failed to get connection string: %s" , err )
55
+ }
56
+
57
+ mongoClient , err := mongo .Connect (ctx , options .Client ().ApplyURI (endpoint ))
58
+ if err != nil {
59
+ t .Fatalf ("failed to connect to MongoDB: %s" , err )
60
+ }
61
+
62
+ err = mongoClient .Ping (ctx , nil )
63
+ if err != nil {
64
+ log .Fatalf ("failed to ping MongoDB: %s" , err )
65
+ }
66
+
67
+ if mongoClient .Database ("test" ).Name () != "test" {
68
+ t .Fatalf ("failed to connect to the correct database" )
69
+ }
70
+ })
111
71
}
112
-
113
- err = mongoClient .Ping (ctx , nil )
114
- if err != nil {
115
- log .Fatalf ("failed to ping MongoDB: %s" , err )
116
- }
117
- fmt .Println (strings .Split (connStr , "@" )[0 ])
118
-
119
- // Output:
120
- // mongodb://root:password
121
72
}
0 commit comments