From 4d2332f1e3e9f0610fe7e49b43d937a708d61faf Mon Sep 17 00:00:00 2001 From: "Xinwei Xiong(cubxxw)" <3293172751nss@gmail.com> Date: Tue, 7 Nov 2023 03:31:16 +0800 Subject: [PATCH] feat: add api test Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com> --- .github/workflows/openimci.yml | 55 +- docs/contrib/environment.md | 4 +- install.sh | 15 +- scripts/genconfig.sh | 3 + scripts/install/dependency.sh | 6 +- scripts/install/environment.sh | 8 +- scripts/install/install.sh | 9 +- scripts/install/openim-api.sh | 3 +- scripts/install/openim-crontask.sh | 12 +- scripts/install/openim-msggateway.sh | 12 +- scripts/install/openim-msgtransfer.sh | 15 +- scripts/install/openim-push.sh | 12 +- scripts/install/openim-rpc.sh | 9 +- scripts/install/test.sh | 1375 ++++++++++++++++++++----- scripts/lib/color.sh | 3 +- scripts/lib/logging.sh | 3 +- scripts/lib/util.sh | 15 + scripts/wait-for-it.sh | 9 +- test/wrktest.sh | 12 +- 19 files changed, 1254 insertions(+), 326 deletions(-) diff --git a/.github/workflows/openimci.yml b/.github/workflows/openimci.yml index 05a8ce5c5..abd204898 100644 --- a/.github/workflows/openimci.yml +++ b/.github/workflows/openimci.yml @@ -160,18 +160,55 @@ jobs: sudo make tidy sudo make tools.verify.go-gitlint - - name: Build, Start and Check Services + - name: Build, Start, Check Services and Print Logs run: | - sudo make init - sudo make build - sudo make start - sudo make check + sudo make init && \ + sudo make build && \ + sudo make start && \ + sudo make check || \ + (echo "An error occurred, printing logs:" && sudo cat ./_output/logs/* 2>/dev/null) - - name: Print OpenIM Logs - run: sudo cat ./_output/logs/* 2>/dev/null - continue-on-error: true + execute-linux-systemd-scripts: + name: Execute OpenIM script on ${{ matrix.os }} + runs-on: ${{ matrix.os }} + environment: + name: openim + strategy: + matrix: + go_version: ["1.20"] + os: ["ubuntu-latest"] + steps: + - name: Checkout code + uses: actions/checkout@v4 - openim-build-image: + - name: Set up Go ${{ matrix.go_version }} + uses: actions/setup-go@v4 + with: + go-version: ${{ matrix.go_version }} + id: go + - name: Install Task + uses: arduino/setup-task@v1 + with: + version: 2.x + - name: Docker Operations + run: | + curl -o docker-compose.yml https://raw.githubusercontent.com/OpenIMSDK/openim-docker/main/example/basic-openim-server-dependency.yml + sudo docker compose up -d + sudo sleep 60 + + - name: Module Operations + run: | + sudo make tidy + sudo make tools.verify.go-gitlint + + - name: Build, Start, Check Services and Print Logs + run: | + sudo ./scripts/install/install.sh -i && \ + sudo ./scripts/install/install.sh -s && \ + sudo ./scripts/install/install.sh -u || \ + (echo "An error occurred, printing logs:" && sudo cat ./_output/logs/* 2>/dev/null) + + openim-test-build-image: name: Build OpenIM Docker Image runs-on: ubuntu-latest environment: diff --git a/docs/contrib/environment.md b/docs/contrib/environment.md index 6efddafd3..20939f213 100644 --- a/docs/contrib/environment.md +++ b/docs/contrib/environment.md @@ -136,13 +136,13 @@ For convenience, configuration through modifying environment variables is recomm export PASSWORD="openIM123" ``` -+ USER ++ OPENIM_USER + **Description**: Username for mysql, mongodb, redis, and minio. + **Default**: `root` ```bash - export USER="root" + export OPENIM_USER="root" ``` + API_URL diff --git a/install.sh b/install.sh index 9809646fa..9318c33ba 100755 --- a/install.sh +++ b/install.sh @@ -458,25 +458,20 @@ function openim_color() { } # --- helper functions for logs --- -info() -{ +info() { echo -e "[${GREEN_PREFIX}INFO${COLOR_SUFFIX}] " "$@" } -warn() -{ +warn() { echo -e "[${YELLOW_PREFIX}WARN${COLOR_SUFFIX}] " "$@" >&2 } -fatal() -{ +fatal() { echo -e "[${RED_PREFIX}ERROR${COLOR_SUFFIX}] " "$@" >&2 exit 1 } -debug() -{ +debug() { echo -e "[${BLUE_PREFIX}DEBUG${COLOR_SUFFIX}]===> " "$@" } -success() -{ +success() { echo -e "${BRIGHT_GREEN_PREFIX}=== [SUCCESS] ===${COLOR_SUFFIX}\n=> " "$@" } diff --git a/scripts/genconfig.sh b/scripts/genconfig.sh index 030c31faf..2d3e6313d 100755 --- a/scripts/genconfig.sh +++ b/scripts/genconfig.sh @@ -29,6 +29,9 @@ if [ $# -ne 2 ];then exit 1 fi +# Check if the required commands exist +openim::util::require-dig + source "${env_file}" declare -A envs diff --git a/scripts/install/dependency.sh b/scripts/install/dependency.sh index 4a03c6563..7d6685186 100755 --- a/scripts/install/dependency.sh +++ b/scripts/install/dependency.sh @@ -43,10 +43,10 @@ docker run -d \ -v "./scripts/mongo-init.sh:/docker-entrypoint-initdb.d/mongo-init.sh:ro" \ -e TZ=Asia/Shanghai \ -e wiredTigerCacheSizeGB=1 \ - -e MONGO_INITDB_ROOT_USERNAME=${USER} \ + -e MONGO_INITDB_ROOT_USERNAME=${OPENIM_USER} \ -e MONGO_INITDB_ROOT_PASSWORD=${PASSWORD} \ -e MONGO_INITDB_DATABASE=openIM \ - -e MONGO_USERNAME=${USER} \ + -e MONGO_USERNAME=${OPENIM_USER} \ -e MONGO_PASSWORD=${PASSWORD} \ --restart always \ mongo:6.0.2 --wiredTigerCacheSizeGB 1 --auth @@ -94,7 +94,7 @@ docker run -d \ -p 9090:9090 \ -v "/mnt/data:/data" \ -v "/mnt/config:/root/.minio" \ - -e MINIO_ROOT_USER=${USER} \ + -e MINIO_ROOT_USER=${OPENIM_USER} \ -e MINIO_ROOT_PASSWORD=${PASSWORD} \ --restart always \ minio/minio server /data --console-address ':9090' diff --git a/scripts/install/environment.sh b/scripts/install/environment.sh index 09fb94e7d..01e531f9f 100755 --- a/scripts/install/environment.sh +++ b/scripts/install/environment.sh @@ -46,7 +46,7 @@ function def() { def "DATA_DIR" "${OPENIM_ROOT}" # 设置统一的用户名,方便记忆 -def "USER" "root" +def "OPENIM_USER" "root" # 设置统一的密码,方便记忆 readonly PASSWORD=${PASSWORD:-'openIM123'} @@ -166,7 +166,7 @@ def "ZOOKEEPER_PASSWORD" "" # Zookeeper的密码 ###################### MySQL 配置信息 ###################### def "MYSQL_PORT" "13306" # MySQL的端口 def "MYSQL_ADDRESS" "${DOCKER_BRIDGE_GATEWAY}" # MySQL的地址 -def "MYSQL_USERNAME" "${USER}" # MySQL的用户名 +def "MYSQL_USERNAME" "${OPENIM_USER}" # MySQL的用户名 # MySQL的密码 readonly MYSQL_PASSWORD=${MYSQL_PASSWORD:-"${PASSWORD}"} def "MYSQL_DATABASE" "${DATABASE_NAME}" # MySQL的数据库名 @@ -181,7 +181,7 @@ def "MONGO_URI" # MongoDB的URI def "MONGO_PORT" "37017" # MongoDB的端口 def "MONGO_ADDRESS" "${DOCKER_BRIDGE_GATEWAY}" # MongoDB的地址 def "MONGO_DATABASE" "${DATABASE_NAME}" # MongoDB的数据库名 -def "MONGO_USERNAME" "${USER}" # MongoDB的用户名 +def "MONGO_USERNAME" "${OPENIM_USER}" # MongoDB的用户名 # MongoDB的密码 readonly MONGO_PASSWORD=${MONGO_PASSWORD:-"${PASSWORD}"} def "MONGO_MAX_POOL_SIZE" "100" # 最大连接池大小 @@ -198,7 +198,7 @@ def "MINIO_PORT" "10005" # MinIO的端口 # MinIO的端点URL def MINIO_ADDRESS "${DOCKER_BRIDGE_GATEWAY}" readonly MINIO_ENDPOINT=${MINIO_ENDPOINT:-"http://${MINIO_ADDRESS}:${MINIO_PORT}"} -def "MINIO_ACCESS_KEY" "${USER}" # MinIO的访问密钥ID +def "MINIO_ACCESS_KEY" "${OPENIM_USER}" # MinIO的访问密钥ID readonly MINIO_SECRET_KEY=${MINIO_SECRET_KEY:-"${PASSWORD}"} def "MINIO_SESSION_TOKEN" # MinIO的会话令牌 readonly MINIO_SIGN_ENDPOINT=${MINIO_SIGN_ENDPOINT:-"http://${OPENIM_IP}:${MINIO_PORT}"} # signEndpoint为minio公网地址 diff --git a/scripts/install/install.sh b/scripts/install/install.sh index 915eef3f6..afa07df6d 100755 --- a/scripts/install/install.sh +++ b/scripts/install/install.sh @@ -71,8 +71,7 @@ function openim::install::show_help() { echo " $0 --install Same as above." } -function openim::install::install_openim() -{ +function openim::install::install_openim() { openim::common::sudo "mkdir -p ${OPENIM_DATA_DIR} ${OPENIM_INSTALL_DIR} ${OPENIM_CONFIG_DIR} ${OPENIM_LOG_DIR}" openim::log::info "check openim dependency" openim::common::sudo "cp -r ${OPENIM_ROOT}/config/* ${OPENIM_CONFIG_DIR}/" @@ -96,8 +95,7 @@ function openim::install::install_openim() openim::log::success "openim install success" } -function openim::uninstall::uninstall_openim() -{ +function openim::uninstall::uninstall_openim() { openim::log::info "uninstall openim" ${OPENIM_ROOT}/scripts/install/openim-msggateway.sh openim::msggateway::uninstall || return 1 @@ -115,8 +113,7 @@ function openim::uninstall::uninstall_openim() openim::log::success "openim uninstall success" } -function openim::install::status() -{ +function openim::install::status() { openim::log::info "check openim status" ${OPENIM_ROOT}/scripts/install/openim-msggateway.sh openim::msggateway::status || return 1 diff --git a/scripts/install/openim-api.sh b/scripts/install/openim-api.sh index a2d40b777..a40e23611 100755 --- a/scripts/install/openim-api.sh +++ b/scripts/install/openim-api.sh @@ -33,8 +33,7 @@ readonly OPENIM_API_SERVICE_TARGETS=( ) readonly OPENIM_API_SERVICE_LISTARIES=("${OPENIM_API_SERVICE_TARGETS[@]##*/}") -function openim::api::start() -{ +function openim::api::start() { echo "++ OPENIM_API_SERVICE_LISTARIES: ${OPENIM_API_SERVICE_LISTARIES[@]}" echo "++ OPENIM_API_PORT_LISTARIES: ${OPENIM_API_PORT_LISTARIES[@]}" echo "++ OpenIM API config path: ${OPENIM_API_CONFIG}" diff --git a/scripts/install/openim-crontask.sh b/scripts/install/openim-crontask.sh index 49e018ad1..26dc1a47f 100755 --- a/scripts/install/openim-crontask.sh +++ b/scripts/install/openim-crontask.sh @@ -43,8 +43,7 @@ OPENIM_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")"/../.. && pwd -P) SERVER_NAME="openim-crontask" -function openim::crontask::start() -{ +function openim::crontask::start() { openim::log::info "Start OpenIM Cron, binary root: ${SERVER_NAME}" openim::log::status "Start OpenIM Cron, path: ${OPENIM_CRONTASK_BINARY}" @@ -66,8 +65,7 @@ EOF } # install openim-crontask -function openim::crontask::install() -{ +function openim::crontask::install() { pushd "${OPENIM_ROOT}" # 1. Build openim-crontask @@ -97,8 +95,7 @@ function openim::crontask::install() # Unload -function openim::crontask::uninstall() -{ +function openim::crontask::uninstall() { set +o errexit openim::common::sudo "systemctl stop ${SERVER_NAME}" openim::common::sudo "systemctl disable ${SERVER_NAME}" @@ -110,8 +107,7 @@ function openim::crontask::uninstall() } # Status Check -function openim::crontask::status() -{ +function openim::crontask::status() { # Check the running status of the ${SERVER_NAME}. If active (running) is displayed, the ${SERVER_NAME} is started successfully. if systemctl is-active --quiet "${SERVER_NAME}"; then openim::log::info "${SERVER_NAME} is running successfully." diff --git a/scripts/install/openim-msggateway.sh b/scripts/install/openim-msggateway.sh index 79aa6fa15..af3d87b23 100755 --- a/scripts/install/openim-msggateway.sh +++ b/scripts/install/openim-msggateway.sh @@ -25,8 +25,7 @@ openim::util::set_max_fd 200000 SERVER_NAME="openim-msggateway" -function openim::msggateway::start() -{ +function openim::msggateway::start() { openim::log::info "Start OpenIM Msggateway, binary root: ${SERVER_NAME}" openim::log::status "Start OpenIM Msggateway, path: ${OPENIM_MSGGATEWAY_BINARY}" @@ -79,8 +78,7 @@ EOF } # install openim-msggateway -function openim::msggateway::install() -{ +function openim::msggateway::install() { pushd "${OPENIM_ROOT}" # 1. Build openim-msggateway @@ -109,8 +107,7 @@ function openim::msggateway::install() # Unload -function openim::msggateway::uninstall() -{ +function openim::msggateway::uninstall() { set +o errexit openim::common::sudo "systemctl stop ${SERVER_NAME}" openim::common::sudo "systemctl disable ${SERVER_NAME}" @@ -122,8 +119,7 @@ function openim::msggateway::uninstall() } # Status Check -function openim::msggateway::status() -{ +function openim::msggateway::status() { # Check the running status of the ${SERVER_NAME}. If active (running) is displayed, the ${SERVER_NAME} is started successfully. systemctl status ${SERVER_NAME}|grep -q 'active' || { openim::log::error "${SERVER_NAME} failed to start, maybe not installed properly" diff --git a/scripts/install/openim-msgtransfer.sh b/scripts/install/openim-msgtransfer.sh index 08a7d3ec7..18bbb3c02 100755 --- a/scripts/install/openim-msgtransfer.sh +++ b/scripts/install/openim-msgtransfer.sh @@ -25,8 +25,7 @@ openim::util::set_max_fd 200000 SERVER_NAME="openim-msgtransfer" -function openim::msgtransfer::start() -{ +function openim::msgtransfer::start() { openim::log::info "Start OpenIM Msggateway, binary root: ${SERVER_NAME}" openim::log::status "Start OpenIM Msggateway, path: ${OPENIM_MSGTRANSFER_BINARY}" @@ -61,8 +60,7 @@ function openim::msgtransfer::start() openim::util::check_process_names "${OPENIM_OUTPUT_HOSTBIN}/${SERVER_NAME}" } -function openim::msgtransfer::check() -{ +function openim::msgtransfer::check() { PIDS=$(pgrep -f "${OPENIM_OUTPUT_HOSTBIN}/openim-msgtransfer") NUM_PROCESSES=$(echo "$PIDS" | wc -l) @@ -89,8 +87,7 @@ EOF } # install openim-msgtransfer -function openim::msgtransfer::install() -{ +function openim::msgtransfer::install() { pushd "${OPENIM_ROOT}" # 1. Build openim-msgtransfer @@ -122,8 +119,7 @@ function openim::msgtransfer::install() # Unload -function openim::msgtransfer::uninstall() -{ +function openim::msgtransfer::uninstall() { set +o errexit openim::common::sudo "systemctl stop ${SERVER_NAME}" openim::common::sudo "systemctl disable ${SERVER_NAME}" @@ -135,8 +131,7 @@ function openim::msgtransfer::uninstall() } # Status Check -function openim::msgtransfer::status() -{ +function openim::msgtransfer::status() { # Check the running status of the ${SERVER_NAME}. If active (running) is displayed, the ${SERVER_NAME} is started successfully. if systemctl is-active --quiet "${SERVER_NAME}"; then openim::log::info "${SERVER_NAME} is running successfully." diff --git a/scripts/install/openim-push.sh b/scripts/install/openim-push.sh index 5197c9948..c17b80e67 100755 --- a/scripts/install/openim-push.sh +++ b/scripts/install/openim-push.sh @@ -49,8 +49,7 @@ OPENIM_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")"/../.. && pwd -P) SERVER_NAME="openim-push" -function openim::push::start() -{ +function openim::push::start() { openim::log::status "Start OpenIM Push, binary root: ${SERVER_NAME}" openim::log::info "Start OpenIM Push, path: ${OPENIM_PUSH_BINARY}" @@ -88,8 +87,7 @@ EOF } # install openim-push -function openim::push::install() -{ +function openim::push::install() { pushd "${OPENIM_ROOT}" # 1. Build openim-push @@ -117,8 +115,7 @@ function openim::push::install() } # Unload -function openim::push::uninstall() -{ +function openim::push::uninstall() { set +o errexit openim::common::sudo "systemctl stop ${SERVER_NAME}" openim::common::sudo "systemctl disable ${SERVER_NAME}" @@ -130,8 +127,7 @@ function openim::push::uninstall() } # Status Check -function openim::push::status() -{ +function openim::push::status() { # Check the running status of the ${SERVER_NAME}. If active (running) is displayed, the ${SERVER_NAME} is started successfully. systemctl status ${SERVER_NAME}|grep -q 'active' || { openim::log::error "${SERVER_NAME} failed to start, maybe not installed properly" diff --git a/scripts/install/openim-rpc.sh b/scripts/install/openim-rpc.sh index d6000b093..bd00ff9f2 100755 --- a/scripts/install/openim-rpc.sh +++ b/scripts/install/openim-rpc.sh @@ -173,8 +173,7 @@ function openim::rpc::info() { } # install openim-rpc -function openim::rpc::install() -{ +function openim::rpc::install() { pushd "${OPENIM_ROOT}" # 1. Build openim-rpc @@ -208,8 +207,7 @@ function openim::rpc::install() } # Unload -function openim::rpc::uninstall() -{ +function openim::rpc::uninstall() { set +o errexit for service in "${OPENIM_RPC_SERVICE_LISTARIES[@]}"; do openim::common::sudo "systemctl stop ${service}" @@ -223,8 +221,7 @@ function openim::rpc::uninstall() } # Status Check -function openim::rpc::status() -{ +function openim::rpc::status() { for service in "${OPENIM_RPC_SERVICE_LISTARIES[@]}"; do # Check the running status of the ${service}. If active (running) is displayed, the ${service} is started successfully. systemctl status ${service}|grep -q 'active' || { diff --git a/scripts/install/test.sh b/scripts/install/test.sh index f1cfee119..27aedd49c 100755 --- a/scripts/install/test.sh +++ b/scripts/install/test.sh @@ -7,68 +7,1044 @@ IAM_ROOT=$(dirname "${BASH_SOURCE[0]}")/../.. INSECURE_OPENIMAPI=${IAM_APISERVER_HOST}:${API_OPENIM_PORT} INSECURE_OPENIMAUTO=${OPENIM_RPC_AUTH_HOST}:${OPENIM_AUTH_PORT} -Header="-HContent-Type: application/json" CCURL="curl -f -s -XPOST" # Create UCURL="curl -f -s -XPUT" # Update RCURL="curl -f -s -XGET" # Retrieve DCURL="curl -f -s -XDELETE" # Delete -openim::test::user() -{ - token="-HAuthorization: Bearer $(openim::test::login)" +openim::test::check_error() { + local response=$1 + local err_code=$(echo "$response" | jq '.errCode') + openim::log::status "Response from user registration: $response" + if [[ "$err_code" != "0" ]]; then + openim::log::error_exit "Error occurred: $response, You can read the error code in the API documentation https://docs.openim.io/restapi/errcode" + else + openim::log::success "Operation was successful." + fi +} - # 1. If colin, mark, john users exist, clear them first - ${DCURL} "${token}" http://${INSECURE_OPENIMAPI}/v1/users/colin; echo - ${DCURL} "${token}" http://${INSECURE_OPENIMAPI}/v1/users/mark; echo - ${DCURL} "${token}" http://${INSECURE_OPENIMAPI}/v1/users/john; echo - - # 2. Create colin, mark, john users - ${CCURL} "${Header}" "${token}" http://${INSECURE_OPENIMAPI}/v1/users \ - -d'{"password":"User@2021","metadata":{"name":"colin"},"nickname":"colin","email":"colin@foxmail.com","phone":"1812884xxxx"}'; echo - - # 3. List all users - ${RCURL} "${token}" "http://${INSECURE_OPENIMAPI}/v1/users?offset=0&limit=10"; echo - - # 4. Get detailed information of colin user - ${RCURL} "${token}" http://${INSECURE_OPENIMAPI}/v1/users/colin; echo - - # 5. Modify colin user - ${UCURL} "${Header}" "${token}" http://${INSECURE_OPENIMAPI}/v1/users/colin \ - -d'{"nickname":"colin","email":"colin_modified@foxmail.com","phone":"1812884xxxx"}'; echo - - # 6. Delete colin user - ${DCURL} "${token}" http://${INSECURE_OPENIMAPI}/v1/users/colin; echo - - # 7. Batch delete users - ${DCURL} "${token}" "http://${INSECURE_OPENIMAPI}/v1/users?name=mark&name=john"; echo +# The `openim::test::auth` function serves as a test suite for authentication-related operations. +function openim::test::auth() { + # 1. Retrieve and set the authentication token. + openim::test::get_token - openim::log::info "$(echo -e '\033[32mcongratulations, /v1/user test passed!\033[0m')" + # 2. Force logout the test user from a specific platform. + openim::test::force_logout + + # Log the completion of the auth test suite. + openim::log::success "Auth test suite completed successfully." } - # userRouterGroup := r.Group("/user") - # { - # userRouterGroup.POST("/user_register", u.UserRegister) - # userRouterGroup.POST("/update_user_info", ParseToken, u.UpdateUserInfo) - # userRouterGroup.POST("/set_global_msg_recv_opt", ParseToken, u.SetGlobalRecvMessageOpt) - # userRouterGroup.POST("/get_users_info", ParseToken, u.GetUsersPublicInfo) - # userRouterGroup.POST("/get_all_users_uid", ParseToken, u.GetAllUsersID) - # userRouterGroup.POST("/account_check", ParseToken, u.AccountCheck) - # userRouterGroup.POST("/get_users", ParseToken, u.GetUsers) - # userRouterGroup.POST("/get_users_online_status", ParseToken, u.GetUsersOnlineStatus) - # userRouterGroup.POST("/get_users_online_token_detail", ParseToken, u.GetUsersOnlineTokenDetail) - # userRouterGroup.POST("/subscribe_users_status", ParseToken, u.SubscriberStatus) - # userRouterGroup.POST("/get_users_status", ParseToken, u.GetUserStatus) - # userRouterGroup.POST("/get_subscribe_users_status", ParseToken, u.GetSubscribeUsersStatus) - # } -openim::test::group() -{ - token="-HAuthorization: Bearer $(openim::test::login)" +#################################### Auth Module #################################### + +# Define a function to get a token (Admin Token) +openim::test::get_token() { + token_response=$(${CCURL} "${OperationID}" "${Header}" http://${OPENIM_API_HOST}:${API_OPENIM_PORT}/auth/user_token \ + -d'{"secret": "'"$SECRET"'","platformID": 1,"userID": "openIM123456"}') + token=$(echo $token_response | grep -Po 'token[" :]+\K[^"]+') + echo "$token" } +Header="-HContent-Type: application/json" +OperationID="-HoperationID: 1646445464564" +Token="-Htoken: $(openim::test::get_token)" + +# Forces a user to log out from the specified platform by user ID. +openim::test::force_logout() { + local request_body=$(cat <","users:maria","groups:admins"],"actions":["delete",""],"effect":"allow","resources":["resources:articles:<.*>","resources:printer"],"conditions":{"remoteIPAddress":{"type":"CIDRCondition","options":{"cidr":"192.168.0.1/16"}}}}}'; echo - - # 3. 列出所有策略 - ${RCURL} "${token}" http://${INSECURE_OPENIMAPI}/v1/policies; echo - - # 4. 获取 policy0 策略的详细信息 - ${RCURL} "${token}" http://${INSECURE_OPENIMAPI}/v1/policies/policy0; echo - - # 5. 修改 policy0 策略 - ${UCURL} "${Header}" "${token}" http://${INSECURE_OPENIMAPI}/v1/policies/policy0 \ - -d'{"policy":{"description":"One policy to rule them all(modified).","subjects":["users:","users:maria","groups:admins"],"actions":["delete",""],"effect":"allow","resources":["resources:articles:<.*>","resources:printer"],"conditions":{"remoteIPAddress":{"type":"CIDRCondition","options":{"cidr":"192.168.0.1/16"}}}}}'; echo - - # 6. 删除 policy0 策略 - ${DCURL} "${token}" http://${INSECURE_OPENIMAPI}/v1/policies/policy0; echo - openim::log::info "$(echo -e '\033[32mcongratulations, /v1/policy test passed!\033[0m')" + openim::test::check_error "$response" } -openim::test::apiserver() + +# Clears all messages for a user. +openim::test::user_clear_all_msg() { + local userID="${1}" + + local request_body=$(cat <","users:maria","groups:admins"],"actions":["delete",""],"effect":"allow","resources":["resources:articles:<.*>","resources:printer"],"conditions":{"remoteIPAddress":{"type":"CIDRCondition","options":{"cidr":"192.168.0.1/16"}}}}}'; echo + # 2. Clear all messages for a user. + openim::test::user_clear_all_msg "${RECV_USER_ID}" - # 3. 如果有 authzsecret 密钥先清空 - ${DCURL} "${token}" http://${INSECURE_OPENIMAPI}/v1/secrets/authzsecret; echo - - # 4. 创建 authzsecret 密钥 - secret=$(${CCURL} "${Header}" "${token}" http://${INSECURE_OPENIMAPI}/v1/secrets -d'{"metadata":{"name":"authzsecret"},"expires":0,"description":"admin secret"}') - secretID=$(echo ${secret} | grep -Po 'secretID[" :]+\K[^"]+') - secretKey=$(echo ${secret} | grep -Po 'secretKey[" :]+\K[^"]+') - - # 5. 生成 token - token=$(iamctl jwt sign ${secretID} ${secretKey}) - - # 6. 调用 /v1/authz 完成资源授权。 - # 注意这里要 sleep 3s 等待 openim-authz-server 将新建的密钥同步到其内存中 - echo "wait 3s to allow openim-authz-server to sync information into its memory ..." - sleep 3 - ret=`$CCURL "${Header}" -H"Authorization: Bearer ${token}" http://${INSECURE_OPENIMAUTO}/v1/authz \ - -d'{"subject":"users:maria","action":"delete","resource":"resources:articles:ladon-introduction","context":{"remoteIPAddress":"192.168.0.5"}}' | grep -Po 'allowed[" :]+\K\w+'` - - if [ "$ret" != "true" ];then - return 1 - fi - - openim::log::info "$(echo -e '\033[32mcongratulations, /v1/authz test passed!\033[0m')" + # Log the completion of the message test suite. + openim::log::success "Message test suite completed successfully." } -openim::test::authzserver() -{ - openim::test::authz - openim::log::info "$(echo -e '\033[32mcongratulations, openim-authz-server test passed!\033[0m')" +#################################### Man Module #################################### + +# TODO: + +openim::test::man() { + openim::log::info "TODO: openim test man" } -openim::test::pump() -{ - ${RCURL} http://${IAM_PUMP_HOST}:7070/healthz | egrep -q 'status.*ok' || { - openim::log::error "cannot access openim-pump healthz api, openim-pump maybe down" - return 1 - } - openim::test::real_pump_test - - openim::log::info "$(echo -e '\033[32mcongratulations, openim-pump test passed!\033[0m')" -} - -# 使用真实的数据测试 openim-pump 是否正常工作 -openim::test::real_pump_test() -{ - # 1. 创建访问 openim-authz-server 需要用到的密钥对 - iamctl secret create pumptest &>/dev/null - - # 2. 使用步骤 1 创建的密钥对生成 JWT Token - authzAccessToken=`iamctl jwt sign njcho8gJQArsq7zr5v1YpG5NcvL0aeuZ38Ti if70HgRgp021iq5ex2l7pfy5XvgtZM3q` # iamctl jwt sign $secretID $secretKey - - # 3. 创建授权策略 - iamctl policy create pumptest '{"metadata":{"name":"policy0"},"policy":{"description":"One policy to rule them all.","subjects":["users:","users:maria","groups:admins"],"actions":["delete",""],"effect":"allow","resources":["resources:articles:<.*>","resources:printer"],"conditions":{"remoteIPAddress":{"type":"CIDRCondition","options":{"cidr":"192.168.0.1/16"}}}}}' &>/dev/null - - # 注意这里要 sleep 3s 等待 openim-authz-server 将新建的密钥和授权策略同步到其内存中 - echo "wait 3s to allow openim-authz-server to sync information into its memory ..." - sleep 3 - - # 4. 访问 /v1/authz 接口进行资源授权 - $CCURL "${Header}" -H"Authorization: Bearer ${token}" http://${INSECURE_OPENIMAUTO}/v1/authz \ - -d'{"subject":"users:maria","action":"delete","resource":"resources:articles:ladon-introduction","context":{"remoteIPAddress":"192.168.0.5"}}' &>/dev/null - - # 这里要 sleep 5s,等待 openim-pump 将 Redis 中的日志,分析并转存到 MongoDB 中 - echo "wait 10s to allow openim-pump analyze and dump authorization log into MongoDB ..." - sleep 10 - - # 5. 查看 MongoDB 中是否有经过解析后的授权日志。 - echo "db.iam_analytics.find()" | mongosh --quiet "${IAM_PUMP_MONGO_URL}" | grep -q "allow access" || { - openim::log::error "cannot find analyzed authorization log in MongoDB" - return 1 - } -} - -openim::test::watcher() -{ - ${RCURL} http://${IAM_WATCHER_HOST}:5050/healthz | egrep -q 'status.*ok' || { - openim::log::error "cannot access openim-watcher healthz api, openim-watcher maybe down" - return 1 - } - openim::log::info "$(echo -e '\033[32mcongratulations, openim-watcher test passed!\033[0m')" -} - -openim::test::iamctl() -{ - iamctl user list | egrep -q admin || { - openim::log::error "iamctl cannot list users from openim-apiserver" - return 1 - } - openim::log::info "$(echo -e '\033[32mcongratulations, iamctl test passed!\033[0m')" -} - -openim::test::man() -{ - man openim-apiserver | grep -q 'OPENIM API Server' || { - openim::log::error "openim man page not installed or may not installed properly" - return 1 - } - openim::log::info "$(echo -e '\033[32mcongratulations, man test passed!\033[0m')" -} +#################################### Build Module #################################### # OpenIM Smoke Test -openim::test::smoke() -{ - openim::test::apiserver - openim::test::authzserver - openim::test::pump - openim::test::watcher - openim::test::iamctl - openim::log::info "$(echo -e '\033[32mcongratulations, smoke test passed!\033[0m')" +openim::test::smoke() { + openim::register_user + openim::test::check_account + openim::test::register_and_check +} + +openim::test::api() { + openim::test::auth + openim::test::user + openim::test::friend + openim::test::group + openim::test::msg } # OpenIM Test -openim::test::test() -{ +openim::test::test() { + openim::util::require-jq openim::test::smoke openim::test::man + openim::test::api openim::log::info "$(echo -e '\033[32mcongratulations, all test passed!\033[0m')" } diff --git a/scripts/lib/color.sh b/scripts/lib/color.sh index 2e53ae5ae..4d69c1771 100755 --- a/scripts/lib/color.sh +++ b/scripts/lib/color.sh @@ -70,8 +70,7 @@ ITALIC_PREFIX="\033[3m" # Italic prefix CYAN_PREFIX="\033[0;36m" # Cyan prefix # Print colors you can use -openim::color::print_color() -{ +openim::color::print_color() { echo echo -e ${bmagenta}--back-color:${normal} echo "bblack; bgreen; bblue; bcyan; bred; byellow; bmagenta; bwhite" diff --git a/scripts/lib/logging.sh b/scripts/lib/logging.sh index 021a366e1..90f9d0c7f 100755 --- a/scripts/lib/logging.sh +++ b/scripts/lib/logging.sh @@ -200,8 +200,7 @@ openim::log::status() { done } -openim::log::success() -{ +openim::log::success() { local V="${V:-0}" if [[ ${OPENIM_VERBOSE} < ${V} ]]; then return diff --git a/scripts/lib/util.sh b/scripts/lib/util.sh index 6fd9a5cfd..de94d8ceb 100755 --- a/scripts/lib/util.sh +++ b/scripts/lib/util.sh @@ -1126,6 +1126,21 @@ function openim::util::require-jq { fi } +# openim::util::require-dig +# Checks whether dig is installed and provides installation instructions if it is not. +function openim::util::require-dig { + if ! command -v dig &>/dev/null; then + echo "dig command not found." + echo "Please install 'dig' to use this feature." + echo "Installation instructions:" + echo " For Ubuntu/Debian: sudo apt-get install dnsutils" + echo " For CentOS/RedHat: sudo yum install bind-utils" + echo " For macOS: 'dig' should be preinstalled. If missing, try: brew install bind" + echo " For Windows: Install BIND9 tools from https://www.isc.org/download/" + return 1 + fi +} + # outputs md5 hash of $1, works on macOS and Linux function openim::util::md5() { if which md5 >/dev/null 2>&1; then diff --git a/scripts/wait-for-it.sh b/scripts/wait-for-it.sh index 5a1022d26..99a36affe 100755 --- a/scripts/wait-for-it.sh +++ b/scripts/wait-for-it.sh @@ -17,8 +17,7 @@ WAITFORIT_cmdname=${0##*/} echoerr() { if [[ $WAITFORIT_QUIET -ne 1 ]]; then echo "$@" 1>&2; fi } -usage() -{ +usage() { cat << USAGE >&2 Usage: $WAITFORIT_cmdname host:port [-s] [-t timeout] [-- command args] @@ -34,8 +33,7 @@ USAGE exit 1 } -wait_for() -{ +wait_for() { if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then echoerr "$WAITFORIT_cmdname: waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT" else @@ -61,8 +59,7 @@ wait_for() return $WAITFORIT_result } -wait_for_wrapper() -{ +wait_for_wrapper() { # In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692 if [[ $WAITFORIT_QUIET -eq 1 ]]; then timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --quiet --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT & diff --git a/test/wrktest.sh b/test/wrktest.sh index 61b159b80..01617676a 100755 --- a/test/wrktest.sh +++ b/test/wrktest.sh @@ -46,8 +46,7 @@ openim::wrk::setup() { } # Print usage infomation -openim::wrk::usage() -{ +openim::wrk::usage() { cat << EOF Usage: $0 [OPTION] [diff] URL @@ -66,8 +65,7 @@ EOF } # Convert plot data to useable data -function openim::wrk::convert_plot_data() -{ +function openim::wrk::convert_plot_data() { echo "$1" | awk -v datfile="${wrkdir}/${datfile}" ' { if ($0 ~ "Running") { common_time=$2 @@ -123,8 +121,7 @@ if (s ~ "s") { } # Remove existing data file -function openim::wrk::prepare() -{ +function openim::wrk::prepare() { rm -f ${wrkdir}/${datfile} } @@ -167,8 +164,7 @@ EOF } # Plot diff graphic -function openim::wrk::plot_diff() -{ +function openim::wrk::plot_diff() { gnuplot << EOF set terminal png enhanced #输出格式为png文件 set xlabel 'Concurrent'