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

import (
	"context"
Łukasz Magiera's avatar
Łukasz Magiera committed
5
	"github.com/ipfs/interface-go-ipfs-core/path"
6
	"math"
7 8 9
	"strings"
	"testing"

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

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

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

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

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

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

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

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

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

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

	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))
	}
}

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

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

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

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

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

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

Łukasz Magiera's avatar
Łukasz Magiera committed
131
	err = api.Pin().Add(ctx, path.IpldPath(nd2.Cid()))
132
	if err != nil {
133
		t.Fatal(err)
134 135
	}

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

	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))
	}

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

	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))
	}

Łukasz Magiera's avatar
Łukasz Magiera committed
172 173
	if list[0].Path().String() != path.IpldPath(nd2.Cid()).String() {
		t.Errorf("unexpected path, %s != %s", list[0].Path().String(), path.IpldPath(nd3.Cid()).String())
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 240
	}

	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)
		}
	*/
}