Skip to content

Commit 10e16a5

Browse files
committed
coreapi: pin tests
License: MIT Signed-off-by: Łukasz Magiera <[email protected]>
1 parent 2bf95f3 commit 10e16a5

File tree

2 files changed

+210
-1
lines changed

2 files changed

+210
-1
lines changed

core/coreapi/pin.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ type badNode struct {
8080
}
8181

8282
func (s *pinStatus) Ok() bool {
83-
return s.Ok()
83+
return s.ok
8484
}
8585

8686
func (s *pinStatus) BadNodes() []coreiface.BadPinNode {

core/coreapi/pin_test.go

+209
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
package coreapi_test
2+
3+
import (
4+
"context"
5+
"strings"
6+
"testing"
7+
)
8+
9+
func TestPinAdd(t *testing.T) {
10+
ctx := context.Background()
11+
_, api, err := makeAPI(ctx)
12+
if err != nil {
13+
t.Error(err)
14+
}
15+
16+
p, err := api.Unixfs().Add(ctx, strings.NewReader("foo"))
17+
if err != nil {
18+
t.Error(err)
19+
}
20+
21+
err = api.Pin().Add(ctx, p)
22+
if err != nil {
23+
t.Error(err)
24+
}
25+
}
26+
27+
func TestPinSimple(t *testing.T) {
28+
ctx := context.Background()
29+
_, api, err := makeAPI(ctx)
30+
if err != nil {
31+
t.Error(err)
32+
}
33+
34+
p, err := api.Unixfs().Add(ctx, strings.NewReader("foo"))
35+
if err != nil {
36+
t.Error(err)
37+
}
38+
39+
err = api.Pin().Add(ctx, p)
40+
if err != nil {
41+
t.Error(err)
42+
}
43+
44+
list, err := api.Pin().Ls(ctx)
45+
if err != nil {
46+
t.Fatal(err)
47+
}
48+
49+
if len(list) != 1 {
50+
t.Errorf("unexpected pin list len: %d", len(list))
51+
}
52+
53+
if list[0].Path().String() != p.String() {
54+
t.Error("paths don't match")
55+
}
56+
57+
if list[0].Type() != "recursive" {
58+
t.Error("unexpected pin type")
59+
}
60+
61+
err = api.Pin().Rm(ctx, p)
62+
if err != nil {
63+
t.Fatal(err)
64+
}
65+
66+
list, err = api.Pin().Ls(ctx)
67+
if err != nil {
68+
t.Fatal(err)
69+
}
70+
71+
if len(list) != 0 {
72+
t.Errorf("unexpected pin list len: %d", len(list))
73+
}
74+
}
75+
76+
func TestPinRecursive(t *testing.T) {
77+
ctx := context.Background()
78+
nd, api, err := makeAPI(ctx)
79+
if err != nil {
80+
t.Error(err)
81+
}
82+
83+
p0, err := api.Unixfs().Add(ctx, strings.NewReader("foo"))
84+
if err != nil {
85+
t.Error(err)
86+
}
87+
88+
p1, err := api.Unixfs().Add(ctx, strings.NewReader("bar"))
89+
if err != nil {
90+
t.Error(err)
91+
}
92+
93+
p2, err := api.Dag().Put(ctx, strings.NewReader(`{"lnk": {"/": "`+p0.Cid().String()+`"}}`))
94+
if err != nil {
95+
t.Error(err)
96+
}
97+
98+
p3, err := api.Dag().Put(ctx, strings.NewReader(`{"lnk": {"/": "`+p1.Cid().String()+`"}}`))
99+
if err != nil {
100+
t.Error(err)
101+
}
102+
103+
err = api.Pin().Add(ctx, p2)
104+
if err != nil {
105+
t.Error(err)
106+
}
107+
108+
err = api.Pin().Add(ctx, p3, api.Pin().WithRecursive(false))
109+
if err != nil {
110+
t.Error(err)
111+
}
112+
113+
list, err := api.Pin().Ls(ctx)
114+
if err != nil {
115+
t.Fatal(err)
116+
}
117+
118+
if len(list) != 3 {
119+
t.Errorf("unexpected pin list len: %d", len(list))
120+
}
121+
122+
list, err = api.Pin().Ls(ctx, api.Pin().WithType("direct"))
123+
if err != nil {
124+
t.Fatal(err)
125+
}
126+
127+
if len(list) != 1 {
128+
t.Errorf("unexpected pin list len: %d", len(list))
129+
}
130+
131+
if list[0].Path().String() != p3.String() {
132+
t.Error("unexpected path")
133+
}
134+
135+
list, err = api.Pin().Ls(ctx, api.Pin().WithType("recursive"))
136+
if err != nil {
137+
t.Fatal(err)
138+
}
139+
140+
if len(list) != 1 {
141+
t.Errorf("unexpected pin list len: %d", len(list))
142+
}
143+
144+
if list[0].Path().String() != p2.String() {
145+
t.Error("unexpected path")
146+
}
147+
148+
list, err = api.Pin().Ls(ctx, api.Pin().WithType("indirect"))
149+
if err != nil {
150+
t.Fatal(err)
151+
}
152+
153+
if len(list) != 1 {
154+
t.Errorf("unexpected pin list len: %d", len(list))
155+
}
156+
157+
if list[0].Path().String() != p0.String() {
158+
t.Error("unexpected path")
159+
}
160+
161+
res, err := api.Pin().Verify(ctx)
162+
if err != nil {
163+
t.Fatal(err)
164+
}
165+
n := 0
166+
for r := range res {
167+
if !r.Ok() {
168+
t.Error("expected pin to be ok")
169+
}
170+
n++
171+
}
172+
173+
if n != 1 {
174+
t.Errorf("unexpected verify result count: %d", n)
175+
}
176+
177+
err = nd.Blockstore.DeleteBlock(p0.Cid())
178+
if err != nil {
179+
t.Fatal(err)
180+
}
181+
182+
res, err = api.Pin().Verify(ctx)
183+
if err != nil {
184+
t.Fatal(err)
185+
}
186+
n = 0
187+
for r := range res {
188+
if r.Ok() {
189+
t.Error("expected pin to not be ok")
190+
}
191+
192+
if len(r.BadNodes()) != 1 {
193+
t.Fatalf("unexpected badNodes len")
194+
}
195+
196+
if r.BadNodes()[0].Path().String() != p0.String() {
197+
t.Error("unexpected badNode path")
198+
}
199+
200+
if r.BadNodes()[0].Err().Error() != "merkledag: not found" {
201+
t.Errorf("unexpected badNode error: %s", r.BadNodes()[0].Err().Error())
202+
}
203+
n++
204+
}
205+
206+
if n != 1 {
207+
t.Errorf("unexpected verify result count: %d", n)
208+
}
209+
}

0 commit comments

Comments
 (0)