def test_run_command(self): """ tests run_and_log runs given command, exits on error and writes to log """ cmd = ['ls', '-la'] msg = 'successfully ran command' runlog = CommandFileUtils(self.dist_version, self.log_file, self.log_level) args = (cmd, msg) runlog.run_command(*args) self.log('INFO: %s' % msg)
def test_run_command_exits_on_error(self): """ tests run_and_log runs given command, exits on error and writes to log """ cmd = ['ls', 'fjlskhgtioeb.bla'] msg = 'successfully ran command' runlog = CommandFileUtils(self.dist_version, self.log_file, self.log_level) args = (cmd, msg) try: runlog.run_command(*args) self.fail('command did not raise error') except SystemExit: self.log('ERROR: ls fjlskhgtioeb.bla exited with exit code 2')
def test_run_command_exits_on_error_and_does_not_log_when_log_error_is_false(self): """ tests run_and_log runs given command, exits on error and does not write to log when log_error is false """ cmd = ['ls', 'fjlskhgtioeb.bla'] msg = 'successfully ran command' runlog = CommandFileUtils(self.dist_version, self.log_file, self.log_level) args = (cmd, msg) try: runlog.run_command(*args, log_error=False) self.fail('command did not raise error') except SystemExit: with open(self.log_file) as log: log_content = log.readlines() self.assertFalse('ERROR: ls fjlskhgtioeb.bla exited with exit code 2' in log_content, log_content)