/
testcontainers_node_handler.go
98 lines (77 loc) · 1.85 KB
/
testcontainers_node_handler.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 testcontainers
import (
"context"
"fmt"
"github.com/adrianbrad/privatebtc"
"github.com/docker/go-connections/nat"
"github.com/testcontainers/testcontainers-go"
"golang.org/x/sync/errgroup"
)
var _ privatebtc.NodeHandler = (*NodeHandler)(nil)
// NodeHandler represents a bitcoin node running in a docker container.
type NodeHandler struct {
cont testcontainers.Container
containerIP string
hostRPCPort string
name string
}
func newNodeHandler(
ctx context.Context,
testCont testcontainers.Container,
) (*NodeHandler, error) {
var (
containerIP string
hostRPCPort nat.Port
name string
)
eg, egCtx := errgroup.WithContext(ctx)
eg.Go(func() error {
var err error
containerIP, err = testCont.ContainerIP(egCtx)
if err != nil {
return fmt.Errorf("get container ip: %w", err)
}
return nil
})
eg.Go(func() error {
var err error
hostRPCPort, err = testCont.MappedPort(egCtx, privatebtc.RPCRegtestDefaultPort)
if err != nil {
return fmt.Errorf("get rpc port: %w", err)
}
return nil
})
eg.Go(func() error {
var err error
name, err = testCont.Name(egCtx)
if err != nil {
return fmt.Errorf("get container name: %w", err)
}
return nil
})
if err := eg.Wait(); err != nil {
return nil, err
}
return &NodeHandler{
cont: testCont,
containerIP: containerIP,
hostRPCPort: hostRPCPort.Port(),
name: name,
}, nil
}
// InternalIP returns the IP address of the container.
func (c NodeHandler) InternalIP() string {
return c.containerIP
}
// HostRPCPort returns the host RPC port of the container.
func (c NodeHandler) HostRPCPort() string {
return c.hostRPCPort
}
// Name returns the container name.
func (c NodeHandler) Name() string {
return c.name
}
// Close terminates the container.
func (c NodeHandler) Close() error {
return c.cont.Terminate(context.Background())
}