| 1 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 2 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
import unittest |
| 18 |
|
| 19 |
from cylc.flow.cylc_subproc import procopen |
| 20 |
from unittest.mock import call |
| 21 |
from testfixtures import compare |
| 22 |
from testfixtures.popen import PIPE, MockPopen |
| 23 |
|
| 24 |
# Method could be a function |
| 25 |
# pylint: disable=no-self-use |
| 26 |
|
| 27 |
|
| 28 |
class TestSubprocessSafe(unittest.TestCase): |
| 29 |
"""Unit tests for the parameter procopen utility function""" |
| 30 |
|
| 31 |
def setUp(self): |
| 32 |
self.Popen = MockPopen() |
| 33 |
|
| 34 |
def test_sprocess_communicate_with_process(self): |
| 35 |
foo = ' foo' |
| 36 |
bar = ' bar' |
| 37 |
cmd = ["echo", "this is a command" + foo + bar] |
| 38 |
p = procopen(cmd, stdoutpipe=True) |
| 39 |
stdout, _ = p.communicate() |
| 40 |
compare(stdout, b"this is a command foo bar\n") |
| 41 |
|
| 42 |
def test_sprocess_communicate_with_input(self): |
| 43 |
command = "a command" |
| 44 |
Popen = MockPopen() |
| 45 |
Popen.set_command(command) |
| 46 |
# only static input used with simulated mockpopen |
| 47 |
# codacy mistakenly sees this as a call to popen |
| 48 |
process = Popen(command, stdout=PIPE, stderr=PIPE, shell=True) # nosec |
| 49 |
err, out = process.communicate('foo') |
| 50 |
compare([ |
| 51 |
# only static input used with simulated mockpopen |
| 52 |
# codacy mistakenly sees this as a call to popen |
| 53 |
call.Popen(command, shell=True, stderr=-1, stdout=-1), # nosec |
| 54 |
call.Popen_instance.communicate('foo'), |
| 55 |
], Popen.mock.method_calls) |
| 56 |
return err, out |
| 57 |
|
| 58 |
def test_sprocess_safe_read_from_stdout_and_stderr(self): |
| 59 |
command = "a command" |
| 60 |
Popen = MockPopen() |
| 61 |
# only static input used with simulated mockpopen |
| 62 |
# codacy mistakenly sees this as a call to popen |
| 63 |
Popen.set_command(command, stdout=b'foo', stderr=b'bar') |
| 64 |
process = Popen(command, stdout=PIPE, stderr=PIPE, shell=True) # nosec |
| 65 |
compare(process.stdout.read(), b'foo') |
| 66 |
compare(process.stderr.read(), b'bar') |
| 67 |
compare([ |
| 68 |
call.Popen(command, shell=True, stderr=PIPE, # nosec |
| 69 |
stdout=PIPE), |
| 70 |
], Popen.mock.method_calls) |
| 71 |
|
| 72 |
def test_sprocess_safe_write_to_stdin(self): |
| 73 |
command = "a command" |
| 74 |
Popen = MockPopen() |
| 75 |
Popen.set_command(command) |
| 76 |
# only static input used with simulated mockpopen |
| 77 |
# codacy mistakenly sees this as a call to popen |
| 78 |
process = Popen(command, stdin=PIPE, shell=True) # nosec |
| 79 |
process.stdin.write(command) |
| 80 |
process.stdin.close() |
| 81 |
compare([ |
| 82 |
# static input used with simulated mockpopen |
| 83 |
# codacy mistakenly sees this as a call to popen |
| 84 |
call.Popen(command, shell=True, stdin=PIPE), # nosec |
| 85 |
call.Popen_instance.stdin.write(command), |
| 86 |
call.Popen_instance.stdin.close(), |
| 87 |
], Popen.mock.method_calls) |
| 88 |
|
| 89 |
def test_sprocess_safe_wait_and_return_code(self): |
| 90 |
command = "a command" |
| 91 |
Popen = MockPopen() |
| 92 |
Popen.set_command(command, returncode=3) |
| 93 |
process = Popen(command) |
| 94 |
compare(process.returncode, None) |
| 95 |
compare(process.wait(), 3) |
| 96 |
compare(process.returncode, 3) |
| 97 |
compare([ |
| 98 |
call.Popen(command), |
| 99 |
call.Popen_instance.wait(), |
| 100 |
], Popen.mock.method_calls) |