/
test_utils.go
135 lines (112 loc) · 3.06 KB
/
test_utils.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
package datasource
import (
"fmt"
"net"
"os"
"strings"
"sync"
"time"
"golang.org/x/net/context"
etcd "github.com/coreos/etcd/client"
)
// ForTestParams is the way to create a customized DataSource to be
// used in a test. Fields with value=nil will be ignored.
type ForTestParams struct {
leaseStart *net.IP
leaseRange *int
workspacePath *string
listenIF *string
dnsIPStrings *[]string
}
const (
forTestDefaultLeaseStart = "127.0.0.2"
forTestDefaultLeaseRange = 10
forTestDefaultWorkspacePath = "/tmp/blacksmith/workspaces/test-workspace"
forTestDefaultListenIF = "lo"
forTestDNSIPStrings = "8.8.8.8"
)
var (
forTestLock = &sync.Mutex{}
forTestIndex = 1
)
func etcdClietForTest() (etcd.Client, error) {
etcdFlag := os.Getenv("ETCD_ENDPOINT")
etcdClient, err := etcd.New(etcd.Config{
Endpoints: strings.Split(etcdFlag, ","),
HeaderTimeoutPerRequest: 5 * time.Second,
})
return etcdClient, err
}
// ForTest constructs a DataSource to be used in tests
func ForTest(params *ForTestParams) (DataSource, error) {
var err error
leaseStart := net.ParseIP(forTestDefaultLeaseStart)
leaseRange := forTestDefaultLeaseRange
workspacePath := forTestDefaultWorkspacePath
listenIF := "lo"
dnsIPStrings := strings.Split(forTestDNSIPStrings, ",")
if params != nil {
if params.leaseStart != nil {
leaseStart = *params.leaseStart
}
if params.leaseRange != nil {
leaseRange = *params.leaseRange
}
if params.workspacePath != nil {
workspacePath = *params.workspacePath
}
if params.listenIF != nil {
listenIF = *params.listenIF
}
if params.dnsIPStrings != nil {
dnsIPStrings = *params.dnsIPStrings
}
}
// For tests to be safe for parallel execution
forTestLock.Lock()
clusterNameFlag := fmt.Sprintf("blacksmith-%04d", forTestIndex)
forTestIndex++
forTestLock.Unlock()
var dhcpIF *net.Interface
dhcpIF, err = net.InterfaceByName(listenIF)
if err != nil {
return nil,
fmt.Errorf("error while trying to get the interface (%s): %s", listenIF, err)
}
serverIP := net.IPv4(127, 0, 0, 1)
etcdClient, err := etcdClietForTest()
if err != nil {
return nil, fmt.Errorf("etcd instance not found: %s", err)
}
kapi := etcd.NewKeysAPI(etcdClient)
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
_, err = kapi.Delete(ctx, clusterNameFlag,
&etcd.DeleteOptions{Dir: true, Recursive: true})
if err != nil && !etcd.IsKeyNotFound(err) {
return nil, fmt.Errorf("error while purging previous data from etcd: %s", err)
}
selfInfo := InstanceInfo{
IP: serverIP,
Nic: dhcpIF.HardwareAddr,
WebPort: 8000,
Version: "test",
Commit: "unknown",
BuildTime: "unknown",
ServiceStartTime: time.Now().UTC().Unix(),
}
etcdDataSource, err := NewEtcdDataSource(
kapi,
etcdClient,
leaseStart,
leaseRange,
clusterNameFlag,
workspacePath,
dnsIPStrings,
selfInfo,
)
if err != nil {
return nil, fmt.Errorf("couldn't create runtime configuration: %s", err)
}
return etcdDataSource, nil
}