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