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

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

9 10 11
	iface "gitlab.dms3.io/dms3/interface-go-dms3-core"
	opt "gitlab.dms3.io/dms3/interface-go-dms3-core/options"
	"gitlab.dms3.io/dms3/interface-go-dms3-core/path"
12

13 14 15
	"gitlab.dms3.io/dms3/go-cid"
	ldcbor "gitlab.dms3.io/dms3/go-ld-cbor"
	ld "gitlab.dms3.io/dms3/go-ld-format"
16 17
)

Łukasz Magiera's avatar
Łukasz Magiera committed
18
func (tp *TestSuite) TestPin(t *testing.T) {
19 20 21 22 23 24 25
	tp.hasApi(t, func(api iface.CoreAPI) error {
		if api.Pin() == nil {
			return apiNotImplemented
		}
		return nil
	})

26 27 28
	t.Run("TestPinAdd", tp.TestPinAdd)
	t.Run("TestPinSimple", tp.TestPinSimple)
	t.Run("TestPinRecursive", tp.TestPinRecursive)
29 30
	t.Run("TestPinLsIndirect", tp.TestPinLsIndirect)
	t.Run("TestPinLsPrecedence", tp.TestPinLsPrecedence)
Michael Muré's avatar
Michael Muré committed
31
	t.Run("TestPinIsPinned", tp.TestPinIsPinned)
32 33
}

Łukasz Magiera's avatar
Łukasz Magiera committed
34
func (tp *TestSuite) TestPinAdd(t *testing.T) {
35 36
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
37
	api, err := tp.makeAPI(ctx)
38
	if err != nil {
39
		t.Fatal(err)
40 41 42 43
	}

	p, err := api.Unixfs().Add(ctx, strFile("foo")())
	if err != nil {
44
		t.Fatal(err)
45 46 47 48
	}

	err = api.Pin().Add(ctx, p)
	if err != nil {
49
		t.Fatal(err)
50 51 52
	}
}

Łukasz Magiera's avatar
Łukasz Magiera committed
53
func (tp *TestSuite) TestPinSimple(t *testing.T) {
54 55
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
56
	api, err := tp.makeAPI(ctx)
57
	if err != nil {
58
		t.Fatal(err)
59 60 61 62
	}

	p, err := api.Unixfs().Add(ctx, strFile("foo")())
	if err != nil {
63
		t.Fatal(err)
64 65 66 67
	}

	err = api.Pin().Add(ctx, p)
	if err != nil {
68
		t.Fatal(err)
69 70
	}

71
	list, err := accPins(api.Pin().Ls(ctx))
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
	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")
	}

Michael Muré's avatar
Michael Muré committed
88 89
	assertIsPinned(t, ctx, api, p, "recursive")

90 91 92 93 94
	err = api.Pin().Rm(ctx, p)
	if err != nil {
		t.Fatal(err)
	}

95
	list, err = accPins(api.Pin().Ls(ctx))
96 97 98 99 100 101 102 103 104
	if err != nil {
		t.Fatal(err)
	}

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

