Adding CCN RMs for Linux backend
[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 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_host = "planetlab2.u-strasbg.fr"
36         self.fedora_user = "inria_nepi"
37
38         self.ubuntu_host = "roseval.pl.sophia.inria.fr"
39         self.ubuntu_user = "alina"
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         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         app_home = os.path.join(node.exp_home, "my-app")
88         node.mkdir(app_home, clean = True)
89          
90         (out, err), proc = node.run_and_wait(command, app_home,
91             shfile = "cmd.sh",
92             pidfile = "pid",
93             ecodefile = "exitcode",
94             stdout = "stdout", 
95             stderr = "stderr",
96             raise_on_error = True)
97  
98         # get the pid of the process
99         ecode = node.exitcode(app_home)
100         self.assertEquals(ecode, ExitCode.OK)
101
102     @skipIfNotAlive
103     def t_exitcode_kill(self, host, user):
104         node, ec = create_node(host, user)
105          
106         app_home = os.path.join(node.exp_home, "my-app")
107         node.mkdir(app_home, clean = True)
108        
109         # Upload command that will not finish
110         command = "ping localhost"
111         (out, err), proc = node.upload_command(command, app_home, 
112             shfile = "cmd.sh",
113             ecodefile = "exitcode")
114
115         (out, err), proc = node.run(command, app_home,
116             pidfile = "pidfile",
117             stdout = "stdout", 
118             stderr = "stderr")
119  
120         # Just wait to make sure the ping started
121         time.sleep(5)
122
123         # The process is still running, so no retfile has been created yet
124         ecode = node.exitcode(app_home)
125         self.assertEquals(ecode, ExitCode.FILENOTFOUND)
126         
127         (out, err), proc = node.check_errors(app_home)
128         self.assertEquals(err, "")
129         
130         # Now kill the app
131         pid, ppid = node.getpid(app_home)
132         node.kill(pid, ppid)
133          
134         (out, err), proc = node.check_errors(app_home)
135         self.assertEquals(err, "")
136
137     @skipIfNotAlive
138     def t_exitcode_error(self, host, user):
139         # Try to execute a command that doesn't exist
140         command = "unexistent-command"
141         
142         node, ec = create_node(host, user)
143          
144         app_home = os.path.join(node.exp_home, "my-app")
145         node.mkdir(app_home, clean = True)
146          
147         (out, err), proc = node.run_and_wait(command, app_home,
148             shfile = "cmd.sh",
149             pidfile = "pid",
150             ecodefile = "exitcode",
151             stdout = "stdout", 
152             stderr = "stderr",
153             raise_on_error = False)
154  
155         # get the pid of the process
156         ecode = node.exitcode(app_home)
157
158         # bash erro 127 - command not found
159         self.assertEquals(ecode, 127)
160  
161         (out, err), proc = node.check_errors(app_home)
162
163         self.assertEquals(err.strip(), "./cmd.sh: line 1: unexistent-command: command not found")
164
165     @skipIfNotAlive
166     def t_install(self, host, user):
167         node, ec = create_node(host, user)
168
169         (out, err), proc = node.mkdir(node.node_home, clean = True)
170         self.assertEquals(err, "")
171
172         (out, err), proc = node.install_packages("gcc", node.node_home)
173         self.assertEquals(err, "")
174
175         (out, err), proc = node.remove_packages("gcc", node.node_home)
176         self.assertEquals(err, "")
177
178         (out, err), proc = node.rmdir(node.exp_home)
179         self.assertEquals(err, "")
180
181     @skipIfNotAlive
182     def t_xterm(self, host, user):
183         node, ec = create_node(host, user)
184
185         (out, err), proc = node.mkdir(node.node_home, clean = True)
186         self.assertEquals(err, "")
187         
188         node.install_packages("xterm", node.node_home)
189         self.assertEquals(err, "")
190
191         (out, err), proc = node.execute("xterm", forward_x11 = True)
192         self.assertEquals(err, "")
193
194         (out, err), proc = node.remove_packages("xterm", node.node_home)
195         self.assertEquals(err, "")
196
197     @skipIfNotAlive
198     def t_compile(self, host, user):
199         node, ec = create_node(host, user)
200
201         app_home = os.path.join(node.exp_home, "my-app")
202         node.mkdir(app_home, clean = True)
203
204         prog = """#include <stdio.h>
205
206 int
207 main (void)
208 {
209     printf ("Hello, world!\\n");
210     return 0;
211 }
212 """
213         # upload the test program
214         dst = os.path.join(app_home, "hello.c")
215         node.upload(prog, dst, text = True)
216
217         # install gcc
218         node.install_packages('gcc', app_home)
219
220         # compile the program using gcc
221         command = "cd %s; gcc -Wall hello.c -o hello" % app_home
222         (out, err), proc = node.execute(command)
223
224         # execute the program and get the output from stdout
225         command = "%s/hello" % app_home 
226         (out, err), proc = node.execute(command)
227
228         self.assertEquals(out, "Hello, world!\n")
229
230         # execute the program and get the output from a file
231         command = "%(home)s/hello > %(home)s/hello.out" % {
232                 'home': app_home}
233         (out, err), proc = node.execute(command)
234
235         # retrieve the output file 
236         src = os.path.join(app_home, "hello.out")
237         f = tempfile.NamedTemporaryFile(delete = False)
238         dst = f.name
239         node.download(src, dst)
240         f.close()
241
242         node.remove_packages("gcc", app_home)
243         node.rmdir(app_home)
244
245         f = open(dst, "r")
246         out = f.read()
247         f.close()
248         
249         self.assertEquals(out, "Hello, world!\n")
250
251     def test_execute_fedora(self):
252         self.t_execute(self.fedora_host, self.fedora_user)
253
254     def test_execute_ubuntu(self):
255         self.t_execute(self.ubuntu_host, self.ubuntu_user)
256
257     def test_run_fedora(self):
258         self.t_run(self.fedora_host, self.fedora_user)
259
260     def test_run_ubuntu(self):
261         self.t_run(self.ubuntu_host, self.ubuntu_user)
262
263     def test_intall_fedora(self):
264         self.t_install(self.fedora_host, self.fedora_user)
265
266     def test_install_ubuntu(self):
267         self.t_install(self.ubuntu_host, self.ubuntu_user)
268
269     def test_compile_fedora(self):
270         self.t_compile(self.fedora_host, self.fedora_user)
271
272     def test_compile_ubuntu(self):
273         self.t_compile(self.ubuntu_host, self.ubuntu_user)
274
275     def test_exitcode_ok_fedora(self):
276         self.t_exitcode_ok(self.fedora_host, self.fedora_user)
277
278     def test_exitcode_ok_ubuntu(self):
279         self.t_exitcode_ok(self.ubuntu_host, self.ubuntu_user)
280
281     def test_exitcode_kill_fedora(self):
282         self.t_exitcode_kill(self.fedora_host, self.fedora_user)
283
284     def test_exitcode_kill_ubuntu(self):
285         self.t_exitcode_kill(self.ubuntu_host, self.ubuntu_user)
286
287     def test_exitcode_error_fedora(self):
288         self.t_exitcode_error(self.fedora_host, self.fedora_user)
289
290     def test_exitcode_error_ubuntu(self):
291         self.t_exitcode_error(self.ubuntu_host, self.ubuntu_user)
292     
293     @skipInteractive
294     def test_xterm_ubuntu(self):
295         """ Interactive test. Should not run automatically """
296         self.t_xterm(self.ubuntu_host, self.ubuntu_user)
297
298
299 if __name__ == '__main__':
300     unittest.main()
301