Ejemplo n.º 1
0
 def test_prepend_base_command_errors_on_neither_string_nor_list(self):
     """Raise an error for each command which is not a string or list."""
     orig_commands = ['ls', 1, {'not': 'gonna work'}, ['basecmd', 'list']]
     with self.assertRaises(TypeError) as context_manager:
         subp.prepend_base_command(base_command='basecmd',
                                   commands=orig_commands)
     self.assertEqual(
         "Invalid basecmd config. These commands are not a string or"
         " list:\n1\n{'not': 'gonna work'}", str(context_manager.exception))
Ejemplo n.º 2
0
def run_commands(commands):
    """Run the provided commands provided in snap:commands configuration.

     Commands are run individually. Any errors are collected and reported
     after attempting all commands.

     @param commands: A list or dict containing commands to run. Keys of a
         dict will be used to order the commands provided as dict values.
     """
    if not commands:
        return
    LOG.debug('Running user-provided snap commands')
    if isinstance(commands, dict):
        # Sort commands based on dictionary key
        commands = [v for _, v in sorted(commands.items())]
    elif not isinstance(commands, list):
        raise TypeError(
            'commands parameter was not a list or dict: {commands}'.format(
                commands=commands))

    fixed_snap_commands = prepend_base_command('snap', commands)

    cmd_failures = []
    for command in fixed_snap_commands:
        shell = isinstance(command, str)
        try:
            subp.subp(command, shell=shell, status_cb=sys.stderr.write)
        except subp.ProcessExecutionError as e:
            cmd_failures.append(str(e))
    if cmd_failures:
        msg = 'Failures running snap commands:\n{cmd_failures}'.format(
            cmd_failures=cmd_failures)
        util.logexc(LOG, msg)
        raise RuntimeError(msg)
Ejemplo n.º 3
0
def run_commands(commands):
    """Run the provided commands provided in snap:commands configuration.

     Commands are run individually. Any errors are collected and reported
     after attempting all commands.

     @param commands: A list or dict containing commands to run. Keys of a
         dict will be used to order the commands provided as dict values.
     """
    if not commands:
        return
    LOG.debug('Running user-provided snap commands')
    if isinstance(commands, dict):
        # Sort commands based on dictionary key
        commands = [v for _, v in sorted(commands.items())]
    elif not isinstance(commands, list):
        raise TypeError(
            'commands parameter was not a list or dict: {commands}'.format(
                commands=commands))

    fixed_snap_commands = prepend_base_command('snap', commands)

    cmd_failures = []
    for command in fixed_snap_commands:
        shell = isinstance(command, str)
        try:
            util.subp(command, shell=shell, status_cb=sys.stderr.write)
        except util.ProcessExecutionError as e:
            cmd_failures.append(str(e))
    if cmd_failures:
        msg = 'Failures running snap commands:\n{cmd_failures}'.format(
            cmd_failures=cmd_failures)
        util.logexc(LOG, msg)
        raise RuntimeError(msg)
Ejemplo n.º 4
0
 def test_prepend_base_command_removes_first_item_when_none(self):
     """Remove the first element of a non-basecmd when it is None."""
     orig_commands = [[None, 'ls'], ['basecmd', 'list'],
                      [None, 'touch', '/blah'], ['basecmd', 'install', 'x']]
     expected = [['ls'], ['basecmd', 'list'], ['touch', '/blah'],
                 ['basecmd', 'install', 'x']]
     fixed_commands = subp.prepend_base_command(base_command='basecmd',
                                                commands=orig_commands)
     self.assertEqual('', self.logs.getvalue())
     self.assertEqual(expected, fixed_commands)
Ejemplo n.º 5
0
 def test_prepend_base_command_prepends_on_non_base_list_commands(self):
     """Prepend 'basecmd' for each non-basecmd command of type list."""
     orig_commands = [['ls'], ['basecmd', 'list'], ['basecmda', '/blah'],
                      ['basecmd', 'install', 'x']]
     expected = [['basecmd', 'ls'], ['basecmd', 'list'],
                 ['basecmd', 'basecmda', '/blah'],
                 ['basecmd', 'install', 'x']]
     fixed_commands = subp.prepend_base_command(base_command='basecmd',
                                                commands=orig_commands)
     self.assertEqual('', self.logs.getvalue())
     self.assertEqual(expected, fixed_commands)
Ejemplo n.º 6
0
 def test_prepend_base_command_warns_on_non_base_string_commands(self):
     """Warn on each non-base for commands of type string."""
     orig_commands = [
         'ls', 'basecmd list', 'touch /blah', 'basecmd install x'
     ]
     fixed_commands = subp.prepend_base_command(base_command='basecmd',
                                                commands=orig_commands)
     self.assertEqual(
         'WARNING: Non-basecmd commands in basecmd config:\n'
         'ls\ntouch /blah\n', self.logs.getvalue())
     self.assertEqual(orig_commands, fixed_commands)
Ejemplo n.º 7
0
 def test_prepend_base_command_warns_on_non_base_string_commands(self):
     """Warn on each non-base for commands of type string."""
     orig_commands = [
         "ls",
         "basecmd list",
         "touch /blah",
         "basecmd install x",
     ]
     fixed_commands = subp.prepend_base_command(
         base_command="basecmd", commands=orig_commands
     )
     self.assertEqual(
         "WARNING: Non-basecmd commands in basecmd config:\n"
         "ls\ntouch /blah\n",
         self.logs.getvalue(),
     )
     self.assertEqual(orig_commands, fixed_commands)
Ejemplo n.º 8
0
 def test_prepend_base_command_removes_first_item_when_none(self):
     """Remove the first element of a non-basecmd when it is None."""
     orig_commands = [
         [None, "ls"],
         ["basecmd", "list"],
         [None, "touch", "/blah"],
         ["basecmd", "install", "x"],
     ]
     expected = [
         ["ls"],
         ["basecmd", "list"],
         ["touch", "/blah"],
         ["basecmd", "install", "x"],
     ]
     fixed_commands = subp.prepend_base_command(
         base_command="basecmd", commands=orig_commands
     )
     self.assertEqual("", self.logs.getvalue())
     self.assertEqual(expected, fixed_commands)
Ejemplo n.º 9
0
 def test_prepend_base_command_prepends_on_non_base_list_commands(self):
     """Prepend 'basecmd' for each non-basecmd command of type list."""
     orig_commands = [
         ["ls"],
         ["basecmd", "list"],
         ["basecmda", "/blah"],
         ["basecmd", "install", "x"],
     ]
     expected = [
         ["basecmd", "ls"],
         ["basecmd", "list"],
         ["basecmd", "basecmda", "/blah"],
         ["basecmd", "install", "x"],
     ]
     fixed_commands = subp.prepend_base_command(
         base_command="basecmd", commands=orig_commands
     )
     self.assertEqual("", self.logs.getvalue())
     self.assertEqual(expected, fixed_commands)