Added unit tests for linux application
[nepi.git] / test / resources / linux / application.py
1 #!/usr/bin/env python
2 from neco.execution.ec import ExperimentController 
3 from neco.execution.resource import ResourceState, ResourceAction
4 from neco.execution.trace import TraceAttr
5 from neco.resources.linux.node import LinuxNode
6 from neco.resources.linux.application import LinuxApplication
7
8 from test_utils import skipIfNotAlive
9
10 import os
11 import time
12 import tempfile
13 import unittest
14
15 class LinuxApplicationTestCase(unittest.TestCase):
16     def setUp(self):
17         self.fedora_host = 'nepi2.pl.sophia.inria.fr'
18         self.fedora_user = 'inria_nepi'
19
20         self.ubuntu_host = 'roseval.pl.sophia.inria.fr'
21         self.ubuntu_user = 'alina'
22         
23         self.target = 'nepi5.pl.sophia.inria.fr'
24
25     @skipIfNotAlive
26     def t_stdout(self, host, user):
27         from neco.execution.resource import ResourceFactory
28         
29         ResourceFactory.register_type(LinuxNode)
30         ResourceFactory.register_type(LinuxApplication)
31
32         ec = ExperimentController()
33         
34         node = ec.register_resource("LinuxNode")
35         ec.set(node, "hostname", host)
36         ec.set(node, "username", user)
37         ec.set(node, "cleanHome", True)
38         ec.set(node, "cleanProcesses", True)
39
40         app = ec.register_resource("LinuxApplication")
41         cmd = "echo 'HOLA'"
42         ec.set(app, "command", cmd)
43         ec.register_connection(app, node)
44
45         ec.deploy()
46
47         ec.wait_finished([app])
48
49         self.assertTrue(ec.state(node) == ResourceState.STARTED)
50         self.assertTrue(ec.state(app) == ResourceState.FINISHED)
51
52         stdout = ec.trace(app, 'stdout')
53         self.assertTrue(stdout.strip() == "HOLA")
54
55         ec.shutdown()
56
57     @skipIfNotAlive
58     def t_ping(self, host, user):
59         from neco.execution.resource import ResourceFactory
60         
61         ResourceFactory.register_type(LinuxNode)
62         ResourceFactory.register_type(LinuxApplication)
63
64         ec = ExperimentController()
65         
66         node = ec.register_resource("LinuxNode")
67         ec.set(node, "hostname", host)
68         ec.set(node, "username", user)
69         ec.set(node, "cleanHome", True)
70         ec.set(node, "cleanProcesses", True)
71
72         app = ec.register_resource("LinuxApplication")
73         cmd = "ping -c5 %s" % self.target 
74         ec.set(app, "command", cmd)
75         
76         ec.register_connection(app, node)
77
78         ec.deploy()
79
80         ec.wait_finished([app])
81
82         self.assertTrue(ec.state(node) == ResourceState.STARTED)
83         self.assertTrue(ec.state(app) == ResourceState.FINISHED)
84
85         stdout = ec.trace(app, 'stdout')
86         size = ec.trace(app, 'stdout', attr = TraceAttr.SIZE)
87         self.assertEquals(len(stdout), size)
88         
89         block = ec.trace(app, 'stdout', attr = TraceAttr.STREAM, block = 5, offset = 1)
90         self.assertEquals(block, stdout[5:10])
91
92         path = ec.trace(app, 'stdout', attr = TraceAttr.PATH)
93         rm = ec.get_resource(app)
94         p = os.path.join(rm.app_home, 'stdout')
95         self.assertEquals(path, p)
96
97         ec.shutdown()
98
99     @skipIfNotAlive
100     def t_concurrency(self, host, user):
101         from neco.execution.resource import ResourceFactory
102         
103         ResourceFactory.register_type(LinuxNode)
104         ResourceFactory.register_type(LinuxApplication)
105
106         ec = ExperimentController()
107         
108         node = ec.register_resource("LinuxNode")
109         ec.set(node, "hostname", host)
110         ec.set(node, "username", user)
111         ec.set(node, "cleanHome", True)
112         ec.set(node, "cleanProcesses", True)
113
114         apps = list()
115         for i in xrange(50):
116             app = ec.register_resource("LinuxApplication")
117             cmd = "ping -c5 %s" % self.target 
118             ec.set(app, "command", cmd)
119             ec.register_connection(app, node)
120             apps.append(app)
121
122         ec.deploy()
123
124         ec.wait_finished(apps)
125
126         self.assertTrue(ec.state(node) == ResourceState.STARTED)
127         self.assertTrue(
128                all([ec.state(guid) == ResourceState.FINISHED \
129                 for guid in apps])
130                 )
131
132         for app in apps:
133             stdout = ec.trace(app, 'stdout')
134             size = ec.trace(app, 'stdout', attr = TraceAttr.SIZE)
135             self.assertEquals(len(stdout), size)
136             
137             block = ec.trace(app, 'stdout', attr = TraceAttr.STREAM, block = 5, offset = 1)
138             self.assertEquals(block, stdout[5:10])
139
140             path = ec.trace(app, 'stdout', attr = TraceAttr.PATH)
141             rm = ec.get_resource(app)
142             p = os.path.join(rm.app_home, 'stdout')
143             self.assertEquals(path, p)
144
145         ec.shutdown()
146
147     @skipIfNotAlive
148     def t_condition(self, host, user, depends):
149         from neco.execution.resource import ResourceFactory
150         
151         ResourceFactory.register_type(LinuxNode)
152         ResourceFactory.register_type(LinuxApplication)
153
154         ec = ExperimentController()
155         
156         node = ec.register_resource("LinuxNode")
157         ec.set(node, "hostname", host)
158         ec.set(node, "username", user)
159         ec.set(node, "cleanHome", True)
160         ec.set(node, "cleanProcesses", True)
161
162         server = ec.register_resource("LinuxApplication")
163         cmd = "echo 'HOLA' | nc -l 3333"
164         ec.set(server, "command", cmd)
165         ec.set(server, "depends", depends)
166         ec.register_connection(server, node)
167
168         client = ec.register_resource("LinuxApplication")
169         cmd = "nc 127.0.0.1 3333"
170         ec.set(client, "command", cmd)
171         ec.register_connection(client, node)
172
173         ec.register_condition(client, ResourceAction.START, server, ResourceState.STARTED)
174
175         apps = [client, server]
176         
177         ec.deploy()
178
179         ec.wait_finished(apps)
180
181         self.assertTrue(ec.state(node) == ResourceState.STARTED)
182         self.assertTrue(ec.state(server) == ResourceState.FINISHED)
183         self.assertTrue(ec.state(client) == ResourceState.FINISHED)
184
185         stdout = ec.trace(client, 'stdout')
186         self.assertTrue(stdout.strip() == "HOLA")
187
188         ec.shutdown()
189
190     def test_stdout_fedora(self):
191         self.t_stdout(self.fedora_host, self.fedora_user)
192
193     def test_stdout_ubuntu(self):
194         self.t_stdout(self.ubuntu_host, self.ubuntu_user)
195
196     def test_ping_fedora(self):
197         self.t_ping(self.fedora_host, self.fedora_user)
198
199     def test_ping_ubuntu(self):
200         self.t_ping(self.ubuntu_host, self.ubuntu_user)
201
202     def test_concurrency_fedora(self):
203         self.t_concurrency(self.fedora_host, self.fedora_user)
204
205     def test_concurrency_ubuntu(self):
206         self.t_concurrency(self.ubuntu_host, self.ubuntu_user)
207
208     def test_condition_fedora(self):
209         self.t_condition(self.fedora_host, self.fedora_user, "nc")
210
211     def test_condition_ubuntu(self):
212         self.t_condition(self.ubuntu_host, self.ubuntu_user, "netcat")
213
214     # TODO: test compilation, sources, dependencies, etc!!!
215
216 if __name__ == '__main__':
217     unittest.main()
218