Skip to content

tests: add tests.session create-session/destroy-session - #17469

Open
olivercalder wants to merge 4 commits into
canonical:masterfrom
olivercalder:add-tests-session-create-destroy-session
Open

tests: add tests.session create-session/destroy-session#17469
olivercalder wants to merge 4 commits into
canonical:masterfrom
olivercalder:add-tests-session-create-destroy-session

Conversation

@olivercalder

@olivercalder olivercalder commented Aug 17, 2026

Copy link
Copy Markdown
Member

The tests.session -u <username> prepare function enables linger and starts default.target, but it doesn't actually create a systemd session. But we need a real session in some situations, as in #17380 where we need services to run a program in ExecCondition which will look up the systemd session and decide whether to run based on its class.

This PR adds two new commands:

tests.session -u <username> create-session [-c <class>] [-t <type>] [-l <label>]
tests.session -u <username> destroy-session [-l <label>]

These allow callers to create and destroy systemd user sessions with a given class and type. The optional label argument allows a label to be appended to the default session name, so it's possible to manage multiple sessions for the same user at the same time.

The new subcommands should be used alongside tests.session -u <username> prepare and tests.session -u <username> cleanup in tests which require a real systemd user session to be present.

Cherry-picked from #17380

Tracked internally by: https://warthogs.atlassian.net/browse/SNAPDENG-37328

Add new `tests.session` subcommands to create a systemd user session
with a given class and type, and another subcommand to destroy a
previously-created session.

These are useful for tests which require a real systemd user session to
be present. The existing `tests.session prepare` subcommand enables
linger and starts the `default.target`, but it does not start a session.
So the new subcommands should be used in conjunction with `prepare` when
a test requires a real session.

This is the case for any spread test which needs to run a user daemon,
once the `ExecCondition=/usr/bin/snap routine user-service-precondition`
is in place in their service files.

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>
Add a comment explaining interactions between sessions created via
`tests.session create-session` and `tests.session exec`. In short, these
are separate sessions which should never interact. Commands run within
`tests.session exec` run in their own background session, and will never
be mistakenly identified with an existing session. And a non-background
session has precedence over background sessions for the fallback session
lookup of processes running outside any session, such as during
systemd's `ExecCondition` evaluations.

Add a comment explaining why `systemd-run` can be used on all systems.
We don't use `--wait` or `--pipe`, which are the newer arguments not
found on all systems.

Remove the unnecessary `whitelist-environment` checks, as `pam_systemd`
passes in the `XDG_SESSION_CLASS` and `XDG_SESSION_TYPE` variables for
us automatically from the calling process.

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

Copilot AI 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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@olivercalder olivercalder changed the title Add tests session create destroy session tests: add tests.session create-session and destroy-session Aug 17, 2026
@olivercalder olivercalder changed the title tests: add tests.session create-session and destroy-session tests: add tests.session create-session/destroy-session Aug 17, 2026
@olivercalder olivercalder reopened this Aug 17, 2026
@codecov

codecov Bot commented Aug 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.82%. Comparing base (55b15bd) to head (9218dc0).
⚠️ Report is 8 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #17469      +/-   ##
==========================================
+ Coverage   78.75%   78.82%   +0.07%     
==========================================
  Files        1404     1408       +4     
  Lines      197794   198088     +294     
  Branches     2502     2502              
==========================================
+ Hits       155773   156148     +375     
+ Misses      32651    32555      -96     
- Partials     9370     9385      +15     
Flag Coverage Δ
unittests 78.82% <ø> (+0.07%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions

Copy link
Copy Markdown

Tue Aug 18 14:33:04 UTC 2026
The following results are from: https://github.com/canonical/snapd/actions/runs/32069000161

Test Predictor Analysis

Preparing

Test Success %
openstack:ubuntu-core-20-64:tests/main/interfaces-content-empty-content-attr 🟢 99.0%

Executing

Test Success %
openstack:arch-linux-64:tests/main/systemd-success-exit-status 🟢 99.0%
openstack:ubuntu-20.04-64:tests/main/degraded 🟢 99.0%
openstack:ubuntu-20.04-64:tests/main/snap-connectivity-check 🟢 99.0%
openstack:ubuntu-26.04-64:tests/main/auto-refresh-pre-download:ignore 🔴 5.5%
openstack:ubuntu-26.04-64:tests/main/default-tracks 🟢 99.0%
openstack:ubuntu-26.04-64:tests/unit/go:static 🔴 0.8%
openstack:ubuntu-core-18-64:tests/core/services 🟢 99.0%

Skipped tests from snapd-testing-skip

If you wish to have any of the below tests run in your PR, in your PR description, add 'unskip:' followed by a copy-and-pasted list of the below tests you wish to run (unskip plus test list must be valid yaml)

  • openstack:ubuntu-24.04-64:tests/main/i18n
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-flag-restart
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-integration-tests
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-prompt-restoration
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-smoke
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-snapd-startup
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-support
  • openstack:ubuntu-26.04-64:tests/main/i18n
  • openstack:ubuntu-26.04-64:tests/main/interfaces-requests-activates-handlers


systemctl stop "$unit_name" || true
systemctl reset-failed "$unit_name" || true
rm -f "$id_file"

@bboozzoo bboozzoo Aug 18, 2026

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.

should we confirm somewhere that loginctl no longer lists the session with our ID?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good idea!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The spread test covers this. I've updated it to improve it more.

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>
@olivercalder
olivercalder requested review from bboozzoo and a balanced review from Copilot August 18, 2026 18:40

Copilot AI 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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Suppressed comments (3)

tests/lib/tools/tests.session:274

  • This encoding is ambiguous: user foo-bar without a label and user foo with label bar both map to tests.session-persist-foo-bar.service and the same ID file. The second create can therefore report a false conflict, and destroy can stop another user's session. Use an unambiguous user key such as the numeric UID and apply the same encoding in destroy-session.
			unit_name="tests.session-persist-$user$session_suffix.service"
			id_file="/run/tests.session/$user$session_suffix.id"

tests/lib/tools/tests.session:321

  • echo "$XDG_SESSION_ID" writes a newline even when pam_systemd did not create a session, so test -s succeeds with an empty ID. When class and type are omitted, no later loginctl call detects this and create-session incorrectly reports success. Retry on a non-whitespace ID and clean up the transient unit if readiness times out.
			retry -n 10 --wait 1 test -s "$id_file"
			session_id="$(cat "$id_file")"

tests/lib/tools/suite/tests.session/task.yaml:107

  • The new -t behavior is not exercised by this suite because every create call omits it, so regressions in propagating or verifying XDG_SESSION_TYPE would pass. Include a portable type in the initial creation; the implementation's own verification will then make this path observable.
        tests.session create-session -u "$USER" -c user


exit 0
;;
create-session)
…prove testing

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>
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