Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions client/client_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ def write_a_file(self, path):
@param path File Path of download
"""
abs_path, content = self.server_com.download_file(path)
if abs_path and content:
if abs_path and content is not False:
self.add_event_to_ignore(get_abspath(path))
try:
os.makedirs(os.path.split(abs_path)[0], 0755)
Expand All @@ -681,6 +681,7 @@ def write_a_file(self, path):
f.write(content)
self.snapshot_manager.update_snapshot_upload(
{"src_path": get_abspath(abs_path)})
self.snapshot_manager.save_snapshot()
else:
logger.error("DOWNLOAD REQUEST for file {} "
", not found on server".format(path))
Expand Down Expand Up @@ -977,9 +978,10 @@ def instant_snapshot(self):
dir_snapshot[file_md5] = [rel_path]
return dir_snapshot

def save_snapshot(self, timestamp):
def save_snapshot(self, timestamp=False):
"""This method saves snapshot to file"""
self.last_status['timestamp'] = float(timestamp)
if timestamp is not False:
self.last_status['timestamp'] = float(timestamp)
self.last_status['snapshot'] = self.global_md5()

with open(self.snapshot_file_path, 'w') as f:
Expand Down Expand Up @@ -1242,6 +1244,9 @@ def logger_init(crash_repo_path, stdout_level, file_level, disabled=False):
# create formatter for crash file logging
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")

# create formatter for std-out
formatter_print = logging.Formatter('%(lineno)d %(message)s', '%M:%S')

logger = logging.getLogger()

# create file handler which logs even debug messages
Expand All @@ -1250,9 +1255,12 @@ def logger_init(crash_repo_path, stdout_level, file_level, disabled=False):
crash_logger.setLevel(file_level)
crash_logger.setFormatter(formatter)
logger.addHandler(crash_logger)

# create console handler with a low log level
print_logger = logging.StreamHandler()
print_logger.setLevel(stdout_level)
print_logger.setFormatter(formatter_print)

# add the handlers to logger
logger.addHandler(print_logger)
if disabled:
Expand Down
35 changes: 35 additions & 0 deletions client/test_client_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,7 @@ def __init__(self):
self.move = False
self.copy = False
self.upload = False
self.md5_update = False

def update_snapshot_delete(self, body):
self.delete = body
Expand All @@ -724,15 +725,23 @@ def update_snapshot_copy(self, body):
def update_snapshot_upload(self, body):
self.upload = body

def save_snapshot(self, timestamp=False):
if timestamp is False:
self.md5_update = True

self.client_path = '/tmp/user_dir'
client_daemon.CONFIG_DIR_PATH = self.client_path
self.filename = 'test_file_1.txt'
self.empty_file = 'test_empty_file.txt'
if not os.path.exists(self.client_path):
os.makedirs(self.client_path)
httpretty.enable()
httpretty.register_uri(httpretty.GET, 'http://localhost/api/v1/files/{}'.format(self.filename),
body='this is a test',
content_type='text/plain')
httpretty.register_uri(httpretty.GET, 'http://localhost/api/v1/files/{}'.format(self.empty_file),
body='',
content_type='text/plain')
self.snapshot_manager = DirSnapshotManager()
self.server_com = ServerCommunicator(
server_url='http://localhost/api/v1',
Expand Down Expand Up @@ -765,6 +774,23 @@ def test_write_a_file(self):
#check if source isadded by write_a_file
self.assertEqual([source_path], self.event_handler.paths_ignored)

#check if gobal md5 is updated
self.assertTrue(self.snapshot_manager.md5_update)
self.snapshot_manager.md5_update = False

#reset variable
self.snapshot_manager.upload = False
self.event_handler.paths_ignored = []

#Case: empty file
source_path = '{}/{}'.format(self.client_path, self.empty_file)
self.file_system_op.write_a_file(source_path)
self.assertEqual(
self.snapshot_manager.upload,
{"src_path": source_path})
#check if source isadded by write_a_file
self.assertEqual([source_path], self.event_handler.paths_ignored)

#reset variable
self.snapshot_manager.upload = False
self.event_handler.paths_ignored = []
Expand Down Expand Up @@ -1165,6 +1191,7 @@ def test_instant_snapshot(self):

def test_save_snapshot(self):
test_timestamp = 1234
#Case: update timestamp and snapshot
self.snapshot_manager.save_snapshot(test_timestamp)

self.assertEqual(self.snapshot_manager.last_status['timestamp'], test_timestamp)
Expand All @@ -1175,6 +1202,14 @@ def test_save_snapshot(self):

self.assertEqual(expected_conf, new_conf)

#Case: update only snapshot
self.snapshot_manager.save_snapshot()

expected_conf = {'timestamp': 0.0, 'snapshot': self.md5_snapshot}
new_conf = json.load(open(self.conf_snap_path))

self.assertEqual(expected_conf, new_conf)

def test_update_snapshot_upload(self):
original_snapshot = copy.deepcopy(self.snapshot_manager.local_full_snapshot)
del self.snapshot_manager.local_full_snapshot['fea80f2db003d4ebc4536023814aa885']
Expand Down