]>
git.ipfire.org Git - pakfire.git/blob - tests/python/jail.py
265f7e66a3451f9e96ed1c85fcf93db9183198b9
8 class JailTests(tests
.TestCase
):
10 This tests the execute command
13 self
.pakfire
= self
.setup_pakfire()
15 def test_execute(self
):
16 r
= self
.pakfire
.execute(["/command", "exit-with-code", "0"])
20 def test_return_value(self
):
21 with self
.assertRaises(pakfire
.CommandExecutionError
) as e
:
22 self
.pakfire
.execute(["/command", "exit-with-code", "123"])
25 code
, = e
.exception
.args
27 self
.assertTrue(code
== 123)
29 def test_environ(self
):
30 r
= self
.pakfire
.execute(["/command", "echo-environ", "VAR1"],
31 environ
={"VAR1" : "VAL1"})
35 def test_invalid_inputs(self
):
37 with self
.assertRaises(TypeError):
38 self
.pakfire
.execute("/command")
40 with self
.assertRaises(TypeError):
41 self
.pakfire
.execute(["/command", 1])
43 with self
.assertRaises(TypeError):
44 self
.pakfire
.execute(("/command", "--help"))
47 with self
.assertRaises(TypeError):
48 self
.pakfire
.execute(["/command", "--help"], environ
={"VAR1" : 1})
50 with self
.assertRaises(TypeError):
51 self
.pakfire
.execute(["/command", "--help"], environ
={1 : "VAL1"})
53 with self
.assertRaises(TypeError):
54 self
.pakfire
.execute(["/command", "--help"], environ
="VAR1=VAL1")
56 def test_execute_non_existant_command(self
):
58 Executing non-existant commands should raise an error
60 with self
.assertRaises(pakfire
.CommandExecutionError
):
61 self
.pakfire
.execute(["/command-does-not-exist"])
63 def test_execute_output(self
):
64 self
.pakfire
.execute(["/command", "echo", "123"])
66 # Multiple newlines in one read
67 self
.pakfire
.execute(["/command", "echo", "1\n2\n3"])
69 # Run a command with a lot of output which exceeds the buffer size
70 self
.pakfire
.execute(["/command", "lines", "1", "65536"])
72 # Run a command that generates lots of lines
73 self
.pakfire
.execute(["/command", "lines", "100", "40"])
75 def test_execute_logger(self
):
76 def log(priority
, message
):
77 # Priority must be INFO
78 self
.assertEqual(priority
, logging
.INFO
)
80 # All lines must be 20 characters long
81 self
.assertEqual(len(message
), 20)
83 self
.pakfire
.execute(["/command", "lines", "10", "20"], logging_callback
=log
)
85 # XXX This does not work
86 #def test_execute_logger_exceptions(self):
88 # The logger callback will raise an Exception which should be handled
90 # def log(priority, message):
91 # raise RuntimeError(message)
93 # with self.assertRaises(RuntimeError):
94 # self.pakfire.execute(
95 # ["/command", "echo", "Hello World!"],
96 # logging_callback=log,
100 def checkpid(priority
, message
):
102 self
.assertEqual(message
, "1")
104 self
.pakfire
.execute(["/command", "print-pid"], logging_callback
=checkpid
)
107 self
.pakfire
.execute(["/command", "print-nice"], nice
=5)
109 def test_nice_invalid_input(self
):
111 Tries using an invalid nice value
113 with self
.assertRaises(OSError):
114 self
.pakfire
.execute(["/command", "print-nice"], nice
=100)
116 def test_check_open_file_descriptors(self
):
118 Since we are spawning child processes, it might happen that we leak file
119 descriptors to the child process.
121 self
.pakfire
.execute(["/command", "check-open-file-descriptors"])
125 def test_send_signal_DEFAULT(self
):
127 Sends a stupid signal which doesn't do anything
129 self
.pakfire
.execute(["/command", "send-signal", "0"])
131 def test_send_signal_KILL(self
):
133 Test the process killing itself
135 self
.pakfire
.execute(["/command", "send-signal", "9"])
137 def test_send_signal_TERM(self
):
139 Test the process terminating itself
141 self
.pakfire
.execute(["/command", "send-signal", "15"])
143 # This is an interactive test which cannot be performed automatically
144 #def test_shell(self):
145 # self.pakfire.execute(["/bin/bash", "-i"])
148 if __name__
== "__main__":