|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# WARNING - to work with python environments we cannot use /bin/python3 or |
| 3 | +# a hardcoded abs path. |
| 4 | +""" |
| 5 | +Integration tests for DataFed MessageLib.API class. |
| 6 | +
|
| 7 | +These tests assume a mock DataFed service is running externally and will |
| 8 | +interact with it to test various scenarios including connection establishment, |
| 9 | +authentication, message handling, and error conditions. |
| 10 | +""" |
| 11 | + |
| 12 | +import pytest |
| 13 | +import os |
| 14 | +import tempfile |
| 15 | +import time |
| 16 | +import zmq |
| 17 | + |
| 18 | +# Assuming the module structure based on the imports in the source file |
| 19 | +from datafed import CommandLib |
| 20 | +from datafed import SDMS_Anon_pb2 as anon |
| 21 | +from datafed import SDMS_Auth_pb2 as auth |
| 22 | +from datafed import Version_pb2 |
| 23 | +from mock_defaults import Defaults |
| 24 | + |
| 25 | +# Test configuration - adjust these based on your mock service setup |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture |
| 29 | +def command_lib_options(): |
| 30 | + """Create temporary key files for testing.""" |
| 31 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 32 | + |
| 33 | + server_key_file = os.getenv("DATAFED_MOCK_CORE_PUB_KEY", os.path.join(temp_dir, "server.key")) |
| 34 | + client_pub_key_file = os.path.join(temp_dir, "client.pub") |
| 35 | + client_priv_key_file = os.path.join(temp_dir, "client.priv") |
| 36 | + |
| 37 | + if os.path.exists(server_key_file): |
| 38 | + # grab the key from the file instead of writing the default key |
| 39 | + # to the file. |
| 40 | + with open(server_key_file, "r") as f: |
| 41 | + server_key = f.read().strip() |
| 42 | + else: |
| 43 | + with open(server_key_file, 'w') as f: |
| 44 | + f.write(Defaults.mock_core_server_public_key) |
| 45 | + server_key = Defaults.mock_core_server_public_key |
| 46 | + |
| 47 | + with open(client_pub_key_file, 'w') as f: |
| 48 | + f.write(Defaults.mock_python_client_public_key) |
| 49 | + with open(client_priv_key_file, 'w') as f: |
| 50 | + f.write(Defaults.mock_python_client_private_key) |
| 51 | + |
| 52 | + yield { |
| 53 | + 'client_pub_key_file': client_pub_key_file, |
| 54 | + 'client_priv_key_file': client_priv_key_file, |
| 55 | + 'server_pub_key_file': server_key_file, |
| 56 | + 'server_port': Defaults.mock_core_server_port, |
| 57 | + 'server_host': Defaults.mock_core_server_host |
| 58 | + } |
| 59 | + |
| 60 | +@pytest.fixture |
| 61 | +def repo_create_options(): |
| 62 | + """Create temporary key files for testing.""" |
| 63 | + yield { |
| 64 | + 'repo_id': Defaults.mock_repo_id, |
| 65 | + 'title': Defaults.mock_repo_title, |
| 66 | + 'desc': Defaults.mock_repo_desc, |
| 67 | + 'capacity': Defaults.mock_repo_capacity, |
| 68 | + 'pub_key': Defaults.mock_repo_pub_key, |
| 69 | + 'address': Defaults.mock_repo_address, |
| 70 | + 'endpoint': Defaults.mock_repo_globus_uuid, |
| 71 | + 'path': Defaults.mock_repo_path, |
| 72 | + 'type': Defaults.mock_repo_type, |
| 73 | + 'admins': [Defaults.mock_repo_admin] |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | +class TestCommandLibRepo: |
| 78 | + """Test various connection establishment scenarios.""" |
| 79 | + |
| 80 | + def test_successful_connection_with_key_files(self, command_lib_options, repo_create_options): |
| 81 | + """Test successful connection using key files.""" |
| 82 | + api = CommandLib.API(command_lib_options) |
| 83 | + |
| 84 | + api.loginByPassword(Defaults.mock_authenticated_test_user, Defaults.test_user_password) |
| 85 | + |
| 86 | + result = api.repoCreate( |
| 87 | + repo_id=repo_create_options["repo_id"], |
| 88 | + title=repo_create_options["title"], |
| 89 | + desc=repo_create_options["desc"], |
| 90 | + capacity=repo_create_options["capacity"], |
| 91 | + pub_key=repo_create_options["pub_key"], |
| 92 | + address=repo_create_options["address"], |
| 93 | + endpoint=repo_create_options["endpoint"], |
| 94 | + path=repo_create_options["path"], |
| 95 | + admins=repo_create_options["admins"], |
| 96 | + domain='', |
| 97 | + exp_path='' |
| 98 | + ) |
| 99 | + |
| 100 | + print("Result is") |
| 101 | + print(result) |
| 102 | + repo = result[0].repo[0] |
| 103 | + print(repo) |
| 104 | + print("Admins") |
| 105 | + print(repo.admin) |
| 106 | + assert repo.id == f"repo/{repo_create_options['repo_id']}" |
| 107 | + assert repo.title == repo_create_options['title'] |
| 108 | + assert repo.desc == repo_create_options['desc'] |
| 109 | + assert repo.capacity == repo_create_options['capacity'] |
| 110 | + assert repo.pub_key == repo_create_options['pub_key'] |
| 111 | + assert repo.address == repo_create_options['address'] |
| 112 | + assert repo.endpoint == repo_create_options['endpoint'] |
| 113 | + assert repo.admin == repo_create_options['admins'] |
| 114 | + assert repo.type == repo_create_options['type'] |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + pytest.main([__file__, "-v"]) |
0 commit comments