/
tools.go
114 lines (96 loc) · 2.08 KB
/
tools.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
package test
import (
"bytes"
"fmt"
"net/http"
"os"
"os/exec"
"strings"
"syscall"
"testing"
"github.com/gavv/httpexpect/v2"
)
func Q(
t *testing.T,
cid string,
multiaddr string,
) *httpexpect.Object {
url := GetEnv("GATEWAY_URL", "http://localhost:3333")
// url := GetEnv("GATEWAY_URL", "https://ipfs-check-backend.ipfs.io")
expectedContentType := "application/json"
if url == "https://ipfs-check-backend.ipfs.io" {
// Temporary patch: the current released gateway returns text/plain.
// TODO: when the correct Content-Type is released, remove all code related to this
// override.
expectedContentType = "text/plain"
}
opts := httpexpect.ContentOpts{
MediaType: expectedContentType,
}
e := httpexpect.Default(t, url)
return e.POST("/").
WithQuery("cid", cid).
WithQuery("multiaddr", multiaddr).
Expect().
Status(http.StatusOK).
JSON(opts).Object()
}
func GetEnv(key string, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}
/**
Example of outputs:
```json
{
"CidInDHT": true,
"ConnectionError": "no addresses",
"DataAvailableOverBitswap": {
"Duration": 0,
"Error": "could not connect to peer",
"Found": false,
"Responded": false
},
"PeerFoundInDHT": {}
}
```
*/
func call(name string, xs ...string) string {
cmd := exec.Command(name, xs...)
output, err := cmd.Output()
if err != nil {
panic(err)
}
result := string(output)
return strings.TrimSpace(result)
}
func callWhile(fct func(), name string, xs ...string) {
var output bytes.Buffer
cmd := exec.Command(name, xs...)
cmd.Stdout = &output
err := cmd.Start()
if err != nil {
panic(err)
}
done := make(chan error, 1)
go func() {
done <- cmd.Wait()
}()
defer func() {
select {
case err := <-done:
if err != nil {
panic(err)
} else {
fmt.Printf("Command exited successfully. Output:\n%s\n", output.String())
}
default:
// The command is still running, so we just send the SIGTERM signal
cmd.Process.Signal(syscall.SIGTERM)
fmt.Printf("Command killed. Output:\n%s\n", output.String())
}
}()
fct()
}