Skip to content

Commit

Permalink
daemon: attach refresh failure information to /v2/snaps
Browse files Browse the repository at this point in the history
Signed-off-by: Zeyad Gouda <[email protected]>
  • Loading branch information
ZeyadYasser committed Oct 7, 2024
1 parent 095fb60 commit 2ab76bc
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions client/clientutil/snapinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func (*cmdSuite) TestClientSnapFromSnapInfo(c *C) {
"Hold",
"GatingHold",
"RefreshInhibit",
"RefreshFailures",
"Components",
}
var checker func(string, reflect.Value)
Expand Down
2 changes: 2 additions & 0 deletions client/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ type Snap struct {
GatingHold *time.Time `json:"gating-hold,omitempty"`
// if RefreshInhibit is nil, then there is no pending refresh.
RefreshInhibit *SnapRefreshInhibit `json:"refresh-inhibit,omitempty"`
// RefreshFailures tracks information about snap failed refreshes.
RefreshFailures *snap.RefreshFailuresInfo `json:"refresh-failures,omitempty"`

// Components is a list of the snap components
Components []Component `json:"components,omitempty"`
Expand Down
10 changes: 10 additions & 0 deletions client/packages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,11 @@ func (cs *clientSuite) testClientSnap(c *check.C, refreshInhibited bool) {
"private": true,
"devmode": true,
"trymode": true,
"refresh-failures": {
"to-revision": 43,
"failure-count": 5,
"last-failure-time": "2024-10-06T21:31:05Z"
},
"screenshots": [
{"url":"http://example.com/shot1.png", "width":640, "height":480},
{"url":"http://example.com/shot2.png"}
Expand Down Expand Up @@ -356,6 +361,11 @@ func (cs *clientSuite) testClientSnap(c *check.C, refreshInhibited bool) {
Website: "http://example.com/funky",
StoreURL: "https://snapcraft.io/chatroom",
RefreshInhibit: expectedSnapRefreshInhibit,
RefreshFailures: &snap.RefreshFailuresInfo{
ToRevision: snap.R(43),
FailureCount: 5,
LastFailureTime: time.Date(2024, 10, 6, 21, 31, 5, 0, time.UTC),
},
})
}

Expand Down
32 changes: 32 additions & 0 deletions daemon/api_snaps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1578,6 +1578,38 @@ func (s *snapsSuite) TestSnapManyInfosSelectRefreshInhibited(c *check.C) {
}
}

func (s *snapsSuite) TestSnapInfoReturnsRefreshFailures(c *check.C) {
s.expectSnapsNameReadAccess()
d := s.daemon(c)
s.mkInstalledInState(c, d, "foo", "bar", "v0", snap.R(5), true, "")

expectedRefreshFailures := &snap.RefreshFailuresInfo{
ToRevision: snap.R(6),
FailureCount: 4,
LastFailureTime: time.Date(2024, time.October, 10, 21, 22, 11, 0, time.UTC),
}

st := d.Overlord().State()
st.Lock()
var snapst snapstate.SnapState
// Update snap state with RefreshFailure.
c.Assert(snapstate.Get(st, "foo", &snapst), check.IsNil)
snapst.RefreshFailures = expectedRefreshFailures
snapstate.Set(st, "foo", &snapst)
st.Unlock()

req, err := http.NewRequest("GET", "/v2/snaps/foo", nil)
c.Assert(err, check.IsNil)

rsp := s.syncReq(c, req, nil)

c.Assert(rsp.Result, check.FitsTypeOf, &client.Snap{})
snapInfo := rsp.Result.(*client.Snap)
c.Assert(snapInfo.RefreshFailures, check.NotNil)

c.Check(snapInfo.RefreshFailures, check.DeepEquals, expectedRefreshFailures)
}

func (s *snapsSuite) TestMapLocalFields(c *check.C) {
media := snap.MediaInfos{
{
Expand Down
1 change: 1 addition & 0 deletions daemon/snap.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ func mapLocal(about aboutSnap, sd clientutil.StatusDecorator) *client.Snap {
result.DevMode = snapst.DevMode
result.TryMode = snapst.TryMode
result.JailMode = snapst.JailMode
result.RefreshFailures = snapst.RefreshFailures
result.MountedFrom = localSnap.MountFile()
if result.TryMode {
// Readlink instead of EvalSymlinks because it's only expected
Expand Down

0 comments on commit 2ab76bc

Please sign in to comment.