veyron/services/mounttable/mounttable: Add test.sh and fix a couple of bugs.
Add a test for the mounttabled binary. It verifies that the basic
functionality is working as intended.
Fix the first argument to NewNeighborhoodServer. It is supposed to be
the name of this instance in the neighbodhood.
Fix the deduplication code in neighbor().
Change-Id: Ibb982192085722a8354ba9ba825c069ac1993854
diff --git a/services/mounttable/lib/neighborhood.go b/services/mounttable/lib/neighborhood.go
index 5f3a110..7b49c1b 100644
--- a/services/mounttable/lib/neighborhood.go
+++ b/services/mounttable/lib/neighborhood.go
@@ -132,10 +132,11 @@
var reply []mounttable.MountedServer
si := nh.mdns.ResolveInstance(instance, "veyron")
+ // Use a map to dedup any addresses seen
+ addrMap := make(map[string]uint32)
+
// Look for any TXT records with addresses.
for _, rr := range si.TxtRRs {
- // Use a map to dedup any addresses seen
- addrMap := make(map[string]uint32)
for _, s := range rr.Txt {
if !strings.HasPrefix(s, addressPrefix) {
continue
@@ -143,9 +144,9 @@
addr := s[len(addressPrefix):]
addrMap[addr] = rr.Header().Ttl
}
- for addr, ttl := range addrMap {
- reply = append(reply, mounttable.MountedServer{addr, ttl})
- }
+ }
+ for addr, ttl := range addrMap {
+ reply = append(reply, mounttable.MountedServer{addr, ttl})
}
if reply != nil {
diff --git a/services/mounttable/mounttabled/mounttable.go b/services/mounttable/mounttabled/mounttable.go
index fadc4a8..23cf394 100644
--- a/services/mounttable/mounttabled/mounttable.go
+++ b/services/mounttable/mounttabled/mounttable.go
@@ -78,7 +78,7 @@
mtAddr := naming.JoinAddressName(mtEndpoint.String(), "")
r.Namespace().SetRoots(mtAddr)
- vlog.Infof("Mount table service at: %q (%s)",
+ vlog.Infof("Mount table service at: %q endpoint: %s",
name,
naming.JoinAddressName(mtEndpoint.String(), ""))
@@ -98,7 +98,7 @@
vlog.Errorf("nhServer.Listen failed: %v", err)
return
}
- nh, err := mounttable.NewNeighborhoodServer("", *nhName, mtAddr)
+ nh, err := mounttable.NewNeighborhoodServer(*nhName, mtAddr)
if err != nil {
vlog.Errorf("NewNeighborhoodServer failed: %v", err)
return
diff --git a/services/mounttable/mounttabled/test.sh b/services/mounttable/mounttabled/test.sh
new file mode 100755
index 0000000..1f15be1
--- /dev/null
+++ b/services/mounttable/mounttabled/test.sh
@@ -0,0 +1,87 @@
+#!/bin/sh
+
+# Test the mounttabled binary
+#
+# This test starts a mounttable server with the neighborhood option enabled,
+# and mounts 1) the mounttable on itself as 'myself', and 2) www.google.com:80
+# as 'google'.
+#
+# Then it verifies that <mounttable>.Glob(*) and <neighborhood>.Glob(nhname)
+# return the correct result.
+
+toplevel=$(git rev-parse --show-toplevel)
+go=${toplevel}/scripts/build/go
+thisscript=$0
+
+echo "Test directory: $(dirname $0)"
+
+builddir=$(mktemp -d --tmpdir=${toplevel}/go)
+tmplog=$(mktemp)
+trap onexit EXIT
+
+onexit() {
+ cd /
+ exec 2> /dev/null
+ kill -9 $(jobs -p)
+ rm -rf $builddir $tmplog
+}
+
+FAIL() {
+ [ $# -gt 0 ] && echo "$thisscript $*"
+ echo FAIL
+ exit 1
+}
+
+PASS() {
+ echo PASS
+ exit 0
+}
+
+# Build mounttabled and mounttable binaries.
+cd $builddir
+$go build veyron/services/mounttable/mounttabled || FAIL "line $LINENO: failed to build mounttabled"
+$go build veyron/tools/mounttable || FAIL "line $LINENO: failed to build mounttable"
+
+# Start mounttabled and find its endpoint.
+nhname=test$$
+./mounttabled --address=localhost:0 --neighborhood_name=$nhname > $tmplog 2>&1 &
+
+for i in 1 2 3 4; do
+ ep=$(grep "Mount table service at:" $tmplog | sed -re 's/^.*endpoint: ([^ ]*).*/\1/')
+ if [ -n "$ep" ]; then
+ break
+ fi
+ sleep 1
+done
+
+[ -z $ep ] && FAIL "line $LINENO: no server"
+
+# Get the neighborhood endpoint from the mounttable.
+nhep=$(./mounttable glob $ep nh | grep ^nh | cut -d' ' -f2)
+[ -z $nhep ] && FAIL "line $LINENO: no neighborhood server"
+
+# Mount objects and verify the result.
+./mounttable mount "${ep}/myself" "$ep" 5m > /dev/null || FAIL "line $LINENO: failed to mount the mounttable on itself"
+./mounttable mount "${ep}/google" /www.google.com:80 5m > /dev/null || FAIL "line $LINENO: failed to mount www.google.com"
+
+# <mounttable>.Glob('*')
+got=$(./mounttable glob $ep '*' | sed 's/TTL .m..s/TTL XmXXs/' | sort)
+want="[${ep}]
+google /www.google.com:80 (TTL XmXXs)
+myself ${ep} (TTL XmXXs)
+nh ${nhep} (TTL XmXXs)"
+
+if [ "$got" != "$want" ]; then
+ FAIL "line $LINENO: unexpected output. Got $got, want $want"
+fi
+
+# <neighborhood>.Glob('nhname')
+got=$(./mounttable glob $nhep $nhname | sed 's/TTL .m..s/TTL XmXXs/' | sort)
+want="[${nhep}]
+${nhname} ${ep} (TTL XmXXs)"
+
+if [ "$got" != "$want" ]; then
+ FAIL "line $LINENO: unexpected output. Got $got, want $want"
+fi
+
+PASS