e46f667339f183536e3b4872d14c9537d48cc680
[nepi.git] / test / resources / linux / node.py
1 #!/usr/bin/env python
2 #
3 #    NEPI, a framework to manage network experiments
4 #    Copyright (C) 2013 INRIA
5 #
6 #    This program is free software: you can redistribute it and/or modify
7 #    it under the terms of the GNU General Public License as published by
8 #    the Free Software Foundation, either version 3 of the License, or
9 #    (at your option) any later version.
10 #
11 #    This program is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #    GNU General Public License for more details.
15 #
16 #    You should have received a copy of the GNU General Public License
17 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
20
21
22 from nepi.resources.linux.node import LinuxNode, ExitCode
23 from nepi.util.sshfuncs import ProcStatus
24
25 from test_utils import skipIfNotAlive, skipInteractive, create_node
26
27 import shutil
28 import os
29 import time
30 import tempfile
31 import unittest
32
33 class LinuxNodeTestCase(unittest.TestCase):
34     def setUp(self):
35         self.fedora_host = "nepi2.pl.sophia.inria.fr"
36         self.fedora_user = "inria_nepi"
37
38         self.ubuntu_host = "roseval.pl.sophia.inria.fr"
39         self.ubuntu_user = "inria_nepi"
40         
41         self.target = "nepi5.pl.sophia.inria.fr"
42
43     @skipIfNotAlive
44     def t_execute(self, host, user):
45         node, ec = create_node(host, user)
46
47         command = "ping -qc3 %s" % self.target
48         
49         (out, err), proc = node.execute(command)
50
51         expected = """3 packets transmitted, 3 received, 0% packet loss"""
52
53         self.assertTrue(out.find(expected) > 0)
54
55     @skipIfNotAlive
56     def t_run(self, host, user):
57         node, ec = create_node(host, user)
58         
59         node.find_home()
60         app_home = os.path.join(node.exp_home, "my-app")
61         node.mkdir(app_home, clean = True)
62         
63         command = "ping %s" % self.target
64         node.run(command, app_home)
65         pid, ppid = node.getpid(app_home)
66
67         status = node.status(pid, ppid)
68         self.assertTrue(status, ProcStatus.RUNNING)
69
70         node.kill(pid, ppid)
71         status = node.status(pid, ppid)
72         self.assertTrue(status, ProcStatus.FINISHED)
73         
74         (out, err), proc = node.check_output(app_home, "stdout")
75
76         expected = """64 bytes from"""
77
78         self.assertTrue(out.find(expected) > 0)
79
80         node.rmdir(app_home)
81
82     @skipIfNotAlive
83     def t_exitcode_ok(self, host, user):
84         command = "echo 'OK!'"
85         
86         node, ec = create_node(host, user)
87          
88         node.find_home()
89         app_home = os.path.join(node.exp_home, "my-app")
90         node.mkdir(app_home, clean = True)
91          
92         (out, err), proc = node.run_and_wait(command, app_home,
93             shfile = "cmd.sh",
94             pidfile = "pid",
95             ecodefile = "exitcode",
96             stdout = "stdout", 
97             stderr = "stderr",
98             raise_on_error = True)
99  
100         # get the pid of the process
101         ecode = node.exitcode(app_home)
102         self.assertEquals(ecode, ExitCode.OK)
103
104     @skipIfNotAlive
105     def t_exitcode_kill(self, host, user):
106         node, ec = create_node(host, user)
107          
108         node.find_home()
109         app_home = os.path.join(node.exp_home, "my-app")
110         node.mkdir(app_home, clean = True)
111        
112         # Upload command that will not finish
113         command = "ping localhost"
114         shfile = os.path.join(app_home, "cmd.sh")
115         (out, err), proc = node.upload_command(command, 
116             shfile = shfile,
117             ecodefile = "exitcode")
118
119         (out, err), proc = node.run(command, app_home,
120             pidfile = "pidfile",
121             stdout = "stdout", 
122             stderr = "stderr")
123  
124         # Just wait to make sure the ping started
125         time.sleep(5)
126
127         # The process is still running, so no retfile has been created yet
128         ecode = node.exitcode(app_home)
129         self.assertEquals(ecode, ExitCode.FILENOTFOUND)
130         
131         (out, err), proc = node.check_errors(app_home)
132         self.assertEquals(err, "")
133         
134         # Now kill the app
135         pid, ppid = node.getpid(app_home)
136         node.kill(pid, ppid)
137          
138         (out, err), proc = node.check_errors(app_home)
139         self.assertEquals(err, "")
140
141     @skipIfNotAlive
142     def t_exitcode_error(self, host, user):
143         # Try to execute a command that doesn't exist
144         command = "unexistent-command"
145         
146         node, ec = create_node(host, user)
147          
148         node.find_home()
149         app_home = os.path.join(node.exp_home, "my-app")
150         node.mkdir(app_home, clean = True)
151          
152         (out, err), proc = node.run_and_wait(command, app_home,
153             shfile = "cmd.sh",
154             pidfile = "pid",
155             ecodefile = "exitcode",
156             stdout = "stdout", 
157             stderr = "stderr",
158             raise_on_error = False)
159  
160         # get the pid of the process
161         ecode = node.exitcode(app_home)
162
163         # bash erro 127 - command not found
164         self.assertEquals(ecode, 127)
165  
166         (out, err), proc = node.check_errors(app_home)
167
168         self.assertTrue(err.find("cmd.sh: line 1: unexistent-command: command not found") > -1)
169
170     @skipIfNotAlive
171     def t_install(self, host, user):
172         node, ec = create_node(host, user)
173
174         node.find_home()
175         (out, err), proc = node.mkdir(node.node_home, clean = True)
176         self.assertEquals(err, "")
177
178         (out, err), proc = node.install_packages("gcc", node.node_home)
179         self.assertEquals(err, "")
180
181         (out, err), proc = node.remove_packages("gcc", node.node_home)
182         self.assertEquals(err, "")
183
184         (out, err), proc = node.rmdir(node.exp_home)
185         self.assertEquals(err, "")
186
187     @skipIfNotAlive
188     def t_clean(self, host, user):
189         node, ec = create_node(host, user)
190
191         node.find_home()
192         node.mkdir(node.lib_dir)
193         node.mkdir(node.node_home)
194
195         command1 = " [ -d %s ] && echo 'Found'" % node.lib_dir
196         (out, err), proc = node.execute(command1)
197     
198         self.assertEquals(out.strip(), "Found")
199
200         command2 = " [ -d %s ] && echo 'Found'" % node.node_home
201         (out, err), proc = node.execute(command2)
202     
203         self.assertEquals(out.strip(), "Found")
204
205         node.clean_experiment()
206         
207         (out, err), proc = node.execute(command2)
208
209         self.assertEquals(out.strip(), "")
210
211         node.clean_home()
212         
213         (out, err), proc = node.execute(command1)
214
215         self.assertEquals(out.strip(), "")
216
217     @skipIfNotAlive
218     def t_xterm(self, host, user):
219         node, ec = create_node(host, user)
220
221         node.find_home()
222         (out, err), proc = node.mkdir(node.node_home, clean = True)
223         self.assertEquals(err, "")
224         
225         node.install_packages("xterm", node.node_home)
226         self.assertEquals(err, "")
227
228         (out, err), proc = node.execute("xterm", forward_x11 = True)
229         self.assertEquals(err, "")
230
231         (out, err), proc = node.remove_packages("xterm", node.node_home)
232         self.assertEquals(err, "")
233
234     @skipIfNotAlive
235     def t_compile(self, host, user):
236         node, ec = create_node(host, user)
237
238         node.find_home()
239         app_home = os.path.join(node.exp_home, "my-app")
240         node.mkdir(app_home, clean = True)
241
242         prog = """#include <stdio.h>
243
244 int
245 main (void)
246 {
247     printf ("Hello, world!\\n");
248     return 0;
249 }
250 """
251         # upload the test program
252         dst = os.path.join(app_home, "hello.c")
253         node.upload(prog, dst, text = True)
254
255         # install gcc
256         node.install_packages('gcc', app_home)
257
258         # compile the program using gcc
259         command = "cd %s; gcc -Wall hello.c -o hello" % app_home
260         (out, err), proc = node.execute(command)
261
262         # execute the program and get the output from stdout
263         command = "%s/hello" % app_home 
264         (out, err), proc = node.execute(command)
265
266         self.assertEquals(out, "Hello, world!\n")
267
268         # execute the program and get the output from a file
269         command = "%(home)s/hello > %(home)s/hello.out" % {
270                 'home': app_home}
271         (out, err), proc = node.execute(command)
272
273         # retrieve the output file 
274         src = os.path.join(app_home, "hello.out")
275         f = tempfile.NamedTemporaryFile(delete = False)
276         dst = f.name
277         node.download(src, dst)
278         f.close()
279
280         node.remove_packages("gcc", app_home)
281         node.rmdir(app_home)
282
283         f = open(dst, "r")
284         out = f.read()
285         f.close()
286         
287         self.assertEquals(out, "Hello, world!\n")
288
289     @skipIfNotAlive
290     def t_copy_files(self, host, user):
291         node, ec = create_node(host, user)
292
293         node.find_home()
294         app_home = os.path.join(node.exp_home, "my-app")
295         node.mkdir(app_home, clean = True)
296
297         # create some temp files and directories to copy
298         dirpath = tempfile.mkdtemp()
299         f = tempfile.NamedTemporaryFile(dir=dirpath, delete=False)
300         f.close()
301       
302         f1 = tempfile.NamedTemporaryFile(delete=False)
303         f1.close()
304         f1.name
305
306         source = [dirpath, f1.name]
307         destdir = "test"
308         node.mkdir(destdir, clean = True)
309         dest = "%s@%s:test" % (user, host)
310         node.copy(source, dest)
311
312         command = "ls %s" % destdir
313         
314         (out, err), proc = node.execute(command)
315
316         os.remove(f1.name)
317         shutil.rmtree(dirpath)
318
319         self.assertTrue(out.find(os.path.basename(dirpath)) > -1)
320         self.assertTrue(out.find(os.path.basename(f1.name)) > -1)
321
322         f2 = tempfile.NamedTemporaryFile(delete=False)
323         f2.close()
324         f2.name
325
326         node.mkdir(destdir, clean = True)
327         dest = "%s@%s:test" % (user, host)
328         node.copy(f2.name, dest)
329
330         command = "ls %s" % destdir
331         
332         (out, err), proc = node.execute(command)
333
334         os.remove(f2.name)
335         
336         self.assertTrue(out.find(os.path.basename(f2.name)) > -1)
337
338     def test_execute_fedora(self):
339         self.t_execute(self.fedora_host, self.fedora_user)
340
341     def test_execute_ubuntu(self):
342         self.t_execute(self.ubuntu_host, self.ubuntu_user)
343
344     def test_run_fedora(self):
345         self.t_run(self.fedora_host, self.fedora_user)
346
347     def test_run_ubuntu(self):
348         self.t_run(self.ubuntu_host, self.ubuntu_user)
349
350     def test_intall_fedora(self):
351         self.t_install(self.fedora_host, self.fedora_user)
352
353     def test_install_ubuntu(self):
354         self.t_install(self.ubuntu_host, self.ubuntu_user)
355
356     def test_compile_fedora(self):
357         self.t_compile(self.fedora_host, self.fedora_user)
358
359     def test_compile_ubuntu(self):
360         self.t_compile(self.ubuntu_host, self.ubuntu_user)
361
362     def test_exitcode_ok_fedora(self):
363         self.t_exitcode_ok(self.fedora_host, self.fedora_user)
364
365     def test_exitcode_ok_ubuntu(self):
366         self.t_exitcode_ok(self.ubuntu_host, self.ubuntu_user)
367
368     def test_exitcode_kill_fedora(self):
369         self.t_exitcode_kill(self.fedora_host, self.fedora_user)
370
371     def test_exitcode_kill_ubuntu(self):
372         self.t_exitcode_kill(self.ubuntu_host, self.ubuntu_user)
373
374     def test_exitcode_error_fedora(self):
375         self.t_exitcode_error(self.fedora_host, self.fedora_user)
376
377     def test_exitcode_error_ubuntu(self):
378         self.t_exitcode_error(self.ubuntu_host, self.ubuntu_user)
379
380     def test_clean_fedora(self):
381         self.t_clean(self.fedora_host, self.fedora_user)
382
383     def test_clean_ubuntu(self):
384         self.t_clean(self.ubuntu_host, self.ubuntu_user)
385      
386     @skipInteractive
387     def test_xterm_ubuntu(self):
388         """ Interactive test. Should not run automatically """
389         self.t_xterm(self.ubuntu_host, self.ubuntu_user)
390
391     def test_copy_files_fedora(self):
392         self.t_copy_files(self.fedora_host, self.fedora_user)
393
394     def test_copy_files_ubuntu(self):
395         self.t_copy_files(self.ubuntu_host, self.ubuntu_user)
396
397 if __name__ == '__main__':
398     unittest.main()
399