X-Git-Url: https://gerrit.akraino.org/r/gitweb?p=ta%2Frpmbuilder.git;a=blobdiff_plain;f=rpmbuilder%2Fexecutor_test.py;fp=rpmbuilder%2Fexecutor_test.py;h=d6596677e7123f2cac5506531b4c6404ed514903;hp=0000000000000000000000000000000000000000;hb=876631a959303430aafc0be7897b086ee9b921fe;hpb=d8468e0423a9af0d3fd5bf30d45ebe18ba8b1801 diff --git a/rpmbuilder/executor_test.py b/rpmbuilder/executor_test.py new file mode 100644 index 0000000..d659667 --- /dev/null +++ b/rpmbuilder/executor_test.py @@ -0,0 +1,48 @@ +# Copyright 2019 Nokia +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import re +import mock +import pytest + +from rpmbuilder.executor import Executor + + +@pytest.mark.parametrize('input_cmd, expected_output', [ + (['true'], ''), + (['echo', 'foo'], 'foo\n'), +]) +def test_run_cmd(input_cmd, expected_output): + assert Executor().run(input_cmd) == expected_output + + +@mock.patch('logging.Logger.debug') +@mock.patch('subprocess.Popen') +def test_stderr_is_logged(mock_popen, mock_log): + process_mock = mock.Mock() + process_mock.configure_mock(**{ + 'communicate.return_value': ('some ouput', 'some errput'), + 'returncode': 0, + }) + mock_popen.return_value = process_mock + Executor().run(['ls']) + assert re.match('.*exit status 0 but stderr not empty.*', mock_log.call_args[0][0]) + + +def test_run_cmd_fail(): + err_regexp = 'Command .* returned non-zero exit status 2: stdout="", ' \ + 'stderr="ls: .* No such file or directory' + with pytest.raises(Exception, + match=err_regexp): + Executor().run(['ls', 'bar'])