def test_write_to_stdin(self): # setup Popen = MockPopen() Popen.set_command('a command') # usage process = Popen('a command', stdin=PIPE, shell=True) process.stdin.write('some text') # test call list compare(Popen.mock.method_calls, expected=[ call.Popen('a command', shell=True, stdin=PIPE), call.Popen_instance.stdin.write('some text'), ]) compare(Popen.all_calls, expected=[ call.Popen('a command', shell=True, stdin=PIPE), call.Popen('a command', shell=True, stdin=PIPE).stdin.write('some text'), ]) compare(process.mock.method_calls, expected=[ call.stdin.write('some text'), ]) compare(process.calls, expected=[ call.stdin.write('some text'), ]) repr(call.stdin.write('some text'))
def test_invalid_method_or_attr(self): Popen = MockPopen() Popen.set_command('command') process = Popen('command') with ShouldRaise( AttributeError("Mock object has no attribute 'foo'")): process.foo()
def test_job(): Popen = MockPopen() Popen.set_command('top', stdout=b'o', stderr=b'e', returncode=1, pid=1000) process = Popen('top', stdout=b'o', stderr=b'e', shell=True) process.wait() execute.kill_job(process) assert process.returncode == 1
def test_simultaneous_processes(self): Popen = MockPopen() Popen.set_command('a command', b'a', returncode=1) Popen.set_command('b command', b'b', returncode=2) process_a = Popen('a command', stdout=PIPE, stderr=PIPE, shell=True) process_b = Popen(['b', 'command'], stdout=PIPE, stderr=PIPE, shell=True) compare(process_a.wait(), expected=1) compare(process_b.wait(), expected=2) a_call = call.Popen('a command', stdout=PIPE, stderr=PIPE, shell=True) b_call = call.Popen(['b', 'command'], stdout=PIPE, stderr=PIPE, shell=True) compare(Popen.all_calls, expected=[ a_call, b_call, a_call.wait(), b_call.wait(), ]) compare(process_a.mock.method_calls, expected=[call.wait()]) compare(process_b.mock.method_calls, expected=[call.wait()])
def test_invalid_wait_call(self): Popen = MockPopen() Popen.set_command('bar') process = Popen('bar') with ShouldRaise( TypeError("wait() got an unexpected keyword argument 'foo'")): process.wait(foo='bar')
class TestCommandTaskWithMockPopen(MockLoggerMixin, unittest.TestCase): """ Run command tasks with a mocked popen """ def setUp(self): self.global_config = BaseGlobalConfig() self.project_config = BaseProjectConfig( self.global_config, config={"noyaml": True} ) self.task_config = TaskConfig() self._task_log_handler.reset() self.task_log = self._task_log_handler.messages self.Popen = MockPopen() self.r = Replacer() self.r.replace("cumulusci.tasks.command.subprocess.Popen", self.Popen) self.addCleanup(self.r.restore) def test_functional_mock_command(self): """ Functional test that runs a command with mocked popen results and checks the log. """ self.task_config.config["options"] = {"command": "ls -la"} self.Popen.set_command("ls -la", stdout=b"testing testing 123", stderr=b"e") task = Command(self.project_config, self.task_config) task() self.assertTrue(any("testing testing" in s for s in self.task_log["info"]))
class TestGitPopenMockupMixin: def setup_git_popen(self): # repository mockup (in a temporary place) self.repository = Repo.init(self.tempdir.name) # setup git command mockup self.Popen = MockPopen() self.Popen.mock.Popen_instance.stdin = None self.Popen.mock.Popen_instance.wait = lambda *a, **k: self.Popen.wait() self.Popen.mock.Popen_instance.__enter__ = lambda self: self self.Popen.mock.Popen_instance.__exit__ = lambda self, *a, **k: None def set_mock_popen_commands(self, cmd_list): for cmd, out, err, rc in cmd_list: self.Popen.set_command(cmd, out, err, returncode=rc) def mockup_git(self, namespace, repository, url=None): # disable refspec check from git import remote remote.Remote._assert_refspec = lambda self: None # write FETCH_HEAD ref with open(os.path.join(self.repository.git_dir, 'FETCH_HEAD'), 'w') as f: url = url or "{}:{}/{}".format(self.service.fqdn, namespace, repository) f.write("749656b8b3b282d11a4221bb84e48291ca23ecc6" \ " branch 'master' of {}".format(url)) return Replace('git.cmd.Popen', self.Popen)
class TestGitPopenMockupMixin: def setup_git_popen(self): # repository mockup (in a temporary place) self.repository = Repo.init(self.tempdir.name) # setup git command mockup self.Popen = MockPopen() def FixPopen(*a, **k): if 'start_new_session' in k: del k['start_new_session'] return self.Popen.Popen(*a, **k) self.Popen.mock.Popen.side_effect = FixPopen self.Popen.mock.Popen_instance.stdin = None self.Popen.mock.Popen_instance.wait = lambda *a, **k: self.Popen.wait() self.Popen.mock.Popen_instance.__enter__ = lambda self: self self.Popen.mock.Popen_instance.__exit__ = lambda self, *a, **k: None def set_mock_popen_commands(self, cmd_list): for cmd, out, err, rc in cmd_list: self.Popen.set_command(cmd, out, err, returncode=rc) def mockup_git(self, namespace, repository, url=None): # disable refspec check from git import remote remote.Remote._assert_refspec = lambda self: None # write FETCH_HEAD ref with open(os.path.join(self.repository.git_dir, 'FETCH_HEAD'), 'w') as f: url = url or "{}:{}/{}".format(self.service.fqdn, namespace, repository) f.write("749656b8b3b282d11a4221bb84e48291ca23ecc6" \ " branch 'master' of {}".format(url)) return Replace('git.cmd.Popen', self.Popen)
def test_pass_executable(self): Popen = MockPopen() Popen.set_command('a command', b'a', returncode=1) Popen('a command', executable='/foo/bar') compare(Popen.all_calls, expected=[ call.Popen('a command', executable='/foo/bar') ])
def test_read_from_stderr(self): Popen = MockPopen() Popen.set_command('a command', stderr='foo') process = Popen('a command', stdout=PIPE, stderr=PIPE, shell=True) self.assertTrue(isinstance(process.stdout.fileno(), int)) compare(process.stderr.read(), 'foo') compare([call.Popen('a command', shell=True, stderr=-1, stdout=-1)], Popen.mock.method_calls)
def test_invalid_terminate(self): Popen = MockPopen() Popen.set_command('bar') process = Popen('bar') with ShouldRaise(TypeError( "terminate() got an unexpected keyword argument 'foo'" )): process.terminate(foo='bar')
def test_read_from_stdout_and_stderr_text_mode(self): Popen = MockPopen() Popen.set_command('a command', stdout=b'foo', stderr=b'bar') # usage process = Popen('a command', stdout=PIPE, stderr=PIPE, text=True) actual = process.stdout.read(), process.stderr.read() # check compare(actual, expected=(u'foo', u'bar'))
def test_communicate_text_mode(self): Popen = MockPopen() Popen.set_command('a command', stdout=b'foo', stderr=b'bar') # usage process = Popen('a command', stdout=PIPE, stderr=PIPE, text=True) actual = process.communicate() # check compare(actual, expected=(u'foo', u'bar'))
def test_local_agent_heartbeat( monkeypatch, cloud_api, returncode, show_flow_logs, logs ): popen = MockPopen() # expect a process to be called with the following command (with specified behavior) popen.set_command( "prefect execute flow-run", stdout=b"awesome output!", stderr=b"blerg, eRroR!", returncode=returncode, poll_count=2, ) monkeypatch.setattr("prefect.agent.local.agent.Popen", popen) agent = LocalAgent(import_paths=["paths"], show_flow_logs=show_flow_logs) agent.deploy_flow( flow_run=GraphQLResult( { "flow": GraphQLResult( { "storage": Local(directory="test").serialize(), "id": "foo", "core_version": "0.13.0", } ), "id": "id", } ) ) process = list(agent.processes)[0] process_call = process.root_call with LogCapture() as logcap: agent.heartbeat() agent.heartbeat() agent.heartbeat() # ensure the expected logs exist (or the absense of logs) if logs: logcap.check(*logs) else: logcap.check() # ensure the process was opened and was polled compare( popen.all_calls, expected=[ process_call, process_call.poll(), process_call.poll(), process_call.poll(), ], ) # the heartbeat should stop tracking upon exit compare(process.returncode, returncode) assert len(agent.processes) == 0
def test_command_is_sequence(self): Popen = MockPopen() Popen.set_command('a command') process = Popen(['a', 'command'], stdout=PIPE, stderr=PIPE) compare(process.wait(), 0) compare([ call.Popen(['a', 'command'], stderr=-1, stdout=-1), call.Popen_instance.wait() ], Popen.mock.method_calls)
def test_read_from_stdout_and_stderr(self): Popen = MockPopen() Popen.set_command('a command', stdout='foo', stderr='bar') process = Popen('a command', stdout=PIPE, stderr=PIPE, shell=True) compare(process.stdout.read(), 'foo') compare(process.stderr.read(), 'bar') compare( [call.Popen('a command', shell=True, stderr=PIPE, stdout=PIPE)], Popen.mock.method_calls)
def test_wait_and_return_code(self): Popen = MockPopen() Popen.set_command('a command', returncode=3) process = Popen('a command') compare(process.returncode, None) compare(process.wait(), 3) compare(process.returncode, 3) compare([call.Popen('a command'), call.Popen_instance.wait()], Popen.mock.method_calls)
def test_read_from_stdout_with_stderr_redirected_check_stdout_contents(self): # setup Popen = MockPopen() Popen.set_command('a command', stdout=b'foo', stderr=b'bar') # usage process = Popen('a command', stdout=PIPE, stderr=STDOUT, shell=True) # test stdout contents compare(b'foobar', process.stdout.read()) compare(process.stderr, None)
def test_read_from_stdout_with_stderr_redirected_check_stdout_stderr_interleaved(self): # setup Popen = MockPopen() Popen.set_command('a command', stdout=b'o1\no2\no3\no4\n', stderr=b'e1\ne2\n') # usage process = Popen('a command', stdout=PIPE, stderr=STDOUT, shell=True) self.assertTrue(isinstance(process.stdout.fileno(), int)) # test stdout contents compare(b'o1\ne1\no2\ne2\no3\no4\n', process.stdout.read())
def test_communicate_with_input(self): Popen = MockPopen() Popen.set_command('a command') process = Popen('a command', stdout=PIPE, stderr=PIPE, shell=True) out, err = process.communicate('foo') compare([ call.Popen('a command', shell=True, stderr=-1, stdout=-1), call.Popen_instance.communicate('foo') ], Popen.mock.method_calls)
def test_kill(self): Popen = MockPopen() Popen.set_command('a command') process = Popen('a command', stdout=PIPE, stderr=PIPE, shell=True) process.kill() compare([ call.Popen('a command', shell=True, stderr=-1, stdout=-1), call.Popen_instance.kill() ], Popen.mock.method_calls)
def test_read_from_stderr(self): # setup Popen = MockPopen() Popen.set_command("a command", stderr=b"foo") # usage process = Popen("a command", stdout=PIPE, stderr=PIPE, shell=True) self.assertTrue(isinstance(process.stdout.fileno(), int)) compare(process.stderr.read(), b"foo") # test call list compare([call.Popen("a command", shell=True, stderr=-1, stdout=-1)], Popen.mock.method_calls)
def test_start_new_session(self): # setup Popen = MockPopen() Popen.set_command('a command') # usage Popen('a command', start_new_session=True) # test call list compare([ call.Popen('a command', start_new_session=True), ], Popen.mock.method_calls)
def test_command_is_sequence(self): Popen = MockPopen() Popen.set_command("a command") process = Popen(["a", "command"], stdout=PIPE, stderr=PIPE) compare(process.wait(), 0) compare( [call.Popen(["a", "command"], stderr=-1, stdout=-1), call.Popen_instance.wait()], Popen.mock.method_calls )
def test_read_from_stdout_and_stderr(self): # setup Popen = MockPopen() Popen.set_command("a command", stdout=b"foo", stderr=b"bar") # usage process = Popen("a command", stdout=PIPE, stderr=PIPE, shell=True) compare(process.stdout.read(), b"foo") compare(process.stderr.read(), b"bar") # test call list compare([call.Popen("a command", shell=True, stderr=PIPE, stdout=PIPE)], Popen.mock.method_calls)
def test_invalid_poll(self): Popen = MockPopen() Popen.set_command('bar') process = Popen('bar') if PY2: text = 'poll() takes exactly 1 argument (2 given)' else: text = 'poll() takes 1 positional argument but 2 were given' with ShouldRaise(TypeError(text)): process.poll('moo')
def test_communicate_with_stderr_redirected_check_stderr_is_none(self): # setup Popen = MockPopen() Popen.set_command('a command', stdout=b'foo', stderr=b'bar') # usage process = Popen('a command', stdout=PIPE, stderr=STDOUT, shell=True) out, err = process.communicate() # test stderr is None compare(out, b'foobar') compare(err, None)
def test_read_from_stdout_with_stderr_redirected_check_stdout_contents( self): # setup Popen = MockPopen() Popen.set_command('a command', stdout=b'foo', stderr=b'bar') # usage process = Popen('a command', stdout=PIPE, stderr=STDOUT, shell=True) # test stdout contents compare(b'foobar', process.stdout.read()) compare(process.stderr, None)
def test_invalid_poll(self): Popen = MockPopen() Popen.set_command("bar") process = Popen("bar") if PY2: text = "poll() takes exactly 1 argument (2 given)" else: text = "poll() takes 1 positional argument but 2 were given" with ShouldRaise(TypeError(text)): process.poll("moo")
def test_communicate_encoding(self): Popen = MockPopen() Popen.set_command('a command', stdout=b'foo', stderr=b'bar') # usage process = Popen('a command', stdout=PIPE, stderr=PIPE, encoding='ascii') actual = process.communicate() # check compare(actual, expected=(u'foo', u'bar'))
def test_sprocess_safe_wait_and_return_code(self): command = "a command" Popen = MockPopen() Popen.set_command(command, returncode=3) process = Popen(command) compare(process.returncode, None) compare(process.wait(), 3) compare(process.returncode, 3) compare([ call.Popen(command), call.Popen_instance.wait(), ], Popen.mock.method_calls)
def test_kill(self): # setup Popen = MockPopen() Popen.set_command('a command') # usage process = Popen('a command', stdout=PIPE, stderr=PIPE, shell=True) process.kill() # result checking compare([ call.Popen('a command', shell=True, stderr=-1, stdout=-1), call.Popen_instance.kill(), ], Popen.mock.method_calls)
def test_read_from_stdout_and_stderr(self): # setup Popen = MockPopen() Popen.set_command('a command', stdout=b'foo', stderr=b'bar') # usage process = Popen('a command', stdout=PIPE, stderr=PIPE, shell=True) compare(process.stdout.read(), b'foo') compare(process.stderr.read(), b'bar') # test call list compare([ call.Popen('a command', shell=True, stderr=PIPE, stdout=PIPE), ], Popen.mock.method_calls)
def test_write_to_stdin(self): # setup Popen = MockPopen() Popen.set_command('a command') # usage process = Popen('a command', stdin=PIPE, shell=True) process.stdin.write('some text') # test call list compare([ call.Popen('a command', shell=True, stdin=PIPE), call.Popen_instance.stdin.write('some text'), ], Popen.mock.method_calls)
def test_wait_and_return_code(self): # setup Popen = MockPopen() Popen.set_command("a command", returncode=3) # usage process = Popen("a command") compare(process.returncode, None) # result checking compare(process.wait(), 3) compare(process.returncode, 3) # test call list compare([call.Popen("a command"), call.Popen_instance.wait()], Popen.mock.method_calls)
def test_terminate(self): # setup Popen = MockPopen() Popen.set_command("a command") # usage process = Popen("a command", stdout=PIPE, stderr=PIPE, shell=True) process.terminate() # result checking compare( [call.Popen("a command", shell=True, stderr=-1, stdout=-1), call.Popen_instance.terminate()], Popen.mock.method_calls, )
def test_read_from_stdout(self): # setup Popen = MockPopen() Popen.set_command('a command', stdout=b'foo') # usage process = Popen('a command', stdout=PIPE, stderr=PIPE, shell=True) self.assertTrue(isinstance(process.stdout.fileno(), int)) compare(process.stdout.read(), b'foo') # test call list compare([ call.Popen('a command', shell=True, stderr=-1, stdout=-1), ], Popen.mock.method_calls)
def test_read_from_stdout_with_stderr_redirected_check_stdout_stderr_interleaved( self): # setup Popen = MockPopen() Popen.set_command('a command', stdout=b'o1\no2\no3\no4\n', stderr=b'e1\ne2\n') # usage process = Popen('a command', stdout=PIPE, stderr=STDOUT, shell=True) self.assertTrue(isinstance(process.stdout.fileno(), int)) # test stdout contents compare(b'o1\ne1\no2\ne2\no3\no4\n', process.stdout.read())
def test_communicate_with_input(self): # setup Popen = MockPopen() Popen.set_command('a command') # usage process = Popen('a command', stdout=PIPE, stderr=PIPE, shell=True) out, err = process.communicate('foo') # test call list compare([ call.Popen('a command', shell=True, stderr=-1, stdout=-1), call.Popen_instance.communicate('foo'), ], Popen.mock.method_calls)
def test_all_signals(self): Popen = MockPopen() Popen.set_command('a command') process = Popen('a command') process.send_signal(signal.SIGINT) process.terminate() process.kill() compare([ call.Popen('a command'), call.Popen_instance.send_signal(signal.SIGINT), call.Popen_instance.terminate(), call.Popen_instance.kill() ], Popen.mock.method_calls)
def test_sprocess_safe_read_from_stdout_and_stderr(self): command = "a command" Popen = MockPopen() # only static input used with simulated mockpopen # codacy mistakenly sees this as a call to popen Popen.set_command(command, stdout=b'foo', stderr=b'bar') process = Popen(command, stdout=PIPE, stderr=PIPE, shell=True) # nosec compare(process.stdout.read(), b'foo') compare(process.stderr.read(), b'bar') compare([ call.Popen(command, shell=True, stderr=PIPE, # nosec stdout=PIPE), ], Popen.mock.method_calls)
class IntegrationTests(TestCase): def setUp(self): self.popen = MockPopen() replacer = Replacer() replacer.replace('testfixtures.tests.test_popen.subprocess.Popen', self.popen) self.addCleanup(replacer.restore) def test_command_called_with_check_call_check_returncode(self): self.popen.set_command('ls') compare(0, subprocess.check_call(['ls'])) def test_command_called_with_check_output_check_stdout_returned(self): self.popen.set_command('ls', stdout=b'abc') compare(b'abc', subprocess.check_output(['ls'])) def test_command_called_with_check_output_stderr_to_stdout_check_returned( self): self.popen.set_command('ls', stderr=b'xyz') compare(b'xyz', subprocess.check_output(['ls'], stderr=STDOUT)) def test_command_called_with_check_call_failing_command_check_exception( self): self.popen.set_command('ls', returncode=1) with self.assertRaises(subprocess.CalledProcessError): subprocess.check_output(['ls'])
def checker(test_sha=commit_sha, **kwargs): rollback = RollbackImporter() with TempDirectory() as d: popen = MockPopen() with patch('subprocess.run', new=make_run): with patch.multiple('subprocess', Popen=popen) as values: popen.set_command('git version', stdout=b'git version 2.14.3') popen.set_command('git cat-file --batch-check', stdout=b"%s commit 239" % test_sha) popen.set_command('git rev-list %s --' % test_sha.decode('utf-8'), stdout=test_sha) sha_str = test_sha.decode("utf-8") popen.set_command("git show %s" % sha_str, stdout=dummy_commit) from clincher import CommitChecker if 'manual_signing_path' not in kwargs: kwargs['manual_signing_path'] = d.path c = CommitChecker(_TestArgs(**kwargs)) with OutputCapture() as output: yield { "output": output, "popen": popen, "checker": c, "sha": sha_str, "directory": d } rollback.uninstall()
def test_sprocess_communicate_with_input(self): command = "a command" Popen = MockPopen() Popen.set_command(command) # only static input used with simulated mockpopen # codacy mistakenly sees this as a call to popen process = Popen(command, stdout=PIPE, stderr=PIPE, shell=True) # nosec err, out = process.communicate('foo') compare([ # only static input used with simulated mockpopen # codacy mistakenly sees this as a call to popen call.Popen(command, shell=True, stderr=-1, stdout=-1), # nosec call.Popen_instance.communicate('foo'), ], Popen.mock.method_calls) return err, out
class PopenCommandsTest(unittest.TestCase): def setUp(self): self.Popen = MockPopen() self.r = Replacer() self.r.replace('swabbie.utils.command.subprocess.Popen', self.Popen) self.addCleanup(self.r.restore) def test_call_cmd(self): self.Popen.set_command('cmd1', stdout='foo') command_result = Command._call_cmd('cmd1') expected_command_result = CommandResult(output='foo', return_code=0) self.assertEqual(command_result.output, expected_command_result.output) self.assertEqual(command_result.return_code, expected_command_result.return_code) self.assertFalse(command_result.err)
def test_sprocess_safe_write_to_stdin(self): command = "a command" Popen = MockPopen() Popen.set_command(command) # only static input used with simulated mockpopen # codacy mistakenly sees this as a call to popen process = Popen(command, stdin=PIPE, shell=True) # nosec process.stdin.write(command) process.stdin.close() compare([ # static input used with simulated mockpopen # codacy mistakenly sees this as a call to popen call.Popen(command, shell=True, stdin=PIPE), # nosec call.Popen_instance.stdin.write(command), call.Popen_instance.stdin.close(), ], Popen.mock.method_calls)
def test_all_signals(self): # setup Popen = MockPopen() Popen.set_command('a command') # usage process = Popen('a command') process.send_signal(signal.SIGINT) process.terminate() process.kill() # test call list compare([ call.Popen('a command'), call.Popen_instance.send_signal(signal.SIGINT), call.Popen_instance.terminate(), call.Popen_instance.kill(), ], Popen.mock.method_calls)
def test_multiple_uses(self): Popen = MockPopen() Popen.set_command('a command', b'a') Popen.set_command('b command', b'b') process = Popen('a command', stdout=PIPE, stderr=PIPE, shell=True) out, err = process.communicate('foo') compare(out, b'a') process = Popen(['b', 'command'], stdout=PIPE, stderr=PIPE, shell=True) out, err = process.communicate('foo') compare(out, b'b') compare([ call.Popen('a command', shell=True, stderr=-1, stdout=-1), call.Popen_instance.communicate('foo'), call.Popen(['b', 'command'], shell=True, stderr=-1, stdout=-1), call.Popen_instance.communicate('foo'), ], Popen.mock.method_calls)
def test_communicate_with_timeout(self): Popen = MockPopen() Popen.set_command('a command', returncode=3) process = Popen('a command') if PY2: with ShouldRaise(TypeError): process.communicate(timeout=1) with ShouldRaise(TypeError): process.communicate('foo', 1) else: process.communicate(timeout=1) process.communicate('foo', 1) compare([ call.Popen('a command'), call.Popen_instance.communicate(timeout=1), call.Popen_instance.communicate('foo', 1), ], expected=Popen.mock.method_calls)
def test_poll_until_result(self): # setup Popen = MockPopen() Popen.set_command('a command', returncode=3, poll_count=2) # example usage process = Popen('a command') while process.poll() is None: # you'd probably have a sleep here, or go off and # do some other work. pass # result checking compare(process.returncode, 3) compare([ call.Popen('a command'), call.Popen_instance.poll(), call.Popen_instance.poll(), call.Popen_instance.poll(), ], Popen.mock.method_calls)