Skip to content

test: add unit tests for opslog, threads and auth - #565

Merged
pengfeixx merged 1 commit into
linuxdeepin:masterfrom
pengfeixx:ut-coverage-batch-7
Oct 27, 2026
Merged

pengfeixx merged 1 commit into
linuxdeepin:masterfrom
pengfeixx:ut-coverage-batch-7

Conversation

@pengfeixx

@pengfeixx pengfeixx commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Part 7/8 of the deepin-log-viewer unit-test coverage series (single-commit PR; all 8 PRs touch disjoint file sets and are mutually independent).

Files in this PR (6 files, +1469/-8 lines)

  • tests/src/ut_opslogexport.cpp (new)
  • tests/src/ut_parsethreadkern_kwin.cpp (new)
  • tests/src/ut_logauththread.cpp
  • tests/src/ut_parsethreadbase.cpp (new)
  • tests/dbusproxy/ut_dldbusinterface.cpp
  • tests/src/ut_dbusmanager.cpp

Notes

  • Test-only change: no production source code is modified.
  • New test files live in tests/src/ / tests/dbusproxy/ / tests/ut_logviewerplugin/, globbed automatically by tests/CMakeLists.txt (re-run cmake after pulling).
  • Each file was compiled and executed individually (GTest + lcov) in its generating task; whole-branch build verification recommended in CI.
  • Series goal: function coverage toward 100% (baseline: 690 passing cases, 68.2% function / 58.5% line coverage).

Summary by Sourcery

Increase unit-test coverage across log export, parsing, authentication, and DBus components.

Enhancements:

  • Expand unit-test coverage for operations-log export, kernel/KWin parsing, log authentication, parsing-thread lifecycle, DBus interfaces, and DBus manager behavior.

Tests:

  • Add comprehensive tests covering operations-log export helpers and workflows, parser edge cases, authentication log handlers, and DBus API and manager behavior.

@sourcery-ai

sourcery-ai Bot commented Oct 27, 2026

Copy link
Copy Markdown

Reviewer's Guide

This test-only PR adds broad unit coverage across ops-log exporting, parse-thread lifecycle and log parsing, authentication-related handlers, and DBus interfaces/managers, using temporary filesystem fixtures and targeted stubs to exercise success, failure, boundary, and unavailable-service branches.

File-Level Changes

Change Details Files
Adds focused unit coverage for operations-log export behavior, including file operations, grep-style extraction, command execution, path expansion, directory creation, log-category exports, and aggregate execution.
  • Exercises append and grep helpers with temporary files.
  • Covers successful and failing process execution paths.
  • Tests wildcard expansion, copy behavior, directory structures, and export entry points using stubs where needed.
tests/src/ut_opslogexport.cpp
Adds lifecycle and branch tests for parse-thread infrastructure and kernel/KWin log parsing.
  • Introduces a concrete ParseThreadBase test subclass to verify construction, process management, stopping, filters, indexing, connections, and run behavior.
  • Covers kernel and KWin parsing for missing files, empty and compressed inputs, command-mode and pkexec paths, process exit outcomes, line-range boundaries, early stopping, and date parsing.
tests/src/ut_parsethreadbase.cpp
tests/src/ut_parsethreadkern_kwin.cpp
Expands LogAuthThread coverage by stubbing filesystem, DBus, process, audit, authentication, coredump, and dpkg inputs.
  • Exercises audit, auth, coredump, and dpkg handlers across missing-file, parsing, command, and time-filter branches.
  • Tests reading trailing application-log lines for existing, short, and nonexistent files.
tests/src/ut_logauththread.cpp
Expands DBus-facing unit tests to cover interface methods and DBus manager behavior in unavailable-service environments.
  • Adds individual tests for interface naming, file-path and descriptor overloads, stream operations, metadata queries, command execution, and export success/failure paths.
  • Verifies DBusManager construction with and without parents plus false/empty fallbacks for service-dependent queries.
