|
| 1 | +from unittest.mock import MagicMock, create_autospec, patch |
| 2 | + |
| 3 | +import oci |
| 4 | +import pytest |
| 5 | +from fastmcp import Client |
| 6 | +from oracle.oci_network_load_balancer_mcp_server.server import mcp |
| 7 | + |
| 8 | + |
| 9 | +class TestNlbTools: |
| 10 | + @pytest.mark.asyncio |
| 11 | + @patch("oracle.oci_network_load_balancer_mcp_server.server.get_nlb_client") |
| 12 | + async def test_list_nlbs(self, mock_get_client): |
| 13 | + mock_client = MagicMock() |
| 14 | + mock_get_client.return_value = mock_client |
| 15 | + |
| 16 | + mock_list_response = create_autospec(oci.response.Response) |
| 17 | + mock_list_response.data = ( |
| 18 | + oci.network_load_balancer.models.NetworkLoadBalancerCollection( |
| 19 | + items=[ |
| 20 | + oci.network_load_balancer.models.NetworkLoadBalancerSummary( |
| 21 | + id="nlb1", |
| 22 | + display_name="NLB 1", |
| 23 | + lifecycle_state="ACTIVE", |
| 24 | + ip_addresses=[ |
| 25 | + oci.network_load_balancer.models.IpAddress( |
| 26 | + ip_address="192.168.1.1", is_public=True |
| 27 | + ), |
| 28 | + oci.network_load_balancer.models.IpAddress( |
| 29 | + ip_address="10.0.0.0", is_public=False |
| 30 | + ), |
| 31 | + ], |
| 32 | + ) |
| 33 | + ] |
| 34 | + ) |
| 35 | + ) |
| 36 | + mock_client.list_network_load_balancers.return_value = mock_list_response |
| 37 | + |
| 38 | + async with Client(mcp) as client: |
| 39 | + result = ( |
| 40 | + await client.call_tool( |
| 41 | + "list_network_load_balancers", |
| 42 | + {"compartment_id": "test_compartment"}, |
| 43 | + ) |
| 44 | + ).structured_content["result"] |
| 45 | + |
| 46 | + assert len(result) == 1 |
| 47 | + assert result[0]["nlb_id"] == "nlb1" |
| 48 | + |
| 49 | + @pytest.mark.asyncio |
| 50 | + @patch("oracle.oci_network_load_balancer_mcp_server.server.get_nlb_client") |
| 51 | + async def test_list_listeners(self, mock_get_client): |
| 52 | + mock_client = MagicMock() |
| 53 | + mock_get_client.return_value = mock_client |
| 54 | + |
| 55 | + mock_list_response = create_autospec(oci.response.Response) |
| 56 | + mock_list_response.data = oci.network_load_balancer.models.ListenerCollection( |
| 57 | + items=[ |
| 58 | + oci.network_load_balancer.models.ListenerSummary( |
| 59 | + name="Listener 1", |
| 60 | + ip_version="IPV4", |
| 61 | + protocol="HTTP", |
| 62 | + port=8008, |
| 63 | + is_ppv2_enabled=False, |
| 64 | + ) |
| 65 | + ] |
| 66 | + ) |
| 67 | + mock_client.list_listeners.return_value = mock_list_response |
| 68 | + |
| 69 | + async with Client(mcp) as client: |
| 70 | + result = ( |
| 71 | + await client.call_tool( |
| 72 | + "list_network_load_balancer_listeners", |
| 73 | + {"network_load_balancer_id": "test_nlb"}, |
| 74 | + ) |
| 75 | + ).structured_content["result"] |
| 76 | + |
| 77 | + assert len(result) == 1 |
| 78 | + assert result[0]["name"] == "Listener 1" |
| 79 | + |
| 80 | + @pytest.mark.asyncio |
| 81 | + @patch("oracle.oci_network_load_balancer_mcp_server.server.get_nlb_client") |
| 82 | + async def test_list_backend_sets(self, mock_get_client): |
| 83 | + mock_client = MagicMock() |
| 84 | + mock_get_client.return_value = mock_client |
| 85 | + |
| 86 | + mock_list_response = create_autospec(oci.response.Response) |
| 87 | + mock_list_response.data = oci.network_load_balancer.models.BackendSetCollection( |
| 88 | + items=[ |
| 89 | + oci.network_load_balancer.models.BackendSetSummary( |
| 90 | + name="Backend Set 1", |
| 91 | + ip_version="IPV4", |
| 92 | + are_operationally_active_backends_preferred=False, |
| 93 | + policy="3-TUPLE", |
| 94 | + backends=[], |
| 95 | + ) |
| 96 | + ] |
| 97 | + ) |
| 98 | + mock_client.list_backend_sets.return_value = mock_list_response |
| 99 | + |
| 100 | + async with Client(mcp) as client: |
| 101 | + result = ( |
| 102 | + await client.call_tool( |
| 103 | + "list_network_load_balancer_backend_sets", |
| 104 | + {"network_load_balancer_id": "test_nlb"}, |
| 105 | + ) |
| 106 | + ).structured_content["result"] |
| 107 | + |
| 108 | + assert len(result) == 1 |
| 109 | + assert result[0]["name"] == "Backend Set 1" |
| 110 | + |
| 111 | + @pytest.mark.asyncio |
| 112 | + @patch("oracle.oci_network_load_balancer_mcp_server.server.get_nlb_client") |
| 113 | + async def test_list_backends(self, mock_get_client): |
| 114 | + mock_client = MagicMock() |
| 115 | + mock_get_client.return_value = mock_client |
| 116 | + |
| 117 | + mock_list_response = create_autospec(oci.response.Response) |
| 118 | + mock_list_response.data = oci.network_load_balancer.models.BackendCollection( |
| 119 | + items=[ |
| 120 | + oci.network_load_balancer.models.BackendSummary( |
| 121 | + name="Backend 1", |
| 122 | + ip_address="192.168.1.1", |
| 123 | + port=8008, |
| 124 | + weight=0, |
| 125 | + is_drain=False, |
| 126 | + is_backup=False, |
| 127 | + is_offline=False, |
| 128 | + ) |
| 129 | + ] |
| 130 | + ) |
| 131 | + mock_client.list_backends.return_value = mock_list_response |
| 132 | + |
| 133 | + async with Client(mcp) as client: |
| 134 | + result = ( |
| 135 | + await client.call_tool( |
| 136 | + "list_network_load_balancer_backends", |
| 137 | + { |
| 138 | + "network_load_balancer_id": "test_nlb", |
| 139 | + "backend_set_name": "test_backend_set", |
| 140 | + }, |
| 141 | + ) |
| 142 | + ).structured_content["result"] |
| 143 | + |
| 144 | + assert len(result) == 1 |
| 145 | + assert result[0]["name"] == "Backend 1" |
0 commit comments