flatfs_test.go 11.7 KB
Newer Older
Tommi Virtanen's avatar
Tommi Virtanen committed
1 2 3
package flatfs_test

import (
Jeromy's avatar
Jeromy committed
4
	"encoding/base32"
Tommi Virtanen's avatar
Tommi Virtanen committed
5
	"io/ioutil"
6
	"math"
Tommi Virtanen's avatar
Tommi Virtanen committed
7 8
	"os"
	"path/filepath"
9
	"runtime"
Tommi Virtanen's avatar
Tommi Virtanen committed
10 11
	"testing"

Jeromy's avatar
Jeromy committed
12 13 14
	"github.com/ipfs/go-datastore"
	"github.com/ipfs/go-datastore/query"
	dstest "github.com/ipfs/go-datastore/test"
Jakub Sztandera's avatar
Jakub Sztandera committed
15
	"github.com/ipfs/go-ds-flatfs"
Jeromy's avatar
Jeromy committed
16

Jakub Sztandera's avatar
Jakub Sztandera committed
17
	rand "github.com/dustin/randbo"
Tommi Virtanen's avatar
Tommi Virtanen committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
)

func tempdir(t testing.TB) (path string, cleanup func()) {
	path, err := ioutil.TempDir("", "test-datastore-flatfs-")
	if err != nil {
		t.Fatalf("cannot create temp directory: %v", err)
	}

	cleanup = func() {
		if err := os.RemoveAll(path); err != nil {
			t.Errorf("tempdir cleanup failed: %v", err)
		}
	}
	return path, cleanup
}

34 35 36
func tryAllShardFuncs(t *testing.T, testFunc func(mkShardFunc, *testing.T)) {
	t.Run("prefix", func(t *testing.T) { testFunc(flatfs.Prefix, t) })
	t.Run("suffix", func(t *testing.T) { testFunc(flatfs.Suffix, t) })
37
	t.Run("next-to-last", func(t *testing.T) { testFunc(flatfs.NextToLast, t) })
38 39
}

Tommi Virtanen's avatar
Tommi Virtanen committed
40 41 42 43
func TestPutBadValueType(t *testing.T) {
	temp, cleanup := tempdir(t)
	defer cleanup()

44
	fs, err := flatfs.New(temp, flatfs.Prefix(2), false)
Tommi Virtanen's avatar
Tommi Virtanen committed
45 46 47 48 49 50 51 52 53 54
	if err != nil {
		t.Fatalf("New fail: %v\n", err)
	}

	err = fs.Put(datastore.NewKey("quux"), 22)
	if g, e := err, datastore.ErrInvalidType; g != e {
		t.Fatalf("expected ErrInvalidType, got: %v\n", g)
	}
}

55 56 57
type mkShardFunc func(int) flatfs.ShardFunc

func testPut(dirFunc mkShardFunc, t *testing.T) {
Tommi Virtanen's avatar
Tommi Virtanen committed
58 59 60
	temp, cleanup := tempdir(t)
	defer cleanup()

61
	fs, err := flatfs.New(temp, dirFunc(2), false)
Tommi Virtanen's avatar
Tommi Virtanen committed
62 63 64 65 66 67 68 69 70 71
	if err != nil {
		t.Fatalf("New fail: %v\n", err)
	}

	err = fs.Put(datastore.NewKey("quux"), []byte("foobar"))
	if err != nil {
		t.Fatalf("Put fail: %v\n", err)
	}
}

72
func TestPut(t *testing.T) { tryAllShardFuncs(t, testPut) }
73 74

func testGet(dirFunc mkShardFunc, t *testing.T) {
Tommi Virtanen's avatar
Tommi Virtanen committed
75 76 77
	temp, cleanup := tempdir(t)
	defer cleanup()

78
	fs, err := flatfs.New(temp, dirFunc(2), false)
Tommi Virtanen's avatar
Tommi Virtanen committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
	if err != nil {
		t.Fatalf("New fail: %v\n", err)
	}

	const input = "foobar"
	err = fs.Put(datastore.NewKey("quux"), []byte(input))
	if err != nil {
		t.Fatalf("Put fail: %v\n", err)
	}

	data, err := fs.Get(datastore.NewKey("quux"))
	if err != nil {
		t.Fatalf("Get failed: %v", err)
	}
	buf, ok := data.([]byte)
	if !ok {
		t.Fatalf("expected []byte from Get, got %T: %v", data, data)
	}
	if g, e := string(buf), input; g != e {
		t.Fatalf("Get gave wrong content: %q != %q", g, e)
	}
}

