Skip to content

Add aide-helpers library#58

Open
msafarik wants to merge 8 commits into
masterfrom
ms_library
Open

Add aide-helpers library#58
msafarik wants to merge 8 commits into
masterfrom
ms_library

Conversation

@msafarik

@msafarik msafarik commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Extract duplicate functions (aideInit, aideCheck, aideGetDbPaths, config helpers) from tests into a shared Library/aide-helpers

Summary by Sourcery

Introduce a shared aide-helpers testing library and update existing aide tests to use it instead of duplicated inline helpers.

Enhancements:

  • Add a reusable Library/aide-helpers shell library providing common aide testing utilities for database handling, configuration management, and test directory setup.
  • Refactor aide-related sanity and regression tests to import aide-helpers and rely on its helpers for configuration cleanup and database initialization/checks.

Tests:

  • Add a self-test for the aide-helpers library to verify it can be imported and used within the BeakerLib test environment.

Signed-off-by: Marek Safarik <msafarik@redhat.com>
@msafarik msafarik self-assigned this Jul 20, 2026
@sourcery-ai

sourcery-ai Bot commented Jul 20, 2026

Copy link
Copy Markdown

Reviewer's Guide

Introduce a shared Beakerlib-based aide-helpers library for AIDE tests, centralizing database path resolution, initialization/check helpers, config utilities, and test directory management; update existing tests to consume the shared helpers instead of duplicating logic.

File-Level Changes

Change Details Files
Introduce a new Beakerlib aide-helpers shell library with reusable functions for AIDE database management, configuration handling, and test directory utilities.
  • Define internal defaults for aide.conf, database, log, and temporary directories.
  • Implement aideGetDbPaths, aideInit, and aideCheck to standardize AIDE DB path resolution, initialization, and checks with RHEL-version awareness.
  • Add configuration backup/restore helpers and RHEL-version-aware config selection (aideGetRhelConfig).
  • Provide config manipulation helpers such as aideSetupConfig, aideStripDefaultRules, aideStripPaths, and aideAddContentexGroup.
  • Add test directory helpers aideCreateTestDir and aideCleanupTestDir, plus a verification callback aideLibraryLoaded for rlImport.
Library/aide-helpers/lib.sh
Add a minimal self-test for the aide-helpers library to validate import and basic setup.
  • Create a Beakerlib runtest.sh that imports ./aide-helpers, creates a temporary directory, and runs a simple phase-based self test.
  • Ensure cleanup of the temporary directory and journaling via rlJournalStart/End.
Library/aide-helpers/runtest.sh
Refactor existing AIDE tests to use the shared aide-helpers library instead of inlined helper functions and config munging.
  • Remove local aideInit/aideCheck and DB path computation from sanity and regression tests.
  • Import ./aide-helpers via rlImport in affected tests.
  • Replace direct sed manipulation of aide.conf paths and CONTENTEX group addition with aideStripPaths and aideAddContentexGroup calls.
  • Keep existing test semantics while delegating the common operations to the shared library.
Sanity/aide-check-exit-codes/runtest.sh
Sanity/aide-conf-selection-lines/runtest.sh
Regression/rhel4331-grubenv-timestaps-break-aide-integrity-check/runtest.sh

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 3 issues, and left some high level feedback:

  • In aideLibraryLoaded, automatically running yum -y install aide inside a helper library can be surprising and may fail on non-yum systems; consider relying on existing rlAssertRpm/test setup instead of performing package installation in the library.
  • The sed patterns in aideSetupConfig assume that @@define DBDIR and @@define LOGDIR lines are present in the source config; if they are missing or formatted differently, the function silently leaves them unchanged—consider handling that case explicitly or returning an error.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `aideLibraryLoaded`, automatically running `yum -y install aide` inside a helper library can be surprising and may fail on non-yum systems; consider relying on existing `rlAssertRpm`/test setup instead of performing package installation in the library.
- The `sed` patterns in `aideSetupConfig` assume that `@@define DBDIR` and `@@define LOGDIR` lines are present in the source config; if they are missing or formatted differently, the function silently leaves them unchanged—consider handling that case explicitly or returning an error.

## Individual Comments

### Comment 1
<location path="Library/aide-helpers/lib.sh" line_range="86" />
<code_context>
+
+aideGetDbPaths() {
+    local CONF="${1:-$__INTERNAL_aideConfDefault}"
+    DBDIR=$(sed -n -e 's/@@define DBDIR \([a-z/]\+\)/\1/p' "$CONF")
+    if rlIsRHELLike "=<9.7"; then
+        DB=$(grep "^database=" "$CONF" | cut -d/ -f2-)
</code_context>
<issue_to_address>
**issue (bug_risk):** DBDIR extraction regex is overly restrictive and may fail for common directory names.

The current pattern `\([a-z/]\+\)` will fail for paths containing digits, underscores, dashes, uppercase letters, etc., leaving `DBDIR` empty and potentially breaking downstream logic. Please broaden the match (for example, using `[^ ]\+` or `\(.\+\)` with suitable anchoring) so it reliably captures any valid directory path used in `@@define DBDIR`.
</issue_to_address>

### Comment 2
<location path="Library/aide-helpers/lib.sh" line_range="278-279" />
<code_context>
+    local DEST="$1"
+    local SRC="${2:-$__INTERNAL_aideConfDefault}"
+    rlRun "cp $SRC $DEST/aide.conf"
+    rlRun "sed -i 's#^@@define DBDIR.*#@@define DBDIR ${DEST}/db#' $DEST/aide.conf"
+    rlRun "sed -i 's#^@@define LOGDIR.*#@@define LOGDIR ${DEST}/log#' $DEST/aide.conf"
+}
+
</code_context>
<issue_to_address>
**issue (bug_risk):** Using DEST directly in the sed replacement can break when DEST contains `#` or other special characters.

