Skip to content

Commit 727c1dc

Browse files
michaeltlombardim0dulardonoghuc
committed
(MODULES-9979) Fix empty return values in Linux task
Prior to this commit, querying for a nonexistent service using some service managers in the Linux task would return empty values. This commit also captures stderr in status checks to accurately convey that the service is unknown (or any other error). Co-authored-by: m0dular <[email protected]> Co-authored-by: donoghuc <[email protected]>
1 parent 8e555b9 commit 727c1dc

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

spec/acceptance/linux_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,19 @@
3838
end
3939
end
4040

41+
context 'when a service does not exist' do
42+
let(:non_existent_service) { 'foo' }
43+
44+
it 'reports useful information for status' do
45+
params = { 'action' => 'restart', 'name' => 'foo' }
46+
result = run_bolt_task('service::linux', params, expect_failures: true)
47+
expect(result['result']).to include('status' => 'failure')
48+
expect(result['result']['_error']).to include('msg' => %r{#{non_existent_service}})
49+
expect(result['result']['_error']).to include('kind' => 'bash-error')
50+
expect(result['result']['_error']).to include('details')
51+
end
52+
end
53+
4154
context 'when puppet-agent feature not available on target' do
4255
before(:all) do
4356
target = targeting_localhost? ? 'litmus_localhost' : ENV['TARGET_HOST']

tasks/linux.sh

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ esac
3030
case "$available_manager" in
3131
"systemctl")
3232
if [[ $action != "status" ]]; then
33-
"$service" "$action" "$name" || fail
33+
"$service" "$action" "$name" 2>"$_tmp" || fail
3434
fi
3535

3636
# `systemctl show` is the command to use in scripts. Use it to get the pid, load, and active states
@@ -40,7 +40,7 @@ case "$available_manager" in
4040
if [[ $action != "status" ]]; then
4141
success "{ \"status\": \"${cmd_out}\" }"
4242
else
43-
enabled_out="$("$service" "is-enabled" "$name")"
43+
enabled_out="$("$service" "is-enabled" "$name" 2>&1)"
4444
success "{ \"status\": \"${cmd_out}\", \"enabled\": \"${enabled_out}\" }"
4545
fi
4646
;;
@@ -51,26 +51,24 @@ case "$available_manager" in
5151
cmd=("$service" "$name" "$action")
5252
cmd_status=("$service" "$name" "status")
5353
# The chkconfig output has 'interesting' spacing/tabs, use word splitting to have single spaces
54-
word_split=($(chkconfig --list "$name"))
55-
enabled_out="${word_split[@]}"
54+
word_split=($(chkconfig --list "$name" 2>&1))
5655
else
57-
cmd=("$service" "$action" "$name")
58-
cmd_status=("$service" "status" "$name")
59-
enabled_out="$("$service" "show-config" "$name")"
56+
word_split=($("$service" "$name" "show-config" 2>&1))
6057
fi
58+
enabled_out="${word_split[@]}"
6159

6260
if [[ $action != "status" ]]; then
6361
# service and initctl may return non-zero if the service is already started or stopped
6462
# If so, check for either "already running" or "Unknown instance" in the output before failing
65-
"${cmd[@]}" >/dev/null || {
66-
grep -q "Job is already running" "$_tmp" || grep -q "Unknown instance:" "$_tmp" || fail
63+
"${cmd[@]}" &>"$_tmp" || {
64+
grep -q "already running" "$_tmp" || grep -q "Unknown instance:" "$_tmp" || grep -q "is not running" "$_tmp" || fail
6765
}
6866

69-
cmd_out="$("${cmd_status[@]}")"
67+
cmd_out="$("${cmd_status[@]}" 2>&1)"
7068
success "{ \"status\": \"${cmd_out}\" }"
7169
fi
7270

7371
# "status" is already pretty terse for these commands
74-
cmd_out="$("${cmd_status[@]}")"
72+
cmd_out="$("${cmd_status[@]}" 2>&1)"
7573
success "{ \"status\": \"${cmd_out}\", \"enabled\": \"${enabled_out}\" }"
7674
esac

0 commit comments

Comments
 (0)