-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathclient_v2_test.py
More file actions
282 lines (228 loc) · 11.9 KB
/
client_v2_test.py
File metadata and controls
282 lines (228 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import asyncio
import os
import unittest
from typing import List
from unittest.mock import AsyncMock
from unittest.mock import AsyncMock, MagicMock
from v2.nacos import ConfigParam
from v2.nacos.common.client_config import GRPCConfig
from v2.nacos.common.client_config_builder import ClientConfigBuilder
from v2.nacos.naming.model.instance import Instance
from v2.nacos.naming.model.naming_param import RegisterInstanceParam, DeregisterInstanceParam, \
BatchRegisterInstanceParam, GetServiceParam, ListServiceParam, SubscribeServiceParam, ListInstanceParam
from v2.nacos.naming.nacos_naming_service import NacosNamingService
from v2.nacos.config.nacos_config_service import NacosConfigService
from v2.nacos.common.auth import CredentialsProvider, Credentials
from v2.nacos.common.client_config import ClientConfig
from v2.nacos.transport.auth_client import AuthClient
client_config = (ClientConfigBuilder()
.access_key(os.getenv('NACOS_ACCESS_KEY'))
.secret_key(os.getenv('NACOS_SECRET_KEY'))
.server_address(os.getenv('NACOS_SERVER_ADDR', 'localhost:8848'))
.log_level('INFO')
.grpc_config(GRPCConfig(grpc_timeout=5000))
.build())
class CustomCredentialsProvider(CredentialsProvider):
def __init__(self, ak="", sk="", token=""):
self.credential = Credentials(ak, sk, token)
def get_credentials(self):
return self.credential
class TestClientV2(unittest.IsolatedAsyncioTestCase):
async def test_init_naming_and_config_service(self):
config_client = await NacosConfigService.create_config_service(client_config)
assert await config_client.server_health()
naming_client = await NacosNamingService.create_naming_service(client_config)
assert await naming_client.server_health()
response = await naming_client.register_instance(
request=RegisterInstanceParam(service_name='nacos.test.1', group_name='DEFAULT_GROUP', ip='1.1.1.1',
port=7001, weight=1.0, cluster_name='c1', metadata={'a': 'b'},
enabled=True,
healthy=True, ephemeral=True))
self.assertEqual(response, True)
print('register instance')
data_id = "com.alibaba.nacos.test.config"
group = "DEFAULT_GROUP"
content = await config_client.get_config(ConfigParam(
data_id=data_id,
group=group,
))
assert content == ""
await asyncio.sleep(10000)
async def test_register_with_endpoint_and_fixed_ak(self):
config = (ClientConfigBuilder()
.access_key(os.getenv('NACOS_ACCESS_KEY'))
.secret_key(os.getenv('NACOS_SECRET_KEY'))
.endpoint(os.getenv('NACOS_SERVER_ENDPOINT', 'localhost:8848'))
.log_level('INFO')
.grpc_config(GRPCConfig(grpc_timeout=5000))
.build())
client = await NacosNamingService.create_naming_service(config)
assert await client.server_health()
async def test_register_with_endpoint_and_provider(self):
config = (ClientConfigBuilder()
.credentials_provider(CustomCredentialsProvider(os.getenv('NACOS_ACCESS_KEY'), os.getenv('NACOS_SECRET_KEY')))
.endpoint(os.getenv('NACOS_SERVER_ENDPOINT', 'localhost:8848'))
.log_level('INFO')
.grpc_config(GRPCConfig(grpc_timeout=5000))
.build())
client = await NacosNamingService.create_naming_service(config)
assert await client.server_health()
async def test_register(self):
client = await NacosNamingService.create_naming_service(client_config)
assert await client.server_health()
async def cb(instance_list: List[Instance]):
print('received subscribe callback', str(instance_list))
await client.subscribe(
SubscribeServiceParam(service_name='nacos.test.1', group_name='DEFAULT_GROUP', subscribe_callback=cb))
print('subscribe service')
response = await client.register_instance(
request=RegisterInstanceParam(service_name='nacos.test.1', group_name='DEFAULT_GROUP', ip='1.1.1.1',
port=7001, weight=1.0, cluster_name='c1', metadata={'a': 'b'},
enabled=True,
healthy=True, ephemeral=True))
self.assertEqual(response, True)
print('register instance')
await asyncio.sleep(1)
response = await client.update_instance(
request=RegisterInstanceParam(service_name='nacos.test.1', group_name='DEFAULT_GROUP', ip='1.1.1.1',
port=7001, weight=2.0, cluster_name='c1', metadata={'a': 'b'},
enabled=True,
healthy=True, ephemeral=True))
self.assertEqual(response, True)
print('update instance')
await asyncio.sleep(1)
response = await client.deregister_instance(
request=DeregisterInstanceParam(service_name='nacos.test.1', group_name='DEFAULT_GROUP', ip='1.1.1.1',
port=7001, cluster_name='c1', ephemeral=True)
)
self.assertEqual(response, True)
print('deregister instance')
await asyncio.sleep(1)
param1 = RegisterInstanceParam(service_name='nacos.test.1',
group_name='DEFAULT_GROUP',
ip='1.1.1.1',
port=7001,
weight=1.0,
cluster_name='c1',
metadata={'a': 'b'},
enabled=True,
healthy=True,
ephemeral=True
)
param2 = RegisterInstanceParam(service_name='nacos.test.1',
group_name='DEFAULT_GROUP',
ip='1.1.1.1',
port=7002,
weight=1.0,
cluster_name='c1',
metadata={'a': 'b'},
enabled=True,
healthy=True,
ephemeral=True
)
param3 = RegisterInstanceParam(service_name='nacos.test.1',
group_name='DEFAULT_GROUP',
ip='1.1.1.1',
port=7003,
weight=1.0,
cluster_name='c1',
metadata={'a': 'b'},
enabled=True,
healthy=False,
ephemeral=True
)
response = await client.batch_register_instances(
request=BatchRegisterInstanceParam(service_name='nacos.test.1', group_name='DEFAULT_GROUP',
instances=[param1, param2, param3]))
self.assertEqual(response, True)
print('batch register instance')
await asyncio.sleep(1)
service = await client.get_service(
GetServiceParam(service_name='nacos.test.1', group_name='DEFAULT_GROUP', cluster_name='c1'))
print('get service', str(service))
assert service.name == 'nacos.test.1'
service_list = await client.list_services(ListServiceParam(
namespace_id="public",
group_name='DEFAULT_GROUP',
))
assert service_list.count == 1
instance_list = await client.list_instances(ListInstanceParam(service_name='nacos.test.1', healthy_only=True))
assert len(instance_list) == 2
instance_list = await client.list_instances(ListInstanceParam(service_name='nacos.test.1', healthy_only=False))
assert len(instance_list) == 1
instance_list = await client.list_instances(ListInstanceParam(service_name='nacos.test.1', healthy_only=None))
assert len(instance_list) == 3
await client.unsubscribe(
SubscribeServiceParam(service_name='nacos.test.1', group_name='DEFAULT_GROUP', subscribe_callback=cb))
await client.shutdown()
async def test_auth_login_url_with_root_context_path(self):
"""
[Issue #300] Verifies that when context_path is '/', the login URL
is correctly formed as '/v1/auth/users/login' without double slashes.
"""
# 1. Setup config with root context path
config = ClientConfig(
server_addresses="http://127.0.0.1:8848",
username="nacos",
password="nacos",
context_path="/"
)
# 2. Create required mock objects
mock_get_server_list = MagicMock(return_value=["http://127.0.0.1:8848"])
mock_http_agent = MagicMock()
mock_logger = MagicMock() # Mock logger
# 3. Initialize AuthClient with ALL required arguments
client = AuthClient(
client_config=config,
get_server_list_func=mock_get_server_list,
http_agent=mock_http_agent,
logger=mock_logger
)
# 4. Mock the HTTP request to capture the URL
mock_response = (b'{"accessToken":"mock-token", "tokenTtl":18000}', None)
mock_request = AsyncMock(return_value=mock_response)
mock_http_agent.request = mock_request
# 5. Execute login logic
await client.get_access_token(force_refresh=True)
# 6. Assert the generated URL
called_url = mock_request.call_args[0][0]
expected_url = "http://127.0.0.1:8848/v1/auth/users/login"
self.assertEqual(called_url, expected_url,
f"URL mismatch for root context_path. Expected '{expected_url}', but got '{called_url}'")
async def test_auth_login_url_with_standard_context_path(self):
"""
[Regression Test] Verifies that when context_path is '/nacos',
the login URL correctly includes the prefix.
"""
import logging
# 1. Setup config with standard context path
config = ClientConfig(
server_addresses="http://127.0.0.1:8848",
username="nacos",
password="nacos",
context_path="/nacos"
)
# 2. Create required mock objects
mock_get_server_list = MagicMock(return_value=["http://127.0.0.1:8848"])
mock_http_agent = MagicMock()
mock_logger = MagicMock() # Mock logger
# 3. Initialize AuthClient with ALL required arguments
client = AuthClient(
client_config=config,
get_server_list_func=mock_get_server_list,
http_agent=mock_http_agent,
logger=mock_logger
)
# 4. Mock the HTTP request
mock_response = (b'{"accessToken":"mock-token", "tokenTtl":18000}', None)
mock_request = AsyncMock(return_value=mock_response)
mock_http_agent.request = mock_request
# 5. Execute login logic
await client.get_access_token(force_refresh=True)
# 6. Assert the generated URL
called_url = mock_request.call_args[0][0]
expected_url = "http://127.0.0.1:8848/nacos/v1/auth/users/login"
self.assertEqual(called_url, expected_url,
f"URL mismatch for standard context_path. Expected '{expected_url}', but got '{called_url}'")
if __name__ == '__main__':
unittest.main()