flatfs_test.go 8.63 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 6 7
	"io/ioutil"
	"os"
	"path/filepath"
8
	"runtime"
Tommi Virtanen's avatar
Tommi Virtanen committed
9 10 11 12
	"testing"

	"github.com/jbenet/go-datastore"
	"github.com/jbenet/go-datastore/flatfs"
13
	"github.com/jbenet/go-datastore/query"
Jeromy's avatar
Jeromy committed
14
	dstest "github.com/jbenet/go-datastore/test"
Jeromy's avatar
Jeromy committed
15

Jeromy's avatar
Jeromy committed
16
	rand "github.com/jbenet/go-datastore/Godeps/_workspace/src/github.com/dustin/randbo"
Tommi Virtanen's avatar
Tommi Virtanen committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
)

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
}

func TestBadPrefixLen(t *testing.T) {
	temp, cleanup := tempdir(t)
	defer cleanup()

	for i := 0; i > -3; i-- {
Jeromy's avatar
Jeromy committed
38
		_, err := flatfs.New(temp, i, false)
Tommi Virtanen's avatar
Tommi Virtanen committed
39 40 41 42 43 44 45 46 47 48
		if g, e := err, flatfs.ErrBadPrefixLen; g != e {
			t.Errorf("expected ErrBadPrefixLen, got: %v", g)
		}
	}
}

func TestPutBadValueType(t *testing.T) {
	temp, cleanup := tempdir(t)
	defer cleanup()

Jeromy's avatar
Jeromy committed
49
	fs, err := flatfs.New(temp, 2, false)
Tommi Virtanen's avatar
Tommi Virtanen committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63
	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)
	}
}

func TestPut(t *testing.T) {
	temp, cleanup := tempdir(t)
	defer cleanup()

Jeromy's avatar
Jeromy committed
64
	fs, err := flatfs.New(temp, 2, false)
Tommi Virtanen's avatar
Tommi Virtanen committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78
	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)
	}
}

func TestGet(t *testing.T) {
	temp, cleanup := tempdir(t)
	defer cleanup()

Jeromy's avatar
Jeromy committed
79
	fs, err := flatfs.New(temp, 2, false)
Tommi Virtanen's avatar
Tommi Virtanen committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
	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)
	}
}

func TestPutOverwrite(t *testing.T) {
	temp, cleanup := tempdir(t)
	defer cleanup()

Jeromy's avatar
Jeromy committed
107
	fs, err := flatfs.New(temp, 2, false)
Tommi Virtanen's avatar
Tommi Virtanen committed
108 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 136 137 138
	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)
	}
}

func TestGetNotFoundError(t *testing.T) {
	temp, cleanup := tempdir(t)
	defer cleanup()

Jeromy's avatar
Jeromy committed
139
	fs, err := flatfs.New(temp, 2, false)
Tommi Virtanen's avatar
Tommi Virtanen committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
	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)
	}
}

func TestStorage(t *testing.T) {
	temp, cleanup := tempdir(t)
	defer cleanup()

	const prefixLen = 2
155
	const prefix = "7175"
156
	const target = prefix + string(os.PathSeparator) + "71757578.data"
Jeromy's avatar
Jeromy committed
157
	fs, err := flatfs.New(temp, prefixLen, false)
Tommi Virtanen's avatar
Tommi Virtanen committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
	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)
	}

	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
		case prefix:
			if !fi.IsDir() {
				t.Errorf("prefix directory is not a file? %v", fi.Mode())
			}
			// 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())
			}
190 191 192 193
			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
194 195 196 197 198 199 200 201 202 203 204 205 206
			}
		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
207 208 209 210 211

func TestHasNotFound(t *testing.T) {
	temp, cleanup := tempdir(t)
	defer cleanup()

Jeromy's avatar
Jeromy committed
212
	fs, err := flatfs.New(temp, 2, false)
Tommi Virtanen's avatar
Tommi Virtanen committed
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
	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)
	}
}

func TestHasFound(t *testing.T) {
	temp, cleanup := tempdir(t)
	defer cleanup()

Jeromy's avatar
Jeromy committed
230
	fs, err := flatfs.New(temp, 2, false)
Tommi Virtanen's avatar
Tommi Virtanen committed
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
	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
247 248 249 250 251

func TestDeleteNotFound(t *testing.T) {
	temp, cleanup := tempdir(t)
	defer cleanup()

Jeromy's avatar
Jeromy committed
252
	fs, err := flatfs.New(temp, 2, false)
Tommi Virtanen's avatar
Tommi Virtanen committed
253 254 255 256 257 258 259 260 261 262 263 264 265 266
	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)
	}
}

func TestDeleteFound(t *testing.T) {
	temp, cleanup := tempdir(t)
	defer cleanup()

Jeromy's avatar
Jeromy committed
267
	fs, err := flatfs.New(temp, 2, false)
Tommi Virtanen's avatar
Tommi Virtanen committed
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
	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)
	}
}
287 288 289 290 291

func TestQuerySimple(t *testing.T) {
	temp, cleanup := tempdir(t)
	defer cleanup()

Jeromy's avatar
Jeromy committed
292
	fs, err := flatfs.New(temp, 2, false)
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
	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
323

Jeromy's avatar
Jeromy committed
324 325 326 327
func TestBatchPut(t *testing.T) {
	temp, cleanup := tempdir(t)
	defer cleanup()

Jeromy's avatar
Jeromy committed
328
	fs, err := flatfs.New(temp, 2, false)
Jeromy's avatar
Jeromy committed
329 330 331 332
	if err != nil {
		t.Fatalf("New fail: %v\n", err)
	}

Jeromy's avatar
Jeromy committed
333 334
	dstest.RunBatchTest(t, fs)
}
Jeromy's avatar
Jeromy committed
335

Jeromy's avatar
Jeromy committed
336 337 338
func TestBatchDelete(t *testing.T) {
	temp, cleanup := tempdir(t)
	defer cleanup()
Jeromy's avatar
Jeromy committed
339

Jeromy's avatar
Jeromy committed
340
	fs, err := flatfs.New(temp, 2, false)
Jeromy's avatar
Jeromy committed
341
	if err != nil {
Jeromy's avatar
Jeromy committed
342
		t.Fatalf("New fail: %v\n", err)
Jeromy's avatar
Jeromy committed
343 344
	}

Jeromy's avatar
Jeromy committed
345
	dstest.RunBatchDeleteTest(t, fs)
Jeromy's avatar
Jeromy committed
346 347
}

Jeromy's avatar
Jeromy committed
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
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()

Jeromy's avatar
Jeromy committed
363
	fs, err := flatfs.New(temp, 2, false)
Jeromy's avatar
Jeromy committed
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
	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()

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

	b.ResetTimer()

	for i := 0; i < b.N; {
Jeromy's avatar
Jeromy committed
401 402 403 404
		batch, err := fs.Batch()
		if err != nil {
			b.Fatal(err)
		}
Jeromy's avatar
Jeromy committed
405 406 407 408 409 410 411 412 413 414 415 416 417

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