|
| 1 | +import pytest |
| 2 | + |
| 3 | +from aleph.vm import haproxy |
| 4 | + |
| 5 | +# Sample response for https://api.dns.public.aleph.sh/instances/list |
| 6 | +sample_domain_instance_list = [ |
| 7 | + { |
| 8 | + "name": "api-dev.thronetools.com", |
| 9 | + "item_hash": "747b52c712e16642b498f16c4c6e68d5fb00ddbaf8d2a0dc7bd298d33abb9124", |
| 10 | + "ipv6": "2a01:240:ad00:2502:3:747b:52c7:12e1", |
| 11 | + "ipv4": {"public": "46.247.131.211", "local": "172.16.15.1/32"}, |
| 12 | + }, |
| 13 | + { |
| 14 | + "name": "centurion.cybernetwork.me", |
| 15 | + "item_hash": "cefb9373558927d70365746900a410f01e1340ecff0dda93deb672f55bb70ac8", |
| 16 | + "ipv6": "2a01:240:ad00:2502:3:cefb:9373:5581", |
| 17 | + "ipv4": {"public": "46.247.131.211", "local": "172.16.52.1/32"}, |
| 18 | + }, |
| 19 | + { |
| 20 | + "name": "cms-dev.thronetools.com", |
| 21 | + "item_hash": "747b52c712e16642b498f16c4c6e68d5fb00ddbaf8d2a0dc7bd298d33abb9124", |
| 22 | + "ipv6": "2a01:240:ad00:2502:3:747b:52c7:12e1", |
| 23 | + "ipv4": {"public": "46.247.131.211", "local": "172.16.15.1/32"}, |
| 24 | + }, |
| 25 | + { |
| 26 | + "name": "platform-api.3mera.dev", |
| 27 | + "item_hash": "d78e81d99e7468302bdaf82b5ca338b486629cf813384bdc3282e2b8fa7f478f", |
| 28 | + "ipv6": "2a01:240:ad00:2502:3:d78e:81d9:9e71", |
| 29 | + "ipv4": {"public": "46.247.131.211", "local": "172.16.30.1/32"}, |
| 30 | + }, |
| 31 | + { |
| 32 | + "name": "platform-variants.3mera.dev", |
| 33 | + "item_hash": "d78e81d99e7468302bdaf82b5ca338b486629cf813384bdc3282e2b8fa7f478f", |
| 34 | + "ipv6": "2a01:240:ad00:2502:3:d78e:81d9:9e71", |
| 35 | + "ipv4": {"public": "46.247.131.211", "local": "172.16.30.1/32"}, |
| 36 | + }, |
| 37 | + { |
| 38 | + "name": "platform.3mera.dev", |
| 39 | + "item_hash": "d78e81d99e7468302bdaf82b5ca338b486629cf813384bdc3282e2b8fa7f478f", |
| 40 | + "ipv6": "2a01:240:ad00:2502:3:d78e:81d9:9e71", |
| 41 | + "ipv4": {"public": "46.247.131.211", "local": "172.16.30.1/32"}, |
| 42 | + }, |
| 43 | + { |
| 44 | + "name": "praetorian.cybernetwork.me", |
| 45 | + "item_hash": "ec18fa850f6a530a8c0e6a616b0df5def3ab3662eb6feeba8ece580780a86dc6", |
| 46 | + "ipv6": "2a01:240:ad00:2502:3:ec18:fa85:f61", |
| 47 | + "ipv4": {"public": "46.247.131.211", "local": "172.16.19.1/32"}, |
| 48 | + }, |
| 49 | + { |
| 50 | + "name": "template-frontend.3mera.dev", |
| 51 | + "item_hash": "d78e81d99e7468302bdaf82b5ca338b486629cf813384bdc3282e2b8fa7f478f", |
| 52 | + "ipv6": "2a01:240:ad00:2502:3:d78e:81d9:9e71", |
| 53 | + "ipv4": {"public": "46.247.131.211", "local": "172.16.30.1/32"}, |
| 54 | + }, |
| 55 | + { |
| 56 | + "name": "test-twentysix-cloud.gerardmolina.com", |
| 57 | + "item_hash": "31826cf53c655bd25d50f7e371242baf240d4f854372c798a37bb9eb6c562682", |
| 58 | + "ipv6": "2a01:240:ad00:2501:3:3182:6cf5:3c61", |
| 59 | + "ipv4": {"public": "46.255.204.201", "local": "172.16.5.1/32"}, |
| 60 | + }, |
| 61 | +] |
| 62 | + |
| 63 | + |
| 64 | +@pytest.fixture |
| 65 | +def mock_sample_domain_instance_list(mocker): |
| 66 | + mocker.patch("aleph.vm.haproxy.fetch_list", mocker.AsyncMock(return_value=sample_domain_instance_list)) |
| 67 | + |
| 68 | + |
| 69 | +@pytest.mark.asyncio |
| 70 | +async def test_fetch_list(mock_sample_domain_instance_list): |
| 71 | + list = await haproxy.fetch_list() |
| 72 | + assert len(list) == 9 |
| 73 | + |
| 74 | + |
| 75 | +@pytest.fixture |
| 76 | +def mock_small_domain_list(mocker): |
| 77 | + small_list = [ |
| 78 | + { |
| 79 | + "name": "echo.agot.be", |
| 80 | + "item_hash": "decadecadecadecadecadecadecadecadecadecadecadecadecadecadecadeca", |
| 81 | + "ipv6": "2a01:240:ad00:2502:3:747b:52c7:12e1", |
| 82 | + "ipv4": {"public": "46.247.131.211", "local": "172.16.4.1/32"}, |
| 83 | + } |
| 84 | + ] |
| 85 | + mocker.patch("aleph.vm.haproxy.fetch_list", mocker.AsyncMock(return_value=small_list)) |
| 86 | + |
| 87 | + |
| 88 | +@pytest.mark.asyncio |
| 89 | +async def test_update_map_file(mock_small_domain_list, tmp_path): |
| 90 | + map_file_path = tmp_path / "backend.map" |
| 91 | + instance_list = await haproxy.fetch_list() |
| 92 | + assert instance_list |
| 93 | + |
| 94 | + haproxy.update_mapfile(instance_list, str(map_file_path), 22) |
| 95 | + content = map_file_path.read_text() |
| 96 | + assert content == "echo.agot.be 172.16.4.2:22\n" |
| 97 | + |
| 98 | + |
| 99 | +@pytest.fixture |
| 100 | +def mock_socket_command(mocker): |
| 101 | + commands = [] |
| 102 | + existing_servers: list[str] = [] |
| 103 | + |
| 104 | + def mock_response(socket_path, command): # noqa: ARG001 |
| 105 | + commands.append(command) |
| 106 | + if "show servers state" in command: |
| 107 | + return "1\n# be_id be_name srv_id srv_name srv_addr srv_op_state\n" + "\n".join(mock.existing_servers) |
| 108 | + elif "disable server" in command: |
| 109 | + return "" |
| 110 | + elif "set server" in command: |
| 111 | + return "" |
| 112 | + elif "enable server" in command: |
| 113 | + return "" |
| 114 | + return "" |
| 115 | + |
| 116 | + mock = mocker.patch("aleph.vm.haproxy.send_socket_command", mock_response) |
| 117 | + mock.existing_servers = existing_servers |
| 118 | + mock.commands = commands |
| 119 | + return mock |
| 120 | + |
| 121 | + |
| 122 | +@pytest.mark.asyncio |
| 123 | +async def test_update_backend_add_server(mock_socket_command, tmp_path): |
| 124 | + map_file_path = tmp_path / "backend.map" |
| 125 | + map_file_path.write_text("echo.agot.be 172.16.4.2:22\n") |
| 126 | + socket_path = "fakyfake" |
| 127 | + |
| 128 | + # Run test |
| 129 | + haproxy.update_haproxy_backends(socket_path, "test_backend", map_file_path, weight=1) |
| 130 | + |
| 131 | + # Verify commands |
| 132 | + assert mock_socket_command.commands == [ |
| 133 | + "show servers state test_backend", |
| 134 | + # "disable server test_backend echo.agot.be", |
| 135 | + "add server test_backend/echo.agot.be 172.16.4.2:22 weight 1 maxconn 30", |
| 136 | + # "set server test_backend echo.agot.be addr 172.16.4.2 port 22", |
| 137 | + # "set server test_backend echo.agot.be weight 1", |
| 138 | + "enable server test_backend/echo.agot.be", |
| 139 | + ] |
| 140 | + |
| 141 | + |
| 142 | +@pytest.mark.asyncio |
| 143 | +def test_update_backend_add_server_remove_server(mock_socket_command, tmp_path): |
| 144 | + map_file_path = tmp_path / "backend.map" |
| 145 | + map_file_path.write_text("echo.agot.be 172.16.4.2:22\n") |
| 146 | + socket_path = "fakyfake" |
| 147 | + |
| 148 | + mock_socket_command.existing_servers = [ |
| 149 | + "8 test_backend 1 existing_bk 127.0.0.1 2 0 1 1 683294 1 0 2 0 0 0 0 - 4020 - 0 0 - - 0" |
| 150 | + ] |
| 151 | + haproxy.update_haproxy_backends(socket_path, "test_backend", map_file_path, weight=1) |
| 152 | + |
| 153 | + # Verify commands |
| 154 | + assert mock_socket_command.commands == [ |
| 155 | + "show servers state test_backend", |
| 156 | + "add server test_backend/echo.agot.be 172.16.4.2:22 weight 1 maxconn 30", |
| 157 | + "enable server test_backend/echo.agot.be", |
| 158 | + "set server test_backend/existing_bk state maint", |
| 159 | + "del server test_backend/existing_bk", |
| 160 | + ] |
| 161 | + |
| 162 | + |
| 163 | +@pytest.mark.asyncio |
| 164 | +def test_update_backend_do_no_remove_fallback(mock_socket_command, tmp_path): |
| 165 | + map_file_path = tmp_path / "backend.map" |
| 166 | + map_file_path.write_text("echo.agot.be 172.16.4.2:22\n") |
| 167 | + socket_path = "fakyfake" |
| 168 | + |
| 169 | + mock_socket_command.existing_servers = [ |
| 170 | + "8 test_backend 1 fallback_local 127.0.0.1 2 0 1 1 683294 1 0 2 0 0 0 0 - 4020 - 0 0 - - 0" |
| 171 | + ] |
| 172 | + haproxy.update_haproxy_backends(socket_path, "test_backend", map_file_path, weight=1) |
| 173 | + |
| 174 | + # Verify commands |
| 175 | + assert mock_socket_command.commands == [ |
| 176 | + "show servers state test_backend", |
| 177 | + "add server test_backend/echo.agot.be 172.16.4.2:22 weight 1 maxconn 30", |
| 178 | + "enable server test_backend/echo.agot.be", |
| 179 | + ] |
0 commit comments