File: command_capabilities_test.py

package info (click to toggle)
kiwi 10.2.33-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,528 kB
  • sloc: python: 67,299; sh: 3,980; xml: 3,379; ansic: 391; makefile: 354
file content (136 lines) | stat: -rw-r--r-- 5,301 bytes parent folder | download
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import logging
from unittest.mock import (
    patch, call
)
from pytest import (
    raises, fixture
)
from collections import namedtuple

from kiwi.utils.command_capabilities import CommandCapabilities

from kiwi.exceptions import KiwiCommandCapabilitiesError


class TestCommandCapabilities:
    @fixture(autouse=True)
    def inject_fixtures(self, caplog):
        self._caplog = caplog

    @patch('kiwi.command.Command.run')
    def test_has_option_in_help(self, mock_run):
        command_type = namedtuple('command', ['output', 'error'])
        mock_run.return_value = command_type(
            output="Dummy line\n\t--some-flag\n\t--some-other-flag",
            error="Dummy line\n\t--error-flag\n\t--some-other-flag"
        )
        assert CommandCapabilities.has_option_in_help(
            'command', '--some-flag'
        ) is True
        assert CommandCapabilities.has_option_in_help(
            'command', '--error-flag'
        ) is True
        assert CommandCapabilities.has_option_in_help(
            'command', '--some-flag', help_flags=['subcommand', '-h']
        ) is True
        assert CommandCapabilities.has_option_in_help(
            'command', '--some-other-flag',
            help_flags=['subcommand', '-h'], root='root_dir'
        ) is True
        assert CommandCapabilities.has_option_in_help(
            'command', '--non-existing-flag', raise_on_error=False
        ) is False
        mock_run.assert_has_calls(
            [
                call(['command', '--help'], raise_on_error=False),
                call(['command', '--help'], raise_on_error=False),
                call(['command', 'subcommand', '-h'], raise_on_error=False),
                call(
                    ['chroot', 'root_dir', 'command', 'subcommand', '-h'],
                    raise_on_error=False
                ),
                call(['command', '--help'], raise_on_error=False)
            ]
        )

    @patch('kiwi.command.Command.run')
    def test_has_option_in_help_command_failure_warning(self, mock_run):
        mock_run.return_value.output = ''
        mock_run.return_value.error = ''
        with self._caplog.at_level(logging.WARNING):
            CommandCapabilities.has_option_in_help(
                'command_that_fails', '--non-existing-flag',
                raise_on_error=False
            )

    @patch('kiwi.command.Command.run')
    def test_has_option_in_help_command_failure_exception(self, mock_run):
        mock_run.return_value.output = ''
        mock_run.return_value.error = ''
        with raises(KiwiCommandCapabilitiesError):
            CommandCapabilities.has_option_in_help(
                'command_that_fails', '--non-existing-flag'
            )

    @patch('kiwi.command.Command.run')
    def test_check_version(self, mock_run):
        command_type = namedtuple('command', ['output'])
        mock_run.return_value = command_type(
            output="Dummy line\ncommand v1.2.3\n"
        )
        assert CommandCapabilities.check_version('command', (1, 2, 3))
        assert CommandCapabilities.check_version('command', (1, 1, 3))
        assert not CommandCapabilities.check_version('command', (1, 3))
        assert CommandCapabilities.check_version(
            'command', (1, 2, 3), version_flags=['-v']
        )
        assert CommandCapabilities.check_version(
            'command', (1, 2, 3), version_flags=['-v'], root='root_dir'
        )
        mock_run.assert_has_calls([
            call(['command', '--version']),
            call(['command', '--version']),
            call(['command', '--version']),
            call(['command', '-v']),
            call(['chroot', 'root_dir', 'command', '-v'])
        ])

    @patch('kiwi.command.Command.run')
    def test_check_version_complex_pattern(self, mock_run):
        command_type = namedtuple('command', ['output'])
        mock_run.return_value = command_type(
            output="grub2-mkconfig (GRUB2) 2.02\n"
        )
        assert CommandCapabilities.check_version('command', (2, 2)) is True
        assert CommandCapabilities.check_version('command', (2, 4)) is False

    @patch('kiwi.command.Command.run')
    def test_check_version_no_match(self, mock_run):
        command_type = namedtuple('command', ['output'])
        mock_run.return_value = command_type(
            output="Dummy line\ncommand someother stuff\n"
        )
        with raises(KiwiCommandCapabilitiesError):
            CommandCapabilities.check_version('command', (1, 2, 3))

    @patch('kiwi.command.Command.run')
    def test_check_version_failure_warning(self, mock_run):
        def side_effect():
            raise Exception("Something went wrong")

        mock_run.side_effect = side_effect
        with self._caplog.at_level(logging.WARNING):
            CommandCapabilities.check_version(
                'command_that_fails', (1, 2), raise_on_error=False
            )

    @patch('kiwi.command.Command.run')
    def test_check_version_failure_exception(self, mock_run):
        def side_effect():
            raise Exception("Something went wrong")

        mock_run.side_effect = side_effect
        with raises(KiwiCommandCapabilitiesError):
            CommandCapabilities.check_version(
                'command_that_fails', '--non-existing-flag'
            )