Adding authors and correcting licence information
[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.install_packages('gcc')
99         self.assertEquals(out, "")
100
101         (out, err), proc = node.remove_packages('gcc')
102         
103         self.assertEquals(out, "")
104
105
106     @skipIfNotAlive
107     def t_compile(self, host, user):
108         node, ec = create_node(host, user)
109
110         app_home = os.path.join(node.exp_home, "my-app")
111         node.mkdir(app_home, clean = True)
112
113         prog = """#include <stdio.h>
114
115 int
116 main (void)
117 {
118     printf ("Hello, world!\\n");
119     return 0;
120 }
121 """
122         # upload the test program
123         dst = os.path.join(app_home, "hello.c")
124         node.upload(prog, dst, text = True)
125
126         # install gcc
127         node.install_packages('gcc')
128
129         # compile the program using gcc
130         command = "cd %s; gcc -Wall hello.c -o hello" % app_home
131         (out, err), proc = node.execute(command)
132
133         # execute the program and get the output from stdout
134         command = "%s/hello" % app_home 
135         (out, err), proc = node.execute(command)
136
137         self.assertEquals(out, "Hello, world!\n")
138
139         # execute the program and get the output from a file
140         command = "%(home)s/hello > %(home)s/hello.out" % {
141                 'home': app_home}
142         (out, err), proc = node.execute(command)
143
144         # retrieve the output file 
145         src = os.path.join(app_home, "hello.out")
146         f = tempfile.NamedTemporaryFile(delete=False)
147         dst = f.name
148         node.download(src, dst)
149         f.close()
150
151         node.remove_packages('gcc')
152         node.rmdir(app_home)
153
154         f = open(dst, "r")
155         out = f.read()
156         f.close()
157         
158         self.assertEquals(out, "Hello, world!\n")
159
160     def test_execute_fedora(self):
161         self.t_execute(self.fedora_host, self.fedora_user)
162
163     def test_execute_ubuntu(self):
164         self.t_execute(self.ubuntu_host, self.ubuntu_user)
165
166     def test_run_fedora(self):
167         self.t_run(self.fedora_host, self.fedora_user)
168
169     def test_run_ubuntu(self):
170         self.t_run(self.ubuntu_host, self.ubuntu_user)
171
172     def test_intall_fedora(self):
173         self.t_install(self.fedora_host, self.fedora_user)
174
175     def test_install_ubuntu(self):
176         self.t_install(self.ubuntu_host, self.ubuntu_user)
177
178     def test_compile_fedora(self):
179         self.t_compile(self.fedora_host, self.fedora_user)
180
181     def test_compile_ubuntu(self):
182         self.t_compile(self.ubuntu_host, self.ubuntu_user)
183     
184     @skipInteractive
185     def test_xterm_ubuntu(self):
186         """ Interactive test. Should not run automatically """
187         self.t_xterm(self.ubuntu_host, self.ubuntu_user)
188
189
190 if __name__ == '__main__':
191     unittest.main()
192