deprecated sfa.util.misc (duplicate for namespace)
[sfa.git] / tests / testXrn.py
1 #!/usr/bin/python
2 import sys
3 import unittest
4
5 from sfa.util.faults import *
6 from sfa.util.xrn import Xrn
7
8 verbose=False
9
10 class TestXrn(unittest.TestCase):
11
12     hrns=[ # hrn, type, expected_urn
13         ('ple.inria.baris','user', "urn:publicid:IDN+ple:inria+user+baris"),
14         ('emulab\.net.slice.jktest','slice', "urn:publicid:IDN+emulab.net:slice+slice+jktest"),
15         ('plc.princeton.tmack','user', "urn:publicid:IDN+plc:princeton+user+tmack"),
16         ('fake-pi1@onelab.eu','user', "urn:publicid:IDN+fake-pi1@onelab+user+eu"),
17         # not providing a type is currently not supported
18         ('fake-pi1@onelab.eu',None, None),
19         ]
20     
21     urns=[ # urn, expected_hrn, expected_type
22         ('urn:publicid:IDN+emulab:net+slice+jktest', "emulab.net.jktest", "slice"),
23         ('urn:publicid:IDN+emulab.net+slice+jktest', "emulab\\.net.jktest", "slice"),
24         ("urn:publicid:IDN+plc:princeton+user+tmack", "plc.princeton.tmack", "user"),
25         ]
26
27     def test_hrns(self):
28         for (h,t,exp_urn) in TestXrn.hrns:
29             print 'testing (',h,t,') expecting',exp_urn
30             if exp_urn:
31                 xrn=Xrn(hrn=h,type=t)
32                 if verbose: print xrn.dump_string()
33                 urn=xrn.get_urn()
34                 (h1,t1) = Xrn(urn=urn).get_hrn()
35                 if h1!=h or t1!=t or urn!=exp_urn:
36                     print "hrn->urn->hrn : MISMATCH with in=(%s,%s) -- out=(%s,%s) -- urn=%s"%(h,t,h1,t1,urn)
37                 self.assertEqual(h1,h)
38                 self.assertEqual(t1,t)
39                 self.assertEqual(urn,exp_urn)
40             else:
41                 # could not figure how to use assertFails on object construction..
42                 # with self.assertRaises(SfaAPIError):
43                 #    Xrn(hrn=h,type=t).get_urn()
44                 try:
45                     Xrn(hrn=h,type=t).get_urn()
46                     failure="Unexpectedly created Xrn object"
47                 except SfaAPIError:
48                     failure=False
49                 except Exception,e:
50                     failure="Xrn creation raised unexpected exception %r"%e
51                 if failure: 
52                     print "hrn->urn->hrn - %s with HRN=%s TYPE=%s"%(failure,h,t)
53                     self.assertFalse(True)
54                     
55
56     def test_urns(self):
57         for (urn, exp_hrn, exp_type) in TestXrn.urns:
58             xrn=Xrn(urn=urn)
59             print 'testing urn',urn,'expecting (',exp_hrn,exp_type,')'
60             if verbose: print xrn.dump_string()
61             (h,t)=xrn.get_hrn()
62             urn1 = Xrn(hrn=h,type=t).get_urn()
63             if urn1!=urn:
64                 print "urn->hrn->urn : MISMATCH with in=(%s) -- out=(%s) -- hrn=(%s,%s)"%(urn,urn1,h,t)
65             self.assertEqual(urn1,urn)