Commit df29bab7 authored by Łukasz Nowak's avatar Łukasz Nowak

Implement own simple server to do the tests.

parent a3c9c616
import BaseHTTPServer
import errno
import hashlib
import httplib
import json
import os
import random
import shutil
import socket
import tempfile
import threading
import time
import unittest
from slapos.libnetworkcache import NetworkcacheClient
from slapos.signature import parseArgument, \
createPrivateKeyAndCertificateFile
class NCHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def __init__(self, request, address, server):
self.__server = server
self.tree = server.tree
BaseHTTPServer.BaseHTTPRequestHandler.__init__(
self, request, address, server)
def do_KILL(self):
raise SystemExit
def do_GET(self):
path = os.path.abspath(os.path.join(self.tree, *self.path.split('/')))
if not (
((path == self.tree) or path.startswith(self.tree+os.path.sep))
and
os.path.exists(path)
):
self.send_response(404, 'Not Found')
return
self.send_response(200)
out = open(path, 'rb').read()
self.send_header('Content-Length', len(out))
self.end_headers()
self.wfile.write(out)
def do_POST(self):
path = os.path.abspath(os.path.join(self.tree, *self.path.split('/')))
if not os.path.exists(path):
os.makedirs(path)
data = self.rfile.read(int(self.headers.getheader('content-length')))
cksum = hashlib.sha512(data).hexdigest()
if 'shadir' in path:
d = json.loads(data)
path = os.path.join(path, d[0]['urlmd5'])
data = json.dumps([d])
if os.path.exists(path):
f = open(path, 'r')
try:
file_data = f.read()
finally:
f.close()
file_data = file_data.strip()
json_data_list = json.loads(file_data)
json_data_list.append(d)
data = json.dumps(json_data_list)
else:
path = os.path.join(path, cksum)
open(path, 'wb').write(data)
self.send_response(201)
self.send_header('Content-Length', str(len(cksum)))
self.send_header('Content-Type', 'text/html')
self.end_headers()
self.wfile.write(cksum)
return
class Server(BaseHTTPServer.HTTPServer):
def __init__(self, tree, *args):
BaseHTTPServer.HTTPServer.__init__(self, *args)
self.tree = os.path.abspath(tree)
__run = True
def serve_forever(self):
while self.__run:
self.handle_request()
def handle_error(self, *_):
self.__run = False
def _run_nc(tree, host, port):
server_address = (host, port)
httpd = Server(tree, server_address, NCHandler)
httpd.serve_forever()
class OfflineTest(unittest.TestCase):
def test_download_offline(self):
nc = NetworkcacheClient('http://127.0.0.1:0', 'http://127.0.0.1:0')
......@@ -17,11 +100,38 @@ class OfflineTest(unittest.TestCase):
nc = NetworkcacheClient('http://127.0.0.1:0', 'http://127.0.0.1:0')
self.assertRaises(IOError, nc.upload, content)
def wait(host, port):
addr = host, port
for i in range(120):
time.sleep(0.25)
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(addr)
s.close()
break
except socket.error, e:
if e[0] not in (errno.ECONNREFUSED, errno.ECONNRESET):
raise
s.close()
else:
raise
class OnlineTest(unittest.TestCase):
"""Online tests against real HTTP server"""
def setUp(self):
self.shacache = os.environ.get('TEST_SHA_CACHE', 'http://127.0.0.1:8080')
self.shadir = os.environ.get('TEST_SHA_DIR', 'http://127.0.0.1:8080')
self.host = "127.0.0.1"
self.port = 8080
self.tree = tempfile.mkdtemp()
self.thread = threading.Thread(target=_run_nc, args=(self.tree,
self.host, self.port))
self.thread.setDaemon(True)
self.thread.start()
wait(self.host, self.port)
self.url = 'http://%s:%s/' % (self.host, self.port)
self.shacache = os.environ.get('TEST_SHA_CACHE',
self.url + 'shacache')
self.shadir = os.environ.get('TEST_SHA_DIR',
self.url + 'shadir')
self.test_data = tempfile.TemporaryFile()
self.test_string = str(random.random())
self.test_data.write(self.test_string)
......@@ -29,6 +139,13 @@ class OnlineTest(unittest.TestCase):
self.test_shasum = hashlib.sha512(self.test_data.read()).hexdigest()
self.test_data.seek(0)
def tearDown(self):
httplib.HTTPConnection(self.host, self.port).request('KILL', '/')
if self.thread is not None:
self.thread.join()
shutil.rmtree(self.tree)
def test_upload(self):
nc = NetworkcacheClient(self.shacache, self.shadir)
nc.upload(self.test_data)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment