Passing a bash script error to the console in rails - ruby-on-rails

I have a shell script that runs some acceptance tests for an application I'm working on. The scripts runs the tests, checks if there were errors and then exits with either 0 (success) or 1 (failure).
I have a rake task that calls the shell script, and then gets the result. The problem I'm having is, how do I pass that result to the rails console so that when I echo $? it will be equal the value returned by the shell script?
My current code is as follows:
def acceptance_tests
system("./run_tests.sh");
error_code = $?.success? ? 0 : 1
result = error_code == 0 ? 'passed' : 'failed'
puts ("The acceptance tests have #{result}.")
SystemExit.new(error_code)
end
The tests pass / fail as expected when I run them, but after they are complete, I run echo $? and it's always equal to 0.
Any ideas about what I'm doing wrong?

SystemExit is an Exception, so raise it:
$ echo "raise SystemExit.new(5)" | ruby; echo $?
5

In the end, changing the SystemExit.new() to exit() worked for me.
def acceptance_tests
system("./run_tests.sh");
error_code = $?.success? ? 0 : 1
result = error_code == 0 ? 'passed' : 'failed'
puts ("The acceptance tests have #{result}.")
exit(error_code)
end

Related

In Jenkins job, behave tests stops after any failure

I have created a jenkins "freestyle" job, in which I am trying to run multiple BDD testing process. Following is the "commands" I have put in "Jenins/Build/execute shell" section:
cd ~/FEXT_BETA_BDD
rm -rf allure_reports allure-reports allure-results
pip install behave
pip install selenium
pip install -r features/requirements.txt
# execute features in plan section
behave -f allure_behave.formatter:AllureFormatter -f pretty -o ./allure-reports
./features/plan/*.feature
# execute features in blueprint section
behave -f allure_behave.formatter:AllureFormatter -f pretty -o ./allure-reports
./features/blueprint/*.feature
What I have found is in Jenkins, if there is any test case intermittent failure, such message is shown in the Console Output:
"
...
0 features passed, 1 failed, 0 skipped
0 scenarios passed, 1 failed, 0 skipped
3 steps passed, 1 failed, 1 skipped, 0 undefined
Took 2m48.770s
Build step 'Execute shell' marked build as failure
"
And the leftover test cases are skipped. But if I was to run the behave command on my local host directly, I don't get this type of behaviour. The failure will be detected and the remaining test cases continues till all are finished.
So How may I work around this issue in Jenkins ?
Thanks,
Jack
You may try the following syntax:
set +e
# execute features in plan section
behave -f allure_behave.formatter:AllureFormatter -f pretty -o ./allure-reports
./features/plan/*.feature || echo 'ALERT: Build failed while running the plan section'
# execute features in blueprint section
behave -f allure_behave.formatter:AllureFormatter -f pretty -o ./allure-reports
./features/blueprint/*.feature || echo 'ALERT: Build failed while running the blueprint section'
# Restoring original configuration
set -e
Note:
Goal of set -e is to cause the shell to abort any time an error occurs. If you will see your log output, you will notice sh -xe at the start of execution which confirms that Execute Shell in Jenkins uses -e option. So, to disable it, you can use +e instead. However, it's good to restore it once your purpose is fulfilled so that subsequent commands produce expected result.
Ref: https://superuser.com/questions/1113014/what-would-set-e-and-set-x-commands-do-in-the-context-of-a-shell-script
The ConsoleOutput from the SummaryReporter above indicates that you have only one feature with one scenario (that fails). Behave has no such thing that it stops when the first scenario fails.
An early abortion of the test run can only occur if critical things happen:
A failure/exception in the before_all() hook occurs
A critical exception is raised (SystemExit, KeyboardInterrupt) to end the test run
Your implementation tells behave to abort the test run (make sense on critical failures when all other tests will also fail; why waste the time)
BUT: If the test run is aborted early, all the features/scenarios that are not executed yet are reported as untested counts in the SummaryReporter.
...
0 features passed, 1 failed, 0 skipped, 2 untested
0 scenarios passed, 1 failed, 0 skipped, 3 untested
0 steps passed, 1 failed, 0 skipped, 0 undefined, 6 untested
HINT: Untested counts are normally hidden. They are only shown if the counter is not zero (greater than zero).
This is not the case in your description.
SEE ALSO:
behave: features/runner.abort_by_user.feature

How does Redis run lua script?

local a = {}
for i,v in ipairs(KEYS) do
a[i] = redis.call('hgetall',v)
end
return a
Above is my script
eval test.lua 3 user:1 user:2 user:3 0
(error) ERR Error compiling script (new function): user_script:2: '=' expected near 'end'
Above is my redis command and error
Can anyone tell why the error happened and show me how to write a correct command?
The [EVAL command] (https://redis.io/commands/eval) expects the first argument to be the actual script, not a filename. Fix that and it should work.

expect output only stdout of the command and nothing else

How to write expect script which executes command and prints just the command's output?
I've tried various things but none works, e.g.
#!/usr/bin/expect
log_user 0
spawn bash
send "echo 1\r"
log_user 1
expect "1"
log_user 0
send "exit\r"
expect eof
Gives in output:
echo 1
While I need just "1" . I hope somebody knows simple solution how to fix my example
Capturing the output from sent commands is a bit of a pain in expect.
Here's a more general case that does not rely on the log_user setting, it captures the output with a regular expression:
#!/usr/bin/expect
log_user 0
spawn bash
# set the prompt to a known value
send "PS1='>'\r"
expect -re {>$}
# send a command: we don't know what the output is going to be
send "echo \$RANDOM\r"
# capture the portion of the output that occurs just before the prompt
expect -re "\r\n(.*?)\r\n>$"
puts "output is: $expect_out(1,string)"
send "exit\r"
expect eof
A thought just occurred to me: if the command does not require any interaction, then expect is overkill: just use exec
set output [exec bash -c {echo $RANDOM}]
Ok, it looks following script does (at least similar to) what I need:
log_user 0
spawn bash
expect "#" {} "\\\$" {}
send -- "echo AA\r"
expect -- "echo AA\r" {}
log_user 1
expect -- "AA"
log_user 0
send -- "exit\r"
expect eof

Ruby infinite while true ends silently

I start a script simple.rb with ruby simple.rb > log.txt &. I want it to run infinitely. It runs for a while, but pidof ruby does not return anything. The script stops running, and there is no error code or exit msg in the log file. What happened? Do ruby loops end eventually? I can restart the ruby script when it ends from a bash endless loop, but I'm curious as to why this script ends, and how I can find out if it doesn't emit an exit code/msg.
def main_loop
puts "Doing stuff.."
end
while true
main_loop
sleep 5.seconds
end
The #seconds is unnecessary and is probably messing up your code, since #sleep takes a number (float or integer, I believe). See http://apidock.com/ruby/Kernel/sleep .
$stdout.sync = true
def main_loop
puts "Doing stuff.."
end
while true
main_loop
sleep 5
end

Capistrano & Bash: ignore command exit status

I'm using Capistrano run a remote task. My task looks like this:
task :my_task do
run "my_command"
end
My problem is that if my_command has an exit status != 0, then Capistrano considers it failed and exits. How can I make capistrano keep going when exit when the exit status is not 0? I've changed my_command to my_command;echo and it works but it feels like a hack.
The simplest way is to just append true to the end of your command.
task :my_task do
run "my_command"
end
Becomes
task :my_task do
run "my_command; true"
end
For Capistrano 3, you can (as suggested here) use the following:
execute "some_command.sh", raise_on_non_zero_exit: false
The +grep+ command exits non-zero based on what it finds. In the use case where you care about the output but don't mind if it's empty, you'll discard the exit state silently:
run %Q{bash -c 'grep #{escaped_grep_command_args} ; true' }
Normally, I think the first solution is just fine -- I'd make it document itself tho:
cmd = "my_command with_args escaped_correctly"
run %Q{bash -c '#{cmd} || echo "Failed: [#{cmd}] -- ignoring."'}
You'll need to patch the Capistrano code if you want it to do different things with the exit codes; it's hard-coded to raise an exception if the exit status is not zero.
Here's the relevant portion of lib/capistrano/command.rb. The line that starts with if (failed... is the important one. Basically it says if there are any nonzero return values, raise an error.
# Processes the command in parallel on all specified hosts. If the command
# fails (non-zero return code) on any of the hosts, this will raise a
# Capistrano::CommandError.
def process!
loop do
break unless process_iteration { #channels.any? { |ch| !ch[:closed] } }
end
logger.trace "command finished" if logger
if (failed = #channels.select { |ch| ch[:status] != 0 }).any?
commands = failed.inject({}) { |map, ch| (map[ch[:command]] ||= []) << ch[:server]; map }
message = commands.map { |command, list| "#{command.inspect} on #{list.join(',')}" }.join("; ")
error = CommandError.new("failed: #{message}")
error.hosts = commands.values.flatten
raise error
end
self
end
I find the easiest option to do this:
run "my_command || :"
Notice: : is the NOP command so the exit code will simply be ignored.
I just redirect STDERR and STDOUT to /dev/null, so your
run "my_command"
becomes
run "my_command > /dev/null 2> /dev/null"
this works for standard unix tools pretty well, where, say, cp or ln could fail, but you don't want to halt deployment on such a failure.
I not sure what version they added this code but I like handling this problem by using raise_on_non_zero_exit
namespace :invoke do
task :cleanup_workspace do
on release_roles(:app), in: :parallel do
execute 'sudo /etc/cron.daily/cleanup_workspace', raise_on_non_zero_exit: false
end
end
end
Here is where that feature is implemented in the gem.
https://github.com/capistrano/sshkit/blob/4cfddde6a643520986ed0f66f21d1357e0cd458b/lib/sshkit/command.rb#L94

Resources