|
5 | 5 | """ |
6 | 6 |
|
7 | 7 | import importlib.metadata |
| 8 | +import json |
8 | 9 | import subprocess |
9 | 10 | from unittest.mock import ANY, MagicMock, patch |
10 | 11 |
|
@@ -72,47 +73,77 @@ async def test_get_oci_command_help_failure(self, mock_run): |
72 | 73 |
|
73 | 74 | @pytest.mark.asyncio |
74 | 75 | @patch("oracle.oci_api_mcp_server.server.subprocess.run") |
75 | | - @patch("oracle.oci_api_mcp_server.server.json.loads") |
76 | | - async def test_run_oci_command_success(self, mock_json_loads, mock_run): |
| 76 | + async def test_run_oci_command_success(self, mock_run): |
| 77 | + command = "compute instance list" |
| 78 | + |
77 | 79 | mock_result = MagicMock() |
78 | 80 | mock_result.stdout = '{"key": "value"}' |
79 | 81 | mock_result.stderr = "" |
| 82 | + mock_result.returncode = 0 |
80 | 83 | mock_run.return_value = mock_result |
81 | | - mock_json_loads.return_value = {"key": "value"} |
82 | 84 |
|
83 | 85 | async with Client(mcp) as client: |
84 | 86 | result = ( |
85 | | - await client.call_tool( |
86 | | - "run_oci_command", {"command": "compute instance list"} |
87 | | - ) |
| 87 | + await client.call_tool("run_oci_command", {"command": command}) |
| 88 | + ).data |
| 89 | + |
| 90 | + assert result == { |
| 91 | + "command": command, |
| 92 | + "output": json.loads(mock_result.stdout), |
| 93 | + "error": mock_result.stderr, |
| 94 | + "returncode": mock_result.returncode, |
| 95 | + } |
| 96 | + |
| 97 | + @pytest.mark.asyncio |
| 98 | + @patch("oracle.oci_api_mcp_server.server.subprocess.run") |
| 99 | + async def test_run_oci_command_string_success(self, mock_run): |
| 100 | + command = "compute instance list" |
| 101 | + |
| 102 | + mock_result = MagicMock() |
| 103 | + mock_result.stdout = "This is not JSON" |
| 104 | + mock_result.stderr = "" |
| 105 | + mock_result.returncode = 0 |
| 106 | + mock_run.return_value = mock_result |
| 107 | + |
| 108 | + async with Client(mcp) as client: |
| 109 | + result = ( |
| 110 | + await client.call_tool("run_oci_command", {"command": command}) |
88 | 111 | ).data |
89 | 112 |
|
90 | | - assert result == {"key": "value"} |
91 | | - mock_json_loads.assert_called_once_with('{"key": "value"}') |
| 113 | + assert result == { |
| 114 | + "command": command, |
| 115 | + "output": mock_result.stdout, |
| 116 | + "error": mock_result.stderr, |
| 117 | + "returncode": mock_result.returncode, |
| 118 | + } |
92 | 119 |
|
93 | 120 | @pytest.mark.asyncio |
94 | 121 | @patch("oracle.oci_api_mcp_server.server.subprocess.run") |
95 | 122 | async def test_run_oci_command_failure(self, mock_run): |
| 123 | + command = "compute instance list" |
| 124 | + |
96 | 125 | mock_result = MagicMock() |
97 | 126 | mock_result.stdout = "Some output" |
98 | 127 | mock_result.stderr = "Some error" |
| 128 | + mock_result.returncode = 1 |
| 129 | + |
99 | 130 | mock_run.side_effect = subprocess.CalledProcessError( |
100 | | - returncode=1, |
101 | | - cmd=["oci", "compute", "instance", "list"], |
| 131 | + returncode=mock_result.returncode, |
| 132 | + cmd=["oci"] + command.split(), |
102 | 133 | output=mock_result.stdout, |
103 | 134 | stderr=mock_result.stderr, |
104 | 135 | ) |
105 | 136 |
|
106 | 137 | async with Client(mcp) as client: |
107 | 138 | result = ( |
108 | | - await client.call_tool( |
109 | | - "run_oci_command", {"command": "compute instance list"} |
110 | | - ) |
| 139 | + await client.call_tool("run_oci_command", {"command": command}) |
111 | 140 | ).data |
112 | 141 |
|
113 | 142 | assert result == { |
114 | | - "error": "Some error", |
115 | | - "output": "Some output", |
| 143 | + "command": command, |
| 144 | + "output": mock_result.stdout, |
| 145 | + "error": mock_result.stderr, |
| 146 | + "returncode": mock_result.returncode, |
116 | 147 | } |
117 | 148 |
|
118 | 149 | @pytest.mark.asyncio |
|
0 commit comments