-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesttool_docker.go
144 lines (128 loc) · 4.09 KB
/
testtool_docker.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package bbrpc
import (
"fmt"
"log"
"net"
"os/exec"
"testing"
)
type DockerCore struct {
Client *Client
MinerAddress string //矿工地址(资金来源)
MinerOwnerPubk string //矿工owner address 公钥
UnlockPass string //已有地址解锁密码
Conf ConnConfig
}
// MustDockerRunDevCore 运行1个bbc core, 运行失败时t.Fatal, 自动注册 cleanup func
func MustDockerRunDevCore(t *testing.T, imageName string) DockerCore {
killFunc, info, err := CmdRunDockerDevCore(imageName)
if killFunc != nil {
t.Cleanup(killFunc)
}
if err != nil {
t.Fatal("unable to run bbc core dev container", err)
}
return info
}
// CmdRunDockerDevCore 运行1个bbc core
func CmdRunDockerDevCore(imageName string) (func(), DockerCore, error) {
info := DockerCore{
MinerAddress: "20g003rgxdn4s64r4d0dchvb87p791q4epswkn1txadgv1evjqqwv70e5",
MinerOwnerPubk: "3bc3e5f2e5e44f1cdbc44d3bf9325c93314be123f7563b8e6a88dc6eb1a25465",
UnlockPass: "123",
Conf: ConnConfig{
User: "bbc",
Pass: "123",
DisableTLS: true,
},
}
idlePort, err := GetIdlePort()
if err != nil {
return func() {}, info, err
}
cmd := exec.Command("docker", "run", "-d", "--rm", "-p", fmt.Sprintf("9550:%d", idlePort), imageName)
runErr := cmd.Run()
outputBytes, err := cmd.CombinedOutput()
if err != nil {
return func() {}, info, err
}
if runErr != nil {
return func() {}, info, fmt.Errorf("%v, out:%v", runErr, string(outputBytes))
}
log.Println("[info] docker dev core started, container id: ", string(outputBytes))
stopContainer := func() {
if er := exec.Command(fmt.Sprintf("docker kill %s", string(outputBytes))).Run(); er != nil {
log.Println("[warn] stop bbc core dev container err", er)
}
}
info.Conf.Host = fmt.Sprintf("127.0.0.1:%d", idlePort)
info.Client, err = NewClient(&info.Conf)
if err != nil {
return stopContainer, info, err
}
return stopContainer, info, nil
}
// 说明:该函数代码有效,避免引入过多docker依赖注释掉了,需要的话自行拷贝使用
// DockerRunDevCore 运行1个bbc core
// func DockerRunDevCore(imageName string) (func(), DockerCore, error) {
// info := DockerCore{
// MinerAddress: "20g003rgxdn4s64r4d0dchvb87p791q4epswkn1txadgv1evjqqwv70e5",
// MinerOwnerPubk: "3bc3e5f2e5e44f1cdbc44d3bf9325c93314be123f7563b8e6a88dc6eb1a25465",
// UnlockPass: "123",
// Conf: ConnConfig{
// User: "bbc",
// Pass: "123",
// DisableTLS: true,
// },
// }
// cli, err := client.NewEnvClient()
// if err != nil {
// return func() {}, info, err
// }
// idlePort, err := GetIdlePort()
// if err != nil {
// return func() {}, info, err
// }
// cont, err := cli.ContainerCreate(context.Background(), &container.Config{
// // AttachStderr: true,
// // AttachStdout: true,
// // Tty: true,
// Image: imageName,
// ExposedPorts: nat.PortSet{"9550": struct{}{}},
// }, &container.HostConfig{
// PortBindings: nat.PortMap{"9550": []nat.PortBinding{{HostIP: "0.0.0.0", HostPort: strconv.Itoa(idlePort)}}},
// PublishAllPorts: true,
// AutoRemove: true,
// }, &network.NetworkingConfig{}, "")
// if err != nil {
// return func() {}, info, err
// }
// err = cli.ContainerStart(context.Background(), cont.ID, types.ContainerStartOptions{})
// if err != nil {
// return func() {}, info, err
// }
// stopContainer := func() {
// if er := cli.ContainerStop(context.Background(), cont.ID, nil); er != nil {
// log.Println("[warn] stop bbc core dev container err", er)
// }
// }
// info.Conf.Host = fmt.Sprintf("127.0.0.1:%d", idlePort)
// info.Client, err = NewClient(&info.Conf)
// if err != nil {
// return stopContainer, info, err
// }
// return stopContainer, info, nil
// }
// GetIdlePort 随机获取一个空闲的端口
func GetIdlePort() (int, error) {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0") //当指定的端口为0时,操作系统会自动分配一个空闲的端口
if err != nil {
return 0, err
}
l, err := net.ListenTCP("tcp", addr)
if err != nil {
return 0, err
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port, nil
}