#!/bin/bash # # Sandstorm - Personal Cloud Sandbox # Copyright (c) 2014 Sandstorm Development Group, Inc. and contributors # All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. set -euo pipefail global XVFB_PID := ''"" global RUN_SELENIUM := $(RUN_SELENIUM:-true) global BUNDLE_PATH := ''"" global THIS_DIR := $[dirname $[readlink -f $(BASH_SOURCE[0])]] global METEOR_DEV_BUNDLE := $["$THIS_DIR/../find-meteor-dev-bundle.sh] global NODEJS := ""$METEOR_DEV_BUNDLE/bin/node"" global NPM := ""$METEOR_DEV_BUNDLE/bin/npm"" global SELENIUM_JAR := '"selenium-server-standalone-2.53.0.jar'" global SELENIUM_JAR_SHA256 := '"67b88cbfd3b130de6ff3770948f56cc485fd1abb5b7a769397d9050a59b1e036'" global SELENIUM_DOWNLOAD_URL := ""https://selenium-release.storage.googleapis.com/2.53/$SELENIUM_JAR"" proc cleanExit { global rc := $1 if test $rc -ne 0 { echo "Log output: " cat "$SANDSTORM_DIR/var/log/sandstorm.log" } "$SANDSTORM_DIR/sandstorm" stop sleep 1 if test $rc -eq 0 { # Only clean up the test directory if the test run was successful - if tests failed, # it's nice to be able to inspect the logs. We wipe out $SANDSTORM_DIR before starting # a new test run, so this is fine. rm -rf $SANDSTORM_DIR } if test -n $XVFB_PID { # Send SIGINT to the selenium-server child of the backgrounded xvfb-run, so # it will exit cleanly and the Xvfb process will also be cleaned up. # We don't actually know that PID, so we find it with pgrep. kill $[pgrep --parent $XVFB_PID java] wait $XVFB_PID } exit $rc } proc cacheSeleniumJar { if [[ ! -e ./$SELENIUM_JAR ]] { global DOWNLOAD := $[mktemp selenium-download.XXXXXX] curl -o $DOWNLOAD $SELENIUM_DOWNLOAD_URL global DOWNLOAD_SHASUM := $[sha256sum $DOWNLOAD | cut -f 1 -d ' ] if [[ "$DOWNLOAD_SHASUM" = "$SELENIUM_JAR_SHA256" ]] { mv $DOWNLOAD ./$SELENIUM_JAR } else { echo "Selenium jar download didn't match expected checksum. Discarding." exit 1 } } } proc checkInstalled { if ! $[which $1 >/dev/null !2 >/dev/null] { echo "Couldn't find executable '$1' - try installing the $2 package?" exit 1 } } # Parse arguments. while [ $# -gt 0 ] { matchstr $1 { --no-selenium { global RUN_SELENIUM := '"false'" } * { if test -n $BUNDLE_PATH { echo "Multiple bundle paths specified, please name only one." exit 1 } global BUNDLE_PATH := $[readlink -f $1] } } shift } if test -z $BUNDLE_PATH { echo "No bundle path specified; perhaps you meant to write '$0 sandstorm-0-fast.tar.xz'?" exit 1 } cd $THIS_DIR checkInstalled firefox firefox $NPM install if test $RUN_SELENIUM != "false" { checkInstalled java default-jre-headless checkInstalled xvfb-run Xvfb checkInstalled pgrep procps cacheSeleniumJar xvfb-run --server-args="-screen 0, 1280x1024x24" java -jar ./$SELENIUM_JAR & global XVFB_PID := $BgPid } export SANDSTORM_DIR=$THIS_DIR/tmp-sandstorm export OVERRIDE_SANDSTORM_DEFAULT_DIR=$SANDSTORM_DIR # Picking some fixed ports because email tests are being flaky with system-assigned ports and we # don't do parallel tests yet anyway. export PORT=9000 export MONGO_PORT=9001 export SMTP_LISTEN_PORT=9002 export SMTP_OUTGOING_PORT=9003 export IP_INTERFACE_TEST_PORT=9004 export LAUNCH_URL="http://local.sandstorm.io:$PORT" rm -rf $SANDSTORM_DIR ../install.sh -d -u $BUNDLE_PATH echo "IS_TESTING=true ALLOW_DEMO_ACCOUNTS=true BASE_URL=http://local.sandstorm.io:$PORT WILDCARD_HOST=*.local.sandstorm.io:$PORT PORT=$PORT MONGO_PORT=$MONGO_PORT SMTP_LISTEN_PORT=$(SMTP_LISTEN_PORT) MAIL_URL=smtp://127.0.0.1:$(SMTP_OUTGOING_PORT) UPDATE_CHANNEL=none " >> "$SANDSTORM_DIR/sandstorm.conf" "$SANDSTORM_DIR/sandstorm" start echo -n "Waiting for sandstorm to start." global COUNT := '0' while ! curl -s localhost:$PORT > /dev/null { if test $COUNT -gt 600 { # wait 60 seconds for server to start echo "Sandstorm failed to start" cleanExit 1 } global COUNT := $($COUNT+1) echo -n . sleep .1 }; echo set +e export DISABLE_DEMO=true $NPM test cleanExit $Status (CommandList children: [ (C {(set)} {(-euo)} {(pipefail)}) (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:XVFB_PID) op:Equal rhs:{(DQ )} spids:[59])] spids: [59] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:RUN_SELENIUM) op: Equal rhs: { (DQ (BracedVarSub token: suffix_op: (StringUnary op_id:VTest_ColonHyphen arg_word:{(true)}) spids: [65 69] ) ) } spids: [63] ) ] spids: [63] ) (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:BUNDLE_PATH) op:Equal rhs:{(DQ )} spids:[72])] spids: [72] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:THIS_DIR) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [ (C {(dirname)} { (DQ (CommandSubPart command_list: (CommandList children: [ (C {(readlink)} {(-f)} { (DQ (BracedVarSub token: bracket_op: (ArrayIndex expr:(ArithWord w:{(Lit_Digits 0)})) spids: [87 92] ) ) } ) ] ) left_token: spids: [81 94] ) ) } ) ] ) left_token: spids: [77 96] ) } spids: [76] ) ] spids: [76] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:METEOR_DEV_BUNDLE) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [(C {(DQ ($ VSub_Name "$THIS_DIR") (/../find-meteor-dev-bundle.sh))})] ) left_token: spids: [99 104] ) } spids: [98] ) ] spids: [98] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:NODEJS) op: Equal rhs: {(DQ ($ VSub_Name "$METEOR_DEV_BUNDLE") (/bin/node))} spids: [106] ) ] spids: [106] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:NPM) op: Equal rhs: {(DQ ($ VSub_Name "$METEOR_DEV_BUNDLE") (/bin/npm))} spids: [112] ) ] spids: [112] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:SELENIUM_JAR) op: Equal rhs: {(DQ (selenium-server-standalone-2.53.0.jar))} spids: [118] ) ] spids: [118] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:SELENIUM_JAR_SHA256) op: Equal rhs: {(DQ (67b88cbfd3b130de6ff3770948f56cc485fd1abb5b7a769397d9050a59b1e036))} spids: [123] ) ] spids: [123] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:SELENIUM_DOWNLOAD_URL) op: Equal rhs: { (DQ ("https://selenium-release.storage.googleapis.com/2.53/") ($ VSub_Name "$SELENIUM_JAR") ) } spids: [128] ) ] spids: [128] ) (FuncDef name: cleanExit body: (BraceGroup children: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:rc) op: Equal rhs: {($ VSub_Number "$1")} spids: [143] ) ] spids: [143] ) (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {($ VSub_Name "$rc")} {(-ne)} {(0)} {(Lit_Other "]")}) terminator: ) ] action: [ (C {(echo)} {(DQ ("Log output: "))}) (C {(cat)} {(DQ ($ VSub_Name "$SANDSTORM_DIR") (/var/log/sandstorm.log))}) ] spids: [-1 161] ) ] spids: [-1 179] ) (C {(DQ ($ VSub_Name "$SANDSTORM_DIR") (/sandstorm))} {(stop)}) (C {(sleep)} {(1)}) (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {($ VSub_Name "$rc")} {(-eq)} {(0)} {(Lit_Other "]")}) terminator: ) ] action: [(C {(rm)} {(-rf)} {(DQ ($ VSub_Name "$SANDSTORM_DIR"))})] spids: [-1 210] ) ] spids: [-1 234] ) (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {(-n)} {(DQ ($ VSub_Name "$XVFB_PID"))} {(Lit_Other "]")}) terminator: ) ] action: [ (C {(kill)} { (CommandSubPart command_list: (CommandList children: [ (C {(pgrep)} {(--parent)} {($ VSub_Name "$XVFB_PID")} {(java)}) ] ) left_token: spids: [269 277] ) } ) (C {(wait)} {($ VSub_Name "$XVFB_PID")}) ] spids: [-1 252] ) ] spids: [-1 285] ) (C {(exit)} {($ VSub_Name "$rc")}) ] spids: [140] ) spids: [135 139] ) (FuncDef name: cacheSeleniumJar body: (BraceGroup children: [ (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (LogicalNot child: (BoolUnary op_id: BoolUnary_e child: {(./) ($ VSub_Name "$SELENIUM_JAR")} ) ) ) terminator: ) ] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:DOWNLOAD) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [(C {(mktemp)} {(selenium-download.XXXXXX)})] ) left_token: spids: [321 325] ) } spids: [320] ) ] spids: [320] ) (C {(curl)} {(-o)} {($ VSub_Name "$DOWNLOAD")} {($ VSub_Name "$SELENIUM_DOWNLOAD_URL")} ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:DOWNLOAD_SHASUM) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [ (Pipeline children: [ (C {(sha256sum)} {($ VSub_Name "$DOWNLOAD")}) (C {(cut)} {(-f)} {(1)} {(-d)} {(SQ <" ">)}) ] negated: False ) ] ) left_token: spids: [338 356] ) } spids: [337] ) ] spids: [337] ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobEqual left: {(DQ ($ VSub_Name "$DOWNLOAD_SHASUM"))} right: {(DQ ($ VSub_Name "$SELENIUM_JAR_SHA256"))} ) ) terminator: ) ] action: [ (C {(mv)} {($ VSub_Name "$DOWNLOAD")} {(./) ($ VSub_Name "$SELENIUM_JAR")}) ] spids: [-1 377] ) ] else_action: [ (C {(echo)} { (DQ ( "Selenium jar download didn't match expected checksum. Discarding." ) ) } ) (C {(exit)} {(1)}) ] spids: [388 403] ) ] spids: [-1 317] ) ] spids: [-1 406] ) ] spids: [299] ) spids: [295 298] ) (FuncDef name: checkInstalled body: (BraceGroup children: [ (If arms: [ (if_arm cond: [ (Sentence child: (Pipeline children: [ (C { (CommandSubPart command_list: (CommandList children: [ (SimpleCommand words: [{(which)} {($ VSub_Number "$1")}] redirects: [ (Redir op_id: Redir_Great fd: -1 arg_word: {(/dev/null)} spids: [427] ) (Redir op_id: Redir_Great fd: 2 arg_word: {(/dev/null)} spids: [430] ) ] ) ] ) left_token: spids: [422 432] ) } ) ] negated: True ) terminator: ) ] action: [ (C {(echo)} { (DQ ("Couldn't find executable '") ($ VSub_Number "$1") ("' - try installing the ") ($ VSub_Number "$2") (" package?") ) } ) (C {(exit)} {(1)}) ] spids: [-1 436] ) ] spids: [-1 455] ) ] spids: [415] ) spids: [411 414] ) (While cond: [ (Sentence child: (C {(Lit_Other "[")} {($ VSub_Pound "$#")} {(-gt)} {(0)} {(Lit_Other "]")}) terminator: ) ] body: (DoGroup children: [ (Case to_match: {($ VSub_Number "$1")} arms: [ (case_arm pat_list: [{(--no-selenium)}] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:RUN_SELENIUM) op: Equal rhs: {(DQ (false))} spids: [491] ) ] spids: [491] ) ] spids: [487 488 497 -1] ) (case_arm pat_list: [{(Lit_Other "*")}] action: [ (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {(-n)} {(DQ ($ VSub_Name "$BUNDLE_PATH"))} {(Lit_Other "]")} ) terminator: ) ] action: [ (C {(echo)} {(DQ ("Multiple bundle paths specified, please name only one."))} ) (C {(exit)} {(1)}) ] spids: [-1 517] ) ] spids: [-1 532] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:BUNDLE_PATH) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [(C {(readlink)} {(-f)} {(DQ ($ VSub_Number "$1"))})] ) left_token: spids: [536 544] ) } spids: [535] ) ] spids: [535] ) ] spids: [500 501 547 -1] ) ] spids: [480 484 550] ) (C {(shift)}) ] spids: [477 555] ) ) (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {(-z)} {(DQ ($ VSub_Name "$BUNDLE_PATH"))} {(Lit_Other "]")}) terminator: ) ] action: [ (C {(echo)} { (DQ ("No bundle path specified; perhaps you meant to write '") ($ VSub_Number "$0") (" sandstorm-0-fast.tar.xz'?") ) } ) (C {(exit)} {(1)}) ] spids: [-1 572] ) ] spids: [-1 588] ) (C {(cd)} {(DQ ($ VSub_Name "$THIS_DIR"))}) (C {(checkInstalled)} {(firefox)} {(firefox)}) (C {(DQ ($ VSub_Name "$NPM"))} {(install)}) (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {(DQ ($ VSub_Name "$RUN_SELENIUM"))} {(KW_Bang "!") (Lit_Other "=")} {(DQ (false))} {(Lit_Other "]")} ) terminator: ) ] action: [ (C {(checkInstalled)} {(java)} {(default-jre-headless)}) (C {(checkInstalled)} {(xvfb-run)} {(Xvfb)}) (C {(checkInstalled)} {(pgrep)} {(procps)}) (C {(cacheSeleniumJar)}) (Sentence child: (C {(xvfb-run)} {(--server-args) (Lit_Other "=") (DQ ("-screen 0, 1280x1024x24"))} {(java)} {(-jar)} {(./) ($ VSub_Name "$SELENIUM_JAR")} ) terminator: ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:XVFB_PID) op: Equal rhs: {($ VSub_Bang "$!")} spids: [676] ) ] spids: [676] ) ] spids: [-1 631] ) ] spids: [-1 679] ) (C {(export)} {(Lit_VarLike "SANDSTORM_DIR=") ($ VSub_Name "$THIS_DIR") (/tmp-sandstorm)}) (C {(export)} {(Lit_VarLike "OVERRIDE_SANDSTORM_DEFAULT_DIR=") ($ VSub_Name "$SANDSTORM_DIR")}) (C {(export)} {(Lit_VarLike "PORT=") (9000)}) (C {(export)} {(Lit_VarLike "MONGO_PORT=") (9001)}) (C {(export)} {(Lit_VarLike "SMTP_LISTEN_PORT=") (9002)}) (C {(export)} {(Lit_VarLike "SMTP_OUTGOING_PORT=") (9003)}) (C {(export)} {(Lit_VarLike "IP_INTERFACE_TEST_PORT=") (9004)}) (C {(export)} {(Lit_VarLike "LAUNCH_URL=") (DQ ("http://local.sandstorm.io:") ($ VSub_Name "$PORT"))} ) (C {(rm)} {(-rf)} {(DQ ($ VSub_Name "$SANDSTORM_DIR"))}) (C {(../install.sh)} {(-d)} {(-u)} {(DQ ($ VSub_Name "$BUNDLE_PATH"))}) (SimpleCommand words: [ {(echo)} { (DQ ("IS_TESTING=true\n") ("ALLOW_DEMO_ACCOUNTS=true\n") ("BASE_URL=http://local.sandstorm.io:") ($ VSub_Name "$PORT") ("\n") ("WILDCARD_HOST=*.local.sandstorm.io:") ($ VSub_Name "$PORT") ("\n") ("PORT=") ($ VSub_Name "$PORT") ("\n") ("MONGO_PORT=") ($ VSub_Name "$MONGO_PORT") ("\n") ("SMTP_LISTEN_PORT=") (${ VSub_Name SMTP_LISTEN_PORT) ("\n") ("MAIL_URL=smtp://127.0.0.1:") (${ VSub_Name SMTP_OUTGOING_PORT) ("\n") ("UPDATE_CHANNEL=none\n") ) } ] redirects: [ (Redir op_id: Redir_DGreat fd: -1 arg_word: {(DQ ($ VSub_Name "$SANDSTORM_DIR") (/sandstorm.conf))} spids: [782] ) ] ) (C {(DQ ($ VSub_Name "$SANDSTORM_DIR") (/sandstorm))} {(start)}) (C {(echo)} {(-n)} {(DQ ("Waiting for sandstorm to start."))}) (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:COUNT) op:Equal rhs:{(0)} spids:[805])] spids: [805] ) (Sentence child: (While cond: [ (Sentence child: (Pipeline children: [ (SimpleCommand words: [{(curl)} {(-s)} {(localhost) (Lit_Other ":") ($ VSub_Name "$PORT")}] redirects: [(Redir op_id:Redir_Great fd:-1 arg_word:{(/dev/null)} spids:[820])] ) ] negated: True ) terminator: ) ] body: (DoGroup children: [ (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {(DQ ($ VSub_Name "$COUNT"))} {(-gt)} {(600)} {(Lit_Other "]")} ) terminator: ) ] action: [(C {(echo)} {(DQ ("Sandstorm failed to start"))}) (C {(cleanExit)} {(1)})] spids: [-1 843] ) ] spids: [-1 861] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:COUNT) op: Equal rhs: { (ArithSubPart anode: (ArithBinary op_id: Arith_Plus left: (ArithWord w:{($ VSub_Name "$COUNT")}) right: (ArithWord w:{(Lit_Digits 1)}) ) spids: [865 870] ) } spids: [864] ) ] spids: [864] ) (C {(echo)} {(-n)} {(.)}) (C {(sleep)} {(.1)}) ] spids: [825 884] ) ) terminator: ) (C {(echo)}) (C {(set)} {(Lit_Other "+") (e)}) (C {(export)} {(Lit_VarLike "DISABLE_DEMO=") (true)}) (C {(DQ ($ VSub_Name "$NPM"))} {(test)}) (C {(cleanExit)} {($ VSub_QMark "$?")}) ] )