[ARVADOS] updated: 1a825caf53f352f63cafe10ef6211f4f689177e0

git at public.curoverse.com git at public.curoverse.com
Tue Apr 1 17:51:23 EDT 2014


Summary of changes:
 services/keep/keep.go      |   29 +++++++++++++++--------------
 services/keep/keep_test.go |   42 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 56 insertions(+), 15 deletions(-)

       via  1a825caf53f352f63cafe10ef6211f4f689177e0 (commit)
      from  2c2d5fc62029b559669c860ab1d083eed44cf30e (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.


commit 1a825caf53f352f63cafe10ef6211f4f689177e0
Author: Tim Pierce <twp at curoverse.com>
Date:   Tue Apr 1 17:51:08 2014 -0400

    Added TestPutBlockOK. (refs #2292, refs #2449)
    
    Also:
    * Fixed some sloppy/confusing error handling.
    * Removed dependency on "path" in keep.go.

diff --git a/services/keep/keep.go b/services/keep/keep.go
index 2fa5c37..e0883f4 100644
--- a/services/keep/keep.go
+++ b/services/keep/keep.go
@@ -9,7 +9,6 @@ import (
 	"log"
 	"net/http"
 	"os"
-	"path"
 	"strings"
 )
 
@@ -116,7 +115,8 @@ func PutBlockHandler(w http.ResponseWriter, req *http.Request) {
 		if err := PutBlock(buf[:nread], hash); err == nil {
 			w.WriteHeader(http.StatusOK)
 		} else {
-			http.Error(w, err.Error(), err.HTTPCode)
+			ke := err.(*KeepError)
+			http.Error(w, ke.Error(), ke.HTTPCode)
 		}
 	} else {
 		log.Println("error reading request: ", err)
@@ -133,17 +133,17 @@ func GetBlock(hash string) ([]byte, error) {
 		var err error
 		var nread int
 
-		path := fmt.Sprintf("%s/%s/%s", vol, hash[0:3], hash)
+		blockFilename := fmt.Sprintf("%s/%s/%s", vol, hash[0:3], hash)
 
-		f, err = os.Open(path)
+		f, err = os.Open(blockFilename)
 		if err != nil {
-			log.Printf("%s: opening %s: %s\n", vol, path, err)
+			log.Printf("%s: opening %s: %s\n", vol, blockFilename, err)
 			continue
 		}
 
 		nread, err = f.Read(buf)
 		if err != nil {
-			log.Printf("%s: reading %s: %s\n", vol, path, err)
+			log.Printf("%s: reading %s: %s\n", vol, blockFilename, err)
 			continue
 		}
 
@@ -157,7 +157,7 @@ func GetBlock(hash string) ([]byte, error) {
 			// priority or logged as urgent problems.
 			//
 			log.Printf("%s: checksum mismatch: %s (actual hash %s)\n",
-				vol, path, filehash)
+				vol, blockFilename, filehash)
 			continue
 		}
 
@@ -193,7 +193,7 @@ func GetBlock(hash string) ([]byte, error) {
             provide as much detail as possible.
 */
 
-func PutBlock(block []byte, hash string) *KeepError {
+func PutBlock(block []byte, hash string) error {
 	// Check that BLOCK's checksum matches HASH.
 	blockhash := fmt.Sprintf("%x", md5.Sum(block))
 	if blockhash != hash {
@@ -205,14 +205,15 @@ func PutBlock(block []byte, hash string) *KeepError {
 	any_success := false
 	for _, vol := range KeepVolumes {
 
-		bFilename := fmt.Sprintf("%s/%s/%s", vol, hash[0:3], hash)
-		if err := os.MkdirAll(path.Dir(bFilename), 0755); err != nil {
+		blockDir := fmt.Sprintf("%s/%s", vol, hash[0:3])
+		if err := os.MkdirAll(blockDir, 0755); err != nil {
 			log.Printf("%s: could not create directory %s: %s",
-				hash, path.Dir(bFilename), err)
+				hash, blockDir, err)
 			continue
 		}
 
-		f, err := os.OpenFile(bFilename, os.O_CREATE|os.O_WRONLY, 0644)
+		blockFilename := fmt.Sprintf("%s/%s", blockDir, hash)
+		f, err := os.OpenFile(blockFilename, os.O_CREATE|os.O_WRONLY, 0644)
 		if err != nil {
 			// if the block already exists, just skip to the next volume.
 			if os.IsExist(err) {
@@ -220,7 +221,7 @@ func PutBlock(block []byte, hash string) *KeepError {
 				continue
 			} else {
 				// Open failed for some other reason.
-				log.Printf("%s: creating %s: %s\n", vol, bFilename, err)
+				log.Printf("%s: creating %s: %s\n", vol, blockFilename, err)
 				continue
 			}
 		}
@@ -230,7 +231,7 @@ func PutBlock(block []byte, hash string) *KeepError {
 			any_success = true
 			continue
 		} else {
-			log.Printf("%s: writing to %s: %s\n", vol, bFilename, err)
+			log.Printf("%s: writing to %s: %s\n", vol, blockFilename, err)
 			continue
 		}
 	}
diff --git a/services/keep/keep_test.go b/services/keep/keep_test.go
index 437e92c..4795dad 100644
--- a/services/keep/keep_test.go
+++ b/services/keep/keep_test.go
@@ -12,13 +12,16 @@ var TEST_BLOCK = []byte("The quick brown fox jumps over the lazy dog.")
 var TEST_HASH = "e4d909c290d0fb1ca068ffaddf22cbd0"
 var BAD_BLOCK = []byte("The magic words are squeamish ossifrage.")
 
+// ========================================
+// GetBlock tests.
+// ========================================
+
 // Test simple block reads.
 func TestGetBlockOK(t *testing.T) {
 	defer teardown()
 
 	// Create two test Keep volumes and store a block in each of them.
 	KeepVolumes = setup(t, 2)
-	fmt.Println("KeepVolumes = ", KeepVolumes)
 
 	for _, vol := range KeepVolumes {
 		store(t, vol, TEST_HASH, TEST_BLOCK)
@@ -84,6 +87,42 @@ func TestGetBlockCorrupt(t *testing.T) {
 	}
 }
 
+// ========================================
+// PutBlock tests
+// ========================================
+
+// Test simple block stores.
+
+func TestPutBlockOK(t *testing.T) {
+	defer teardown()
+
+	// Create two test Keep volumes.
+	KeepVolumes = setup(t, 2)
+
+	// Check that PutBlock stores the data as expected.
+	err := PutBlock(TEST_BLOCK, TEST_HASH)
+	if err == nil {
+		t.Log("err is nil")
+	}
+	if err != nil {
+		t.Fatalf("PutBlock: %v", err)
+	}
+
+	var result []byte
+	result, err = GetBlock(TEST_HASH)
+	t.Log("result = %v", result)
+	t.Log("err = %v", err)
+
+	if err != nil {
+		t.Fatalf("GetBlock: %s", err.Error())
+	}
+	if string(result) != string(TEST_BLOCK) {
+		t.Error("PutBlock/GetBlock mismatch")
+		t.Fatalf("PutBlock stored '%s', GetBlock retrieved '%s'",
+			string(TEST_BLOCK), string(result))
+	}
+}
+
 // Test finding Keep volumes.
 func TestFindKeepVolumes(t *testing.T) {
 	defer teardown()
@@ -168,6 +207,7 @@ func teardown() {
 }
 
 // store
+//     Low-level code to write Keep blocks directly to disk for testing.
 //
 func store(t *testing.T, keepdir string, filename string, block []byte) error {
 	blockdir := fmt.Sprintf("%s/%s", keepdir, filename[:3])

-----------------------------------------------------------------------


hooks/post-receive
-- 




More information about the arvados-commits mailing list