github.com/metacurrency/holochain@v0.1.0-alpha-26.0.20200915073418-5c83169c9b5b/test.go (about) 1 package holochain 2 3 import ( 4 "bytes" 5 "fmt" 6 . "github.com/holochain/holochain-proto/hash" 7 ic "github.com/libp2p/go-libp2p-crypto" 8 . "github.com/smartystreets/goconvey/convey" 9 "io" 10 "io/ioutil" 11 "net" 12 "os" 13 "path/filepath" 14 "strconv" 15 "strings" 16 "time" 17 ) 18 19 var Crash bool 20 21 func Panix(on string) { 22 if Crash { 23 panic(on) 24 } 25 } 26 27 func MakeTestDirName() string { 28 t := time.Now() 29 d := "holochain_test" + strconv.FormatInt(t.Unix(), 10) + "." + strconv.Itoa(t.Nanosecond()) 30 return d 31 } 32 33 func MakeTestSeed(id string) io.Reader { 34 return strings.NewReader(id + "1234567890123456789012345678901234567890") 35 } 36 37 func setupTestService() (d string, s *Service) { 38 d = SetupTestDir() 39 identity := "Herbert <h@bert.com>" 40 s, err := Init(filepath.Join(d, DefaultDirectoryName), AgentIdentity(identity), MakeTestSeed(identity)) 41 if err != nil { 42 panic(err) 43 } 44 s.Settings.DefaultBootstrapServer = "localhost:3142" 45 return 46 } 47 48 func SetupTestService() (d string, s *Service) { 49 return setupTestService() 50 } 51 52 // Ask the kernel for a free open port that is ready to use 53 func getFreePort() (port int, err error) { 54 port = -1 55 err = nil 56 57 addr, err := net.ResolveTCPAddr("tcp", "localhost:0") 58 if err != nil { 59 return 60 } 61 62 l, err := net.ListenTCP("tcp", addr) 63 if err != nil { 64 return 65 } 66 defer l.Close() 67 port = l.Addr().(*net.TCPAddr).Port 68 return 69 } 70 71 func setupTestChain(name string, count int, s *Service) (h *Holochain) { 72 path := filepath.Join(s.Path, name) 73 74 a := s.DefaultAgent 75 if count > 0 { 76 var err error 77 identity := string(a.Identity()) + fmt.Sprintf("%d", count) 78 a, err = NewAgent(LibP2P, AgentIdentity(identity), MakeTestSeed(identity)) 79 if err != nil { 80 panic(err) 81 } 82 } 83 84 h, err := s.MakeTestingApp(path, "toml", InitializeDB, CloneWithSameUUID, a) 85 if err != nil { 86 panic(err) 87 } 88 h.Config.DHTPort, err = getFreePort() 89 if err != nil { 90 panic(err) 91 } 92 return 93 } 94 95 func chainTestSetup() (hs HashSpec, key ic.PrivKey, now time.Time) { 96 a, _ := NewAgent(LibP2P, "agent id", MakeTestSeed("")) 97 key = a.PrivKey() 98 hc := Holochain{agent: a} 99 dna := DNA{DHTConfig: DHTConfig{HashType: "sha2-256"}} 100 hc.nucleus = NewNucleus(&hc, &dna) 101 hP := &hc 102 hP.PrepareHashType() 103 hs = hP.hashSpec 104 return 105 } 106 107 func SetupTestChain(n string) (d string, s *Service, h *Holochain) { 108 d, s = setupTestService() 109 h = setupTestChain(n, 0, s) 110 return 111 } 112 113 func prepareTestChain(h *Holochain) { 114 _, err := h.GenChain() 115 if err != nil { 116 panic(err) 117 } 118 119 err = h.Activate() 120 if err != nil { 121 panic(err) 122 } 123 } 124 125 func PrepareTestChain(n string) (d string, s *Service, h *Holochain) { 126 d, s, h = SetupTestChain(n) 127 prepareTestChain(h) 128 return 129 } 130 131 func SetupTestDir() string { 132 n := MakeTestDirName() 133 d, err := ioutil.TempDir("", n) 134 if err != nil { 135 panic(err) 136 } 137 return d 138 } 139 140 func CleanupTestDir(path string) { 141 err := os.RemoveAll(path) 142 if err != nil { 143 panic(err) 144 } 145 } 146 147 func CleanupTestChain(h *Holochain, d string) { 148 h.Close() 149 CleanupTestDir(d) 150 } 151 152 func ShouldLog(log *Logger, fn func(), messages ...string) { 153 var buf bytes.Buffer 154 w := log.w 155 log.w = &buf 156 e := log.Enabled 157 log.Enabled = true 158 fn() 159 for _, message := range messages { 160 matched := strings.Index(buf.String(), message) >= 0 161 if matched { 162 So(matched, ShouldBeTrue) 163 } else { 164 So(buf.String(), ShouldEqual, message) 165 } 166 } 167 log.Enabled = e 168 log.w = w 169 } 170 171 func compareFile(path1 string, path2 string, fileName string) bool { 172 src, err := ReadFile(path1, fileName) 173 if err != nil { 174 panic(err) 175 } 176 dst, _ := ReadFile(path2, fileName) 177 if err != nil { 178 panic(err) 179 } 180 return (string(src) == string(dst)) && (string(src) != "") 181 } 182 183 func SetIdentity(h *Holochain, identity AgentIdentity) { 184 agent := h.Agent() 185 SetAgentIdentity(agent, identity) 186 h.nodeID, h.nodeIDStr, _ = agent.NodeID() 187 } 188 189 func SetAgentIdentity(agent Agent, identity AgentIdentity) { 190 agent.SetIdentity(identity) 191 var seed io.Reader 192 seed = MakeTestSeed(string(identity)) 193 agent.GenKeys(seed) 194 } 195 196 func NormaliseJSON(json string) string { 197 json = strings.Replace(json, "\n", "", -1) 198 json = strings.Replace(json, " ", "", -1) 199 return strings.Replace(json, ": ", ":", -1) 200 }