Since `#` is the sed delimiter, any `#` in `${DEST}` will break the command (prematurely ending the pattern or causing a syntax error). This is fragile if tests or callers pass arbitrary paths. Consider using a delimiter less likely to appear in paths (e.g. `|`) or escaping `${DEST}` before inserting it into the sed expression.
</issue_to_address>

### Comment 3
<location path="Library/aide-helpers/lib.sh" line_range="395-396" />
<code_context>
+
+aideCreateTestDir() {
+    local DIR="${1:-/var/aide-testing-dir}"
+    mkdir -p "$DIR/db" "$DIR/log" "$DIR/data"
+    echo "$DIR"
+}
+
</code_context>
<issue_to_address>
**suggestion (bug_risk):** aideCreateTestDir does not use rlRun or check for failures when creating directories.

Other helpers wrap side-effecting operations (cp, sed, rm) in `rlRun` for logging and error handling. Here `mkdir -p` runs directly, so permission or other errors are silent while the function still echoes the directory. Please use `rlRun "mkdir -p ..."` so failures are reported to beakerlib and callers don’t see a false success.

Suggested implementation:

```
aideCreateTestDir() {
    local DIR="${1:-/var/aide-testing-dir}"

    rlRun "mkdir -p \"$DIR/db\" \"$DIR/log\" \"$DIR/data\"" 0 \
        "Create AIDE test directories in $DIR" || return $?

    echo "$DIR"
}

```

This change assumes `beakerlib.sh` is sourced before `aideCreateTestDir` is ever called so that `rlRun` is available. If it is not, you should either:
1. Move the `. /usr/share/beakerlib/beakerlib.sh || exit 1` line above the function definition, or
2. Ensure the library is sourced by callers before invoking `aideCreateTestDir`.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.


aideGetDbPaths() {
local CONF="${1:-$__INTERNAL_aideConfDefault}"
DBDIR=$(sed -n -e 's/@@define DBDIR \([a-z/]\+\)/\1/p' "$CONF")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue (bug_risk): DBDIR extraction regex is overly restrictive and may fail for common directory names.

The current pattern \([a-z/]\+\) will fail for paths containing digits, underscores, dashes, uppercase letters, etc., leaving DBDIR empty and potentially breaking downstream logic. Please broaden the match (for example, using [^ ]\+ or \(.\+\) with suitable anchoring) so it reliably captures any valid directory path used in @@define DBDIR.

Comment thread Library/aide-helpers/lib.sh Outdated
Comment thread Library/aide-helpers/lib.sh Outdated
Marek Safarik added 3 commits July 20, 2026 12:08
Signed-off-by: Marek Safarik <msafarik@redhat.com>
@msafarik

Copy link
Copy Markdown
Contributor Author

@msafarik
msafarik requested a review from Koncpa July 20, 2026 12:58
@Koncpa

Koncpa commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Why is failing that CI for Rawhide? @msafarik

@Koncpa

Koncpa commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Should be cover more than 3 tests, also should cover tests in downstream, please extend it. @msafarik

@Koncpa Koncpa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Overall this library should be applied for all tests (upstream/downstream), I don't think that cover just three of them, also should be properly tested, not only via CI plan . Structure of function should be different, there should be one configuration function. Also creating tmp dir could be replaced by beakerlib general function.

@@ -32,27 +32,9 @@ PACKAGE="aide"
AIDE_CONF="/etc/aide.conf"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This test it's no ran in CI, should prepare TF where is executed for correctnes of changes, please schedule it.

Comment thread Library/aide-helpers/lib.sh Outdated

=cut

aideCreateTestDir() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could be used temporary dir from base beakerlib library? And adjust in all tests?

Comment thread Library/aide-helpers/lib.sh Outdated

=cut

aideCleanupTestDir() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

When would be used creating dir from base beakerlib library, this function it's not needed, but also need to be replaced in code.

Comment thread Library/aide-helpers/lib.sh Outdated

=cut

aideSetupConfig() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Couldnt be one function which prepare whole configuration for aide testing?

Comment thread Library/aide-helpers/lib.sh Outdated
Removes everything after the "Next decide what directories/files"
comment.

aideStripDefaultRules AIDE_CONF

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Couldnt be one function which prepare whole configuration for aide testing?

Comment thread Library/aide-helpers/lib.sh Outdated

=cut

aideStripPaths() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Couldnt be one function which prepare whole configuration for aide testing?

Comment thread Library/aide-helpers/lib.sh Outdated

=cut

aideAddContentexGroup() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Couldnt be one function which prepare whole configuration for aide testing?

@msafarik

Copy link
Copy Markdown
Contributor Author

@msafarik

msafarik commented Jul 23, 2026

Copy link
Copy Markdown
Contributor Author

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.

2 participants