MCPcopy Create free account
hub / github.com/cafebazaar/blacksmith / ForTest

Function ForTest

datasource/test_utils.go:51–135  ·  view source on GitHub ↗

ForTest constructs a DataSource to be used in tests

(params *ForTestParams)

Source from the content-addressed store, hash-verified

49
50// ForTest constructs a DataSource to be used in tests
51func ForTest(params *ForTestParams) (DataSource, error) {
52 var err error
53
54 leaseStart := net.ParseIP(forTestDefaultLeaseStart)
55 leaseRange := forTestDefaultLeaseRange
56 workspacePath := forTestDefaultWorkspacePath
57 listenIF := "lo"
58 dnsIPStrings := strings.Split(forTestDNSIPStrings, ",")
59
60 if params != nil {
61 if params.leaseStart != nil {
62 leaseStart = *params.leaseStart
63 }
64 if params.leaseRange != nil {
65 leaseRange = *params.leaseRange
66 }
67 if params.workspacePath != nil {
68 workspacePath = *params.workspacePath
69 }
70 if params.listenIF != nil {
71 listenIF = *params.listenIF
72 }
73 if params.dnsIPStrings != nil {
74 dnsIPStrings = *params.dnsIPStrings
75 }
76 }
77
78 // For tests to be safe for parallel execution
79 forTestLock.Lock()
80 clusterNameFlag := fmt.Sprintf("blacksmith-%04d", forTestIndex)
81 forTestIndex++
82 forTestLock.Unlock()
83
84 var dhcpIF *net.Interface
85 dhcpIF, err = net.InterfaceByName(listenIF)
86 if err != nil {
87 return nil,
88 fmt.Errorf("error while trying to get the interface (%s): %s", listenIF, err)
89 }
90
91 serverIP := net.IPv4(127, 0, 0, 1)
92
93 etcdClient, err := etcdClietForTest()
94
95 if err != nil {
96 return nil, fmt.Errorf("etcd instance not found: %s", err)
97 }
98
99 kapi := etcd.NewKeysAPI(etcdClient)
100
101 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
102 defer cancel()
103 _, err = kapi.Delete(ctx, clusterNameFlag,
104 &etcd.DeleteOptions{Dir: true, Recursive: true})
105 if err != nil && !etcd.IsKeyNotFound(err) {
106 return nil, fmt.Errorf("error while purging previous data from etcd: %s", err)
107 }
108

Callers 5

TestAssignFunction · 0.85
TestLeaseRangeFunction · 0.85
TestInstancesFunction · 0.85
TestCoreOSVersionFunction · 0.85
TestEtcdMembersFunction · 0.85

Calls 2

etcdClietForTestFunction · 0.85
NewEtcdDataSourceFunction · 0.85

Tested by 5

TestAssignFunction · 0.68
TestLeaseRangeFunction · 0.68
TestInstancesFunction · 0.68
TestCoreOSVersionFunction · 0.68
TestEtcdMembersFunction · 0.68