Log an error when lsb_release cannot be executed

Right now, if lsb_release is not on the system, bindep just dumps stack
and indicates "No such file or directory". This gives no indication of
what the problem is or what to do to resolve it. This change logs an
error message about lsb_release being missing in that case.

Change-Id: I501647770478fe154d66cdb2c6adff48973dd23c
This commit is contained in:
Dan Smith 2017-05-26 07:14:51 -07:00
parent 1a3b063990
commit 1743da873c
2 changed files with 14 additions and 3 deletions

View File

@ -226,9 +226,14 @@ class Depends(object):
return sorted(profiles)
def platform_profiles(self):
output = subprocess.check_output(
["lsb_release", "-cirs"],
stderr=subprocess.STDOUT).decode(getpreferredencoding(False))
try:
output = subprocess.check_output(
["lsb_release", "-cirs"],
stderr=subprocess.STDOUT).decode(getpreferredencoding(False))
except OSError:
log = logging.getLogger(__name__)
log.error('Unable to execute lsb_release. Is it installed?')
raise
lsbinfo = output.lower().split()
# NOTE(toabctl): distro can be more than one string (i.e. "SUSE LINUX")
codename = lsbinfo[len(lsbinfo) - 1:len(lsbinfo)][0]

View File

@ -207,6 +207,12 @@ class TestDepends(TestCase):
depends.platform_profiles(), Contains("platform:pacman"))
self.assertIsInstance(depends.platform, Pacman)
def test_missing_lsb_release(self):
with mock.patch('subprocess.check_output') as mock_co:
mock_co.side_effect = OSError
depends = Depends("")
self.assertRaises(OSError, depends.platform_profiles)
def test_finds_profiles(self):
depends = Depends(dedent("""\
foo