Łukasz Magiera's avatar
Łukasz Magiera committed
105
func (tp *TestSuite) TestPinRecursive(t *testing.T) {
106 107
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
108
	api, err := tp.makeAPI(ctx)
109
	if err != nil {
110
		t.Fatal(err)
111 112 113 114
	}

	p0, err := api.Unixfs().Add(ctx, strFile("foo")())
	if err != nil {
115
		t.Fatal(err)
116 117 118 119
	}

	p1, err := api.Unixfs().Add(ctx, strFile("bar")())
	if err != nil {
120
		t.Fatal(err)
121 122
	}

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

128
	nd3, err := ldcbor.FromJSON(strings.NewReader(`{"lnk": {"/": "`+p1.Cid().String()+`"}}`), math.MaxUint64, -1)
129
	if err != nil {
130
		t.Fatal(err)
131 132
	}

133
	if err := api.Dag().AddMany(ctx, []ld.Node{nd2, nd3}); err != nil {
134 135 136
		t.Fatal(err)
	}

137
	err = api.Pin().Add(ctx, path.Dms3LdPath(nd2.Cid()))
138
	if err != nil {
139
		t.Fatal(err)
140 141
	}

142
	err = api.Pin().Add(ctx, path.Dms3LdPath(nd3.Cid()), opt.Pin.Recursive(false))
143
	if err != nil {
144
		t.Fatal(err)
145 146
	}

147
	list, err := accPins(api.Pin().Ls(ctx))
148 149 150 151 152 153 154 155
	if err != nil {
		t.Fatal(err)
	}

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

Michael Muré's avatar
Michael Muré committed
156
	list, err = accPins(api.Pin().Ls(ctx, opt.Pin.Ls.Direct()))
157 158 159 160 161 162 163 164
	if err != nil {
		t.Fatal(err)
	}

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

165 166
	if list[0].Path().String() != path.Dms3LdPath(nd3.Cid()).String() {
		t.Errorf("unexpected path, %s != %s", list[0].Path().String(), path.Dms3Path(nd3.Cid()).String())
167 168
	}

Michael Muré's avatar
Michael Muré committed
169
	list, err = accPins(api.Pin().Ls(ctx, opt.Pin.Ls.Recursive()))
170 171 172 173 174 175 176 177
	if err != nil {
		t.Fatal(err)
	}

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

178 179
	if list[0].Path().String() != path.Dms3LdPath(nd2.Cid()).String() {
		t.Errorf("unexpected path, %s != %s", list[0].Path().String(), path.Dms3LdPath(nd2.Cid()).String())
180 181
	}

Michael Muré's avatar
Michael Muré committed
182
	list, err = accPins(api.Pin().Ls(ctx, opt.Pin.Ls.Indirect()))
183 184 185 186 187 188 189 190 191
	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() {
Michael Muré's avatar
Michael Muré committed
192
		t.Errorf("unexpected path, %s != %s", list[0].Path().Cid().String(), p0.Cid().String())
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
	}

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

211
	//TODO: figure out a way to test verify without touching Dms3Node
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 241 242 243 244 245 246
	/*
		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)
		}
	*/
}
247 248 249 250 251 252 253 254 255 256 257 258

// TestPinLsIndirect verifies that indirect nodes are listed by pin ls even if a parent node is directly pinned
func (tp *TestSuite) TestPinLsIndirect(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	api, err := tp.makeAPI(ctx)
	if err != nil {
		t.Fatal(err)
	}

	leaf, parent, grandparent := getThreeChainedNodes(t, ctx, api, "foo")

259
	err = api.Pin().Add(ctx, path.Dms3LdPath(grandparent.Cid()))
260 261 262 263
	if err != nil {
		t.Fatal(err)
	}

264
	err = api.Pin().Add(ctx, path.Dms3LdPath(parent.Cid()), opt.Pin.Recursive(false))
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
	if err != nil {
		t.Fatal(err)
	}

	assertPinTypes(t, ctx, api, []cidContainer{grandparent}, []cidContainer{parent}, []cidContainer{leaf})
}

// TestPinLsPrecedence verifies the precedence of pins (recursive > direct > indirect)
func (tp *TestSuite) TestPinLsPrecedence(t *testing.T) {
	// Testing precedence of recursive, direct and indirect pins
	// Results should be recursive > indirect, direct > indirect, and recursive > direct

	t.Run("TestPinLsPredenceRecursiveIndirect", tp.TestPinLsPredenceRecursiveIndirect)
	t.Run("TestPinLsPrecedenceDirectIndirect", tp.TestPinLsPrecedenceDirectIndirect)
	t.Run("TestPinLsPrecedenceRecursiveDirect", tp.TestPinLsPrecedenceRecursiveDirect)
}

