From 737ca94c3502677b3f930f6e00b837d5a5dfd711 Mon Sep 17 00:00:00 2001 From: PHILO-HE Date: Fri, 29 Jun 2018 14:37:03 +0800 Subject: [PATCH] Add sync test case (#1839) --- docs/rest-api.md | 2 +- supports/integration-test/test_sync.py | 92 ++++++++++++++++++++------ 2 files changed, 71 insertions(+), 23 deletions(-) diff --git a/docs/rest-api.md b/docs/rest-api.md index d1b5f33a6ff..805ee5b4f26 100644 --- a/docs/rest-api.md +++ b/docs/rest-api.md @@ -54,7 +54,7 @@ Example: ``` Example: ``` - GET http://:/smart/api/v1/rules/{ruleId}/detail + GET http://:/smart/api/v1/rules/{ruleId}/info Code:200 Content-Type:application/json ``` diff --git a/supports/integration-test/test_sync.py b/supports/integration-test/test_sync.py index e1b36009fe3..9c92a0295dc 100644 --- a/supports/integration-test/test_sync.py +++ b/supports/integration-test/test_sync.py @@ -4,17 +4,24 @@ from util import * -class TestSync(unittest.TestCase): +@timeout_decorator.timeout(seconds=200) +def verify(rid, num_cmds_gen): + while True: + time.sleep(1) + rule = get_rule(rid) + if rule['numCmdsGen'] >= num_cmds_gen: + break + - @timeout_decorator.timeout(seconds=60) +class TestSync(unittest.TestCase): def test_sync_rule(self): file_paths = [] cids = [] # create a directory with random name - source_dir = TEST_DIR + random_string() + "/" + sync_dir = TEST_DIR + random_string() + "/" # create random files in the above directory for i in range(MAX_NUMBER): - file_path, cid = create_random_file_parallel(FILE_SIZE, source_dir) + file_path, cid = create_random_file_parallel(FILE_SIZE, sync_dir) file_paths.append(file_path) cids.append(cid) wait_for_cmdlets(cids) @@ -22,15 +29,12 @@ def test_sync_rule(self): # wait for DB sync time.sleep(5) rule_str = "file : every 1s | path matches " + \ - "\"" + source_dir + "*\" | sync -dest " + DEST_DIR + "\"" + sync_dir + "*\" | sync -dest " + DEST_DIR rid = submit_rule(rule_str) start_rule(rid) # Status check - while True: - time.sleep(1) - rule = get_rule(rid) - if rule['numCmdsGen'] >= MAX_NUMBER: - break + num_cmds_gen = MAX_NUMBER + verify(rid, num_cmds_gen) cids = get_cids_of_rule(rid) failed = wait_for_cmdlets(cids) self.assertTrue(len(failed) == 0) @@ -40,28 +44,72 @@ def test_sync_rule(self): time.sleep(5) cids = [] num_delete = random.randrange(len(file_paths)) + num_cmds_gen = num_cmds_gen + num_delete for i in range(num_delete): cids.append(submit_cmdlet("delete -file " + file_paths[i])) wait_for_cmdlets(cids) - while True: - time.sleep(1) - rule = get_rule(rid) - if rule['numCmdsGen'] >= MAX_NUMBER + num_delete: - break + verify(rid, num_cmds_gen) # test create src file cids = [] num_create = 10 + num_cmds_gen = num_cmds_gen + num_create for i in range(num_create): - file_path, cid = create_random_file_parallel(FILE_SIZE, source_dir) + file_path, cid = create_random_file_parallel(FILE_SIZE, sync_dir) + cids.append(cid) + wait_for_cmdlets(cids) + verify(rid, num_cmds_gen) + failed = wait_for_cmdlets(get_cids_of_rule(rid)) + self.assertTrue(len(failed) == 0) + + # test subdir case + cids = [] + subdir = sync_dir + random_string() + "/" + num_subdir_files = 10 + num_cmds_gen = num_cmds_gen + num_subdir_files + for i in range(num_subdir_files): + file_path, cid = create_random_file_parallel(FILE_SIZE, subdir) + file_paths.append(file_path) + cids.append(cid) + wait_for_cmdlets(cids) + verify(rid, num_cmds_gen) + failed = wait_for_cmdlets(get_cids_of_rule(rid)) + self.assertTrue(len(failed) == 0) + # delete subdir + cid = submit_cmdlet("delete -file " + subdir) + wait_for_cmdlet(cid) + num_cmds_gen = num_cmds_gen + 1 + verify(rid, num_cmds_gen) + failed = wait_for_cmdlets(get_cids_of_rule(rid)) + self.assertTrue(len(failed) == 0) + + # test rename case + cids = [] + num_rename = 10 + src_rename = [] + num_cmds_gen = num_cmds_gen + num_rename + for i in range(num_rename): + file_path, cid = create_random_file_parallel(FILE_SIZE, sync_dir) + src_rename.append(file_path) cids.append(cid) wait_for_cmdlets(cids) - while True: - time.sleep(1) - rule = get_rule(rid) - if rule['numCmdsGen'] >= MAX_NUMBER + num_delete + num_create: - break - # TODO: test delete file while syncing + # sync original files + verify(rid, num_cmds_gen) + failed = wait_for_cmdlets(get_cids_of_rule(rid)) + self.assertTrue(len(failed) == 0) + cids = [] + for i in range(num_rename): + cids.append(submit_cmdlet("rename -file " + src_rename[i] + " -dest " + sync_dir + random_string())) + wait_for_cmdlets(cids) + num_cmds_gen = num_cmds_gen + num_rename + # sync renamed file + verify(rid, num_cmds_gen) + failed = wait_for_cmdlets(get_cids_of_rule(rid)) + self.assertTrue(len(failed) == 0) + + # TODO: test rename from outside dir into sync dir + # TODO: rename from sync dir to outside dir + # TODO: rename the syncing file time.sleep(5) stop_rule(rid)