3 # NEPI, a framework to manage network experiments
4 # Copyright (C) 2013 INRIA
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;
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.
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/>.
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
21 from nepi.resources.linux.node import LinuxNode, ExitCode
22 from nepi.util.sshfuncs import ProcStatus
24 from test_utils import skipIfNotAlive, skipInteractive, create_node
32 class LinuxNodeTestCase(unittest.TestCase):
34 self.fedora_host = "nepi2.pl.sophia.inria.fr"
35 self.fedora_user = "inria_nepi"
37 self.ubuntu_host = "roseval.pl.sophia.inria.fr"
38 self.ubuntu_user = "inria_nepi"
40 self.target = "nepi5.pl.sophia.inria.fr"
43 def t_execute(self, host, user):
44 node, ec = create_node(host, user)
46 command = "ping -qc3 %s" % self.target
48 (out, err), proc = node.execute(command)
50 expected = """3 packets transmitted, 3 received, 0% packet loss"""
52 self.assertTrue(out.find(expected) > 0)
55 def t_run(self, host, user):
56 node, ec = create_node(host, user)
59 app_home = os.path.join(node.exp_home, "my-app")
60 node.mkdir(app_home, clean = True)
62 command = "ping %s" % self.target
63 node.run(command, app_home)
64 pid, ppid = node.getpid(app_home)
66 status = node.status(pid, ppid)
67 self.assertTrue(status, ProcStatus.RUNNING)
70 status = node.status(pid, ppid)
71 self.assertTrue(status, ProcStatus.FINISHED)
73 (out, err), proc = node.check_output(app_home, "stdout")
75 expected = """64 bytes from"""
77 self.assertTrue(out.find(expected) > 0)
82 def t_exitcode_ok(self, host, user):
83 command = "echo 'OK!'"
85 node, ec = create_node(host, user)
88 app_home = os.path.join(node.exp_home, "my-app")
89 node.mkdir(app_home, clean = True)
91 (out, err), proc = node.run_and_wait(command, app_home,
94 ecodefile = "exitcode",
97 raise_on_error = True)
99 # get the pid of the process
100 ecode = node.exitcode(app_home)
101 self.assertEqual(ecode, ExitCode.OK)
104 def t_exitcode_kill(self, host, user):
105 node, ec = create_node(host, user)
108 app_home = os.path.join(node.exp_home, "my-app")
109 node.mkdir(app_home, clean = True)
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,
116 ecodefile = "exitcode")
118 (out, err), proc = node.run(command, app_home,
123 # Just wait to make sure the ping started
126 # The process is still running, so no retfile has been created yet
127 ecode = node.exitcode(app_home)
128 self.assertEqual(ecode, ExitCode.FILENOTFOUND)
130 (out, err), proc = node.check_errors(app_home)
131 self.assertEqual(err, "")
134 pid, ppid = node.getpid(app_home)
137 (out, err), proc = node.check_errors(app_home)
138 self.assertEqual(err, "")
141 def t_exitcode_error(self, host, user):
142 # Try to execute a command that doesn't exist
143 command = "unexistent-command"
145 node, ec = create_node(host, user)
148 app_home = os.path.join(node.exp_home, "my-app")
149 node.mkdir(app_home, clean = True)
151 (out, err), proc = node.run_and_wait(command, app_home,
154 ecodefile = "exitcode",
157 raise_on_error = False)
159 # get the pid of the process
160 ecode = node.exitcode(app_home)
162 # bash erro 127 - command not found
163 self.assertEqual(ecode, 127)
165 (out, err), proc = node.check_errors(app_home)
167 self.assertTrue(err.find("cmd.sh: line 1: unexistent-command: command not found") > -1)
170 def t_install(self, host, user):
171 node, ec = create_node(host, user)
174 (out, err), proc = node.mkdir(node.node_home, clean = True)
175 self.assertEqual(err, "")
177 (out, err), proc = node.install_packages("gcc", node.node_home)
178 self.assertEqual(err, "")
180 (out, err), proc = node.remove_packages("gcc", node.node_home)
181 self.assertEqual(err, "")
183 (out, err), proc = node.rmdir(node.exp_home)
184 self.assertEqual(err, "")
187 def t_clean(self, host, user):
188 node, ec = create_node(host, user)
191 node.mkdir(node.lib_dir)
192 node.mkdir(node.node_home)
194 command1 = " [ -d %s ] && echo 'Found'" % node.lib_dir
195 (out, err), proc = node.execute(command1)
197 self.assertEqual(out.strip(), "Found")
199 command2 = " [ -d %s ] && echo 'Found'" % node.node_home
200 (out, err), proc = node.execute(command2)
202 self.assertEqual(out.strip(), "Found")
204 node.clean_experiment()
206 (out, err), proc = node.execute(command2)
208 self.assertEqual(out.strip(), "")
212 (out, err), proc = node.execute(command1)
214 self.assertEqual(out.strip(), "")
217 def t_xterm(self, host, user):
218 node, ec = create_node(host, user)
221 (out, err), proc = node.mkdir(node.node_home, clean = True)
222 self.assertEqual(err, "")
224 node.install_packages("xterm", node.node_home)
225 self.assertEqual(err, "")
227 (out, err), proc = node.execute("xterm", forward_x11 = True)
228 self.assertEqual(err, "")
230 (out, err), proc = node.remove_packages("xterm", node.node_home)
231 self.assertEqual(err, "")
234 def t_compile(self, host, user):
235 node, ec = create_node(host, user)
238 app_home = os.path.join(node.exp_home, "my-app")
239 node.mkdir(app_home, clean = True)
241 prog = """#include <stdio.h>
246 printf ("Hello, world!\\n");
250 # upload the test program
251 dst = os.path.join(app_home, "hello.c")
252 node.upload(prog, dst, text = True)
255 node.install_packages('gcc', app_home)
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)
261 # execute the program and get the output from stdout
262 command = "%s/hello" % app_home
263 (out, err), proc = node.execute(command)
265 self.assertEqual(out, "Hello, world!\n")
267 # execute the program and get the output from a file
268 command = "%(home)s/hello > %(home)s/hello.out" % {
270 (out, err), proc = node.execute(command)
272 # retrieve the output file
273 src = os.path.join(app_home, "hello.out")
274 f = tempfile.NamedTemporaryFile(delete = False)
276 node.download(src, dst)
279 node.remove_packages("gcc", app_home)
286 self.assertEqual(out, "Hello, world!\n")
289 def t_copy_files(self, host, user):
290 node, ec = create_node(host, user)
293 app_home = os.path.join(node.exp_home, "my-app")
294 node.mkdir(app_home, clean = True)
296 # create some temp files and directories to copy
297 dirpath = tempfile.mkdtemp()
298 f = tempfile.NamedTemporaryFile(dir=dirpath, delete=False)
301 f1 = tempfile.NamedTemporaryFile(delete=False)
305 source = [dirpath, f1.name]
307 node.mkdir(destdir, clean = True)
308 dest = "%s@%s:test" % (user, host)
309 node.copy(source, dest)
311 command = "ls %s" % destdir
313 (out, err), proc = node.execute(command)
316 shutil.rmtree(dirpath)
318 self.assertTrue(out.find(os.path.basename(dirpath)) > -1)
319 self.assertTrue(out.find(os.path.basename(f1.name)) > -1)
321 f2 = tempfile.NamedTemporaryFile(delete=False)
325 node.mkdir(destdir, clean = True)
326 dest = "%s@%s:test" % (user, host)
327 node.copy(f2.name, dest)
329 command = "ls %s" % destdir
331 (out, err), proc = node.execute(command)
335 self.assertTrue(out.find(os.path.basename(f2.name)) > -1)
337 def test_execute_fedora(self):
338 self.t_execute(self.fedora_host, self.fedora_user)
340 def test_execute_ubuntu(self):
341 self.t_execute(self.ubuntu_host, self.ubuntu_user)
343 def test_run_fedora(self):
344 self.t_run(self.fedora_host, self.fedora_user)
346 def test_run_ubuntu(self):
347 self.t_run(self.ubuntu_host, self.ubuntu_user)
349 def test_intall_fedora(self):
350 self.t_install(self.fedora_host, self.fedora_user)
352 def test_install_ubuntu(self):
353 self.t_install(self.ubuntu_host, self.ubuntu_user)
355 def test_compile_fedora(self):
356 self.t_compile(self.fedora_host, self.fedora_user)
358 def test_compile_ubuntu(self):
359 self.t_compile(self.ubuntu_host, self.ubuntu_user)
361 def test_exitcode_ok_fedora(self):
362 self.t_exitcode_ok(self.fedora_host, self.fedora_user)
364 def test_exitcode_ok_ubuntu(self):
365 self.t_exitcode_ok(self.ubuntu_host, self.ubuntu_user)
367 def test_exitcode_kill_fedora(self):
368 self.t_exitcode_kill(self.fedora_host, self.fedora_user)
370 def test_exitcode_kill_ubuntu(self):
371 self.t_exitcode_kill(self.ubuntu_host, self.ubuntu_user)
373 def test_exitcode_error_fedora(self):
374 self.t_exitcode_error(self.fedora_host, self.fedora_user)
376 def test_exitcode_error_ubuntu(self):
377 self.t_exitcode_error(self.ubuntu_host, self.ubuntu_user)
379 def test_clean_fedora(self):
380 self.t_clean(self.fedora_host, self.fedora_user)
382 def test_clean_ubuntu(self):
383 self.t_clean(self.ubuntu_host, self.ubuntu_user)
386 def test_xterm_ubuntu(self):
387 """ Interactive test. Should not run automatically """
388 self.t_xterm(self.ubuntu_host, self.ubuntu_user)
390 def test_copy_files_fedora(self):
391 self.t_copy_files(self.fedora_host, self.fedora_user)
393 def test_copy_files_ubuntu(self):
394 self.t_copy_files(self.ubuntu_host, self.ubuntu_user)
396 if __name__ == '__main__':