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