func (tp *TestSuite) TestPinLsPredenceRecursiveIndirect(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	api, err := tp.makeAPI(ctx)
	if err != nil {
		t.Fatal(err)
	}

	// Test recursive > indirect
	leaf, parent, grandparent := getThreeChainedNodes(t, ctx, api, "recursive > indirect")

293
	err = api.Pin().Add(ctx, path.Dms3LdPath(grandparent.Cid()))
294 295 296 297
	if err != nil {
		t.Fatal(err)
	}

298
	err = api.Pin().Add(ctx, path.Dms3LdPath(parent.Cid()))
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
	if err != nil {
		t.Fatal(err)
	}

	assertPinTypes(t, ctx, api, []cidContainer{grandparent, parent}, []cidContainer{}, []cidContainer{leaf})
}

func (tp *TestSuite) TestPinLsPrecedenceDirectIndirect(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	api, err := tp.makeAPI(ctx)
	if err != nil {
		t.Fatal(err)
	}

	// Test direct > indirect
	leaf, parent, grandparent := getThreeChainedNodes(t, ctx, api, "direct > indirect")

317
	err = api.Pin().Add(ctx, path.Dms3LdPath(grandparent.Cid()))
318 319 320 321
	if err != nil {
		t.Fatal(err)
	}

322
	err = api.Pin().Add(ctx, path.Dms3LdPath(parent.Cid()), opt.Pin.Recursive(false))
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
	if err != nil {
		t.Fatal(err)
	}

	assertPinTypes(t, ctx, api, []cidContainer{grandparent}, []cidContainer{parent}, []cidContainer{leaf})
}

func (tp *TestSuite) TestPinLsPrecedenceRecursiveDirect(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	api, err := tp.makeAPI(ctx)
	if err != nil {
		t.Fatal(err)
	}

	// Test recursive > direct
	leaf, parent, grandparent := getThreeChainedNodes(t, ctx, api, "recursive + direct = error")

341
	err = api.Pin().Add(ctx, path.Dms3LdPath(parent.Cid()))
342 343 344 345
	if err != nil {
		t.Fatal(err)
	}

346
	err = api.Pin().Add(ctx, path.Dms3LdPath(parent.Cid()), opt.Pin.Recursive(false))
347 348 349 350 351 352
	if err == nil {
		t.Fatal("expected error directly pinning a recursively pinned node")
	}

	assertPinTypes(t, ctx, api, []cidContainer{parent}, []cidContainer{}, []cidContainer{leaf})

353
	err = api.Pin().Add(ctx, path.Dms3LdPath(grandparent.Cid()), opt.Pin.Recursive(false))
354 355 356 357
	if err != nil {
		t.Fatal(err)
	}

358
	err = api.Pin().Add(ctx, path.Dms3LdPath(grandparent.Cid()))
359 360 361 362 363 364 365
	if err != nil {
		t.Fatal(err)
	}

	assertPinTypes(t, ctx, api, []cidContainer{grandparent, parent}, []cidContainer{}, []cidContainer{leaf})
}

Michael Muré's avatar
Michael Muré committed
366 367 368 369 370 371 372 373 374 375
func (tp *TestSuite) TestPinIsPinned(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	api, err := tp.makeAPI(ctx)
	if err != nil {
		t.Fatal(err)
	}

	leaf, parent, grandparent := getThreeChainedNodes(t, ctx, api, "foofoo")

376 377 378
	assertNotPinned(t, ctx, api, path.Dms3LdPath(grandparent.Cid()))
	assertNotPinned(t, ctx, api, path.Dms3LdPath(parent.Cid()))
	assertNotPinned(t, ctx, api, path.Dms3LdPath(leaf.Cid()))
Michael Muré's avatar
Michael Muré committed
379

380
	err = api.Pin().Add(ctx, path.Dms3LdPath(parent.Cid()), opt.Pin.Recursive(true))
Michael Muré's avatar
Michael Muré committed
381 382 383 384
	if err != nil {
		t.Fatal(err)
	}

385 386 387
	assertNotPinned(t, ctx, api, path.Dms3LdPath(grandparent.Cid()))
	assertIsPinned(t, ctx, api, path.Dms3LdPath(parent.Cid()), "recursive")
	assertIsPinned(t, ctx, api, path.Dms3LdPath(leaf.Cid()), "indirect")
Michael Muré's avatar
Michael Muré committed
388

389
	err = api.Pin().Add(ctx, path.Dms3LdPath(grandparent.Cid()), opt.Pin.Recursive(false))
Michael Muré's avatar
Michael Muré committed
390 391 392 393
	if err != nil {
		t.Fatal(err)
	}

394 395 396
	assertIsPinned(t, ctx, api, path.Dms3LdPath(grandparent.Cid()), "direct")
	assertIsPinned(t, ctx, api, path.Dms3LdPath(parent.Cid()), "recursive")
	assertIsPinned(t, ctx, api, path.Dms3LdPath(leaf.Cid()), "indirect")
Michael Muré's avatar
Michael Muré committed
397 398
}