102
func TestGet(t *testing.T) { tryAllShardFuncs(t, testGet) }
103 104

func testPutOverwrite(dirFunc mkShardFunc, t *testing.T) {
Tommi Virtanen's avatar
Tommi Virtanen committed
105 106 107
	temp, cleanup := tempdir(t)
	defer cleanup()

108
	fs, err := flatfs.New(temp, dirFunc(2), false)
Tommi Virtanen's avatar
Tommi Virtanen committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
	if err != nil {
		t.Fatalf("New fail: %v\n", err)
	}

	const (
		loser  = "foobar"
		winner = "xyzzy"
	)
	err = fs.Put(datastore.NewKey("quux"), []byte(loser))
	if err != nil {
		t.Fatalf("Put fail: %v\n", err)
	}

	err = fs.Put(datastore.NewKey("quux"), []byte(winner))
	if err != nil {
		t.Fatalf("Put fail: %v\n", err)
	}

	data, err := fs.Get(datastore.NewKey("quux"))
	if err != nil {
		t.Fatalf("Get failed: %v", err)
	}
	if g, e := string(data.([]byte)), winner; g != e {
		t.Fatalf("Get gave wrong content: %q != %q", g, e)
	}
}

136
func TestPutOverwrite(t *testing.T) { tryAllShardFuncs(t, testPutOverwrite) }
137 138

func testGetNotFoundError(dirFunc mkShardFunc, t *testing.T) {
Tommi Virtanen's avatar
Tommi Virtanen committed
139 140 141
	temp, cleanup := tempdir(t)
	defer cleanup()

142
	fs, err := flatfs.New(temp, dirFunc(2), false)
Tommi Virtanen's avatar
Tommi Virtanen committed
143 144 145 146 147 148 149 150 151 152
	if err != nil {
		t.Fatalf("New fail: %v\n", err)
	}

	_, err = fs.Get(datastore.NewKey("quux"))
	if g, e := err, datastore.ErrNotFound; g != e {
		t.Fatalf("expected ErrNotFound, got: %v\n", g)
	}
}

153
func TestGetNotFoundError(t *testing.T) { tryAllShardFuncs(t, testGetNotFoundError) }
154 155 156 157 158 159 160 161 162

type params struct {
	what    string
	dir     string
	key     string
	dirFunc mkShardFunc
}

