Adding sfa support ple using hostname
[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         import pdb;pdb.set_trace() 
315         (out, err), proc = node.execute(command)
316
317         os.remove(f1.name)
318         shutil.rmtree(dirpath)
319
320         self.assertTrue(out.find(os.path.basename(dirpath)) > -1)
321         self.assertTrue(out.find(os.path.basename(f1.name)) > -1)
322
323         f2 = tempfile.NamedTemporaryFile(delete=False)
324         f2.close()
325         f2.name
326
327         node.mkdir(destdir, clean = True)
328         dest = "%s@%s:test" % (user, host)
329         node.copy(f2.name, dest)
330
331         command = "ls %s" % destdir
332         
333         (out, err), proc = node.execute(command)
334
335         os.remove(f2.name)
336         
337         self.assertTrue(out.find(os.path.basename(f2.name)) > -1)
338
339     def test_execute_fedora(self):
340         self.t_execute(self.fedora_host, self.fedora_user)
341
342     def test_execute_ubuntu(self):
343         self.t_execute(self.ubuntu_host, self.ubuntu_user)
344
345     def test_run_fedora(self):
346         self.t_run(self.fedora_host, self.fedora_user)
347
348     def test_run_ubuntu(self):
349         self.t_run(self.ubuntu_host, self.ubuntu_user)
350
351     def test_intall_fedora(self):
352         self.t_install(self.fedora_host, self.fedora_user)
353
354     def test_install_ubuntu(self):
355         self.t_install(self.ubuntu_host, self.ubuntu_user)
356
357     def test_compile_fedora(self):
358         self.t_compile(self.fedora_host, self.fedora_user)
359
360     def test_compile_ubuntu(self):
361         self.t_compile(self.ubuntu_host, self.ubuntu_user)
362
363     def test_exitcode_ok_fedora(self):
364         self.t_exitcode_ok(self.fedora_host, self.fedora_user)
365
366     def test_exitcode_ok_ubuntu(self):
367         self.t_exitcode_ok(self.ubuntu_host, self.ubuntu_user)
368
369     def test_exitcode_kill_fedora(self):
370         self.t_exitcode_kill(self.fedora_host, self.fedora_user)
371
372     def test_exitcode_kill_ubuntu(self):
373         self.t_exitcode_kill(self.ubuntu_host, self.ubuntu_user)
374
375     def test_exitcode_error_fedora(self):
376         self.t_exitcode_error(self.fedora_host, self.fedora_user)
377
378     def test_exitcode_error_ubuntu(self):
379         self.t_exitcode_error(self.ubuntu_host, self.ubuntu_user)
380
381     def test_clean_fedora(self):
382         self.t_clean(self.fedora_host, self.fedora_user)
383
384     def test_clean_ubuntu(self):
385         self.t_clean(self.ubuntu_host, self.ubuntu_user)
386      
387     @skipInteractive
388     def test_xterm_ubuntu(self):
389         """ Interactive test. Should not run automatically """
390         self.t_xterm(self.ubuntu_host, self.ubuntu_user)
391
392     def test_copy_files_fedora(self):
393         self.t_copy_files(self.fedora_host, self.fedora_user)
394
395     def test_copy_files_ubuntu(self):
396         self.t_copy_files(self.ubuntu_host, self.ubuntu_user)
397
398 if __name__ == '__main__':
399     unittest.main()
400