#!/usr/bin/env python
-from neco.resources.linux.node import LinuxNode
-from neco.design.box import Box
-from neco.util.sshfuncs import RUNNING, FINISHED
-
-import os.path
+#
+# NEPI, a framework to manage network experiments
+# Copyright (C) 2013 INRIA
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 2 as
+# published by the Free Software Foundation;
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+# Author: Alina Quereilhac <alina.quereilhac@inria.fr>
+
+
+from nepi.resources.linux.node import LinuxNode, ExitCode
+from nepi.util.sshfuncs import ProcStatus
+
+from test_utils import skipIfNotAlive, skipInteractive, create_node
+
+import shutil
+import os
import time
+import tempfile
import unittest
-
-class DummyEC(object):
- pass
-
-class LinuxBoxTestCase(unittest.TestCase):
+class LinuxNodeTestCase(unittest.TestCase):
def setUp(self):
- host = 'nepi2.pl.sophia.inria.fr'
- user = 'inria_nepi'
- self.node_fedora = self.create_node(host, user)
+ self.fedora_host = "fedora.pl.sophia.inria.fr"
+ self.fedora_user = "inria_nepi"
- host = 'roseval.pl.sophia.inria.fr'
- user = 'alina'
- self.node_ubuntu = self.create_node(host, user)
+ self.ubuntu_host = "ubuntu.pl.sophia.inria.fr"
+ self.ubuntu_user = "inria_nepi"
- self.target = 'nepi5.pl.sophia.inria.fr'
- self.home = '${HOME}/test-app'
+ self.target = "nepi.pl.sophia.inria.fr"
- def create_node(self, host, user):
- box = Box()
- ec = DummyEC()
+ @skipIfNotAlive
+ def t_execute(self, host, user):
+ node, ec = create_node(host, user)
- node = LinuxNode(box, ec)
- node.host = host
- node.user = user
+ command = "ping -qc3 %s" % self.target
+
+ (out, err), proc = node.execute(command)
- return node
+ expected = """3 packets transmitted, 3 received, 0% packet loss"""
- def t_xterm(self, node, target):
- if not node.is_alive():
- print "*** WARNING: Skipping test: Node %s is not alive\n" % (node.host)
- return
+ self.assertTrue(out.find(expected) > 0)
- node.enable_x11 = True
+ @skipIfNotAlive
+ def t_run(self, host, user):
+ node, ec = create_node(host, user)
+
+ node.find_home()
+ app_home = os.path.join(node.exp_home, "my-app")
+ node.mkdir(app_home, clean = True)
+
+ command = "ping %s" % self.target
+ node.run(command, app_home)
+ pid, ppid = node.getpid(app_home)
- node.install('xterm')
+ status = node.status(pid, ppid)
+ self.assertTrue(status, ProcStatus.RUNNING)
- out = node.execute('xterm')
+ node.kill(pid, ppid)
+ status = node.status(pid, ppid)
+ self.assertTrue(status, ProcStatus.FINISHED)
+
+ (out, err), proc = node.check_output(app_home, "stdout")
- node.uninstall('xterm')
+ expected = """64 bytes from"""
- self.assertEquals(out, "")
+ self.assertTrue(out.find(expected) > 0)
- def t_execute(self, node, target):
- if not node.is_alive():
- print "*** WARNING: Skipping test: Node %s is not alive\n" % (node.host)
- return
+ node.rmdir(app_home)
- command = "ping -qc3 %s" % target
- out = node.execute(command)
+ @skipIfNotAlive
+ def t_exitcode_ok(self, host, user):
+ command = "echo 'OK!'"
+
+ node, ec = create_node(host, user)
+
+ node.find_home()
+ app_home = os.path.join(node.exp_home, "my-app")
+ node.mkdir(app_home, clean = True)
+
+ (out, err), proc = node.run_and_wait(command, app_home,
+ shfile = "cmd.sh",
+ pidfile = "pid",
+ ecodefile = "exitcode",
+ stdout = "stdout",
+ stderr = "stderr",
+ raise_on_error = True)
+
+ # get the pid of the process
+ ecode = node.exitcode(app_home)
+ self.assertEqual(ecode, ExitCode.OK)
+
+ @skipIfNotAlive
+ def t_exitcode_kill(self, host, user):
+ node, ec = create_node(host, user)
+
+ node.find_home()
+ app_home = os.path.join(node.exp_home, "my-app")
+ node.mkdir(app_home, clean = True)
+
+ # Upload command that will not finish
+ command = "ping localhost"
+ shfile = os.path.join(app_home, "cmd.sh")
+ (out, err), proc = node.upload_command(command,
+ shfile = shfile,
+ ecodefile = "exitcode")
+
+ (out, err), proc = node.run(command, app_home,
+ pidfile = "pidfile",
+ stdout = "stdout",
+ stderr = "stderr")
+
+ # Just wait to make sure the ping started
+ time.sleep(5)
+
+ # The process is still running, so no retfile has been created yet
+ ecode = node.exitcode(app_home)
+ self.assertEqual(ecode, ExitCode.FILENOTFOUND)
+
+ (out, err), proc = node.check_errors(app_home)
+ self.assertEqual(err, "")
+
+ # Now kill the app
+ pid, ppid = node.getpid(app_home)
+ node.kill(pid, ppid)
+
+ (out, err), proc = node.check_errors(app_home)
+ self.assertEqual(err, "")
+
+ @skipIfNotAlive
+ def t_exitcode_error(self, host, user):
+ # Try to execute a command that doesn't exist
+ command = "unexistent-command"
+
+ node, ec = create_node(host, user)
+
+ node.find_home()
+ app_home = os.path.join(node.exp_home, "my-app")
+ node.mkdir(app_home, clean = True)
+
+ (out, err), proc = node.run_and_wait(command, app_home,
+ shfile = "cmd.sh",
+ pidfile = "pid",
+ ecodefile = "exitcode",
+ stdout = "stdout",
+ stderr = "stderr",
+ raise_on_error = False)
+
+ # get the pid of the process
+ ecode = node.exitcode(app_home)
+
+ # bash erro 127 - command not found
+ self.assertEqual(ecode, 127)
+
+ (out, err), proc = node.check_errors(app_home)
+
+ self.assertTrue(err.find("cmd.sh: line 1: unexistent-command: command not found") > -1)
+
+ @skipIfNotAlive
+ def t_install(self, host, user):
+ node, ec = create_node(host, user)
+
+ node.find_home()
+ (out, err), proc = node.mkdir(node.node_home, clean = True)
+ self.assertEqual(err, "")
+
+ (out, err), proc = node.install_packages("gcc", node.node_home)
+ self.assertEqual(err, "")
+
+ (out, err), proc = node.remove_packages("gcc", node.node_home)
+ self.assertEqual(err, "")
+
+ (out, err), proc = node.rmdir(node.exp_home)
+ self.assertEqual(err, "")
+
+ @skipIfNotAlive
+ def t_clean(self, host, user):
+ node, ec = create_node(host, user)
+
+ node.find_home()
+ node.mkdir(node.lib_dir)
+ node.mkdir(node.node_home)
+
+ command1 = " [ -d %s ] && echo 'Found'" % node.lib_dir
+ (out, err), proc = node.execute(command1)
+
+ self.assertEqual(out.strip(), "Found")
+
+ command2 = " [ -d %s ] && echo 'Found'" % node.node_home
+ (out, err), proc = node.execute(command2)
+
+ self.assertEqual(out.strip(), "Found")
+
+ node.clean_experiment()
+
+ (out, err), proc = node.execute(command2)
- expected = """3 packets transmitted, 3 received, 0% packet loss"""
+ self.assertEqual(out.strip(), "")
- self.assertTrue(out.find(expected) > 0)
+ node.clean_home()
+
+ (out, err), proc = node.execute(command1)
- def t_run(self, node, target):
- if not node.is_alive():
- print "*** WARNING: Skipping test: Node %s is not alive\n" % (node.host)
- return
+ self.assertEqual(out.strip(), "")
- node.mkdir(self.home, clean = True)
-
- command = "ping %s" % target
- dst = os.path.join(self.home, "app.sh")
- node.upload(command, dst)
-
- cmd = "bash ./app.sh"
- node.run(cmd, self.home)
- pid, ppid = node.checkpid(self.home)
+ @skipIfNotAlive
+ def t_xterm(self, host, user):
+ node, ec = create_node(host, user)
- status = node.status(pid, ppid)
- self.assertTrue(status, RUNNING)
+ node.find_home()
+ (out, err), proc = node.mkdir(node.node_home, clean = True)
+ self.assertEqual(err, "")
+
+ node.install_packages("xterm", node.node_home)
+ self.assertEqual(err, "")
- node.kill(pid, ppid)
- status = node.status(pid, ppid)
- self.assertTrue(status, FINISHED)
+ (out, err), proc = node.execute("xterm", forward_x11 = True)
+ self.assertEqual(err, "")
- node.rmdir(self.home)
+ (out, err), proc = node.remove_packages("xterm", node.node_home)
+ self.assertEqual(err, "")
- def t_install(self, node, target):
- if not node.is_alive():
- print "*** WARNING: Skipping test: Node %s is not alive\n" % (node.host)
- return
+ @skipIfNotAlive
+ def t_compile(self, host, user):
+ node, ec = create_node(host, user)
- node.mkdir(self.home, clean = True)
+ node.find_home()
+ app_home = os.path.join(node.exp_home, "my-app")
+ node.mkdir(app_home, clean = True)
prog = """#include <stdio.h>
return 0;
}
"""
- dst = os.path.join(self.home, "hello.c")
- node.upload(prog, dst)
+ # upload the test program
+ dst = os.path.join(app_home, "hello.c")
+ node.upload(prog, dst, text = True)
+
+ # install gcc
+ node.install_packages('gcc', app_home)
- node.install('gcc')
+ # compile the program using gcc
+ command = "cd %s; gcc -Wall hello.c -o hello" % app_home
+ (out, err), proc = node.execute(command)
- command = "cd %s; gcc -Wall hello.c -o hello" % self.home
- out = node.execute(command)
+ # execute the program and get the output from stdout
+ command = "%s/hello" % app_home
+ (out, err), proc = node.execute(command)
- command = "%s/hello" % self.home
- out = node.execute(command)
+ self.assertEqual(out, "Hello, world!\n")
- node.uninstall('gcc')
- node.rmdir(self.home)
+ # execute the program and get the output from a file
+ command = "%(home)s/hello > %(home)s/hello.out" % {
+ 'home': app_home}
+ (out, err), proc = node.execute(command)
- self.assertEquals(out, "Hello, world!\n")
+ # retrieve the output file
+ src = os.path.join(app_home, "hello.out")
+ f = tempfile.NamedTemporaryFile(delete = False)
+ dst = f.name
+ node.download(src, dst)
+ f.close()
+
+ node.remove_packages("gcc", app_home)
+ node.rmdir(app_home)
+
+ with open(dst, "r") as f:
+ out = f.read()
+
+ self.assertEqual(out, "Hello, world!\n")
+
+ @skipIfNotAlive
+ def t_copy_files(self, host, user):
+ node, ec = create_node(host, user)
+
+ node.find_home()
+ app_home = os.path.join(node.exp_home, "my-app")
+ node.mkdir(app_home, clean = True)
+
+ # create some temp files and directories to copy
+ dirpath = tempfile.mkdtemp()
+ f = tempfile.NamedTemporaryFile(dir=dirpath, delete=False)
+ f.close()
+
+ f1 = tempfile.NamedTemporaryFile(delete=False)
+ f1.close()
+ f1.name
+
+ source = [dirpath, f1.name]
+ destdir = "test"
+ node.mkdir(destdir, clean = True)
+ dest = "%s@%s:test" % (user, host)
+ node.copy(source, dest)
+
+ command = "ls %s" % destdir
+
+ (out, err), proc = node.execute(command)
+
+ os.remove(f1.name)
+ shutil.rmtree(dirpath)
+
+ self.assertTrue(out.find(os.path.basename(dirpath)) > -1)
+ self.assertTrue(out.find(os.path.basename(f1.name)) > -1)
+
+ f2 = tempfile.NamedTemporaryFile(delete=False)
+ f2.close()
+ f2.name
+
+ node.mkdir(destdir, clean = True)
+ dest = "%s@%s:test" % (user, host)
+ node.copy(f2.name, dest)
+
+ command = "ls %s" % destdir
+
+ (out, err), proc = node.execute(command)
+
+ os.remove(f2.name)
+
+ self.assertTrue(out.find(os.path.basename(f2.name)) > -1)
def test_execute_fedora(self):
- self.t_execute(self.node_fedora, self.target)
+ self.t_execute(self.fedora_host, self.fedora_user)
def test_execute_ubuntu(self):
- self.t_execute(self.node_ubuntu, self.target)
+ self.t_execute(self.ubuntu_host, self.ubuntu_user)
def test_run_fedora(self):
- self.t_run(self.node_fedora, self.target)
+ self.t_run(self.fedora_host, self.fedora_user)
def test_run_ubuntu(self):
- self.t_run(self.node_ubuntu, self.target)
+ self.t_run(self.ubuntu_host, self.ubuntu_user)
def test_intall_fedora(self):
- self.t_install(self.node_fedora, self.target)
+ self.t_install(self.fedora_host, self.fedora_user)
def test_install_ubuntu(self):
- self.t_install(self.node_ubuntu, self.target)
+ self.t_install(self.ubuntu_host, self.ubuntu_user)
+
+ def test_compile_fedora(self):
+ self.t_compile(self.fedora_host, self.fedora_user)
+
+ def test_compile_ubuntu(self):
+ self.t_compile(self.ubuntu_host, self.ubuntu_user)
+
+ def test_exitcode_ok_fedora(self):
+ self.t_exitcode_ok(self.fedora_host, self.fedora_user)
- def xtest_xterm_fedora(self):
- """ PlanetLab doesn't currently support X11 forwarding.
- Interactive test. Should not run automatically """
- self.t_xterm(self.node_fedora, self.target)
+ def test_exitcode_ok_ubuntu(self):
+ self.t_exitcode_ok(self.ubuntu_host, self.ubuntu_user)
- def xtest_xterm_ubuntu(self):
+ def test_exitcode_kill_fedora(self):
+ self.t_exitcode_kill(self.fedora_host, self.fedora_user)
+
+ def test_exitcode_kill_ubuntu(self):
+ self.t_exitcode_kill(self.ubuntu_host, self.ubuntu_user)
+
+ def test_exitcode_error_fedora(self):
+ self.t_exitcode_error(self.fedora_host, self.fedora_user)
+
+ def test_exitcode_error_ubuntu(self):
+ self.t_exitcode_error(self.ubuntu_host, self.ubuntu_user)
+
+ def test_clean_fedora(self):
+ self.t_clean(self.fedora_host, self.fedora_user)
+
+ def test_clean_ubuntu(self):
+ self.t_clean(self.ubuntu_host, self.ubuntu_user)
+
+ @skipInteractive
+ def test_xterm_ubuntu(self):
""" Interactive test. Should not run automatically """
- self.t_xterm(self.node_ubuntu, self.target)
+ self.t_xterm(self.ubuntu_host, self.ubuntu_user)
+
+ def test_copy_files_fedora(self):
+ self.t_copy_files(self.fedora_host, self.fedora_user)
+ def test_copy_files_ubuntu(self):
+ self.t_copy_files(self.ubuntu_host, self.ubuntu_user)
if __name__ == '__main__':
unittest.main()