def test_run_command_logs_commands_and_exit_codes_to_stderr(self): """All exit codes are logged to stderr.""" outfile = self.tmp_path('output.log', dir=self.tmp) cmd1 = 'echo "HI" >> %s' % outfile cmd2 = 'bogus command' cmd3 = 'echo "MOM" >> %s' % outfile commands = [cmd1, cmd2, cmd3] mock_path = 'cloudinit.config.cc_snap.sys.stderr' with mock.patch(mock_path, new_callable=StringIO) as m_stderr: with self.assertRaises(RuntimeError) as context_manager: run_commands(commands=commands) self.assertIsNotNone( re.search(r'bogus: (command )?not found', str(context_manager.exception)), msg='Expected bogus command not found') expected_stderr_log = '\n'.join([ 'Begin run command: {cmd}'.format(cmd=cmd1), 'End run command: exit(0)', 'Begin run command: {cmd}'.format(cmd=cmd2), 'ERROR: End run command: exit(127)', 'Begin run command: {cmd}'.format(cmd=cmd3), 'End run command: exit(0)\n']) self.assertEqual(expected_stderr_log, m_stderr.getvalue())
def test_run_command_logs_commands_and_exit_codes_to_stderr(self): """All exit codes are logged to stderr.""" outfile = self.tmp_path('output.log', dir=self.tmp) cmd1 = 'echo "HI" >> %s' % outfile cmd2 = 'bogus command' cmd3 = 'echo "MOM" >> %s' % outfile commands = [cmd1, cmd2, cmd3] mock_path = 'cloudinit.config.cc_snap.sys.stderr' with mock.patch(mock_path, new_callable=StringIO) as m_stderr: with self.assertRaises(RuntimeError) as context_manager: run_commands(commands=commands) self.assertIsNotNone( re.search(r'bogus: (command )?not found', str(context_manager.exception)), msg='Expected bogus command not found') expected_stderr_log = '\n'.join([ 'Begin run command: {cmd}'.format(cmd=cmd1), 'End run command: exit(0)', 'Begin run command: {cmd}'.format(cmd=cmd2), 'ERROR: End run command: exit(127)', 'Begin run command: {cmd}'.format(cmd=cmd3), 'End run command: exit(0)\n']) self.assertEqual(expected_stderr_log, m_stderr.getvalue())
def test_run_commands_on_non_list_or_dict(self): """When provided an invalid type, run_commands raises an error.""" with self.assertRaises(TypeError) as context_manager: run_commands(commands="I'm Not Valid") self.assertEqual( "commands parameter was not a list or dict: I'm Not Valid", str(context_manager.exception))
def test_run_commands_on_non_list_or_dict(self): """When provided an invalid type, run_commands raises an error.""" with self.assertRaises(TypeError) as context_manager: run_commands(commands="I'm Not Valid") self.assertEqual( "commands parameter was not a list or dict: I'm Not Valid", str(context_manager.exception))
def test_run_command_dict_sorted_as_command_script(self): """When commands are a dict, sort them and run.""" outfile = self.tmp_path('output.log', dir=self.tmp) cmd1 = 'echo "HI" >> %s' % outfile cmd2 = 'echo "MOM" >> %s' % outfile commands = {'02': cmd1, '01': cmd2} mock_path = 'cloudinit.config.cc_snap.sys.stderr' with mock.patch(mock_path, new_callable=StringIO): run_commands(commands=commands) expected_messages = ['DEBUG: Running user-provided snap commands'] for message in expected_messages: self.assertIn(message, self.logs.getvalue()) self.assertEqual('MOM\nHI\n', util.load_file(outfile))
def test_run_command_dict_sorted_as_command_script(self): """When commands are a dict, sort them and run.""" outfile = self.tmp_path('output.log', dir=self.tmp) cmd1 = 'echo "HI" >> %s' % outfile cmd2 = 'echo "MOM" >> %s' % outfile commands = {'02': cmd1, '01': cmd2} mock_path = 'cloudinit.config.cc_snap.sys.stderr' with mock.patch(mock_path, new_callable=StringIO): run_commands(commands=commands) expected_messages = [ 'DEBUG: Running user-provided snap commands'] for message in expected_messages: self.assertIn(message, self.logs.getvalue()) self.assertEqual('MOM\nHI\n', util.load_file(outfile))
def test_run_command_as_lists(self): """When commands are specified as a list, run them in order.""" outfile = self.tmp_path('output.log', dir=self.tmp) cmd1 = 'echo "HI" >> %s' % outfile cmd2 = 'echo "MOM" >> %s' % outfile commands = [cmd1, cmd2] mock_path = 'cloudinit.config.cc_snap.sys.stderr' with mock.patch(mock_path, new_callable=StringIO): run_commands(commands=commands) self.assertIn('DEBUG: Running user-provided snap commands', self.logs.getvalue()) self.assertEqual('HI\nMOM\n', util.load_file(outfile)) self.assertIn('WARNING: Non-snap commands in snap config:', self.logs.getvalue())
def test_run_command_as_lists(self): """When commands are specified as a list, run them in order.""" outfile = self.tmp_path('output.log', dir=self.tmp) cmd1 = 'echo "HI" >> %s' % outfile cmd2 = 'echo "MOM" >> %s' % outfile commands = [cmd1, cmd2] mock_path = 'cloudinit.config.cc_snap.sys.stderr' with mock.patch(mock_path, new_callable=StringIO): run_commands(commands=commands) self.assertIn( 'DEBUG: Running user-provided snap commands', self.logs.getvalue()) self.assertEqual('HI\nMOM\n', util.load_file(outfile)) self.assertIn( 'WARNING: Non-snap commands in snap config:', self.logs.getvalue())
def test_run_commands_on_empty_list(self, m_subp): """When provided with an empty list, run_commands does nothing.""" run_commands([]) self.assertEqual('', self.logs.getvalue()) m_subp.assert_not_called()
def test_run_commands_on_empty_list(self, m_subp): """When provided with an empty list, run_commands does nothing.""" run_commands([]) self.assertEqual('', self.logs.getvalue()) m_subp.assert_not_called()