From 5cb87ad449a7dfd4d5e695e5284b89eeebd2cfc0 Mon Sep 17 00:00:00 2001
From: Bruno Goncalves <bgoncalv@redhat.com>
Date: May 21 2019 09:48:50 +0000
Subject: fix issue with interactive shell


we need to run timeout with --foreground to be support tests that use interactive shell

Also added test to make sure tests that use timeout cmdline also work

---

diff --git a/roles/standard-test-basic/files/run-basic-test b/roles/standard-test-basic/files/run-basic-test
index e7f9d25..c8e29eb 100755
--- a/roles/standard-test-basic/files/run-basic-test
+++ b/roles/standard-test-basic/files/run-basic-test
@@ -161,6 +161,6 @@ cd "$STR_WORKDIR"
 if [ -f "$STR_CMD" ]; then
     chmod 0775 "$STR_CMD"
 fi
-timeout "$STR_TIMEOUT" bash -c "$STR_CMD"
+timeout --foreground "$STR_TIMEOUT" bash -c "$STR_CMD"
 # Explicit return code
 exit $?
diff --git a/roles/standard-test-beakerlib/README.md b/roles/standard-test-beakerlib/README.md
index 3b22150..8059c4e 100644
--- a/roles/standard-test-beakerlib/README.md
+++ b/roles/standard-test-beakerlib/README.md
@@ -26,7 +26,7 @@ testing.  You can redefine the following variables in
 
 ## Test case parameters
 
- * **timeout**: abort test case after this time. More details on [timeout][1]. default: 0
+ * **timeout**: abort test case after this time. More details on [timeout][2]. default: 0
 
 Example usage:
 
@@ -83,3 +83,4 @@ See the Fedora CI [metadata wiki][1] for more details and examples on test
 metadata.
 
 [1]: https://fedoraproject.org/wiki/CI/Metadata
+[2]: http://man7.org/linux/man-pages/man1/timeout.1.html
diff --git a/roles/standard-test-beakerlib/files/run-beakerlib-test b/roles/standard-test-beakerlib/files/run-beakerlib-test
index 292bbf7..3f07bd9 100755
--- a/roles/standard-test-beakerlib/files/run-beakerlib-test
+++ b/roles/standard-test-beakerlib/files/run-beakerlib-test
@@ -167,7 +167,7 @@ fi
 if [ -f "$STR_BKR_TEST" ]; then
     debug "Running test from file: $STR_BKR_TEST"
     cd $(dirname "$STR_BKR_TEST")
-    timeout "$STR_TIMEOUT" /bin/sh -e ./$(basename "$STR_BKR_TEST")
+    timeout --foreground "$STR_TIMEOUT" /bin/sh -e ./$(basename "$STR_BKR_TEST")
     exit
 fi
 
@@ -178,10 +178,10 @@ if [ -d "$STR_BKR_TEST" ]; then
     get-test-deps -i .
     if [ -f "Makefile" ] && command -p -v "make" >"/dev/null" 2>&1; then
         debug "Running test from Makefile"
-        timeout "$STR_TIMEOUT" make run
+        timeout --foreground "$STR_TIMEOUT" make run
     elif [ -f "runtest.sh" ]; then
         debug "Running test from runtest.sh"
-        timeout "$STR_TIMEOUT" /bin/sh -e ./runtest.sh
+        timeout --foreground "$STR_TIMEOUT" /bin/sh -e ./runtest.sh
     else
         echo "FAIL test $STR_BKR_TEST do not know how to run test"
     fi
diff --git a/tests/basic.yml b/tests/basic.yml
index 2667143..b70b3ac 100644
--- a/tests/basic.yml
+++ b/tests/basic.yml
@@ -68,7 +68,6 @@
   - role: standard-test-basic
     tests:
     - test-basic-timeout:
-        run: ls; sleep 1
         timeout: 5
   tasks:
   - import_tasks: shared-tasks/artifacts_test_env.yml
@@ -84,7 +83,7 @@
   - role: standard-test-basic
     tests:
     - test-basic-timeout-fail:
-        run: ls; sleep 5
+        dir: test-basic-timeout
         timeout: 1
     ignore_errors: yes
   tasks:
diff --git a/tests/test-basic-timeout/runtest.sh b/tests/test-basic-timeout/runtest.sh
new file mode 100644
index 0000000..ca8cde5
--- /dev/null
+++ b/tests/test-basic-timeout/runtest.sh
@@ -0,0 +1,15 @@
+#!/bin/bash
+PACKAGE="bash"
+
+set -e
+
+# Interactive shell
+bash -lic "echo OK" | grep OK
+set +e
+timeout 1 sleep 3
+if [ $? -ne 124 ]; then
+    echo "FAIL: Should have exited wiht timeout, but didn't"
+    exit 1
+fi
+set -e
+timeout 2 sleep 1
diff --git a/tests/test-beakerlib-timeout/runtest.sh b/tests/test-beakerlib-timeout/runtest.sh
index 9b78f46..1c49fd9 100644
--- a/tests/test-beakerlib-timeout/runtest.sh
+++ b/tests/test-beakerlib-timeout/runtest.sh
@@ -6,7 +6,9 @@ PACKAGE="bash"
 
 rlJournalStart
     rlPhaseStartTest "beakerlib timeout test"
-        rlRun "ls /; sleep 5"
+        rlRun "bash -lic \"echo OK\" | grep OK" 0 "Interactive shell"
+        rlRun "timeout 2 sleep 1"
+        rlRun "timeout 1 sleep 3" 124
     rlPhaseEnd
 rlJournalPrintText
 rlJournalEnd

