Commit 8543b70d authored by Julien Muchembled's avatar Julien Muchembled

qa: clean up / fix CrontabMixin

- more reliable crontab parsing
- execute command the same way as dcron
- fix poor escaping when execute command
parent f140191f
...@@ -194,8 +194,7 @@ class ManagedTemporaryDirectory(ManagedResource): ...@@ -194,8 +194,7 @@ class ManagedTemporaryDirectory(ManagedResource):
class CrontabMixin(object): class CrontabMixin(object):
computer_partition_root_path = None # type: str
logger = None # type: logging.Logger
def _getCrontabCommand(self, crontab_name): def _getCrontabCommand(self, crontab_name):
# type: (str) -> str # type: (str) -> str
"""Read a crontab and return the command that is executed. """Read a crontab and return the command that is executed.
...@@ -207,8 +206,9 @@ class CrontabMixin(object): ...@@ -207,8 +206,9 @@ class CrontabMixin(object):
'cron.d', 'cron.d',
crontab_name, crontab_name,
)) as f: )) as f:
crontab_spec = f.read() crontab_spec, = f.readlines()
return " ".join(crontab_spec.split()[5:]) self.assertNotEqual(crontab_spec[0], '@', crontab_spec)
return crontab_spec.split(None, 5)[-1]
def _executeCrontabAtDate(self, crontab_name, date): def _executeCrontabAtDate(self, crontab_name, date):
# type: (str, str) -> None # type: (str, str) -> None
...@@ -220,15 +220,16 @@ class CrontabMixin(object): ...@@ -220,15 +220,16 @@ class CrontabMixin(object):
crontab_command = self._getCrontabCommand(crontab_name) crontab_command = self._getCrontabCommand(crontab_name)
try: try:
crontab_output = subprocess.check_output( crontab_output = subprocess.check_output(
"faketime {date} bash -o pipefail -e -c '{crontab_command}'".format(**locals()), ("faketime", date, '/bin/sh', '-c', crontab_command),
shell=True,
stderr=subprocess.STDOUT, stderr=subprocess.STDOUT,
) )
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
self.logger.debug('error executing crontab %s output: %s', crontab_command, e.output) self.logger.debug('error executing crontab %s output: %s',
crontab_command, e.output)
raise raise
else: else:
self.logger.debug("crontab %s output: %s", crontab_command, crontab_output) self.logger.debug("crontab %s output: %s",
crontab_command, crontab_output)
class ImageComparisonTestCase(unittest.TestCase): class ImageComparisonTestCase(unittest.TestCase):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment