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_example(self): self.Popen.set_command('svn ls -R foo', stdout='o', stderr='e') compare(my_func(), 'o') compare([ call.Popen('svn ls -R foo', shell=True, stderr=PIPE, stdout=PIPE), call.Popen_instance.communicate() ], Popen.mock.method_calls)
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_terminate(self): Popen = MockPopen() Popen.set_command('a command') process = Popen('a command', stdout=PIPE, stderr=PIPE, shell=True) process.terminate() compare([ call.Popen('a command', shell=True, stderr=-1, stdout=-1), call.Popen_instance.terminate() ], Popen.mock.method_calls)
def test_send_signal(self): Popen = MockPopen() Popen.set_command('a command') process = Popen('a command', stdout=PIPE, stderr=PIPE, shell=True) process.send_signal(0) compare([ call.Popen('a command', shell=True, stderr=-1, stdout=-1), call.Popen_instance.send_signal(0) ], 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_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_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_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_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
def test_check_completion_calls_tool(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')) self.reassignment.check_completion('zkconnect', '/path/to/tools', 'assignfilename') compare([call.Popen(['/path/to/tools/kafka-reassign-partitions.sh', '--verify', '--zookeeper', 'zkconnect', '--reassignment-json-file', 'assignfilename'], stderr=ANY, stdout=PIPE)], mock_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_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_example(self): # set up self.Popen.set_command('svn ls -R foo', stdout=b'o', stderr=b'e') # testing of results compare(my_func(), b'o') # testing calls were in the right order and with the correct parameters: compare([ call.Popen('svn ls -R foo', shell=True, stderr=PIPE, stdout=PIPE), call.Popen_instance.communicate() ], 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_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_poll_until_result(self): Popen = MockPopen() Popen.set_command('a command', returncode=3, poll_count=2) process = Popen('a command') while process.poll() is None: pass 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)
def test_command_max_args(self): Popen = MockPopen() Popen.set_command('a command', 'out', 'err', 1, 345) process = Popen('a command', stdout=PIPE, stderr=PIPE) compare(process.pid, 345) compare(None, process.returncode) out, err = process.communicate() compare(out, 'out') compare(err, 'err') compare(process.returncode, 1) compare([ call.Popen('a command', stderr=-1, stdout=-1), call.Popen_instance.communicate() ], Popen.mock.method_calls)
def test_poll_setup(self): Popen = MockPopen() Popen.set_command('a command', poll_count=1) process = Popen('a command', stdout=PIPE, stderr=PIPE, shell=True) compare(process.poll(), None) compare(process.poll(), 0) compare(process.wait(), 0) compare(process.poll(), 0) compare([ call.Popen('a command', shell=True, stderr=-1, stdout=-1), call.Popen_instance.poll(), call.Popen_instance.poll(), call.Popen_instance.wait(), call.Popen_instance.poll() ], 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_poll_no_setup(self): # setup Popen = MockPopen() Popen.set_command('a command') # usage process = Popen('a command', stdout=PIPE, stderr=PIPE, shell=True) compare(process.poll(), None) compare(process.poll(), None) compare(process.wait(), 0) compare(process.poll(), 0) # result checking compare([ call.Popen('a command', shell=True, stderr=-1, stdout=-1), call.Popen_instance.poll(), call.Popen_instance.poll(), call.Popen_instance.wait(), call.Popen_instance.poll(), ], Popen.mock.method_calls)
def test_push_app(mock_popen): app_location = '/some/app/path' manifest_location = '/some/app/app_manifest.yml' timeout = 100 command = [ 'cf', 'push', '-t', str(timeout), '-f', manifest_location, '--no-start', '--no-hostname' ] mock_popen.set_command(' '.join(command)) cf_cli.push(app_location, manifest_location, ' '.join(command[-2:]), timeout) assert call.Popen(command, stdout=PIPE, stderr=STDOUT, cwd=app_location, shell=False) in mock_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)
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
def test_command_min_args(self): # setup Popen = MockPopen() Popen.set_command('a command') # usage process = Popen('a command', stdout=PIPE, stderr=PIPE) # process started, no return code compare(process.pid, 1234) compare(None, process.returncode) out, err = process.communicate() # test the rest compare(out, b'') compare(err, b'') compare(process.returncode, 0) # test call list compare([ call.Popen('a command', stderr=-1, stdout=-1), call.Popen_instance.communicate(), ], Popen.mock.method_calls)