summaryrefslogtreecommitdiff
path: root/evergreen/selinux_test_executor.sh
diff options
context:
space:
mode:
Diffstat (limited to 'evergreen/selinux_test_executor.sh')
-rwxr-xr-xevergreen/selinux_test_executor.sh240
1 files changed, 86 insertions, 154 deletions
diff --git a/evergreen/selinux_test_executor.sh b/evergreen/selinux_test_executor.sh
index fc1e04cf2be..a0616d314d6 100755
--- a/evergreen/selinux_test_executor.sh
+++ b/evergreen/selinux_test_executor.sh
@@ -1,177 +1,109 @@
#!/bin/bash
-set +o errexit
-readonly k_log_path="/var/log/mongodb/mongod.log"
-readonly k_mongo="$(pwd)/dist-test/bin/mongo"
-readonly k_test_path="$1"
-return_code=1
+set -o errexit
+set -o xtrace
-export PATH="$(dirname "$k_mongo"):$PATH"
+mongo="$(pwd)/dist-test/bin/mongo"
+export PATH="$(dirname "$mongo"):$PATH"
+if [ ! -f "$mongo" ]; then
+ echo "Mongo shell at $mongo is missing"
+ exit 1
+fi
-function print_err() {
+function print() {
echo "$@" >&2
}
function monitor_log() {
- sed "s!^!mongod| $(date '+%F %H-%M-%S') !" <(sudo --non-interactive tail -f $k_log_path)
-}
-
-function output_ausearch() {
- local cmd_parameters="AVC,USER_AVC,SELINUX_ERR,USER_SELINUX_ERR"
-
- echo ""
- echo "====== SELinux errors (ausearch -m $cmd_parameters): ======"
- sudo --non-interactive ausearch -m $cmd_parameters -ts $1
-}
-
-function output_journalctl() {
- echo ""
- echo "============================== journalctl ========================================="
- sudo --non-interactive journalctl --no-pager --catalog --since="$1" | grep -i mongo
+ sed "s!^!mongod| $(date '+%F %H-%M-%S') !" <(sudo --non-interactive tail -f /var/log/mongodb/mongod.log)
}
-function fail_and_exit_err() {
-
- echo ""
- echo "==================================================================================="
- echo "++++++++ Test failed, outputting last 5 seconds of additional log info ++++++++++++"
- echo "==================================================================================="
- output_ausearch "$(date --utc --date='5 seconds ago' '+%x %H:%M:%S')"
- output_journalctl "$(date --utc --date='5 seconds ago' +'%Y-%m-%d %H:%M:%S')"
-
- echo ""
- echo "==== FAIL: $1 ===="
+TEST_PATH="$1"
+if [ ! -f "$TEST_PATH" ]; then
+ print "No test supplied or test file not found. Run:"
+ print " $(basename "${BASH_SOURCE[0]}") <path>"
exit 1
-}
-
-function create_mongo_config() {
- echo "Writing /etc/mongod.conf for $k_test_path:"
- "$k_mongo" --nodb --norc --quiet --eval='
- assert(load("'"$k_test_path"'"));
- const test = new TestDefinition();
- print(JSON.stringify(test.config, null, 2));
-
- ' | sudo --non-interactive tee /etc/mongod.conf
-}
-
-function start_mongod() {
- # Start mongod and if it won't come up, fail and exit
-
- sudo --non-interactive systemctl start mongod \
- && sudo --non-interactive systemctl status mongod || (
- fail_and_exit_err "systemd failed to start mongod server!"
- )
-}
-
-function wait_for_mongod_to_accept_connections() {
- # Once the mongod process starts via systemd, it can still take a couple of seconds
- # to set up and accept connections... we will wait for log id 23016 to show up
- # indicating that the server is ready to accept incoming connections before starting the tests
-
- local server_ready=0
- local wait_seconds=2
- local wait_retries_max=30
- local wait_retries=0
-
- while [[ $wait_retries -le $wait_retries_max ]]; do
- local server_status="$(grep 23016 $k_log_path || echo "")"
-
- if [ "$server_status" != "" ]; then
- server_ready=1
- break
- fi
-
- sleep $wait_seconds
- ((wait_retries++))
- done
-
- if [ ! $server_ready ]; then
- fail_and_exit_err "failed to connect to mongod server after waiting for $(($wait_seconds * $wait_retries)) seconds!"
- fi
-}
-
-function clear_mongo_config() {
- # stop mongod, zero mongo log, clean up database, set all booleans to off
- sudo --non-interactive bash -c '
- systemctl stop mongod
-
- rm -f '"$k_log_path"'
- touch '"$k_log_path"'
- chown mongod '"$k_log_path"'
-
- rm -rf /var/lib/mongo/*
-
- rm -rf /etc/sysconfig/mongod /etc/mongod
-
- setsebool mongod_can_connect_ldap off
- setsebool mongod_can_use_kerberos off
- '
-}
-
-function exit_with_code() {
- exit $return_code
-}
-
-function setup_test_definition() {
- "$k_mongo" --nodb --norc --quiet --eval='
- assert(load("'"$k_test_path"'"));
- (() => {
- const test = new TestDefinition();
- print("Running setup() for '"$k_test_path"'");
- test.setup();
- })();
- '
-}
-
-function run_test() {
- "$k_mongo" --norc --gssapiServiceName=mockservice --eval='
- assert(load("'"$k_test_path"'"));
- print("Running test '"$k_test_path"'");
+fi
- (() => {
- const test = new TestDefinition();
+# test file is even good before going on
+if ! "$mongo" --nodb --norc --quiet "$TEST_PATH"; then
+ print "File $TEST_PATH has syntax errors"
+ exit 1
+fi
- try {
- test.run();
- } finally {
- test.teardown();
- }
- })();
- ' || fail_and_exit_err "Test failed"
+# stop mongod, zero mongo log, clean up database, set all booleans to off
+sudo --non-interactive bash -c '
+ systemctl stop mongod
- echo "SUCCESS: $k_test_path"
-}
+ rm -f /var/log/mongodb/mongod.log
+ touch /var/log/mongodb/mongod.log
+ chown mongod /var/log/mongodb/mongod.log
-if [ ! -f "$k_mongo" ]; then
- print_err "Mongo shell at $k_mongo is missing"
- exit 1
-fi
+ rm -rf /var/lib/mongo/*
-if [ ! -f "$k_test_path" ]; then
- print_err "No test supplied or test file not found. Run:"
- print_err "$(basename "${BASH_SOURCE[0]}") <path>"
- exit 1
-fi
+ rm -rf /etc/sysconfig/mongod /etc/mongod
-# Ensure file containing tests is valid before executing
-if ! "$k_mongo" --nodb --norc --quiet "$k_test_path"; then
- print_err "File $k_test_path has syntax errors"
- exit 1
-fi
+ setsebool mongod_can_connect_snmp off
+ setsebool mongod_can_connect_ldap off
+ setsebool mongod_can_use_kerberos off
+'
-echo "STARTING TEST: $k_test_path"
+# create mongo config
+"$mongo" --nodb --norc --quiet --eval='
+ assert(load("'"$TEST_PATH"'"));
+ const test = new TestDefinition();
+ print(typeof(test.config) === "string" ? test.config : JSON.stringify(test.config, null, 2));
+' | sudo --non-interactive tee /etc/mongod.conf
-clear_mongo_config
-create_mongo_config
-setup_test_definition
+# setup
+"$mongo" --nodb --norc --quiet --eval='
+ assert(load("'"$TEST_PATH"'"));
+ const test = new TestDefinition();
+ jsTest.log("Running setup()");
+ test.setup();
+'
# start log monitor, also kill it on exit
monitor_log &
-monitor_pid="$!"
-trap "sudo --non-interactive pkill -P $monitor_pid; exit_with_code" SIGINT SIGTERM ERR EXIT
-
-start_mongod
-wait_for_mongod_to_accept_connections
-run_test
+MONITORPID="$!"
+trap "sudo --non-interactive pkill -P $MONITORPID" SIGINT SIGTERM ERR EXIT
+
+# start mongod and if it won't come up, log SELinux errors
+ts="$(date --utc --date='1 seconds ago' '+%x %H:%M:%S')"
+tsj="$(date --utc --date='1 seconds ago' +'%Y-%m-%d %H:%M:%S')"
+sudo --non-interactive systemctl start mongod \
+ && sudo --non-interactive systemctl status mongod || (
+ set +o errexit
+ echo "=== SELinux errors:"
+ sudo --non-interactive ausearch -m AVC,USER_AVC,SELINUX_ERR,USER_SELINUX_ERR -ts $ts
+ echo "=== journalctl --unit=mongod:"
+ sudo --non-interactive journalctl --no-pager --since="$tsj" --unit=mongod --unit=systemd --catalog
+ echo "=== /var/log/mongodb/mongod.log:"
+ sudo --non-interactive cat /var/log/mongodb/mongod.log
+ echo "==== FAIL: mongod service was not started successfully"
+ exit 1
+)
+
+# run test and teardown
+"$mongo" --norc --gssapiServiceName=mockservice --eval='
+ assert(load("'"$TEST_PATH"'"));
+ // name is such to prevent collisions
+ const test_812de7ce = new TestDefinition();
+ try {
+ jsTest.log("Running test");
+ test_812de7ce.run();
+ } finally {
+ test_812de7ce.teardown();
+ }
+' || (
+ echo "==== FAIL: test returned result: $?"
+ echo "=== SELinux errors:"
+ set +o errexit
+ sudo --non-interactive ausearch -m AVC,USER_AVC,SELINUX_ERR,USER_SELINUX_ERR -ts $ts
+ echo "=== /var/log/mongodb/mongod.log:"
+ sudo --non-interactive cat /var/log/mongodb/mongod.log
+ exit 1
+)
-return_code=0
+set +o xtrace
+echo "SUCCESS: $TEST_PATH"