Skip to content

Commit

Permalink
o/snapstate: remove component files in mount-component when installin…
Browse files Browse the repository at this point in the history
…g from ephemeral files
  • Loading branch information
andrewphelpsj committed Jun 25, 2024
1 parent 49149b5 commit 03f2877
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
10 changes: 10 additions & 0 deletions overlord/snapstate/handlers_components.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package snapstate
import (
"errors"
"fmt"
"os"
"time"

"github.com/snapcore/snapd/logger"
Expand Down Expand Up @@ -291,6 +292,15 @@ func (m *SnapManager) doMountComponent(t *state.Task, _ *tomb.Tomb) (err error)
perfTimings.Save(st)
st.Unlock()

// if we're removing the snap file and we are mounting a component for the
// firs time, then we know that the component also must be coming from an
// emphemeral file. in that case, remove it.
if snapsup.RemoveSnapPath {
if err := os.Remove(compSetup.CompPath); err != nil {
return err
}
}

return nil
}

Expand Down
44 changes: 37 additions & 7 deletions overlord/snapstate/snapstate_install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6580,47 +6580,62 @@ func (s *snapmgrTestSuite) TestInstallComponentsFromPathNoneRunThrough(c *C) {
undo = false
snapName = "test-snap"
instanceKey = ""
removePaths = false
)
s.testInstallComponentsFromPathRunThrough(c, snapName, instanceKey, nil, undo)
s.testInstallComponentsFromPathRunThrough(c, snapName, instanceKey, nil, undo, removePaths)
}

func (s *snapmgrTestSuite) TestInstallComponentsFromPathOneRunThrough(c *C) {
const (
undo = false
snapName = "test-snap"
instanceKey = ""
removePaths = false
)
s.testInstallComponentsFromPathRunThrough(c, snapName, instanceKey, []string{"test-component"}, undo)
s.testInstallComponentsFromPathRunThrough(c, snapName, instanceKey, []string{"test-component"}, undo, removePaths)
}

func (s *snapmgrTestSuite) TestInstallComponentsFromPathManyRunThrough(c *C) {
const (
undo = false
snapName = "test-snap"
instanceKey = ""
removePaths = false
)
s.testInstallComponentsFromPathRunThrough(c, snapName, instanceKey, []string{"test-component", "kernel-modules-component"}, undo)
s.testInstallComponentsFromPathRunThrough(c, snapName, instanceKey, []string{"test-component", "kernel-modules-component"}, undo, removePaths)
}

func (s *snapmgrTestSuite) TestInstallComponentsFromPathManyInstanceRunThrough(c *C) {
const (
undo = false
snapName = "test-snap"
instanceKey = "key"
removePaths = false
)
s.testInstallComponentsFromPathRunThrough(c, snapName, instanceKey, []string{"test-component", "kernel-modules-component"}, undo)
s.testInstallComponentsFromPathRunThrough(c, snapName, instanceKey, []string{"test-component", "kernel-modules-component"}, undo, removePaths)
}

func (s *snapmgrTestSuite) TestInstallComponentsFromPathInstanceRunThroughUndo(c *C) {
const (
undo = true
snapName = "test-snap"
instanceKey = "key"
removePaths = false
)
s.testInstallComponentsFromPathRunThrough(c, snapName, instanceKey, []string{"test-component", "kernel-modules-component"}, undo)
s.testInstallComponentsFromPathRunThrough(c, snapName, instanceKey, []string{"test-component", "kernel-modules-component"}, undo, removePaths)
}

func (s *snapmgrTestSuite) testInstallComponentsFromPathRunThrough(c *C, snapName, instanceKey string, compNames []string, undo bool) {
func (s *snapmgrTestSuite) TestInstallComponentsFromPathManyRemovePaths(c *C) {
const (
undo = false
snapName = "test-snap"
instanceKey = ""
removePaths = true
)
s.testInstallComponentsFromPathRunThrough(c, snapName, instanceKey, []string{"test-component", "kernel-modules-component"}, undo, removePaths)
}

func (s *snapmgrTestSuite) testInstallComponentsFromPathRunThrough(c *C, snapName, instanceKey string, compNames []string, undo bool, removePaths bool) {
s.state.Lock()
defer s.state.Unlock()

Expand Down Expand Up @@ -6692,7 +6707,10 @@ components:
goal := snapstate.PathInstallGoal(instanceName, snapPath, si, components, snapstate.RevisionOptions{})

info, ts, err := snapstate.InstallOne(context.Background(), s.state, goal, snapstate.Options{
Flags: snapstate.Flags{Required: true},
Flags: snapstate.Flags{
Required: true,
RemoveSnapPath: removePaths,
},
})
c.Assert(err, IsNil)
c.Check(info.InstanceName(), Equals, instanceName)
Expand Down Expand Up @@ -6828,6 +6846,18 @@ components:

// make sure that all of our components are accounted for
c.Assert(snapst.Sequence.Revisions[0].Components, DeepEquals, compst)

// if we requested that the snap be removed after install, then make
// sure it is gone. otherwise, make sure that it is still there
fileChecker := testutil.FilePresent
if removePaths {
fileChecker = testutil.FileAbsent
}

c.Check(snapPath, fileChecker)
for _, compPath := range components {
c.Check(compPath, fileChecker)
}
}
}

Expand Down

0 comments on commit 03f2877

Please sign in to comment.