-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlightning_mocker.go
78 lines (69 loc) · 1.89 KB
/
lightning_mocker.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
package mock
import (
"fmt"
"strconv"
"github.com/docker/go-connections/nat"
"github.com/hashicorp/go-multierror"
"github.com/xplorfin/docker-utils"
"github.com/xplorfin/netutils"
)
// LightningMocker defines the lnd mocker object
type LightningMocker struct {
docker.Client
networkID string
portStack netutils.FreePortStack
}
// NewLightningMocker creates a new lightning mock object with a given
// session id
func NewLightningMocker() LightningMocker {
return LightningMocker{
Client: docker.NewDockerClient(),
networkID: "",
portStack: netutils.NewFreeportStack(),
}
}
// Initialize creates common resources by calling CreateNetworks and CreateVolumes
// and returning an error if necessary
func (c *LightningMocker) Initialize() (err error) {
cve := c.CreateNetworks()
if cve != nil {
err = multierror.Append(err, cve)
}
cne := c.CreateVolumes()
if cne != nil {
err = multierror.Append(err, cne)
}
return err
}
// CreateNetworks sets up the networks needed for
func (c *LightningMocker) CreateNetworks() (err error) {
c.networkID, err = c.CreateNetwork(string(NetworkName))
return err
}
// CreateVolumes creates new volumes if they don't exist
func (c LightningMocker) CreateVolumes() error {
for _, volume := range Volumes() {
if !c.VolumeExists(volume) {
err := c.CreateVolume(volume)
if err != nil {
return err
}
}
}
return nil
}
// Teardown removes all containers created in the session
func (c LightningMocker) Teardown() error {
return c.TeardownSession()
}
// portsToMap takes an arbitrary list of ports and converts them to a port map
// with the local machine (using free, random ports)
func (c *LightningMocker) portsToMap(ports []int) (pm PortMap) {
pm = make(PortMap)
for _, port := range ports {
pm[nat.Port(fmt.Sprintf("%d/tcp", port))] = []nat.PortBinding{
{HostIP: "0.0.0.0", HostPort: strconv.Itoa(c.portStack.GetPort())},
}
}
return pm
}