func testStorage(p *params, t *testing.T) {
Tommi Virtanen's avatar
Tommi Virtanen committed
163 164 165
	temp, cleanup := tempdir(t)
	defer cleanup()

166 167
	target := p.dir + string(os.PathSeparator) + p.key + ".data"
	fs, err := flatfs.New(temp, p.dirFunc(len(p.dir)), false)
Tommi Virtanen's avatar
Tommi Virtanen committed
168 169 170 171
	if err != nil {
		t.Fatalf("New fail: %v\n", err)
	}

172
	err = fs.Put(datastore.NewKey(p.key), []byte("foobar"))
Tommi Virtanen's avatar
Tommi Virtanen committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
	if err != nil {
		t.Fatalf("Put fail: %v\n", err)
	}

	seen := false
	walk := func(absPath string, fi os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		path, err := filepath.Rel(temp, absPath)
		if err != nil {
			return err
		}
		switch path {
		case ".", "..":
			// ignore
189
		case p.dir:
Tommi Virtanen's avatar
Tommi Virtanen committed
190
			if !fi.IsDir() {
191
				t.Errorf("%s directory is not a file? %v", p.what, fi.Mode())
Tommi Virtanen's avatar
Tommi Virtanen committed
192 193 194 195 196 197 198 199
			}
			// we know it's there if we see the file, nothing more to
			// do here
		case target:
			seen = true
			if !fi.Mode().IsRegular() {
				t.Errorf("expected a regular file, mode: %04o", fi.Mode())
			}
200 201 202 203
			if runtime.GOOS != "windows" {
				if g, e := fi.Mode()&os.ModePerm&0007, os.FileMode(0000); g != e {
					t.Errorf("file should not be world accessible: %04o", fi.Mode())
				}
Tommi Virtanen's avatar
Tommi Virtanen committed
204 205 206 207 208 209 210 211 212 213 214 215 216
			}
		default:
			t.Errorf("saw unexpected directory entry: %q %v", path, fi.Mode())
		}
		return nil
	}
	if err := filepath.Walk(temp, walk); err != nil {
		t.Fatal("walk: %v", err)
	}
	if !seen {
		t.Error("did not see the data file")
	}
}
Tommi Virtanen's avatar
Tommi Virtanen committed
217

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
func TestStorage(t *testing.T) {
	t.Run("prefix", func(t *testing.T) {
		testStorage(&params{
			what:    "prefix",
			dir:     "qu",
			key:     "quux",
			dirFunc: flatfs.Prefix,
		}, t)
	})
	t.Run("suffix", func(t *testing.T) {
		testStorage(&params{
			what:    "suffix",
			dir:     "ux",
			key:     "quux",
			dirFunc: flatfs.Suffix,
		}, t)
	})
235 236 237 238 239 240 241 242
	t.Run("next-to-last", func(t *testing.T) {
		testStorage(&params{
			what:    "next-to-last",
			dir:     "uu",
			key:     "quux",
			dirFunc: flatfs.NextToLast,
		}, t)
	})
243 244 245
}

func testHasNotFound(dirFunc mkShardFunc, t *testing.T) {
Tommi Virtanen's avatar
Tommi Virtanen committed
246 247 248
	temp, cleanup := tempdir(t)
	defer cleanup()

249
	fs, err := flatfs.New(temp, dirFunc(2), false)
Tommi Virtanen's avatar
Tommi Virtanen committed
250 251 252 253 254 255 256 257 258 259 260 261 262
	if err != nil {
		t.Fatalf("New fail: %v\n", err)
	}

	found, err := fs.Has(datastore.NewKey("quux"))
	if err != nil {
		t.Fatalf("Has fail: %v\n", err)
	}
	if g, e := found, false; g != e {
		t.Fatalf("wrong Has: %v != %v", g, e)
	}
}

263
func TestHasNotFound(t *testing.T) { tryAllShardFuncs(t, testHasNotFound) }
264 265

func testHasFound(dirFunc mkShardFunc, t *testing.T) {
Tommi Virtanen's avatar
Tommi Virtanen committed
266 267 268
	temp, cleanup := tempdir(t)
	defer cleanup()

269
	fs, err := flatfs.New(temp, dirFunc(2), false)
Tommi Virtanen's avatar
Tommi Virtanen committed
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
	if err != nil {
		t.Fatalf("New fail: %v\n", err)
	}
	err = fs.Put(datastore.NewKey("quux"), []byte("foobar"))
	if err != nil {
		t.Fatalf("Put fail: %v\n", err)
	}

	found, err := fs.Has(datastore.NewKey("quux"))
	if err != nil {
		t.Fatalf("Has fail: %v\n", err)
	}
	if g, e := found, true; g != e {
		t.Fatalf("wrong Has: %v != %v", g, e)
	}
}
Tommi Virtanen's avatar
Tommi Virtanen committed
286

287
func TestHasFound(t *testing.T) { tryAllShardFuncs(t, testHasFound) }
288 289

func testDeleteNotFound(dirFunc mkShardFunc, t *testing.T) {
Tommi Virtanen's avatar
Tommi Virtanen committed
290 291 292
	temp, cleanup := tempdir(t)
	defer cleanup()

293
	fs, err := flatfs.New(temp, dirFunc(2), false)
Tommi Virtanen's avatar
Tommi Virtanen committed
294 295 296 297 298 299 300 301 302 303
	if err != nil {
		t.Fatalf("New fail: %v\n", err)
	}

	err = fs.Delete(datastore.NewKey("quux"))
	if g, e := err, datastore.ErrNotFound; g != e {
		t.Fatalf("expected ErrNotFound, got: %v\n", g)
	}
}

