Use the extension and authentication in the namespace browser tests.

This required moving/refactoring some of the test logic.  The
servicerunner binary is used to start the proxy, root mounttable, and
identity server.  All the binaries are built in the Makefile's "go/bin"
task.

I added a test for globbing on a rooted host:port mounttable.

Change-Id: I4dc8e24666ea0ca6e54bc1eda89f070643ccd8f8
diff --git a/.gitignore b/.gitignore
index 5940bac..2c8a455 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,6 +5,7 @@
 npm-debug.log
 go/bin/
 *.vdl.go
+tmp/
 
 # Vanadium
 /.v23
diff --git a/Makefile b/Makefile
index 2e0ebf1..3ecf090 100644
--- a/Makefile
+++ b/Makefile
@@ -13,12 +13,20 @@
 # see http://stackoverflow.com/questions/21708839/problems-setting-path-in-makefile for details.
 ##
 
+export GOPATH:=$(VANADIUM_ROOT)/release/projects/namespace_browser/go
+export VDLPATH:=$(VANADIUM_ROOT)/release/projects/namespace_browser/go
+export GOBIN:=$(VANADIUM_ROOT)/release/projects/namespace_browser/go/bin
+
 PATH:=$(VANADIUM_ROOT)/environment/cout/node/bin:$(PATH)
-PATH:=node_modules/.bin:$(PATH)
+PATH:=node_modules/.bin:$(GOBIN):$(PATH)
+
 ifndef TMPDIR
 	export TMPDIR:=/tmp
 endif
-TMPDIR:=$(TMPDIR)/vb
+ORIG_TMPDIR:=$(TMPDIR)
+TMPDIR:=$(TMPDIR)/viz
+
+VANADIUM_JS:=$(VANADIUM_ROOT)/release/javascript/core
 
 # All JS and CSS files except build.js and third party.
 BROWSERIFY_FILES = $(shell find src -name "*.js" -o -name "*.css")
@@ -28,9 +36,6 @@
 GO_FILES = $(shell find go -name "*.go")
 VDL_FILES = $(shell find go -name "*.vdl")
 
