Exemple #1
0
func TestFileWriteRead(t *testing.T) {
	snapshotio := inodedb.NewSimpleDBStateSnapshotIO()
	txio := inodedb.NewSimpleDBTransactionLogIO()
	idb, err := inodedb.NewEmptyDB(snapshotio, txio)
	if err != nil {
		t.Errorf("NewEmptyDB failed: %v", err)
		return
	}

	bs := tu.TestFileBlobStore()
	fs := otaru.NewFileSystem(idb, bs, tu.TestCipher())
	h, err := fs.OpenFileFullPath("/hello.txt", flags.O_CREATE|flags.O_RDWR, 0666)
	if err != nil {
		t.Errorf("OpenFileFullPath failed: %v", err)
		return
	}

	err = h.PWrite(tu.HelloWorld, 0)
	if err != nil {
		t.Errorf("PWrite failed: %v", err)
	}

	buf := make([]byte, 32)
	n, err := h.ReadAt(buf, 0)
	if err != nil {
		t.Errorf("PRead failed: %v", err)
	}
	buf = buf[:n]
	if n != len(tu.HelloWorld) {
		t.Errorf("n: %d", n)
	}
	if !bytes.Equal(tu.HelloWorld, buf) {
		t.Errorf("PRead content != PWrite content: %v", buf)
	}
}
func TestCachedBackendVersion_UseCached(t *testing.T) {
	bs := tu.TestFileBlobStore()
	cbv := cachedblobstore.NewCachedBackendVersion(bs, tu.TestQueryVersion)

	cbv.Set("foobar", 123)
	v, err := cbv.Query("foobar")
	if err != nil {
		t.Errorf("Unexpected Query() err: %v", err)
		return
	}
	if v != 123 {
		t.Errorf("Unexpected Query() result. v: %d", v)
		return
	}
}
func TestCachedBackendVersion_FillCache(t *testing.T) {
	bs := tu.TestFileBlobStore()
	if err := tu.WriteVersionedBlob(bs, "uncached", 42); err != nil {
		t.Errorf("%v", err)
		return
	}

	cbv := cachedblobstore.NewCachedBackendVersion(bs, tu.TestQueryVersion)
	v, err := cbv.Query("uncached")
	if err != nil {
		t.Errorf("Unexpected Query() err: %v", err)
		return
	}
	if v != 42 {
		t.Errorf("Unexpected Query() result. v: %d", v)
		return
	}
}
func TestSS_SaveRestore(t *testing.T) {
	loc := datastore.NewINodeDBSSLocator(authtu.TestDSConfig(testRootKey()))
	if _, err := loc.DeleteAll(); err != nil {
		t.Errorf("Failed to loc.DeleteAll: %v", err)
	}

	bs := tu.TestFileBlobStore()
	sio := blobstoredbstatesnapshotio.New(bs, tu.TestCipher(), loc)

	db, err := inodedb.NewEmptyDB(sio, inodedb.NewSimpleDBTransactionLogIO())
	if err != nil {
		t.Errorf("Failed to NewEmptyDB: %v", err)
		return
	}
	if err := db.Sync(); err != nil {
		t.Errorf("Failed to Sync DB: %v", err)
		return
	}

	_, err = inodedb.NewDB(sio, inodedb.NewSimpleDBTransactionLogIO())
	if err != nil {
		t.Errorf("Failed to NewDB: %v", err)
	}
}
func TestCachedBackedVersion_SaveRestore(t *testing.T) {
	bs := tu.TestFileBlobStore()

	cbv := cachedblobstore.NewCachedBackendVersion(bs, tu.TestQueryVersion)
	cbv.Set("foobar", 123)
	if err := cbv.SaveStateToBlobstore(tu.TestCipher(), bs); err != nil {
		t.Errorf("Failed to save state: %v", err)
		return
	}

	cbv2 := cachedblobstore.NewCachedBackendVersion(bs, tu.TestQueryVersion)
	if err := cbv2.RestoreStateFromBlobstore(tu.TestCipher(), bs); err != nil {
		t.Errorf("Failed to restore  state: %v", err)
	}
	v, err := cbv2.Query("foobar")
	if err != nil {
		t.Errorf("Unexpected Query() err: %v", err)
		return
	}
	if v != 123 {
		t.Errorf("Unexpected Query() result. v: %d", v)
		return
	}
}
func TestSS_AutoAvoidCorruptedSnapshot(t *testing.T) {
	loc := datastore.NewINodeDBSSLocator(authtu.TestDSConfig(testRootKey()))
	if _, err := loc.DeleteAll(); err != nil {
		t.Errorf("Failed to loc.DeleteAll: %v", err)
	}

	bs := tu.TestFileBlobStore()
	sio := blobstoredbstatesnapshotio.New(bs, tu.TestCipher(), loc)
	txlogio := inodedb.NewSimpleDBTransactionLogIO()

	{
		db, err := inodedb.NewEmptyDB(sio, txlogio)
		if err != nil {
			t.Errorf("Failed to NewEmptyDB: %v", err)
			return
		}

		// create 1st snapshot
		if err := db.Sync(); err != nil {
			t.Errorf("Failed to Sync DB: %v", err)
			return
		}

		// apply some mod to inodedb
		nlock, err := db.LockNode(inodedb.AllocateNewNodeID)
		if err != nil {
			t.Errorf("Failed to LockNode: %v", err)
			return
		}

		tx := inodedb.DBTransaction{Ops: []inodedb.DBOperation{
			&inodedb.CreateNodeOp{NodeLock: nlock, OrigPath: "/hoge.txt", Type: inodedb.FileNodeT},
			&inodedb.HardLinkOp{NodeLock: inodedb.NodeLock{1, inodedb.NoTicket}, Name: "hoge.txt", TargetID: nlock.ID},
		}}
		if _, err := db.ApplyTransaction(tx); err != nil {
			t.Errorf("Failed to apply tx: %v", err)
			return
		}

		if err := db.UnlockNode(nlock); err != nil {
			t.Errorf("Failed to UnlockNode: %v", err)
			return
		}

		// create 2nd snapshot
		if err := db.Sync(); err != nil {
			t.Errorf("Failed to Sync DB (2): %v", err)
			return
		}
	}

	if _, err := inodedb.NewDB(sio, txlogio); err != nil {
		t.Errorf("Failed to NewDB (uncorrupted): %v", err)
		return
	}

	// destroy latest snapshot (corrupt data)
	ssbp, err := loc.Locate(0)
	if err != nil {
		t.Errorf("Failed to locate latest ssbp: %v", err)
		return
	}
	{
		wc, err := bs.OpenWriter(ssbp)
		if err != nil {
			t.Errorf("Failed to OpenWriter: %v", err)
			return
		}
		if _, err := wc.Write([]byte("hoge")); err != nil {
			t.Errorf("Failed to Write: %v", err)
		}
		wc.Close()
	}

	{
		_, err = inodedb.NewDB(sio, txlogio)
		if err != nil {
			t.Errorf("Failed to NewDB (corrupted): %v", err)
			return
		}
	}

	// destroy latest snapshot (remove ss blob)
	if err := bs.RemoveBlob(ssbp); err != nil {
		t.Errorf("Failed to RemoveBlob: %v", err)
	}

	{
		_, err = inodedb.NewDB(sio, txlogio)
		if err != nil {
			t.Errorf("Failed to NewDB (ss blob removed): %v", err)
			return
		}
	}
}