399 400 401 402 403 404 405 406 407 408
type cidContainer interface {
	Cid() cid.Cid
}

func getThreeChainedNodes(t *testing.T, ctx context.Context, api iface.CoreAPI, leafData string) (cidContainer, cidContainer, cidContainer) {
	leaf, err := api.Unixfs().Add(ctx, strFile(leafData)())
	if err != nil {
		t.Fatal(err)
	}

409
	parent, err := ldcbor.FromJSON(strings.NewReader(`{"lnk": {"/": "`+leaf.Cid().String()+`"}}`), math.MaxUint64, -1)
410 411 412 413
	if err != nil {
		t.Fatal(err)
	}

414
	grandparent, err := ldcbor.FromJSON(strings.NewReader(`{"lnk": {"/": "`+parent.Cid().String()+`"}}`), math.MaxUint64, -1)
415 416 417 418
	if err != nil {
		t.Fatal(err)
	}

419
	if err := api.Dag().AddMany(ctx, []ld.Node{parent, grandparent}); err != nil {
420 421 422 423 424 425 426 427 428
		t.Fatal(err)
	}

	return leaf, parent, grandparent
}

func assertPinTypes(t *testing.T, ctx context.Context, api iface.CoreAPI, recusive, direct, indirect []cidContainer) {
	assertPinLsAllConsistency(t, ctx, api)

Michael Muré's avatar
Michael Muré committed
429
	list, err := accPins(api.Pin().Ls(ctx, opt.Pin.Ls.Recursive()))
430 431 432 433 434 435
	if err != nil {
		t.Fatal(err)
	}

	assertPinCids(t, list, recusive...)

Michael Muré's avatar
Michael Muré committed
436
	list, err = accPins(api.Pin().Ls(ctx, opt.Pin.Ls.Direct()))
437 438 439 440 441 442
	if err != nil {
		t.Fatal(err)
	}

	assertPinCids(t, list, direct...)

Michael Muré's avatar
Michael Muré committed
443
	list, err = accPins(api.Pin().Ls(ctx, opt.Pin.Ls.Indirect()))
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
	if err != nil {
		t.Fatal(err)
	}

	assertPinCids(t, list, indirect...)
}

// assertPinCids verifies that the pins match the expected cids
func assertPinCids(t *testing.T, pins []iface.Pin, cids ...cidContainer) {
	t.Helper()

	if expected, actual := len(cids), len(pins); expected != actual {
		t.Fatalf("expected pin list to have len %d, was %d", expected, actual)
	}

	cSet := cid.NewSet()
	for _, c := range cids {
		cSet.Add(c.Cid())
	}

	valid := true
	for _, p := range pins {
		c := p.Path().Cid()
		if cSet.Has(c) {
			cSet.Remove(c)
		} else {
			valid = false
			break
		}
	}

	valid = valid && cSet.Len() == 0

	if !valid {
		pinStrs := make([]string, len(pins))
		for i, p := range pins {
			pinStrs[i] = p.Path().Cid().String()
		}
		pathStrs := make([]string, len(cids))
		for i, c := range cids {
			pathStrs[i] = c.Cid().String()
		}
		t.Fatalf("expected: %s \nactual: %s", strings.Join(pathStrs, ", "), strings.Join(pinStrs, ", "))
	}
}

