def verify_file_create_upload_start(node, key_id, volume_path, filename='cat.txt', randomize_bytes=0, verify_list_files=True, reliable_shares=True, expected_reliable=100): virtual_filename = filename local_filepath = f'{volume_path}/{filename}' remote_path = f'{key_id}:{virtual_filename}' download_filepath = f'/tmp/{filename}' if randomize_bytes == 0: run_ssh_command_and_wait(node, f'echo "{node}" > {local_filepath}') else: run_ssh_command_and_wait( node, f'python -c "import os, base64; print(base64.b64encode(os.urandom({randomize_bytes})).decode()[:{randomize_bytes}])" > {local_filepath}' ) if verify_list_files: file_list_all_v1(node, reliable_shares=reliable_shares, expected_reliable=expected_reliable) file_create_v1(node, remote_path) file_upload_start_v1(node, remote_path, local_filepath) packet_list_v1(node, wait_all_finish=True) transfer_list_v1(node, wait_all_finish=True) return local_filepath, remote_path, download_filepath
def verify_file_download_start(node, remote_path, destination_path, verify_from_local_path=None, verify_list_files=True, reliable_shares=True, expected_reliable=100): if verify_list_files: file_list_all_v1(node, reliable_shares=reliable_shares, expected_reliable=expected_reliable) file_download_start_v1(node, remote_path=remote_path, destination=os.path.dirname(destination_path)) if verify_from_local_path is not None: file_body_source = run_ssh_command_and_wait(node, f'cat {verify_from_local_path}')[0].strip() file_body_downloaded = run_ssh_command_and_wait(node, f'cat {destination_path}')[0].strip() assert file_body_source == file_body_downloaded if verify_list_files: file_list_all_v1(node, reliable_shares=reliable_shares, expected_reliable=expected_reliable)
def wait_event(nodes, event, expected_count=1, attempts=10, delay=5, verbose=False): dbg('wait event "%s" to occur %d times on %d nodes' % (event, expected_count, len(nodes), )) for node in nodes: for _ in range(attempts): event_log = run_ssh_command_and_wait(node, 'cat /root/.bitdust/logs/event.log', verbose=verbose)[0].strip() if event_log.count(event) == expected_count: break time.sleep(delay) else: assert False, f'event "{event}" did not occurred {expected_count} times on [{node}] after many attempts' dbg('')
def wait_specific_event(nodes, event, match_regex, expected_count=1, attempts=20, delay=5, verbose=False): dbg('wait event "%s" with "%s" to occur %d times on %d nodes' % (event, match_regex, expected_count, len(nodes), )) for node in nodes: for _ in range(attempts): event_log = run_ssh_command_and_wait(node, 'cat /root/.bitdust/logs/event.log', verbose=verbose)[0].strip() captured = False for ln in event_log.split('\n'): if ln.count(event): if re.match(match_regex, ln): captured = True break if captured: break time.sleep(delay) else: assert False, f'event "{event}" did not occurred {expected_count} times on [{node}] after many attempts' dbg('')
def test_customer_1_share_file_to_customer_2_same_name_as_existing(): if os.environ.get('RUN_TESTS', '1') == '0': return pytest.skip() # @UndefinedVariable supplier_list_v1('customer-1', expected_min_suppliers=2, expected_max_suppliers=2) supplier_list_v1('customer-2', expected_min_suppliers=4, expected_max_suppliers=4) service_info_v1('customer-1', 'service_shared_data', 'ON') service_info_v1('customer-2', 'service_shared_data', 'ON') # create shares (logic unit to upload/download/share files) share_id_customer_1 = share_create_v1('customer-1') share_id_customer_2 = share_create_v1('customer-2') filename = 'cat.txt' virtual_filename = filename volume_customer_1 = '/customer_1' filepath_customer_1 = f'{volume_customer_1}/{filename}' volume_customer_2 = '/customer_2' filepath_customer_2 = f'{volume_customer_2}/{filename}' run_ssh_command_and_wait('customer-1', f'echo customer_1 > {filepath_customer_1}') run_ssh_command_and_wait('customer-2', f'echo customer_2 > {filepath_customer_2}') remote_path_customer_1 = f'{share_id_customer_1}:{virtual_filename}' remote_path_customer_2 = f'{share_id_customer_2}:{virtual_filename}' # create virtual file for customer_1 file_create_v1('customer-1', remote_path_customer_1) # create virtual file for customer_2 file_create_v1('customer-2', remote_path_customer_2) # upload file for customer_1 service_info_v1('customer-1', 'service_shared_data', 'ON') file_upload_start_v1('customer-1', remote_path_customer_1, filepath_customer_1) # upload file for customer_2 service_info_v1('customer-2', 'service_shared_data', 'ON') file_upload_start_v1('customer-2', remote_path_customer_2, filepath_customer_2) packet_list_v1('customer-2', wait_all_finish=True) transfer_list_v1('customer-2', wait_all_finish=True) # wait for quite a while to allow files to be uploaded # time.sleep(5) service_info_v1('customer-1', 'service_shared_data', 'ON') file_download_start_v1('customer-1', remote_path=remote_path_customer_1, destination=volume_customer_1) service_info_v1('customer-2', 'service_shared_data', 'ON') file_download_start_v1('customer-2', remote_path=remote_path_customer_2, destination=volume_customer_2) service_info_v1('customer-2', 'service_shared_data', 'ON') packet_list_v1('customer-1', wait_all_finish=True) transfer_list_v1('customer-1', wait_all_finish=True) packet_list_v1('customer-2', wait_all_finish=True) transfer_list_v1('customer-2', wait_all_finish=True) response = request_put( 'customer-1', 'share/grant/v1', json={ 'trusted_global_id': 'customer-2@id-a_8084', 'key_id': share_id_customer_1, }, timeout=40, ) assert response.status_code == 200 assert response.json()['status'] == 'OK', response.json() print('\n\nshare/grant/v1 trusted_global_id=%s key_id=%s : %s\n' % ( 'customer-2@id-a_8084', share_id_customer_1, response.json(), )) response = request_get('customer-2', 'file/list/all/v1') assert response.status_code == 200, response.json() run_ssh_command_and_wait('customer-2', f'mkdir {volume_customer_2}/sharesamename') run_ssh_command_and_wait('customer-2', f'mkdir {volume_customer_2}/sharesamename2') service_info_v1('customer-2', 'service_shared_data', 'ON') file_download_start_v1('customer-2', remote_path=remote_path_customer_1, destination=f'{volume_customer_2}/sharesamename') service_info_v1('customer-2', 'service_shared_data', 'ON') file_download_start_v1('customer-2', remote_path=remote_path_customer_2, destination=f'{volume_customer_2}/sharesamename2') file_1 = run_ssh_command_and_wait( 'customer-2', f'cat {volume_customer_2}/sharesamename/cat.txt')[0].strip() file_2 = run_ssh_command_and_wait( 'customer-2', f'cat {volume_customer_2}/sharesamename2/cat.txt')[0].strip() assert file_1 != file_2
def test_customer_1_share_file_to_customer_3(): if os.environ.get('RUN_TESTS', '1') == '0': return pytest.skip() # @UndefinedVariable supplier_list_v1('customer-1', expected_min_suppliers=2, expected_max_suppliers=2) key_id = share_create_v1('customer-1') # create randomized file to test files upload/download origin_volume = '/customer_1' origin_filename = 'second_file_customer_1.txt' run_ssh_command_and_wait( 'customer-1', f'python -c "import os, base64; print(base64.b64encode(os.urandom(24)).decode())" > {origin_volume}/{origin_filename}' ) local_path = '%s/%s' % (origin_volume, origin_filename) virtual_file = 'second_virtual_file.txt' remote_path = '%s:%s' % (key_id, virtual_file) download_volume = '/customer_3' downloaded_file = '%s/%s' % (download_volume, virtual_file) service_info_v1('customer-1', 'service_shared_data', 'ON') file_create_v1('customer-1', remote_path) file_upload_start_v1('customer-1', remote_path, local_path) packet_list_v1('customer-1', wait_all_finish=True) transfer_list_v1('customer-1', wait_all_finish=True) file_download_start_v1('customer-1', remote_path=remote_path, destination='/customer_1') packet_list_v1('customer-1', wait_all_finish=True) transfer_list_v1('customer-1', wait_all_finish=True) service_info_v1('customer-3', 'service_shared_data', 'ON') packet_list_v1('customer-3', wait_all_finish=True) transfer_list_v1('customer-3', wait_all_finish=True) response = request_put( 'customer-1', 'share/grant/v1', json={ 'trusted_global_id': 'customer-3@id-a_8084', 'key_id': key_id, }, timeout=40, ) assert response.status_code == 200 assert response.json()['status'] == 'OK', response.json() print('\n\nshare/grant/v1 trusted_global_id=%s key_id=%s : %s\n' % ( 'customer-3@id-a_8084', key_id, response.json(), )) file_download_start_v1('customer-3', remote_path=remote_path, destination=download_volume) local_file_src = run_ssh_command_and_wait('customer-1', 'cat %s' % local_path)[0].strip() print('customer-1: file %s is %d bytes long' % (local_path, len(local_file_src))) downloaded_file_src = run_ssh_command_and_wait('customer-3', 'cat %s' % downloaded_file)[0].strip() print('customer-3: file %s is %d bytes long' % (downloaded_file, len(downloaded_file_src))) assert local_file_src == downloaded_file_src, "source file and shared file content is not equal"
def test_identity_rotate_customer_1(): if os.environ.get('RUN_TESTS', '1') == '0': return pytest.skip() # @UndefinedVariable sys.stderr.write('\ntest_identity_rotate_customer_1\n') service_info_v1('customer-1', 'service_customer', 'ON') supplier_list_v1('customer-1', expected_min_suppliers=2, expected_max_suppliers=2) service_info_v1('customer-1', 'service_shared_data', 'ON') # remember current ID sources r = identity_get_v1('customer-1') old_sources = r['result']['sources'] old_global_id = r['result']['global_id'] old_idurl = r['result']['idurl'] # test other nodes able to talk to customer-1 before identity get rotated user_ping_v1('customer-3', old_global_id) user_ping_v1('supplier-1', old_global_id) user_ping_v1('supplier-2', old_global_id) # test customer-3 can chat with customer-1 before identity get rotated service_info_v1('customer-3', 'service_private_messages', 'ON') service_info_v1('customer-1', 'service_private_messages', 'ON') random_string = base64.b32encode(os.urandom(20)).decode() random_message = { 'random_message': random_string, } t = threading.Timer(2.0, message_send_v1, [ 'customer-3', 'master$%s' % old_global_id, random_message, ]) t.start() message_receive_v1('customer-1', expected_data=random_message) # create one share and upload one file for customer-1 share_id_customer_1 = share_create_v1('customer-1') filename = 'cat.txt' virtual_filename = filename volume_customer_1 = '/customer_1' filepath_customer_1 = f'{volume_customer_1}/{filename}' run_ssh_command_and_wait('customer-1', f'echo customer_1 > {filepath_customer_1}') remote_path_customer_1 = f'{share_id_customer_1}:{virtual_filename}' downloaded_filepath = f'/tmp/{filename}' local_file_src = run_ssh_command_and_wait('customer-1', 'cat %s' % filepath_customer_1)[0].strip() service_info_v1('customer-1', 'service_shared_data', 'ON') packet_list_v1('customer-1', wait_all_finish=True) transfer_list_v1('customer-1', wait_all_finish=True) file_create_v1('customer-1', remote_path=remote_path_customer_1) file_upload_start_v1('customer-1', remote_path=remote_path_customer_1, local_path=filepath_customer_1) time.sleep(3) service_info_v1('customer-1', 'service_shared_data', 'ON') # make sure file is available before identity rotate share_open_v1('customer-1', share_id_customer_1) file_download_start_v1('customer-1', remote_path=remote_path_customer_1, destination='/tmp') # and make sure this is the same file downloaded_file_src = run_ssh_command_and_wait( 'customer-1', 'cat %s' % downloaded_filepath)[0].strip() assert local_file_src == downloaded_file_src, "source file and received file content is not equal" # remember list of existing keys old_keys = [k['key_id'] for k in key_list_v1('customer-1')['result']] assert f'master${old_global_id}' in old_keys assert f'messages${old_global_id}' in old_keys assert f'customer${old_global_id}' in old_keys # make customer-1 and customer-3 friends to each other friend_add_v1('customer-1', 'http://id-a:8084/customer-3.xml', 'friend2') friend_add_v1('customer-3', old_idurl, 'friend1') old_friends = friend_list_v1('customer-3', extract_idurls=True) assert old_idurl in old_friends # rotate identity sources identity_rotate_v1('customer-1') time.sleep(1) # and make sure ID sources were changed r = identity_get_v1('customer-1') new_sources = r['result']['sources'] new_global_id = r['result']['global_id'] new_idurl = r['result']['idurl'] assert new_sources != old_sources assert new_global_id != old_global_id assert new_idurl != old_idurl service_info_v1('customer-1', 'service_gateway', 'ON') service_info_v1('customer-1', 'service_customer', 'ON') # remember current suppliers of customer-1 customer_1_suppliers = supplier_list_v1('customer-1', expected_min_suppliers=2, expected_max_suppliers=2, extract_suppliers=True) first_supplier = customer_1_suppliers[0].replace( 'http://id-a:8084/', '').replace('http://id-b:8084/', '').replace('.xml', '') second_supplier = customer_1_suppliers[1].replace( 'http://id-a:8084/', '').replace('http://id-b:8084/', '').replace('.xml', '') service_info_v1('customer-1', 'service_shared_data', 'ON') # test other nodes able to talk to customer-1 again on new IDURL user_ping_v1('customer-3', new_global_id) user_ping_v1('supplier-1', new_global_id) user_ping_v1('supplier-2', new_global_id) # make sure keys are renamed on customer-1 new_keys = [k['key_id'] for k in key_list_v1('customer-1')['result']] assert len(old_keys) == len(new_keys) assert f'master${new_global_id}' in new_keys assert f'messages${new_global_id}' in new_keys assert f'customer${new_global_id}' in new_keys assert f'master${old_global_id}' not in new_keys assert f'messages${old_global_id}' not in new_keys assert f'customer${old_global_id}' not in new_keys # make sure file is still available after identity rotate new_share_id_customer_1 = share_id_customer_1.replace( old_global_id, new_global_id) share_open_v1('customer-1', new_share_id_customer_1) new_remote_path_customer_1 = remote_path_customer_1.replace( old_global_id, new_global_id) run_ssh_command_and_wait('customer-1', 'rm -rfv %s' % downloaded_filepath)[0].strip() file_download_start_v1('customer-1', remote_path=new_remote_path_customer_1, destination='/tmp') # and make sure this is still the same file new_downloaded_file_src = run_ssh_command_and_wait( 'customer-1', 'cat %s' % downloaded_filepath)[0].strip() assert local_file_src == downloaded_file_src, "source file and received file content is not equal after identity rotate" assert new_downloaded_file_src == downloaded_file_src, "received file content before identity rotate is not equal to received file after identity rotate" # check again current suppliers of customer-1 customer_1_suppliers = supplier_list_v1('customer-1', expected_min_suppliers=2, expected_max_suppliers=2, extract_suppliers=True) first_supplier = customer_1_suppliers[0].replace( 'http://id-a:8084/', '').replace('http://id-b:8084/', '').replace('.xml', '') second_supplier = customer_1_suppliers[1].replace( 'http://id-a:8084/', '').replace('http://id-b:8084/', '').replace('.xml', '') # verify files on first supplier were moved to correct sub folder print(f'checking customer-1 files on {first_supplier}') old_folder_first_supplier = run_ssh_command_and_wait( first_supplier, f'ls -la ~/.bitdust/customers/{old_global_id}/')[0].strip() new_folder_first_supplier = run_ssh_command_and_wait( first_supplier, f'ls -la ~/.bitdust/customers/{new_global_id}/')[0].strip() assert old_folder_first_supplier == '' assert new_folder_first_supplier != '' print(f'first supplier {first_supplier} :\n', new_folder_first_supplier) # verify files on second supplier were moved to correct sub folder print(f'checking customer-1 files on {second_supplier}') old_folder_second_supplier = run_ssh_command_and_wait( second_supplier, f'ls -la ~/.bitdust/customers/{old_global_id}/')[0].strip() new_folder_second_supplier = run_ssh_command_and_wait( second_supplier, f'ls -la ~/.bitdust/customers/{new_global_id}/')[0].strip() assert old_folder_second_supplier == '' assert new_folder_second_supplier != '' print(f'second supplier {second_supplier} :\n', new_folder_second_supplier) # test that friend1 idurl changed for customer-3 new_friends = friend_list_v1('customer-3', extract_idurls=True) assert new_idurl in new_friends assert old_idurl not in new_friends # test customer-3 can still chat with customer-1 after identity rotated service_info_v1('customer-3', 'service_private_messages', 'ON') service_info_v1('customer-1', 'service_private_messages', 'ON') random_string = base64.b32encode(os.urandom(20)).decode() random_message = { 'random_message': random_string, } t = threading.Timer(1.0, message_send_v1, [ 'customer-3', 'master$%s' % new_global_id, random_message, ]) t.start() message_receive_v1('customer-1', expected_data=random_message)
def test_customer_1_replace_supplier_at_position_0(): if os.environ.get('RUN_TESTS', '1') == '0': return pytest.skip() # @UndefinedVariable packet_list_v1('supplier-1', wait_all_finish=True) packet_list_v1('supplier-2', wait_all_finish=True) packet_list_v1('supplier-3', wait_all_finish=True) packet_list_v1('supplier-4', wait_all_finish=True) packet_list_v1('supplier-5', wait_all_finish=True) packet_list_v1('supplier-6', wait_all_finish=True) packet_list_v1('supplier-7', wait_all_finish=True) packet_list_v1('supplier-8', wait_all_finish=True) packet_list_v1('customer-1', wait_all_finish=True) transfer_list_v1('customer-1', wait_all_finish=True) supplier_list_v1('customer-1', expected_min_suppliers=2, expected_max_suppliers=2) supplier_list_dht_v1( customer_id='customer-1@id-a_8084', observers_ids=['customer-1@id-a_8084', 'customer-3@id-a_8084', ], expected_ecc_map='ecc/2x2', expected_suppliers_number=2, ) supplier_list_dht_v1( customer_id='customer-1@id-a_8084', observers_ids=['customer-3@id-a_8084', 'customer-1@id-a_8084', ], expected_ecc_map='ecc/2x2', expected_suppliers_number=2, ) supplier_list_dht_v1( customer_id='customer-1@id-a_8084', observers_ids=['supplier-2@id-a_8084', 'customer-3@id-a_8084', 'customer-1@id-a_8084', ], expected_ecc_map='ecc/2x2', expected_suppliers_number=2, ) share_id_customer_1 = share_create_v1('customer-1') filename = 'file_to_be_distributed.txt' virtual_filename = filename volume_customer_1 = '/customer_1' filepath_customer_1 = f'{volume_customer_1}/{filename}' remote_path_customer_1 = f'{share_id_customer_1}:{virtual_filename}' run_ssh_command_and_wait('customer-1', f'echo customer_1 > {filepath_customer_1}') file_create_v1('customer-1', remote_path_customer_1) file_upload_start_v1('customer-1', remote_path_customer_1, filepath_customer_1) service_info_v1('customer-1', 'service_shared_data', 'ON') packet_list_v1('customer-1', wait_all_finish=True) transfer_list_v1('customer-1', wait_all_finish=True) file_download_start_v1('customer-1', remote_path=remote_path_customer_1, destination=volume_customer_1) response_before = request_get('customer-1', 'supplier/list/v1') assert response_before.status_code == 200 supplier_list_before = response_before.json()['result'] suppliers_before = list([x['global_id'] for x in supplier_list_before]) assert len(suppliers_before) == 2 response = request_post('customer-1', 'supplier/replace/v1', json={'position': '0'}) assert response.status_code == 200 packet_list_v1('supplier-1', wait_all_finish=True) packet_list_v1('supplier-2', wait_all_finish=True) packet_list_v1('supplier-3', wait_all_finish=True) packet_list_v1('supplier-4', wait_all_finish=True) packet_list_v1('supplier-5', wait_all_finish=True) packet_list_v1('supplier-6', wait_all_finish=True) packet_list_v1('supplier-7', wait_all_finish=True) packet_list_v1('supplier-8', wait_all_finish=True) supplier_list_v1('customer-1', expected_min_suppliers=2, expected_max_suppliers=2) service_info_v1('customer-1', 'service_shared_data', 'ON') supplier_list_dht_v1( customer_id='customer-1@id-a_8084', observers_ids=['customer-1@id-a_8084', 'customer-3@id-a_8084', ], expected_ecc_map='ecc/2x2', expected_suppliers_number=2, ) supplier_list_dht_v1( customer_id='customer-1@id-a_8084', observers_ids=['customer-3@id-a_8084', 'customer-1@id-a_8084', ], expected_ecc_map='ecc/2x2', expected_suppliers_number=2, ) supplier_list_dht_v1( customer_id='customer-1@id-a_8084', observers_ids=['supplier-2@id-a_8084', 'customer-3@id-a_8084', 'customer-1@id-a_8084', ], expected_ecc_map='ecc/2x2', expected_suppliers_number=2, ) count = 0 while True: if count > 20: assert False, 'supplier was not replaced after many attempts' break response_after = request_get('customer-1', 'supplier/list/v1') assert response_after.status_code == 200 supplier_list_after = response_after.json()['result'] suppliers_after = list([x['global_id'] for x in supplier_list_after]) assert len(suppliers_after) == 2 assert suppliers_after[1] == suppliers_before[1] if suppliers_after[0] != suppliers_before[0]: break count += 1 time.sleep(1)
def test_customer_2_switch_supplier_at_position_0(): if os.environ.get('RUN_TESTS', '1') == '0': return pytest.skip() # @UndefinedVariable packet_list_v1('supplier-1', wait_all_finish=True) packet_list_v1('supplier-2', wait_all_finish=True) packet_list_v1('supplier-3', wait_all_finish=True) packet_list_v1('supplier-4', wait_all_finish=True) packet_list_v1('supplier-5', wait_all_finish=True) packet_list_v1('supplier-6', wait_all_finish=True) packet_list_v1('supplier-7', wait_all_finish=True) packet_list_v1('supplier-8', wait_all_finish=True) packet_list_v1('customer-2', wait_all_finish=True) transfer_list_v1('customer-2', wait_all_finish=True) supplier_list_v1('customer-2', expected_min_suppliers=4, expected_max_suppliers=4) response_before = request_get('customer-2', 'supplier/list/v1') assert response_before.status_code == 200 supplier_list_before = response_before.json()['result'] suppliers_before = list([x['global_id'] for x in supplier_list_before]) assert len(suppliers_before) == 4 possible_suppliers = set([ 'supplier-1@id-a_8084', 'supplier-2@id-a_8084', 'supplier-3@id-a_8084', 'supplier-4@id-a_8084', 'supplier-5@id-a_8084', 'supplier-6@id-a_8084', 'supplier-7@id-a_8084', 'supplier-8@id-a_8084', ]) possible_suppliers.difference_update(set(suppliers_before)) new_supplier = list(possible_suppliers)[0] supplier_list_dht_v1( customer_id='customer-2@id-a_8084', observers_ids=[new_supplier, 'customer-2@id-a_8084', 'customer-3@id-a_8084', ], expected_ecc_map='ecc/4x4', expected_suppliers_number=4, ) supplier_list_dht_v1( customer_id='customer-2@id-a_8084', observers_ids=['customer-2@id-a_8084', 'customer-3@id-a_8084', ], expected_ecc_map='ecc/4x4', expected_suppliers_number=4, ) supplier_list_dht_v1( customer_id='customer-2@id-a_8084', observers_ids=['customer-3@id-a_8084', 'customer-1@id-a_8084', ], expected_ecc_map='ecc/4x4', expected_suppliers_number=4, ) supplier_list_dht_v1( customer_id='customer-2@id-a_8084', observers_ids=['supplier-2@id-a_8084', 'customer-3@id-a_8084', 'customer-1@id-a_8084', ], expected_ecc_map='ecc/4x4', expected_suppliers_number=4, ) share_id_customer_2 = share_create_v1('customer-2') filename = 'file_to_be_distributed.txt' virtual_filename = filename volume_customer_2 = '/customer_2' filepath_customer_2 = f'{volume_customer_2}/{filename}' remote_path_customer_2 = f'{share_id_customer_2}:{virtual_filename}' run_ssh_command_and_wait('customer-2', f'echo customer_2 > {filepath_customer_2}') file_create_v1('customer-2', remote_path_customer_2) file_upload_start_v1('customer-2', remote_path_customer_2, filepath_customer_2) service_info_v1('customer-2', 'service_shared_data', 'ON') packet_list_v1('customer-2', wait_all_finish=True) transfer_list_v1('customer-2', wait_all_finish=True) file_download_start_v1('customer-2', remote_path=remote_path_customer_2, destination=volume_customer_2) response = request_put('customer-2', 'supplier/switch/v1', json={ 'position': '0', 'new_global_id': new_supplier, }) assert response.status_code == 200 packet_list_v1('supplier-1', wait_all_finish=True) packet_list_v1('supplier-2', wait_all_finish=True) packet_list_v1('supplier-3', wait_all_finish=True) packet_list_v1('supplier-4', wait_all_finish=True) packet_list_v1('supplier-5', wait_all_finish=True) packet_list_v1('supplier-6', wait_all_finish=True) packet_list_v1('supplier-7', wait_all_finish=True) packet_list_v1('supplier-8', wait_all_finish=True) supplier_list_v1('customer-2', expected_min_suppliers=4, expected_max_suppliers=4) service_info_v1('customer-2', 'service_shared_data', 'ON') supplier_list_dht_v1( customer_id='customer-2@id-a_8084', observers_ids=[new_supplier, 'customer-2@id-a_8084', 'customer-3@id-a_8084', ], expected_ecc_map='ecc/4x4', expected_suppliers_number=4, ) supplier_list_dht_v1( customer_id='customer-2@id-a_8084', observers_ids=['customer-2@id-a_8084', 'customer-3@id-a_8084', ], expected_ecc_map='ecc/4x4', expected_suppliers_number=4, ) supplier_list_dht_v1( customer_id='customer-2@id-a_8084', observers_ids=['customer-3@id-a_8084', 'customer-1@id-a_8084', ], expected_ecc_map='ecc/4x4', expected_suppliers_number=4, ) supplier_list_dht_v1( customer_id='customer-2@id-a_8084', observers_ids=['supplier-2@id-a_8084', 'customer-3@id-a_8084', 'customer-1@id-a_8084', ], expected_ecc_map='ecc/4x4', expected_suppliers_number=4, ) count = 0 while True: if count > 20: assert False, 'supplier was not switched after many attempts' break response_after = request_get('customer-2', 'supplier/list/v1') assert response_after.status_code == 200 supplier_list_after = response_after.json()['result'] suppliers_after = list([x['global_id'] for x in supplier_list_after]) assert len(suppliers_after) == 4 assert suppliers_after[1] == suppliers_before[1] assert suppliers_after[2] == suppliers_before[2] assert suppliers_after[3] == suppliers_before[3] if suppliers_after[0] != suppliers_before[0] and suppliers_after[0] == new_supplier: break count += 1 time.sleep(1)
def test_customer_1_upload_download_file_with_master_key(): if os.environ.get('RUN_TESTS', '1') == '0': return pytest.skip() # @UndefinedVariable packet_list_v1('customer-1', wait_all_finish=True) transfer_list_v1('customer-1', wait_all_finish=True) key_id = 'master$customer-1@id-a_8084' shared_volume = '/customer_1' origin_filename = 'file_customer_1.txt' run_ssh_command_and_wait( 'customer-1', f'python -c "import os, base64; print(base64.b64encode(os.urandom(30000)).decode())" > {shared_volume}/{origin_filename}' ) local_path = '%s/%s' % (shared_volume, origin_filename) virtual_file = 'virtual_file.txt' remote_path = '%s:%s' % (key_id, virtual_file) download_volume = '/customer_1' downloaded_file = '%s/%s' % (download_volume, virtual_file) count = 0 while True: if count > 10: assert False, 'customer-1 failed to hire enough suppliers after many attempts' return response = request_get('customer-1', 'supplier/list/v1') assert response.status_code == 200 assert response.json()['status'] == 'OK', response.json() print('\n\nsupplier/list/v1 : %s\n' % response.json()) if len(response.json()['result']) == 2: for s in response.json()['result']: assert s['supplier_state'] == 'CONNECTED' assert s['contact_state'] == 'CONNECTED' assert True break else: print('\nstill see %d suppliers, expect 2 suppliers\n' % len(response.json()['result'])) count += 1 time.sleep(5) service_info_v1('customer-1', 'service_shared_data', 'ON') file_create_v1('customer-1', remote_path) file_upload_start_v1( 'customer-1', remote_path, local_path, wait_result=True, ) packet_list_v1('customer-1', wait_all_finish=True) transfer_list_v1('customer-1', wait_all_finish=True) for _ in range(20): response = request_post( 'customer-1', 'file/download/start/v1', json={ 'remote_path': remote_path, 'destination_folder': download_volume, 'wait_result': '1', }, ) assert response.status_code == 200 if response.json()['status'] == 'OK': break if response.json()['errors'][0].startswith( 'download not possible, uploading'): time.sleep(1) else: assert False, response.json() else: assert False, 'download was not successful: %r' % response.json() local_file_src = run_ssh_command_and_wait('customer-1', 'cat %s' % local_path)[0].strip() print('customer-1: file %s is %d bytes long' % (local_path, len(local_file_src))) downloaded_file_src = run_ssh_command_and_wait('customer-1', 'cat %s' % downloaded_file)[0].strip() print('customer-1: file %s is %d bytes long' % (downloaded_file, len(downloaded_file_src))) assert local_file_src == downloaded_file_src, ( local_file_src, downloaded_file_src, )
def kill_all_nodes(): for node in ALL_NODES: print('Shutdown %s' % node) tsup.run_ssh_command_and_wait(node, 'pkill -e sshd') print('All nodes stopped')
def test_identity_recover_from_customer_backup_to_customer_restore(): if os.environ.get('RUN_TESTS', '1') == '0': return pytest.skip() # @UndefinedVariable # step1: first upload/download one file on customer_backup key_id = 'master$customer-backup@id-a_8084' source_volume = '/customer_backup' origin_filename = 'file_customer_backup.txt' source_local_path = '%s/%s' % (source_volume, origin_filename) virtual_file = 'virtual_file.txt' remote_path = '%s:%s' % (key_id, virtual_file) download_volume = '/customer_backup' downloaded_file = '%s/%s' % (download_volume, virtual_file) supplier_list_v1('customer-backup', expected_min_suppliers=2, expected_max_suppliers=2) service_info_v1('customer-backup', 'service_shared_data', 'ON') supplier_list_dht_v1( customer_id='customer-backup@id-a_8084', observers_ids=[ 'customer-backup@id-a_8084', 'supplier-1@id-a_8084', 'supplier-2@id-a_8084', ], expected_ecc_map='ecc/2x2', expected_suppliers_number=2, ) supplier_list_dht_v1( customer_id='customer-backup@id-a_8084', observers_ids=[ 'supplier-1@id-a_8084', 'supplier-2@id-a_8084', 'customer-backup@id-a_8084', ], expected_ecc_map='ecc/2x2', expected_suppliers_number=2, ) supplier_list_dht_v1( customer_id='customer-backup@id-a_8084', observers_ids=[ 'supplier-2@id-a_8084', 'customer-backup@id-a_8084', 'supplier-1@id-a_8084', ], expected_ecc_map='ecc/2x2', expected_suppliers_number=2, ) file_create_v1('customer-backup', remote_path) run_ssh_command_and_wait( 'customer-backup', f'python -c "import os, base64; print(base64.b64encode(os.urandom(30000)).decode())" > /customer_backup/{origin_filename}' ) file_upload_start_v1('customer-backup', remote_path, source_local_path) service_info_v1('customer-backup', 'service_shared_data', 'ON') file_download_start_v1('customer-backup', remote_path, download_volume, open_share=False) source_local_file_src = run_ssh_command_and_wait( 'customer-backup', 'cat %s' % source_local_path)[0].strip() print('customer-backup: file %s is %d bytes long' % (source_local_path, len(source_local_file_src))) downloaded_file_src = run_ssh_command_and_wait( 'customer-backup', 'cat %s' % downloaded_file)[0].strip() print('customer-backup: file %s is %d bytes long' % (downloaded_file, len(downloaded_file_src))) assert source_local_file_src == downloaded_file_src, ( source_local_file_src, downloaded_file_src, ) # step2: backup customer-backup private key and stop that container backup_file_directory_c2 = '/customer_backup/identity.backup' backup_file_directory_c3 = '/customer_restore/identity.backup' assert not os.path.exists(backup_file_directory_c2) response = request_post( 'customer-backup', 'identity/backup/v1', json={ 'destination_path': backup_file_directory_c2, }, ) print('\n\nidentity/backup/v1 : %s\n' % response.json()) assert response.json()['status'] == 'OK', response.json() # copy private key from one container to another # just like when you backup your private key and restore it from USB stick on another device shutil.move(backup_file_directory_c2, backup_file_directory_c3) # to make sure all uploads to finish transfer_list_v1('customer-backup', wait_all_finish=True) packet_list_v1('customer-backup', wait_all_finish=True) file_list_all_v1('customer-backup') supplier_list_dht_v1( customer_id='customer-backup@id-a_8084', observers_ids=[ 'customer-backup@id-a_8084', 'supplier-1@id-a_8084', 'supplier-2@id-a_8084', ], expected_ecc_map='ecc/2x2', expected_suppliers_number=2, ) supplier_list_dht_v1( customer_id='customer-backup@id-a_8084', observers_ids=[ 'supplier-1@id-a_8084', 'supplier-2@id-a_8084', 'customer-backup@id-a_8084', ], expected_ecc_map='ecc/2x2', expected_suppliers_number=2, ) supplier_list_dht_v1( customer_id='customer-backup@id-a_8084', observers_ids=[ 'supplier-2@id-a_8084', 'customer-backup@id-a_8084', 'supplier-1@id-a_8084', ], expected_ecc_map='ecc/2x2', expected_suppliers_number=2, ) try: response = request_get('customer-backup', 'process/stop/v1') assert response.json()['status'] == 'OK', response.json() except Exception as exc: print(f'\n\nprocess/stop/v1 failed with {exc}') # step3: recover key on customer-restore container and join network for _ in range(5): response = request_post( 'customer-restore', 'identity/recover/v1', json={ 'private_key_local_file': backup_file_directory_c3, }, ) print('\n\nidentity/recover/v1 : %s\n' % response.json()) if response.json()['status'] == 'OK': break time.sleep(1) else: assert False, 'customer-restore was not able to recover identity after few seconds' response = request_get('customer-restore', 'network/connected/v1?wait_timeout=1') assert response.json()['status'] == 'ERROR' for _ in range(5): response = request_get('customer-restore', 'network/connected/v1?wait_timeout=5') if response.json()['status'] == 'OK': break time.sleep(5) else: assert False, 'customer-restore was not able to join the network after identity recover' supplier_list_v1('customer-restore', expected_min_suppliers=2, expected_max_suppliers=2) service_info_v1('customer-restore', 'service_shared_data', 'ON') supplier_list_dht_v1( customer_id='customer-backup@id-a_8084', observers_ids=[ 'customer-restore@id-a_8084', 'supplier-3@id-a_8084', 'supplier-1@id-a_8084', ], expected_ecc_map='ecc/2x2', expected_suppliers_number=2, ) supplier_list_dht_v1( customer_id='customer-backup@id-a_8084', observers_ids=[ 'supplier-3@id-a_8084', 'supplier-1@id-a_8084', 'customer-restore@id-a_8084', ], expected_ecc_map='ecc/2x2', expected_suppliers_number=2, ) supplier_list_dht_v1( customer_id='customer-backup@id-a_8084', observers_ids=[ 'supplier-1@id-a_8084', 'customer-restore@id-a_8084', 'supplier-3@id-a_8084', ], expected_ecc_map='ecc/2x2', expected_suppliers_number=2, ) file_list_all_v1('customer-restore') # step4: try to recover stored file again key_id = 'master$customer-backup@id-a_8084' recover_volume = '/customer_restore' virtual_file = 'virtual_file.txt' remote_path = '%s:%s' % (key_id, virtual_file) recovered_file = '%s/%s' % (recover_volume, virtual_file) for _ in range(20): response = request_post( 'customer-restore', 'file/download/start/v1', json={ 'remote_path': remote_path, 'destination_folder': recover_volume, 'wait_result': '1', }, ) assert response.status_code == 200 if response.json()['status'] == 'OK': break if response.json()['errors'][0].startswith( 'download not possible, uploading'): time.sleep(1) else: assert False, response.json() else: assert False, 'download was not successful: %r' % response.json() recovered_file_src = run_ssh_command_and_wait( 'customer-restore', 'cat %s' % recovered_file)[0].strip() print('customer-restore:%s' % recovered_file, recovered_file_src) assert source_local_file_src == recovered_file_src, ( source_local_file_src, recovered_file_src, )
def test_identity_rotate_supplier_1(): if os.environ.get('RUN_TESTS', '1') == '0': return pytest.skip() # @UndefinedVariable # first create supplier-1 identity and start the node, identity will be rotated later create_identity('supplier-1', 'supplier-1') connect_network('supplier-1') r = identity_get_v1('supplier-1') supplier_1_global_id = r['result']['global_id'] supplier_1_idurl = r['result']['idurl'] service_info_v1('supplier-1', 'service_supplier', 'ON') # make sure supplier-1 was hired by customer-1 current_suppliers_idurls = supplier_list_v1('customer-1', expected_min_suppliers=2, expected_max_suppliers=2) service_info_v1('customer-1', 'service_shared_data', 'ON') # if he is not hired yet, we switch our first supplier to supplier-1 if supplier_1_idurl not in current_suppliers_idurls: supplier_switch_v1('customer-1', supplier_idurl=supplier_1_idurl, position=0) service_info_v1('customer-1', 'service_shared_data', 'ON') current_suppliers_idurls = supplier_list_v1('customer-1', expected_min_suppliers=2, expected_max_suppliers=2) assert supplier_1_idurl in current_suppliers_idurls share_id_customer_1 = share_create_v1('customer-1') filename = 'cat.txt' virtual_filename = filename volume_customer_1 = '/customer_1' filepath_customer_1 = f'{volume_customer_1}/{filename}' remote_path_customer_1 = f'{share_id_customer_1}:{virtual_filename}' download_filepath_customer_1 = f'/tmp/{filename}' run_ssh_command_and_wait('customer-1', f'echo "customer_1" > {filepath_customer_1}') file_create_v1('customer-1', remote_path_customer_1) file_upload_start_v1('customer-1', remote_path_customer_1, filepath_customer_1) packet_list_v1('customer-1', wait_all_finish=True) transfer_list_v1('customer-1', wait_all_finish=True) service_info_v1('customer-1', 'service_shared_data', 'ON') file_download_start_v1('customer-1', remote_path=remote_path_customer_1, destination='/tmp') file_1 = run_ssh_command_and_wait('customer-1', f'cat {filepath_customer_1}')[0].strip() file_2 = run_ssh_command_and_wait( 'customer-1', f'cat {download_filepath_customer_1}')[0].strip() assert file_1 == file_2 # rotate identity sources on supplier-1 identity_rotate_v1('supplier-1') time.sleep(1) r = identity_get_v1('supplier-1') supplier_1_global_id_new = r['result']['global_id'] supplier_1_idurl_new = r['result']['idurl'] assert supplier_1_global_id_new != supplier_1_global_id assert supplier_1_idurl_new != supplier_1_idurl service_info_v1('supplier-1', 'service_gateway', 'ON') service_info_v1('supplier-1', 'service_supplier', 'ON') file_sync_v1('customer-1') time.sleep(1) packet_list_v1('customer-1', wait_all_finish=True) transfer_list_v1('customer-1', wait_all_finish=True) service_info_v1('customer-1', 'service_shared_data', 'ON') file_list_all_v1('customer-1')