From: Moritz Röhrich Date: Mon, 21 Mar 2022 16:32:25 +0000 (+0100) Subject: cephadm: avoid crashing on expected non-zero exit X-Git-Tag: v16.2.11~578^2~7 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=94bdc00f83025e6bc00901d2af9c262c807a8ab8;p=ceph.git cephadm: avoid crashing on expected non-zero exit - Avoid crashing when a call out to an external program expectedly does not return exit status zero. There are programs that communicate other information than error/no error through exit status. E.g. `systemctl status` will return different exit codes depending on the actual status of the units in question. In cases where this is expected crashing with a RuntimeError exception is inappropriate and should be avoided. Fixes: https://tracker.ceph.com/issues/55117 Signed-off-by: Moritz Röhrich (cherry picked from commit a02be6f22fa18094cd8758700ab74581b6ce1701) --- diff --git a/qa/workunits/cephadm/test_cephadm.sh b/qa/workunits/cephadm/test_cephadm.sh index 10ccd2458108..df463cb50bbb 100755 --- a/qa/workunits/cephadm/test_cephadm.sh +++ b/qa/workunits/cephadm/test_cephadm.sh @@ -77,6 +77,22 @@ function expect_false() if eval "$@"; then return 1; else return 0; fi } +# expect_return_code $expected_code $command ... +function expect_return_code() +{ + set -x + local expected_code="$1" + shift + local command="$@" + + set +e + eval "$command" + local return_code="$?" + set -e + + if [ ! "$return_code" -eq "$expected_code" ]; then return 1; else return 0; fi +} + function is_available() { local name="$1" @@ -386,6 +402,10 @@ $CEPHADM unit --fsid $FSID --name mon.a -- disable expect_false $CEPHADM unit --fsid $FSID --name mon.a -- is-enabled $CEPHADM unit --fsid $FSID --name mon.a -- enable $CEPHADM unit --fsid $FSID --name mon.a -- is-enabled +$CEPHADM unit --fsid $FSID --name mon.a -- status +$CEPHADM unit --fsid $FSID --name mon.a -- stop +expect_return_code 3 $CEPHADM unit --fsid $FSID --name mon.a -- status +$CEPHADM unit --fsid $FSID --name mon.a -- start ## shell $CEPHADM shell --fsid $FSID -- true diff --git a/src/cephadm/cephadm b/src/cephadm/cephadm index 60f1c5343608..3088d3e3e6c0 100755 --- a/src/cephadm/cephadm +++ b/src/cephadm/cephadm @@ -5178,19 +5178,19 @@ def command_ceph_volume(ctx): @infer_fsid def command_unit(ctx): - # type: (CephadmContext) -> None + # type: (CephadmContext) -> int if not ctx.fsid: raise Error('must pass --fsid to specify cluster') unit_name = get_unit_name_by_daemon_name(ctx, ctx.fsid, ctx.name) - call_throws(ctx, [ - 'systemctl', - ctx.command, - unit_name], + _, _, code = call( + ctx, + ['systemctl', ctx.command, unit_name], verbosity=CallVerbosity.VERBOSE, desc='' ) + return code ##################################