def supplier_switch_v1(customer: str, supplier_idurl: str, position: int, validate_retries=30, delay=3): response = request_put(customer, 'supplier/switch/v1', json={ 'index': position, 'new_idurl': supplier_idurl, }, timeout=20) assert response.status_code == 200 dbg('supplier/switch/v1 [%s] with new supplier %s at position %r : %s\n' % ( customer, supplier_idurl, position, pprint.pformat(response.json()))) assert response.json()['status'] == 'OK', response.json() if not validate_retries: return response.json() count = 0 while True: if count >= validate_retries: break current_suppliers_idurls = supplier_list_v1(customer, expected_min_suppliers=None, expected_max_suppliers=None, attempts=1) if supplier_idurl in current_suppliers_idurls: _pos = current_suppliers_idurls.index(supplier_idurl) assert position == _pos dbg('found supplier %r at position %d for customer %r' % (supplier_idurl, position, customer)) return current_suppliers_idurls count += 1 time.sleep(delay) assert False, 'failed to switch supplier at position %r to %r after %d retries' % ( position, supplier_idurl, count, ) return None
def identity_rotate_v1(node): response = request_put(node, 'identity/rotate/v1', timeout=30) assert response.status_code == 200 print('identity/rotate/v1 [%s] : %s\n' % ( node, pprint.pformat(response.json()), )) assert response.json()['status'] == 'OK', response.json() return response.json()
def group_share_v1(customer: str, group_key_id, trusted_id, attempts=1, timeout=60): response = request_put(customer, 'group/share/v1', json={ 'group_key_id': group_key_id, 'trusted_id': trusted_id, 'timeout': timeout, }, timeout=timeout+1, attempts=attempts) assert response.status_code == 200 dbg('group/share/v1 [%s] group_key_id=%r trusted_id=%r : %s\n' % (customer, group_key_id, trusted_id, pprint.pformat(response.json()))) assert response.json()['status'] == 'OK', response.json() return response.json()
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_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 group_reconnect_v1(customer: str, group_key_id, timeout=120): response = request_put(customer, 'group/reconnect/v1', json={'group_key_id': group_key_id, }, timeout=timeout) assert response.status_code == 200 dbg('group/reconnect/v1 [%s] group_key_id=%r : %s\n' % (customer, group_key_id, pprint.pformat(response.json()))) assert response.json()['status'] == 'OK', response.json() return response.json()