def log_diff(tree, _logger, prop_key=None): """Log the tree before and after the opteration.""" _logger.debug('+++ model pre update: \n%s\n +++', bushn.tree_to_str(tree, prop_key=prop_key)) yield _logger.debug('+++ model post update: \n%s\n +++', bushn.tree_to_str(tree, prop_key=prop_key))
def add_merge_set(merge_set, key='_id', node=None, storage_id=None, event_sink=None, emit_events=False): """Wrap `node.add_merge_set_with_id` in all the event emiting code. Parameters ---------- merge_set : list A list of `Merge`s to be applied. key : str, optional The key which is used to match nodes with their parents. node: bushn.Node The root node of the tree to work on. storage_id: str Name of the storage in the syncengine. event_sink: function A function which to pass each event. ie. `my_task_list.append` emit_events: bool wether or not these events should be issued. Returns ------- None """ adapter = jars.TreeToSyncEngineEngineAdapter( node=node, sync_engine=event_sink, storage_id=storage_id, ) logger.info('Merging %d changes', len(merge_set)) logger.debug('+++ model pre update: \n%s\n +++', bushn.tree_to_str(node, prop_key=key)) with contextlib.ExitStack() as stack: if emit_events: stack.enter_context(adapter) left_overs = node.add_merge_set_with_id(merge_set, key) if left_overs: logger.critical("There are %s merges left over", len(left_overs)) for merge in left_overs: logger.info(merge) logger.debug('+++ model post update: \n%s\n +++', bushn.tree_to_str(node, prop_key=key))
def test_monkey_need_banana(): """Ask tester to share a file with this account through the monkey share and test the resutls Note: The share needs to be accepted and added to the account being tested. """ init_storage = init_onedrive() input( termcolor.colored( 'Please share a file called banana.txt in the monkey share with this' 'user and hit [ENTER]', 'green', attrs=['blink', 'bold'])) tree = init_storage.storage.model logger.info(bushn.tree_to_str(tree)) banana = tree.get_node(['monkey', 'banana.txt']) logger.info(banana.props['_remote_item']) assert '_id' in banana.props # In the OneDrive situation, shared is set for nodes # which are shared, and the account is the owner. assert banana.props['shared'] is False # this is a child of a remote_item, but itself is not marked as such # otherwise checking iter_share_roots() would create more deltas than # needed. assert banana.props['_share_root'] is False
def test_monkey_lost_banana(): """Ask tester to share a file with this account through the monkey share and test the resutls Note: The share needs to be accepted and added to the account being tested. """ init_storage = init_onedrive() input( termcolor.colored( 'please place the banana file in, a new folder ' 'called mouth inside the monkey folder.', 'green', attrs=['blink', 'bold'])) tree = init_storage.storage.model logger.info(bushn.tree_to_str(tree)) init_storage.storage.update() banana = tree.get_node(['monkey', 'mouth', 'banana.txt']) logger.info('\n' + bushn.tree_to_str(tree, '_id')) assert '_id' in banana.props with pytest.raises(KeyError): banana = tree.get_node(['monkey', 'banana.txt'])
def base_tree(): """Simple tree with 4 nodes. a +-b +-a c """ tree = bushn.Node(None) a_node = tree.add_child('a', props={'_inode': 'a_node'}) c_node = tree.add_child('c', props={'_inode': 'c_node'}) a_node.add_child('b', props={'_inode': 'b_node'}) a_node.add_child('a', props={'_inode': 'aa_node'}) logger.info('base_tree fixture\n%s', bushn.tree_to_str(tree, prop_key='_inode')) return tree
def init_storage_test(init_storage_test_without_files_events): """Inits the storage tests with a bunch of files.""" storage = init_storage_test_without_files_events.storage event_sink = init_storage_test_without_files_events.event_sink # this ensures no files and dirs are existing except the root node delete_all_files(storage=storage, event_sink=event_sink, reset=True) # check if the cached tree is empty as well tree = storage.get_tree(cached=True) assert len(tree) == 1, 'tree is not empty\n' + bushn.tree_to_str(tree) # free_space_before = tree.props[METRICS].free_space total_space_before = tree.props[METRICS].total_space # create test file structure test_paths = [['a'], ['a', 'b'], ['a', 'c'], ['a', 'b', TEST_FILE_NAME], ['c']] test_content = b'test content' assert storage.write(path=['a', 'b', TEST_FILE_NAME], file_obj=io.BytesIO(test_content), original_version_id=None, size=len(test_content)) storage.make_dir(path=['a', 'c']) storage.make_dir(path=['c']) # wait for events logger.info('waiting for %s storage_create in init_storage_test', len(test_paths)) wait_for_events(event_sink=event_sink, storage_create=len(test_paths)) # get tree tree = storage.get_tree(cached=False) assert not tree.get_node(['a', 'b', TEST_FILE_NAME]).props[IS_DIR] # check event arguments and model expected_calls = list() for path in test_paths: # logger.info('### tree: %s', bushn.tree_to_str(tree)) # check model node = tree.get_node(path) # check event logger.debug('node name:%s props:%s', node.name, node.props) event_props = dict(is_dir=node.props[IS_DIR], size=mock.ANY, version_id=node.props[VERSION_ID], modified_date=mock.ANY, shared=mock.ANY) if init_storage_test_without_files_events.normalized_paths: path = IgnoringNormalizationPath(path) expected_calls.append(('storage_create', (), dict(path=path, event_props=event_props, storage_id=storage.storage_id))) assert_expected_calls_in_timeout(expected_calls, event_sink) tree = storage.get_tree() # free_space_after = tree.props[METRICS].free_space total_space_after = tree.props[METRICS].total_space # on some storages including fs this change is not enough if not isinstance(storage, Filesystem): assert total_space_before <= total_space_after # check model entries for folder and files assert tree.get_node(['a']).props[IS_DIR] assert tree.get_node(['a', 'b']).props[IS_DIR] assert tree.get_node(['a', 'c']).props[IS_DIR] assert tree.get_node(['c']).props[IS_DIR] assert not tree.get_node(['a', 'b', TEST_FILE_NAME]).props[IS_DIR] reset_event_sink(event_sink=event_sink) logger.info( '*************** INITIALIZED TEST for %s ***********************', str(storage)) return_type = namedtuple('InitStorageTest', [ 'storage', 'event_sink', 'test_content', 'test_paths', 'emits_move_events', 'normalized_paths' ]) return return_type( storage=storage, event_sink=event_sink, test_content=test_content, test_paths=test_paths, emits_move_events=init_storage_test_without_files_events. emits_move_events, normalized_paths=init_storage_test_without_files_events. normalized_paths)