blob: 0374601934d03832d7e60951a7820d5547abfab6 [file] [log] [blame]
Bogdan Capritac98a8b52014-12-01 10:08:47 -08001#!/bin/bash
2
Bogdan Caprita2b219362014-12-09 17:03:33 -08003# Test the device manager and related services and tools.
Bogdan Capritac98a8b52014-12-01 10:08:47 -08004
Jiri Simsa7d1b28d2014-12-12 22:39:37 -08005source "$(go list -f {{.Dir}} veyron.io/veyron/shell/lib)/shell_test.sh"
Bogdan Capritac98a8b52014-12-01 10:08:47 -08006
7readonly WORKDIR="${shell_test_WORK_DIR}"
8
9build() {
10 BINARYD_BIN="$(shell_test::build_go_binary 'veyron.io/veyron/veyron/services/mgmt/binary/binaryd')"
11 BINARY_BIN="$(shell_test::build_go_binary 'veyron.io/veyron/veyron/tools/binary')"
Bogdan Caprita4ad04992014-12-04 21:56:23 -080012 APPLICATIOND_BIN="$(shell_test::build_go_binary 'veyron.io/veyron/veyron/services/mgmt/application/applicationd')"
13 APPLICATION_BIN="$(shell_test::build_go_binary 'veyron.io/veyron/veyron/tools/application')"
Bogdan Capritac98a8b52014-12-01 10:08:47 -080014 AGENTD_BIN="$(shell_test::build_go_binary 'veyron.io/veyron/veyron/security/agent/agentd')"
15 SUIDHELPER_BIN="$(shell_test::build_go_binary 'veyron.io/veyron/veyron/services/mgmt/suidhelper')"
Bogdan Capritaa456f472014-12-10 10:18:03 -080016 DEVICEMANAGER_BIN="$(shell_test::build_go_binary 'veyron.io/veyron/veyron/services/mgmt/device/deviced')"
17 DEVICE_BIN="$(shell_test::build_go_binary 'veyron.io/veyron/veyron/tools/mgmt/device')"
Bogdan Caprita4ad04992014-12-04 21:56:23 -080018 NAMESPACE_BIN="$(shell_test::build_go_binary 'veyron.io/veyron/veyron/tools/namespace')"
19 PRINCIPAL_BIN="$(shell_test::build_go_binary 'veyron.io/veyron/veyron/tools/principal')"
20 DEBUG_BIN="$(shell_test::build_go_binary 'veyron.io/veyron/veyron/tools/debug')"
Jiri Simsa7d1b28d2014-12-12 22:39:37 -080021 DMINSTALL_SCRIPT="$(go list -f {{.Dir}} veyron.io/veyron/veyron/tools/mgmt/device)/dminstall"
Bogdan Capritaa40d3382014-12-19 16:30:26 -080022 DMUNINSTALL_SCRIPT="$(go list -f {{.Dir}} veyron.io/veyron/veyron/tools/mgmt/device)/dmuninstall"
Bogdan Capritac98a8b52014-12-01 10:08:47 -080023}
24
Bogdan Caprita4ad04992014-12-04 21:56:23 -080025# TODO(caprita): Move to shell_tesh.sh
26
27###############################################################################
28# Waits until the given name appears in the mounttable, within a set timeout.
29# Arguments:
30# path to namespace command-line tool
31# timeout in seconds
32# name to look up
33# Returns:
34# 0 if the name was successfully found, and 1 if the timeout expires before
35# the name appears.
36###############################################################################
37wait_for_mountentry() {
38 local -r NAMESPACE_BIN="$1"
39 local -r TIMEOUT="$2"
40 local -r NAME="$3"
41 for i in $(seq 1 "${TIMEOUT}"); do
42 local ENTRY=$("${NAMESPACE_BIN}" glob "${NAME}")
43 if [[ ! -z "${ENTRY}" ]]; then
44 return 0
45 fi
46 sleep 1
47 done
Bogdan Caprita4ad04992014-12-04 21:56:23 -080048 echo "Timed out waiting for ${NAME} to appear in the mounttable."
49 return 1
50}
51
Bogdan Capritaa40d3382014-12-19 16:30:26 -080052###############################################################################
53# Waits until the given process is gone, within a set timeout.
54# Arguments:
55# pid of process
56# timeout in seconds
57# Returns:
58# 0 if the pid is gone, and 1 if the timeout expires before that happens.
59###############################################################################
60wait_for_process_exit() {
61 local -r PID="$1"
62 local -r TIMEOUT="$2"
63 for i in $(seq 1 "${TIMEOUT}"); do
64 local RESULT=$(shell::check_result kill -0 "${PID}")
65 if [[ "${RESULT}" != "0" ]]; then
66 # Process is gone, can return early.
67 return 0
68 fi
69 sleep 1
70 done
71 echo "Timed out waiting for PID ${PID} to disappear."
72 return 1
73}
74
Bogdan Capritac98a8b52014-12-01 10:08:47 -080075main() {
76 cd "${WORKDIR}"
77 build
78
79 BIN_STAGING_DIR=$(shell::tmp_dir)
Bogdan Caprita2b219362014-12-09 17:03:33 -080080 cp "${AGENTD_BIN}" "${SUIDHELPER_BIN}" "${DEVICEMANAGER_BIN}" "${BIN_STAGING_DIR}"
Bogdan Capritac98a8b52014-12-01 10:08:47 -080081 shell_test::setup_server_test
82 # Unset VEYRON_CREDENTIALS set in setup_server_test.
83 export VEYRON_CREDENTIALS=
84
85 # TODO(caprita): Expose an option to turn --single_user off, so we can run
86 # test.sh by hand and exercise the code that requires root privileges.
87
Bogdan Caprita2b219362014-12-09 17:03:33 -080088 # Install and start device manager.
Bogdan Capritaa40d3382014-12-19 16:30:26 -080089 DM_INSTALL_DIR=$(shell::tmp_dir)
90 shell_test::start_server "${DMINSTALL_SCRIPT}" --single_user "${DM_INSTALL_DIR}" \
Bogdan Caprita2b219362014-12-09 17:03:33 -080091 "${BIN_STAGING_DIR}" -- --veyron.tcp.address=127.0.0.1:0 || shell_test::fail "line ${LINENO} failed to start device manager"
Bogdan Caprita550fa382014-12-10 15:59:40 -080092 # Dump dminstall's log, just to provide visibility into its steps.
Bogdan Capritaa40d3382014-12-19 16:30:26 -080093 local -r DM_PID="${START_SERVER_PID}"
Bogdan Caprita4ad04992014-12-04 21:56:23 -080094 cat "${START_SERVER_LOG_FILE}"
Bogdan Capritac98a8b52014-12-01 10:08:47 -080095
Bogdan Caprita9c4aa222014-12-10 14:46:30 -080096 local -r DM_NAME=$(hostname)
Bogdan Caprita2b219362014-12-09 17:03:33 -080097 # Verify that device manager is published under the expected name (hostname).
Bogdan Caprita9c4aa222014-12-10 14:46:30 -080098 shell_test::assert_ne "$("${NAMESPACE_BIN}" glob "${DM_NAME}")" "" "${LINENO}"
Bogdan Caprita4ad04992014-12-04 21:56:23 -080099
100 # Create the client principal, "alice".
101 "${PRINCIPAL_BIN}" create --overwrite=true ./alice alice >/dev/null || \
102 shell_test::fail "line ${LINENO}: create alice failed"
103
104 # All the commands executed henceforth will run as alice.
105 export VEYRON_CREDENTIALS=./alice
106
Bogdan Caprita2b219362014-12-09 17:03:33 -0800107 # Claim the device as "alice/myworkstation".
Bogdan Caprita9c4aa222014-12-10 14:46:30 -0800108 "${DEVICE_BIN}" claim "${DM_NAME}/device" myworkstation
Bogdan Caprita4ad04992014-12-04 21:56:23 -0800109
Bogdan Caprita2b219362014-12-09 17:03:33 -0800110 # Verify the device's default blessing is as expected.
Bogdan Caprita9c4aa222014-12-10 14:46:30 -0800111 shell_test::assert_eq "$("${DEBUG_BIN}" stats read "${DM_NAME}/__debug/stats/security/principal/blessingstore" | head -1 | sed -e 's/^.*Default blessings: '//)" \
Bogdan Caprita4ad04992014-12-04 21:56:23 -0800112 "alice/myworkstation" "${LINENO}"
113
114 # Start a binary server.
115 local -r BINARYD_NAME="binaryd"
116 shell_test::start_server "${BINARYD_BIN}" --name="${BINARYD_NAME}" \
Bogdan Capritabeee8fc2014-12-09 09:11:12 -0800117 --root_dir="$(shell::tmp_dir)/binstore" --veyron.tcp.address=127.0.0.1:0 --http=127.0.0.1:0 \
Bogdan Caprita4ad04992014-12-04 21:56:23 -0800118 || shell_test::fail "line ${LINENO} failed to start binaryd"
119
120 # Upload a binary to the binary server. The binary we upload is binaryd
121 # itself.
122 local -r SAMPLE_APP_BIN_NAME="${BINARYD_NAME}/testapp"
123 "${BINARY_BIN}" upload "${SAMPLE_APP_BIN_NAME}" "${BINARYD_BIN}"
124
125 # Verify that the binary we uploaded is shown by glob.
126 shell_test::assert_eq "$("${NAMESPACE_BIN}" glob "${SAMPLE_APP_BIN_NAME}")" \
127 "${SAMPLE_APP_BIN_NAME}" "${LINENO}"
128
129 # Start an application server.
130 local -r APPLICATIOND_NAME="applicationd"
131 shell_test::start_server "${APPLICATIOND_BIN}" --name="${APPLICATIOND_NAME}" \
132 --store="$(shell::tmp_dir)" --veyron.tcp.address=127.0.0.1:0 \
133 || shell_test::fail "line ${LINENO} failed to start applicationd"
134
135 # Upload an envelope for our test app.
136 local -r SAMPLE_APP_NAME="${APPLICATIOND_NAME}/testapp/v0"
137 local -r APP_PUBLISH_NAME="testbinaryd"
Bogdan Capritabeee8fc2014-12-09 09:11:12 -0800138 echo "{\"Title\":\"BINARYD\", \"Args\":[\"--name=${APP_PUBLISH_NAME}\", \"--root_dir=./binstore\", \"--veyron.tcp.address=127.0.0.1:0\"], \"Binary\":\"${SAMPLE_APP_BIN_NAME}\", \"Env\":[]}" > ./app.envelope && \
Bogdan Caprita4ad04992014-12-04 21:56:23 -0800139 "${APPLICATION_BIN}" put "${SAMPLE_APP_NAME}" test ./app.envelope && rm ./app.envelope
140
141 # Verify that the envelope we uploaded shows up with glob.
142 shell_test::assert_eq "$("${APPLICATION_BIN}" match "${SAMPLE_APP_NAME}" test | grep Title | sed -e 's/^.*"Title": "'// | sed -e 's/",//')" \
143 "BINARYD" "${LINENO}"
144
Bogdan Caprita2b219362014-12-09 17:03:33 -0800145 # Install the app on the device.
Bogdan Caprita9c4aa222014-12-10 14:46:30 -0800146 local -r INSTALLATION_NAME=$("${DEVICE_BIN}" install "${DM_NAME}/apps" "${SAMPLE_APP_NAME}" | sed -e 's/Successfully installed: "//' | sed -e 's/"//')
Bogdan Caprita4ad04992014-12-04 21:56:23 -0800147
Bogdan Caprita2b219362014-12-09 17:03:33 -0800148 # Verify that the installation shows up when globbing the device manager.
Bogdan Caprita9c4aa222014-12-10 14:46:30 -0800149 shell_test::assert_eq "$("${NAMESPACE_BIN}" glob "${DM_NAME}/apps/BINARYD/*")" \
Bogdan Caprita4ad04992014-12-04 21:56:23 -0800150 "${INSTALLATION_NAME}" "${LINENO}"
151
152 # Start an instance of the app, granting it blessing extension myapp.
Bogdan Capritaa456f472014-12-10 10:18:03 -0800153 local -r INSTANCE_NAME=$("${DEVICE_BIN}" start "${INSTALLATION_NAME}" myapp | sed -e 's/Successfully started: "//' | sed -e 's/"//')
Bogdan Caprita4ad04992014-12-04 21:56:23 -0800154 wait_for_mountentry "${NAMESPACE_BIN}" "5" "${APP_PUBLISH_NAME}"
155
Bogdan Caprita2b219362014-12-09 17:03:33 -0800156 # Verify that the instance shows up when globbing the device manager.
Bogdan Caprita9c4aa222014-12-10 14:46:30 -0800157 shell_test::assert_eq "$("${NAMESPACE_BIN}" glob "${DM_NAME}/apps/BINARYD/*/*")" "${INSTANCE_NAME}" "${LINENO}"
Bogdan Caprita4ad04992014-12-04 21:56:23 -0800158
159 # Verify the app's default blessing.
160 shell_test::assert_eq "$("${DEBUG_BIN}" stats read "${INSTANCE_NAME}/stats/security/principal/blessingstore" | head -1 | sed -e 's/^.*Default blessings: '//)" \
161 "alice/myapp/BINARYD" "${LINENO}"
162
163 # Stop the instance.
Bogdan Capritaa456f472014-12-10 10:18:03 -0800164 "${DEVICE_BIN}" stop "${INSTANCE_NAME}"
Bogdan Caprita4ad04992014-12-04 21:56:23 -0800165
166 # Verify that logs, but not stats, show up when globbing the stopped instance.
167 shell_test::assert_eq "$("${NAMESPACE_BIN}" glob "${INSTANCE_NAME}/stats/...")" "" "${LINENO}"
168 shell_test::assert_ne "$("${NAMESPACE_BIN}" glob "${INSTANCE_NAME}/logs/...")" "" "${LINENO}"
169
Bogdan Capritaa40d3382014-12-19 16:30:26 -0800170 kill "${DM_PID}"
171 wait_for_process_exit "${DM_PID}" 5
172
173 "${DMUNINSTALL_SCRIPT}" --single_user "${DM_INSTALL_DIR}" \
174 || shell_test::fail "line ${LINENO} failed to uninstall device manager"
175
176 if [[ -n "$(ls -A "${DM_INSTALL_DIR}")" ]]; then
177 shell_test::fail "${DM_INSTALL_DIR} is not empty"
178 fi
Bogdan Capritac98a8b52014-12-01 10:08:47 -0800179 shell_test::pass
180}
181
182main "$@"