4 import sfa.util.xmlrpcprotocol as xmlrpc
6 from unittest import TestCase
7 from optparse import OptionParser
8 from sfa.util.xmlrpcprotocol import ServerException
9 from sfa.util.namespace import *
10 from sfa.util.config import *
11 from sfa.trust.certificate import *
12 from sfa.client import sfi
24 def __init__(self, options):
25 try: self.config = config = Config(options.config_file)
27 print "failed to read config_file %s" % options.config_file
29 key_path = os.path.dirname(options.config_file)
30 user_name = self.config.SFI_USER.split('.')[-1:][0]
31 key_file = key_path + os.sep + user_name + '.pkey'
32 cert_file = key_path + os.sep + user_name + '.cert'
33 self.key = Keypair(filename=key_file)
34 self.cert = Certificate(subject=self.config.SFI_USER)
35 self.cert.set_pubkey(self.key)
36 self.cert.set_issuer(self.key, self.config.SFI_USER)
38 self.cert.save_to_file(cert_file)
39 SFI_AGGREGATE = config.SFI_SM.replace('12347', '12346')
40 SFI_CM = 'http://' + options.cm_host + ':12346'
41 self.registry = xmlrpc.get_server(config.SFI_REGISTRY, key_file, cert_file)
42 self.aggregate = xmlrpc.get_server(SFI_AGGREGATE, key_file, cert_file)
43 self.sm = xmlrpc.get_server(config.SFI_SM, key_file, cert_file)
44 self.cm = xmlrpc.get_server(SFI_CM, key_file, cert_file)
45 self.user = config.SFI_USER
46 # XX defaulting to user, but this should be configurable so we can
47 # test from components persepctive
50 def get_credential(self, hrn = None, type = 'user'):
51 if not hrn: hrn = self.user
53 cert = self.cert.save_to_string(save_parents=True)
54 request_hash = self.key.compute_hash([cert, 'user', hrn])
55 self.credential = self.registry.get_self_credential(cert, type, hrn, request_hash)
56 return self.credential
58 if not self.credential:
59 self.get_credential(self.user, 'user')
60 return self.registry.get_credential(self.credential, type, hrn)
64 class BasicTestCase(unittest.TestCase):
65 def __init__(self, testname, client):
66 unittest.TestCase.__init__(self, testname)
70 self.registry = self.client.registry
71 self.aggregate = self.client.aggregate
72 self.sm = self.client.sm
73 self.cm = self.client.cm
74 self.credential = self.client.credential
75 self.hrn = self.client.user
76 self.type = self.client.type
79 class RegistryTest(BasicTestCase):
83 Make sure test records dont exsit
85 BasicTestCase.setUp(self)
87 def testGetSelfCredential(self):
88 self.client.get_credential()
90 def testRegister(self):
93 def testRegisterPeerObject(self):
99 def testResolve(self):
100 self.registry.resolve(self.credential, self.hrn)
103 def testRemove(self):
106 def testRemovePeerObject(self):
110 authority = get_authority(self.client.user)
111 self.registry.list(self.credential, authority)
113 def testGetRegistries(self):
114 self.registry.get_registries(self.credential)
116 def testGetAggregates(self):
117 self.registry.get_aggregates(self.credential)
119 def testGetTrustedCerts(self):
120 # this should fail unless we are a node
121 callable = self.registry.get_trusted_certs
122 server_exception = False
124 callable(self.credential)
125 except ServerException:
126 server_exception = True
128 if self.type in ['user'] and not server_exception:
134 class AggregateTest(BasicTestCase):
136 BasicTestCase.setUp(self)
138 def testGetSlices(self):
139 self.aggregate.get_slices(self.credential)
141 def testGetResources(self):
142 self.aggregate.get_resources(self.credential)
144 def testCreateSlice(self):
147 def testDeleteSlice(self):
150 def testGetTicket(self):
153 class SlicemgrTest(AggregateTest):
155 AggregateTest.setUp(self)
157 class ComponentTest(BasicTestCase):
159 BasicTestCase.setUp(self)
161 def testStartSlice(self):
164 def testStopSlice(self):
167 def testDeleteSlice(self):
170 def testRestartSlice(self):
173 def testGetSlices(self):
176 def testRedeemTicket(self):
180 def test_names(testcase):
181 return [name for name in dir(testcase) if name.startswith('test')]
183 if __name__ == '__main__':
187 default_config_dir = os.path.expanduser('~/.sfi/sfi_config')
188 default_cm = "echo.cs.princeton.edu"
189 parser = OptionParser(usage="%(prog_name)s [options]" % locals())
190 parser.add_option('-f', '--config_file', dest='config_file', default=default_config_dir,
191 help='config file. default is %s' % default_config_dir)
192 parser.add_option('-r', '--registry', dest='registry', action='store_true',
193 default=False, help='run registry tests')
194 parser.add_option('-a', '--aggregate', dest='aggregate', action='store_true',
195 default=False, help='run aggregate tests')
196 parser.add_option('-s', '--slicemgr', dest='slicemgr', action='store_true',
197 default=False, help='run slicemgr tests')
198 parser.add_option('-c', '--component', dest='component', action='store_true',
199 default=False, help='run component tests')
200 parser.add_option('-d', '--cm_host', dest='cm_host', default=default_cm,
201 help='dns name of component to test. default is %s' % default_cm)
202 parser.add_option('-A', '--all', dest='all', action='store_true',
203 default=False, help='run component tests')
205 options, args = parser.parse_args()
206 suite = unittest.TestSuite()
207 client = Client(options)
208 # bootstrap the users credential
209 client.get_credential()
211 if options.registry or options.all:
212 for name in test_names(RegistryTest):
213 suite.addTest(RegistryTest(name, client))
215 if options.aggregate or options.all:
216 for name in test_names(AggregateTest):
217 suite.addTest(AggregateTest(name, client))
219 if options.slicemgr or options.all:
220 for name in test_names(SlicemgrTest):
221 suite.addTest(SlicemgrTest(name, client))
223 if options.component or options.all:
224 for name in test_names(ComponentTest):
225 suite.addTest(ComponentTest(name, client))
227 unittest.TextTestRunner(verbosity=2).run(suite)