Skip to content

Commit b8d3586

Browse files
o/snapstate: set UserID for download-component task
1 parent 4620040 commit b8d3586

3 files changed

Lines changed: 74 additions & 7 deletions

File tree

overlord/snapstate/component.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ func InstallComponents(
109109
return nil, err
110110
}
111111

112+
snapUserID, err := userIDForSnap(st, &snapst, opts.UserID)
113+
if err != nil {
114+
return nil, err
115+
}
116+
112117
snapsup := SnapSetup{
113118
Base: info.Base,
114119
SideInfo: &info.SideInfo,
@@ -118,6 +123,7 @@ func InstallComponents(
118123
Version: info.Version,
119124
PlugsOnly: len(info.Slots) == 0,
120125
InstanceKey: info.InstanceKey,
126+
UserID: snapUserID,
121127
ComponentExclusiveOperation: true,
122128
}
123129

overlord/snapstate/component_install_test.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -975,9 +975,30 @@ func (s *snapmgrTestSuite) TestInstallComponentsTransactionPerSnap(c *C) {
975975
})
976976
}
977977

978+
func (s *snapmgrTestSuite) TestInstallComponentsWithSnapStateAndInstallOptsUserIDs(c *C) {
979+
s.testInstallComponents(c, testInstallComponentsOpts{
980+
userIDSnapState: s.user.ID,
981+
userIDInstallOpts: s.user2.ID,
982+
})
983+
}
984+
985+
func (s *snapmgrTestSuite) TestInstallComponentsWithUserIDSnapState(c *C) {
986+
s.testInstallComponents(c, testInstallComponentsOpts{
987+
userIDSnapState: s.user2.ID,
988+
})
989+
}
990+
991+
func (s *snapmgrTestSuite) TestInstallComponentsWithUserIDInstallOpts(c *C) {
992+
s.testInstallComponents(c, testInstallComponentsOpts{
993+
userIDInstallOpts: s.user.ID,
994+
})
995+
}
996+
978997
type testInstallComponentsOpts struct {
979-
lane int
980-
transaction client.TransactionType
998+
lane int
999+
transaction client.TransactionType
1000+
userIDInstallOpts int
1001+
userIDSnapState int
9811002
}
9821003

9831004
func (s *snapmgrTestSuite) testInstallComponents(c *C, opts testInstallComponentsOpts) {
@@ -1007,6 +1028,7 @@ func (s *snapmgrTestSuite) testInstallComponents(c *C, opts testInstallComponent
10071028
}),
10081029
Current: snapRev,
10091030
TrackingChannel: "channel-for-components",
1031+
UserID: opts.userIDSnapState,
10101032
})
10111033

10121034
components := []string{"standard-component", "kernel-modules-component"}
@@ -1038,6 +1060,7 @@ func (s *snapmgrTestSuite) testInstallComponents(c *C, opts testInstallComponent
10381060
}
10391061

10401062
installOpts := snapstate.Options{
1063+
UserID: opts.userIDInstallOpts,
10411064
Flags: snapstate.Flags{
10421065
Lane: opts.lane,
10431066
Transaction: opts.transaction,
@@ -1052,6 +1075,16 @@ func (s *snapmgrTestSuite) testInstallComponents(c *C, opts testInstallComponent
10521075
setupProfiles := setupTs.Tasks()[0]
10531076
c.Assert(setupProfiles.Kind(), Equals, "setup-profiles")
10541077

1078+
snapsupSetupProfiles, err := snapstate.TaskSnapSetup(setupProfiles)
1079+
c.Assert(err, IsNil)
1080+
var expectedUserID int
1081+
if opts.userIDSnapState != 0 {
1082+
expectedUserID = opts.userIDSnapState
1083+
} else {
1084+
expectedUserID = opts.userIDInstallOpts
1085+
}
1086+
c.Assert(snapsupSetupProfiles.UserID, Equals, expectedUserID)
1087+
10551088
prepareKmodComps := setupTs.Tasks()[1]
10561089
c.Assert(prepareKmodComps.Kind(), Equals, "prepare-kernel-modules-components")
10571090

overlord/snapstate/handlers_components_download_test.go

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,16 @@ func (s *downloadComponentSuite) TestDoDownloadComponentCustomBlobDir(c *C) {
6565
})
6666
}
6767

68+
func (s *downloadComponentSuite) TestDoDownloadComponentWithUserAuth(c *C) {
69+
s.testDoDownloadComponent(c, testDoDownloadComponentOpts{
70+
withUserAuth: true,
71+
})
72+
}
73+
6874
type testDoDownloadComponentOpts struct {
69-
autoRefresh bool
70-
blobDir string
75+
autoRefresh bool
76+
blobDir string
77+
withUserAuth bool
7178
}
7279

7380
func (s *downloadComponentSuite) testDoDownloadComponent(c *C, opts testDoDownloadComponentOpts) {
@@ -79,6 +86,20 @@ func (s *downloadComponentSuite) testDoDownloadComponent(c *C, opts testDoDownlo
7986
tr.Set("core", "refresh.rate-limit", "1234B")
8087
tr.Commit()
8188

89+
var userID int
90+
var user *auth.UserState
91+
if opts.withUserAuth {
92+
var err error
93+
user, err = auth.NewUser(s.state, auth.NewUserParams{
94+
Username: "username",
95+
Email: "email@test.com",
96+
Macaroon: "user-macaroon",
97+
Discharges: []string{"discharge"},
98+
})
99+
c.Assert(err, IsNil)
100+
userID = user.ID
101+
}
102+
82103
si := &snap.SideInfo{
83104
RealName: "snap",
84105
SnapID: snaptest.AssertedSnapID("snap"),
@@ -93,6 +114,7 @@ func (s *downloadComponentSuite) testDoDownloadComponent(c *C, opts testDoDownlo
93114
InstanceKey: "key",
94115
Flags: snapstate.Flags{IsAutoRefresh: opts.autoRefresh},
95116
DownloadBlobDir: opts.blobDir,
117+
UserID: userID,
96118
})
97119

98120
t.Set("component-setup", &snapstate.ComponentSetup{
@@ -148,11 +170,17 @@ func (s *downloadComponentSuite) testDoDownloadComponent(c *C, opts testDoDownlo
148170
}
149171
}
150172

173+
var expectedMacaroon string
174+
if opts.withUserAuth {
175+
expectedMacaroon = user.StoreMacaroon
176+
}
177+
151178
c.Check(s.fakeStore.downloads, DeepEquals, []fakeDownload{
152179
{
153-
name: "snap+comp",
154-
target: expectedPath,
155-
opts: downloadOpts,
180+
name: "snap+comp",
181+
target: expectedPath,
182+
opts: downloadOpts,
183+
macaroon: expectedMacaroon,
156184
},
157185
})
158186
}

0 commit comments

Comments
 (0)