tests/dbusproxy/ut_dldbusinterface.cpp
tests/src/ut_dbusmanager.cpp

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

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 4 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="tests/src/ut_logauththread.cpp" line_range="830" />
<code_context>
+    stub.set(ADDR(QProcess, exitCode), stub_exitCode);
+    stub.set(ADDR(DLDBusHandler, readLog), stub_authReadLog);
+
+    QString tmpPath = "/tmp/test_auth_ut_dlv.txt";
+    {
+        QFile tmpFile(tmpPath);
</code_context>
<issue_to_address>
**Temporary files overwrite or disrupt other data**

When these tests run concurrently or a fixed `/tmp` path already exists or is a symlink, `QFile::open(QIODevice::WriteOnly)` truncates shared files or follows symlinks, and `QFile::remove` can delete another run's fixture; tests overwrite unrelated data or fail nondeterministically.

Use a unique, securely created temporary file for each test invocation and remove only the file that invocation created.

Also at `tests/src/ut_logauththread.cpp:833-834`, `tests/src/ut_logauththread.cpp:843`, `tests/src/ut_logauththread.cpp:883-899`, `tests/src/ut_logauththread.cpp:904-915`.
</issue_to_address>

### Comment 2
<location path="tests/src/ut_opslogexport.cpp" line_range="402" />
<code_context>
+    ASSERT_TRUE(tmpDir.isValid());
+    OpsLogExport exporter(tmpDir.path().toStdString());
+
+    exporter.exportAptLogs();
+    SUCCEED();
+}
</code_context>
<issue_to_address>
**Export tests use host log trees**

When the tests run on a host with large, inaccessible, or unusual log directories, `exportAptLogs` and the UOS-Ste exporters read and copy real `/var/log` trees, so test runtime and resource use depend on host contents and large trees can exhaust temporary storage.

Stub the filesystem work and exercise the exporters with isolated fixture directories.

Also at `tests/src/ut_opslogexport.cpp:412`, `tests/src/ut_opslogexport.cpp:422`.
</issue_to_address>

### Comment 3
<location path="tests/src/ut_logauththread.cpp" line_range="804" />
<code_context>
+    m_logAuthThread->m_FilePath = QStringList() << "/var/log/audit/audit.log";
+    m_logAuthThread->handleAudit();
+    Utils::runInCmd = false;
+    EXPECT_EQ(m_logAuthThread->m_canRun, true);
+}
+
</code_context>
<issue_to_address>
**Handler tests accept empty results**

When a handler returns normally but fails to parse, emit, or apply its filter to the supplied records, `handleAudit_UT_001` (line 804), `handleAuth_UT_001` (line 841), `handleCoredump_UT_001` (line 877), and both DPKG time-filter tests (lines 942 and 971) only assert that `m_canRun` remains true. The handlers leave it true after successful returns, so dropping every parsed record or ignoring the DPKG filter still passes.

Assert the emitted data and its contents, and verify that the in-range and out-of-range DPKG inputs produce different results.

Also at `tests/src/ut_logauththread.cpp:841`, `tests/src/ut_logauththread.cpp:877`, `tests/src/ut_logauththread.cpp:943`, `tests/src/ut_logauththread.cpp:972`.
</issue_to_address>

### Comment 4
<location path="tests/src/ut_logauththread.cpp" line_range="892" />
<code_context>
+    }
+
+    QString result = m_logAuthThread->readAppLogFromLastLines(tmpPath, 2);
+    EXPECT_TRUE(result.contains("line3") || result.contains("line4"));
+
+    // Test with non-existent file
</code_context>
<issue_to_address>
**Log test accepts only one line**

When `readAppLogFromLastLines` returns only one of the requested final two lines, the OR assertion passes when the result contains `line3` or `line4` alone, so the test does not catch a regression that returns fewer lines than requested.

Assert that the result contains both expected final lines.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

stub.set(ADDR(QProcess, exitCode), stub_exitCode);
stub.set(ADDR(DLDBusHandler, readLog), stub_authReadLog);

QString tmpPath = "/tmp/test_auth_ut_dlv.txt";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟠 High · Temporary files overwrite or disrupt other data

When these tests run concurrently or a fixed /tmp path already exists or is a symlink, QFile::open(QIODevice::WriteOnly) truncates shared files or follows symlinks, and QFile::remove can delete another run's fixture; tests overwrite unrelated data or fail nondeterministically.

Use a unique, securely created temporary file for each test invocation and remove only the file that invocation created.

Also at tests/src/ut_logauththread.cpp:833-834, tests/src/ut_logauththread.cpp:843, tests/src/ut_logauththread.cpp:883-899, tests/src/ut_logauththread.cpp:904-915.

Prompt for AI agents
In `tests/src/ut_logauththread.cpp` at line 830:

**Temporary files overwrite or disrupt other data**

When these tests run concurrently or a fixed `/tmp` path already exists or is a symlink, `QFile::open(QIODevice::WriteOnly)` truncates shared files or follows symlinks, and `QFile::remove` can delete another run's fixture; tests overwrite unrelated data or fail nondeterministically.

