From da398b64e1e3babfb2c821206a93fb40218f0b2e Mon Sep 17 00:00:00 2001 From: Kurtis Rader Date: Sun, 11 Aug 2019 20:53:51 -0700 Subject: [PATCH] Add some `disown` tests This doesn't totally resolve #1359 since one of the tests won't pass until issue https://github.com/att/ast/issues/1377 is fixed. So that test is currently commented out. Resolves #1359 --- .travis.yml | 4 +- src/cmd/ksh93/tests/b_jobs.exp | 109 +++++++++++++++++++++++++++-- src/cmd/ksh93/tests/b_jobs.exp.out | 5 ++ 3 files changed, 111 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index aa7d7f03bbdf..c15d8010ab2c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,7 +14,7 @@ matrix: compiler: gcc env: - DISTRO_TYPE=fedora - INSTALL_REQUIREMENTS="dnf repolist; dnf --allowerasing install -y gcc python3 meson sudo langpacks-zh_CN ed ncurses vi findutils which nmap-ncat expect git glibc-all-langpacks" + INSTALL_REQUIREMENTS="dnf repolist; dnf --allowerasing install -y gcc python3 meson sudo langpacks-zh_CN ed ncurses vi findutils which nmap-ncat expect git glibc-all-langpacks procps" before_install: - docker pull ${DISTRO_TYPE} @@ -35,7 +35,7 @@ matrix: compiler: gcc env: - DISTRO_TYPE=ubuntu - INSTALL_REQUIREMENTS="apt-get update; DEBIAN_FRONTEND=noninteractive apt-get install -yq gcc python3 sudo locales ed vim python3-pip ninja-build findutils debianutils netcat-openbsd expect git; pip3 install meson==0.44.0; locale-gen en_US.UTF-8" + INSTALL_REQUIREMENTS="apt-get update; DEBIAN_FRONTEND=noninteractive apt-get install -yq gcc python3 sudo locales ed vim python3-pip ninja-build findutils debianutils netcat-openbsd expect git procps; pip3 install meson==0.44.0; locale-gen en_US.UTF-8" before_install: - docker pull ${DISTRO_TYPE} diff --git a/src/cmd/ksh93/tests/b_jobs.exp b/src/cmd/ksh93/tests/b_jobs.exp index cb1d272448bb..547a1176fda5 100644 --- a/src/cmd/ksh93/tests/b_jobs.exp +++ b/src/cmd/ksh93/tests/b_jobs.exp @@ -1,11 +1,33 @@ # vim: set filetype=expect: # -# Interactive tests for jobs, bg, fg and related builtins -set pid [spawn $ksh] +# Interactive tests for jobs, bg, fg, disown, and related builtins. +# +# Note the `-l` arg to force an interactive login shell. Most of the expect based unit tests don't +# bother with that flag since they only need an interactive shell. This unit test however is +# different because some tests (e.g., of the `disown` command) only work in a login shell. +set pid [spawn $ksh -l] expect_prompt # Terminal rows and columns may default to zero so ensure sane values. send "stty rows 24 cols 80\r" expect_prompt +send "set -o\r" +expect_prompt + +# WTF +send "print PATH=\$PATH\r" +expect_prompt +send "ls -l /bin/ps\r" +expect_prompt +send "type ps\r" +expect_prompt +send "ps -fp 999999\r" +expect_prompt + +# ====== +# Enable job monitoring. This should already be enabled but let's be paranoid and explicit. +log_test_entry +send "set -o monitor\r" +expect_prompt # ====== # Get the current stty values to aid debugging failures. This is also necessary so the [ctrl-Z] test @@ -16,9 +38,86 @@ send "stty -a\r" expect_prompt # ====== -# An invalid job id should be an error. -send "fg %3\r" -expect -re ": fg: no such job\r\n" +# An invalid `disown` job ID should be an error. +log_test_entry +send "disown 666\r" +expect -re ": disown: no such job\r\n" { + puts "disown invalid job ID fails" +} +expect_prompt + +# ====== +# Put a job in the background and verify that not disowning it causes the job to be killed when +# the shell terminates. +log_test_entry +# See https://github.com/att/ast/issues/1377 for why this test is commented out. +# TODO: enable this test when that issue is fixed. +puts "background job killed when shell exits" +#send "$ksh -l\r" +## We can't use `expect_prompt` here because the subshell will reset the prompt counter to one. +#expect -re "\r\nKSH PROMPT:1:" +#send "\$(type -p sleep) 17 &\r" +#expect -re "\r\nKSH PROMPT:2:" +#send "ps waux | grep '\[s\]leep 17'\r" +#expect -re "\r\nKSH PROMPT:3:" +#send "echo status $?\r" +#expect -re "\r\nKSH PROMPT:4:" +#send "set -o\r" +#expect -re "\r\nKSH PROMPT:5:" +#send "exit\r" +#expect_prompt +#send "ps waux | grep '\[s\]leep 17'\r" +#expect_prompt +#send "echo status $?\r" +#expect -re "\r\nstatus 1\r\n" { +# puts "background job killed when shell exits" +#} +#expect_prompt + +# ====== +# Put a job in the background and verify that disowning it causes the job to continue running when +# the shell terminates. +log_test_entry +send "$ksh -l\r" +# We can't use `expect_prompt` here because the subshell will reset the prompt counter to one. +expect -re "\r\nKSH PROMPT:1:" +send "\$(type -p sleep) 17 &\r" +expect -re "\r\nKSH PROMPT:2:" +send "disown %1\r" +expect -re "\r\nKSH PROMPT:3:" +send "ps waux | grep '\[s\]leep 17'\r" +expect -re "\r\nKSH PROMPT:4:" +send "echo status $?\r" +expect -re "\r\nKSH PROMPT:5:" +send "exit\r" +expect_prompt +send "ps waux | grep '\[s\]leep 17'\r" +expect_prompt +send "echo status $?\r" +expect -re "\r\nstatus 0\r\n" { + puts "disowned background job not killed when shell exits" +} +expect_prompt +# Kill the backgrounded, and orphaned, sleep. +send "ps waux | grep '\[s\]leep 17' | read user pid x; kill $pid\r" +expect_prompt + +# ====== +# An invalid `bg` job ID should be an error. +log_test_entry +send "bg %9\r" +expect -re ": bg: no such job\r\n" { + puts "bg invalid job ID fails" +} +expect_prompt + +# ====== +# An invalid `fg` job ID should be an error. +log_test_entry +send "fg %9\r" +expect -re ": fg: no such job\r\n" { + puts "fg invalid job ID fails" +} expect_prompt # ====== diff --git a/src/cmd/ksh93/tests/b_jobs.exp.out b/src/cmd/ksh93/tests/b_jobs.exp.out index 5aafe1d09846..8dce99da4ce7 100644 --- a/src/cmd/ksh93/tests/b_jobs.exp.out +++ b/src/cmd/ksh93/tests/b_jobs.exp.out @@ -1,3 +1,8 @@ +disown invalid job ID fails +background job killed when shell exits +disowned background job not killed when shell exits +bg invalid job ID fails +fg invalid job ID fails Ctrl-z stops sleep 60 processs jobs lists stopped sleep 60 process jobs -l lists pid of stopped sleep 60 process