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