示例#1
0
// marshalData returns the tree of sha1s and the json encoded Node as bytes.
func marshalData(t testing.TB, tree map[string]string) (map[string]string, []byte) {
	sha1tree := map[string]string{}
	entries := &dumbcaslib.Entry{}
	for k, v := range tree {
		h := dumbcaslib.Sha1Bytes([]byte(v))
		sha1tree[k] = h
		e := entries
		parts := strings.Split(k, "/")
		for i := 0; i < len(parts)-1; i++ {
			if e.Files == nil {
				e.Files = map[string]*dumbcaslib.Entry{}
			}
			if e.Files[parts[i]] == nil {
				e.Files[parts[i]] = &dumbcaslib.Entry{}
			}
			e = e.Files[parts[i]]
		}
		if e.Files == nil {
			e.Files = map[string]*dumbcaslib.Entry{}
		}
		e.Files[parts[len(parts)-1]] = &dumbcaslib.Entry{
			Sha1: h,
			Size: int64(len(v)),
		}
	}

	// Then process entries itself.
	data, err := json.Marshal(entries)
	ut.AssertEqual(t, nil, err)
	return sha1tree, data
}
示例#2
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))
}