// assertPinLsAllConsistency verifies that listing all pins gives the same result as listing the pin types individually
func assertPinLsAllConsistency(t *testing.T, ctx context.Context, api iface.CoreAPI) {
	t.Helper()
493
	allPins, err := accPins(api.Pin().Ls(ctx))
494 495 496 497 498 499 500 501 502 503 504
	if err != nil {
		t.Fatal(err)
	}

	type pinTypeProps struct {
		*cid.Set
		opt.PinLsOption
	}

	all, recursive, direct, indirect := cid.NewSet(), cid.NewSet(), cid.NewSet(), cid.NewSet()
	typeMap := map[string]*pinTypeProps{
Michael Muré's avatar
Michael Muré committed
505 506 507
		"recursive": {recursive, opt.Pin.Ls.Recursive()},
		"direct":    {direct, opt.Pin.Ls.Direct()},
		"indirect":  {indirect, opt.Pin.Ls.Indirect()},
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
	}

	for _, p := range allPins {
		if !all.Visit(p.Path().Cid()) {
			t.Fatalf("pin ls returned the same cid multiple times")
		}

		typeStr := p.Type()
		if typeSet, ok := typeMap[p.Type()]; ok {
			typeSet.Add(p.Path().Cid())
		} else {
			t.Fatalf("unknown pin type: %s", typeStr)
		}
	}

	for typeStr, pinProps := range typeMap {
524
		pins, err := accPins(api.Pin().Ls(ctx, pinProps.PinLsOption))
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
		if err != nil {
			t.Fatal(err)
		}

		if expected, actual := len(pins), pinProps.Set.Len(); expected != actual {
			t.Fatalf("pin ls all has %d pins of type %s, but pin ls for the type has %d", expected, typeStr, actual)
		}

		for _, p := range pins {
			if pinType := p.Type(); pinType != typeStr {
				t.Fatalf("returned wrong pin type: expected %s, got %s", typeStr, pinType)
			}

			if c := p.Path().Cid(); !pinProps.Has(c) {
				t.Fatalf("%s expected to be in pin ls all as type %s", c.String(), typeStr)
			}
		}
	}
}
544

Michael Muré's avatar
Michael Muré committed
545 546 547 548
func assertIsPinned(t *testing.T, ctx context.Context, api iface.CoreAPI, p path.Path, typeStr string) {
	t.Helper()
	withType, err := opt.Pin.IsPinned.Type(typeStr)
	if err != nil {
549
		t.Fatal("unhandled pin type")
Michael Muré's avatar
Michael Muré committed
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
	}

	whyPinned, pinned, err := api.Pin().IsPinned(ctx, p, withType)
	if err != nil {
		t.Fatal(err)
	}

	if !pinned {
		t.Fatalf("%s expected to be pinned with type %s", p, typeStr)
	}

	switch typeStr {
	case "recursive", "direct":
		if typeStr != whyPinned {
			t.Fatalf("reason for pinning expected to be %s for %s, got %s", typeStr, p, whyPinned)
		}
	case "indirect":
		if whyPinned == "" {
			t.Fatalf("expected to have a pin reason for %s", p)
		}
	}
}

func assertNotPinned(t *testing.T, ctx context.Context, api iface.CoreAPI, p path.Path) {
	t.Helper()

	_, pinned, err := api.Pin().IsPinned(ctx, p)
	if err != nil {
		t.Fatal(err)
	}

	if pinned {
		t.Fatalf("%s expected to not be pinned", p)
	}
}

586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
func accPins(pins <-chan iface.Pin, err error) ([]iface.Pin, error) {
	if err != nil {
		return nil, err
	}

	var result []iface.Pin

	for pin := range pins {
		if pin.Err() != nil {
			return nil, pin.Err()
		}
		result = append(result, pin)
	}

	return result, nil
}