Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
Merge fake_http_server and fake_test_server
Browse files Browse the repository at this point in the history
Signed-off-by: Laurent Bonnans <[email protected]>
  • Loading branch information
lbonn committed Apr 29, 2019
1 parent 168508b commit cb99aa6
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 135 deletions.
2 changes: 1 addition & 1 deletion src/libaktualizr/http/httpclient_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ int main(int argc, char** argv) {

std::string port = TestUtils::getFreePort();
server += port;
boost::process::child server_process("tests/fake_http_server/fake_http_server.py", port);
boost::process::child server_process("tests/fake_http_server/fake_test_server.py", port, "-f");
TestUtils::waitForServer(server + "/");

return RUN_ALL_TESTS();
Expand Down
2 changes: 1 addition & 1 deletion src/libaktualizr/package_manager/fetcher_death_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ int main(int argc, char** argv) {
std::string port = TestUtils::getFreePort();
server = "http://127.0.0.1:" + port;
config.uptane.repo_server = server;
boost::process::child http_server_process("tests/fake_http_server/fake_http_server.py", port);
boost::process::child http_server_process("tests/fake_http_server/fake_test_server.py", port, "-f");
TestUtils::waitForServer(server + "/");
return RUN_ALL_TESTS();
}
Expand Down
2 changes: 1 addition & 1 deletion src/libaktualizr/package_manager/fetcher_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ int main(int argc, char** argv) {

std::string port = TestUtils::getFreePort();
server += port;
boost::process::child http_server_process("tests/fake_http_server/fake_http_server.py", port);
boost::process::child http_server_process("tests/fake_http_server/fake_test_server.py", port, "-f");
TestUtils::waitForServer(server + "/");
#ifdef BUILD_OSTREE
std::string treehub_port = TestUtils::getFreePort();
Expand Down
126 changes: 0 additions & 126 deletions tests/fake_http_server/fake_http_server.py

This file was deleted.

38 changes: 32 additions & 6 deletions tests/fake_http_server/fake_test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@

file_path = None
meta_path = None
fail_injector = None


class FailInjector:
def __init__(self):
self.last_fails = False

def fail(self, http_handler):
if self.last_fails:
self.last_fails = False
return False
else:
http_handler.send_response(503)
http_handler.end_headers()
http_handler.wfile.write(b"Internal server error")
self.last_fails = True
return True


class Handler(SimpleHTTPRequestHandler):
Expand All @@ -31,8 +48,6 @@ def serve_target(self, filename):
self._serve_simple(file_path + filename)

def do_GET(self):
print("path: " + self.path)

if self.path.startswith("/director/") and self.path.endswith(".json"):
self.send_response(200)
self.end_headers()
Expand Down Expand Up @@ -72,7 +87,7 @@ def do_GET(self):
r = self.headers["Range"]
r_from = int(r.split("=")[1].split("-")[0])
self.send_response(206)
self.send_header('Content-Range', 'bytes %d-%d/%d' %(r_from, response_size-1, response_size))
self.send_header('Content-Range', 'bytes %d-%d/%d' % (r_from, response_size-1, response_size))
response_size = response_size - r_from
else:
self.send_response(200)
Expand All @@ -95,11 +110,16 @@ def do_GET(self):
self.wfile.write(b'aa')
sleep(1)
else:
if fail_injector is not None and fail_injector.fail(self):
return
self.send_response(200)
self.end_headers()
self.wfile.write(b'{"path": "%b"}' % bytes(self.path, "utf8"))

def do_POST(self):
if fail_injector is not None and fail_injector.fail(self):
return

if self.path == '/devices':
self.send_response(200)
self.end_headers()
Expand Down Expand Up @@ -129,10 +149,13 @@ def do_POST(self):
self.wfile.write(b"{\"access_token\": \"token\"}")
else:
self.wfile.write(b'')

else:
self.send_response(404)
# for httpclient_test
self.send_response(200)
self.end_headers()
length = int(self.headers.get('content-length'))
result = b'{"data": %b, "path": "%b"}'%(self.rfile.read(length), bytes(self.path, "utf8"))
self.wfile.write(result)

def do_PUT(self):
self.do_POST()
Expand All @@ -145,17 +168,20 @@ def server_bind(self):


def main():
global file_path, meta_path
global file_path, meta_path, fail_injector

parser = argparse.ArgumentParser(description='Run a fake OTA backend')
parser.add_argument('port', type=int, help='server port')
parser.add_argument('-t', '--targets', help='targets directory', default=None)
parser.add_argument('-m', '--meta', help='meta directory', default=None)
parser.add_argument('-f', '--fail', help='enable intermittent failure', action='store_true')
args = parser.parse_args()

server_address = ('', args.port)
file_path = args.targets
meta_path = args.meta
if args.fail:
fail_injector = FailInjector()

httpd = ReUseHTTPServer(server_address, Handler)
try:
Expand Down

0 comments on commit cb99aa6

Please sign in to comment.