2008-01-13 02:56:03 +00:00
|
|
|
#! /usr/bin/env python
|
|
|
|
'''
|
2008-01-30 09:45:54 +00:00
|
|
|
Unit and system tests for tahoe-fuse.
|
2008-01-13 02:56:03 +00:00
|
|
|
'''
|
|
|
|
|
2008-01-30 09:45:54 +00:00
|
|
|
# Note: It's always a SetupFailure, not a TestFailure if a webapi
|
|
|
|
# operation fails, because this does not indicate a fuse interface
|
|
|
|
# failure.
|
|
|
|
|
2008-06-01 02:21:17 +00:00
|
|
|
# TODO: Unmount after tests regardless of failure or success!
|
|
|
|
|
2008-01-30 09:45:54 +00:00
|
|
|
# TODO: Test mismatches between tahoe and fuse/posix. What about nodes
|
|
|
|
# with crazy names ('\0', unicode, '/', '..')? Huuuuge files?
|
|
|
|
# Huuuuge directories... As tahoe approaches production quality, it'd
|
|
|
|
# be nice if the fuse interface did so also by hardening against such cases.
|
|
|
|
|
2008-06-01 02:21:17 +00:00
|
|
|
# FIXME: Only create / launch necessary nodes. Do we still need an introducer and three nodes?
|
|
|
|
|
2008-01-21 03:06:19 +00:00
|
|
|
# FIXME: This framework might be replaceable with twisted.trial,
|
|
|
|
# especially the "layer" design, which is a bit cumbersome when
|
|
|
|
# using recursion to manage multiple clients.
|
|
|
|
|
|
|
|
# FIXME: Identify all race conditions (hint: starting clients, versus
|
|
|
|
# using the grid fs).
|
|
|
|
|
2008-01-21 01:47:47 +00:00
|
|
|
import sys, os, shutil, unittest, subprocess
|
|
|
|
import tempfile, re, time, signal, random, httplib
|
2008-01-29 05:25:11 +00:00
|
|
|
import traceback
|
2008-01-13 02:56:03 +00:00
|
|
|
|
2008-06-07 05:19:23 +00:00
|
|
|
# Import fuse implementations:
|
|
|
|
FuseDir = os.path.join('.', 'contrib', 'fuse')
|
|
|
|
if not os.path.isdir(FuseDir):
|
|
|
|
raise SystemExit('''
|
|
|
|
Could not find directory "%s". Please run this script from the tahoe
|
|
|
|
source base directory.
|
|
|
|
''' % (FuseDir,))
|
|
|
|
|
|
|
|
sys.path.append(os.path.join(FuseDir, 'impl_a'))
|
|
|
|
import tahoe_fuse as impl_a
|
|
|
|
|
|
|
|
sys.path.append(os.path.join(FuseDir, 'impl_b'))
|
|
|
|
import pyfuse.tahoe as impl_b
|
2008-01-13 02:56:03 +00:00
|
|
|
|
|
|
|
|
2008-01-20 23:54:56 +00:00
|
|
|
### Main flow control:
|
2008-06-07 05:19:23 +00:00
|
|
|
def main(args = sys.argv):
|
2008-01-20 23:54:56 +00:00
|
|
|
target = 'all'
|
2008-06-07 05:19:23 +00:00
|
|
|
if len(args) > 1:
|
|
|
|
target = args.pop(1)
|
2008-01-20 23:54:56 +00:00
|
|
|
|
|
|
|
if target not in ('all', 'unit', 'system'):
|
|
|
|
raise SystemExit(Usage)
|
|
|
|
|
|
|
|
if target in ('all', 'unit'):
|
|
|
|
run_unit_tests()
|
|
|
|
|
|
|
|
if target in ('all', 'system'):
|
|
|
|
run_system_test()
|
|
|
|
|
|
|
|
|
|
|
|
def run_unit_tests():
|
|
|
|
print 'Running Unit Tests.'
|
|
|
|
try:
|
|
|
|
unittest.main()
|
|
|
|
except SystemExit, se:
|
|
|
|
pass
|
|
|
|
print 'Unit Tests complete.\n'
|
|
|
|
|
|
|
|
|
|
|
|
def run_system_test():
|
|
|
|
SystemTest().run()
|
|
|
|
|
|
|
|
|
|
|
|
### System Testing:
|
|
|
|
class SystemTest (object):
|
|
|
|
def __init__(self):
|
2008-01-29 05:27:19 +00:00
|
|
|
# These members represent configuration:
|
|
|
|
self.fullcleanup = False # FIXME: Make this a commandline option.
|
|
|
|
|
|
|
|
# These members represent test state:
|
2008-01-20 23:54:56 +00:00
|
|
|
self.cliexec = None
|
2008-01-29 05:27:19 +00:00
|
|
|
self.testroot = None
|
2008-01-21 03:02:20 +00:00
|
|
|
|
2008-01-29 05:27:19 +00:00
|
|
|
# This test state is specific to the first client:
|
|
|
|
self.port = None
|
|
|
|
self.clientbase = None
|
2008-01-21 03:02:20 +00:00
|
|
|
|
2008-01-20 23:54:56 +00:00
|
|
|
## Top-level flow control:
|
|
|
|
# These "*_layer" methods call eachother in a linear fashion, using
|
|
|
|
# exception unwinding to do cleanup properly. Each "layer" invokes
|
|
|
|
# a deeper layer, and each layer does its own cleanup upon exit.
|
|
|
|
|
2008-01-29 05:27:19 +00:00
|
|
|
def run(self, fullcleanup = False):
|
|
|
|
'''
|
|
|
|
If full_cleanup, delete all temporary state.
|
|
|
|
Else: If there is an error do not delete basedirs.
|
|
|
|
|
|
|
|
Set to False if you wish to analyze a failure.
|
|
|
|
'''
|
|
|
|
self.fullcleanup = fullcleanup
|
2008-01-30 09:46:24 +00:00
|
|
|
print '\n*** Setting up system tests.'
|
2008-01-20 23:54:56 +00:00
|
|
|
try:
|
2008-06-07 05:19:23 +00:00
|
|
|
results = self.init_cli_layer()
|
|
|
|
print '\n*** System Tests complete:'
|
2008-06-07 07:05:07 +00:00
|
|
|
for result in results:
|
|
|
|
print 'Implementation %s: %d failed out of %d.' % result
|
|
|
|
except SetupFailure, sfail:
|
2008-01-20 23:54:56 +00:00
|
|
|
print
|
|
|
|
print sfail
|
2008-01-30 09:46:24 +00:00
|
|
|
print '\n*** System Tests were not successfully completed.'
|
2008-01-20 23:54:56 +00:00
|
|
|
|
|
|
|
def init_cli_layer(self):
|
|
|
|
'''This layer finds the appropriate tahoe executable.'''
|
2008-06-07 05:19:23 +00:00
|
|
|
self.cliexec = os.path.join('.', 'bin', 'tahoe')
|
2008-01-20 23:54:56 +00:00
|
|
|
version = self.run_tahoe('--version')
|
|
|
|
print 'Using %r with version:\n%s' % (self.cliexec, version.rstrip())
|
|
|
|
|
2008-01-30 09:46:24 +00:00
|
|
|
return self.create_testroot_layer()
|
2008-01-20 23:54:56 +00:00
|
|
|
|
2008-01-29 05:27:19 +00:00
|
|
|
def create_testroot_layer(self):
|
|
|
|
print 'Creating test base directory.'
|
|
|
|
self.testroot = tempfile.mkdtemp(prefix='tahoe_fuse_test_')
|
|
|
|
try:
|
2008-01-30 09:46:24 +00:00
|
|
|
return self.launch_introducer_layer()
|
2008-01-20 23:54:56 +00:00
|
|
|
finally:
|
2008-01-29 05:27:19 +00:00
|
|
|
if self.fullcleanup:
|
|
|
|
print 'Cleaning up test root directory.'
|
|
|
|
try:
|
|
|
|
shutil.rmtree(self.testroot)
|
|
|
|
except Exception, e:
|
|
|
|
print 'Exception removing test root directory: %r' % (self.testroot, )
|
|
|
|
print 'Ignoring cleanup exception: %r' % (e,)
|
|
|
|
else:
|
|
|
|
print 'Leaving test root directory: %r' % (self.testroot, )
|
|
|
|
|
|
|
|
|
2008-01-20 23:54:56 +00:00
|
|
|
def launch_introducer_layer(self):
|
|
|
|
print 'Launching introducer.'
|
2008-01-29 05:27:19 +00:00
|
|
|
introbase = os.path.join(self.testroot, 'introducer')
|
|
|
|
|
|
|
|
# NOTE: We assume if tahoe exits with non-zero status, no separate
|
2008-01-20 23:54:56 +00:00
|
|
|
# tahoe child process is still running.
|
2008-01-29 05:27:19 +00:00
|
|
|
createoutput = self.run_tahoe('create-introducer', '--basedir', introbase)
|
|
|
|
|
2008-01-30 08:10:53 +00:00
|
|
|
self.check_tahoe_output(createoutput, ExpectedCreationOutput, introbase)
|
2008-01-29 05:27:19 +00:00
|
|
|
|
|
|
|
startoutput = self.run_tahoe('start', '--basedir', introbase)
|
2008-01-20 23:54:56 +00:00
|
|
|
try:
|
2008-01-30 08:10:53 +00:00
|
|
|
self.check_tahoe_output(startoutput, ExpectedStartOutput, introbase)
|
2008-01-20 23:54:56 +00:00
|
|
|
|
2008-01-30 09:46:24 +00:00
|
|
|
return self.launch_clients_layer(introbase)
|
2008-01-20 23:54:56 +00:00
|
|
|
|
|
|
|
finally:
|
|
|
|
print 'Stopping introducer node.'
|
2008-01-29 05:27:19 +00:00
|
|
|
self.stop_node(introbase)
|
2008-01-20 23:54:56 +00:00
|
|
|
|
2008-01-21 03:02:20 +00:00
|
|
|
TotalClientsNeeded = 3
|
2008-06-07 05:19:23 +00:00
|
|
|
def launch_clients_layer(self, introbase, clientnum = 0):
|
|
|
|
if clientnum >= self.TotalClientsNeeded:
|
2008-01-30 09:46:24 +00:00
|
|
|
return self.create_test_dirnode_layer()
|
2008-01-21 03:02:20 +00:00
|
|
|
|
2008-01-29 05:27:19 +00:00
|
|
|
tmpl = 'Launching client %d of %d.'
|
|
|
|
print tmpl % (clientnum,
|
2008-01-21 03:02:20 +00:00
|
|
|
self.TotalClientsNeeded)
|
|
|
|
|
2008-01-29 05:27:19 +00:00
|
|
|
base = os.path.join(self.testroot, 'client_%d' % (clientnum,))
|
2008-01-21 03:02:20 +00:00
|
|
|
|
2008-01-29 05:27:19 +00:00
|
|
|
output = self.run_tahoe('create-client', '--basedir', base)
|
2008-01-30 08:10:53 +00:00
|
|
|
self.check_tahoe_output(output, ExpectedCreationOutput, base)
|
2008-01-20 23:54:56 +00:00
|
|
|
|
2008-06-01 02:03:51 +00:00
|
|
|
webportpath = os.path.join(base, 'webport')
|
2008-06-07 05:19:23 +00:00
|
|
|
if clientnum == 0:
|
2008-01-29 05:27:19 +00:00
|
|
|
# The first client is special:
|
|
|
|
self.clientbase = base
|
|
|
|
self.port = random.randrange(1024, 2**15)
|
2008-01-21 00:09:44 +00:00
|
|
|
|
2008-06-01 02:03:51 +00:00
|
|
|
f = open(webportpath, 'w')
|
2008-01-29 05:27:19 +00:00
|
|
|
f.write('tcp:%d:interface=127.0.0.1\n' % self.port)
|
2008-01-21 03:02:20 +00:00
|
|
|
f.close()
|
2008-06-01 02:03:51 +00:00
|
|
|
else:
|
|
|
|
os.remove(webportpath)
|
|
|
|
|
2008-01-21 00:09:44 +00:00
|
|
|
|
2008-01-29 05:27:19 +00:00
|
|
|
introfurl = os.path.join(introbase, 'introducer.furl')
|
2008-01-21 00:09:44 +00:00
|
|
|
|
2008-06-07 06:17:19 +00:00
|
|
|
self.polling_operation(lambda : os.path.isfile(introfurl),
|
|
|
|
'introducer.furl creation')
|
2008-01-29 05:27:19 +00:00
|
|
|
shutil.copy(introfurl, base)
|
2008-01-21 00:09:44 +00:00
|
|
|
|
2008-01-20 23:54:56 +00:00
|
|
|
# NOTE: We assume if tahoe exist with non-zero status, no separate
|
|
|
|
# tahoe child process is still running.
|
2008-01-29 05:27:19 +00:00
|
|
|
startoutput = self.run_tahoe('start', '--basedir', base)
|
2008-01-20 23:54:56 +00:00
|
|
|
try:
|
2008-01-30 08:10:53 +00:00
|
|
|
self.check_tahoe_output(startoutput, ExpectedStartOutput, base)
|
2008-01-21 03:02:20 +00:00
|
|
|
|
2008-01-30 09:46:24 +00:00
|
|
|
return self.launch_clients_layer(introbase, clientnum+1)
|
2008-01-20 23:54:56 +00:00
|
|
|
|
|
|
|
finally:
|
2008-01-29 05:27:19 +00:00
|
|
|
print 'Stopping client node %d.' % (clientnum,)
|
|
|
|
self.stop_node(base)
|
2008-01-20 23:54:56 +00:00
|
|
|
|
2008-01-21 01:47:47 +00:00
|
|
|
def create_test_dirnode_layer(self):
|
|
|
|
print 'Creating test dirnode.'
|
2008-01-21 03:02:20 +00:00
|
|
|
|
2008-01-30 09:56:25 +00:00
|
|
|
cap = self.create_dirnode()
|
2008-01-21 01:47:47 +00:00
|
|
|
|
2008-01-29 05:27:19 +00:00
|
|
|
f = open(os.path.join(self.clientbase, 'private', 'root_dir.cap'), 'w')
|
2008-01-21 01:47:47 +00:00
|
|
|
f.write(cap)
|
|
|
|
f.close()
|
|
|
|
|
2008-01-30 09:46:24 +00:00
|
|
|
return self.mount_fuse_layer(cap)
|
2008-01-21 01:47:47 +00:00
|
|
|
|
2008-06-01 03:16:05 +00:00
|
|
|
def mount_fuse_layer(self, fusebasecap):
|
2008-06-07 05:19:23 +00:00
|
|
|
mpbase = os.path.join(self.testroot, 'mountpoint')
|
|
|
|
os.mkdir(mpbase)
|
2008-01-29 05:39:13 +00:00
|
|
|
|
2008-06-07 05:19:23 +00:00
|
|
|
results = []
|
2008-01-21 01:47:47 +00:00
|
|
|
|
2008-06-07 05:19:23 +00:00
|
|
|
# Mount and test each implementation:
|
2008-06-07 07:08:25 +00:00
|
|
|
for implnum, implmanklass in enumerate([Impl_A_ProcessManager, Impl_B_ProcessManager]):
|
|
|
|
implman = implmanklass(self.clientbase, mpbase)
|
|
|
|
print '\n*** Testing impl #%d: %r' % (implnum, implman.Name)
|
2008-06-01 03:16:05 +00:00
|
|
|
|
2008-06-07 07:08:25 +00:00
|
|
|
implman.setup()
|
2008-01-29 05:27:19 +00:00
|
|
|
|
2008-06-07 05:19:23 +00:00
|
|
|
try:
|
2008-06-07 07:08:25 +00:00
|
|
|
failures, total = self.run_test_layer(fusebasecap, implman)
|
|
|
|
result = (implman.Name, failures, total)
|
|
|
|
tmpl = '\n*** Test Results implementation %s: %d failed out of %d.'
|
|
|
|
print tmpl % result
|
|
|
|
results.append(result)
|
2008-06-07 05:19:23 +00:00
|
|
|
|
|
|
|
finally:
|
2008-06-07 07:08:25 +00:00
|
|
|
implman.cleanup()
|
2008-06-07 05:19:23 +00:00
|
|
|
|
|
|
|
return results
|
|
|
|
|
2008-06-07 07:08:25 +00:00
|
|
|
def run_test_layer(self, fbcap, iman):
|
2008-06-07 05:19:23 +00:00
|
|
|
testnames = [n for n in sorted(dir(self)) if n.startswith('test_')]
|
2008-06-07 06:19:15 +00:00
|
|
|
|
|
|
|
failures = 0
|
2008-06-07 07:08:25 +00:00
|
|
|
for testnum, testname in enumerate(testnames):
|
|
|
|
print '\n*** Running test #%d: %s' % (testnum, testname)
|
2008-06-07 05:19:23 +00:00
|
|
|
try:
|
|
|
|
testcap = self.create_dirnode()
|
2008-06-07 07:08:25 +00:00
|
|
|
dirname = '%s_%s' % (iman.Name, testname)
|
|
|
|
self.attach_node(fbcap, testcap, dirname)
|
2008-01-30 09:57:54 +00:00
|
|
|
|
2008-06-07 07:08:25 +00:00
|
|
|
method = getattr(self, testname)
|
|
|
|
method(testcap, testdir = os.path.join(iman.mountpath, dirname))
|
2008-06-07 05:19:23 +00:00
|
|
|
print 'Test succeeded.'
|
2008-06-07 07:06:00 +00:00
|
|
|
except TestFailure, f:
|
2008-06-07 05:19:23 +00:00
|
|
|
print f
|
|
|
|
failures += 1
|
|
|
|
except:
|
|
|
|
print 'Error in test code... Cleaning up.'
|
|
|
|
raise
|
2008-01-29 05:42:28 +00:00
|
|
|
|
2008-06-07 06:19:15 +00:00
|
|
|
return (failures, len(testnames))
|
2008-01-29 05:25:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Tests:
|
2008-01-30 09:57:54 +00:00
|
|
|
def test_directory_existence(self, testcap, testdir):
|
2008-06-07 07:06:00 +00:00
|
|
|
if not wrap_os_error(os.path.isdir, testdir):
|
|
|
|
raise TestFailure('Attached test directory not found: %r', testdir)
|
2008-01-30 09:57:54 +00:00
|
|
|
|
|
|
|
def test_empty_directory_listing(self, testcap, testdir):
|
2008-06-07 07:07:18 +00:00
|
|
|
listing = wrap_os_error(os.listdir, testdir)
|
2008-01-29 05:40:47 +00:00
|
|
|
if listing:
|
2008-06-07 07:06:00 +00:00
|
|
|
raise TestFailure('Expected empty directory, found: %r', listing)
|
2008-01-29 05:25:11 +00:00
|
|
|
|
2008-01-30 09:59:43 +00:00
|
|
|
def test_directory_listing(self, testcap, testdir):
|
|
|
|
names = []
|
|
|
|
filesizes = {}
|
|
|
|
|
|
|
|
for i in range(3):
|
|
|
|
fname = 'file_%d' % (i,)
|
|
|
|
names.append(fname)
|
|
|
|
body = 'Hello World #%d!' % (i,)
|
|
|
|
filesizes[fname] = len(body)
|
|
|
|
|
|
|
|
cap = self.webapi_call('PUT', '/uri', body)
|
|
|
|
self.attach_node(testcap, cap, fname)
|
|
|
|
|
|
|
|
dname = 'dir_%d' % (i,)
|
|
|
|
names.append(dname)
|
|
|
|
|
|
|
|
cap = self.create_dirnode()
|
|
|
|
self.attach_node(testcap, cap, dname)
|
|
|
|
|
|
|
|
names.sort()
|
|
|
|
|
2008-06-07 07:07:18 +00:00
|
|
|
listing = wrap_os_error(os.listdir, testdir)
|
2008-01-30 09:59:43 +00:00
|
|
|
listing.sort()
|
2008-06-07 07:08:25 +00:00
|
|
|
|
2008-01-30 09:59:43 +00:00
|
|
|
if listing != names:
|
|
|
|
tmpl = 'Expected directory list containing %r but fuse gave %r'
|
2008-06-07 07:06:00 +00:00
|
|
|
raise TestFailure(tmpl, names, listing)
|
2008-01-30 09:59:43 +00:00
|
|
|
|
|
|
|
for file, size in filesizes.items():
|
2008-06-07 07:07:18 +00:00
|
|
|
st = wrap_os_error(os.stat, os.path.join(testdir, file))
|
2008-01-30 09:59:43 +00:00
|
|
|
if st.st_size != size:
|
|
|
|
tmpl = 'Expected %r size of %r but fuse returned %r'
|
2008-06-07 07:06:00 +00:00
|
|
|
raise TestFailure(tmpl, file, size, st.st_size)
|
2008-01-30 09:59:43 +00:00
|
|
|
|
2008-01-30 10:14:48 +00:00
|
|
|
def test_file_contents(self, testcap, testdir):
|
|
|
|
name = 'hw.txt'
|
|
|
|
body = 'Hello World!'
|
|
|
|
|
|
|
|
cap = self.webapi_call('PUT', '/uri', body)
|
|
|
|
self.attach_node(testcap, cap, name)
|
|
|
|
|
|
|
|
path = os.path.join(testdir, name)
|
|
|
|
try:
|
|
|
|
found = open(path, 'r').read()
|
|
|
|
except Exception, err:
|
|
|
|
tmpl = 'Could not read file contents of %r: %r'
|
2008-06-07 07:06:00 +00:00
|
|
|
raise TestFailure(tmpl, path, err)
|
2008-01-30 10:14:48 +00:00
|
|
|
|
|
|
|
if found != body:
|
|
|
|
tmpl = 'Expected file contents %r but found %r'
|
2008-06-07 07:06:00 +00:00
|
|
|
raise TestFailure(tmpl, body, found)
|
2008-01-30 10:14:48 +00:00
|
|
|
|
2008-01-30 09:59:43 +00:00
|
|
|
|
2008-01-20 23:54:56 +00:00
|
|
|
# Utilities:
|
|
|
|
def run_tahoe(self, *args):
|
|
|
|
realargs = ('tahoe',) + args
|
|
|
|
status, output = gather_output(realargs, executable=self.cliexec)
|
|
|
|
if status != 0:
|
|
|
|
tmpl = 'The tahoe cli exited with nonzero status.\n'
|
|
|
|
tmpl += 'Executable: %r\n'
|
|
|
|
tmpl += 'Command arguments: %r\n'
|
|
|
|
tmpl += 'Exit status: %r\n'
|
|
|
|
tmpl += 'Output:\n%s\n[End of tahoe output.]\n'
|
2008-06-07 07:06:00 +00:00
|
|
|
raise SetupFailure(tmpl,
|
2008-01-20 23:54:56 +00:00
|
|
|
self.cliexec,
|
|
|
|
realargs,
|
|
|
|
status,
|
|
|
|
output)
|
|
|
|
return output
|
|
|
|
|
|
|
|
def check_tahoe_output(self, output, expected, expdir):
|
|
|
|
m = re.match(expected, output, re.M)
|
|
|
|
if m is None:
|
|
|
|
tmpl = 'The output of tahoe did not match the expectation:\n'
|
|
|
|
tmpl += 'Expected regex: %s\n'
|
|
|
|
tmpl += 'Actual output: %r\n'
|
2008-01-30 08:10:53 +00:00
|
|
|
self.warn(tmpl, expected, output)
|
2008-01-20 23:54:56 +00:00
|
|
|
|
2008-01-30 08:10:53 +00:00
|
|
|
elif expdir != m.group('path'):
|
2008-01-20 23:54:56 +00:00
|
|
|
tmpl = 'The output of tahoe refers to an unexpected directory:\n'
|
|
|
|
tmpl += 'Expected directory: %r\n'
|
|
|
|
tmpl += 'Actual directory: %r\n'
|
2008-01-30 08:10:53 +00:00
|
|
|
self.warn(tmpl, expdir, m.group(1))
|
2008-01-20 23:54:56 +00:00
|
|
|
|
2008-01-29 05:27:19 +00:00
|
|
|
def stop_node(self, basedir):
|
2008-01-21 00:54:48 +00:00
|
|
|
try:
|
2008-01-29 05:27:19 +00:00
|
|
|
self.run_tahoe('stop', '--basedir', basedir)
|
2008-01-21 00:54:48 +00:00
|
|
|
except Exception, e:
|
2008-01-29 05:27:19 +00:00
|
|
|
print 'Failed to stop tahoe node.'
|
|
|
|
print 'Ignoring cleanup exception:'
|
|
|
|
# Indent the exception description:
|
|
|
|
desc = str(e).rstrip()
|
|
|
|
print ' ' + desc.replace('\n', '\n ')
|
2008-01-20 23:54:56 +00:00
|
|
|
|
2008-01-30 09:56:25 +00:00
|
|
|
def webapi_call(self, method, path, body=None, **options):
|
|
|
|
if options:
|
|
|
|
path = path + '?' + ('&'.join(['%s=%s' % kv for kv in options.items()]))
|
|
|
|
|
|
|
|
conn = httplib.HTTPConnection('127.0.0.1', self.port)
|
|
|
|
conn.request(method, path, body = body)
|
|
|
|
resp = conn.getresponse()
|
|
|
|
|
|
|
|
if resp.status != 200:
|
|
|
|
tmpl = 'A webapi operation failed.\n'
|
|
|
|
tmpl += 'Request: %r %r\n'
|
|
|
|
tmpl += 'Body:\n%s\n'
|
|
|
|
tmpl += 'Response:\nStatus %r\nBody:\n%s'
|
2008-06-07 07:06:00 +00:00
|
|
|
raise SetupFailure(tmpl,
|
2008-01-30 09:56:25 +00:00
|
|
|
method, path,
|
|
|
|
body or '',
|
|
|
|
resp.status, body)
|
|
|
|
|
|
|
|
return resp.read()
|
|
|
|
|
|
|
|
def create_dirnode(self):
|
|
|
|
return self.webapi_call('PUT', '/uri', t='mkdir').strip()
|
|
|
|
|
|
|
|
def attach_node(self, dircap, childcap, childname):
|
|
|
|
body = self.webapi_call('PUT',
|
|
|
|
'/uri/%s/%s' % (dircap, childname),
|
|
|
|
body = childcap,
|
|
|
|
t = 'uri',
|
|
|
|
replace = 'false')
|
|
|
|
assert body.strip() == childcap, `status, dircap, childcap, childname`
|
|
|
|
|
2008-06-07 06:17:19 +00:00
|
|
|
def polling_operation(self, operation, polldesc, timeout = 10.0, pollinterval = 0.2):
|
2008-01-21 01:47:47 +00:00
|
|
|
totaltime = timeout # Fudging for edge-case SetupFailure description...
|
|
|
|
|
|
|
|
totalattempts = int(timeout / pollinterval)
|
|
|
|
|
|
|
|
starttime = time.time()
|
|
|
|
for attempt in range(totalattempts):
|
|
|
|
opstart = time.time()
|
|
|
|
|
|
|
|
try:
|
|
|
|
result = operation()
|
|
|
|
except KeyboardInterrupt, e:
|
|
|
|
raise
|
|
|
|
except Exception, e:
|
|
|
|
result = False
|
|
|
|
|
|
|
|
totaltime = time.time() - starttime
|
|
|
|
|
|
|
|
if result is not False:
|
2008-01-29 05:27:19 +00:00
|
|
|
#tmpl = '(Polling took over %.2f seconds.)'
|
|
|
|
#print tmpl % (totaltime,)
|
2008-01-21 01:47:47 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
elif totaltime > timeout:
|
|
|
|
break
|
|
|
|
|
|
|
|
else:
|
|
|
|
opdelay = time.time() - opstart
|
|
|
|
realinterval = max(0., pollinterval - opdelay)
|
|
|
|
|
|
|
|
#tmpl = '(Poll attempt %d failed after %.2f seconds, sleeping %.2f seconds.)'
|
|
|
|
#print tmpl % (attempt+1, opdelay, realinterval)
|
|
|
|
time.sleep(realinterval)
|
|
|
|
|
2008-06-07 06:17:19 +00:00
|
|
|
|
|
|
|
tmpl = 'Timeout while polling for: %s\n'
|
2008-01-21 01:47:47 +00:00
|
|
|
tmpl += 'Waited %.2f seconds (%d polls).'
|
2008-06-07 07:06:00 +00:00
|
|
|
raise SetupFailure(tmpl, polldesc, totaltime, attempt+1)
|
2008-01-21 01:47:47 +00:00
|
|
|
|
2008-01-30 08:10:53 +00:00
|
|
|
def warn(self, tmpl, *args):
|
|
|
|
print ('Test Warning: ' + tmpl) % args
|
|
|
|
|
2008-01-29 05:40:47 +00:00
|
|
|
|
2008-06-07 07:06:00 +00:00
|
|
|
# SystemTest Exceptions:
|
|
|
|
class Failure (Exception):
|
|
|
|
def __init__(self, tmpl, *args):
|
|
|
|
msg = self.Prefix + (tmpl % args)
|
|
|
|
Exception.__init__(self, msg)
|
2008-01-20 23:54:56 +00:00
|
|
|
|
2008-06-07 07:06:00 +00:00
|
|
|
class SetupFailure (Failure):
|
|
|
|
Prefix = 'Setup Failure - The test framework encountered an error:\n'
|
2008-01-20 23:54:56 +00:00
|
|
|
|
2008-06-07 07:06:00 +00:00
|
|
|
class TestFailure (Failure):
|
|
|
|
Prefix = 'TestFailure: '
|
2008-01-29 05:40:47 +00:00
|
|
|
|
2008-01-20 23:54:56 +00:00
|
|
|
|
|
|
|
### Unit Tests:
|
2008-06-07 05:19:23 +00:00
|
|
|
class Impl_A_UnitTests (unittest.TestCase):
|
2008-01-13 02:56:03 +00:00
|
|
|
'''Tests small stand-alone functions.'''
|
|
|
|
def test_canonicalize_cap(self):
|
|
|
|
iopairs = [('http://127.0.0.1:8123/uri/URI:DIR2:yar9nnzsho6czczieeesc65sry:upp1pmypwxits3w9izkszgo1zbdnsyk3nm6h7e19s7os7s6yhh9y',
|
|
|
|
'URI:DIR2:yar9nnzsho6czczieeesc65sry:upp1pmypwxits3w9izkszgo1zbdnsyk3nm6h7e19s7os7s6yhh9y'),
|
|
|
|
('http://127.0.0.1:8123/uri/URI%3ACHK%3Ak7ktp1qr7szmt98s1y3ha61d9w%3A8tiy8drttp65u79pjn7hs31po83e514zifdejidyeo1ee8nsqfyy%3A3%3A12%3A242?filename=welcome.html',
|
|
|
|
'URI:CHK:k7ktp1qr7szmt98s1y3ha61d9w:8tiy8drttp65u79pjn7hs31po83e514zifdejidyeo1ee8nsqfyy:3:12:242?filename=welcome.html')]
|
|
|
|
|
|
|
|
for input, output in iopairs:
|
2008-06-07 05:19:23 +00:00
|
|
|
result = impl_a.canonicalize_cap(input)
|
2008-01-13 02:56:03 +00:00
|
|
|
self.failUnlessEqual(output, result, 'input == %r' % (input,))
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-01-20 23:54:56 +00:00
|
|
|
### Misc:
|
2008-06-07 07:08:25 +00:00
|
|
|
class ImplProcessManager (object):
|
|
|
|
'''Subclasses must have Name and Mod class attributes.'''
|
|
|
|
def __init__(self, clientbase, mpbase):
|
|
|
|
self.clientbase = clientbase
|
|
|
|
self.mountpath = os.path.join(mpbase, self.Name)
|
|
|
|
self.script = self.Mod.__file__
|
|
|
|
os.mkdir(self.mountpath)
|
|
|
|
|
|
|
|
|
|
|
|
class Impl_A_ProcessManager (ImplProcessManager):
|
|
|
|
Name = 'impl_a'
|
|
|
|
Mod = impl_a
|
|
|
|
|
|
|
|
def setup(self):
|
|
|
|
print 'Mounting implementation: %s' % (self.Name,)
|
|
|
|
exitcode, output = gather_output(['python',
|
|
|
|
self.script,
|
|
|
|
self.mountpath,
|
|
|
|
'--basedir', self.clientbase])
|
|
|
|
|
|
|
|
if exitcode != 0 or output:
|
|
|
|
tmpl = '%r failed to launch:\n'
|
|
|
|
tmpl += 'Exit Status: %r\n'
|
|
|
|
tmpl += 'Output:\n%s\n'
|
|
|
|
raise SetupFailure(tmpl, implpath, exitcode, output)
|
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
print 'Unmounting implementation: %s' % (self.Name,)
|
|
|
|
args = ['fusermount', '-u', self.mountpath]
|
|
|
|
ec, out = gather_output(args)
|
|
|
|
if ec != 0 or out:
|
|
|
|
tmpl = 'fusermount failed to unmount:\n'
|
|
|
|
tmpl += 'Arguments: %r\n'
|
|
|
|
tmpl += 'Exit Status: %r\n'
|
|
|
|
tmpl += 'Output:\n%s\n'
|
|
|
|
raise SetupFailure(tmpl, args, ec, out)
|
|
|
|
|
|
|
|
|
|
|
|
class Impl_B_ProcessManager (ImplProcessManager):
|
|
|
|
Name = 'impl_b'
|
|
|
|
Mod = impl_b
|
|
|
|
|
|
|
|
def setup(self):
|
|
|
|
print 'Mounting implementation: %s' % (self.Name,)
|
|
|
|
self.proc = subprocess.Popen(['python',
|
|
|
|
self.script,
|
|
|
|
self.mountpath,
|
|
|
|
'--basedir', self.clientbase])
|
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
print 'Unmounting implementation: %s' % (self.Name,)
|
|
|
|
args = ['fusermount', '-u', self.mountpath]
|
|
|
|
ec, out = gather_output(args)
|
|
|
|
if ec != 0 or out:
|
|
|
|
tmpl = 'fusermount failed to unmount:\n'
|
|
|
|
tmpl += 'Arguments: %r\n'
|
|
|
|
tmpl += 'Exit Status: %r\n'
|
|
|
|
tmpl += 'Output:\n%s\n'
|
|
|
|
raise SetupFailure(tmpl, args, ec, out)
|
|
|
|
|
|
|
|
|
2008-01-20 23:54:56 +00:00
|
|
|
def gather_output(*args, **kwargs):
|
|
|
|
'''
|
|
|
|
This expects the child does not require input and that it closes
|
|
|
|
stdout/err eventually.
|
|
|
|
'''
|
|
|
|
p = subprocess.Popen(stdout = subprocess.PIPE,
|
|
|
|
stderr = subprocess.STDOUT,
|
|
|
|
*args,
|
|
|
|
**kwargs)
|
|
|
|
output = p.stdout.read()
|
|
|
|
exitcode = p.wait()
|
|
|
|
return (exitcode, output)
|
|
|
|
|
2008-01-30 08:10:53 +00:00
|
|
|
|
2008-06-07 07:07:18 +00:00
|
|
|
def wrap_os_error(meth, *args):
|
|
|
|
try:
|
|
|
|
return meth(*args)
|
|
|
|
except os.error, e:
|
|
|
|
raise TestFailure('%s', e)
|
|
|
|
|
|
|
|
|
2008-01-30 08:10:53 +00:00
|
|
|
ExpectedCreationOutput = r'(introducer|client) created in (?P<path>.*?)\n'
|
|
|
|
ExpectedStartOutput = r'STARTING (?P<path>.*?)\n(introducer|client) node probably started'
|
|
|
|
|
|
|
|
|
2008-01-20 23:54:56 +00:00
|
|
|
Usage = '''
|
|
|
|
Usage: %s [target]
|
|
|
|
|
|
|
|
Run tests for the given target.
|
|
|
|
|
|
|
|
target is one of: unit, system, or all
|
|
|
|
''' % (sys.argv[0],)
|
|
|
|
|
2008-01-13 02:56:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2008-01-20 23:54:56 +00:00
|
|
|
main()
|