Example #1
0
func checkDirectoryInCell(t *testing.T, ts topo.Impl, cell string) {
	t.Logf("===   checkDirectoryInCell %v", cell)
	ctx := context.Background()

	// ListDir root: nothing
	checkListDir(ctx, t, ts, cell, "/", nil)

	// Create a topolevel entry
	version, err := ts.Create(ctx, cell, "/MyFile", []byte{'a'})
	if err != nil {
		t.Fatalf("cannot create toplevel file: %v", err)
	}

	// ListDir should return it.
	checkListDir(ctx, t, ts, cell, "/", []string{"MyFile"})

	// Delete it, it should be gone.
	if err := ts.Delete(ctx, cell, "/MyFile", version); err != nil {
		t.Fatalf("cannot delete toplevel file: %v", err)
	}
	checkListDir(ctx, t, ts, cell, "/", nil)

	// Create a file 3 layers down.
	version, err = ts.Create(ctx, cell, "/types/name/MyFile", []byte{'a'})
	if err != nil {
		t.Fatalf("cannot create deep file: %v", err)
	}

	// Check listing at all levels.
	checkListDir(ctx, t, ts, cell, "/", []string{"types"})
	checkListDir(ctx, t, ts, cell, "/types/", []string{"name"})
	checkListDir(ctx, t, ts, cell, "/types/name/", []string{"MyFile"})

	// Add a second file
	version2, err := ts.Create(ctx, cell, "/types/othername/MyFile", []byte{'a'})
	if err != nil {
		t.Fatalf("cannot create deep file2: %v", err)
	}

	// Check entries at all levels
	checkListDir(ctx, t, ts, cell, "/", []string{"types"})
	checkListDir(ctx, t, ts, cell, "/types/", []string{"name", "othername"})
	checkListDir(ctx, t, ts, cell, "/types/name/", []string{"MyFile"})
	checkListDir(ctx, t, ts, cell, "/types/othername/", []string{"MyFile"})

	// Delete the first file, expect all lists to return the second one.
	if err := ts.Delete(ctx, cell, "/types/name/MyFile", version); err != nil {
		t.Fatalf("cannot delete deep file: %v", err)
	}
	checkListDir(ctx, t, ts, cell, "/", []string{"types"})
	checkListDir(ctx, t, ts, cell, "/types/", []string{"othername"})
	checkListDir(ctx, t, ts, cell, "/types/name/", nil)
	checkListDir(ctx, t, ts, cell, "/types/othername/", []string{"MyFile"})

	// Delete the second file, expect all lists to return nothing.
	if err := ts.Delete(ctx, cell, "/types/othername/MyFile", version2); err != nil {
		t.Fatalf("cannot delete second deep file: %v", err)
	}
	for _, dir := range []string{"/", "/types/", "/types/name/", "/types/othername/"} {
		checkListDir(ctx, t, ts, cell, dir, nil)
	}
}
Example #2
0
File: file.go Project: erzel/vitess
func checkFileInCell(t *testing.T, ts topo.Impl, cell string) {
	t.Logf("===   checkFileInCell %v", cell)
	ctx := context.Background()

	// ListDir root: nothing.
	checkListDir(ctx, t, ts, cell, "/", nil)

	// Get with no file -> ErrNoNode.
	contents, version, err := ts.Get(ctx, cell, "/myfile")
	if err != topo.ErrNoNode {
		t.Errorf("Get(non-existent) didn't return ErrNoNode but: %v", err)
	}

	// Create a file.
	version, err = ts.Create(ctx, cell, "/myfile", []byte{'a'})
	if err != nil {
		t.Fatalf("Create('/myfile') failed: %v", err)
	}

	// See it in the listing now.
	checkListDir(ctx, t, ts, cell, "/", []string{"myfile"})

	// Get should work, get the right contents and version.
	contents, getVersion, err := ts.Get(ctx, cell, "/myfile")
	if err != nil {
		t.Errorf("Get('/myfile') returned an error: %v", err)
	} else {
		if len(contents) != 1 || contents[0] != 'a' {
			t.Errorf("Get('/myfile') returned bad content: %v", contents)
		}
		if !reflect.DeepEqual(getVersion, version) {
			t.Errorf("Get('/myfile') returned bad version: got %v expected %v", getVersion, version)
		}
	}

	// Update it, make sure version changes.
	newVersion, err := ts.Update(ctx, cell, "/myfile", []byte{'b'}, version)
	if err != nil {
		t.Fatalf("Update('/myfile') failed: %v", err)
	}
	if reflect.DeepEqual(version, newVersion) {
		t.Errorf("Version didn't change, stayed %v", newVersion)
	}

	// Get should work, get the right contents and version.
	contents, getVersion, err = ts.Get(ctx, cell, "/myfile")
	if err != nil {
		t.Errorf("Get('/myfile') returned an error: %v", err)
	} else {
		if len(contents) != 1 || contents[0] != 'b' {
			t.Errorf("Get('/myfile') returned bad content: %v", contents)
		}
		if !reflect.DeepEqual(getVersion, newVersion) {
			t.Errorf("Get('/myfile') returned bad version: got %v expected %v", getVersion, newVersion)
		}
	}

	// Try to update again with wrong version, should fail.
	if _, err = ts.Update(ctx, cell, "/myfile", []byte{'b'}, version); err != topo.ErrBadVersion {
		t.Errorf("Update(bad version) didn't return ErrBadVersion but: %v", err)
	}

	// Try to update again with nil version, should work.
	newVersion, err = ts.Update(ctx, cell, "/myfile", []byte{'c'}, nil)
	if err != nil {
		t.Errorf("Update(nil version) should have worked but got: %v", err)
	}

	// Get should work, get the right contents and version.
	contents, getVersion, err = ts.Get(ctx, cell, "/myfile")
	if err != nil {
		t.Errorf("Get('/myfile') returned an error: %v", err)
	} else {
		if len(contents) != 1 || contents[0] != 'c' {
			t.Errorf("Get('/myfile') returned bad content: %v", contents)
		}
		if !reflect.DeepEqual(getVersion, newVersion) {
			t.Errorf("Get('/myfile') returned bad version: got %v expected %v", getVersion, newVersion)
		}
	}

	// Try to delete with wrong version, should fail.
	if err = ts.Delete(ctx, cell, "/myfile", version); err != topo.ErrBadVersion {
		t.Errorf("Delete('/myfile', wrong version) returned bad error: %v", err)
	}

	// Now delete it.
	if err = ts.Delete(ctx, cell, "/myfile", newVersion); err != nil {
		t.Fatalf("Delete('/myfile') failed: %v", err)
	}

	// Try to delete again, should fail.
	if err = ts.Delete(ctx, cell, "/myfile", newVersion); err != topo.ErrNoNode {
		t.Errorf("Delete(already gone) returned bad error: %v", err)
	}

	// Create again, with unconditional update.
	version, err = ts.Update(ctx, cell, "/myfile", []byte{'d'}, nil)
	if err != nil {
		t.Fatalf("Update('/myfile', nil) failed: %v", err)
	}

	// Check contents.
	contents, getVersion, err = ts.Get(ctx, cell, "/myfile")
	if err != nil {
		t.Errorf("Get('/myfile') returned an error: %v", err)
	} else {
		if len(contents) != 1 || contents[0] != 'd' {
			t.Errorf("Get('/myfile') returned bad content: %v", contents)
		}
		if !reflect.DeepEqual(getVersion, version) {
			t.Errorf("Get('/myfile') returned bad version: got %v expected %v", getVersion, version)
		}
	}

	// Unconditional delete.
	if err = ts.Delete(ctx, cell, "/myfile", nil); err != nil {
		t.Errorf("Delete('/myfile', nil) failed: %v", err)
	}
}