pin.go 4.67 KB
Newer Older
1
package tests
2 3 4

import (
	"context"
5
	"math"
6 7 8
	"strings"
	"testing"

Łukasz Magiera's avatar
Łukasz Magiera committed
9 10
	"github.com/ipfs/interface-go-ipfs-core"
	opt "github.com/ipfs/interface-go-ipfs-core/options"
11

Łukasz Magiera's avatar
Łukasz Magiera committed
12 13
	ipldcbor "github.com/ipfs/go-ipld-cbor"
	ipld "github.com/ipfs/go-ipld-format"
14 15
)

16
func (tp *provider) TestPin(t *testing.T) {
17 18 19 20 21 22 23
	tp.hasApi(t, func(api iface.CoreAPI) error {
		if api.Pin() == nil {
			return apiNotImplemented
		}
		return nil
	})

24 25 26
	t.Run("TestPinAdd", tp.TestPinAdd)
	t.Run("TestPinSimple", tp.TestPinSimple)
	t.Run("TestPinRecursive", tp.TestPinRecursive)
27 28
}

29
func (tp *provider) TestPinAdd(t *testing.T) {
30 31
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
32
	api, err := tp.makeAPI(ctx)
33
	if err != nil {
34
		t.Fatal(err)
35 36 37 38
	}

	p, err := api.Unixfs().Add(ctx, strFile("foo")())
	if err != nil {
39
		t.Fatal(err)
40 41 42 43
	}

	err = api.Pin().Add(ctx, p)
	if err != nil {
44
		t.Fatal(err)
45 46 47
	}
}

48
func (tp *provider) TestPinSimple(t *testing.T) {
49 50
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
51
	api, err := tp.makeAPI(ctx)
52
	if err != nil {
53
		t.Fatal(err)
54 55 56 57
	}

	p, err := api.Unixfs().Add(ctx, strFile("foo")())
	if err != nil {
58
		t.Fatal(err)
59 60 61 62
	}

	err = api.Pin().Add(ctx, p)
	if err != nil {
63
		t.Fatal(err)
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
	}

	list, err := api.Pin().Ls(ctx)
	if err != nil {
		t.Fatal(err)
	}

	if len(list) != 1 {
		t.Errorf("unexpected pin list len: %d", len(list))
	}

	if list[0].Path().Cid().String() != p.Cid().String() {
		t.Error("paths don't match")
	}

	if list[0].Type() != "recursive" {
		t.Error("unexpected pin type")
	}

	err = api.Pin().Rm(ctx, p)
	if err != nil {
		t.Fatal(err)
	}

	list, err = api.Pin().Ls(ctx)
	if err != nil {
		t.Fatal(err)
	}

	if len(list) != 0 {
		t.Errorf("unexpected pin list len: %d", len(list))
	}
}

98
func (tp *provider) TestPinRecursive(t *testing.T) {
99 100
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
101
	api, err := tp.makeAPI(ctx)
102
	if err != nil {
103
		t.Fatal(err)
104 105 106 107
	}

	p0, err := api.Unixfs().Add(ctx, strFile("foo")())
	if err != nil {
108
		t.Fatal(err)
109 110 111 112
	}

	p1, err := api.Unixfs().Add(ctx, strFile("bar")())
	if err != nil {
113
		t.Fatal(err)
114 115
	}

116
	nd2, err := ipldcbor.FromJSON(strings.NewReader(`{"lnk": {"/": "`+p0.Cid().String()+`"}}`), math.MaxUint64, -1)
117
	if err != nil {
118
		t.Fatal(err)
119 120
	}

121
	nd3, err := ipldcbor.FromJSON(strings.NewReader(`{"lnk": {"/": "`+p1.Cid().String()+`"}}`), math.MaxUint64, -1)
122
	if err != nil {
123
		t.Fatal(err)
124 125
	}

126
	if err := api.Dag().AddMany(ctx, []ipld.Node{nd2, nd3}); err != nil {
127 128 129
		t.Fatal(err)
	}

130
	err = api.Pin().Add(ctx, iface.IpldPath(nd2.Cid()))
131
	if err != nil {
132
		t.Fatal(err)
133 134
	}

135
	err = api.Pin().Add(ctx, iface.IpldPath(nd3.Cid()), opt.Pin.Recursive(false))
136
	if err != nil {
137
		t.Fatal(err)
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
	}

	list, err := api.Pin().Ls(ctx)
	if err != nil {
		t.Fatal(err)
	}

	if len(list) != 3 {
		t.Errorf("unexpected pin list len: %d", len(list))
	}

	list, err = api.Pin().Ls(ctx, opt.Pin.Type.Direct())
	if err != nil {
		t.Fatal(err)
	}

	if len(list) != 1 {
		t.Errorf("unexpected pin list len: %d", len(list))
	}

158 159
	if list[0].Path().String() != iface.IpldPath(nd3.Cid()).String() {
		t.Errorf("unexpected path, %s != %s", list[0].Path().String(), iface.IpfsPath(nd2.Cid()).String())
160 161 162 163 164 165 166 167 168 169 170
	}

	list, err = api.Pin().Ls(ctx, opt.Pin.Type.Recursive())
	if err != nil {
		t.Fatal(err)
	}

	if len(list) != 1 {
		t.Errorf("unexpected pin list len: %d", len(list))
	}

171 172
	if list[0].Path().String() != iface.IpldPath(nd2.Cid()).String() {
		t.Errorf("unexpected path, %s != %s", list[0].Path().String(), iface.IpldPath(nd3.Cid()).String())
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
	}

	list, err = api.Pin().Ls(ctx, opt.Pin.Type.Indirect())
	if err != nil {
		t.Fatal(err)
	}

	if len(list) != 1 {
		t.Errorf("unexpected pin list len: %d", len(list))
	}

	if list[0].Path().Cid().String() != p0.Cid().String() {
		t.Error("unexpected path")
	}

	res, err := api.Pin().Verify(ctx)
	if err != nil {
		t.Fatal(err)
	}
	n := 0
	for r := range res {
		if !r.Ok() {
			t.Error("expected pin to be ok")
		}
		n++
	}

	if n != 1 {
		t.Errorf("unexpected verify result count: %d", n)
	}

	//TODO: figure out a way to test verify without touching IpfsNode
	/*
		err = api.Block().Rm(ctx, p0, opt.Block.Force(true))
		if err != nil {
			t.Fatal(err)
		}

		res, err = api.Pin().Verify(ctx)
		if err != nil {
			t.Fatal(err)
		}
		n = 0
		for r := range res {
			if r.Ok() {
				t.Error("expected pin to not be ok")
			}

			if len(r.BadNodes()) != 1 {
				t.Fatalf("unexpected badNodes len")
			}

			if r.BadNodes()[0].Path().Cid().String() != p0.Cid().String() {
				t.Error("unexpected badNode path")
			}

			if r.BadNodes()[0].Err().Error() != "merkledag: not found" {
				t.Errorf("unexpected badNode error: %s", r.BadNodes()[0].Err().Error())
			}
			n++
		}

		if n != 1 {
			t.Errorf("unexpected verify result count: %d", n)
		}
	*/
}