-
Notifications
You must be signed in to change notification settings - Fork 680
snapenv: Include snap application environment state #16016
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,19 +49,25 @@ var userCurrent = user.Current | |
| // | ||
| // It ensures all SNAP_* override any pre-existing environment | ||
| // variables. | ||
| func ExtendEnvForRun(env osutil.Environment, info *snap.Info, component *snap.ComponentInfo, opts *dirs.SnapDirOptions) { | ||
| func ExtendEnvForRun(env osutil.Environment, info *snap.Info, app *snap.AppInfo, component *snap.ComponentInfo, opts *dirs.SnapDirOptions) { | ||
| // Set various SNAP_ environment variables as well as some non-SNAP variables, | ||
| // depending on snap confinement mode. Note that this does not include environment | ||
| // set by snap-exec. | ||
| for k, v := range snapEnv(info, component, opts) { | ||
| for k, v := range snapEnv(info, app, component, opts) { | ||
| env[k] = v | ||
| } | ||
| } | ||
|
|
||
| func snapEnv(info *snap.Info, component *snap.ComponentInfo, opts *dirs.SnapDirOptions) osutil.Environment { | ||
| func snapEnv(info *snap.Info, app *snap.AppInfo, component *snap.ComponentInfo, opts *dirs.SnapDirOptions) osutil.Environment { | ||
| // Environment variables with basic properties of a snap. | ||
| env := basicEnv(info) | ||
|
|
||
| if app != nil { | ||
| for k, v := range appEnv(info, app) { | ||
| env[k] = v | ||
| } | ||
| } | ||
|
|
||
| if component != nil { | ||
| for k, v := range componentEnv(info, component) { | ||
| env[k] = v | ||
|
|
@@ -137,6 +143,26 @@ func basicEnv(info *snap.Info) osutil.Environment { | |
| logger.Noticef("cannot determine existence of save data directory for snap %q: %v", | ||
| info.InstanceName(), err) | ||
| } | ||
|
|
||
| return env | ||
| } | ||
|
|
||
| // appEnv returns the app-level environment variables for a snap. | ||
| func appEnv(info *snap.Info, app *snap.AppInfo) osutil.Environment { | ||
| env := osutil.Environment{ | ||
| "SNAP_APP_NAME": app.Name, | ||
|
zyga marked this conversation as resolved.
|
||
| } | ||
|
|
||
| if app.CommonID != "" { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is all very welcome. We must document it somewhere. |
||
| env["SNAP_APP_COMMON_ID"] = app.CommonID | ||
| } | ||
| if df := app.DesktopFile(); df != "" && osutil.FileExists(df) { | ||
| env["SNAP_APP_DESKTOP_FILE"] = df | ||
| } | ||
| if app.BusName != "" { | ||
| env["SNAP_APP_BUS_NAME"] = app.BusName | ||
| } | ||
|
|
||
| return env | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ prepare: | | |
| snap set system experimental.parallel-instances=true | ||
| fi | ||
| "$TESTSTOOLS"/snaps-state install-local-as test-snapd-tools "$NAME" | ||
| "$TESTSTOOLS"/snaps-state install-local test-snapd-desktop-file-ids | ||
|
|
||
| restore: | | ||
| if [[ "$SPREAD_VARIANT" == "parallel" ]]; then | ||
|
|
@@ -57,6 +58,7 @@ execute: | | |
| MATCH "^SNAP_CONTEXT=$CTX" < snap-vars.txt | ||
| # parallel-installs: $SNAP_NAME is always _the_ snap name | ||
| MATCH '^SNAP_NAME=test-snapd-tools$' < snap-vars.txt | ||
| MATCH '^SNAP_APP_NAME=env$' < snap-vars.txt | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be nice to test a snap which has all the new variables set.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eh... Could you hep with that? Not being able to run the spread tests myself makes it a bit annoying to do...
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can run them with
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| # parallel-install: name of a particular instance | ||
| MATCH "^SNAP_INSTANCE_NAME=$NAME$" < snap-vars.txt | ||
| # parallel-installs: empty if none is set | ||
|
|
@@ -67,11 +69,11 @@ execute: | | |
| # if on UC20+, then we should see an additional variable (SNAP_SAVE_DATA) | ||
| if [[ "$SPREAD_SYSTEM" == ubuntu-core-2* ]]; then | ||
| MATCH "^SNAP_SAVE_DATA=/var/lib/snapd/save/snap/$NAME$" < snap-vars.txt | ||
| # 18 variables are expected on ubuntu-core | ||
| test "$(wc -l < snap-vars.txt)" -eq 18 || { cat snap-vars.txt; exit 1; } | ||
| # 19 variables are expected on ubuntu-core | ||
| test "$(wc -l < snap-vars.txt)" -eq 19 || { cat snap-vars.txt; exit 1; } | ||
| else | ||
| # 17 variables are expected on non ubuntu-core | ||
| test "$(wc -l < snap-vars.txt)" -eq 17 || { cat snap-vars.txt; exit 1; } | ||
| # 18 variables are expected on non ubuntu-core | ||
| test "$(wc -l < snap-vars.txt)" -eq 18 || { cat snap-vars.txt; exit 1; } | ||
| fi | ||
|
|
||
| echo "Ensure that XDG environment variables are what we expect" | ||
|
|
@@ -99,3 +101,19 @@ execute: | | |
| # parallel-installs: $HOME is set to instance specific path | ||
| MATCH "^HOME=/root/snap/$NAME/x1$" < misc-vars.txt | ||
| test "$(wc -l < misc-vars.txt)" -eq 4 || { cat misc-vars.txt; exit 1; } | ||
|
|
||
| echo "Collect SNAP environment variables for snap with desktop metadata" | ||
| echo env | snap run --shell test-snapd-desktop-file-ids.cmd | grep -E '^SNAP_' | sort > snap-vars-desktop.txt | ||
| echo "Ensure that SNAP environment variables are what we expect" | ||
| MATCH '^SNAP_NAME=test-snapd-desktop-file-ids$' < snap-vars-desktop.txt | ||
| MATCH '^SNAP_APP_NAME=cmd$' < snap-vars-desktop.txt | ||
| MATCH '^SNAP_APP_COMMON_ID=org.example.Foo$' < snap-vars-desktop.txt | ||
| MATCH '^SNAP_APP_DESKTOP_FILE=/var/lib/snapd/desktop/applications/org.example.Foo.desktop$' < snap-vars-desktop.txt | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAICT there are no existing spread tests which check the bus name, except a few use python scripts, which seems a stretch to use here. But we can at least check the other new desktop metadata. |
||
| # if on UC20+, then we should see an additional variable (SNAP_SAVE_DATA) | ||
| if [[ "$SPREAD_SYSTEM" == ubuntu-core-2* ]]; then | ||
| # 21 variables are expected on ubuntu-core | ||
| test "$(wc -l < snap-vars-desktop.txt)" -eq 21 || { cat snap-vars-desktop.txt; exit 1; } | ||
| else | ||
| # 20 variables are expected on non ubuntu-core | ||
| test "$(wc -l < snap-vars-desktop.txt)" -eq 20 || { cat snap-vars-desktop.txt; exit 1; } | ||
| fi | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does it make sense to have similar information for hooks?
looks like we could have
SNAP_HOOK=<hook-name>There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, look like they could be useful too, although if an hook is running I assume it know that it knows what its own name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in theory the same applies to apps 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well... Not much, in the sense that while hooks are designed to be inside a snap, an application can be embedded into anyof snap