forked from holochain/holochain-proto
/
test.go
186 lines (161 loc) · 3.59 KB
/
test.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package holochain
import (
"bytes"
"fmt"
. "github.com/smartystreets/goconvey/convey"
"io"
"io/ioutil"
"net"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
var Crash bool
func Panix(on string) {
if Crash {
panic(on)
}
}
func MakeTestDirName() string {
t := time.Now()
d := "holochain_test" + strconv.FormatInt(t.Unix(), 10) + "." + strconv.Itoa(t.Nanosecond())
return d
}
func MakeTestSeed(id string) io.Reader {
return strings.NewReader(id + "1234567890123456789012345678901234567890")
}
func setupTestService() (d string, s *Service) {
d = SetupTestDir()
identity := "Herbert <h@bert.com>"
s, err := Init(filepath.Join(d, DefaultDirectoryName), AgentIdentity(identity), MakeTestSeed(identity))
if err != nil {
panic(err)
}
s.Settings.DefaultBootstrapServer = "localhost:3142"
return
}
func SetupTestService() (d string, s *Service) {
return setupTestService()
}
// Ask the kernel for a free open port that is ready to use
func getFreePort() (port int, err error) {
port = -1
err = nil
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
return
}
l, err := net.ListenTCP("tcp", addr)
if err != nil {
return
}
defer l.Close()
port = l.Addr().(*net.TCPAddr).Port
return
}
func setupTestChain(name string, count int, s *Service) (h *Holochain) {
path := filepath.Join(s.Path, name)
a := s.DefaultAgent
if count > 0 {
var err error
identity := string(a.Identity()) + fmt.Sprintf("%d", count)
a, err = NewAgent(LibP2P, AgentIdentity(identity), MakeTestSeed(identity))
if err != nil {
panic(err)
}
}
h, err := s.MakeTestingApp(path, "toml", InitializeDB, CloneWithSameUUID, a)
if err != nil {
panic(err)
}
h.Config.DHTPort, err = getFreePort()
if err != nil {
panic(err)
}
return
}
func SetupTestChain(n string) (d string, s *Service, h *Holochain) {
d, s = setupTestService()
h = setupTestChain(n, 0, s)
return
}
func prepareTestChain(h *Holochain) {
_, err := h.GenChain()
if err != nil {
panic(err)
}
err = h.Activate()
if err != nil {
panic(err)
}
}
func PrepareTestChain(n string) (d string, s *Service, h *Holochain) {
d, s, h = SetupTestChain(n)
prepareTestChain(h)
return
}
func SetupTestDir() string {
n := MakeTestDirName()
d, err := ioutil.TempDir("", n)
if err != nil {
panic(err)
}
return d
}
func CleanupTestDir(path string) {
err := os.RemoveAll(path)
if err != nil {
panic(err)
}
}
func CleanupTestChain(h *Holochain, d string) {
h.Close()
CleanupTestDir(d)
}
func ShouldLog(log *Logger, fn func(), messages ...string) {
var buf bytes.Buffer
w := log.w
log.w = &buf
e := log.Enabled
log.Enabled = true
fn()
for _, message := range messages {
matched := strings.Index(buf.String(), message) >= 0
if matched {
So(matched, ShouldBeTrue)
} else {
So(buf.String(), ShouldEqual, message)
}
}
log.Enabled = e
log.w = w
}
func compareFile(path1 string, path2 string, fileName string) bool {
src, err := ReadFile(path1, fileName)
if err != nil {
panic(err)
}
dst, _ := ReadFile(path2, fileName)
if err != nil {
panic(err)
}
return (string(src) == string(dst)) && (string(src) != "")
}
func SetIdentity(h *Holochain, identity AgentIdentity) {
agent := h.Agent()
SetAgentIdentity(agent, identity)
h.nodeID, h.nodeIDStr, _ = agent.NodeID()
}
func SetAgentIdentity(agent Agent, identity AgentIdentity) {
agent.SetIdentity(identity)
var seed io.Reader
seed = MakeTestSeed(string(identity))
agent.GenKeys(seed)
}
func NormaliseJSON(json string) string {
json = strings.Replace(json, "\n", "", -1)
json = strings.Replace(json, " ", "", -1)
return strings.Replace(json, ": ", ":", -1)
}