Commit a2ee9fa6 authored by Julien Muchembled's avatar Julien Muchembled

fake test server: more checks and print exceptions to stdout

parent 9658123b
...@@ -8,12 +8,12 @@ import os ...@@ -8,12 +8,12 @@ import os
import urllib2 import urllib2
import random import random
import shutil import shutil
import socket
import ssl import ssl
import subprocess import subprocess
import tempfile import tempfile
import threading import threading
import time import time
import traceback
import unittest import unittest
import slapos.libnetworkcache import slapos.libnetworkcache
import slapos.signature import slapos.signature
...@@ -28,6 +28,13 @@ class NCHandler(BaseHTTPServer.BaseHTTPRequestHandler): ...@@ -28,6 +28,13 @@ class NCHandler(BaseHTTPServer.BaseHTTPRequestHandler):
BaseHTTPServer.BaseHTTPRequestHandler.__init__( BaseHTTPServer.BaseHTTPRequestHandler.__init__(
self, request, address, server) self, request, address, server)
def handle(self):
try:
BaseHTTPServer.BaseHTTPRequestHandler.handle(self)
except Exception:
traceback.print_exc()
raise
def do_KILL(self): def do_KILL(self):
raise SystemExit raise SystemExit
...@@ -47,26 +54,20 @@ class NCHandler(BaseHTTPServer.BaseHTTPRequestHandler): ...@@ -47,26 +54,20 @@ class NCHandler(BaseHTTPServer.BaseHTTPRequestHandler):
self.wfile.write(out) self.wfile.write(out)
def do_PUT(self): def do_PUT(self):
assert 'shadir' in self.path
assert self.headers.getheader('content-type') == 'application/json'
path = os.path.abspath(os.path.join(self.tree, *self.path.split('/'))) path = os.path.abspath(os.path.join(self.tree, *self.path.split('/')))
if not os.path.exists(os.path.dirname(path)): if not os.path.exists(os.path.dirname(path)):
os.makedirs(os.path.dirname(path)) os.makedirs(os.path.dirname(path))
data = self.rfile.read(int(self.headers.getheader('content-length'))) data = self.rfile.read(int(self.headers.getheader('content-length')))
cksum = hashlib.sha512(data).hexdigest() cksum = hashlib.sha512(data).hexdigest()
if 'shadir' in path:
d = json.loads(data)
data = json.dumps([d])
if os.path.exists(path): if os.path.exists(path):
f = open(path, 'r') with open(path, 'rb') as f:
try: json_data_list = json.loads(f.read().strip())
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: else:
raise ValueError('shacache shall use POST') json_data_list = []
json_data_list.append(json.loads(data))
data = json.dumps(json_data_list)
open(path, 'wb').write(data) open(path, 'wb').write(data)
self.send_response(201) self.send_response(201)
...@@ -77,14 +78,12 @@ class NCHandler(BaseHTTPServer.BaseHTTPRequestHandler): ...@@ -77,14 +78,12 @@ class NCHandler(BaseHTTPServer.BaseHTTPRequestHandler):
return return
def do_POST(self): def do_POST(self):
assert 'shacache' in self.path
path = os.path.abspath(os.path.join(self.tree, *self.path.split('/'))) path = os.path.abspath(os.path.join(self.tree, *self.path.split('/')))
if not os.path.exists(path): if not os.path.exists(path):
os.makedirs(path) os.makedirs(path)
data = self.rfile.read(int(self.headers.getheader('content-length'))) data = self.rfile.read(int(self.headers.getheader('content-length')))
cksum = hashlib.sha512(data).hexdigest() cksum = hashlib.sha512(data).hexdigest()
if 'shadir' in path:
raise ValueError('shadir shall use PUT')
else:
path = os.path.join(path, cksum) path = os.path.join(path, cksum)
open(path, 'wb').write(data) open(path, 'wb').write(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