Binary building support - and tests for it
[nepi.git] / test / testbeds / planetlab / execute.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import getpass
5 from nepi.util.constants import STATUS_FINISHED, TIME_NOW
6 from nepi.testbeds import planetlab
7 import os
8 import shutil
9 import tempfile
10 import time
11 import unittest
12 import re
13 import test_util
14
15 class PlanetLabExecuteTestCase(unittest.TestCase):
16     def setUp(self):
17         self.root_dir = tempfile.mkdtemp()
18         
19     def tearDown(self):
20         shutil.rmtree(self.root_dir)
21
22     def make_instance(self):
23         testbed_version = "01"
24         instance = planetlab.TestbedController(testbed_version)
25         slicename = "inria_nepi12"
26         pl_user, pl_pwd = test_util.pl_auth()
27         
28         instance.defer_configure("homeDirectory", self.root_dir)
29         instance.defer_configure("slice", slicename)
30         instance.defer_configure("sliceSSHKey", "/user/%s/home/.ssh/id_rsa_planetlab" % (getpass.getuser(),))
31         instance.defer_configure("authUser", pl_user)
32         instance.defer_configure("authPass", pl_pwd)
33         
34         return instance
35
36     @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
37     def test_simple(self):
38         instance = self.make_instance()
39         
40         instance.defer_create(2, "Node")
41         instance.defer_create_set(2, "hostname", "onelab11.pl.sophia.inria.fr")
42         instance.defer_create(3, "Node")
43         instance.defer_create_set(3, "hostname", "onelab10.pl.sophia.inria.fr")
44         instance.defer_create(4, "NodeInterface")
45         instance.defer_connect(2, "devs", 4, "node")
46         instance.defer_create(5, "NodeInterface")
47         instance.defer_connect(3, "devs", 5, "node")
48         instance.defer_create(6, "Internet")
49         instance.defer_connect(4, "inet", 6, "devs")
50         instance.defer_connect(5, "inet", 6, "devs")
51         instance.defer_create(7, "Application")
52         instance.defer_create_set(7, "command", "ping -qc1 {#GUID-5.addr[0].[Address]#}")
53         instance.defer_add_trace(7, "stdout")
54         instance.defer_connect(7, "node", 2, "apps")
55
56         instance.do_setup()
57         instance.do_create()
58         instance.do_connect()
59         instance.do_preconfigure()
60         
61         # Manually replace netref
62         instance.set(TIME_NOW, 7, "command",
63             instance.get(TIME_NOW, 7, "command")
64                 .replace("{#GUID-5.addr[0].[Address]#}", 
65                     instance.get_address(5, 0, "Address") )
66         )
67
68         instance.do_configure()
69         
70         instance.start()
71         while instance.status(7) != STATUS_FINISHED:
72             time.sleep(0.5)
73         ping_result = instance.trace(7, "stdout") or ""
74         comp_result = r"""PING .* \(.*\) \d*\(\d*\) bytes of data.
75
76 --- .* ping statistics ---
77 1 packets transmitted, 1 received, 0% packet loss, time \d*ms.*
78 """
79         self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
80             "Unexpected trace:\n" + ping_result)
81         instance.stop()
82         instance.shutdown()
83         
84     @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
85     def test_depends(self):
86         instance = self.make_instance()
87         
88         instance.defer_create(2, "Node")
89         instance.defer_create_set(2, "hostname", "onelab11.pl.sophia.inria.fr")
90         instance.defer_create(3, "NodeInterface")
91         instance.defer_connect(2, "devs", 3, "node")
92         instance.defer_create(4, "Internet")
93         instance.defer_connect(3, "inet", 4, "devs")
94         instance.defer_create(5, "Application")
95         instance.defer_create_set(5, "command", "gfortran --version")
96         instance.defer_create_set(5, "depends", "gcc-gfortran")
97         instance.defer_add_trace(5, "stdout")
98         instance.defer_connect(5, "node", 2, "apps")
99
100         instance.do_setup()
101         instance.do_create()
102         instance.do_connect()
103         instance.do_preconfigure()
104         instance.do_configure()
105         
106         instance.start()
107         while instance.status(5) != STATUS_FINISHED:
108             time.sleep(0.5)
109         ping_result = instance.trace(5, "stdout") or ""
110         comp_result = r".*GNU Fortran \(GCC\).*"
111         self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
112             "Unexpected trace:\n" + ping_result)
113         instance.stop()
114         instance.shutdown()
115         
116     @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
117     def test_build(self):
118         instance = self.make_instance()
119         
120         instance.defer_create(2, "Node")
121         instance.defer_create_set(2, "hostname", "onelab11.pl.sophia.inria.fr")
122         instance.defer_create(3, "NodeInterface")
123         instance.defer_connect(2, "devs", 3, "node")
124         instance.defer_create(4, "Internet")
125         instance.defer_connect(3, "inet", 4, "devs")
126         instance.defer_create(10, "Application")
127         instance.defer_create_set(10, "command", "./consts")
128         instance.defer_create_set(10, "buildDepends", "gcc")
129         instance.defer_create_set(10, "build", "gcc consts.c -o consts")
130         instance.defer_create_set(10, "sources", os.path.join(os.path.dirname(planetlab.__file__),'scripts','consts.c'))
131         instance.defer_add_trace(10, "stdout")
132         instance.defer_connect(10, "node", 2, "apps")
133
134         instance.do_setup()
135         instance.do_create()
136         instance.do_connect()
137         instance.do_preconfigure()
138         instance.do_configure()
139         
140         instance.start()
141         while instance.status(10) != STATUS_FINISHED:
142             time.sleep(0.5)
143         ping_result = instance.trace(10, "stdout") or ""
144         comp_result = \
145 r""".*ETH_P_ALL = 0x[0-9a-fA-F]{8}
146 ETH_P_IP = 0x[0-9a-fA-F]{8}
147 TUNSETIFF = 0x[0-9a-fA-F]{8}
148 IFF_NO_PI = 0x[0-9a-fA-F]{8}
149 IFF_TAP = 0x[0-9a-fA-F]{8}
150 IFF_TUN = 0x[0-9a-fA-F]{8}
151 IFF_VNET_HDR = 0x[0-9a-fA-F]{8}
152 TUN_PKT_STRIP = 0x[0-9a-fA-F]{8}
153 IFHWADDRLEN = 0x[0-9a-fA-F]{8}
154 IFNAMSIZ = 0x[0-9a-fA-F]{8}
155 IFREQ_SZ = 0x[0-9a-fA-F]{8}
156 FIONREAD = 0x[0-9a-fA-F]{8}.*
157 """
158         self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
159             "Unexpected trace:\n" + ping_result)
160         instance.stop()
161         instance.shutdown()
162         
163
164 if __name__ == '__main__':
165     unittest.main()
166