Debian Codegraph @

test/unit/command_test.py

raw
1 from unittest.mock import patch
2 from collections import namedtuple
3 from pytest import raises
4 import unittest.mock as mock
5 import os
6
7 import pytest
8
9 from kiwi.command import Command
10
11 from kiwi.exceptions import (
12 KiwiCommandError,
13 KiwiCommandNotFound
14 )
15
16
17 class TestCommand:
18 @patch('kiwi.path.Path.which')
19 @patch('subprocess.Popen')
20 def test_run_raises_error(self, mock_popen, mock_which):
21 mock_which.return_value = 'command'
22 mock_process = mock.Mock()
23 mock_process.communicate = mock.Mock(
24 return_value=[str.encode(''), str.encode('')]
25 )
26 mock_process.returncode = 1
27 mock_popen.return_value = mock_process
28 with raises(KiwiCommandError):
29 Command.run(['command', 'args'])
30
31 @patch('kiwi.path.Path.which')
32 @patch('subprocess.Popen')
33 def test_run_failure(self, mock_popen, mock_which):
34 mock_which.return_value = 'command'
35 mock_popen.side_effect = OSError('Run failure')
36 with raises(KiwiCommandError):
37 Command.run(['command', 'args'])
38
39 def test_run_invalid_environment(self):
40 with raises(KiwiCommandNotFound):
41 Command.run(['command', 'args'], custom_env={'PATH': '/root'})
42
43 @patch('kiwi.path.Path.which')
44 @patch('subprocess.Popen')
45 def test_run_does_not_raise_error(self, mock_popen, mock_which):
46 mock_which.return_value = 'command'
47 mock_process = mock.Mock()
48 mock_process.communicate = mock.Mock(
49 return_value=[str.encode('stdout'), str.encode('')]
50 )
51 mock_process.returncode = 1
52 mock_popen.return_value = mock_process
53 result = Command.run(['command', 'args'], os.environ, False)
54 assert result.error == ''
55 assert result.output == 'stdout'
56
57 @patch('kiwi.path.Path.which')
58 def test_run_does_not_raise_error_if_command_not_found(self, mock_which):
59 mock_which.return_value = None
60 result = Command.run(['command', 'args'], os.environ, raise_on_command_not_found=False)
61 assert result is None
62
63 @patch('kiwi.path.Path.which')
64 @patch('os.path.exists')
65 @patch('subprocess.Popen')
66 def test_run_does_not_call_which_for_abspaths(self, mock_popen, mock_exists, mock_which):
67 mock_exists.return_value = True
68 proc = mock.MagicMock()
69 proc.communicate.return_value = (str.encode("stdout"), str.encode("stderr"))
70 proc.returncode = 0
71 mock_popen.return_value = proc
72
73 result = Command.run(['/bin/command', 'args'])
74 mock_which.assert_not_called()
75 assert result is not None
76
77 @patch('kiwi.path.Path.which')
78 @patch('os.path.exists')
79 def test_run_fails_for_non_existent_abspath(self, mock_exists, mock_which):
80 mock_exists.return_value = False
81
82 with pytest.raises(KiwiCommandNotFound) as cmd_not_found_err:
83 Command.run(['/bin/command', 'args'])
84
85 mock_which.assert_not_called()
86 assert '"/bin/command" not found' in str(cmd_not_found_err.value)
87
88 @patch('os.access')
89 @patch('os.path.exists')
90 @patch('subprocess.Popen')
91 def test_run(self, mock_popen, mock_exists, mock_access):
92 mock_exists.return_value = True
93 command_run = namedtuple(
94 'command', ['output', 'error', 'returncode']
95 )
96 run_result = command_run(
97 output='stdout',
98 error='stderr',
99 returncode=0
100 )
101 mock_process = mock.Mock()
102 mock_process.communicate = mock.Mock(
103 return_value=[str.encode('stdout'), str.encode('stderr')]
104 )
105 mock_process.returncode = 0
106 mock_popen.return_value = mock_process
107 mock_access.return_value = True
108 assert Command.run(['command', 'args']) == run_result
109
110 def test_run_command_does_not_exist(self):
111 with raises(KiwiCommandNotFound):
112 Command.run(['does-not-exist'])
113
114 def test_call_command_does_not_exist(self):
115 with raises(KiwiCommandNotFound):
116 Command.call(['does-not-exist'], os.environ)
117
118 @pytest.mark.parametrize("available", (True, False))
119 @patch('kiwi.path.Path.which')
120 @patch('subprocess.Popen')
121 @patch('select.select')
122 def test_call(self, mock_select, mock_popen, mock_which, available: bool):
123 mock_which.return_value = 'command'
124 mock_select.return_value = [True, False, not available]
125 mock_process = mock.Mock()
126 mock_popen.return_value = mock_process
127 call = Command.call(['command', 'args'])
128 assert call.output_available() == available
129 assert call.error_available() == available
130 assert call.output == mock_process.stdout
131 assert call.error == mock_process.stderr
132 assert call.process == mock_process
133
134 @patch('kiwi.path.Path.which')
135 @patch('subprocess.Popen')
136 def test_call_failure(self, mock_popen, mock_which):
137 mock_which.return_value = 'command'
138 mock_popen.side_effect = KiwiCommandError('Call failure')
139 with raises(KiwiCommandError):
140 Command.call(['command', 'args'])