bc3892174b8c3c40019de8aabfbd026ac61a4954
[nepi.git] / test / lib / test_utils.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2013 INRIA
4 #
5 #    This program is free software: you can redistribute it and/or modify
6 #    it under the terms of the GNU General Public License version 2 as
7 #    published by the Free Software Foundation;
8 #
9 #    This program is distributed in the hope that it will be useful,
10 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #    GNU General Public License for more details.
13 #
14 #    You should have received a copy of the GNU General Public License
15 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 #
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
18
19 from nepi.resources.linux.node import LinuxNode
20
21 import os
22 import sys
23
24 class DummyEC(object):
25     @property
26     def exp_id(self):
27         return "nepi-1"
28
29 def create_node(hostname, username = None, identity = None):
30     ec = DummyEC()
31     node = LinuxNode(ec, 1)
32
33     node.set("hostname", hostname)
34     
35     if username:
36         node.set("username", username)
37     
38     if identity:
39         node.set("identity", identity)
40
41     # If we don't return the reference to the EC
42     # it will be released by the garbage collector since 
43     # the resources only save a weak refernce to it.
44     return node, ec
45
46 def skipIfNotAlive(func):
47     name = func.__name__
48     def wrapped(*args, **kwargs):
49         hostname = args[1]
50         if hostname != "localhost":
51             username = None
52             identity = None
53
54             if len(args) >= 3:
55                 username = args[2]
56
57             if len(args) >= 4:
58                 identity = args[3]
59
60             node, ec = create_node(hostname, username, identity)
61
62             if not node.is_alive():
63                 print "*** WARNING: Skipping test %s: Node %s is not alive\n" % (
64                     name, node.get("hostname"))
65                 return
66
67         return func(*args, **kwargs)
68     
69     return wrapped
70
71 def skipIfAnyNotAlive(func):
72     name = func.__name__
73     def wrapped(*args, **kwargs):
74         argss = list(args)
75         argss.pop(0)
76
77         for i in xrange(len(argss)/2):
78             username = argss[i*2]
79             hostname = argss[i*2+1]
80             node, ec = create_node(hostname, username)
81
82             if not node.is_alive():
83                 print "*** WARNING: Skipping test %s: Node %s is not alive\n" % (
84                     name, node.get("hostname"))
85                 return
86
87         return func(*args, **kwargs)
88     
89     return wrapped
90
91 def skipIfAnyNotAliveWithIdentity(func):
92     name = func.__name__
93     def wrapped(*args, **kwargs):
94         argss = list(args)
95         argss.pop(0)
96         for i in xrange(len(argss)/3):
97             username = argss[i*3]
98             hostname = argss[i*3+1]
99             identity = argss[i*3+2]
100
101             node, ec = create_node(hostname, username, identity)
102
103             if not node.is_alive():
104                 print "*** WARNING: Skipping test %s: Node %s is not alive\n" % (
105                     name, node.get("hostname"))
106                 return
107
108         return func(*args, **kwargs)
109     
110     return wrapped
111
112
113 def skipInteractive(func):
114     name = func.__name__
115     def wrapped(*args, **kwargs):
116         mode = os.environ.get("NEPI_INTERACTIVE_TEST", False)
117         mode = mode and  mode.lower() in ['true', 'yes']
118         if not mode:
119             print "*** WARNING: Skipping test %s: Interactive mode off \n" % name
120             return
121
122         return func(*args, **kwargs)
123     
124     return wrapped
125
126 def skipIfNotPLCredentials(func):
127     name = func.__name__
128     def wrapped(*args, **kwargs):
129         pl_user = os.environ.get("PL_USER")
130         pl_pass = os.environ.get("PL_PASS")
131         if not (pl_user and pl_pass):
132             print "*** WARNING: Skipping test %s: Planetlab user, password and slicename not defined\n" % name
133             return
134
135         return func(*args, **kwargs)
136
137     return wrapped
138
139 def skipIfNotPythonVersion(func):
140     name = func.__name__
141     def wrapped(*args, **kwargs):
142         if sys.version_info < 2.7:
143             print "*** WARNING: Skipping test %s: total_seconds() method doesn't exist\n" % name
144             return
145
146         return func(*args, **kwargs)
147
148     return wrapped
149
150 def skipIfNotSfaCredentials(func):
151     name = func.__name__
152     def wrapped(*args, **kwargs):
153         sfa_user = os.environ.get("SFA_USER")
154         sfa_pk = os.environ.get("SFA_PK")
155         
156         if not (sfa_user and os.path.exists(os.path.expanduser(sfa_pk))):
157             print "*** WARNING: Skipping test %s: SFA path to private key doesn't exist\n" % name
158             return
159
160         return func(*args, **kwargs)
161
162     return wrapped
163
164 def skipIfNotSfi(func):
165     name = func.__name__
166     def wrapped(*args, **kwargs):
167         try:
168             from sfa.client.sfi import Sfi
169             from sfa.util.xrn import hrn_to_urn
170         except ImportError:
171             print "*** WARNING: Skipping test %s: sfi-client or sfi-common not installed\n" % name
172             return
173
174         return func(*args, **kwargs)
175
176     return wrapped
177
178 def skipIf(cond, text):
179     def wrapped(func, text):
180         name = func.__name__
181
182         def banner(*args, **kwargs):
183             sys.stderr.write("*** WARNING: Skipping test %s: `%s'\n" %
184                     (name, text))
185             return None
186         return banner
187
188     return (lambda func: wrapped(func, text)) if cond else lambda func: func
189
190
191