Pin pip to 20.3.3 and disable tmpfs in DIB
[ta/build-tools.git] / tools / 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 pytest
16 import mock
17
18 from tools.executor import Executor, ExecutionError
19 from tools.executor import Result
20
21
22 @mock.patch.object(Executor, '_execute')
23 def test_success(mocky):
24     mocky.side_effect = [Result(0, 'fake-stdout', '')]
25     Executor().run('test-cmd')
26     mocky.assert_called_once_with('test-cmd')
27
28
29 @mock.patch.object(Executor, '_execute')
30 def test_fail_returncode(mocky):
31     mocky.side_effect = [Result(1, 'fake-stdout', '')]
32     with pytest.raises(ExecutionError, match=r'execution status NOT zero'):
33         Executor().run('test-cmd')
34     mocky.assert_called_once_with('test-cmd')
35
36
37 @mock.patch.object(Executor, '_execute')
38 def test_fail_stderr(mocky):
39     mocky.side_effect = [Result(0, 'fake-stdout', 'fake-stderr')]
40     with pytest.raises(ExecutionError, match=r'stderr not empty'):
41         Executor().run('test-cmd')
42     mocky.assert_called_once_with('test-cmd')
43
44
45 @mock.patch.object(Executor, '_execute')
46 def test_success_ignore_returncode(mocky):
47     mocky.side_effect = [Result(1, 'fake-stdout', '')]
48     Executor().run('test-cmd', raise_on_error=False)
49     mocky.assert_called_once_with('test-cmd')
50
51
52 @mock.patch.object(Executor, '_execute')
53 def test_success_ignore_stderr(mocky):
54     mocky.side_effect = [Result(0, 'fake-stdout', 'fake-stderr')]
55     Executor().run('test-cmd', raise_on_stderr=False)
56     mocky.assert_called_once_with('test-cmd')
57
58
59 @mock.patch.object(Executor, '_execute')
60 def test_no_retry_on_success(mocky):
61     mocky.side_effect = [Result(0, 'fake-stdout', '')]
62     Executor().run('test-cmd', retries=1)
63     mocky.assert_called_once_with('test-cmd')
64
65
66 @mock.patch.object(Executor, '_execute')
67 def test_retry_on_fail(mocky):
68     mocky.side_effect = [Result(1, 'fake-stdout', ''),
69                          Result(0, 'fake-stdout', '')]
70     Executor().run('test-cmd', retries=1)
71     expected = [mock.call('test-cmd'),
72                 mock.call('test-cmd')]
73     assert mocky.mock_calls == expected
74
75
76 @mock.patch.object(Executor, '_execute')
77 def test_error_on_retry_exceeded(mocky):
78     mocky.side_effect = [Result(1, 'fake-stdout', ''),
79                          Result(1, 'fake-stdout', '')]
80     with pytest.raises(ExecutionError):
81         Executor().run('test-cmd', retries=1)