From d0357510d8616fe958729b096a781bcdf905f0e8 Mon Sep 17 00:00:00 2001 From: zhaobangyu <763098346@QQ.com> Date: Wed, 18 Oct 2023 14:52:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/autotest.lua | 8 +++ server/autotest/client.lua | 86 ++++++++++++++++++++++++++++ server/autotest/robot/robot.lua | 11 ++++ server/autotest/robot/session.lua | 93 +++++++++++++++++++++++++++++++ 4 files changed, 198 insertions(+) create mode 100644 server/autotest.lua create mode 100644 server/autotest/client.lua create mode 100644 server/autotest/robot/robot.lua create mode 100644 server/autotest/robot/session.lua diff --git a/server/autotest.lua b/server/autotest.lua new file mode 100644 index 00000000..df10206f --- /dev/null +++ b/server/autotest.lua @@ -0,0 +1,8 @@ +#!./quanta +import("kernel.lua") + +require("socket.core") +require("LuaPanda").start("127.0.0.1", 8810) +quanta.startup(function() + import("autotest/client.lua") +end) diff --git a/server/autotest/client.lua b/server/autotest/client.lua new file mode 100644 index 00000000..29239921 --- /dev/null +++ b/server/autotest/client.lua @@ -0,0 +1,86 @@ +-- 终端 +local log_info = logger.info +local log_err = logger.err +local json_encode = json.encode + +local update_mgr = quanta.get("update_mgr") +local http = quanta.get("http_client") + +local AUTO_TEST_API = environ.get("AUTO_TEST_API") + +local Client = singleton() +local prop = property(Client) +prop:reader("login", false) + +function Client:__init() + log_info("[Client][__init]...") + update_mgr:attach_second5(self) +end + +-- 获取主机名称 +function Client:host_name() + local file = io.popen("hostname") + local hostname = file:read("*a"):gsub("\n", "") + return hostname or "" +end + +-- 获取主机地址 +function Client:host_ip() + local f = io.popen("ipconfig") + local hostip + for line in f:lines() do + local start_pos, end_pos, ip_addr = line:find(":%s*([^%s]+)") + if start_pos and end_pos and ip_addr then + hostip = ip_addr:match("(%d+%.%d+%.%d+%.%d+)") + if hostip then + break + end + end + end + return hostip or "" +end + +-- 获取git版本 +function Client:git_ver() + local file = io.popen("git rev-parse --short HEAD") + local git_version = file:read("*a"):gsub("\n", "") + return git_version or "" +end + +function Client:on_second5() + self:update() +end + +-- 账号逻辑 +function Client:account() + local host_name = self:host_name() + if not self.login then + local git_ver = self:git_ver() + local ok, status, res = http:call_post(AUTO_TEST_API .. "/pressclt/login", {}, {}, + { host_name = host_name, gitver = git_ver, host_ip = self:host_ip() }) + if not ok or status >= 300 then + log_err("[Client][on_second5] login post failed! code: {}, err: {}", status, res) + return + end + self.login = true + log_info("[Client][on_second5] login success,host_name:{} git_ver:{}", host_name, git_ver) + else + http:call_post(AUTO_TEST_API .. "/pressclt/heart", {}, {}, { host_name = host_name }) + end +end + +-- 任务逻辑 +function Client:task() + +end + +-- 更新定时器 +function Client:update() + -- 账号逻辑 + self:account() + -- 任务逻辑 + self:task() +end + +quanta.client = Client() +return Client diff --git a/server/autotest/robot/robot.lua b/server/autotest/robot/robot.lua new file mode 100644 index 00000000..a48d9651 --- /dev/null +++ b/server/autotest/robot/robot.lua @@ -0,0 +1,11 @@ +local log_debug = logger.debug +local SessionModule = import("autotest/robot/session.lua") + +local Robot = class(nil, SessionModule) +local prop = property(Robot) + +prop:accessor("id", 0) +function Robot:__init() +end + +return Robot diff --git a/server/autotest/robot/session.lua b/server/autotest/robot/session.lua new file mode 100644 index 00000000..bca66473 --- /dev/null +++ b/server/autotest/robot/session.lua @@ -0,0 +1,93 @@ +-- session.lua +local log_warn = logger.warn +local log_debug = logger.debug +local tunpack = table.unpack + +local protobuf_mgr = quanta.get("protobuf_mgr") +local NetClient = import("network/net_client.lua") + +local SessionModule = mixin() +local prop = property(SessionModule) +prop:reader("client", nil) +prop:reader("cmd_doers", {}) + +function SessionModule:__init() +end + +function SessionModule:disconnect() + if self.client then + self.client:close() + end +end + +function SessionModule:connect(ip, port, block) + if self.client then + self.client:close() + end + self.serial = 1 + self.client = NetClient(self, ip, port) + return self.client:connect(block) +end + +-- 连接成回调 +function SessionModule:on_socket_connect(client) + log_debug("[SessionModule][on_socket_connect] %s", self:get_title()) +end + +-- 连接关闭回调 +function SessionModule:on_socket_error(client, token, err) + log_debug("[SessionModule][on_socket_error] %s, err:%s", self:get_title(), err) +end + +-- ntf消息回调 +function SessionModule:on_socket_rpc(client, cmd_id, body) + self:push_message(cmd_id, body) + local doer = self.cmd_doers[cmd_id] + if not doer then + log_warn("[SessionModule][on_socket_rpc] cmd %s hasn't register doer!, msg=%s", cmd_id, body) + return + end + local module, handler = tunpack(doer) + module[handler](self, body) +end + + +-- 注册NTF消息处理 +function SessionModule:register_doer(pb_name, module, handler) + local cmdid = protobuf_mgr:enum("NCmdId", pb_name) + self.cmd_doers[cmdid] = {module, handler} +end + +function SessionModule:conv_type(cmdid) + if type(cmdid) == "string" then + cmdid = protobuf_mgr:msg_id(cmdid) + end + if cmdid < 10000 then + return 0 + end + return (cmdid // 1000) % 10 +end + +function SessionModule:send(cmdid, data) + if self.client then + return self.client:send(cmdid, data, self:conv_type(cmdid)) + end +end + +function SessionModule:call(cmdid, data) + if self.client then + local ok, resp = self.client:call(cmdid, data, self:conv_type(cmdid)) + return ok, resp + end + return false +end + +-- 等待NTF命令或者非RPC命令 +function SessionModule:wait(cmdid, time) + if self.client then + return self.client:wait(cmdid, time) + end + return false +end + +return SessionModule