Beispiel #1
0
func Upload(t btesting.T, bucketId typing.BucketId,
	reader io.Reader) (hash []byte) {

	demoId := []byte("demoId")

	for {
		buf := make([]byte, chunkSize)
		numRead, err := reader.Read(buf)
		if err != nil && err != io.EOF {
			t.Errorf("Error reading: %v", err)
			return
		}
		if numRead > 0 {
			buf = buf[:numRead]
			//##PUT: keys = incubation/[ID]/append, values = [data]
			operations.Put(t, bucketId, typing.Key{[]byte("incubation"), demoId, []byte("append")},
				typing.Value{buf})
			if t.Failed() {
				return
			}
		}
		if numRead != chunkSize {
			// End
			break
		}
	}

	// Get the hash
	// ##GET: keys = incubation/[ID]/sum, values = [CBOR(hash), CBOR(length)]
	value := operations.Get(t, bucketId, typing.Key{[]byte("incubation"),
		demoId, []byte("sum")}, true)
	if t.Failed() {
		return
	}
	if len(value) != 2 {
		t.Errorf("Expecting 2 value entries (cbor(hash) and cbor(length))")
		return
	}
	err := encoding.Cbor().Decode(value[0], &hash)
	if err != nil {
		t.Errorf("Error getting the hash: %v", err)
		return
	}
	var length uint64
	err = encoding.Cbor().Decode(value[1], &length)
	if err != nil {
		t.Errorf("Error getting the length: %v", err)
		return
	}

	// Ok, commit
	//##PUT: keys = incubation/[ID]/finish, values = []

	operations.Put(t, bucketId, typing.Key{[]byte("incubation"), demoId, []byte("finish")},
		typing.Value{})
	if t.Failed() {
		return
	}

	return
}
Beispiel #2
0
func uploadDirectoryAndGet(t btesting.T) {
	// First create the blob bucket (that's where the directories are stored, and the data too)
	var blobBucketId typing.BucketId
	operations.CreateBucket(t, typing.TypeId_BlobStore, &blobBucketId)
	if t.Failed() {
		return
	}

	// Now create the directory bucket and connect that to the blob bucket
	var bucketId typing.BucketId
	var blobBucketIdCBor []byte
	blobBucketIdCBor, err := encoding.Cbor().Encode([]byte(blobBucketId))
	if err != nil {
		t.Errorf("error cbor encoding: %v", err)
		return
	}
	operations.CreateBucketWithMetadata(t, typing.TypeId_Directory, map[string][]byte{
		"const.forwarder.blob": blobBucketIdCBor,
	}, &bucketId)
	if t.Failed() {
		return
	}

	// Both buckets now exist and are connected
	dir, err := createDemoDirectory()
	if err != nil {
		t.Errorf("Error creating directory: %v\n", err)
	}

	// Now upload the directory to the blob storage
	dirReader := bytes.NewReader(dir)
	hash := blob.Upload(t, blobBucketId, dirReader)
	if t.Failed() {
		return
	}

	// The directory is now in the blob storage - the directory bucket can now index it
	cborHash, err := encoding.Cbor().Encode(hash)
	operations.Put(t, bucketId, typing.Key{[]byte("index")}, typing.Value{cborHash})
	if t.Failed() {
		return
	}

	// Get some files
	operations.Get(t, bucketId, typing.Key{hash, []byte("file_1.txt")}, true)
	if t.Failed() {
		return
	}
	operations.Get(t, bucketId, typing.Key{hash, []byte("another_file.jpeg")}, true)
	if t.Failed() {
		return
	}

	// Now some files that do not exist
	operations.Get(t, bucketId, typing.Key{hash, []byte("<UNKNOWN_FILE>.exe")}, false)
	if t.Failed() {
		return
	}
	operations.Get(t, bucketId, typing.Key{hash, []byte("no_is_not_in_directory.com")}, false)
	if t.Failed() {
		return
	}
}