Merge branch 'master' of ssh://git.onelab.eu/git/sfa
[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 from sfa.util.plxrn import PlXrn
8
9 verbose=False
10
11 class TestXrn(unittest.TestCase):
12
13     def __hrn(self,h,t,exp_urn):
14         if verbose: print 'testing (',h,t,') expecting',exp_urn
15         if exp_urn:
16             xrn=Xrn(hrn=h,type=t)
17             if verbose: print xrn.dump_string()
18             urn=xrn.get_urn()
19             (h1,t1) = Xrn(urn=urn).get_hrn_type()
20             if h1!=h or t1!=t or urn!=exp_urn:
21                 print "hrn->urn->hrn : MISMATCH with in=(%s,%s) -- out=(%s,%s) -- urn=%s"%(h,t,h1,t1,urn)
22             self.assertEqual(h1,h)
23             self.assertEqual(t1,t)
24             self.assertEqual(urn,exp_urn)
25         else:
26             # could not figure how to use assertFails on object construction..
27             # with self.assertRaises(SfaAPIError):
28             #    Xrn(hrn=h,type=t).get_urn()
29             try:
30                 Xrn(hrn=h,type=t).get_urn()
31                 failure="Unexpectedly created Xrn object"
32             except SfaAPIError:
33                 failure=False
34             except Exception,e:
35                 failure="Xrn creation raised unexpected exception %r"%e
36             if failure: 
37                 print "hrn->urn->hrn - %s with HRN=%s TYPE=%s"%(failure,h,t)
38                 self.assertFalse(True)
39
40
41     def test_hrn001 (self): 
42         self.__hrn("ple.inria.baris",'user',
43                    "urn:publicid:IDN+ple:inria+user+baris")
44     def test_hnr002 (self): 
45         self.__hrn("emulab\.net.myslice.jktest",'slice',
46                    "urn:publicid:IDN+emulab.net:myslice+slice+jktest")
47     def test_hrn003(self):
48         self.__hrn("emulab\\.net.jktest", "slice",
49                    "urn:publicid:IDN+emulab.net+slice+jktest")
50     def test_hrn004(self):
51         self.__hrn("plc.princeton.tmack",'user',
52                    "urn:publicid:IDN+plc:princeton+user+tmack")
53     def test_hrn005(self):
54         self.__hrn("fake-pi1@onelab.eu",'user',
55                    "urn:publicid:IDN+fake-pi1@onelab+user+eu")
56     def test_hrn006(self):
57         self.__hrn("plc.princeton.tmack", 'user',
58                    "urn:publicid:IDN+plc:princeton+user+tmack" )
59     def test_hrn007(self):
60         # not providing a type is currently not supporte
61         self.__hrn("fake-pi1@onelab.eu",None,
62                    None)
63     def test_hrn008(self):
64         self.__hrn("plc.princeton.planetlab1", 'node',
65                    "urn:publicid:IDN+plc:princeton+node+planetlab1" )
66     def test_hrn009(self):
67         self.__hrn("plc.princeton", 'authority',
68                    "urn:publicid:IDN+plc:princeton+authority+sa" )
69     def test_hrn010(self):
70         self.__hrn("plc.vini.site", 'authority',
71                    "urn:publicid:IDN+plc:vini:site+authority+sa" )
72         
73
74     
75     def test_host001 (self):
76         xrn=PlXrn (auth="ple.inria",hostname="onelab09.pl.sophia.inria.fr")
77         self.assertEqual (xrn.get_hrn_type(), ("ple.inria.onelab09",'node'))
78     def test_host002 (self):
79         xrn=PlXrn (auth="ple\\.inria",hostname="onelab09.pl.sophia.inria.fr")
80         self.assertEqual (xrn.get_hrn_type(), ("ple\\.inria.onelab09",'node'))
81
82     def test_slice001  (self):
83         xrn=PlXrn (auth="ple",slicename="inria_omftest")
84         self.assertEqual (xrn.get_hrn_type(), ("ple.inria.omftest",'slice'))
85
86     def test_person001 (self):
87         xrn=PlXrn (auth="ple.inria",email="first.last@some.domain.com")
88         self.assertEqual (xrn.get_hrn_type(), ("ple.inria.first_last",'person'))
89     def test_person002 (self):
90         xrn=PlXrn (auth="ple.inria",email="first+last@some.domain.com")
91         self.assertEqual (xrn.get_hrn_type(), ("ple.inria.first_last",'person'))
92         
93
94
95     def test_login_base_001 (self):
96         xrn=PlXrn(hrn='ple.inria.omftest',type='slice')
97         self.assertEqual(xrn.login_base(),'inria')
98
99     def test_slicename_001 (self):
100         xrn=PlXrn(hrn='ple.inria.omftest',type='slice')
101         self.assertEqual(xrn.slicename(),'inria_omftest')
102
103     def test_authname_001 (self):
104         xrn=PlXrn(hrn='ple.inria.omftest',type='slice')
105         self.assertEqual(xrn.authname(),'inria')
106
107