Browse Source

add hidden test

Xiang Li 12 years ago
parent
commit
23775dc776
3 changed files with 63 additions and 24 deletions
  1. 5 5
      file_system/event.go
  2. 7 4
      file_system/file_system.go
  3. 51 15
      file_system/file_system_test.go

+ 5 - 5
file_system/event.go

@@ -20,7 +20,7 @@ type Event struct {
 	Dir        bool           `json:"dir,omitempty"`
 	PrevValue  string         `json:"prevValue,omitempty"`
 	Value      string         `json:"value,omitempty"`
-	Pairs      []KeyValuePair `json:"kvs,omitempty"`
+	KVPairs    []KeyValuePair `json:"kvs,omitempty"`
 	Expiration *time.Time     `json:"expiration,omitempty"`
 	TTL        int64          `json:"ttl,omitempty"` // Time to live in second
 	// The command index of the raft machine when the command is executed
@@ -30,10 +30,10 @@ type Event struct {
 
 // When user list a directory, we add all the node into key-value pair slice
 type KeyValuePair struct {
-	Key   string         `json:"key, omitempty"`
-	Value string         `json:"value,omitempty"`
-	Dir   bool           `json:"dir,omitempty"`
-	Pairs []KeyValuePair `json:"kvs,omitempty"`
+	Key     string         `json:"key, omitempty"`
+	Value   string         `json:"value,omitempty"`
+	Dir     bool           `json:"dir,omitempty"`
+	KVPairs []KeyValuePair `json:"kvs,omitempty"`
 }
 
 func newEvent(action string, key string, index uint64, term uint64) *Event {

+ 7 - 4
file_system/file_system.go

@@ -36,7 +36,7 @@ func (fs *FileSystem) Get(path string, recusive bool, index uint64, term uint64)
 	e := newEvent(Get, path, index, term)
 
 	if n.IsDir() { // node is dir
-		e.Pairs = make([]KeyValuePair, len(n.Children))
+		e.KVPairs = make([]KeyValuePair, len(n.Children))
 
 		i := 0
 
@@ -47,12 +47,12 @@ func (fs *FileSystem) Get(path string, recusive bool, index uint64, term uint64)
 			}
 
 			if subN.IsDir() {
-				e.Pairs[i] = KeyValuePair{
+				e.KVPairs[i] = KeyValuePair{
 					Key: subN.Path,
 					Dir: true,
 				}
 			} else {
-				e.Pairs[i] = KeyValuePair{
+				e.KVPairs[i] = KeyValuePair{
 					Key:   subN.Path,
 					Value: subN.Value,
 				}
@@ -61,6 +61,9 @@ func (fs *FileSystem) Get(path string, recusive bool, index uint64, term uint64)
 			i++
 		}
 
+		// eliminate hidden nodes
+		e.KVPairs = e.KVPairs[:i]
+
 	} else { // node is file
 		e.Value = n.Value
 	}
@@ -83,7 +86,7 @@ func (fs *FileSystem) Set(path string, value string, expireTime time.Time, index
 		return nil, err
 	}
 
-	f := newFile(name, value, fs.Index, fs.Term, d, "", expireTime)
+	f := newFile(path, value, fs.Index, fs.Term, d, "", expireTime)
 	e := newEvent(Set, path, fs.Index, fs.Term)
 	e.Value = f.Value
 

+ 51 - 15
file_system/file_system_test.go

@@ -12,17 +12,59 @@ func TestSetAndGet(t *testing.T) {
 	setAndGet(fs, "/foo/foo/bar", t)
 }
 
+func TestListDirectory(t *testing.T) {
+	fs := New()
+
+	// create dir /foo
+	// set key-value /foo/foo=bar
+	fs.Set("/foo/foo", "bar", Permanent, 1, 1)
+
+	// create dir /foo/fooDir
+	// set key-value /foo/fooDir/foo=bar
+	fs.Set("/foo/fooDir/foo", "bar", Permanent, 2, 1)
+
+	e, err := fs.Get("/foo", false, 2, 1)
+
+	if err != nil {
+		t.Fatalf("%v", err)
+	}
+
+	if len(e.KVPairs) != 2 {
+		t.Fatalf("wrong number of kv pairs [%d/2]", len(e.KVPairs))
+	}
+
+	if e.KVPairs[0].Key != "/foo/foo" || e.KVPairs[0].Value != "bar" {
+		t.Fatalf("wrong kv [/foo/foo/ / %s] -> [bar / %s]", e.KVPairs[0].Key, e.KVPairs[0].Value)
+	}
+
+	if e.KVPairs[1].Key != "/foo/fooDir" || e.KVPairs[1].Dir != true {
+		t.Fatalf("wrong kv [/foo/fooDir/ / %s] -> [true / %v]", e.KVPairs[1].Key, e.KVPairs[1].Dir)
+	}
+
+	// test hidden node
+
+	// create dir /foo/_hidden
+	// set key-value /foo/_hidden/foo -> bar
+	fs.Set("/foo/_hidden/foo", "bar", Permanent, 3, 1)
+
+	e, _ = fs.Get("/foo", false, 2, 1)
+
+	if len(e.KVPairs) != 2 {
+		t.Fatalf("hidden node is not hidden! %s", e.KVPairs[2].Key)
+	}
+}
+
 func TestRemove(t *testing.T) {
 	fs := New()
 
 	fs.Set("/foo", "bar", Permanent, 1, 1)
-	err := fs.Delete("/foo", false, 1, 1)
+	_, err := fs.Delete("/foo", false, 1, 1)
 
 	if err != nil {
 		t.Fatalf("cannot delete %s [%s]", "/foo", err.Error())
 	}
 
-	_, err = fs.InternalGet("/foo", 1, 1)
+	_, err = fs.Get("/foo", false, 1, 1)
 
 	if err == nil || err.Error() != "Key Not Found" {
 		t.Fatalf("can get the node after deletion")
@@ -32,19 +74,19 @@ func TestRemove(t *testing.T) {
 	fs.Set("/foo/car", "car", Permanent, 1, 1)
 	fs.Set("/foo/dar/dar", "dar", Permanent, 1, 1)
 
-	err = fs.Delete("/foo", false, 1, 1)
+	_, err = fs.Delete("/foo", false, 1, 1)
 
 	if err == nil {
 		t.Fatalf("should not be able to delete a directory without recursive")
 	}
 
-	err = fs.Delete("/foo", true, 1, 1)
+	_, err = fs.Delete("/foo", true, 1, 1)
 
 	if err != nil {
 		t.Fatalf("cannot delete %s [%s]", "/foo", err.Error())
 	}
 
-	_, err = fs.InternalGet("/foo", 1, 1)
+	_, err = fs.Get("/foo", false, 1, 1)
 
 	if err == nil || err.Error() != "Key Not Found" {
 		t.Fatalf("can get the node after deletion ")
@@ -90,25 +132,19 @@ func TestExpire(t *testing.T) {
 }
 
 func setAndGet(fs *FileSystem, path string, t *testing.T) {
-	err := fs.Set(path, "bar", Permanent, 1, 1)
+	_, err := fs.Set(path, "bar", Permanent, 1, 1)
 
 	if err != nil {
 		t.Fatalf("cannot set %s=bar [%s]", path, err.Error())
 	}
 
-	n, err := fs.InternalGet(path, 1, 1)
+	e, err := fs.Get(path, false, 1, 1)
 
 	if err != nil {
 		t.Fatalf("cannot get %s [%s]", path, err.Error())
 	}
 
-	value, err := n.Read()
-
-	if err != nil {
-		t.Fatalf("cannot read %s [%s]", path, err.Error())
-	}
-
-	if value != "bar" {
-		t.Fatalf("expect value of %s is bar [%s]", path, value)
+	if e.Value != "bar" {
+		t.Fatalf("expect value of %s is bar [%s]", path, e.Value)
 	}
 }