bash_test_tools icon indicating copy to clipboard operation
bash_test_tools copied to clipboard

output of commands run with sudo

Open Cronist opened this issue 1 year ago • 3 comments

hi to all and thank you so much for the application. it is very useful.

my case is, the run command includes a "sudo", the exit code for strace is always "1" but the command is successful. when i try to use "assert_success" it always fails because of output is not "0"

how can I manage this case.

thank u in advance and best regards.

Cronist avatar Aug 18 '23 16:08 Cronist

Hi, been quite a while since I wrote this thing, glad that it is occasionally useful to people. First of all, be careful with running anything as sudo / super user, but I guess you have a valid case for doing that. I see I did not mention in the docs at the time I wrote this that perhaps it's always a good idea to do tests in a change root environment. https://wiki.archlinux.org/title/chroot or nowadays run the thing in a docker container or a VM.

But to answer your question, yes, if you expect a non 0 status code then "assert_success" is not what you want. assert_success is really short for 3x more granular checks combined:

  • assert_terminated_normally (no crash)
  • assert_exit_success (checks if exit code in shell $? is 0)
  • assert_no_error (no text printed to stderr output)

See breakdown in docs: https://thorsteinssonh.github.io/bash_test_tools/#review

So you could replace "assert_success" with just assert_terminated_normally followed by assert_no_error, skipping checking exit code success status.

That way you should catch any program crashes and any errors printed to stderr output, but no check on program exit status code.

The docs are not very complete I see :( But lets say you want to assert that the exit code is something else than 0 as in your case, then we can look at what's inside the source file to find test methods that we need, see here: https://github.com/thorsteinssonh/bash_test_tools/blob/master/bash_test_tools#L207

So, in this case you need assert_equal "$returnval" 123 "exit status is 123" if you want to check exit code 123. This will assert that exit code is equal to 123, and print a custom test result message "exit status is 123" OK or Fail.

thorsteinssonh avatar Aug 19 '23 04:08 thorsteinssonh

btw, this is not the most popular framework for testing in a bash shell :) -- kinda useful for simple checks maybe. You could write shell execution tests in python with https://docs.python.org/3/library/subprocess.html for example. I read you get stdout, stderr and status code there to check.

thorsteinssonh avatar Aug 19 '23 04:08 thorsteinssonh

thank you so much for your rapid and valid response. the thing that I like about your application is reporting and the output view as a table and so understandable. I am not a programmer but a shell scripter. the thing that I am trying to do is create a shell script (one executable file) that checks for some basic requirements like FW permissions given already or not by using netcat commands, and so many others, but also make the environment ready for the next step for installation and ensuring about the required applications have been installed successfully.

so I needed something like that;

function test_installing_necessary_packages { run "sudo apt-get install -y strace ldap-utils" assert_success }

and also when I use netcat, the script is not able to catch the output of the command but it catches error. like;

this works;

function test_mtalk4_google_com_443 { run "netcat -w 3 -v mtalk4.google.com 443" assert_error_contains "succeeded" }

but this not working;

function test_mtalk4_google_com_443 { run "netcat -w 3 -v mtalk4.google.com 443" assert_output_contains "succeeded" }

as u mentioned when I change assert_success to assert_terminated_normally and followed by assert_no_error, at that time I am not able to catch error of the command.

the existing situation;

function test_ps_aux { run "sudo ps a" assert_success }

TEST PS AUX Running: sudo ps a ExecTime: 0.016 seconds Assert: process terminated normally OK Assert: 'exit status' equal to 0 FAIL Assert: 'stderror' is empty FAIL

Status - FAIL

if I change; function test_ps_aux { run "sudo ps a" assert_no_error assert_terminated_normally }

TEST PS AUX Running: sudo ps a ExecTime: 0.024 seconds Assert: 'stderror' is empty FAIL Assert: process terminated normally OK

Status - FAIL

if I change it to:

if I change; function test_ps_aux { run "sudo psss a" < command has been changed assert_no_error assert_terminated_normally }


TEST PS AUX Running: sudo pss a ExecTime: 0.022 seconds Assert: 'stderror' is empty FAIL Assert: process terminated normally OK

Status - FAIL

thank you in advance.

Cronist avatar Aug 19 '23 06:08 Cronist