Use a unique, securely created temporary file for each test invocation and remove only the file that invocation created.

Also at `tests/src/ut_logauththread.cpp:833-834`, `tests/src/ut_logauththread.cpp:843`, `tests/src/ut_logauththread.cpp:883-899`, `tests/src/ut_logauththread.cpp:904-915`.

ASSERT_TRUE(tmpDir.isValid());
OpsLogExport exporter(tmpDir.path().toStdString());

exporter.exportAptLogs();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Export tests use host log trees

When the tests run on a host with large, inaccessible, or unusual log directories, exportAptLogs and the UOS-Ste exporters read and copy real /var/log trees, so test runtime and resource use depend on host contents and large trees can exhaust temporary storage.

Stub the filesystem work and exercise the exporters with isolated fixture directories.

Also at tests/src/ut_opslogexport.cpp:412, tests/src/ut_opslogexport.cpp:422.

Prompt for AI agents
In `tests/src/ut_opslogexport.cpp` at line 402:

**Export tests use host log trees**

When the tests run on a host with large, inaccessible, or unusual log directories, `exportAptLogs` and the UOS-Ste exporters read and copy real `/var/log` trees, so test runtime and resource use depend on host contents and large trees can exhaust temporary storage.

Stub the filesystem work and exercise the exporters with isolated fixture directories.

Also at `tests/src/ut_opslogexport.cpp:412`, `tests/src/ut_opslogexport.cpp:422`.

m_logAuthThread->m_FilePath = QStringList() << "/var/log/audit/audit.log";
m_logAuthThread->handleAudit();
Utils::runInCmd = false;
EXPECT_EQ(m_logAuthThread->m_canRun, true);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Handler tests accept empty results

When a handler returns normally but fails to parse, emit, or apply its filter to the supplied records, handleAudit_UT_001 (line 804), handleAuth_UT_001 (line 841), handleCoredump_UT_001 (line 877), and both DPKG time-filter tests (lines 942 and 971) only assert that m_canRun remains true. The handlers leave it true after successful returns, so dropping every parsed record or ignoring the DPKG filter still passes.

Assert the emitted data and its contents, and verify that the in-range and out-of-range DPKG inputs produce different results.

Also at tests/src/ut_logauththread.cpp:841, tests/src/ut_logauththread.cpp:877, tests/src/ut_logauththread.cpp:943, tests/src/ut_logauththread.cpp:972.

Prompt for AI agents
In `tests/src/ut_logauththread.cpp` at line 804:

**Handler tests accept empty results**

When a handler returns normally but fails to parse, emit, or apply its filter to the supplied records, `handleAudit_UT_001` (line 804), `handleAuth_UT_001` (line 841), `handleCoredump_UT_001` (line 877), and both DPKG time-filter tests (lines 942 and 971) only assert that `m_canRun` remains true. The handlers leave it true after successful returns, so dropping every parsed record or ignoring the DPKG filter still passes.

Assert the emitted data and its contents, and verify that the in-range and out-of-range DPKG inputs produce different results.

Also at `tests/src/ut_logauththread.cpp:841`, `tests/src/ut_logauththread.cpp:877`, `tests/src/ut_logauththread.cpp:943`, `tests/src/ut_logauththread.cpp:972`.

}

QString result = m_logAuthThread->readAppLogFromLastLines(tmpPath, 2);
EXPECT_TRUE(result.contains("line3") || result.contains("line4"));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Log test accepts only one line

When readAppLogFromLastLines returns only one of the requested final two lines, the OR assertion passes when the result contains line3 or line4 alone, so the test does not catch a regression that returns fewer lines than requested.

Assert that the result contains both expected final lines.

Prompt for AI agents
In `tests/src/ut_logauththread.cpp` at line 892:

**Log test accepts only one line**

When `readAppLogFromLastLines` returns only one of the requested final two lines, the OR assertion passes when the result contains `line3` or `line4` alone, so the test does not catch a regression that returns fewer lines than requested.

Assert that the result contains both expected final lines.

@pengfeixx
pengfeixx force-pushed the ut-coverage-batch-7 branch from ce8b9ac to b031dd1 Compare October 27, 2026 11:30
@pengfeixx pengfeixx changed the title feat(test): add opslog, parse-thread and auth unit tests (batch 7/8) test: add unit tests for opslog, threads and auth Oct 27, 2026
@pengfeixx
pengfeixx force-pushed the ut-coverage-batch-7 branch from b031dd1 to 44c86d3 Compare October 27, 2026 11:34
1. Add ut_opslogexport.cpp, ut_parsethreadkern_kwin.cpp and
   ut_parsethreadbase.cpp