-# Builds everything.
-all: directories public/bundle.js public/bundle.html
-
 # Creating the bundle JS file.
 public/bundle.js: $(BROWSERIFY_FILES) node_modules src/components/help/content/*.md
 	:;jshint src # lint all src JavaScript files.
@@ -58,11 +63,31 @@
 	:;bower install --config.interactive=false --quiet
 	touch bower_components
 
+go/bin: directories
+	v23 go install v.io/core/veyron/services/mounttable/mounttabled
+	v23 go install v.io/core/veyron/tools/principal
+	v23 go install v.io/core/veyron/tools/servicerunner
+	v23 go install sample/sampled
+
 # PHONY targets:
 
+# Builds the viz bundle and go binaries.
+all: go/bin build
+
+# Builds the viz bundle.
+build: directories public/bundle.js public/bundle.html
+
 # Run unit and integration tests.
 test: all
 	:;jshint test # lint all test JavaScript files.
+	# Set the TMPDIR back to ORIG_TMPDIR before calling "make test-runner" so
+	# that we don't add "viz" suffix to TMPDIR twice.
+	TMPDIR=$(ORIG_TMPDIR) node $(VANADIUM_JS)/test/integration/runner.js -- \
+	make test-runner
+
+test-runner: directories
+	@$(RM) -fr $(VANADIUM_JS)/extension/build-test
+	$(MAKE) -C $(VANADIUM_JS)/extension build-test
 	:;./scripts/services/run-tests.sh
 
 # Continuously watch for changes to .js, .html or .css files.
@@ -71,15 +96,15 @@
 	watch -n 1 make
 
 # Continuously reruns the tests as they change.
-watch-test:
+watch-test: go/bin
 	:;PROVA_WATCH=true ./scripts/services/run-tests.sh
 
 # Serves the needed daemons and starts a server at http://localhost:9000
 # CTRL-C to stop
-start: all
+start: all go/bin
 	:;./scripts/services/run-webapp.sh
 
-# Create needed directories like temp.
+# Create needed directories like TMPDIR.
 directories:
 	mkdir -p $(TMPDIR)
 
@@ -91,4 +116,4 @@
 	rm -rf bower_components
 	rm -rf $(TMPDIR)
 
-.PHONY: start clean watch test watch-test directories
+.PHONY: all build start clean watch test watch-test directories
diff --git a/go/src/sample/sampled/main.go b/go/src/sample/sampled/main.go
index 5bf74f7..8194dc8 100644
--- a/go/src/sample/sampled/main.go
+++ b/go/src/sample/sampled/main.go
@@ -8,8 +8,7 @@
 	"sample/mocks"
 
 	"v.io/core/veyron/lib/signals"
-	"v.io/core/veyron/profiles"
-	_ "v.io/core/veyron/profiles"
+	"v.io/core/veyron/profiles/static"
 	"v.io/core/veyron2"
 	"v.io/core/veyron2/rt"
 	"v.io/core/veyron2/security"
@@ -63,7 +62,7 @@
 		}
 
 		// Create an endpoint and begin listening.
-		if endpoint, err := s.Listen(profiles.LocalListenSpec); err == nil {
+		if endpoint, err := s.Listen(static.ListenSpec); err == nil {
 			fmt.Printf("Listening at: %v\n", endpoint)
 		} else {
 			log.Fatal("error listening to service: ", err)
diff --git a/package.json b/package.json
index 4fc2986..e27c178 100644
--- a/package.json
+++ b/package.json
@@ -18,7 +18,8 @@
     "vulcanize": "^0.3.1",
     "postie": "~0.4.5",
     "lru-cache": "~2.5.0",
-    "tap-xunit": "~1.1.1"
+    "tap-xunit": "~1.1.1",
+    "envify": "~3.2.0"
   },
   "dependencies": {
     "bars": "git+https://github.com/steel/bars.git",
diff --git a/scripts/services/common.sh b/scripts/services/common.sh
index 1368a47..5175223 100755
--- a/scripts/services/common.sh
+++ b/scripts/services/common.sh
@@ -54,21 +54,6 @@
   fi
 }
 
-# build is used to install binaries needed to run services.
-build() {
-  export GOPATH="${VANADIUM_ROOT}/release/projects/namespace_browser/go"
-  export VDLPATH="${VANADIUM_ROOT}/release/projects/namespace_browser/go"
-  export GOBIN="${VANADIUM_ROOT}/release/projects/namespace_browser/go/bin"
-
-  v23 go install v.io/core/veyron/services/mounttable/mounttabled
-  v23 go install v.io/core/veyron/services/proxy/proxyd
-  v23 go install v.io/core/veyron/services/mgmt/binary/binaryd
-  v23 go install v.io/core/veyron/services/mgmt/build/buildd
-  v23 go install v.io/core/veyron/tools/principal
-  v23 go install v.io/wspr/veyron/services/wsprd
-  v23 go install sample/sampled
-}
-
 # common::fail will output a red FAILED message along with and optional message given
 # as first argument and an optional LOGFILE location to output as second argument.
 # This function will also exit the shell with an exit code of 1.
@@ -117,92 +102,45 @@
   common::fail "${NAME} service did not start or crashed, see log below for details" "${LOGFILE}"
 }
 
-# common::run is used to run the services needed to test and demon veyron browser
-# run will exit the shell if a process fails to start or panics and it will display
-# an error message along with the log file for the misbehaving service.
-common::run() {
-  build || common::fail "failed to build the binaries"
-  cd "${GOBIN}"
+common::run_mounttable() {
+  local -r NAME="$1"
+  local -r PORT="$2"
 
-  local -r ROOT_MOUNTTABLE_PORT="$1"
-  local -r HOUSE_MOUNTTABLE_PORT="$2"
-  local -r COTTAGE_MOUNTTABLE_PORT="$3"
-  local -r WSPR_PORT="$4"
-  local -r PROXY_PORT="$5"
-  local -r IDENTITY_DIR="$6"
-  local -r SEEK_BLESSSING="$7"
-
-  local -r PROXY_ADDR=127.0.0.1:"${PROXY_PORT}"
-  local -r IDENTITY_SERVER=/proxy.envyor.com:8101/identity/veyron-test/google
-
-  # Get credentials
-  export VEYRON_CREDENTIALS="${IDENTITY_DIR}";
-  if [[ ! -e "${IDENTITY_DIR}" ]] || [[ ! "$(ls -A ${IDENTITY_DIR})" ]]; then
-    ./principal create "${IDENTITY_DIR}" "veyron-browser" || common::fail "Failed to create principal."
-    if [[ "${SEEK_BLESSSING}" = true ]]; then
-      # TODO(aghassemi) This is temporarily needed since wspr can not talk to
-      # Identity server otherwise which is a known but to be fixed.
-      ./principal seekblessings || common::fail "Failed to seek blessing."
-    fi
-  fi
-
-  # Run each server in a sub shell so we can call common::fail if process fails to start
-  # or panics as it is running.
+  echo "Running ${NAME} mounttable on ${PORT}"
 
   # Allowed seconds for each service to start
   local -r SRV_TIMEOUT=10
   local -r TIMEDOUT_MSG="Timed out waiting for:"
 
-  # Run mounttables.
-  local -r ROOT_MTLOG="${TMPDIR}/mt_root.log"
+  # Run mounttable and wait for its startup signal.
   local -r MTLOG_MESSAGE="Mount table service at"
-  cat /dev/null > "${ROOT_MTLOG}"
+  local -r MTLOG="${TMPDIR}/mt_${NAME}.log"
+  cat /dev/null > "${MTLOG}"
   (
-    ./mounttabled --veyron.tcp.address="localhost:${ROOT_MOUNTTABLE_PORT}" &> "${ROOT_MTLOG}" &
-    fail_on_exit $! "root mounttable" "${ROOT_MTLOG}"
+    mounttabled --veyron.tcp.protocol=wsh --veyron.tcp.address="localhost:${PORT}" --name="${NAME}" &> "${MTLOG}" &
+    fail_on_exit $! "${NAME} mounttable" "${MTLOG}"
   ) &
-  shell::timed_wait_for "${SRV_TIMEOUT}" "${ROOT_MTLOG}" "${MTLOG_MESSAGE}" || common::fail "${TIMEDOUT_MSG} mounttable root"
+  shell::timed_wait_for "${SRV_TIMEOUT}" "${MTLOG}" "${MTLOG_MESSAGE}" || common::fail "${TIMEDOUT_MSG} ${NAME} mounttable"
+}
 
-  export NAMESPACE_ROOT=/localhost:"${ROOT_MOUNTTABLE_PORT}";
+# common::run is used to run the services needed to test and demon veyron browser
+# run will exit the shell if a process fails to start or panics and it will display
+# an error message along with the log file for the misbehaving service.
+common::run() {
+  local -r HOUSE_MOUNTTABLE_PORT="$1"
+  local -r COTTAGE_MOUNTTABLE_PORT="$2"
 
-  local -r HOUSE_MTLOG="${TMPDIR}/mt_house.log"
-  cat /dev/null > "${HOUSE_MTLOG}"
-  (
-    ./mounttabled --veyron.tcp.address="localhost:${HOUSE_MOUNTTABLE_PORT}" --name="house" &> "${HOUSE_MTLOG}" &
-    fail_on_exit $! "house mounttable" "${HOUSE_MTLOG}"
-  ) &
-  shell::timed_wait_for "${SRV_TIMEOUT}" "${HOUSE_MTLOG}" "${MTLOG_MESSAGE}" || common::fail "${TIMEDOUT_MSG} house mounttable"
+  # Run each server in a sub shell so we can call common::fail if process fails to start
+  # or panics as it is running.
 
-  local -r COTTAGE_MTLOG="${TMPDIR}/mt_cottage.log"
-  cat /dev/null > "${COTTAGE_MTLOG}"
-  (
-    ./mounttabled --veyron.tcp.address="localhost:${COTTAGE_MOUNTTABLE_PORT}" --name="cottage" &> "${COTTAGE_MTLOG}" &
-    fail_on_exit $! "cottage mounttable" "${COTTAGE_MTLOG}"
-  ) &
-  shell::timed_wait_for "${SRV_TIMEOUT}" "${COTTAGE_MTLOG}" "${MTLOG_MESSAGE}" || common::fail "${TIMEDOUT_MSG} cottage mounttable"
+  common::run_mounttable "house" ${HOUSE_MOUNTTABLE_PORT}
+  common::run_mounttable "cottage" ${COTTAGE_MOUNTTABLE_PORT}
 
-  # Run proxies.
-  local -r PROXYLOG="${TMPDIR}/proxy.log"
-  cat /dev/null > "${PROXYLOG}"
-  (
-    ./proxyd --v=1 --http=":0" -address="${PROXY_ADDR}" &> "${PROXYLOG}" &
-    fail_on_exit $! "proxy" "${PROXYLOG}"
-  ) &
-  shell::timed_wait_for "${SRV_TIMEOUT}" "${PROXYLOG}" "Proxy listening on" || common::fail "${TIMEDOUT_MSG} proxy"
-
-  local -r WSPRLOG="${TMPDIR}/wspr.log"
-  cat /dev/null > "${WSPRLOG}"
-  (
-    ./wsprd --v=1 --veyron.proxy="${PROXY_ADDR}" --port="${WSPR_PORT}" --identd="${IDENTITY_SERVER}" &> "${WSPRLOG}" &
-    fail_on_exit $! "wspr" "${WSPRLOG}"
-  ) &
-  shell::timed_wait_for "${SRV_TIMEOUT}" "${WSPRLOG}" "Listening at" || common::fail "${TIMEDOUT_MSG} wspr"
-
-  # Run some veyron services for demo and integration testing.
+  # Run sampled on a free port for demo and integration testing.
   local -r SAMPLEDLOG="${TMPDIR}/sampled.log"
   cat /dev/null > "${SAMPLEDLOG}"
   (
-    ./sampled &> "${SAMPLEDLOG}" &
+    sampled --veyron.tcp.protocol=wsh --veyron.tcp.address="localhost:0" &> "${SAMPLEDLOG}" &
     fail_on_exit $! "sampled" "${SAMPLEDLOG}"
   ) &
  }
diff --git a/scripts/services/run-tests.sh b/scripts/services/run-tests.sh
index 58ebc7f..8dcd791 100755
--- a/scripts/services/run-tests.sh
+++ b/scripts/services/run-tests.sh
@@ -6,26 +6,40 @@
 # pass true as first argument to run the tests in watch mode
 # script will exit with code 0 if tests pass and with 1 if tests fail.
 
+# This script is called by the "make test" command, which runs servicerunner and
+# sets the NAMESPACE_ROOT environment variable.  Thus, all commands run in this
+# script have access to a mounttable at NAMESPACE_ROOT.
+
 source "${VANADIUM_ROOT}/release/projects/namespace_browser/scripts/services/common.sh"
 
 main() {
-
-  local -r MOUNTTABLE_PORT=8881
   local -r MOUNTTABLE_PORT_HOUSE=8882
   local -r MOUNTTABLE_PORT_COTTAGE=8883
-  local -r WSPR_PORT=8885
-  local -r PROXY_PORT=8886
-  local -r VEYRON_IDENTITY_DIR="${TMPDIR}/test_credentials_dir"
-  local -r SEEK_BLESSSING=false
+
+  # Export the name of the house mounttable so it can be used in the tests.
+  export HOUSE_MOUNTTABLE="/localhost:${MOUNTTABLE_PORT_HOUSE}"
 
   PROVA_WATCH="${PROVA_WATCH-false}"
 
-  common::run "${MOUNTTABLE_PORT}" "${MOUNTTABLE_PORT_HOUSE}" "${MOUNTTABLE_PORT_COTTAGE}" "${WSPR_PORT}" "${PROXY_PORT}" "${VEYRON_IDENTITY_DIR}" "${SEEK_BLESSSING}"
+  common::run "${MOUNTTABLE_PORT_HOUSE}" "${MOUNTTABLE_PORT_COTTAGE}"
 
   echo -e "\033[34m-Services are running\033[0m"
 
+  # This is the id of the Vanadium test extension.  It is put into the veyron.js
+  # source code by the "--globalTranform envify" flag in PROVA_OPTIONS.
+  export VANADIUM_EXTENSION_ID=geagjbjjbbamldjlcbpabgdpeopikgne
+
   cd "${VANADIUM_ROOT}/release/projects/namespace_browser"
-  local PROVA_OPTIONS="--browser --includeFilenameAsPackage --launch chrome --plugin proxyquireify/plugin --transform ./main-transform"
+  local -r VANADIUM_JS=${VANADIUM_ROOT}/release/javascript/core
+
+  local PROVA_OPTIONS="--browser \
+    --includeFilenameAsPackage \
+    --launch chrome \
+    --plugin proxyquireify/plugin \
+    --transform ./main-transform \
+    --globalTransform envify \
+    --log tmp/chrome.log \
+    --options=--load-extension=${VANADIUM_JS}/extension/build-test/,--ignore-certificate-errors,--enable-logging=stderr"
   local -r PROVA="${VANADIUM_ROOT}/release/projects/namespace_browser/node_modules/.bin/prova"
   local -r TAP_XUNIT="${VANADIUM_ROOT}/release/projects/namespace_browser/node_modules/.bin/tap-xunit"
   local -r XUNIT_OUTPUT_FILE="${XUNIT_OUTPUT_FILE-${TMPDIR}/test_output.xml}"
diff --git a/scripts/services/run-webapp.sh b/scripts/services/run-webapp.sh
index 251cccf..e1c5755 100755
--- a/scripts/services/run-webapp.sh
+++ b/scripts/services/run-webapp.sh
@@ -9,13 +9,12 @@
   local -r MOUNTTABLE_PORT=5167
   local -r MOUNTTABLE_PORT_HOUSE=5168
   local -r MOUNTTABLE_PORT_COTTAGE=5169
-  local -r WSPR_PORT=8124
-  local -r PROXY_PORT=5164
-  local -r VEYRON_IDENTITY_DIR="${TMPDIR}/app_credentials_dir"
   local -r HTTP_PORT=9000
-  local -r SEEK_BLESSSING=true
 
-  common::run "${MOUNTTABLE_PORT}" "${MOUNTTABLE_PORT_HOUSE}" "${MOUNTTABLE_PORT_COTTAGE}" "${WSPR_PORT}" "${PROXY_PORT}" "${VEYRON_IDENTITY_DIR}" "${SEEK_BLESSSING}"
+  common::run_mounttable "root" ${MOUNTTABLE_PORT}
+  export NAMESPACE_ROOT=/localhost:"${MOUNTTABLE_PORT}"
+
+  common::run "${MOUNTTABLE_PORT_HOUSE}" "${MOUNTTABLE_PORT_COTTAGE}"
 
   local -r SERVE="${VANADIUM_ROOT}/release/projects/namespace_browser/node_modules/.bin/serve"
   "${SERVE}" "${VANADIUM_ROOT}"/release/projects/namespace_browser/public/. --port "${HTTP_PORT}" --compress &
diff --git a/src/veyron-config.js b/src/veyron-config.js
index c45d270..ecb80a1 100644
--- a/src/veyron-config.js
+++ b/src/veyron-config.js
@@ -1,9 +1,6 @@
 var logLevels = require('veyron').logLevels;
 var veyronConfig = {
-  'logLevel': logLevels.INFO,
-  'wspr': 'http://localhost:8124',
-  // TODO(alexfandrianto): Set this back to true ASAP.
-  'authenticate': false
+  logLevel: logLevels.INFO
 };
 
 module.exports = veyronConfig;
diff --git a/test/integration/services/namespace/service.js b/test/integration/services/namespace/service.js
index e60cc47..eadb332 100644
--- a/test/integration/services/namespace/service.js
+++ b/test/integration/services/namespace/service.js
@@ -4,16 +4,18 @@
 var proxyquire = require('proxyquireify')(require);
 var mockLRUCache = require('./mocks/lru-cache');
 
-// 8885 is the expected wspr port to be running for the tests
 // @noCallThru ensures this completely overrdies the original config
 // instead of inheriting the properties that are not defined here from
 // the original dependency
 var veyronConfigForTest = {
-  'authenticate': false,
-  'wspr': 'http://localhost:8885',
   '@noCallThru': true
 };
 
+// The NAMESPACE_ROOT environment variable is set by servicerunner in the "make
+// test" target.  That environment variable is picked up by the "envify" prova
+// transform and used to set process.env.NAMESPACE_ROOT.
+var globalRoot = process.env.NAMESPACE_ROOT;
+
 // Require namespaceService but using test specific mocks and configs
 var namespaceService =
   proxyquire('../../../../src/services/namespace/service.js', {
@@ -93,10 +95,8 @@
   }
 });
 
-test('getChildren of rooted /localhost:8881/house/kitchen', function(t) {
-
-  // 8881 is the expected root mounttable port to be running for the tests
-  namespaceService.getChildren('/localhost:8881/house/kitchen').
+test('getChildren of rooted ' + globalRoot + '/house/kitchen', function(t) {
+  namespaceService.getChildren(globalRoot + '/house/kitchen').
   then(function assertResult(result) {
     assertIsImmutable(t, result);
     // Wait until we finish, we expect 2 items, lights and smoke-detector
@@ -115,7 +115,7 @@
   function assertLightSwitch(item) {
     assertServer(t, item, {
       name: 'lights',
-      objectName: '/localhost:8881/house/kitchen/lights',
+      objectName: globalRoot + '/house/kitchen/lights',
       isGlobbable: false,
       type: 'unknown'
     });
@@ -124,7 +124,48 @@
   function assertSmokeDetector(item) {
     assertServer(t, item, {
       name: 'smoke-detector',
-      objectName: '/localhost:8881/house/kitchen/smoke-detector',
+      objectName: globalRoot + '/house/kitchen/smoke-detector',
+      isGlobbable: false,
+      type: 'unknown'
+    });
+  }
+});
+
+// The HOUSE_MOUNTTABLE environment variable is set by run-tests.sh.  That
+// environment variable is picked up by the "envify" prova transform and used to
+// set process.env.HOUSE_MOUNTTABLE.
+var hostPortRoot = process.env.HOUSE_MOUNTTABLE;
+
+test('getChildren of rooted ' + hostPortRoot + '/kitchen', function(t) {
+  namespaceService.getChildren(hostPortRoot + '/kitchen').
+  then(function assertResult(result) {
+    assertIsImmutable(t, result);
+    // Wait until we finish, we expect 2 items, lights and smoke-detector
+    result.events.on('end', function validate() {
+      mercury.watch(result, function(children) {
+        children = _.sortBy(children, 'mountedName');
+        assertLightSwitch(children[0]);
+        assertSmokeDetector(children[1]);
+        t.end();
+      });
+    });
+    result.events.on('streamError', t.end);
+    result.events.on('itemError', t.end);
+  }).catch(t.end);
+
+  function assertLightSwitch(item) {
+    assertServer(t, item, {
+      name: 'lights',
+      objectName: hostPortRoot + '/kitchen/lights',
+      isGlobbable: false,
+      type: 'unknown'
+    });
+  }
+
+  function assertSmokeDetector(item) {
+    assertServer(t, item, {
+      name: 'smoke-detector',
+      objectName: hostPortRoot + '/kitchen/smoke-detector',
       isGlobbable: false,
       type: 'unknown'
     });
@@ -433,4 +474,4 @@
     t.pass('correctly returned an error');
     t.end();
   });
-}
\ No newline at end of file
+}