veyron/services/mgmt: bringing the mgmt test.sh integration tests back

Change-Id: Idf4fe26fc5c636aa11ccf1c0abf18f36b3fb0c26
diff --git a/services/mgmt/application/applicationd/test.sh b/services/mgmt/application/applicationd/test.sh
new file mode 100755
index 0000000..c3a3d92
--- /dev/null
+++ b/services/mgmt/application/applicationd/test.sh
@@ -0,0 +1,56 @@
+#!/bin/bash
+
+# Test the application repository daemon.
+#
+# This test starts an application repository daemon and uses the
+# application repository client to verify that <application>.Put(),
+# <application>.Match(), and <application>.Remove() work as expected.
+
+source "$(go list -f {{.Dir}} veyron.io/veyron/shell/lib)/shell_test.sh"
+
+readonly WORKDIR="${shell_test_WORK_DIR}"
+
+build() {
+  APPLICATIOND_BIN="$(shell_test::build_go_binary 'veyron.io/veyron/veyron/services/mgmt/application/applicationd')"
+  APPLICATION_BIN="$(shell_test::build_go_binary 'veyron.io/veyron/veyron/tools/application')"
+}
+
+main() {
+  cd "${WORKDIR}"
+  build
+
+  shell_test::setup_server_test
+
+  # Start the application repository daemon.
+  local -r REPO="applicationd-test-repo"
+  local -r STORE=$(shell::tmp_dir)
+  shell_test::start_server "${APPLICATIOND_BIN}" --name="${REPO}" --store="${STORE}" --veyron.tcp.address=127.0.0.1:0 \
+    || shell_test::fail "line ${LINENO} failed to start applicationd"
+
+  # Create an application envelope.
+  local -r APPLICATION="${REPO}/test-application/v1"
+  local -r PROFILE="test-profile"
+  local -r ENVELOPE_WANT=$(shell::tmp_file)
+  cat > "${ENVELOPE_WANT}" <<EOF
+{"Title":"title", "Args":[], "Binary":"foo", "Env":[]}
+EOF
+  "${APPLICATION_BIN}" put "${APPLICATION}" "${PROFILE}" "${ENVELOPE_WANT}" || shell_test::fail "line ${LINENO}: 'put' failed"
+
+  # Match the application envelope.
+  local -r ENVELOPE_GOT=$(shell::tmp_file)
+  "${APPLICATION_BIN}" match "${APPLICATION}" "${PROFILE}" | tee "${ENVELOPE_GOT}" || shell_test::fail "line ${LINENO}: 'match' failed"
+  if [[ $(cmp "${ENVELOPE_WANT}" "${ENVELOPE_GOT}" &> /dev/null) ]]; then
+    shell_test::fail "mismatching application envelopes"
+  fi
+
+  # Remove the application envelope.
+  "${APPLICATION_BIN}" remove "${APPLICATION}" "${PROFILE}" || shell_test::fail "line ${LINENO}: 'remove' failed"
+
+  # Check the application envelope no longer exists.
+  local -r RESULT=$(shell::check_result "${APPLICATION_BIN}" match "${APPLICATION}" "${PROFILE}")
+  shell_test::assert_ne "${RESULT}" "0" "${LINENO}"
+
+  shell_test::pass
+}
+
+main "$@"
diff --git a/services/mgmt/binary/binaryd/test.sh b/services/mgmt/binary/binaryd/test.sh
new file mode 100755
index 0000000..602a643
--- /dev/null
+++ b/services/mgmt/binary/binaryd/test.sh
@@ -0,0 +1,81 @@
+#!/bin/bash
+
+# Test the binary repository daemon.
+#
+# This test starts a binary repository daemon and uses the binary
+# repository client to verify that <binary>.Upload(),
+# <binary>.Download(), and <binary>.Delete() work as expected.
+
+source "$(go list -f {{.Dir}} veyron.io/veyron/shell/lib)/shell_test.sh"
+
+readonly WORKDIR="${shell_test_WORK_DIR}"
+
+build() {
+  BINARYD_BIN="$(shell_test::build_go_binary 'veyron.io/veyron/veyron/services/mgmt/binary/binaryd')"
+  BINARY_BIN="$(shell_test::build_go_binary 'veyron.io/veyron/veyron/tools/binary')"
+}
+
+main() {
+  cd "${WORKDIR}"
+  build
+
+  shell_test::setup_server_test
+
+  # Start the binary repository daemon.
+  local -r REPO="binaryd-test-repo"
+  shell_test::start_server "${BINARYD_BIN}" --name="${REPO}" --veyron.tcp.address=127.0.0.1:0 --http=127.0.0.1:0 \
+    || shell_test::fail "line ${LINENO} failed to start binaryd"
+  local -r HTTP_ADDR=$(grep 'HTTP server at: "' "${START_SERVER_LOG_FILE}" | sed -e 's/^.*HTTP server at: "//' | sed -e 's/"$//')
+
+  # Create a binary file.
+  local -r BINARY_SUFFIX="test-binary"
+  local -r BINARY="${REPO}/${BINARY_SUFFIX}"
+  local -r BINARY_FILE="${WORKDIR}/bin1"
+  dd if=/dev/urandom of="${BINARY_FILE}" bs=1000000 count=16 \
+    || shell_test::fail "line ${LINENO}: faile to create a random binary file"
+  "${BINARY_BIN}" upload "${BINARY}" "${BINARY_FILE}" || shell_test::fail "line ${LINENO}: 'upload' failed"
+
+  # Create TAR file.
+  local -r TAR="${REPO}/tarobj"
+  local -r TAR_FILE="${WORKDIR}/bin1.tar.gz"
+  tar zcvf "${TAR_FILE}" "${BINARY_FILE}"
+  "${BINARY_BIN}" upload "${TAR}" "${TAR_FILE}" || shell_test::fail "line ${LINENO}: 'upload' failed"
+
+  # Download the binary file.
+  local -r BINARY_FILE2="${WORKDIR}/bin2"
+  "${BINARY_BIN}" download "${BINARY}" "${BINARY_FILE2}" || shell_test::fail "line ${LINENO}: 'RPC download' failed"
+  if [[ $(cmp "${BINARY_FILE}" "${BINARY_FILE2}" &> /dev/null) ]]; then
+    shell_test::fail "mismatching binary file downloaded via RPC"
+  fi
+  local -r BINARY_FILE2_INFO=$(cat "${BINARY_FILE2}.__info")
+  shell_test::assert_eq "${BINARY_FILE2_INFO}" '{"Type":"application/octet-stream","Encoding":""}' "${LINENO}"
+
+  # Download the tar file.
+  local -r TAR_FILE2="${WORKDIR}/downloadedtar"
+  "${BINARY_BIN}" download "${TAR}" "${TAR_FILE2}" || shell_test::fail "line ${LINENO}: 'RPC download' failed"
+  if [[ $(cmp "${TAR_FILE}" "${TAR_FILE2}" &> /dev/null) ]]; then
+    shell_test::fail "mismatching tar file downloaded via RPC"
+  fi
+  local -r TAR_FILE2_INFO=$(cat "${TAR_FILE2}.__info")
+  shell_test::assert_eq "${TAR_FILE2_INFO}" '{"Type":"application/x-tar","Encoding":"gzip"}' "${LINENO}"
+
+  local -r BINARY_FILE3="${WORKDIR}/bin3"
+  curl -f -o "${BINARY_FILE3}" "http://${HTTP_ADDR}/${BINARY_SUFFIX}" || shell_test::fail "line ${LINENO}: 'HTTP download' failed"
+  if [[ $(cmp "${BINARY_FILE}" "${BINARY_FILE3}" &> /dev/null) ]]; then
+    shell_test::fail "mismatching binary file downloaded via HTTP"
+  fi
+
+  # Remove the files.
+  "${BINARY_BIN}" delete "${BINARY}" || shell_test::fail "line ${LINENO}: 'delete' failed"
+  "${BINARY_BIN}" delete "${TAR}" || shell_test::fail "line ${LINENO}: 'delete' failed"
+
+  # Check the files no longer exist.
+  local RESULT=$(shell::check_result "${BINARY_BIN}" download "${BINARY}" "${BINARY_FILE2}")
+  shell_test::assert_ne "${RESULT}" "0" "${LINENO}"
+  RESULT=$(shell::check_result "${BINARY_BIN}" download "${TAR}" "${TAR_FILE2}")
+  shell_test::assert_ne "${RESULT}" "0" "${LINENO}"
+
+  shell_test::pass
+}
+
+main "$@"
diff --git a/services/mgmt/build/buildd/test.sh b/services/mgmt/build/buildd/test.sh
new file mode 100755
index 0000000..b16b09b
--- /dev/null
+++ b/services/mgmt/build/buildd/test.sh
@@ -0,0 +1,57 @@
+#!/bin/bash
+
+# Test the build server daemon.
+#
+# This test starts a build server daemon and uses the build client to
+# verify that <build>.Build() works as expected.
+
+source "$(go list -f {{.Dir}} veyron.io/veyron/shell/lib)/shell_test.sh"
+
+readonly WORKDIR="${shell_test_WORK_DIR}"
+
+build() {
+  BUILDD_BIN="$(shell_test::build_go_binary 'veyron.io/veyron/veyron/services/mgmt/build/buildd')"
+  BUILD_BIN="$(shell_test::build_go_binary 'veyron.io/veyron/veyron/tools/build')"
+}
+
+main() {
+  cd "${WORKDIR}"
+  build
+
+  shell_test::setup_server_test
+
+  # Start the binary repository daemon.
+  local -r SERVER="buildd-test-server"
+  local GO_BIN=$(which go)
+  local -r GO_ROOT=$("${GO_BIN}" env GOROOT)
+  shell_test::start_server "${BUILDD_BIN}" --name="${SERVER}" --gobin="${GO_BIN}" --goroot="${GO_ROOT}" --veyron.tcp.address=127.0.0.1:0 \
+    || shell_test::fail "line ${LINENO} failed to start server"
+
+  # Create and build a test source file.
+  local -r GO_PATH=$(shell::tmp_dir)
+  local -r BIN_DIR="${GO_PATH}/bin"
+  mkdir -p "${BIN_DIR}"
+  local -r SRC_DIR="${GO_PATH}/src/test"
+  mkdir -p "${SRC_DIR}"
+  local -r SRC_FILE="${SRC_DIR}/test.go"
+  cat > "${SRC_FILE}" <<EOF
+package main
+
+import "fmt"
+
+func main() {
+  fmt.Printf("Hello World!\n")
+}
+EOF
+  GOPATH="${GO_PATH}" GOROOT="${GO_ROOT}" TMPDIR="${BIN_DIR}" "${BUILD_BIN}" build "${SERVER}" "test" || shell_test::fail "line ${LINENO}: 'build' failed"
+  if [[ ! -e "${BIN_DIR}/test" ]]; then
+    shell_test::fail "test binary not found"
+  fi
+  local -r GOT=$("${BIN_DIR}/test")
+  local -r WANT="Hello World!"
+  shell_test::assert_eq "${GOT}" "${WANT}" "${LINENO}"
+
+  shell_test::pass
+}
+
+main "$@"
diff --git a/services/mgmt/profile/profiled/test.sh b/services/mgmt/profile/profiled/test.sh
new file mode 100755
index 0000000..d7ea356
--- /dev/null
+++ b/services/mgmt/profile/profiled/test.sh
@@ -0,0 +1,72 @@
+#!/bin/bash
+
+# Test the profile repository daemon.
+#
+# This test starts an profile repository daemon and uses the profile
+# repository client to verify that <profile>.Put(), <profile>.Label(),
+# <profile>.Description(), <profile>.Speficiation(), and
+# <profile>.Remove() work as expected.
+
+source "$(go list -f {{.Dir}} veyron.io/veyron/shell/lib)/shell_test.sh"
+
+readonly WORKDIR="${shell_test_WORK_DIR}"
+
+build() {
+  PROFILED_BIN="$(shell_test::build_go_binary 'veyron.io/veyron/veyron/services/mgmt/profile/profiled')"
+  PROFILE_BIN="$(shell_test::build_go_binary 'veyron.io/veyron/veyron/tools/profile')"
+}
+
+main() {
+  local GOT OUTPUT RESULT WANT
+
+  cd "${WORKDIR}"
+  build
+
+  shell_test::setup_server_test
+
+  # Start the profile repository daemon.
+  local -r REPO="profiled-test-repo"
+  local -r STORE=$(shell::tmp_dir)
+  shell_test::start_server "${PROFILED_BIN}" --name="${REPO}" --veyron.tcp.address=127.0.0.1:0 --store="${STORE}" \
+    || shell_test::fail "line ${LINENO} failed to start server"
+
+  # Create a profile.
+  local -r PROFILE="${REPO}/test-profile"
+  "${PROFILE_BIN}" put "${PROFILE}" || shell_test::fail "line ${LINENO}: 'put' failed"
+
+  # Retrieve the profile label.
+  OUTPUT=$(shell::tmp_file)
+  "${PROFILE_BIN}" label "${PROFILE}" | tee "${OUTPUT}" || shell_test::fail "line ${LINENO}: 'label' failed"
+  GOT=$(cat "${OUTPUT}")
+  WANT="example"
+  shell_test::assert_eq "${GOT}" "${WANT}" "${LINENO}"
+
+  # Retrieve the profile description.
+  OUTPUT=$(shell::tmp_file)
+  "${PROFILE_BIN}" description "${PROFILE}" | tee "${OUTPUT}" || shell_test::fail "line ${LINENO}: 'description' failed"
+  GOT=$(cat "${OUTPUT}")
+  WANT="Example profile to test the profile manager implementation."
+  shell_test::assert_eq "${GOT}" "${WANT}" "${LINENO}"
+
+  # Retrieve the profile specification.
+  OUTPUT=$(shell::tmp_file)
+  "${PROFILE_BIN}" specification "${PROFILE}" | tee "${OUTPUT}" || shell_test::fail "line ${LINENO}: 'spec' failed"
+  GOT=$(cat "${OUTPUT}")
+  WANT='profile.Specification{Arch:"amd64", Description:"Example profile to test the profile manager implementation.", Format:"ELF", Libraries:map[profile.Library]struct {}{profile.Library{Name:"foo", MajorVersion:"1", MinorVersion:"0"}:struct {}{}}, Label:"example", OS:"linux"}'
+  shell_test::assert_eq "${GOT}" "${WANT}" "${LINENO}"
+
+  # Remove the profile.
+  "${PROFILE_BIN}" remove "${PROFILE}" || shell_test::fail "line ${LINENO}: 'remove' failed"
+
+  # Check the profile no longer exists.
+  RESULT=$(shell::check_result "${PROFILE_BIN}" label "${PROFILE}")
+  shell_test::assert_ne "${RESULT}" "0" "${LINENO}"
+  RESULT=$(shell::check_result "${PROFILE_BIN}" description "${PROFILE}")
+  shell_test::assert_ne "${RESULT}" "0" "${LINENO}"
+  RESULT=$(shell::check_result "${PROFILE_BIN}" specification "${PROFILE}")
+  shell_test::assert_ne "${RESULT}" "0" "${LINENO}"
+
+  shell_test::pass
+}
+
+main "$@"