bb82107f6762765f98e2c0a36bb419c6a2e0226a
[nepi.git] / test / resources / linux / multirun.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 version 2 as
8 #    published by the Free Software Foundation;
9 #
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.
14 #
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/>.
17 #
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19
20 from nepi.execution.ec import ExperimentController 
21 from nepi.execution.runner import ExperimentRunner 
22
23 from test_utils import skipIfNotAlive, skipInteractive
24
25 import functools
26 import glob
27 import os
28 import re
29 import shutil
30 import time
31 import tempfile
32 import unittest
33
34 _ping_re = re.compile("[^/]+rtt min/avg/max/mdev = (?P<min>\d\.\d+)/(?P<avg>\d\.\d+)/(?P<max>\d\.\d+)/(?P<mdev>\d\.\d+)[^/]+", re.MULTILINE)
35
36 class LinuxMultiRunTestCase(unittest.TestCase):
37     def setUp(self):
38         self.fedora_host = "nepi2.pl.sophia.inria.fr"
39         self.fedora_user = "inria_nepi"
40
41         self.ubuntu_host = "roseval.pl.sophia.inria.fr"
42         self.ubuntu_user = "inria_nepi"
43         
44         self.target = "nepi5.pl.sophia.inria.fr"
45
46     @skipIfNotAlive
47     def t_simple_multirun(self, host, user, depends):
48
49         dirpath = tempfile.mkdtemp()
50
51         ec = ExperimentController(exp_id = "test-condition-multirun", 
52                 local_dir = dirpath)
53         
54         node = ec.register_resource("linux::Node")
55         ec.set(node, "hostname", host)
56         ec.set(node, "username", user)
57         ec.set(node, "cleanExperiment", True)
58         ec.set(node, "cleanProcesses", True)
59
60         ping = ec.register_resource("linux::Application")
61         ec.set(ping, "command", "ping -c10 nepi.inria.fr")
62         ec.register_connection(ping, node)
63
64         collector = ec.register_resource("Collector")
65         ec.set(collector, "traceName", "stdout")
66         ec.register_connection(ping, collector)
67
68         def compute_metric_callback(ping, ec, run):
69             stdout = ec.trace(ping, "stdout")
70
71             m = _ping_re.match(stdout)
72             if not m:
73                 return None
74             
75             return float(m.groupdict()["min"])
76
77         metric_callback = functools.partial(compute_metric_callback, ping)
78
79         rnr = ExperimentRunner()
80         runs = rnr.run(ec, min_runs = 5, 
81                 compute_metric_callback = metric_callback,
82                 wait_guids = [ping],
83                 wait_time = 0)
84
85         self.assertTrue(runs >= 5)
86
87         dircount = 0
88
89         for d in os.listdir(ec.exp_dir):
90             path = os.path.join(ec.exp_dir, d)
91             if os.path.isdir(path):
92                 dircount += 1
93                 logs = glob.glob(os.path.join(path, "*.stdout"))
94                 self.assertEquals(len(logs), 1)
95         
96         self.assertEquals(runs, dircount)
97
98         shutil.rmtree(dirpath)
99
100     def test_simple_multirun_fedora(self):
101         self.t_simple_multirun(self.fedora_host, self.fedora_user, "nc")
102
103     def test_simple_multirun_ubuntu(self):
104         self.t_simple_multirun(self.ubuntu_host, self.ubuntu_user, "netcat")
105
106 if __name__ == '__main__':
107     unittest.main()
108