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

Implement negative scenarios.

parent df29bab7
...@@ -11,7 +11,7 @@ import tempfile ...@@ -11,7 +11,7 @@ import tempfile
import threading import threading
import time import time
import unittest import unittest
from slapos.libnetworkcache import NetworkcacheClient from slapos.libnetworkcache import NetworkcacheClient, UploadError
from slapos.signature import parseArgument, \ from slapos.signature import parseArgument, \
createPrivateKeyAndCertificateFile createPrivateKeyAndCertificateFile
...@@ -72,6 +72,21 @@ class NCHandler(BaseHTTPServer.BaseHTTPRequestHandler): ...@@ -72,6 +72,21 @@ class NCHandler(BaseHTTPServer.BaseHTTPRequestHandler):
self.wfile.write(cksum) self.wfile.write(cksum)
return return
class NCHandlerPOST200(NCHandler):
def do_POST(self):
self.send_response(200)
return
class NCHandlerReturnWrong(NCHandler):
def do_POST(self):
cksum = 'incorrect'
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): class Server(BaseHTTPServer.HTTPServer):
def __init__(self, tree, *args): def __init__(self, tree, *args):
BaseHTTPServer.HTTPServer.__init__(self, *args) BaseHTTPServer.HTTPServer.__init__(self, *args)
...@@ -116,17 +131,19 @@ def wait(host, port): ...@@ -116,17 +131,19 @@ def wait(host, port):
else: else:
raise raise
class OnlineTest(unittest.TestCase): class OnlineMixin:
"""Online tests against real HTTP server""" def _start_nc(self):
def setUp(self):
self.host = "127.0.0.1"
self.port = 8080
self.tree = tempfile.mkdtemp()
self.thread = threading.Thread(target=_run_nc, args=(self.tree, self.thread = threading.Thread(target=_run_nc, args=(self.tree,
self.host, self.port)) self.host, self.port))
self.thread.setDaemon(True) self.thread.setDaemon(True)
self.thread.start() self.thread.start()
wait(self.host, self.port) wait(self.host, self.port)
def setUp(self):
self.host = "127.0.0.1"
self.port = 8080
self.tree = tempfile.mkdtemp()
self._start_nc()
self.url = 'http://%s:%s/' % (self.host, self.port) self.url = 'http://%s:%s/' % (self.host, self.port)
self.shacache = os.environ.get('TEST_SHA_CACHE', self.shacache = os.environ.get('TEST_SHA_CACHE',
self.url + 'shacache') self.url + 'shacache')
...@@ -140,24 +157,21 @@ class OnlineTest(unittest.TestCase): ...@@ -140,24 +157,21 @@ class OnlineTest(unittest.TestCase):
self.test_data.seek(0) self.test_data.seek(0)
def tearDown(self): def tearDown(self):
httplib.HTTPConnection(self.host, self.port).request('KILL', '/') try:
httplib.HTTPConnection(self.host, self.port).request('KILL', '/')
except Exception:
pass
if self.thread is not None: if self.thread is not None:
self.thread.join() self.thread.join()
shutil.rmtree(self.tree) shutil.rmtree(self.tree)
class OnlineTest(OnlineMixin, unittest.TestCase):
"""Online tests against real HTTP server"""
def test_upload(self): def test_upload(self):
nc = NetworkcacheClient(self.shacache, self.shadir) nc = NetworkcacheClient(self.shacache, self.shadir)
nc.upload(self.test_data) nc.upload(self.test_data)
def test_upload_wrong_return_code(self):
"""Check reaction on HTTP return code different then 201"""
raise NotImplementedError
def test_upload_wrong_return_sha(self):
"""Check reaction in case of wrong sha returned"""
raise NotImplementedError
def test_upload_shadir(self): def test_upload_shadir(self):
"""Check scenario with shadir used""" """Check scenario with shadir used"""
nc = NetworkcacheClient(self.shacache, self.shadir) nc = NetworkcacheClient(self.shacache, self.shadir)
...@@ -198,6 +212,44 @@ class OnlineTest(unittest.TestCase): ...@@ -198,6 +212,44 @@ class OnlineTest(unittest.TestCase):
# is it correctly downloaded # is it correctly downloaded
self.assertEqual(result.read(), self.test_string) self.assertEqual(result.read(), self.test_string)
def _run_nc_POST200(tree, host, port):
server_address = (host, port)
httpd = Server(tree, server_address, NCHandlerPOST200)
httpd.serve_forever()
class OnlineTestPOST200(OnlineMixin, unittest.TestCase):
def _start_nc(self):
self.thread = threading.Thread(target=_run_nc_POST200, args=(self.tree,
self.host, self.port))
self.thread.setDaemon(True)
self.thread.start()
wait(self.host, self.port)
def test_upload_wrong_return_code(self):
"""Check reaction on HTTP return code different then 201"""
nc = NetworkcacheClient(self.shacache, self.shadir)
self.assertRaises(UploadError, nc.upload, self.test_data)
def _run_nc_POSTWrongChecksum(tree, host, port):
server_address = (host, port)
httpd = Server(tree, server_address, NCHandlerReturnWrong)
httpd.serve_forever()
class OnlineTestWrongChecksum(OnlineMixin, unittest.TestCase):
def _start_nc(self):
self.thread = threading.Thread(target=_run_nc_POSTWrongChecksum,
args=(self.tree, self.host, self.port))
self.thread.setDaemon(True)
self.thread.start()
wait(self.host, self.port)
def test_upload_wrong_return_sha(self):
"""Check reaction in case of wrong sha returned"""
httplib.HTTPConnection(self.host, self.port).request('SPECIAL',
'/cksum/invalid')
nc = NetworkcacheClient(self.shacache, self.shadir)
self.assertRaises(UploadError, nc.upload, self.test_data)
class LibNetworkCacheMixin(unittest.TestCase): class LibNetworkCacheMixin(unittest.TestCase):
def setUp(self): def setUp(self):
......
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