Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop v2 for config_client #183

Merged
merged 2 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
282 changes: 255 additions & 27 deletions README.md

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion test/client_v2_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import asyncio
import time
import unittest

from v2.nacos.common.client_config import GRPCConfig
Expand Down
158 changes: 158 additions & 0 deletions test/config_client_v2_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import asyncio
import unittest

from v2.nacos.common.client_config import GRPCConfig
from v2.nacos.common.client_config_builder import ClientConfigBuilder
from v2.nacos.config.model.config_param import ConfigParam, Listener
from v2.nacos.config.nacos_config_service import NacosConfigService

client_config = (ClientConfigBuilder()
.username('xxx')
.password('xxx')
.server_address('xxx')
.grpc_config(GRPCConfig())
.cache_dir('xxx')
.log_dir('xxx')
.build())

def generate_random_string(length=4):
import random
import string
letters = string.ascii_letters + string.digits
return ''.join(random.choices(letters, k=length))


class TestListener(Listener):
def listen(self, namespace: str, group: str, data_id: str, content: str):
print(namespace, group, data_id, content, "listener")


class TestClientV2(unittest.IsolatedAsyncioTestCase):
async def test_publish_config(self):
client = await NacosConfigService.create_config_service(client_config)
try:
await asyncio.sleep(5)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

仅保留一个测试类,里面的根据不同测试例子方法名区分开,最好能备注上注释

response = await client.publish_config(
param=ConfigParam(data_id="testttt", group="group", content="content", src_user='nacos'))
self.assertEqual(response, True)
if response:
print("success to publish")
await asyncio.sleep(1000)
except Exception as e:
print(str(e))

async def test_get_config(self):
client = await NacosConfigService.create_config_service(client_config)
try:
await asyncio.sleep(5)
response = await client.get_config(
param=ConfigParam(data_id="testttt", group="group"))
print(response)
# self.assertEqual(response, True)
if response:
print("success to get config")
await asyncio.sleep(1000)
except Exception as e:
print(str(e))

async def test_remove_config(self):
client = await NacosConfigService.create_config_service(client_config)
try:
await asyncio.sleep(5)
response = await client.remove_config(
param=ConfigParam(data_id="testttt", group="group"))
if response:
print("success to remove config")
await asyncio.sleep(1000)
except Exception as e:
print(str(e))

async def test_add_listener(self):
client = await NacosConfigService.create_config_service(client_config)
try:
a = generate_random_string()
b = generate_random_string()

await asyncio.sleep(5)
response = await client.publish_config(
param=ConfigParam(data_id="testttt", group="group", content="1{}".format(a), src_user='nacos'))
if response:
print("publish1")

await asyncio.sleep(5)

listener = TestListener()
response = await client.add_listener(
param=ConfigParam(data_id="testttt", group="group"), listener=listener)

print("add listen")
await asyncio.sleep(5)

response = await client.publish_config(
param=ConfigParam(data_id="testttt", group="group", content="3{}".format(b), src_user='nacos'))
if response:
print("publish2")
await asyncio.sleep(1000)
except Exception as e:
print(str(e))

async def test_cipher_config(self):
client = await NacosConfigService.create_config_service(client_config)
try:

# await asyncio.sleep(5)
response = await client.publish_config(
param=ConfigParam(data_id="cipher-aes-testttt", group="group", content="你好nacos", src_user='nacos'))
if response:
print("publish1")

await asyncio.sleep(5)

response = await client.get_config(
param=ConfigParam(data_id="cipher-aes-testttt", group="group"))
print(response)

await asyncio.sleep(1000)
except Exception as e:
print(str(e))

async def test_cipher_listener(self):
client = await NacosConfigService.create_config_service(client_config)
try:
a = generate_random_string()
b = generate_random_string()

await asyncio.sleep(5)
response = await client.publish_config(
param=ConfigParam(data_id="cipher-aes-testttt", group="group", content="1{}".format(a)))
print("publish1")

await asyncio.sleep(5)

listener = TestListener()
response = await client.add_listener(
param=ConfigParam(data_id="cipher-aes-testttt", group="group"), listener=listener)

print("add listen")
await asyncio.sleep(5)

response = await client.publish_config(
param=ConfigParam(data_id="cipher-aes-testttt", group="group", content="3{}".format(b)))
print("publish2")

# self.assertEqual(response, True)
if response:
print("success to test listen")

await asyncio.sleep(1000)
except Exception as e:
print(str(e))