2. Extend ut_logauththread.cpp auth flows
3. Extend ut_dldbusinterface.cpp and ut_dbusmanager.cpp

Log: No user-facing changes

Influence:
1. Run deepin-log-viewer-test with --gtest_filter='*Ops*;*Parse*'
2. Verify full suite passes with ASAN_OPTIONS=detect_leaks=0
3. Check lcov function coverage of opslogexport and parse threads

test: 新增运维日志、解析线程与鉴权单元测试

1. 新增 ut_opslogexport.cpp、ut_parsethreadkern_kwin.cpp 与
   ut_parsethreadbase.cpp
2. 扩展 ut_logauththread.cpp 鉴权流程
3. 扩展 ut_dldbusinterface.cpp 与 ut_dbusmanager.cpp

Log: 无用户可见变化

Influence:
1. 运行 deepin-log-viewer-test 过滤 Ops/Parse 套件
2. 以 ASAN_OPTIONS=detect_leaks=0 验证全量用例通过
3. 用 lcov 检查 opslogexport 与解析线程覆盖率
@pengfeixx
pengfeixx force-pushed the ut-coverage-batch-7 branch from 44c86d3 to 8cc12ab Compare October 27, 2026 12:09
@deepin-bot

deepin-bot Bot commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

TAG Bot

New tag: 6.5.43
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #567

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

🤖 AI 代码审查报告

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

Pass


📊 总体评价

项目 结果
审查结论 代码审查通过
评分详情 本次提交为 deepin-log-viewer 项目的单元测试新增与扩充,涵盖 DlDbusinterface、DBusManager、LogAuthThread、OpsLogExport、ParseThreadBase、ParseThreadKern/ParseThreadKwin 六个模块的测试代码。代码语法正确、逻辑清晰、测试覆盖面广,未发现安全漏洞,四维度全部通过。

🔍 详细分析

1. 语法逻辑 ✅

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

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

建议: []


2. 代码质量 ✅

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

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

建议: ['建议在 ut_parsethreadkern_kwin.cpp 的 HandleKern_PkexecPath_ExitCodeZero 和 HandleKern_PkexecPath_ExitCodeNonZero 测试中,第二个 Utils::runInCmd = false 为冗余赋值(前一行已设为 false),可移除以保持简洁。']


3. 代码性能 ✅

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

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

建议: []


4. 代码安全 🔒

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

🔐 发现 0 个安全漏洞

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

建议: []


💡 改进建议代码示例

// 建议:移除冗余的 Utils::runInCmd = false 赋值 // ut_parsethreadkern_kwin.cpp - HandleKern_PkexecPath_ExitCodeZero TEST_F(ParseThreadKern_UT, HandleKern_PkexecPath_ExitCodeZero) {
    Stub stub;
    stub.set(ADDR(DLDBusHandler, getFileInfo), stub_getFileInfo_txt);
    stub.set(ADDR(DLDBusHandler, getLineCount), stub_getLineCount_100);
    stub.set(ADDR(DLDBusHandler, readLogLinesInRange), stub_readLogLinesInRange_kern);
    stub.set((void (QProcess::*)(const QString &, const QStringList &, QIODevice::OpenMode))ADDR(QProcess, start), stub_start);
    stub.set(ADDR(QProcess, waitForFinished), stub_waitForFinished);
    stub.set(ADDR(QProcess, setProcessChannelMode), stub_setProcessChannelMode);
    stub.set(ADDR(QProcess, exitCode), stub_exitCode_zero);
    stub.set(ADDR(SharedMemoryManager, setRunnableTag), stub_setRunnableTag);
    stub.set(ADDR(SharedMemoryManager, getRunnableKey), stub_getRunnableKey);
    Utils::runInCmd = false;
    m_kern->handleKern();
    // 移除冗余的 Utils::runInCmd = false; (上一行已设置为 false)
    SUCCEED();
}

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

@pengfeixx
pengfeixx merged commit 3820d2a into linuxdeepin:master Oct 27, 2026
15 of 16 checks passed
@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: lzwind, pengfeixx

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

@pengfeixx
pengfeixx deleted the ut-coverage-batch-7 branch October 27, 2026 12:39
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