304
func TestDeleteNotFound(t *testing.T) { tryAllShardFuncs(t, testDeleteNotFound) }
305 306

func testDeleteFound(dirFunc mkShardFunc, t *testing.T) {
Tommi Virtanen's avatar
Tommi Virtanen committed
307 308 309
	temp, cleanup := tempdir(t)
	defer cleanup()

310
	fs, err := flatfs.New(temp, dirFunc(2), false)
Tommi Virtanen's avatar
Tommi Virtanen committed
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
	if err != nil {
		t.Fatalf("New fail: %v\n", err)
	}
	err = fs.Put(datastore.NewKey("quux"), []byte("foobar"))
	if err != nil {
		t.Fatalf("Put fail: %v\n", err)
	}

	err = fs.Delete(datastore.NewKey("quux"))
	if err != nil {
		t.Fatalf("Delete fail: %v\n", err)
	}

	// check that it's gone
	_, err = fs.Get(datastore.NewKey("quux"))
	if g, e := err, datastore.ErrNotFound; g != e {
		t.Fatalf("expected Get after Delete to give ErrNotFound, got: %v\n", g)
	}
}
330

331
func TestDeleteFound(t *testing.T) { tryAllShardFuncs(t, testDeleteFound) }
332 333

func testQuerySimple(dirFunc mkShardFunc, t *testing.T) {
334 335 336
	temp, cleanup := tempdir(t)
	defer cleanup()

337 338 339 340 341
	err := ioutil.WriteFile(filepath.Join(temp, "README"), []byte("something"), 0666)
	if err != nil {
		t.Fatalf("WriteFile fail: %v\n", err)
	}

342
	fs, err := flatfs.New(temp, dirFunc(2), false)
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
	if err != nil {
		t.Fatalf("New fail: %v\n", err)
	}
	const myKey = "quux"
	err = fs.Put(datastore.NewKey(myKey), []byte("foobar"))
	if err != nil {
		t.Fatalf("Put fail: %v\n", err)
	}

	res, err := fs.Query(query.Query{KeysOnly: true})
	if err != nil {
		t.Fatalf("Query fail: %v\n", err)
	}
	entries, err := res.Rest()
	if err != nil {
		t.Fatalf("Query Results.Rest fail: %v\n", err)
	}
	seen := false
	for _, e := range entries {
		switch e.Key {
		case datastore.NewKey(myKey).String():
			seen = true
		default:
			t.Errorf("saw unexpected key: %q", e.Key)
		}
	}
	if !seen {
		t.Errorf("did not see wanted key %q in %+v", myKey, entries)
	}
}
Jeromy's avatar
Jeromy committed
373

374
func TestQuerySimple(t *testing.T) { tryAllShardFuncs(t, testQuerySimple) }
375 376

func testBatchPut(dirFunc mkShardFunc, t *testing.T) {
Jeromy's avatar
Jeromy committed
377 378 379
	temp, cleanup := tempdir(t)
	defer cleanup()

380
	fs, err := flatfs.New(temp, dirFunc(2), false)
Jeromy's avatar
Jeromy committed
381 382 383 384
	if err != nil {
		t.Fatalf("New fail: %v\n", err)
	}

Jeromy's avatar
Jeromy committed
385 386
	dstest.RunBatchTest(t, fs)
}
Jeromy's avatar
Jeromy committed
387

388
func TestBatchPut(t *testing.T) { tryAllShardFuncs(t, testBatchPut) }
389 390

func testBatchDelete(dirFunc mkShardFunc, t *testing.T) {
Jeromy's avatar
Jeromy committed
391 392
	temp, cleanup := tempdir(t)
	defer cleanup()
Jeromy's avatar
Jeromy committed
393

394
	fs, err := flatfs.New(temp, dirFunc(2), false)
Jeromy's avatar
Jeromy committed
395
	if err != nil {
Jeromy's avatar
Jeromy committed
396
		t.Fatalf("New fail: %v\n", err)
Jeromy's avatar
Jeromy committed
397 398
	}

Jeromy's avatar
Jeromy committed
399
	dstest.RunBatchDeleteTest(t, fs)
Jeromy's avatar
Jeromy committed
400 401
}

