mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-06-30 19:12:07 +08:00
update tests
old-commit-hash: 337fb1a273c4ac57c2a2a17ae68499eb8b49f37b
This commit is contained in:
@@ -28,15 +28,26 @@ def create_random_file(file_path, size_mb, lock=False):
|
||||
os.remove(lock_path)
|
||||
|
||||
class MockResponse():
|
||||
def __init__(self, text):
|
||||
def __init__(self, text, status_code):
|
||||
self.text = text
|
||||
self.status_code = status_code
|
||||
|
||||
class MockApi():
|
||||
def __init__(self, dongle_id):
|
||||
pass
|
||||
|
||||
def get(self, *args, **kwargs):
|
||||
return MockResponse('{"url": "http://localhost/does/not/exist", "headers": {}}')
|
||||
return MockResponse('{"url": "http://localhost/does/not/exist", "headers": {}}', 200)
|
||||
|
||||
def get_token(self):
|
||||
return "fake-token"
|
||||
|
||||
class MockApiIgnore():
|
||||
def __init__(self, dongle_id):
|
||||
pass
|
||||
|
||||
def get(self, *args, **kwargs):
|
||||
return MockResponse('', 412)
|
||||
|
||||
def get_token(self):
|
||||
return "fake-token"
|
||||
@@ -54,6 +65,9 @@ class MockParams():
|
||||
class UploaderTestCase(unittest.TestCase):
|
||||
f_type = "UNKNOWN"
|
||||
|
||||
def set_ignore(self):
|
||||
uploader.Api = MockApiIgnore
|
||||
|
||||
def setUp(self):
|
||||
self.root = tempfile.mkdtemp()
|
||||
uploader.ROOT = self.root # Monkey patch root dir
|
||||
|
||||
@@ -18,12 +18,15 @@ class TestLogHandler(logging.Handler):
|
||||
|
||||
def reset(self):
|
||||
self.upload_order = list()
|
||||
self.upload_ignored = list()
|
||||
|
||||
def emit(self, record):
|
||||
try:
|
||||
j = json.loads(record.message)
|
||||
if j["event"] == "upload_success":
|
||||
self.upload_order.append(j["key"])
|
||||
if j["event"] == "upload_ignored":
|
||||
self.upload_ignored.append(j["key"])
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@@ -73,6 +76,7 @@ class TestUploader(UploaderTestCase):
|
||||
time.sleep(5)
|
||||
self.join_thread()
|
||||
|
||||
self.assertTrue(len(log_handler.upload_ignored) == 0, "Some files were ignored")
|
||||
self.assertFalse(len(log_handler.upload_order) < len(f_paths), "Some files failed to upload")
|
||||
self.assertFalse(len(log_handler.upload_order) > len(f_paths), "Some files were uploaded twice")
|
||||
for f_path in f_paths:
|
||||
@@ -80,6 +84,23 @@ class TestUploader(UploaderTestCase):
|
||||
exp_order = self.gen_order([self.seg_num], [])
|
||||
self.assertTrue(log_handler.upload_order == exp_order, "Files uploaded in wrong order")
|
||||
|
||||
def test_upload_ignored(self):
|
||||
self.set_ignore()
|
||||
f_paths = self.gen_files(lock=False)
|
||||
|
||||
self.start_thread()
|
||||
# allow enough time that files could upload twice if there is a bug in the logic
|
||||
time.sleep(5)
|
||||
self.join_thread()
|
||||
|
||||
self.assertTrue(len(log_handler.upload_order) == 0, "Some files were not ignored")
|
||||
self.assertFalse(len(log_handler.upload_ignored) < len(f_paths), "Some files failed to ignore")
|
||||
self.assertFalse(len(log_handler.upload_ignored) > len(f_paths), "Some files were ignored twice")
|
||||
for f_path in f_paths:
|
||||
self.assertTrue(getxattr(f_path, uploader.UPLOAD_ATTR_NAME), "All files not ignored")
|
||||
exp_order = self.gen_order([self.seg_num], [])
|
||||
self.assertTrue(log_handler.upload_ignored == exp_order, "Files ignored in wrong order")
|
||||
|
||||
def test_upload_files_in_create_order(self):
|
||||
f_paths = list()
|
||||
seg1_nums = [0,1,2,10,20]
|
||||
@@ -96,6 +117,7 @@ class TestUploader(UploaderTestCase):
|
||||
time.sleep(5)
|
||||
self.join_thread()
|
||||
|
||||
self.assertTrue(len(log_handler.upload_ignored) == 0, "Some files were ignored")
|
||||
self.assertFalse(len(log_handler.upload_order) < len(f_paths), "Some files failed to upload")
|
||||
self.assertFalse(len(log_handler.upload_order) > len(f_paths), "Some files were uploaded twice")
|
||||
for f_path in f_paths:
|
||||
|
||||
Reference in New Issue
Block a user