|
| 1 | +"""Unit tests for CIME.Servers lazy loading. |
| 2 | +
|
| 3 | +These tests verify: |
| 4 | +1. Lazy loading behavior (no subprocess at import time) |
| 5 | +2. Backward compatibility with existing usage patterns |
| 6 | +3. is_protocol_available() API |
| 7 | +""" |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | + |
| 12 | +class TestServersLazyLoading: |
| 13 | + """Tests for lazy loading of server modules.""" |
| 14 | + |
| 15 | + def test_import_does_not_run_which(self): |
| 16 | + """Importing CIME.Servers should not run shutil.which().""" |
| 17 | + import CIME.Servers |
| 18 | + |
| 19 | + assert CIME.Servers is not None |
| 20 | + |
| 21 | + def test_availability_not_checked_at_import(self): |
| 22 | + """Availability flags should be unchecked after import.""" |
| 23 | + import importlib |
| 24 | + import sys |
| 25 | + |
| 26 | + # Force reimport |
| 27 | + for mod in list(sys.modules.keys()): |
| 28 | + if "CIME.Servers" in mod: |
| 29 | + del sys.modules[mod] |
| 30 | + |
| 31 | + import CIME.Servers |
| 32 | + |
| 33 | + assert CIME.Servers is not None |
| 34 | + |
| 35 | + |
| 36 | +class TestProtocolAvailability: |
| 37 | + """Tests for is_protocol_available() API.""" |
| 38 | + |
| 39 | + def test_is_protocol_available_ftp(self): |
| 40 | + """FTP should always be available (stdlib).""" |
| 41 | + import CIME.Servers |
| 42 | + |
| 43 | + assert CIME.Servers.is_protocol_available("ftp") is True |
| 44 | + |
| 45 | + def test_is_protocol_available_invalid(self): |
| 46 | + """Invalid protocol should return False.""" |
| 47 | + import CIME.Servers |
| 48 | + |
| 49 | + assert CIME.Servers.is_protocol_available("invalid") is False |
| 50 | + |
| 51 | + def test_is_protocol_available_case_insensitive(self): |
| 52 | + """Protocol check should be case-insensitive.""" |
| 53 | + import CIME.Servers |
| 54 | + |
| 55 | + assert CIME.Servers.is_protocol_available("FTP") is True |
| 56 | + assert CIME.Servers.is_protocol_available("Ftp") is True |
| 57 | + assert CIME.Servers.is_protocol_available("ftp") is True |
| 58 | + |
| 59 | + |
| 60 | +class TestServerClassAccess: |
| 61 | + """Tests for accessing server classes.""" |
| 62 | + |
| 63 | + def test_ftp_attribute_access(self): |
| 64 | + """Accessing CIME.Servers.FTP should work via __getattr__.""" |
| 65 | + import CIME.Servers |
| 66 | + |
| 67 | + FTP = CIME.Servers.FTP |
| 68 | + assert FTP is not None |
| 69 | + assert FTP.__name__ == "FTP" |
| 70 | + |
| 71 | + def test_ftp_has_ftp_login_method(self): |
| 72 | + """FTP class should have ftp_login class method (used by check_input_data).""" |
| 73 | + import CIME.Servers |
| 74 | + |
| 75 | + assert hasattr(CIME.Servers.FTP, "ftp_login") |
| 76 | + |
| 77 | + def test_wget_has_wget_login_method(self): |
| 78 | + """WGET class should have wget_login class method if available.""" |
| 79 | + import CIME.Servers |
| 80 | + |
| 81 | + if CIME.Servers.is_protocol_available("wget"): |
| 82 | + assert hasattr(CIME.Servers.WGET, "wget_login") |
| 83 | + |
| 84 | + def test_unavailable_server_raises_attribute_error(self): |
| 85 | + """Accessing unavailable server should raise AttributeError.""" |
| 86 | + import CIME.Servers |
| 87 | + |
| 88 | + if not CIME.Servers.is_protocol_available("gftp"): |
| 89 | + with pytest.raises(AttributeError): |
| 90 | + _ = CIME.Servers.GridFTP |
| 91 | + |
| 92 | + |
| 93 | +class TestBackwardCompatibility: |
| 94 | + """Tests for backward compatibility with existing code patterns.""" |
| 95 | + |
| 96 | + def test_has_ftp_attribute(self): |
| 97 | + """has_ftp attribute should return True (FTP always available).""" |
| 98 | + import CIME.Servers |
| 99 | + |
| 100 | + assert CIME.Servers.has_ftp is True |
| 101 | + |
| 102 | + def test_has_attributes_exist(self): |
| 103 | + """All has_* attributes should be accessible.""" |
| 104 | + import CIME.Servers |
| 105 | + |
| 106 | + # These should not raise, regardless of availability |
| 107 | + _ = CIME.Servers.has_ftp |
| 108 | + _ = CIME.Servers.has_svn |
| 109 | + _ = CIME.Servers.has_wget |
| 110 | + _ = CIME.Servers.has_gftp |
| 111 | + |
| 112 | + def test_instantiate_ftp_server(self): |
| 113 | + """Should be able to instantiate FTP server (backward compat).""" |
| 114 | + import CIME.Servers |
| 115 | + |
| 116 | + FTP = CIME.Servers.FTP |
| 117 | + assert FTP is not None |
0 commit comments