402
func TestBatchDelete(t *testing.T) { tryAllShardFuncs(t, testBatchDelete) }
403

404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
func TestNoCluster(t *testing.T) {
	tempdir, cleanup := tempdir(t)
	defer cleanup()

	fs, err := flatfs.New(tempdir, flatfs.NextToLast(1), false)
	if err != nil {
		t.Fatalf("New fail: %v\n", err)
	}

	r := rand.New()
	N := 1024 // should be divisible by 32 so the math works out
	for i := 0; i < N; i++ {
		blk := make([]byte, 1000)
		r.Read(blk)

		key := "CIQ" + base32.StdEncoding.EncodeToString(blk[:10])
		err := fs.Put(datastore.NewKey(key), blk)
		if err != nil {
			t.Fatalf("Put fail: %v\n", err)
		}
	}

	dirs, err := ioutil.ReadDir(tempdir)
	if err != nil {
		t.Fatalf("ReadDir fail: %v\n", err)
	}
	if len(dirs) != 32 {
		t.Fatalf("Expected 32 directories in %s", tempdir)
	}
	idealFilesPerDir := float64(N) / 32.0
	tolerance := math.Floor(idealFilesPerDir * 0.50)
	for _, dir := range dirs {
		files, err := ioutil.ReadDir(filepath.Join(tempdir, dir.Name()))
		if err != nil {
			t.Fatalf("ReadDir fail: %v\n", err)
		}
		num := float64(len(files))
		if math.Abs(num-idealFilesPerDir) > tolerance {
			t.Fatalf("Dir %s has %.0f files, expected between %.f and %.f files",
				filepath.Join(tempdir, dir.Name()), num, idealFilesPerDir-tolerance, idealFilesPerDir+tolerance)
		}
	}
}

Jeromy's avatar
Jeromy committed
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
func BenchmarkConsecutivePut(b *testing.B) {
	r := rand.New()
	var blocks [][]byte
	var keys []datastore.Key
	for i := 0; i < b.N; i++ {
		blk := make([]byte, 256*1024)
		r.Read(blk)
		blocks = append(blocks, blk)

		key := base32.StdEncoding.EncodeToString(blk[:8])
		keys = append(keys, datastore.NewKey(key))
	}
	temp, cleanup := tempdir(b)
	defer cleanup()

463
	fs, err := flatfs.New(temp, flatfs.Prefix(2), false)
Jeromy's avatar
Jeromy committed
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 {
		b.Fatalf("New fail: %v\n", err)
	}

	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		err := fs.Put(keys[i], blocks[i])
		if err != nil {
			b.Fatal(err)
		}
	}
}

func BenchmarkBatchedPut(b *testing.B) {
	r := rand.New()
	var blocks [][]byte
	var keys []datastore.Key
	for i := 0; i < b.N; i++ {
		blk := make([]byte, 256*1024)
		r.Read(blk)
		blocks = append(blocks, blk)

		key := base32.StdEncoding.EncodeToString(blk[:8])
		keys = append(keys, datastore.NewKey(key))
	}
	temp, cleanup := tempdir(b)
	defer cleanup()

493
	fs, err := flatfs.New(temp, flatfs.Prefix(2), false)
Jeromy's avatar
Jeromy committed
494 495 496 497 498 499 500
	if err != nil {
		b.Fatalf("New fail: %v\n", err)
	}

	b.ResetTimer()

	for i := 0; i < b.N; {
Jeromy's avatar
Jeromy committed
501 502 503 504
		batch, err := fs.Batch()
		if err != nil {
			b.Fatal(err)
		}
Jeromy's avatar
Jeromy committed
505 506 507 508 509 510 511 512 513 514 515 516 517

		for n := i; i-n < 512 && i < b.N; i++ {
			err := batch.Put(keys[i], blocks[i])
			if err != nil {
				b.Fatal(err)
			}
		}
		err = batch.Commit()
		if err != nil {
			b.Fatal(err)
		}
	}
}