Exemple #1
0
func (suite *GraphTestSuite) TestSize(t *C) {
	node, err := suite.ng.NewNode("child", graph.RootNodeId)
	t.Check(err, IsNil)

	t.Check(node.WriteData(testutils.RandDat(graph.MEGABYTE), 0), IsNil)
	t.Check(node.WriteData(testutils.RandDat(1024), graph.MEGABYTE), IsNil)

	t.Check(node.Size(), Equals, int64(graph.MEGABYTE+1024))
}
Exemple #2
0
func (suite *GraphTestSuite) TestWriteData_SizeChanges(t *C) {
	child, _ := makeNode("child", suite.ng.RootNode.Id, time.Now(), suite.ng)
	dat := testutils.RandDat(graph.BLOCK_SIZE)
	t.Check(child.WriteData(dat, 0), IsNil)

	t.Check(child.Size(), Equals, int64(graph.MEGABYTE))

	dat = testutils.RandDat(graph.BLOCK_SIZE)
	t.Check(child.WriteData(dat, graph.BLOCK_SIZE), IsNil)
	t.Check(child.Size(), Equals, int64(graph.MEGABYTE*2))
}
Exemple #3
0
func (suite *GraphTestSuite) TestBlockWithOffset_findsCorrectBlock(t *C) {
	child, _ := makeNode("child", suite.ng.RootNode.Id, time.Now(), suite.ng)
	data := testutils.RandDat(graph.MEGABYTE)
	t.Check(child.WriteData(data, 0), IsNil)

	data2 := testutils.RandDat(graph.MEGABYTE)
	t.Check(child.WriteData(data2, graph.MEGABYTE), IsNil)

	foundBlock := child.BlockWithOffset(0)
	t.Check(string(foundBlock), Equals, graph.Hash(data))

	foundBlock2 := child.BlockWithOffset(graph.MEGABYTE)
	t.Check(string(foundBlock2), Equals, graph.Hash(data2))
}
Exemple #4
0
func (suite *GraphTestSuite) TestWriteData_removesExistingFingerprintForOffset(t *C) {
	child, _ := makeNode("child", suite.ng.RootNode.Id, time.Now(), suite.ng)
	dat := testutils.RandDat(1024)

	t.Check(child.WriteData(dat, 0), IsNil)

	dat = testutils.RandDat(1024)
	fingerprint := graph.Hash(dat)
	t.Check(child.WriteData(dat, 0), IsNil)

	it := cayley.StartPath(suite.ng, child.Id).Out("offset-0").BuildIterator()
	t.Check(cayley.RawNext(it), Equals, true)
	t.Check(suite.ng.NameOf(it.Result()), Equals, fingerprint)
}
Exemple #5
0
func (suite *GraphTestSuite) TestChmod_throwsIfNewModeIsDirAndHasSize(t *C) {
	child, err := makeNode("child", graph.RootNodeId, time.Now(), suite.ng)
	t.Check(err, IsNil)
	t.Check(child.WriteData(testutils.RandDat(graph.MEGABYTE), 0), IsNil)

	t.Check(child.Chmod(os.ModeDir), ErrorMatches, "File has size, cannot change to directory")
}
Exemple #6
0
func (suite *GraphTestSuite) TestBlocks_returnsCorrectBlocks(t *C) {
	child, err := makeNode("child", suite.ng.RootNode.Id, time.Now(), suite.ng)
	t.Check(err, IsNil)

	block1 := testutils.RandDat(graph.MEGABYTE)
	block2 := testutils.RandDat(graph.MEGABYTE)

	t.Check(child.WriteData(block1, 0), IsNil)
	t.Check(child.WriteData(block2, graph.MEGABYTE), IsNil)

	blocks := child.Blocks()
	t.Assert(blocks, HasLen, 2)

	t.Check(blocks[0].Offset, Equals, int64(0))
	t.Check(blocks[1].Offset, Equals, int64(graph.MEGABYTE))

	t.Check(blocks[0].Hash, Equals, graph.Hash(block1))
	t.Check(blocks[1].Hash, Equals, graph.Hash(block2))
}
Exemple #7
0
func (suite *GraphTestSuite) TestWriteData_writesDataToCorrectBlock(t *C) {
	child, _ := makeNode("child", suite.ng.RootNode.Id, time.Now(), suite.ng)
	dat := testutils.RandDat(1024)
	fingerprint := graph.Hash(dat)

	t.Check(child.WriteData(dat, 0), IsNil)

	blocks := child.Blocks()
	t.Check(blocks, HasLen, 1)
	if len(blocks) > 0 {
		t.Check(blocks[0].Hash, Matches, fingerprint)
	}
}
Exemple #8
0
func (suite *GraphTestSuite) BenchmarkSize(t *C) {
	node, err := suite.ng.NewNode("child", graph.RootNodeId)
	t.Check(err, IsNil)

	for i := 0; i < 10; i++ {
		err := node.WriteData(testutils.RandDat(graph.MEGABYTE), int64(i*graph.MEGABYTE))
		t.Check(err, IsNil)
	}

	t.ResetTimer()
	for i := 0; i < t.N; i++ {
		node.Size()
	}
}
Exemple #9
0
func (suite *ApiClientTestSuite) TestApiClient_ListBlocks_listsBlocks(t *C) {
	node, err := suite.ng.NewNodeWithNodeInfo(graph.NodeInfo{
		ParentId: graph.RootNodeId,
		Name:     "thing.txt",
		Mode:     0755,
	})
	t.Check(err, IsNil)

	dat1 := testutils.RandDat(graph.MEGABYTE)
	hash1 := graph.Hash(dat1)
	dat2 := testutils.RandDat(graph.MEGABYTE)
	hash2 := graph.Hash(dat2)

	t.Check(node.WriteData(dat1, 0), IsNil)
	t.Check(node.WriteData(dat2, graph.MEGABYTE), IsNil)

	blocks, err := suite.client.ListBlocks(node.Id)
	t.Check(err, IsNil)
	t.Check(blocks, HasLen, 2)
	t.Check(blocks[0].Hash, Equals, hash1)
	t.Check(blocks[0].Offset, Equals, int64(0))
	t.Check(blocks[1].Hash, Equals, hash2)
	t.Check(blocks[1].Offset, Equals, int64(graph.MEGABYTE))
}
Exemple #10
0
func (suite *ApiClientTestSuite) TestApiClient_WriteBlock_writesBlock(t *C) {
	node, err := suite.ng.NewNodeWithNodeInfo(graph.NodeInfo{
		ParentId: graph.RootNodeId,
		Name:     "thing.txt",
		Mode:     0755,
	})
	t.Check(err, IsNil)

	dat := testutils.RandDat(graph.MEGABYTE)
	buf := bytes.NewBuffer(dat)

	err = suite.client.WriteBlock(node.Id, 0, graph.Hash(dat), buf)
	t.Check(err, IsNil)

	blocks := node.Blocks()
	t.Assert(blocks, HasLen, 1)
	t.Check(blocks[0].Hash, Equals, graph.Hash(dat))
	t.Check(blocks[0].Offset, Equals, int64(0))
}
Exemple #11
0
func (suite *GraphTestSuite) TestNodeSeeker_readsCorrectData(t *C) {
	child, _ := makeNode("child", suite.ng.RootNode.Id, time.Now(), suite.ng)
	dat := testutils.RandDat(1024)

	t.Check(child.WriteData(dat, 0), IsNil)

	nodeSeeker := child.ReadSeeker()
	offset, err := nodeSeeker.Seek(0, 0)
	t.Check(err, IsNil)
	t.Check(offset, Equals, int64(0))

	p := make([]byte, 1)
	nodeSeeker.Read(p) // expect 1 byte to be read from front of file

	t.Check(p[0], Equals, dat[0])

	offset, err = nodeSeeker.Seek(512, 0)
	t.Check(err, IsNil)
	t.Check(offset, Equals, int64(512))

	p = make([]byte, 25)
	nodeSeeker.Read(p)
	t.Check(p, DeepEquals, dat[offset:int(offset)+len(p)])
}
Exemple #12
0
func fileData(size int) (string, io.Reader) {
	dat := testutils.RandDat(size)
	return graph.Hash(dat), bytes.NewBuffer(dat)
}
Exemple #13
0
func (suite *GraphTestSuite) TestWriteData_throwsOnInvalidBlockOffset(t *C) {
	child, _ := makeNode("child", suite.ng.RootNode.Id, time.Now(), suite.ng)
	dat := testutils.RandDat(1024)

	t.Check(child.WriteData(dat, 1), ErrorMatches, fmt.Sprint("1 is not a valid offset for block size ", graph.BLOCK_SIZE))
}