Initial commit
[ta/rpmbuilder.git] / rpmbuilder / executor_test.py
1 # Copyright 2019 Nokia
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import re
16 import mock
17 import pytest
18
19 from rpmbuilder.executor import Executor
20
21
22 @pytest.mark.parametrize('input_cmd, expected_output', [
23     (['true'], ''),
24     (['echo', 'foo'], 'foo\n'),
25 ])
26 def test_run_cmd(input_cmd, expected_output):
27     assert Executor().run(input_cmd) == expected_output
28
29
30 @mock.patch('logging.Logger.debug')
31 @mock.patch('subprocess.Popen')
32 def test_stderr_is_logged(mock_popen, mock_log):
33     process_mock = mock.Mock()
34     process_mock.configure_mock(**{
35         'communicate.return_value': ('some ouput', 'some errput'),
36         'returncode': 0,
37     })
38     mock_popen.return_value = process_mock
39     Executor().run(['ls'])
40     assert re.match('.*exit status 0 but stderr not empty.*', mock_log.call_args[0][0])
41
42
43 def test_run_cmd_fail():
44     err_regexp = 'Command .* returned non-zero exit status 2: stdout="", ' \
45                  'stderr="ls: .* No such file or directory'
46     with pytest.raises(Exception,
47                        match=err_regexp):
48         Executor().run(['ls', 'bar'])