Skip to content

Commit

Permalink
many: add components side info to snapstate
Browse files Browse the repository at this point in the history
Add components information to the snap state. This is included in the
Sequence variable, so we need to handle in a special way the
marshaling to keep compatibility with state files created by older
snapd versions.

The change to the Sequence field implies multiple changes in tests
that use it. Ancillary methods to handle this have been created.
  • Loading branch information
alfonsosanchezbeato committed Dec 2, 2023
1 parent d58cd19 commit 768021b
Show file tree
Hide file tree
Showing 71 changed files with 1,691 additions and 1,499 deletions.
9 changes: 5 additions & 4 deletions daemon/api_aliases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/overlord/snapstate"
"github.com/snapcore/snapd/overlord/snapstate/snapstatetest"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/snap"
)
Expand Down Expand Up @@ -520,9 +521,9 @@ func (s *aliasesSuite) TestAliases(c *check.C) {
st := d.Overlord().State()
st.Lock()
snapstate.Set(st, "alias-snap1", &snapstate.SnapState{
Sequence: []*snap.SideInfo{
Sequence: snapstatetest.NewSequenceFromSnapSideInfos([]*snap.SideInfo{
{RealName: "alias-snap1", Revision: snap.R(11)},
},
}),
Current: snap.R(11),
Active: true,
Aliases: map[string]*snapstate.AliasTarget{
Expand All @@ -531,9 +532,9 @@ func (s *aliasesSuite) TestAliases(c *check.C) {
},
})
snapstate.Set(st, "alias-snap2", &snapstate.SnapState{
Sequence: []*snap.SideInfo{
Sequence: snapstatetest.NewSequenceFromSnapSideInfos([]*snap.SideInfo{
{RealName: "alias-snap2", Revision: snap.R(12)},
},
}),
Current: snap.R(12),
Active: true,
AutoAliasesDisabled: true,
Expand Down
7 changes: 4 additions & 3 deletions daemon/api_base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
"github.com/snapcore/snapd/overlord/devicestate/devicestatetest"
"github.com/snapcore/snapd/overlord/ifacestate"
"github.com/snapcore/snapd/overlord/snapstate"
"github.com/snapcore/snapd/overlord/snapstate/snapstatetest"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/sandbox"
"github.com/snapcore/snapd/snap"
Expand Down Expand Up @@ -414,13 +415,13 @@ func (s *apiBaseSuite) mockSnap(c *check.C, yamlText string) *snap.Info {
// Put a side info into the state
snapstate.Set(st, snapInfo.InstanceName(), &snapstate.SnapState{
Active: true,
Sequence: []*snap.SideInfo{
Sequence: snapstatetest.NewSequenceFromSnapSideInfos([]*snap.SideInfo{
{
RealName: snapInfo.SnapName(),
Revision: snapInfo.Revision,
SnapID: "ididid",
},
},
}),
Current: snapInfo.Revision,
SnapType: string(snapInfo.Type()),
})
Expand Down Expand Up @@ -481,7 +482,7 @@ version: %s
var snapst snapstate.SnapState
snapstate.Get(st, instanceName, &snapst)
snapst.Active = active
snapst.Sequence = append(snapst.Sequence, &snapInfo.SideInfo)
snapst.Sequence.Revisions = append(snapst.Sequence.Revisions, snapstate.NewRevisionSideInfo(&snapInfo.SideInfo, nil))
snapst.Current = snapInfo.SideInfo.Revision
snapst.TrackingChannel = "stable"
snapst.InstanceKey = instanceKey
Expand Down
4 changes: 2 additions & 2 deletions daemon/api_find_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ func (s *findSuite) TestFindRefreshSideloaded(c *check.C) {
err := snapstate.Get(st, "store", &snapst)
st.Unlock()
c.Assert(err, check.IsNil)
c.Assert(snapst.Sequence, check.HasLen, 1)
c.Assert(snapst.Sequence.Revisions, check.HasLen, 1)

// clear the snapid
snapst.Sequence[0].SnapID = ""
snapst.Sequence.Revisions[0].Snap.SnapID = ""
st.Lock()
snapstate.Set(st, "store", &snapst)
st.Unlock()
Expand Down
2 changes: 1 addition & 1 deletion daemon/api_snap_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (s *snapFileSuite) TestGetFile(c *check.C) {
sideInfo := &snap.SideInfo{Revision: snap.R(-1), RealName: "foo"}
snapst.Active = scen.active
snapst.Current = sideInfo.Revision
snapst.Sequence = append(snapst.Sequence, sideInfo)
snapst.Sequence.Revisions = append(snapst.Sequence.Revisions, snapstate.NewRevisionSideInfo(sideInfo, nil))
if scen.try {
snapst.TryMode = true
}
Expand Down
17 changes: 9 additions & 8 deletions daemon/api_validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/snapcore/snapd/overlord/assertstate/assertstatetest"
"github.com/snapcore/snapd/overlord/auth"
"github.com/snapcore/snapd/overlord/snapstate"
"github.com/snapcore/snapd/overlord/snapstate/snapstatetest"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snap/naming"
Expand Down Expand Up @@ -394,12 +395,12 @@ func (s *apiValidationSetsSuite) TestGetValidationSetLatestFromRemote(c *check.C

snapstate.Set(st, "snap-a", &snapstate.SnapState{
Active: true,
Sequence: []*snap.SideInfo{{RealName: "snap-a", Revision: snap.R(2), SnapID: "snapaid"}},
Sequence: snapstatetest.NewSequenceFromSnapSideInfos([]*snap.SideInfo{{RealName: "snap-a", Revision: snap.R(2), SnapID: "snapaid"}}),
Current: snap.R(2),
})
snapstate.Set(st, "snap-b", &snapstate.SnapState{
Active: true,
Sequence: []*snap.SideInfo{{RealName: "snap-b", Revision: snap.R(4), SnapID: "snapbid"}},
Sequence: snapstatetest.NewSequenceFromSnapSideInfos([]*snap.SideInfo{{RealName: "snap-b", Revision: snap.R(4), SnapID: "snapbid"}}),
Current: snap.R(4),
})

Expand Down Expand Up @@ -462,7 +463,7 @@ func (s *apiValidationSetsSuite) TestGetValidationSetLatestFromRemoteRealValidat
st.Lock()
snapstate.Set(st, "snap-b", &snapstate.SnapState{
Active: true,
Sequence: []*snap.SideInfo{{RealName: "snap-b", Revision: tc.revision, SnapID: "yOqKhntON3vR7kwEbVPsILm7bUViPDzz"}},
Sequence: snapstatetest.NewSequenceFromSnapSideInfos([]*snap.SideInfo{{RealName: "snap-b", Revision: tc.revision, SnapID: "yOqKhntON3vR7kwEbVPsILm7bUViPDzz"}}),
Current: tc.revision,
})
st.Unlock()
Expand Down Expand Up @@ -519,7 +520,7 @@ func (s *apiValidationSetsSuite) TestGetValidationSetSpecificSequenceFromRemote(

snapstate.Set(st, "snap-a", &snapstate.SnapState{
Active: true,
Sequence: []*snap.SideInfo{{RealName: "snap-a", Revision: snap.R(33), SnapID: "snapaid"}},
Sequence: snapstatetest.NewSequenceFromSnapSideInfos([]*snap.SideInfo{{RealName: "snap-a", Revision: snap.R(33), SnapID: "snapaid"}}),
Current: snap.R(33),
})

Expand Down Expand Up @@ -805,7 +806,7 @@ func (s *apiValidationSetsSuite) TestApplyValidationSetEnforceMode(c *check.C) {

snapstate.Set(st, "snap-b", &snapstate.SnapState{
Active: true,
Sequence: []*snap.SideInfo{{RealName: "snap-b", Revision: snap.R(1), SnapID: "yOqKhntON3vR7kwEbVPsILm7bUViPDzz"}},
Sequence: snapstatetest.NewSequenceFromSnapSideInfos([]*snap.SideInfo{{RealName: "snap-b", Revision: snap.R(1), SnapID: "yOqKhntON3vR7kwEbVPsILm7bUViPDzz"}}),
Current: snap.R(1),
})

Expand Down Expand Up @@ -857,7 +858,7 @@ func (s *apiValidationSetsSuite) TestApplyValidationSetEnforceModeIgnoreValidati

snapstate.Set(st, "snap-b", &snapstate.SnapState{
Active: true,
Sequence: []*snap.SideInfo{{RealName: "snap-b", Revision: snap.R(1), SnapID: "yOqKhntON3vR7kwEbVPsILm7bUViPDzz"}},
Sequence: snapstatetest.NewSequenceFromSnapSideInfos([]*snap.SideInfo{{RealName: "snap-b", Revision: snap.R(1), SnapID: "yOqKhntON3vR7kwEbVPsILm7bUViPDzz"}}),
Current: snap.R(1),
Flags: snapstate.Flags{IgnoreValidation: true},
})
Expand Down Expand Up @@ -907,7 +908,7 @@ func (s *apiValidationSetsSuite) TestApplyValidationSetEnforceModeSpecificSequen

snapstate.Set(st, "snap-b", &snapstate.SnapState{
Active: true,
Sequence: []*snap.SideInfo{{RealName: "snap-b", Revision: snap.R(1), SnapID: "yOqKhntON3vR7kwEbVPsILm7bUViPDzz"}},
Sequence: snapstatetest.NewSequenceFromSnapSideInfos([]*snap.SideInfo{{RealName: "snap-b", Revision: snap.R(1), SnapID: "yOqKhntON3vR7kwEbVPsILm7bUViPDzz"}}),
Current: snap.R(1),
})

Expand Down Expand Up @@ -945,7 +946,7 @@ func (s *apiValidationSetsSuite) TestApplyValidationSetEnforceModeError(c *check

snapstate.Set(st, "snap-b", &snapstate.SnapState{
Active: true,
Sequence: []*snap.SideInfo{{RealName: "snap-b", Revision: snap.R(1), SnapID: "yOqKhntON3vR7kwEbVPsILm7bUViPDzz"}},
Sequence: snapstatetest.NewSequenceFromSnapSideInfos([]*snap.SideInfo{{RealName: "snap-b", Revision: snap.R(1), SnapID: "yOqKhntON3vR7kwEbVPsILm7bUViPDzz"}}),
Current: snap.R(1),
})

Expand Down
5 changes: 3 additions & 2 deletions daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import (
"github.com/snapcore/snapd/overlord/patch"
"github.com/snapcore/snapd/overlord/restart"
"github.com/snapcore/snapd/overlord/snapstate"
"github.com/snapcore/snapd/overlord/snapstate/snapstatetest"
"github.com/snapcore/snapd/overlord/standby"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/snap"
Expand Down Expand Up @@ -610,7 +611,7 @@ func (s *daemonSuite) TestStartStop(c *check.C) {
si := &snap.SideInfo{RealName: "core", Revision: snap.R(1), SnapID: "core-snap-id"}
snapstate.Set(st, "core", &snapstate.SnapState{
Active: true,
Sequence: []*snap.SideInfo{si},
Sequence: snapstatetest.NewSequenceFromSnapSideInfos([]*snap.SideInfo{si}),
Current: snap.R(1),
})
st.Unlock()
Expand Down Expand Up @@ -749,7 +750,7 @@ func (s *daemonSuite) TestGracefulStop(c *check.C) {
si := &snap.SideInfo{RealName: "core", Revision: snap.R(1), SnapID: "core-snap-id"}
snapstate.Set(st, "core", &snapstate.SnapState{
Active: true,
Sequence: []*snap.SideInfo{si},
Sequence: snapstatetest.NewSequenceFromSnapSideInfos([]*snap.SideInfo{si}),
Current: snap.R(1),
})
st.Unlock()
Expand Down
8 changes: 4 additions & 4 deletions daemon/snap.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,20 +135,20 @@ func allLocalSnapInfos(st *state.State, all bool, wanted map[string]bool) ([]abo
var aboutThis []aboutSnap
var info *snap.Info
if all {
for _, seq := range snapst.Sequence {
info, err = snap.ReadInfo(name, seq)
for _, si := range snapst.Sequence.SideInfos() {
info, err = snap.ReadInfo(name, si)
if err != nil {
// single revision may be broken
_, instanceKey := snap.SplitInstanceName(name)
info = &snap.Info{
SideInfo: *seq,
SideInfo: *si,
InstanceKey: instanceKey,
Broken: err.Error(),
}
// clear the error
err = nil
}
info.Publisher, err = assertstate.PublisherStoreAccount(st, seq.SnapID)
info.Publisher, err = assertstate.PublisherStoreAccount(st, si.SnapID)
if err != nil {
return nil, err
}
Expand Down
24 changes: 12 additions & 12 deletions overlord/assertstate/assertstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1164,9 +1164,9 @@ func (s *assertMgrSuite) stateFromDecl(c *C, decl *asserts.SnapDeclaration, inst
snapID := decl.SnapID()
snapstate.Set(s.state, instanceName, &snapstate.SnapState{
Active: true,
Sequence: []*snap.SideInfo{
Sequence: snapstatetest.NewSequenceFromSnapSideInfos([]*snap.SideInfo{
{RealName: snapName, SnapID: snapID, Revision: revno},
},
}),
Current: revno,
InstanceKey: instanceKey,
})
Expand Down Expand Up @@ -1291,9 +1291,9 @@ func (s *assertMgrSuite) TestRefreshSnapDeclarationsNoStore(c *C) {
s.stateFromDecl(c, snapDeclBar, "", snap.R(3))
snapstate.Set(s.state, "local", &snapstate.SnapState{
Active: false,
Sequence: []*snap.SideInfo{
Sequence: snapstatetest.NewSequenceFromSnapSideInfos([]*snap.SideInfo{
{RealName: "local", Revision: snap.R(-1)},
},
}),
Current: snap.R(-1),
})

Expand Down Expand Up @@ -2107,9 +2107,9 @@ func (s *assertMgrSuite) TestValidateRefreshesValidationOK(c *C) {
s.stateFromDecl(c, snapDeclBaz, "", snap.R(1))
snapstate.Set(s.state, "local", &snapstate.SnapState{
Active: false,
Sequence: []*snap.SideInfo{
Sequence: snapstatetest.NewSequenceFromSnapSideInfos([]*snap.SideInfo{
{RealName: "local", Revision: snap.R(-1)},
},
}),
Current: snap.R(-1),
})

Expand Down Expand Up @@ -2179,9 +2179,9 @@ func (s *assertMgrSuite) TestValidateRefreshesRevokedValidation(c *C) {
s.stateFromDecl(c, snapDeclBaz, "", snap.R(1))
snapstate.Set(s.state, "local", &snapstate.SnapState{
Active: false,
Sequence: []*snap.SideInfo{
Sequence: snapstatetest.NewSequenceFromSnapSideInfos([]*snap.SideInfo{
{RealName: "local", Revision: snap.R(-1)},
},
}),
Current: snap.R(-1),
})

Expand Down Expand Up @@ -2827,7 +2827,7 @@ func (s *assertMgrSuite) TestRefreshValidationSetAssertionsEnforcingModeHappyNot

snapstate.Set(s.state, "foo", &snapstate.SnapState{
Active: true,
Sequence: []*snap.SideInfo{{RealName: "foo", Revision: snap.R(1), SnapID: "qOqKhntON3vR7kwEbVPsILm7bUViPDzz"}},
Sequence: snapstatetest.NewSequenceFromSnapSideInfos([]*snap.SideInfo{{RealName: "foo", Revision: snap.R(1), SnapID: "qOqKhntON3vR7kwEbVPsILm7bUViPDzz"}}),
Current: snap.R(1),
})
snaptest.MockSnap(c, string(`name: foo
Expand Down Expand Up @@ -2911,7 +2911,7 @@ func (s *assertMgrSuite) TestRefreshValidationSetAssertionsEnforcingModeHappyPin

snapstate.Set(s.state, "foo", &snapstate.SnapState{
Active: true,
Sequence: []*snap.SideInfo{{RealName: "foo", Revision: snap.R(1), SnapID: "qOqKhntON3vR7kwEbVPsILm7bUViPDzz"}},
Sequence: snapstatetest.NewSequenceFromSnapSideInfos([]*snap.SideInfo{{RealName: "foo", Revision: snap.R(1), SnapID: "qOqKhntON3vR7kwEbVPsILm7bUViPDzz"}}),
Current: snap.R(1),
})
snaptest.MockSnap(c, string(`name: foo
Expand Down Expand Up @@ -3120,9 +3120,9 @@ func (s *assertMgrSuite) TestRefreshValidationSetAssertionsEnforcingModeWrongSna

snapstate.Set(s.state, "foo", &snapstate.SnapState{
Active: false,
Sequence: []*snap.SideInfo{
Sequence: snapstatetest.NewSequenceFromSnapSideInfos([]*snap.SideInfo{
{RealName: "foo", Revision: snap.R(1)},
},
}),
Current: snap.R(1),
})

Expand Down
3 changes: 2 additions & 1 deletion overlord/configstate/configcore/kernel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import (
"github.com/snapcore/snapd/overlord/hookstate"
"github.com/snapcore/snapd/overlord/hookstate/hooktest"
"github.com/snapcore/snapd/overlord/snapstate"
"github.com/snapcore/snapd/overlord/snapstate/snapstatetest"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snap/snaptest"
Expand Down Expand Up @@ -226,7 +227,7 @@ func (s *kernelSuite) mockGadget(c *C) {
defer s.state.Unlock()
snapstate.Set(s.state, "pc", &snapstate.SnapState{
SnapType: "gadget",
Sequence: []*snap.SideInfo{pcSideInfo},
Sequence: snapstatetest.NewSequenceFromSnapSideInfos([]*snap.SideInfo{pcSideInfo}),
Current: pcSideInfo.Revision,
Active: true,
})
Expand Down
12 changes: 6 additions & 6 deletions overlord/configstate/configcore/vitality_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (s *vitalitySuite) testConfigureVitalityWithValidSnap(c *C, uc18 bool) {
snaptest.MockSnap(c, mockSnapWithService, si)
s.state.Lock()
snapstate.Set(s.state, "test-snap", &snapstate.SnapState{
Sequence: []*snap.SideInfo{si},
Sequence: snapstatetest.NewSequenceFromSnapSideInfos([]*snap.SideInfo{si}),
Current: snap.R(1),
Active: true,
SnapType: "app",
Expand Down Expand Up @@ -173,7 +173,7 @@ func (s *vitalitySuite) TestConfigureVitalityWithQuotaGroup(c *C) {
snaptest.MockSnap(c, mockSnapWithService, si)
s.state.Lock()
snapstate.Set(s.state, "test-snap", &snapstate.SnapState{
Sequence: []*snap.SideInfo{si},
Sequence: snapstatetest.NewSequenceFromSnapSideInfos([]*snap.SideInfo{si}),
Current: snap.R(1),
Active: true,
SnapType: "app",
Expand Down Expand Up @@ -241,7 +241,7 @@ func (s *vitalitySuite) TestConfigureVitalityManySnaps(c *C) {
snaptest.MockSnap(c, mockSnapWithService, si)
s.state.Lock()
snapstate.Set(s.state, snapName, &snapstate.SnapState{
Sequence: []*snap.SideInfo{si},
Sequence: snapstatetest.NewSequenceFromSnapSideInfos([]*snap.SideInfo{si}),
Current: snap.R(1),
Active: true,
SnapType: "app",
Expand Down Expand Up @@ -272,7 +272,7 @@ func (s *vitalitySuite) TestConfigureVitalityManySnapsDelta(c *C) {
snaptest.MockSnap(c, mockSnapWithService, si)
s.state.Lock()
snapstate.Set(s.state, snapName, &snapstate.SnapState{
Sequence: []*snap.SideInfo{si},
Sequence: snapstatetest.NewSequenceFromSnapSideInfos([]*snap.SideInfo{si}),
Current: snap.R(1),
Active: true,
SnapType: "app",
Expand Down Expand Up @@ -307,7 +307,7 @@ func (s *vitalitySuite) TestConfigureVitalityManySnapsOneRemovedOneUnchanged(c *
snaptest.MockSnap(c, mockSnapWithService, si)
s.state.Lock()
snapstate.Set(s.state, snapName, &snapstate.SnapState{
Sequence: []*snap.SideInfo{si},
Sequence: snapstatetest.NewSequenceFromSnapSideInfos([]*snap.SideInfo{si}),
Current: snap.R(1),
Active: true,
SnapType: "app",
Expand Down Expand Up @@ -365,7 +365,7 @@ func (s *vitalitySuite) TestConfigureVitalityNotActiveSnap(c *C) {
snaptest.MockSnap(c, mockSnapWithService, si)
s.state.Lock()
snapstate.Set(s.state, "test-snap", &snapstate.SnapState{
Sequence: []*snap.SideInfo{si},
Sequence: snapstatetest.NewSequenceFromSnapSideInfos([]*snap.SideInfo{si}),
Current: snap.R(1),
Active: false,
SnapType: "app",
Expand Down
Loading

0 comments on commit 768021b

Please sign in to comment.