-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.go
96 lines (86 loc) · 1.87 KB
/
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
package tests
import (
"context"
"encoding/json"
"fmt"
"net"
"runtime"
"strings"
"testing"
"time"
"beryju.io/gravity/pkg/extconfig"
"beryju.io/gravity/pkg/storage"
"github.com/getsentry/sentry-go"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
clientv3 "go.etcd.io/etcd/client/v3"
)
func MustJSON(in interface{}) string {
j, err := json.Marshal(in)
if err != nil {
panic(err)
}
return string(j)
}
func Context() context.Context {
ctx, _ := context.WithTimeout(context.Background(), 3*time.Second) //nolint
return sentry.StartTransaction(ctx, "test").Context()
}
func RandomString(prefix ...string) string {
str := append(prefix, uuid.New().String())
return strings.Join(str, "-")
}
func AssertEtcd(t *testing.T, c *storage.Client, key *storage.Key, expected ...interface{}) {
args := []clientv3.OpOption{}
if key.IsPrefix() {
args = append(args, clientv3.WithPrefix())
}
values, err := c.Get(
Context(),
key.String(),
args...,
)
assert.NoError(t, err)
assert.Equal(t, len(expected), len(values.Kvs))
for idx, res := range expected {
assert.Equal(t, MustJSON(res), string(values.Kvs[idx].Value))
}
}
func ResetEtcd(t *testing.T) {
ctx := Context()
_, err := extconfig.Get().EtcdClient().Delete(
ctx,
"/",
clientv3.WithPrefix(),
)
assert.NoError(t, err)
}
func HasLocalDocker() bool {
return runtime.GOOS == "linux"
}
func Listen(port int32) string {
if runtime.GOOS == "darwin" {
return fmt.Sprintf(":%d", port)
}
return extconfig.Get().Listen(port)
}
func WaitForPort(port int32) {
max := 30
try := 0
listen := Listen(port)
time.Sleep(500 * time.Millisecond)
for {
ln, err := net.Listen("tcp", listen)
if ln != nil {
_ = ln.Close()
}
if err != nil {
return
}
try += 1
if try >= max {
panic(fmt.Errorf("failed to wait for port '%s' to be listening", listen))
}
time.Sleep(1 * time.Millisecond)
}
}