forked from irinazheltisheva/powergate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.go
More file actions
130 lines (113 loc) · 3.2 KB
/
client_test.go
File metadata and controls
130 lines (113 loc) · 3.2 KB
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
package lotus_test
import (
"bytes"
"context"
"crypto/rand"
"io"
"io/ioutil"
"os"
"testing"
"time"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/types"
logging "github.com/ipfs/go-log/v2"
"github.com/stretchr/testify/require"
"github.com/textileio/powergate/tests"
)
const (
tmpDir = "/tmp/powergate/lotusclienttest"
)
func TestMain(m *testing.M) {
if err := os.RemoveAll(tmpDir); err != nil {
panic(err)
}
if _, err := os.Stat(tmpDir); os.IsNotExist(err) {
if err := os.MkdirAll(tmpDir, os.ModePerm); err != nil {
panic("can't create temp dir")
}
}
logging.SetAllLoggers(logging.LevelError)
os.Exit(m.Run())
}
func TestClientImport(t *testing.T) {
clientBuilder, _, _ := tests.CreateLocalDevnet(t, 1)
client, cls, err := clientBuilder()
require.NoError(t, err)
defer cls()
f, err := ioutil.TempFile(tmpDir, "")
require.NoError(t, err)
defer func() {
require.NoError(t, f.Close())
require.NoError(t, os.Remove(f.Name()))
}()
bts := make([]byte, 4)
_, err = rand.Read(bts)
require.NoError(t, err)
_, err = io.Copy(f, bytes.NewReader(bts))
require.NoError(t, err)
ref := api.FileRef{
Path: f.Name(),
}
res, err := client.ClientImport(context.Background(), ref)
require.NoError(t, err)
if !res.Root.Defined() {
t.Errorf("undefined cid from import")
}
}
func TestClientChainNotify(t *testing.T) {
clientBuilder, _, _ := tests.CreateLocalDevnet(t, 1)
client, cls, err := clientBuilder()
require.NoError(t, err)
defer cls()
ch, err := client.ChainNotify(context.Background())
require.NoError(t, err)
// ch is guaranteed to push always current tipset
h := <-ch
if len(h) != 1 {
t.Fatalf("first pushed notification should have length 1")
}
if h[0].Type != "current" || len(h[0].Val.Cids()) == 0 || h[0].Val.Height() == 0 {
t.Fatalf("current head has invalid values")
}
select {
case <-time.After(time.Second * 10):
t.Fatalf("a new block should be received in less than ~10s")
case <-ch:
return
}
}
func TestChainHead(t *testing.T) {
clientBuilder, _, _ := tests.CreateLocalDevnet(t, 1)
client, cls, err := clientBuilder()
require.NoError(t, err)
defer cls()
ts, err := client.ChainHead(context.Background())
require.NoError(t, err)
if len(ts.Cids()) == 0 || len(ts.Blocks()) == 0 || ts.Height() == 0 {
t.Fatalf("invalid tipset")
}
}
func TestChainGetTipset(t *testing.T) {
clientBuilder, _, _ := tests.CreateLocalDevnet(t, 1)
client, cls, err := clientBuilder()
require.NoError(t, err)
defer cls()
ts, err := client.ChainHead(context.Background())
require.NoError(t, err)
pts, err := client.ChainGetTipSet(context.Background(), types.NewTipSetKey(ts.Blocks()[0].Parents...))
require.NoError(t, err)
if len(pts.Cids()) == 0 || len(pts.Blocks()) == 0 || pts.Height() != ts.Height()-1 {
t.Fatalf("invalid tipset")
}
}
func TestGetPeerID(t *testing.T) {
clientBuilder, _, _ := tests.CreateLocalDevnet(t, 1)
client, cls, err := clientBuilder()
require.NoError(t, err)
defer cls()
miners, err := client.StateListMiners(context.Background(), types.EmptyTSK)
require.NoError(t, err)
mi, err := client.StateMinerInfo(context.Background(), miners[0], types.EmptyTSK)
require.NoError(t, err)
require.NoError(t, mi.PeerId.Validate())
}