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