Skip to content

Commit

Permalink
tests: test fresh install of core22-based snap (#11696)
Browse files Browse the repository at this point in the history
* test fresh install of core22-based snap
* remove 'common' in root's snap user dir
* test a core22->core22 refresh
* remove write to 'common' in remove hook's test
* expand env var checks related to ~/Snap

Signed-off-by: Miguel Pires <[email protected]>
  • Loading branch information
miguelpires authored Apr 25, 2022
1 parent 992c910 commit 66b1013
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 20 deletions.
4 changes: 4 additions & 0 deletions overlord/snapstate/backend/snapdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ func snapCommonDataDirs(snap *snap.Info, opts *dirs.SnapDirOptions) ([]string, e
return nil, err
}

// then the root user's common data dir
rootCommon := snap.UserCommonDataDir(filepath.Join(dirs.GlobalRootDir, "/root/"), opts)
found = append(found, rootCommon)

// then XDG_RUNTIME_DIRs for the users
foundXdg, err := filepath.Glob(snap.XdgRuntimeDirs())
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions overlord/snapstate/backend/snapdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ func (s *snapdataSuite) TestRemoveSnapCommonData(c *C) {
err = os.MkdirAll(varCommonData, 0755)
c.Assert(err, IsNil)

rootCommonDir := filepath.Join(s.tempdir, "root", "snap", "hello", "common")
c.Assert(os.MkdirAll(rootCommonDir, 0700), IsNil)

info := snaptest.MockSnap(c, helloYaml1, &snap.SideInfo{Revision: snap.R(10)})

err = s.be.RemoveSnapCommonData(info, nil)
Expand All @@ -84,6 +87,7 @@ func (s *snapdataSuite) TestRemoveSnapCommonData(c *C) {
c.Assert(osutil.FileExists(filepath.Dir(homeCommonData)), Equals, true)
c.Assert(osutil.FileExists(varCommonData), Equals, false)
c.Assert(osutil.FileExists(filepath.Dir(varCommonData)), Equals, true)
c.Assert(osutil.FileExists(rootCommonDir), Equals, false)
}

func (s *snapdataSuite) TestRemoveSnapDataDir(c *C) {
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/snaps/snap-hooks/meta/hooks/remove
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/sh

RETVAL=$(snapctl get exitcode)
echo "$RETVAL" > "$SNAP_USER_COMMON/remove-hook-executed"
echo "$RETVAL" > /tmp/remove-hook-executed
exit "$RETVAL"
77 changes: 60 additions & 17 deletions tests/main/hidden-snap-dir/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ execute: |
if [ "$CHECK_EXPOSED_HOME" = "yes" ]; then
echo "Checking core22 migration related env vars"
echo "$snapEnv" | MATCH "HOME=$HOME/Snap"
echo "$snapEnv" | MATCH "SNAP_USER_HOME=$HOME/Snap"
echo "$snapEnv" | MATCH "HOME=$HOME/Snap/$NAME"
echo "$snapEnv" | MATCH "SNAP_USER_HOME=$HOME/Snap/$NAME"
echo "$snapEnv" | MATCH "XDG_DATA_HOME=$HOME/\.snap/data/$NAME/$REV/xdg-data"
echo "$snapEnv" | MATCH "XDG_CACHE_HOME=$HOME/\.snap/data/$NAME/$REV/xdg-cache"
echo "$snapEnv" | MATCH "XDG_CONFIG_HOME=$HOME/\.snap/data/$NAME/$REV/xdg-config"
Expand Down Expand Up @@ -118,19 +118,19 @@ execute: |
"$TESTSTOOLS"/snaps-state install-local "$NAME"
# Check env vars, dirs and data after the migration
check_env "x2"
check_env x2
# Note: some dirs are created just before the snap runs for the 1st time,
# so this check must come after a snap run
check_dirs "x2" ""
check_data "x2" "$data"
check_dirs x2
check_data x2 "$data"
echo "Check the snap can write to the new dirs"
#shellcheck disable=SC2016
"$NAME".cmd sh -c 'echo "new_data" > "$SNAP_USER_DATA"/file'
#shellcheck disable=SC2016
"$NAME".cmd sh -c 'echo "new_data" > "$SNAP_USER_COMMON"/file'
check_data "x2" "new_data"
check_data x2 new_data
echo "Restore snapshot and check data was restored"
snap restore "$snapshot"
Expand All @@ -140,13 +140,13 @@ execute: |
snap remove --purge "$NAME"
"$TESTSTOOLS"/snaps-state install-local "$NAME"
check_env "x1"
check_dirs "x1"
check_env x1
check_dirs x1
data="new_data"
"$NAME".echo "$data" > "$HOME/.snap/data/$NAME/x1/file"
"$NAME".echo "$data" > "$HOME/.snap/data/$NAME/common/file"
check_data "x1" "$data"
check_data x1 "$data"
echo "Revert migration (unset flag and refresh)"
snap unset system experimental.hidden-snap-folder
Expand Down Expand Up @@ -185,9 +185,9 @@ execute: |
snap pack "$PWD/$NAME"
snap install --dangerous "$NAME"_1.0_all.snap
check_env "--with-exposed-home" "x3"
check_dirs "--with-exposed-home" "x3"
check_data "--with-exposed-home" "x3" "$data"
check_env --with-exposed-home x3
check_dirs --with-exposed-home x3
check_data --with-exposed-home x3 "$data"
# the XDG dirs shouldn't be copied to the new HOME (they stay under the rev dir)
not test -d "$HOME/Snap/$NAME/.config"
Expand Down Expand Up @@ -231,9 +231,9 @@ execute: |
snap revert "$NAME" --revision="x3"
# check everything is restored after reverting back to x3
check_env "--with-exposed-home" "x3"
check_dirs "--with-exposed-home" "x3"
check_data "--with-exposed-home" "x3" "x3"
check_env --with-exposed-home x3
check_dirs --with-exposed-home x3
check_data --with-exposed-home x3 x3
# write something to the new XDG dirs so we can check that a refresh always
# re-initializes them from the default XDG (even if refreshing after a revert)
Expand Down Expand Up @@ -279,8 +279,8 @@ execute: |
snap install --dangerous "$NAME"_1.0_all.snap
check_env "--with-exposed-home" "x4"
check_dirs "--with-exposed-home" "x4"
check_env --with-exposed-home x4
check_dirs --with-exposed-home x4
MATCH "$data" < "$HOME/.snap/data/$NAME/x4/file"
# data under ~/Snap isn't rewritten
Expand Down Expand Up @@ -324,3 +324,46 @@ execute: |
test -d "$HOME/.snap/data/$NAME"
not test -e "$HOME/snap/$NAME"
echo "Refresh from core22 base to another core22 base revision"
# reset everything
snap unset system experimental.hidden-snap-folder
snap remove --purge "$NAME"
rm -rf ~/.snap ~/snap ~/Snap
snap pack "$PWD/$NAME"
snap install --dangerous "$NAME"_1.0_all.snap
data="fresh"
#shellcheck disable=SC2016
"$NAME".cmd sh -c 'echo fresh > "$SNAP_USER_DATA/file"'
#shellcheck disable=SC2016
"$NAME".cmd sh -c 'echo fresh > "$SNAP_USER_COMMON/file"'
#shellcheck disable=SC2016
"$NAME".cmd sh -c 'echo fresh > "$HOME/file"'
snap install --dangerous "$NAME"_1.0_all.snap
check_env --with-exposed-home x2
check_dirs --with-exposed-home x2
check_data --with-exposed-home x2 "$data"
echo "Check fresh install of core22 based snap"
snap remove --purge "$NAME"
rm -rf "$HOME"/Snap "$HOME"/.snap/data "$HOME/snap/$NAME"
snap install --dangerous "$NAME"_1.0_all.snap
check_env --with-exposed-home x1
check_dirs --with-exposed-home x1
"$NAME".cmd sh -c 'true'
echo "Check remove of freshly installed core22 based snap"
snap remove --purge "$NAME"
not test -e "$HOME/.snap/data/$NAME/x1"
not test -e "$HOME/.snap/data/$NAME/common"
echo "Now re-install"
snap install --dangerous "$NAME"_1.0_all.snap
"$NAME".cmd sh -c 'true'
check_env --with-exposed-home x1
check_dirs --with-exposed-home x1
4 changes: 2 additions & 2 deletions tests/main/install-refresh-remove-hooks/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ summary: Check install, configure, remove and pre-refresh/post-refresh hooks.
backends: [-autopkgtest]

environment:
REMOVE_HOOK_FILE/regular: "$HOME/snap/snap-hooks/common/remove-hook-executed"
REMOVE_HOOK_FILE/parallel: "$HOME/snap/snap-hooks_instance/common/remove-hook-executed"
REMOVE_HOOK_FILE/regular: "/tmp/snap.snap-hooks/tmp/remove-hook-executed"
REMOVE_HOOK_FILE/parallel: "/tmp/snap.snap-hooks_instance/tmp/remove-hook-executed"
NAME/regular: snap-hooks
NAME/parallel: snap-hooks_instance

Expand Down

0 comments on commit 66b1013

Please sign in to comment.