This repository has been archived by the owner on Feb 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
dctk_utils_test.go
98 lines (82 loc) · 2.15 KB
/
dctk_utils_test.go
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
90
91
92
93
94
95
96
97
98
package dctk
import (
"net"
"os/exec"
"strconv"
"testing"
"time"
)
var dockerIP = func() string {
out, _ := exec.Command("docker", "network", "inspect", "bridge", "--format",
"{{range .IPAM.Config}}{{.Subnet}}{{end}}").Output()
subnetStr := string(out[:len(out)-1])
_, subnet, _ := net.ParseCIDR(subnetStr)
addrs, _ := net.InterfaceAddrs()
for _, a := range addrs {
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if subnet.Contains(ipnet.IP) {
return ipnet.IP.String()
}
}
}
return ""
}()
type externalHubDef struct {
name string
proto string
port uint
}
var externalHubDefs = []*externalHubDef{
{"godcpp-adc", "adc", 1411},
{"godcpp-nmdc", "nmdc", 1411},
{"luadch", "adc", 5000},
{"verlihub", "nmdc", 4111},
}
func foreachExternalHub(t *testing.T, testName string, cb func(t *testing.T, e *externalHub)) {
for _, def := range externalHubDefs {
t.Run(def.name, func(t *testing.T) {
e := newExternalHub(testName, def)
defer e.close()
cb(t, e)
})
}
}
type externalHub struct {
su string
}
func newExternalHub(testName string, def *externalHubDef) *externalHub {
exec.Command("docker", "kill", "dctk-test-hub").Run()
exec.Command("docker", "wait", "dctk-test-hub").Run()
exec.Command("docker", "rm", "dctk-test-hub").Run()
// start hub
cmd := []string{"docker", "run", "--rm", "-d", "--name=dctk-test-hub"}
cmd = append(cmd, "dctk-test-hub-"+def.name)
cmd = append(cmd, testName)
exec.Command(cmd[0], cmd[1:]...).Run()
// get hub ip
byts, _ := exec.Command("docker", "inspect", "-f",
"{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}",
"dctk-test-hub").Output()
ip := string(byts[:len(byts)-1])
address := ip + ":" + strconv.FormatUint(uint64(def.port), 10)
// wait for hub
for {
time.Sleep(1 * time.Second)
conn, err := net.DialTimeout("tcp", address, 5*time.Second)
if err != nil {
continue
}
conn.Close()
break
}
return &externalHub{
su: def.proto + "://" + address,
}
}
func (e *externalHub) URL() string {
return e.su
}
func (e *externalHub) close() {
exec.Command("docker", "kill", "dctk-test-hub").Run()
exec.Command("docker", "wait", "dctk-test-hub").Run()
}