Skip to content

test(cmdtool): UT_CmdToolOem 改用纯打桩替代 sudo 部署真实 toml 文件 - #970

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
add-uos:test-cmdtool-oem-no-sudo
Nov 11, 2026
Merged

deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
add-uos:test-cmdtool-oem-no-sudo

Conversation

@add-uos

@add-uos add-uos commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

问题

UT_CmdToolOem 中 parseOemTomlInfo 相关 3 个用例(singleSection/multiSection/emptyFile)原先通过 std::system("sudo ...") 将真实 toml 文件部署到 /etc/deepin/hardware/、测完再清理。在无 sudo 凭据且无终端的环境(CI 容器、沙箱)下,sudo 读取密码挂死不返回,导致整个测试进程卡死、UT 阶段超时失败。

方案

复用同仓库 ut_gapcloser.cpp 已验证的纯打桩方案,全程无文件系统/权限依赖:

  • 序列式 QFile::exists 静态桩:控制 主路径 → 调试路径 → 安装目录 三级文件查找
  • QFile::open 经 dlsym 入口桩替换,QFile::readAll 返回预置 toml 内容
  • 删除 deployTomlFile/cleanupTomlFile sudo 辅助函数,保留并强化原有用例语义
  • 新增内容级断言:单节校验 m_cmdInfo["tomloem"] 中 Manufacturer/Product 键值;多节断言两节数据均入库;空文件断言返回 false 且无数据入库
  • 修正一处断言认知:classkey 取节名首个 . 前的部分(oem.info/oem.other 均为 oem),多节用例两节数据挂同一键下

验证

  • UT_CmdToolOem.*:10/10 通过,用例耗时 0ms,无挂起
  • UT_CmdTool* 全系列:104/104 通过(128ms,此前仅 3 个 sudo 用例即挂起 40s+)
  • 测试二进制进程中已无 sudo 调用路径

Summary by Sourcery

Eliminate sudo and filesystem dependencies from CmdTool OEM parsing tests while improving validation of parsed TOML data.

Enhancements:

  • Replace sudo-based TOML file deployment in CmdTool OEM parsing tests with filesystem- and permission-independent stubs.
  • Strengthen OEM TOML parsing coverage with assertions for single-section, multi-section, and empty-file results.

Tests:

  • Make UT_CmdToolOem parsing tests run without sudo or real filesystem changes, preventing hangs in non-interactive environments.

UT_CmdToolOem 中 parseOemTomlInfo 相关用例原先通过 std::system("sudo ...")将真实 toml 部署到 /etc/deepin/hardware/ 再清理。在无 sudo 凭据且无终端的环境(如 CI 容器)下 sudo 读取密码挂死,导致整个测试进程卡死超时。

改用同仓库 ut_gapcloser.cpp 已验证的纯打桩方案:
- 序列式 QFile::exists 静态桩控制主路径/调试路径/安装目录三级查找
- QFile::open 经 dlsym 入口桩替换,QFile::readAll 返回预置内容
- 断言 parseOemTomlInfo 解析结果(单节/多节均校验 m_cmdInfo 内容)
- 删除 deployTomlFile/cleanupTomlFile 辅助函数,不再依赖 sudo

另修正断言:classkey 取节名首个 '.' 前的部分,多节用例两节数据均挂
在 toml+首段键下。

Log: UT_CmdToolOem 用例在无 sudo 终端环境挂起导致 UT 卡死超时
Influence: UT_CmdToolOem 全部用例改为纯内存打桩,无文件系统/权限依赖
@sourcery-ai

sourcery-ai Bot commented Nov 11, 2026

Copy link
Copy Markdown

Sorry @add-uos, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 1 day and 10 hours by commenting @sourcery-ai review. Upgrade to get a review now.

@sourcery-ai

sourcery-ai Bot commented Nov 11, 2026

Copy link
Copy Markdown

Reviewer's Guide

Tests

Replace sudo-based TOML file setup with QFile stubs and strengthen assertions for single-section, multi-section, and empty-file parsing.

  • deepin-devicemanager/tests/src/GenerateDevice/ut_cmdtool_oem.cpp L79-127, L133-150, L156-174 (+3 small edits)

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

🤖 AI 代码审查报告

总体评分: 98 分 (通过阈值: 70分)

Pass


📊 总体评价

项目 结果
审查结论 代码审查通过
评分详情 本次提交将 UT_CmdToolOem 测试从 sudo 部署真实 TOML 文件方案改为纯函数打桩方案, 消除了 std::system("sudo ...") 命令执行带来的安全风险与测试环境依赖。代码逻辑正确, 注释详尽(含历史方案说明),三个测试用例覆盖单节/多节/空文件场景。仅存在 5 个文件级 可变静态变量用于桩状态管理的小质量问题,无安全漏洞。整体质量优秀。

🔍 详细分析

1. 语法逻辑 ✅

评价: 语法正确,逻辑清晰 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: ['dlsym 硬编码 mangled name (_ZN5QFile4openE6QFlagsIN13QIODeviceBase12OpenModeFlagEE) 依赖特定 Qt ABI 版本,若 Qt 版本升级可能失效。虽有 nullptr 检查不会崩溃, 但会导致 open 桩静默失败使测试出错。建议添加编译期或启动期 ABI 校验日志。']


2. 代码质量 ✅

评价: 代码结构清晰,注释完整 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: ['将桩状态变量封装为一个结构体(如 UtOemStubState),在测试夹具或局部作用域中管理,减少全局可变状态。']


3. 代码性能 ✅

评价: 性能良好,资源使用合理 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: []


4. 代码安全 🔒

评价: 存在0个安全漏洞 ✅ 通过

🔐 发现 0 个安全漏洞

安全漏洞详情:
✅ 未发现安全漏洞

建议: []


💡 改进建议代码示例

// 建议优化:将桩状态封装为结构体,减少全局可变状态
struct UtOemStubState {
    QList<bool> existsSeq;
    int existsIdx = 0;
    QList<bool> openSeq;
    int openIdx = 0;
    QByteArray fileContent;

    void reset(QList<bool> exists, QList<bool> opens, const QByteArray &content) {
        existsSeq = exists;
        existsIdx = 0;
        openSeq = opens;
        openIdx = 0;
        fileContent = content;
    }
};
static UtOemStubState ut_oem_state;
// 桩函数引用 ut_oem_state 而非独立全局变量

本报告由 AI 代码审查工具自动生成

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: add-uos, lzwind

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@add-uos

add-uos commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

/forcemerge

@deepin-bot
deepin-bot Bot merged commit eaf4d41 into linuxdeepin:master Nov 11, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants