BugFix in LinuxNode unit test
[nepi.git] / test / resources / linux / node.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
22 from nepi.resources.linux.node import LinuxNode
23 from nepi.util.sshfuncs import RUNNING, FINISHED
24
25 from test_utils import skipIfNotAlive, skipInteractive, create_node
26
27 import os
28 import time
29 import tempfile
30 import unittest
31
32 class LinuxNodeTestCase(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 = 'alina'
39         
40         self.target = 'nepi5.pl.sophia.inria.fr'
41
42     @skipIfNotAlive
43     def t_xterm(self, host, user):
44         node, ec = create_node(host, user)
45
46         node.install_packages('xterm')
47
48         (out, err), proc = node.execute('xterm', forward_x11 = True)
49         
50         self.assertEquals(out, "")
51
52         (out, err), proc = node.remove_packages('xterm')
53         
54         self.assertEquals(out, "")
55
56     @skipIfNotAlive
57     def t_execute(self, host, user):
58         node, ec = create_node(host, user)
59
60         command = "ping -qc3 %s" % self.target
61         
62         (out, err), proc = node.execute(command)
63
64         expected = """3 packets transmitted, 3 received, 0% packet loss"""
65
66         self.assertTrue(out.find(expected) > 0)
67
68     @skipIfNotAlive
69     def t_run(self, host, user):
70         node, ec = create_node(host, user)
71         
72         app_home = os.path.join(node.exp_home, "my-app")
73         node.mkdir(app_home, clean = True)
74         
75         command = "ping %s" % self.target
76         node.run(command, app_home)
77         pid, ppid = node.checkpid(app_home)
78
79         status = node.status(pid, ppid)
80         self.assertTrue(status, RUNNING)
81
82         node.kill(pid, ppid)
83         status = node.status(pid, ppid)
84         self.assertTrue(status, FINISHED)
85         
86         (out, err), proc = node.check_output(app_home, "stdout")
87
88         expected = """64 bytes from"""
89
90         self.assertTrue(out.find(expected) > 0)
91
92         node.rmdir(app_home)
93
94     @skipIfNotAlive
95     def t_install(self, host, user):
96         node, ec = create_node(host, user)
97
98         (out, err), proc = node.mkdir(node.node_home, clean=True)
99         self.assertEquals(out, "")
100
101         (out, err), proc = node.install_packages('gcc')
102         self.assertEquals(out, "")
103
104         (out, err), proc = node.remove_packages('gcc')
105         self.assertEquals(out, "")
106
107         (out, err), proc = node.rmdir(node.exp_home)
108         self.assertEquals(out, "")
109
110     @skipIfNotAlive
111     def t_compile(self, host, user):
112         node, ec = create_node(host, user)
113
114         app_home = os.path.join(node.exp_home, "my-app")
115         node.mkdir(app_home, clean = True)
116
117         prog = """#include <stdio.h>
118
119 int
120 main (void)
121 {
122     printf ("Hello, world!\\n");
123     return 0;
124 }
125 """
126         # upload the test program
127         dst = os.path.join(app_home, "hello.c")
128         node.upload(prog, dst, text = True)
129
130         # install gcc
131         node.install_packages('gcc')
132
133         # compile the program using gcc
134         command = "cd %s; gcc -Wall hello.c -o hello" % app_home
135         (out, err), proc = node.execute(command)
136
137         # execute the program and get the output from stdout
138         command = "%s/hello" % app_home 
139         (out, err), proc = node.execute(command)
140
141         self.assertEquals(out, "Hello, world!\n")
142
143         # execute the program and get the output from a file
144         command = "%(home)s/hello > %(home)s/hello.out" % {
145                 'home': app_home}
146         (out, err), proc = node.execute(command)
147
148         # retrieve the output file 
149         src = os.path.join(app_home, "hello.out")
150         f = tempfile.NamedTemporaryFile(delete=False)
151         dst = f.name
152         node.download(src, dst)
153         f.close()
154
155         node.remove_packages('gcc')
156         node.rmdir(app_home)
157
158         f = open(dst, "r")
159         out = f.read()
160         f.close()
161         
162         self.assertEquals(out, "Hello, world!\n")
163
164     def test_execute_fedora(self):
165         self.t_execute(self.fedora_host, self.fedora_user)
166
167     def test_execute_ubuntu(self):
168         self.t_execute(self.ubuntu_host, self.ubuntu_user)
169
170     def test_run_fedora(self):
171         self.t_run(self.fedora_host, self.fedora_user)
172
173     def test_run_ubuntu(self):
174         self.t_run(self.ubuntu_host, self.ubuntu_user)
175
176     def test_intall_fedora(self):
177         self.t_install(self.fedora_host, self.fedora_user)
178
179     def test_install_ubuntu(self):
180         self.t_install(self.ubuntu_host, self.ubuntu_user)
181
182     def test_compile_fedora(self):
183         self.t_compile(self.fedora_host, self.fedora_user)
184
185     def test_compile_ubuntu(self):
186         self.t_compile(self.ubuntu_host, self.ubuntu_user)
187     
188     @skipInteractive
189     def test_xterm_ubuntu(self):
190         """ Interactive test. Should not run automatically """
191         self.t_xterm(self.ubuntu_host, self.ubuntu_user)
192
193
194 if __name__ == '__main__':
195     unittest.main()
196