-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain_test.go
More file actions
89 lines (82 loc) · 1.9 KB
/
main_test.go
File metadata and controls
89 lines (82 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package mongoifc_test
import (
"fmt"
"os/exec"
"testing"
"time"
)
const (
DockerImage = "mongoifc"
DockerFile = ".github/test.dockerfile"
DockerName = "mongoifc-test"
MongoPort = "27888"
MongoUsername = "admin"
MongoPassword = "adminpass"
MongoUri = "mongodb://" + MongoUsername + ":" + MongoPassword + "@127.0.0.1:" + MongoPort +
"/?authSource=admin&directConnection=true"
)
func TestMain(m *testing.M) {
fmt.Println("Building docker image...")
cmd := exec.Command( //nolint:noctx
"docker",
"build", ".",
"--rm",
"--file", DockerFile,
"--tag", DockerImage,
)
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("docker build failed:\n%s", string(out))
panic(err)
}
fmt.Println("Starting docker container...")
cmd = exec.Command( //nolint:noctx
"docker",
"run",
"-d",
"--name="+DockerName,
"-p", MongoPort+":27017",
"-e", "MONGO_INITDB_ROOT_USERNAME="+MongoUsername,
"-e", "MONGO_INITDB_ROOT_PASSWORD="+MongoPassword,
DockerImage,
)
out, err = cmd.CombinedOutput()
if err != nil {
fmt.Printf("docker run failed:\n%s", string(out))
panic(err)
}
defer func() {
fmt.Println("Stopping docker container...")
cmd := exec.Command( //nolint:noctx
"docker",
"rm",
"--force",
DockerName,
)
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("docker rm failed:\n%s", string(out))
panic(err)
}
}()
fmt.Println("Waiting docker container...")
time.Sleep(10 * time.Second)
fmt.Println("Initializing mongodb...")
cmd = exec.Command( //nolint:noctx
"docker",
"exec", DockerName,
"/usr/bin/mongosh",
"-u", MongoUsername,
"-p", MongoPassword,
"--eval", "rs.initiate()",
)
out, err = cmd.CombinedOutput()
if err != nil {
fmt.Printf("docker exec failed:\n%s", string(out))
panic(err)
}
fmt.Println("Running tests...")
if code := m.Run(); code != 0 {
panic(fmt.Sprintf("tests failed with code %d", code))
}
}