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
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 = "alina"
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)
58 app_home = os.path.join(node.exp_home, "my-app")
59 node.mkdir(app_home, clean = True)
61 command = "ping %s" % self.target
62 node.run(command, app_home)
63 pid, ppid = node.getpid(app_home)
65 status = node.status(pid, ppid)
66 self.assertTrue(status, ProcStatus.RUNNING)
69 status = node.status(pid, ppid)
70 self.assertTrue(status, ProcStatus.FINISHED)
72 (out, err), proc = node.check_output(app_home, "stdout")
74 expected = """64 bytes from"""
76 self.assertTrue(out.find(expected) > 0)
81 def t_exitcode_ok(self, host, user):
82 command = "echo 'OK!'"
84 node, ec = create_node(host, user)
86 app_home = os.path.join(node.exp_home, "my-app")
87 node.mkdir(app_home, clean = True)
89 (out, err), proc = node.run_and_wait(command, app_home,
92 ecodefile = "exitcode",
95 raise_on_error = True)
97 # get the pid of the process
98 ecode = node.exitcode(app_home)
99 self.assertEquals(ecode, ExitCode.OK)
102 def t_exitcode_kill(self, host, user):
103 node, ec = create_node(host, user)
105 app_home = os.path.join(node.exp_home, "my-app")
106 node.mkdir(app_home, clean = True)
108 # Upload command that will not finish
109 command = "ping localhost"
110 (out, err), proc = node.upload_command(command, app_home,
112 ecodefile = "exitcode")
114 (out, err), proc = node.run(command, app_home,
119 # Just wait to make sure the ping started
122 # The process is still running, so no retfile has been created yet
123 ecode = node.exitcode(app_home)
124 self.assertEquals(ecode, ExitCode.FILENOTFOUND)
126 (out, err), proc = node.check_errors(app_home)
127 self.assertEquals(err, "")
130 pid, ppid = node.getpid(app_home)
133 (out, err), proc = node.check_errors(app_home)
134 self.assertEquals(err, "")
137 def t_exitcode_error(self, host, user):
138 # Try to execute a command that doesn't exist
139 command = "unexistent-command"
141 node, ec = create_node(host, user)
143 app_home = os.path.join(node.exp_home, "my-app")
144 node.mkdir(app_home, clean = True)
146 (out, err), proc = node.run_and_wait(command, app_home,
149 ecodefile = "exitcode",
152 raise_on_error = False)
154 # get the pid of the process
155 ecode = node.exitcode(app_home)
156 # bash erro 127 - command not found
157 self.assertEquals(ecode, 127)
159 (out, err), proc = node.check_errors(app_home)
160 self.assertNotEquals(out, "")
163 def t_install(self, host, user):
164 node, ec = create_node(host, user)
166 (out, err), proc = node.mkdir(node.node_home, clean = True)
167 self.assertEquals(out, "")
169 (out, err), proc = node.install_packages("gcc", node.node_home)
170 self.assertEquals(out, "")
172 (out, err), proc = node.remove_packages("gcc", node.node_home)
173 self.assertEquals(out, "")
175 (out, err), proc = node.rmdir(node.exp_home)
176 self.assertEquals(out, "")
179 def t_xterm(self, host, user):
180 node, ec = create_node(host, user)
182 (out, err), proc = node.mkdir(node.node_home, clean = True)
183 self.assertEquals(out, "")
185 node.install_packages("xterm", node.node_home)
186 self.assertEquals(out, "")
188 (out, err), proc = node.execute("xterm", forward_x11 = True)
189 self.assertEquals(out, "")
191 (out, err), proc = node.remove_packages("xterm", node.node_home)
192 self.assertEquals(out, "")
195 def t_compile(self, host, user):
196 node, ec = create_node(host, user)
198 app_home = os.path.join(node.exp_home, "my-app")
199 node.mkdir(app_home, clean = True)
201 prog = """#include <stdio.h>
206 printf ("Hello, world!\\n");
210 # upload the test program
211 dst = os.path.join(app_home, "hello.c")
212 node.upload(prog, dst, text = True)
215 node.install_packages('gcc', app_home)
217 # compile the program using gcc
218 command = "cd %s; gcc -Wall hello.c -o hello" % app_home
219 (out, err), proc = node.execute(command)
221 # execute the program and get the output from stdout
222 command = "%s/hello" % app_home
223 (out, err), proc = node.execute(command)
225 self.assertEquals(out, "Hello, world!\n")
227 # execute the program and get the output from a file
228 command = "%(home)s/hello > %(home)s/hello.out" % {
230 (out, err), proc = node.execute(command)
232 # retrieve the output file
233 src = os.path.join(app_home, "hello.out")
234 f = tempfile.NamedTemporaryFile(delete = False)
236 node.download(src, dst)
239 node.remove_packages("gcc", app_home)
246 self.assertEquals(out, "Hello, world!\n")
248 def test_execute_fedora(self):
249 self.t_execute(self.fedora_host, self.fedora_user)
251 def test_execute_ubuntu(self):
252 self.t_execute(self.ubuntu_host, self.ubuntu_user)
254 def test_run_fedora(self):
255 self.t_run(self.fedora_host, self.fedora_user)
257 def test_run_ubuntu(self):
258 self.t_run(self.ubuntu_host, self.ubuntu_user)
260 def test_intall_fedora(self):
261 self.t_install(self.fedora_host, self.fedora_user)
263 def test_install_ubuntu(self):
264 self.t_install(self.ubuntu_host, self.ubuntu_user)
266 def test_compile_fedora(self):
267 self.t_compile(self.fedora_host, self.fedora_user)
269 def test_compile_ubuntu(self):
270 self.t_compile(self.ubuntu_host, self.ubuntu_user)
272 def test_exitcode_ok_fedora(self):
273 self.t_exitcode_ok(self.fedora_host, self.fedora_user)
275 def test_exitcode_ok_ubuntu(self):
276 self.t_exitcode_ok(self.ubuntu_host, self.ubuntu_user)
278 def test_exitcode_kill_fedora(self):
279 self.t_exitcode_kill(self.fedora_host, self.fedora_user)
281 def test_exitcode_kill_ubuntu(self):
282 self.t_exitcode_kill(self.ubuntu_host, self.ubuntu_user)
284 def test_exitcode_error_fedora(self):
285 self.t_exitcode_error(self.fedora_host, self.fedora_user)
287 def test_exitcode_error_ubuntu(self):
288 self.t_exitcode_error(self.ubuntu_host, self.ubuntu_user)
291 def test_xterm_ubuntu(self):
292 """ Interactive test. Should not run automatically """
293 self.t_xterm(self.ubuntu_host, self.ubuntu_user)
296 if __name__ == '__main__':