File: cli_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 (174 lines) | stat: -rw-r--r-- 5,215 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import sys
import logging
from unittest.mock import patch
from pytest import (
    raises, fixture
)

from .test_helper import argv_kiwi_tests

from kiwi.cli import Cli
from kiwi.defaults import Defaults

from kiwi.exceptions import (
    KiwiLoadCommandUndefined,
    KiwiCommandNotLoaded,
    KiwiUnknownServiceName
)


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

    def setup(self):
        self.expected_global_args = {
            'help': False,
            '--type': None,
            'image': False,
            'system': True,
            '-h': False,
            '--logfile': None,
            '--logsocket': None,
            '--loglevel': None,
            '--color-output': False,
            '--version': False,
            '--debug': False,
            '--debug-run-scripts-in-screen': False,
            'result': False,
            '--profile': [],
            '--setenv': [],
            '--shared-cache-dir': '/var/cache/kiwi',
            '--temp-dir': '/var/tmp',
            '--target-arch': None,
            '--help': False,
            '--config': 'config-file',
            '--kiwi-file': None
        }
        self.command_args = {
            '--add-repo': [],
            '--add-repo-credentials': [],
            '--set-type-attr': [],
            '--set-release-version': None,
            '--allow-existing-root': False,
            '--description': 'description',
            '--help': False,
            '--ignore-repos': False,
            '--ignore-repos-used-for-build': False,
            '--clear-cache': False,
            '--root': 'directory',
            '--set-repo': None,
            '--set-repo-credentials': None,
            '--add-package': [],
            '--add-bootstrap-package': [],
            '--delete-package': [],
            '--set-container-derived-from': None,
            '--set-container-tag': None,
            '--add-container-label': [],
            '--signing-key': [],
            '-h': False,
            'help': False,
            'prepare': True,
            'system': True
        }
        self.cli = Cli()
        self.loaded_command = self.cli.load_command()

    def setup_method(self, cls):
        self.setup()

    def teardown(self):
        sys.argv = argv_kiwi_tests

    def teardown_method(self, cls):
        self.teardown()

    @patch('kiwi.cli.Help.show')
    def test_show_and_exit_on_help_request(self, help_show):
        self.cli.all_args['help'] = True
        with raises(SystemExit):
            self.cli.show_and_exit_on_help_request()
        help_show.assert_called_once_with('kiwi')

    def test_get_servicename_system(self):
        cli = Cli()
        assert cli.get_servicename() == 'system'

    def test_warning_on_use_of_legacy_disk_type(self):
        sys.argv = [
            sys.argv[0],
            '--type', 'vmx', 'system', 'build',
            '--description', 'description',
            '--target-dir', 'directory'
        ]
        cli = Cli()
        with self._caplog.at_level(logging.WARNING):
            cli.get_global_args()
            assert 'vmx type is now a subset of oem, --type set to oem' in \
                self._caplog.text

    def test_set_target_arch(self):
        sys.argv = [
            sys.argv[0],
            '--target-arch', 'x86_64', 'system', 'build',
            '--description', 'description',
            '--target-dir', 'directory'
        ]
        cli = Cli()
        cli.get_global_args()
        assert Defaults.get_platform_name() == 'x86_64'

    def test_get_servicename_image(self):
        sys.argv = [
            sys.argv[0],
            'image', 'resize',
            '--target-dir', 'directory',
            '--size', '20g'
        ]
        cli = Cli()
        assert cli.get_servicename() == 'image'

    def test_get_servicename_result(self):
        sys.argv = [
            sys.argv[0],
            'result', 'list',
            '--target-dir', 'directory'
        ]
        cli = Cli()
        assert cli.get_servicename() == 'result'

    def test_get_command(self):
        assert self.cli.get_command() == 'prepare'

    def test_get_command_args(self):
        assert self.cli.get_command_args() == self.command_args

    def test_load_command(self):
        assert self.cli.load_command() == self.loaded_command

    def test_load_command_unknown(self):
        self.cli.loaded = False
        self.cli.all_args['<command>'] = 'foo'
        with raises(KiwiCommandNotLoaded):
            self.cli.load_command()

    def test_load_command_undefined(self):
        self.cli.loaded = False
        self.cli.all_args['<command>'] = None
        with raises(KiwiLoadCommandUndefined):
            self.cli.load_command()

    def test_get_command_args_not_loaded(self):
        sys.argv = [
            sys.argv[0], 'system', 'command-not-implemented'
        ]
        cli = Cli()
        with raises(KiwiCommandNotLoaded):
            cli.get_command_args()

    def test_get_servicename_unknown(self):
        self.cli.all_args['system'] = False
        self.cli.all_args['foo'] = False
        with raises(KiwiUnknownServiceName):
            self.cli.get_servicename()