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 as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
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.
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/>.
19 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
22 from nepi.resources.linux.node import LinuxNode, ExitCode
23 from nepi.util.sshfuncs import ProcStatus
25 from test_utils import skipIfNotAlive, skipInteractive, create_node
33 class LinuxNodeTestCase(unittest.TestCase):
35 self.fedora_host = "nepi2.pl.sophia.inria.fr"
36 self.fedora_user = "inria_nepi"
38 self.ubuntu_host = "roseval.pl.sophia.inria.fr"
39 self.ubuntu_user = "inria_nepi"
41 self.target = "nepi5.pl.sophia.inria.fr"
44 def t_execute(self, host, user):
45 node, ec = create_node(host, user)
47 command = "ping -qc3 %s" % self.target
49 (out, err), proc = node.execute(command)
51 expected = """3 packets transmitted, 3 received, 0% packet loss"""
53 self.assertTrue(out.find(expected) > 0)
56 def t_run(self, host, user):
57 node, ec = create_node(host, user)
60 app_home = os.path.join(node.exp_home, "my-app")
61 node.mkdir(app_home, clean = True)
63 command = "ping %s" % self.target
64 node.run(command, app_home)
65 pid, ppid = node.getpid(app_home)
67 status = node.status(pid, ppid)
68 self.assertTrue(status, ProcStatus.RUNNING)
71 status = node.status(pid, ppid)
72 self.assertTrue(status, ProcStatus.FINISHED)
74 (out, err), proc = node.check_output(app_home, "stdout")
76 expected = """64 bytes from"""
78 self.assertTrue(out.find(expected) > 0)
83 def t_exitcode_ok(self, host, user):
84 command = "echo 'OK!'"
86 node, ec = create_node(host, user)
89 app_home = os.path.join(node.exp_home, "my-app")
90 node.mkdir(app_home, clean = True)
92 (out, err), proc = node.run_and_wait(command, app_home,
95 ecodefile = "exitcode",
98 raise_on_error = True)
100 # get the pid of the process
101 ecode = node.exitcode(app_home)
102 self.assertEquals(ecode, ExitCode.OK)
105 def t_exitcode_kill(self, host, user):
106 node, ec = create_node(host, user)
109 app_home = os.path.join(node.exp_home, "my-app")
110 node.mkdir(app_home, clean = True)
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,
117 ecodefile = "exitcode")
119 (out, err), proc = node.run(command, app_home,
124 # Just wait to make sure the ping started
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)
131 (out, err), proc = node.check_errors(app_home)
132 self.assertEquals(err, "")
135 pid, ppid = node.getpid(app_home)
138 (out, err), proc = node.check_errors(app_home)
139 self.assertEquals(err, "")
142 def t_exitcode_error(self, host, user):
143 # Try to execute a command that doesn't exist
144 command = "unexistent-command"
146 node, ec = create_node(host, user)
149 app_home = os.path.join(node.exp_home, "my-app")
150 node.mkdir(app_home, clean = True)
152 (out, err), proc = node.run_and_wait(command, app_home,
155 ecodefile = "exitcode",
158 raise_on_error = False)
160 # get the pid of the process
161 ecode = node.exitcode(app_home)
163 # bash erro 127 - command not found
164 self.assertEquals(ecode, 127)
166 (out, err), proc = node.check_errors(app_home)
168 self.assertTrue(err.find("cmd.sh: line 1: unexistent-command: command not found") > -1)
171 def t_install(self, host, user):
172 node, ec = create_node(host, user)
175 (out, err), proc = node.mkdir(node.node_home, clean = True)
176 self.assertEquals(err, "")
178 (out, err), proc = node.install_packages("gcc", node.node_home)
179 self.assertEquals(err, "")
181 (out, err), proc = node.remove_packages("gcc", node.node_home)
182 self.assertEquals(err, "")
184 (out, err), proc = node.rmdir(node.exp_home)
185 self.assertEquals(err, "")
188 def t_clean(self, host, user):
189 node, ec = create_node(host, user)
192 node.mkdir(node.lib_dir)
193 node.mkdir(node.node_home)
195 command1 = " [ -d %s ] && echo 'Found'" % node.lib_dir
196 (out, err), proc = node.execute(command1)
198 self.assertEquals(out.strip(), "Found")
200 command2 = " [ -d %s ] && echo 'Found'" % node.node_home
201 (out, err), proc = node.execute(command2)
203 self.assertEquals(out.strip(), "Found")
205 node.clean_experiment()
207 (out, err), proc = node.execute(command2)
209 self.assertEquals(out.strip(), "")
213 (out, err), proc = node.execute(command1)
215 self.assertEquals(out.strip(), "")
218 def t_xterm(self, host, user):
219 node, ec = create_node(host, user)
222 (out, err), proc = node.mkdir(node.node_home, clean = True)
223 self.assertEquals(err, "")
225 node.install_packages("xterm", node.node_home)
226 self.assertEquals(err, "")
228 (out, err), proc = node.execute("xterm", forward_x11 = True)
229 self.assertEquals(err, "")
231 (out, err), proc = node.remove_packages("xterm", node.node_home)
232 self.assertEquals(err, "")
235 def t_compile(self, host, user):
236 node, ec = create_node(host, user)
239 app_home = os.path.join(node.exp_home, "my-app")
240 node.mkdir(app_home, clean = True)
242 prog = """#include <stdio.h>
247 printf ("Hello, world!\\n");
251 # upload the test program
252 dst = os.path.join(app_home, "hello.c")
253 node.upload(prog, dst, text = True)
256 node.install_packages('gcc', app_home)
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)
262 # execute the program and get the output from stdout
263 command = "%s/hello" % app_home
264 (out, err), proc = node.execute(command)
266 self.assertEquals(out, "Hello, world!\n")
268 # execute the program and get the output from a file
269 command = "%(home)s/hello > %(home)s/hello.out" % {
271 (out, err), proc = node.execute(command)
273 # retrieve the output file
274 src = os.path.join(app_home, "hello.out")
275 f = tempfile.NamedTemporaryFile(delete = False)
277 node.download(src, dst)
280 node.remove_packages("gcc", app_home)
287 self.assertEquals(out, "Hello, world!\n")
290 def t_copy_files(self, host, user):
291 node, ec = create_node(host, user)
294 app_home = os.path.join(node.exp_home, "my-app")
295 node.mkdir(app_home, clean = True)
297 # create some temp files and directories to copy
298 dirpath = tempfile.mkdtemp()
299 f = tempfile.NamedTemporaryFile(dir=dirpath, delete=False)
302 f1 = tempfile.NamedTemporaryFile(delete=False)
306 source = [dirpath, f1.name]
308 node.mkdir(destdir, clean = True)
309 dest = "%s@%s:test" % (user, host)
310 node.copy(source, dest)
312 command = "ls %s" % destdir
314 (out, err), proc = node.execute(command)
317 shutil.rmtree(dirpath)
319 self.assertTrue(out.find(os.path.basename(dirpath)) > -1)
320 self.assertTrue(out.find(os.path.basename(f1.name)) > -1)
322 f2 = tempfile.NamedTemporaryFile(delete=False)
326 node.mkdir(destdir, clean = True)
327 dest = "%s@%s:test" % (user, host)
328 node.copy(f2.name, dest)
330 command = "ls %s" % destdir
332 (out, err), proc = node.execute(command)
336 self.assertTrue(out.find(os.path.basename(f2.name)) > -1)
338 def test_execute_fedora(self):
339 self.t_execute(self.fedora_host, self.fedora_user)
341 def test_execute_ubuntu(self):
342 self.t_execute(self.ubuntu_host, self.ubuntu_user)
344 def test_run_fedora(self):
345 self.t_run(self.fedora_host, self.fedora_user)
347 def test_run_ubuntu(self):
348 self.t_run(self.ubuntu_host, self.ubuntu_user)
350 def test_intall_fedora(self):
351 self.t_install(self.fedora_host, self.fedora_user)
353 def test_install_ubuntu(self):
354 self.t_install(self.ubuntu_host, self.ubuntu_user)
356 def test_compile_fedora(self):
357 self.t_compile(self.fedora_host, self.fedora_user)
359 def test_compile_ubuntu(self):
360 self.t_compile(self.ubuntu_host, self.ubuntu_user)
362 def test_exitcode_ok_fedora(self):
363 self.t_exitcode_ok(self.fedora_host, self.fedora_user)
365 def test_exitcode_ok_ubuntu(self):
366 self.t_exitcode_ok(self.ubuntu_host, self.ubuntu_user)
368 def test_exitcode_kill_fedora(self):
369 self.t_exitcode_kill(self.fedora_host, self.fedora_user)
371 def test_exitcode_kill_ubuntu(self):
372 self.t_exitcode_kill(self.ubuntu_host, self.ubuntu_user)
374 def test_exitcode_error_fedora(self):
375 self.t_exitcode_error(self.fedora_host, self.fedora_user)
377 def test_exitcode_error_ubuntu(self):
378 self.t_exitcode_error(self.ubuntu_host, self.ubuntu_user)
380 def test_clean_fedora(self):
381 self.t_clean(self.fedora_host, self.fedora_user)
383 def test_clean_ubuntu(self):
384 self.t_clean(self.ubuntu_host, self.ubuntu_user)
387 def test_xterm_ubuntu(self):
388 """ Interactive test. Should not run automatically """
389 self.t_xterm(self.ubuntu_host, self.ubuntu_user)
391 def test_copy_files_fedora(self):
392 self.t_copy_files(self.fedora_host, self.fedora_user)
394 def test_copy_files_ubuntu(self):
395 self.t_copy_files(self.ubuntu_host, self.ubuntu_user)
397 if __name__ == '__main__':