summaryrefslogtreecommitdiff
path: root/cloudinit/tests/test_subp.py
blob: 448097d31b9027abf5e12d3af0972dc72f85ad8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# This file is part of cloud-init. See LICENSE file for license information.

"""Tests for cloudinit.subp utility functions"""

from cloudinit import subp
from cloudinit.tests.helpers import CiTestCase


class TestPrependBaseCommands(CiTestCase):

    with_logs = True

    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))

    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)

    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)

    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)

# vi: ts=4 expandtab