44685d8c4b4db1e9916c4a2cff8b81e724eafe33
[nepi.git] / test / resources / linux / serialization.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.resource import ResourceState, ResourceAction
23 from nepi.execution.trace import TraceAttr
24
25 from test_utils import skipIfNotAlive, skipInteractive
26
27 import os
28 import shutil
29 import time
30 import tempfile
31 import unittest
32
33 class LinuxSerializationTestCase(unittest.TestCase):
34     def setUp(self):
35         self.fedora_host = "nepi2.pl.sophia.inria.fr"
36         self.fedora_user = "inria_nepi"
37
38         self.ubuntu_host = "roseval.pl.sophia.inria.fr"
39         self.ubuntu_user = "inria_nepi"
40         
41         self.target = "nepi5.pl.sophia.inria.fr"
42
43     @skipIfNotAlive
44     def t_condition_serialize(self, host, user, depends):
45
46         dirpath = tempfile.mkdtemp()
47
48         ec = ExperimentController(exp_id="test-condition-serial")
49         
50         node = ec.register_resource("linux::Node")
51         ec.set(node, "hostname", host)
52         ec.set(node, "username", user)
53         ec.set(node, "cleanExperiment", True)
54         ec.set(node, "cleanProcesses", True)
55
56         server = ec.register_resource("linux::Application")
57         cmd = "echo 'HOLA' | nc -l 3333"
58         ec.set(server, "command", cmd)
59         ec.set(server, "depends", depends)
60         ec.register_connection(server, node)
61
62         client = ec.register_resource("linux::Application")
63         cmd = "nc 127.0.0.1 3333"
64         ec.set(client, "command", cmd)
65         ec.register_connection(client, node)
66
67         ec.register_condition(client, ResourceAction.START, server, ResourceState.STARTED)
68
69         apps = [client, server]
70         
71         filepath = ec.save(dirpath)
72         
73         ec.deploy()
74
75         ec.wait_finished(apps)
76
77         self.assertTrue(ec.state(node) == ResourceState.STARTED)
78         self.assertTrue(ec.state(server) == ResourceState.STOPPED)
79         self.assertTrue(ec.state(client) == ResourceState.STOPPED)
80
81         stdout = ec.trace(client, "stdout")
82         self.assertTrue(stdout.strip() == "HOLA")
83
84         ec.shutdown()
85
86         # Load serialized experiment
87         ec2 = ExperimentController.load(filepath)
88         
89         ec2.deploy()
90         ec2.wait_finished(apps)
91         
92         self.assertEquals(len(ec.resources), len(ec2.resources))
93         
94         self.assertTrue(ec2.state(node) == ResourceState.STARTED)
95         self.assertTrue(ec2.state(server) == ResourceState.STOPPED)
96         self.assertTrue(ec2.state(client) == ResourceState.STOPPED)
97
98         stdout = ec2.trace(client, "stdout")
99
100         self.assertTrue(stdout.strip() == "HOLA")
101         
102         ec2.shutdown()
103
104         shutil.rmtree(dirpath)
105
106     def test_condition_serialize_fedora(self):
107         self.t_condition_serialize(self.fedora_host, self.fedora_user, "nc")
108
109     def test_condition_serialize_ubuntu(self):
110         self.t_condition_serialize(self.ubuntu_host, self.ubuntu_user, "netcat")
111
112 if __name__ == '__main__':
113     unittest.main()
114