async def test_remove_listener(self):
client = await NacosConfigService.create_config_service(client_config)
try:
await client.remove_listener(
param=ConfigParam(data_id="testttt", group="group"))
await asyncio.sleep(1000)
except Exception as e:
print(str(e))
12 changes: 7 additions & 5 deletions v2/nacos/common/client_config.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import logging
import os

from v2.nacos.common.constants import Constants
from v2.nacos.common.nacos_exception import NacosException, INVALID_PARAM


class KMSConfig:
def __init__(self, enabled=False, appointed=False, ak='', sk='',
region_id='', endpoint=''):
region_id='', endpoint='', client_key_content='', password='', kms_version=''):
self.enabled = enabled # 是否启用kms
self.appointed = appointed # 指明是否使用预设的配置
self.ak = ak # 阿里云账号的AccessKey
self.sk = sk # 阿里云账号的SecretKey
self.region_id = region_id # 阿里云账号的区域
self.endpoint = endpoint # kms服务的地址
self.client_key_content = client_key_content
self.password = password


class TLSConfig:
Expand Down Expand Up @@ -44,8 +45,8 @@ def __init__(self, max_receive_message_length=Constants.GRPC_MAX_RECEIVE_MESSAGE


class ClientConfig:
def __init__(self, server_addresses=None, endpoint=None, namespace_id='public', context_path='', access_key=None,
secret_key=None, username=None, password=None, app_name='', log_dir='', log_level=None,
def __init__(self, server_addresses=None, endpoint=None, namespace_id='', context_path='', access_key=None,
secret_key=None, username=None, password=None, app_name='', app_key='', log_dir='', log_level=None,
log_rotation_backup_count=None):
self.server_list = []
try:
Expand All @@ -64,8 +65,9 @@ def __init__(self, server_addresses=None, endpoint=None, namespace_id='public',
self.username = username # the username for nacos auth
self.password = password # the password for nacos auth
self.app_name = app_name

self.app_key = app_key
self.cache_dir = ''
self.disable_use_snap_shot = False
self.log_dir = log_dir
self.log_level = logging.INFO if log_level is None else log_level # the log level for nacos client, default value is logging.INFO: log_level
self.log_rotation_backup_count = 7 if log_rotation_backup_count is None else log_rotation_backup_count
Expand Down
2 changes: 1 addition & 1 deletion v2/nacos/common/client_config_builder.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from v2.nacos.common.client_config import ClientConfig, GRPCConfig
from v2.nacos.common.client_config import TLSConfig
from v2.nacos.common.client_config import KMSConfig
from v2.nacos.common.client_config import TLSConfig


class ClientConfigBuilder:
Expand Down
28 changes: 28 additions & 0 deletions v2/nacos/common/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ class Constants:

CLIENT_APPNAME_HEADER = "Client-AppName"

APPNAME_HEADER = "AppName"

CLIENT_REQUEST_TS_HEADER = "Client-RequestTS"

CLIENT_REQUEST_TOKEN_HEADER = "Client-RequestToken"
Expand All @@ -152,8 +154,14 @@ class Constants:

WRITE_REDIRECT_CODE = 307

RESPONSE_SUCCESS_CODE = 200

REQUEST_DOMAIN_RETRY_TIME = 3

SERVICE_INFO_SPLITER = "@@"

CONFIG_INFO_SPLITER = "@@"

SERVICE_INFO_SPLIT_COUNT = 2

NULL_STRING = "null"
Expand Down Expand Up @@ -184,10 +192,14 @@ class Constants:

CHARSET_KEY = "charset"

EX_CONFIG_INFO = "exConfigInfo"

GROUP_NAME_KEY = "groupName"

SERVICE_NAME_KEY = "serviceName"

CIPHER_PRE_FIX = "cipher-"

DEFAULT_PORT = 8848

GRPC_MAX_RECEIVE_MESSAGE_LENGTH = 100 * 1024 * 1024
Expand All @@ -200,4 +212,20 @@ class Constants:

KEEP_ALIVE_TIME_MILLS = 5000

INTERNAL_TIME_MILLS = 3000

DEFAULT_GRPC_TIMEOUT_MILLS = 3000

DEFAULT_TIMEOUT_MILLS = 10000

ALL_SYNC_INTERNAL = 5 * 60

PER_TASK_CONFIG_SIZE = 3000

EXECUTOR_ERR_DELAY = 5

DEFAULT_KEY_ID = "alias/acs/mse"

KMS_AES_128_ALGORITHM_NAME = "AES_128"

KMS_AES_256_ALGORITHM_NAME = "AES_256"
Loading