Example #1
0
func TestGcKept(t *testing.T) {
	t.Parallel()
	f := makeDumbcasAppMock(t)
	args := []string{"gc", "-root=\\test_gc_kept"}
	f.Run(args, 0) // Instantiate f.cas and f.nodes

	// Create a tree of stuff.
	archiveData(f.TB, f.cas, f.nodes, map[string]string{
		"file1":           "content1",
		"dir1/dir2/file2": "content2",
	})

	i1, err := dumbcaslib.EnumerateCasAsList(f.cas)
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, 3, len(i1))
	n1, err := dumbcaslib.EnumerateNodesAsList(f.nodes)
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, 2, len(n1))

	f.Run(args, 0)

	// Nothing disapeared.
	i2, err := dumbcaslib.EnumerateCasAsList(f.cas)
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, i1, i2)
	n2, err := dumbcaslib.EnumerateNodesAsList(f.nodes)
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, n1, n2)
}
Example #2
0
func TestGcTrim(t *testing.T) {
	t.Parallel()
	f := makeDumbcasAppMock(t)
	args := []string{"gc", "-root=\\test_gc_trim"}
	f.Run(args, 0) // Instantiate f.cas and f.nodes

	// Create a tree of stuff.
	archiveData(f.TB, f.cas, f.nodes, map[string]string{
		"file1":           "content1",
		"dir1/dir2/file2": "content2",
	})
	i1, err := dumbcaslib.EnumerateCasAsList(f.cas)
	ut.AssertEqual(t, nil, err)
	n1, err := dumbcaslib.EnumerateNodesAsList(f.nodes)
	ut.AssertEqual(t, nil, err)

	// Add anothera tree of stuff.
	archiveData(f.TB, f.cas, f.nodes, map[string]string{
		"file3":           "content3",
		"dir1/dir4/file5": "content4",
		"dir6/file7":      "content5",
		"file1a":          "content1",
	})

	i2, err := dumbcaslib.EnumerateCasAsList(f.cas)
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, 7, len(i2))
	n2, err := dumbcaslib.EnumerateNodesAsList(f.nodes)
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, 3, len(n2))

	// Remove the first node and gc.
	err = f.nodes.Remove(n1[0])
	ut.AssertEqual(t, nil, err)
	f.Run(args, 0)
	i3, err := dumbcaslib.EnumerateCasAsList(f.cas)
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, 5, len(i3))
	n3, err := dumbcaslib.EnumerateNodesAsList(f.nodes)
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, 2, len(n3))

	// Check both: "n3 == n2 - n1[0]" and "i3 == i2 - i1 + sha1(content1)"
	rest := Sub(n2, []string{n1[0]})
	ut.AssertEqual(t, n3, rest)
	rest = Sub(i2, i1)
	rest = append(rest, sha1String("content1"))
	sort.Strings(rest)
	ut.AssertEqual(t, i3, rest)
}
Example #3
0
func TestGcEmpty(t *testing.T) {
	t.Parallel()
	f := makeDumbcasAppMock(t)
	args := []string{"gc", "-root=\\test_gc_empty"}
	f.Run(args, 0)
	i, err := dumbcaslib.EnumerateCasAsList(f.cas)
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, []string{}, i)
}
Example #4
0
func TestFsckEmpty(t *testing.T) {
	t.Parallel()
	f := makeDumbcasAppMock(t)
	args := []string{"fsck", "-root=\\test_fsck_empty"}
	f.Run(args, 0)
	items, err := dumbcaslib.EnumerateCasAsList(f.cas)
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, []string{}, items)
	nodes, err := dumbcaslib.EnumerateNodesAsList(f.nodes)
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, []string{}, nodes)
}
Example #5
0
func TestFsckCorruptCasFile(t *testing.T) {
	t.Parallel()
	f := makeDumbcasAppMock(t)
	args := []string{"fsck", "-root=\\test_fsck_cas"}
	f.Run(args, 0)

	archiveData(f.TB, f.cas, f.nodes, map[string]string{
		"file1":           "content1",
		"dir1/dir2/file2": "content2",
	})

	i1, err := dumbcaslib.EnumerateCasAsList(f.cas)
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, 3, len(i1))

	// Corrupt an item in CasTable.
	f.cas.(dumbcaslib.Corruptable).Corrupt()

	i1, err = dumbcaslib.EnumerateCasAsList(f.cas)
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, 4, len(i1))

	f.Run(args, 0)

	// One entry disapeared. I hope you had a valid secondary copy of your
	// CasTable.
	i1, err = dumbcaslib.EnumerateCasAsList(f.cas)
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, 3, len(i1))

	// Note: The node is not quarantined, because in theory the data could be
	// found on another copy of the CasTable so it's preferable to not delete the
	// node.
	n1, err := dumbcaslib.EnumerateNodesAsList(f.nodes)
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, 2, len(n1))
}
Example #6
0
func TestArchive(t *testing.T) {
	t.Parallel()
	f := makeDumbcasAppMock(t)
	tempData := makeTempDir(t, "archive")
	defer removeDir(t, tempData)

	// Create a tree of stuff.
	tree := map[string]string{
		"toArchive":          "x\ndir1\n",
		"x":                  "x\n",
		"dir1/bar":           "bar\n",
		"dir1/dir2/dir3/foo": "foo\n",
	}
	archived := map[string]string{
		"toArchive":     "x\ndir1\n",
		"x":             "x\n",
		"bar":           "bar\n",
		"dir2/dir3/foo": "foo\n",
	}
	if err := createTree(tempData, tree); err != nil {
		f.Fatal(err)
	}

	args := []string{"archive", "-root=\\test_archive", filepath.Join(tempData, "toArchive")}
	f.Run(args, 0)
	f.CheckBuffer(true, false)
	items, err := dumbcaslib.EnumerateCasAsList(f.cas)
	ut.AssertEqual(t, nil, err)

	expected := make([]string, 0, len(items))
	sha1tree, entries := marshalData(f.TB, archived)
	for _, v := range sha1tree {
		expected = append(expected, v)
	}
	expected = append(expected, dumbcaslib.Sha1Bytes(entries))
	sort.Strings(expected)
	ut.AssertEqual(t, items, expected)

	nodes, err := dumbcaslib.EnumerateNodesAsList(f.nodes)
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, 2, len(nodes))
}
Example #7
0
func TestFsckCorruptNodeEntry(t *testing.T) {
	t.Parallel()
	f := makeDumbcasAppMock(t)
	args := []string{"fsck", "-root=\\test_fsck_corrupt"}
	f.Run(args, 0)

	// Create a tree of stuff.
	archiveData(f.TB, f.cas, f.nodes, map[string]string{
		"file1":           "content1",
		"dir1/dir2/file2": "content2",
	})

	// Corrupt an item in NodesTable.
	f.nodes.(dumbcaslib.Corruptable).Corrupt()
	f.Run(args, 0)

	i1, err := dumbcaslib.EnumerateCasAsList(f.cas)
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, 3, len(i1))
	n1, err := dumbcaslib.EnumerateNodesAsList(f.nodes)
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, 1, len(n1))
}