]> git.ipfire.org Git - pakfire.git/blob - tests/python/jail.py
265f7e66a3451f9e96ed1c85fcf93db9183198b9
[pakfire.git] / tests / python / jail.py
1 #!/usr/bin/python3
2
3 import logging
4 import pakfire
5
6 import tests
7
8 class JailTests(tests.TestCase):
9 """
10 This tests the execute command
11 """
12 def setUp(self):
13 self.pakfire = self.setup_pakfire()
14
15 def test_execute(self):
16 r = self.pakfire.execute(["/command", "exit-with-code", "0"])
17
18 self.assertIsNone(r)
19
20 def test_return_value(self):
21 with self.assertRaises(pakfire.CommandExecutionError) as e:
22 self.pakfire.execute(["/command", "exit-with-code", "123"])
23
24 # Extract return code
25 code, = e.exception.args
26
27 self.assertTrue(code == 123)
28
29 def test_environ(self):
30 r = self.pakfire.execute(["/command", "echo-environ", "VAR1"],
31 environ={"VAR1" : "VAL1"})
32
33 self.assertIsNone(r)
34
35 def test_invalid_inputs(self):
36 # Arguments
37 with self.assertRaises(TypeError):
38 self.pakfire.execute("/command")
39
40 with self.assertRaises(TypeError):
41 self.pakfire.execute(["/command", 1])
42
43 with self.assertRaises(TypeError):
44 self.pakfire.execute(("/command", "--help"))
45
46 # Environment
47 with self.assertRaises(TypeError):
48 self.pakfire.execute(["/command", "--help"], environ={"VAR1" : 1})
49
50 with self.assertRaises(TypeError):
51 self.pakfire.execute(["/command", "--help"], environ={1 : "VAL1"})
52
53 with self.assertRaises(TypeError):
54 self.pakfire.execute(["/command", "--help"], environ="VAR1=VAL1")
55
56 def test_execute_non_existant_command(self):
57 """
58 Executing non-existant commands should raise an error
59 """
60 with self.assertRaises(pakfire.CommandExecutionError):
61 self.pakfire.execute(["/command-does-not-exist"])
62
63 def test_execute_output(self):
64 self.pakfire.execute(["/command", "echo", "123"])
65
66 # Multiple newlines in one read
67 self.pakfire.execute(["/command", "echo", "1\n2\n3"])
68
69 # Run a command with a lot of output which exceeds the buffer size
70 self.pakfire.execute(["/command", "lines", "1", "65536"])
71
72 # Run a command that generates lots of lines
73 self.pakfire.execute(["/command", "lines", "100", "40"])
74
75 def test_execute_logger(self):
76 def log(priority, message):
77 # Priority must be INFO
78 self.assertEqual(priority, logging.INFO)
79
80 # All lines must be 20 characters long
81 self.assertEqual(len(message), 20)
82
83 self.pakfire.execute(["/command", "lines", "10", "20"], logging_callback=log)
84
85 # XXX This does not work
86 #def test_execute_logger_exceptions(self):
87 # """
88 # The logger callback will raise an Exception which should be handled
89 # """
90 # def log(priority, message):
91 # raise RuntimeError(message)
92 #
93 # with self.assertRaises(RuntimeError):
94 # self.pakfire.execute(
95 # ["/command", "echo", "Hello World!"],
96 # logging_callback=log,
97 # )
98
99 def test_pid(self):
100 def checkpid(priority, message):
101 # The PID must be 1
102 self.assertEqual(message, "1")
103
104 self.pakfire.execute(["/command", "print-pid"], logging_callback=checkpid)
105
106 def test_nice(self):
107 self.pakfire.execute(["/command", "print-nice"], nice=5)
108
109 def test_nice_invalid_input(self):
110 """
111 Tries using an invalid nice value
112 """
113 with self.assertRaises(OSError):
114 self.pakfire.execute(["/command", "print-nice"], nice=100)
115
116 def test_check_open_file_descriptors(self):
117 """
118 Since we are spawning child processes, it might happen that we leak file
119 descriptors to the child process.
120 """
121 self.pakfire.execute(["/command", "check-open-file-descriptors"])
122
123 # Signals
124
125 def test_send_signal_DEFAULT(self):
126 """
127 Sends a stupid signal which doesn't do anything
128 """
129 self.pakfire.execute(["/command", "send-signal", "0"])
130
131 def test_send_signal_KILL(self):
132 """
133 Test the process killing itself
134 """
135 self.pakfire.execute(["/command", "send-signal", "9"])
136
137 def test_send_signal_TERM(self):
138 """
139 Test the process terminating itself
140 """
141 self.pakfire.execute(["/command", "send-signal", "15"])
142
143 # This is an interactive test which cannot be performed automatically
144 #def test_shell(self):
145 # self.pakfire.execute(["/bin/bash", "-i"])
146
147
148 if __name__ == "__main__":
149 tests.main()