From 2778125b794813692bbcf89f32388464a2fb29fa Mon Sep 17 00:00:00 2001 From: LeoHsiao Date: Sat, 22 Aug 2020 19:35:01 +0800 Subject: [PATCH] Completed test_io() --- tests/bash_tests/testcases.py | 34 ++++++++++++++++++++++++++++++++-- tests/bash_tests/utils.py | 24 ++++++++++++++++++++++-- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/tests/bash_tests/testcases.py b/tests/bash_tests/testcases.py index 1578e469..79fcd1c9 100644 --- a/tests/bash_tests/testcases.py +++ b/tests/bash_tests/testcases.py @@ -287,7 +287,7 @@ set Exif.Photo.DateTimeDigitized 2020:05:26 07:31:42''' if filename == 'Reagan2.jp2': return [] if filename == 'exiv2-bug1199.webp': - out = BT.excute('exiv2 --comment abcdefg {filename}', vars(), [0,1]) + out = BT.excute('exiv2 --comment abcdefg {filename}', vars(), expected_returncodes=[0,1]) out += BT.excute('exiv2 -pS {filename}', vars()) out += [''] else: @@ -355,7 +355,7 @@ set Exif.Photo.DateTimeDigitized 2020:05:26 07:31:42''' for tag in BT.excute('exiv2 -Pk --grep GPSInfo {jpg}', vars()): tag = tag.rstrip(' ') out += BT.excute('exiv2 -M"del {tag}" {jpg}', vars()) - out += BT.excute('exiv2 -pa --grep GPS {jpg}', vars(), [0, 1]) + out += BT.excute('exiv2 -pa --grep GPS {jpg}', vars(), expected_returncodes=[0, 1]) out += ['--- run geotag ---'] geotag_out = BT.excute('geotag -ascii -tz -8:00 {jpg} {gpx}', vars()) @@ -375,3 +375,33 @@ set Exif.Photo.DateTimeDigitized 2020:05:26 07:31:42''' BT.copyTestFiles(*test_files) for f in test_files: BT.ioTest(f) + + # Test http I/O + server_bind = '127.0.0.1' + server_port = 12760 + server_url = 'http://{}:{}'.format(server_bind, server_port) + server = BT.HttpServer(bind=server_bind, port=server_port, work_dir=os.path.join(BT.Conf.data_dir)) + server.start() + try: + out = [] + for img in ['table.jpg', 'Reagan.tiff', 'exiv2-bug922a.jpg']: + files = ['s0', 's1', 's2', '{}/{}'.format(server_url, img)] + out += BT.excute('iotest ' + ' '.join(files)) + for f in files: + out += BT.excute('exiv2 -g City -g DateTime {f}', vars()) + + def sniff(*files): + result = [os.path.getsize(i) for i in files] + result += [BT.md5sum(i) for i in files] + result = [str(i) for i in result] + return [' '.join(result)] + + for num in ['0', '10', '1000']: + out += BT.excute('iotest s0 s1 s2 {server_url}/table.jpg {num}', vars()) + out += sniff('s0', 's1', 's2', os.path.join(BT.Conf.data_dir, 'table.jpg')) + finally: + server.stop() + # print('keep the server running...') + + out += [''] + BT.reportTest('iotest', out) diff --git a/tests/bash_tests/utils.py b/tests/bash_tests/utils.py index 8976ec9e..5d078c74 100644 --- a/tests/bash_tests/utils.py +++ b/tests/bash_tests/utils.py @@ -1,10 +1,12 @@ import hashlib +import multiprocessing import os import platform import re import shlex import shutil import subprocess +from http import server class Conf: @@ -15,10 +17,10 @@ class Conf: data_dir = os.path.join(exiv2_dir, 'test/data') tmp_dir = os.path.join(exiv2_dir, 'test/tmp') system_name = platform.system() or 'Unknown' - + @classmethod def init(cls): - """ Init the test environment """ + """ Init the test environment and variables that may be modified """ log.buffer = [''] os.chdir(cls.tmp_dir) os.makedirs(cls.tmp_dir, exist_ok=True) @@ -199,4 +201,22 @@ def ioTest(filename): assert md5sum(src) == md5sum(out2), 'The output file is different' +class HttpServer: + def __init__(self, bind='127.0.0.1', port=12760, work_dir='.'): + self.bind = bind + self.port = port + self.work_dir = work_dir + + def _start(self): + os.chdir(self.work_dir) + server.test(HandlerClass=server.SimpleHTTPRequestHandler, bind=self.bind, port=self.port) + + def start(self): + self.process = multiprocessing.Process(target=self._start) + self.process.start() + + def stop(self): + self.process.terminate() + + log = Log()