3
from landscape.tests.helpers import LandscapeTest
4
from landscape.lib.twisted_util import spawn_process
5
from landscape.lib.fs import create_file
8
class SpawnProcessTest(LandscapeTest):
11
super(SpawnProcessTest, self).setUp()
12
self.command = self.makeFile("#!/bin/sh\necho -n $@")
13
os.chmod(self.command, 0755)
15
def test_spawn_process_return_value(self):
17
The process is executed and returns the expected exit code.
19
create_file(self.command, "#!/bin/sh\nexit 2")
21
def callback((out, err, code)):
22
self.assertEqual(out, "")
23
self.assertEqual(err, "")
24
self.assertEqual(code, 2)
26
result = spawn_process(self.command)
27
result.addCallback(callback)
30
def test_spawn_process_output(self):
32
The process returns the expected standard output.
34
def callback((out, err, code)):
35
self.assertEqual(out, "a b")
36
self.assertEqual(err, "")
37
self.assertEqual(code, 0)
39
result = spawn_process(self.command, args=("a", "b"))
40
result.addCallback(callback)
43
def test_spawn_process_error(self):
45
The process returns the expected standard error.
47
create_file(self.command, "#!/bin/sh\necho -n $@ >&2")
49
def callback((out, err, code)):
50
self.assertEqual(out, "")
51
self.assertEqual(err, "a b")
52
self.assertEqual(code, 0)
54
result = spawn_process(self.command, args=("a", "b"))
55
result.addCallback(callback)
58
def test_spawn_process_callback(self):
60
If a callback for process output is provieded, it is called for every
63
create_file(self.command, "#!/bin/sh\n/bin/echo -ne $@")
64
param = r"some text\nanother line\nok, last one\n"
65
expected = ["some text", "another line", "ok, last one"]
68
def line_received(line):
71
def callback((out, err, code)):
72
self.assertEqual(expected, lines)
74
result = spawn_process(self.command, args=(param,),
75
line_received=line_received)
76
result.addCallback(callback)
79
def test_spawn_process_callback_multiple_newlines(self):
81
If output ends with more than one newline, empty lines are preserved.
83
create_file(self.command, "#!/bin/sh\n/bin/echo -ne $@")
84
param = r"some text\nanother line\n\n\n"
85
expected = ["some text", "another line", "", ""]
88
def line_received(line):
91
def callback((out, err, code)):
92
self.assertEqual(expected, lines)
94
result = spawn_process(self.command, args=(param,),
95
line_received=line_received)
96
result.addCallback(callback)
99
def test_spawn_process_callback_no_newline(self):
101
If output ends without a newline, the line is still passed to the
104
create_file(self.command, "#!/bin/sh\n/bin/echo -ne $@")
105
param = r"some text\nanother line\nok, last one"
106
expected = ["some text", "another line", "ok, last one"]
109
def line_received(line):
112
def callback((out, err, code)):
113
self.assertEqual(expected, lines)
115
result = spawn_process(self.command, args=(param,),
116
line_received=line_received)
117
result.addCallback(callback)
120
def test_spawn_process_with_stdin(self):
122
Optionally C{spawn_process} accepts a C{stdin} argument.
124
create_file(self.command, "#!/bin/sh\n/bin/cat")
126
def callback((out, err, code)):
127
self.assertEqual("hello", out)
129
result = spawn_process(self.command, stdin="hello")
130
result.addCallback(callback)