How to use the testfixtures.popen.MockPopen function in testfixtures

To help you get started, we’ve selected a few testfixtures examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github guyzmo / git-repo / tests / helpers.py View on Github external
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
github linkedin / kafka-tools / tests / tools / assigner / models / test_reassignment.py View on Github external
    @patch('kafka.tools.assigner.models.reassignment.subprocess.Popen', new_callable=MockPopen)
    @patch.object(Reassignment, 'check_completion')
    def test_reassignment_internal_execute(self, mock_check, mock_popen):
        mock_popen.set_default()
        mock_check.side_effect = [10, 5, 0]

        self.reassignment._execute(1, 1, 'zkconnect', '/path/to/tools')

        compare([call.Popen(['/path/to/tools/kafka-reassign-partitions.sh', '--execute', '--zookeeper', 'zkconnect', '--reassignment-json-file', ANY],
                            stderr=ANY, stdout=ANY),
                 call.Popen_instance.wait()], mock_popen.mock.method_calls)
        assert len(mock_check.mock_calls) == 3
github uber / uber-poet / tests / test_statemanagement.py View on Github external
def setUp(self):
        self.mock_popen = testfixtures.popen.MockPopen()
        self.r = testfixtures.Replacer()
        self.r.replace('subprocess.Popen', self.mock_popen)
        self.addCleanup(self.r.restore)
github uber / uber-poet / tests / test_statemanagement.py View on Github external
def setUp(self):
        self.mock_popen = testfixtures.popen.MockPopen()
        self.r = testfixtures.Replacer()
        self.r.replace('subprocess.Popen', self.mock_popen)
        self.addCleanup(self.r.restore)
github Simplistix / testfixtures / testfixtures / popen.py View on Github external
of running processes.
:param poll_count:
    Specifies the number of times :meth:`MockPopen.poll` can be
    called before :attr:`MockPopen.returncode` is set and returned
    by :meth:`MockPopen.poll`.

If supplied, ``behaviour`` must be either a :class:`PopenBehaviour`
instance or a callable that takes the ``command`` string representing
the command to be simulated and the ``stdin`` for that command and
returns a :class:`PopenBehaviour` instance.
"""


# add the param docs, so we only have one copy of them!
extend_docstring(set_command_params,
                 [MockPopen.set_command, MockPopen.set_default])
github uber / uber-poet / tests / test_integration.py View on Github external
args = [
            "--log_dir", log_path, "--app_gen_output_dir", root_path, "--test_build_only", "--switch_xcode_versions",
            "--full_clean"
        ]

        # we need the unused named variable for mocking purposes
        # noinspection PyUnusedLocal
        def command_callable(command, stdin):
            if 'cloc' in command:
                return PopenBehaviour(stdout=cloc_out)
            elif 'xcodebuild -version' in command:
                return PopenBehaviour(stdout=b'Xcode 10.0\nBuild version 10A255\n')
            return PopenBehaviour(stdout=b'test_out', stderr=b'test_error')

        with testfixtures.Replacer() as rep:
            mock_popen = MockPopen()
            rep.replace('subprocess.Popen', mock_popen)
            mock_popen.set_default(behaviour=command_callable)

            with mock.patch('distutils.spawn.find_executable') as mock_find:
                mock_find.return_value = '/bin/ls'  # A non empty return value basically means "I found that executable"
                CommandLineMultisuite().main(args)
                self.assertGreater(os.listdir(app_path), 0)
                self.verify_genproj('MockLib53', 101, app_path)
github linkedin / kafka-tools / tests / tools / assigner / sizers / test_ssh.py View on Github external
    @patch('kafka.tools.assigner.models.reassignment.subprocess.Popen', new_callable=MockPopen)
    def test_sizer_run_setsizes_singlehost(self, mock_ssh):
        m_stdout = ("1001\t/path/to/data/testTopic1-0\n"
                    "1002\t/path/to/data/testTopic1-1\n"
                    "2001\t/path/to/data/testTopic2-0\n"
                    "2002\t/path/to/data/testTopic2-1\n")
        mock_ssh.set_default(stdout=m_stdout.encode('utf-8'))

        sizer = SizerSSH(self.args, self.cluster)
        sizer.get_partition_sizes()

        compare([call.Popen(['ssh', 'brokerhost1.example.com', 'du -sk /path/to/data/*'], stdout=PIPE, stderr=ANY)], mock_ssh.mock.method_calls)
        assert self.cluster.topics['testTopic1'].partitions[0].size == 1001
        assert self.cluster.topics['testTopic1'].partitions[1].size == 1002
        assert self.cluster.topics['testTopic2'].partitions[0].size == 2001
        assert self.cluster.topics['testTopic2'].partitions[1].size == 2002
github linkedin / kafka-tools / tests / tools / assigner / models / test_reassignment.py View on Github external
    @patch('kafka.tools.assigner.models.reassignment.subprocess.Popen', new_callable=MockPopen)
    def test_check_completion_success(self, mock_popen):
        cmd_stdout = ("Status of partition reassignment:\n"
                      "Reassignment of partition [testTopic,0] completed successfully\n"
                      "Reassignment of partition [testTopic,1] completed successfully\n"
                      "Reassignment of partition [testTopic,2] completed successfully\n"
                      "Reassignment of partition [testTopic,3] completed successfully\n")
        mock_popen.set_default(stdout=cmd_stdout.encode('utf-8'))
        assert self.reassignment.check_completion('zkconnect', '/path/to/tools', 'assignfilename') == 0