Skip to content

Commit c31946a

Browse files
shopewfgebhardtr
authored andcommitted
[CHORE] Add NLB tests (oracle#48)
1 parent ccf6ee2 commit c31946a

File tree

4 files changed

+233
-15
lines changed

4 files changed

+233
-15
lines changed

src/oci-network-load-balancer-mcp-server/oracle/oci_network_load_balancer_mcp_server/server.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
logger = Logger(__name__, level="INFO")
88

9-
mcp = FastMCP(name="oracle.oci-compute-mcp-server")
9+
mcp = FastMCP(name="oracle.oci-network-load-balancer-mcp-server")
1010

1111

12-
def get_network_load_balancer_client():
13-
logger.info("entering get_network_load_balancer_client")
12+
def get_nlb_client():
13+
logger.info("entering get_nlb_client")
1414
config = oci.config.from_file(
1515
profile_name=os.getenv("OCI_CONFIG_PROFILE", oci.config.DEFAULT_PROFILE)
1616
)
@@ -25,9 +25,9 @@ def get_network_load_balancer_client():
2525

2626

2727
@mcp.tool
28-
def list_network_load_balancers(compartment_id: str):
28+
def list_network_load_balancers(compartment_id: str) -> list[dict]:
2929
"""Lists the network load balancers from the given compartment"""
30-
nlb_client = get_network_load_balancer_client()
30+
nlb_client = get_nlb_client()
3131
nlbs = nlb_client.list_network_load_balancers(compartment_id).data.items
3232
return [
3333
{
@@ -46,14 +46,14 @@ def list_network_load_balancers(compartment_id: str):
4646
@mcp.tool
4747
def get_network_load_balancer(network_load_balancer_id: str):
4848
"""Gets the network load balancer with the given ocid"""
49-
nlb_client = get_network_load_balancer_client()
49+
nlb_client = get_nlb_client()
5050
return nlb_client.get_network_load_balancer(network_load_balancer_id).data
5151

5252

5353
@mcp.tool(name="list_network_load_balancer_listeners")
54-
def list_listeners(network_load_balancer_id: str):
54+
def list_listeners(network_load_balancer_id: str) -> list[dict]:
5555
"""Lists the listeners from the given network load balancer"""
56-
nlb_client = get_network_load_balancer_client()
56+
nlb_client = get_nlb_client()
5757
listeners = nlb_client.list_listeners(network_load_balancer_id).data.items
5858
return [
5959
{
@@ -74,14 +74,14 @@ def get_listener(
7474
):
7575
"""Gets the listener with the given listener name
7676
from the given network load balancer"""
77-
nlb_client = get_network_load_balancer_client()
77+
nlb_client = get_nlb_client()
7878
return nlb_client.get_listener(network_load_balancer_id, listener_name).data
7979

8080

8181
@mcp.tool(name="list_network_load_balancer_backend_sets")
82-
def list_backend_sets(network_load_balancer_id: str):
82+
def list_backend_sets(network_load_balancer_id: str) -> list[dict]:
8383
"""Lists the backend sets from the given network load balancer"""
84-
nlb_client = get_network_load_balancer_client()
84+
nlb_client = get_nlb_client()
8585
backend_sets = nlb_client.list_backend_sets(network_load_balancer_id).data.items
8686
return [
8787
{
@@ -102,17 +102,17 @@ def get_backend_set(
102102
):
103103
"""Gets the backend set with the given backend set name
104104
from the given network load balancer"""
105-
nlb_client = get_network_load_balancer_client()
105+
nlb_client = get_nlb_client()
106106
return nlb_client.get_backend_set(network_load_balancer_id, backend_set_name).data
107107

108108

109109
@mcp.tool(name="list_network_load_balancer_backends")
110110
def list_backends(
111111
network_load_balancer_id: str,
112112
backend_set_name: str,
113-
):
113+
) -> list[dict]:
114114
"""Lists the backends from the given backend set and network load balancer"""
115-
nlb_client = get_network_load_balancer_client()
115+
nlb_client = get_nlb_client()
116116
backends = nlb_client.list_backends(
117117
network_load_balancer_id, backend_set_name
118118
).data.items
@@ -138,7 +138,7 @@ def get_backend(
138138
):
139139
"""Gets the backend with the given backend name
140140
from the given backend set and network load balancer"""
141-
nlb_client = get_network_load_balancer_client()
141+
nlb_client = get_nlb_client()
142142
return nlb_client.get_backend(
143143
network_load_balancer_id, backend_set_name, backend_name
144144
).data
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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"

src/oci-network-load-balancer-mcp-server/pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,9 @@ build-backend = "hatchling.build"
2121

2222
[tool.hatch.build.targets.wheel]
2323
packages = ["oracle"]
24+
25+
[dependency-groups]
26+
dev = [
27+
"pytest>=8.4.2",
28+
"pytest-asyncio>=1.2.0",
29+
]

src/oci-network-load-balancer-mcp-server/uv.lock

Lines changed: 67 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)