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