Bogdan Caprita | c98a8b5 | 2014-12-01 10:08:47 -0800 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 3 | # Test the device manager and related services and tools. |
Bogdan Caprita | c98a8b5 | 2014-12-01 10:08:47 -0800 | [diff] [blame] | 4 | |
Jiri Simsa | 7d1b28d | 2014-12-12 22:39:37 -0800 | [diff] [blame] | 5 | source "$(go list -f {{.Dir}} veyron.io/veyron/shell/lib)/shell_test.sh" |
Bogdan Caprita | c98a8b5 | 2014-12-01 10:08:47 -0800 | [diff] [blame] | 6 | |
| 7 | readonly WORKDIR="${shell_test_WORK_DIR}" |
| 8 | |
| 9 | build() { |
| 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 Caprita | 4ad0499 | 2014-12-04 21:56:23 -0800 | [diff] [blame] | 12 | 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 Caprita | c98a8b5 | 2014-12-01 10:08:47 -0800 | [diff] [blame] | 14 | 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 Caprita | a456f47 | 2014-12-10 10:18:03 -0800 | [diff] [blame] | 16 | 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 Caprita | 4ad0499 | 2014-12-04 21:56:23 -0800 | [diff] [blame] | 18 | 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 Simsa | 7d1b28d | 2014-12-12 22:39:37 -0800 | [diff] [blame] | 21 | DMINSTALL_SCRIPT="$(go list -f {{.Dir}} veyron.io/veyron/veyron/tools/mgmt/device)/dminstall" |
Bogdan Caprita | a40d338 | 2014-12-19 16:30:26 -0800 | [diff] [blame^] | 22 | DMUNINSTALL_SCRIPT="$(go list -f {{.Dir}} veyron.io/veyron/veyron/tools/mgmt/device)/dmuninstall" |
Bogdan Caprita | c98a8b5 | 2014-12-01 10:08:47 -0800 | [diff] [blame] | 23 | } |
| 24 | |
Bogdan Caprita | 4ad0499 | 2014-12-04 21:56:23 -0800 | [diff] [blame] | 25 | # 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 | ############################################################################### |
| 37 | wait_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 Caprita | 4ad0499 | 2014-12-04 21:56:23 -0800 | [diff] [blame] | 48 | echo "Timed out waiting for ${NAME} to appear in the mounttable." |
| 49 | return 1 |
| 50 | } |
| 51 | |
Bogdan Caprita | a40d338 | 2014-12-19 16:30:26 -0800 | [diff] [blame^] | 52 | ############################################################################### |
| 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 | ############################################################################### |
| 60 | wait_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 Caprita | c98a8b5 | 2014-12-01 10:08:47 -0800 | [diff] [blame] | 75 | main() { |
| 76 | cd "${WORKDIR}" |
| 77 | build |
| 78 | |
| 79 | BIN_STAGING_DIR=$(shell::tmp_dir) |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 80 | cp "${AGENTD_BIN}" "${SUIDHELPER_BIN}" "${DEVICEMANAGER_BIN}" "${BIN_STAGING_DIR}" |
Bogdan Caprita | c98a8b5 | 2014-12-01 10:08:47 -0800 | [diff] [blame] | 81 | 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 Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 88 | # Install and start device manager. |
Bogdan Caprita | a40d338 | 2014-12-19 16:30:26 -0800 | [diff] [blame^] | 89 | DM_INSTALL_DIR=$(shell::tmp_dir) |
| 90 | shell_test::start_server "${DMINSTALL_SCRIPT}" --single_user "${DM_INSTALL_DIR}" \ |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 91 | "${BIN_STAGING_DIR}" -- --veyron.tcp.address=127.0.0.1:0 || shell_test::fail "line ${LINENO} failed to start device manager" |
Bogdan Caprita | 550fa38 | 2014-12-10 15:59:40 -0800 | [diff] [blame] | 92 | # Dump dminstall's log, just to provide visibility into its steps. |
Bogdan Caprita | a40d338 | 2014-12-19 16:30:26 -0800 | [diff] [blame^] | 93 | local -r DM_PID="${START_SERVER_PID}" |
Bogdan Caprita | 4ad0499 | 2014-12-04 21:56:23 -0800 | [diff] [blame] | 94 | cat "${START_SERVER_LOG_FILE}" |
Bogdan Caprita | c98a8b5 | 2014-12-01 10:08:47 -0800 | [diff] [blame] | 95 | |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 96 | local -r DM_NAME=$(hostname) |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 97 | # Verify that device manager is published under the expected name (hostname). |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 98 | shell_test::assert_ne "$("${NAMESPACE_BIN}" glob "${DM_NAME}")" "" "${LINENO}" |
Bogdan Caprita | 4ad0499 | 2014-12-04 21:56:23 -0800 | [diff] [blame] | 99 | |
| 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 Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 107 | # Claim the device as "alice/myworkstation". |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 108 | "${DEVICE_BIN}" claim "${DM_NAME}/device" myworkstation |
Bogdan Caprita | 4ad0499 | 2014-12-04 21:56:23 -0800 | [diff] [blame] | 109 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 110 | # Verify the device's default blessing is as expected. |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 111 | shell_test::assert_eq "$("${DEBUG_BIN}" stats read "${DM_NAME}/__debug/stats/security/principal/blessingstore" | head -1 | sed -e 's/^.*Default blessings: '//)" \ |
Bogdan Caprita | 4ad0499 | 2014-12-04 21:56:23 -0800 | [diff] [blame] | 112 | "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 Caprita | beee8fc | 2014-12-09 09:11:12 -0800 | [diff] [blame] | 117 | --root_dir="$(shell::tmp_dir)/binstore" --veyron.tcp.address=127.0.0.1:0 --http=127.0.0.1:0 \ |
Bogdan Caprita | 4ad0499 | 2014-12-04 21:56:23 -0800 | [diff] [blame] | 118 | || 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 Caprita | beee8fc | 2014-12-09 09:11:12 -0800 | [diff] [blame] | 138 | 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 Caprita | 4ad0499 | 2014-12-04 21:56:23 -0800 | [diff] [blame] | 139 | "${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 Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 145 | # Install the app on the device. |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 146 | local -r INSTALLATION_NAME=$("${DEVICE_BIN}" install "${DM_NAME}/apps" "${SAMPLE_APP_NAME}" | sed -e 's/Successfully installed: "//' | sed -e 's/"//') |
Bogdan Caprita | 4ad0499 | 2014-12-04 21:56:23 -0800 | [diff] [blame] | 147 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 148 | # Verify that the installation shows up when globbing the device manager. |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 149 | shell_test::assert_eq "$("${NAMESPACE_BIN}" glob "${DM_NAME}/apps/BINARYD/*")" \ |
Bogdan Caprita | 4ad0499 | 2014-12-04 21:56:23 -0800 | [diff] [blame] | 150 | "${INSTALLATION_NAME}" "${LINENO}" |
| 151 | |
| 152 | # Start an instance of the app, granting it blessing extension myapp. |
Bogdan Caprita | a456f47 | 2014-12-10 10:18:03 -0800 | [diff] [blame] | 153 | local -r INSTANCE_NAME=$("${DEVICE_BIN}" start "${INSTALLATION_NAME}" myapp | sed -e 's/Successfully started: "//' | sed -e 's/"//') |
Bogdan Caprita | 4ad0499 | 2014-12-04 21:56:23 -0800 | [diff] [blame] | 154 | wait_for_mountentry "${NAMESPACE_BIN}" "5" "${APP_PUBLISH_NAME}" |
| 155 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 156 | # Verify that the instance shows up when globbing the device manager. |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 157 | shell_test::assert_eq "$("${NAMESPACE_BIN}" glob "${DM_NAME}/apps/BINARYD/*/*")" "${INSTANCE_NAME}" "${LINENO}" |
Bogdan Caprita | 4ad0499 | 2014-12-04 21:56:23 -0800 | [diff] [blame] | 158 | |
| 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 Caprita | a456f47 | 2014-12-10 10:18:03 -0800 | [diff] [blame] | 164 | "${DEVICE_BIN}" stop "${INSTANCE_NAME}" |
Bogdan Caprita | 4ad0499 | 2014-12-04 21:56:23 -0800 | [diff] [blame] | 165 | |
| 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 Caprita | a40d338 | 2014-12-19 16:30:26 -0800 | [diff] [blame^] | 170 | 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 Caprita | c98a8b5 | 2014-12-01 10:08:47 -0800 | [diff] [blame] | 179 | shell_test::pass |
| 180 | } |
| 181 | |
| 182 | main "$@" |