summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan Callaghan <dcallagh@redhat.com>2014-06-25 12:42:13 +1000
committerGerrit Code Review <gerrit@beaker-project.org>2014-06-26 05:40:57 +0000
commit715bbb42ade0043437bb32aa0a2aeedb2bb535e5 (patch)
treeea9142612909dedd4a6cf99c5e74fb401e3f25c6
parent0efb8c7a216ec39fce5d4e99adb16af7f4888700 (diff)
tests: consolidate http_server.py and archive_server.py
-rwxr-xr-xIntegrationTests/src/bkr/inttest/archive_server.py68
-rwxr-xr-xIntegrationTests/src/bkr/inttest/http_server.py70
-rw-r--r--IntegrationTests/src/bkr/inttest/labcontroller/test_distro_import.py8
-rw-r--r--IntegrationTests/src/bkr/inttest/labcontroller/test_pxemenu.py5
-rw-r--r--IntegrationTests/src/bkr/inttest/server/tools/test_log_delete.py9
-rw-r--r--IntegrationTests/src/bkr/inttest/server/tools/test_sync_tasks.py4
6 files changed, 76 insertions, 88 deletions
diff --git a/IntegrationTests/src/bkr/inttest/archive_server.py b/IntegrationTests/src/bkr/inttest/archive_server.py
deleted file mode 100755
index e7ffd01..0000000
--- a/IntegrationTests/src/bkr/inttest/archive_server.py
+++ /dev/null
@@ -1,68 +0,0 @@
-#!/usr/bin/python
-
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
-
-"""
-A fake archive server for Beaker logs. It answers the following paths:
-
-/redirect/<status>/<path>
- Responds with a <status> redirect to <path>.
-
-/<path>
- Allows GET and DELETE of files underneath the base path of the server.
-"""
-
-import os, os.path
-import re
-import shutil
-import urlparse
-import wsgiref.util, wsgiref.simple_server
-
-class ArchiveServer(object):
-
- def __init__(self, basepath):
- assert basepath not in (None, '', '/')
- self.basepath = basepath
-
- def wsgi(self, environ, start_response):
- m = re.match(r'/redirect/(\d+)/(.*)$', environ['PATH_INFO'])
- if m:
- start_response('%s Redirected' % m.group(1), [('Location',
- wsgiref.util.application_uri(environ) + m.group(2))])
- return []
- assert '..' not in environ['PATH_INFO']
- assert environ['PATH_INFO'].startswith('/')
- localpath = os.path.join(self.basepath, environ['PATH_INFO'].lstrip('/'))
- if os.path.isdir(localpath) and not localpath.endswith('/'):
- start_response('301 Moved', [('Location',
- wsgiref.util.request_uri(environ, include_query=False) + '/')])
- return []
- if not os.path.exists(localpath):
- start_response('404 Not Found', [])
- return []
- if environ['REQUEST_METHOD'] == 'GET':
- start_response('200 OK', [])
- if os.path.isdir(localpath):
- return '\n'.join(os.listdir(localpath))
- else:
- return wsgiref.util.FileWrapper(open(localpath, 'r'))
- elif environ['REQUEST_METHOD'] == 'DELETE':
- shutil.rmtree(localpath)
- start_response('204 No Content', [])
- return []
- else:
- start_response('405 Method Not Allowed', [])
- return []
-
-if __name__ == '__main__':
- from optparse import OptionParser
- parser = OptionParser()
- parser.add_option('--base', metavar='PATH')
- opts, args = parser.parse_args()
- if not opts.base:
- parser.error('Specify base directory with --base')
- server = wsgiref.simple_server.make_server('', 19998, ArchiveServer(opts.base).wsgi)
- server.serve_forever()
diff --git a/IntegrationTests/src/bkr/inttest/http_server.py b/IntegrationTests/src/bkr/inttest/http_server.py
index c5d334c..dd0fe8b 100755
--- a/IntegrationTests/src/bkr/inttest/http_server.py
+++ b/IntegrationTests/src/bkr/inttest/http_server.py
@@ -1,20 +1,72 @@
+#!/usr/bin/python
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
-import SimpleHTTPServer
-import SocketServer
+"""
+A simple HTTP server for testing. It serves the directory tree under the base
+directory given with --base. DELETE requests are allowed if --writable is given.
+It also treats the following paths specially:
-class HTTPServer(SocketServer.TCPServer):
+/redirect/<status>/<path>
+ Responds with a <status> redirect to <path>.
+"""
- def __init__(self, port):
- self.allow_reuse_address = True
- self.handler = SimpleHTTPServer.SimpleHTTPRequestHandler
- SocketServer.TCPServer.__init__(self, ('', port), self.handler)
+import os, os.path
+import re
+import shutil
+import urlparse
+import wsgiref.util, wsgiref.simple_server
-if __name__ in ('main', '__main__'):
- server = HTTPServer(19998)
+class Application(object):
+
+ def __init__(self, basepath, writable=False):
+ assert basepath not in (None, '', '/')
+ self.basepath = basepath
+ self.writable = writable
+
+ def __call__(self, environ, start_response):
+ path_info = os.path.normpath(environ['PATH_INFO'])
+ assert '..' not in path_info
+ assert path_info.startswith('/')
+ m = re.match(r'/redirect/(\d+)/(.*)$', path_info)
+ if m:
+ start_response('%s Redirected' % m.group(1), [('Location',
+ wsgiref.util.application_uri(environ) + m.group(2))])
+ return []
+ localpath = os.path.join(self.basepath, path_info.lstrip('/'))
+ if os.path.isdir(localpath) and not environ['PATH_INFO'].endswith('/'):
+ start_response('301 Moved', [('Location',
+ wsgiref.util.request_uri(environ, include_query=False) + '/')])
+ return []
+ if not os.path.exists(localpath):
+ start_response('404 Not Found', [])
+ return []
+ if environ['REQUEST_METHOD'] == 'GET':
+ start_response('200 OK', [])
+ if os.path.isdir(localpath):
+ return '\n'.join(os.listdir(localpath))
+ else:
+ return wsgiref.util.FileWrapper(open(localpath, 'r'))
+ elif environ['REQUEST_METHOD'] == 'DELETE' and self.writable:
+ shutil.rmtree(localpath)
+ start_response('204 No Content', [])
+ return []
+ else:
+ start_response('405 Method Not Allowed', [])
+ return []
+
+if __name__ == '__main__':
+ from optparse import OptionParser
+ parser = OptionParser()
+ parser.add_option('--base', metavar='PATH')
+ parser.add_option('--writable', action='store_true')
+ opts, args = parser.parse_args()
+ if not opts.base:
+ parser.error('Specify base directory with --base')
+ application = Application(opts.base, opts.writable)
+ server = wsgiref.simple_server.make_server('', 19998, application)
server.serve_forever()
diff --git a/IntegrationTests/src/bkr/inttest/labcontroller/test_distro_import.py b/IntegrationTests/src/bkr/inttest/labcontroller/test_distro_import.py
index 79730fa..a447a87 100644
--- a/IntegrationTests/src/bkr/inttest/labcontroller/test_distro_import.py
+++ b/IntegrationTests/src/bkr/inttest/labcontroller/test_distro_import.py
@@ -4,6 +4,7 @@
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
+import sys
import os
import unittest2 as unittest
import subprocess
@@ -38,9 +39,10 @@ class DistroImportTest(unittest.TestCase):
@classmethod
def setupClass(cls):
- cls.distro_server = Process('http_server.py',
- args=['python', os.path.join(_current_dir, '..', 'http_server.py'),],
- listen_port=19998, exec_dir=_compose_test_dir)
+ cls.distro_server = Process('http_server.py', [sys.executable,
+ pkg_resources.resource_filename('bkr.inttest', 'http_server.py'),
+ '--base', _compose_test_dir],
+ listen_port=19998)
cls.distro_server.start()
cls.distro_url = 'http://localhost:19998/'
diff --git a/IntegrationTests/src/bkr/inttest/labcontroller/test_pxemenu.py b/IntegrationTests/src/bkr/inttest/labcontroller/test_pxemenu.py
index 9397459..0329026 100644
--- a/IntegrationTests/src/bkr/inttest/labcontroller/test_pxemenu.py
+++ b/IntegrationTests/src/bkr/inttest/labcontroller/test_pxemenu.py
@@ -24,8 +24,9 @@ class PxemenuTest(LabControllerTestCase):
open(os.path.join(cls.distro_dir, 'pxeboot/vmlinuz'), 'w').write('lol')
open(os.path.join(cls.distro_dir, 'pxeboot/initrd'), 'w').write('lol')
cls.distro_server = Process('http_server.py', args=[sys.executable,
- pkg_resources.resource_filename('bkr.inttest', 'http_server.py')],
- listen_port=19998, exec_dir=cls.distro_dir)
+ pkg_resources.resource_filename('bkr.inttest', 'http_server.py'),
+ '--base', cls.distro_dir],
+ listen_port=19998)
cls.distro_server.start()
cls.tftp_dir = tempfile.mkdtemp()
diff --git a/IntegrationTests/src/bkr/inttest/server/tools/test_log_delete.py b/IntegrationTests/src/bkr/inttest/server/tools/test_log_delete.py
index a151ab7..5801a96 100644
--- a/IntegrationTests/src/bkr/inttest/server/tools/test_log_delete.py
+++ b/IntegrationTests/src/bkr/inttest/server/tools/test_log_delete.py
@@ -5,6 +5,7 @@
# (at your option) any later version.
import unittest2 as unittest
+import pkg_resources
import datetime
import os
import errno
@@ -176,10 +177,10 @@ class RemoteLogDeletionTest(unittest.TestCase):
else:
self.logs_dir = tempfile.mkdtemp(prefix='beaker-test-log-delete')
self.recipe_logs_dir = os.path.join(self.logs_dir, 'recipe')
- self.archive_server = Process('archive_server.py',
- args=['python', os.path.join(os.path.dirname(__file__), '..', '..', 'archive_server.py'),
- '--base', self.logs_dir],
- listen_port=19998)
+ self.archive_server = Process('http_server.py', args=[sys.executable,
+ pkg_resources.resource_filename('bkr.inttest', 'http_server.py'),
+ '--base', self.logs_dir, '--writable'],
+ listen_port=19998)
self.archive_server.start()
self.log_server = 'localhost:19998'
self.log_server_url = 'http://%s' % self.log_server
diff --git a/IntegrationTests/src/bkr/inttest/server/tools/test_sync_tasks.py b/IntegrationTests/src/bkr/inttest/server/tools/test_sync_tasks.py
index bc9910e..71c7913 100644
--- a/IntegrationTests/src/bkr/inttest/server/tools/test_sync_tasks.py
+++ b/IntegrationTests/src/bkr/inttest/server/tools/test_sync_tasks.py
@@ -24,8 +24,8 @@ class TestTaskLibrarySync(unittest.TestCase):
@classmethod
def setupClass(cls):
cls.task_server = Process('http_server.py',
- args=[sys.executable, _http_server,],
- listen_port=19998, exec_dir=_sync_tasks_dir)
+ args=[sys.executable, _http_server, '--base', _sync_tasks_dir],
+ listen_port=19998)
cls.task_server.start()
cls.task_url = 'http://localhost:19998/'