Skip to content

Commit

Permalink
o/devicestate: use same assertion for seed/model in install mode test
Browse files Browse the repository at this point in the history
Some tests in this suite were flaky because of the time difference
between the seed and model creation and signing. If the second signing
occurred in a different second, the assertions would differ slightly and
the signatures wouldn't match.
```
timestamp: 2025-01-29T18:46:37Z
sign-key-sha3-384: 35GjZS0BPeCV8fgGI0vYy-jlqaHNJ-2Q8zgPIk53oT_dciAGXVkY4yE6gIPxJ8q5
...
timestamp: 2025-01-29T18:46:38Z
sign-key-sha3-384: 35GjZS0BPeCV8fgGI0vYy-jlqaHNJ-2Q8zgPIk53oT_dciAGXVkY4yE6gIPxJ8q5
```
We can fix the issue by not generating the second model or
by pinning the time for the duration of the test such that the two
models would always be identical. This takes the first approach.

Signed-off-by: Miguel Pires <[email protected]>
  • Loading branch information
miguelpires committed Feb 4, 2025
1 parent 4be46eb commit 749db47
Showing 1 changed file with 41 additions and 29 deletions.
70 changes: 41 additions & 29 deletions overlord/devicestate/devicestate_install_mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"github.com/snapcore/snapd/gadget/install"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/overlord/assertstate/assertstatetest"
"github.com/snapcore/snapd/overlord/auth"
"github.com/snapcore/snapd/overlord/devicestate"
"github.com/snapcore/snapd/overlord/devicestate/devicestatetest"
Expand Down Expand Up @@ -77,7 +78,7 @@ func (s *deviceMgrInstallSuite) SetUpTest(c *C) {
s.SeedDir = dirs.SnapSeedDir
}

func (s *deviceMgrInstallSuite) setupSystemSeed(c *C, sysLabel, gadgetYaml string, isClassic bool, kModsRevs map[string]snap.Revision) (*asserts.Model, map[string]interface{}) {
func (s *deviceMgrInstallSuite) setupSystemSeed(c *C, sysLabel, gadgetYaml string, isClassic bool, kModsRevs map[string]snap.Revision) *asserts.Model {
s.StoreSigning = assertstest.NewStoreStack("can0nical", nil)

s.Brands = assertstest.NewSigningAccounts(s.StoreSigning)
Expand Down Expand Up @@ -171,7 +172,7 @@ func (s *deviceMgrInstallSuite) setupSystemSeed(c *C, sysLabel, gadgetYaml strin
Name: "optional24",
Components: []seedwriter.OptionsComponent{{Name: "comp1"}},
},
}), model
})
}

type fakeSeedCopier struct {
Expand All @@ -198,7 +199,7 @@ type mockSystemSeedWithLabelOpts struct {
types []snap.Type
}

func (s *deviceMgrInstallSuite) mockSystemSeedWithLabel(c *C, label string, seedCopyFn func(string, seed.CopyOptions, timings.Measurer) error, opts mockSystemSeedWithLabelOpts) (gadgetSnapPath, kernelSnapPath string, kCompsPaths []string, ginfo *gadget.Info, mountCmd *testutil.MockCmd, rawModel map[string]interface{}) {
func (s *deviceMgrInstallSuite) mockSystemSeedWithLabel(c *C, label string, seedCopyFn func(string, seed.CopyOptions, timings.Measurer) error, opts mockSystemSeedWithLabelOpts) (gadgetSnapPath, kernelSnapPath string, kCompsPaths []string, ginfo *gadget.Info, mountCmd *testutil.MockCmd, model *asserts.Model) {
// Mock partitioned disk
gadgetYaml := gadgettest.SingleVolumeUC20GadgetYaml
if opts.isClassic {
Expand All @@ -222,7 +223,7 @@ func (s *deviceMgrInstallSuite) mockSystemSeedWithLabel(c *C, label string, seed
s.AddCleanup(restore)

// now create a label with snaps/assertions
model, rawModel := s.setupSystemSeed(c, label, seedGadget, opts.isClassic, opts.kModsRevs)
model = s.setupSystemSeed(c, label, seedGadget, opts.isClassic, opts.kModsRevs)
c.Check(model, NotNil)

// Create fake seed that will return information from the label we created
Expand Down Expand Up @@ -299,7 +300,7 @@ func (s *deviceMgrInstallSuite) mockSystemSeedWithLabel(c *C, label string, seed
mountCmd = testutil.MockCommand(c, "systemd-mount", "")
s.AddCleanup(func() { mountCmd.Restore() })

return gadgetSnapPath, kernelSnapPath, kCompsPaths, ginfo, mountCmd, rawModel
return gadgetSnapPath, kernelSnapPath, kCompsPaths, ginfo, mountCmd, model
}

type deviceMgrInstallModeSuite struct {
Expand Down Expand Up @@ -533,6 +534,17 @@ func (s *deviceMgrInstallModeSuite) makeMockInstallModel(c *C, grade string) *as
})
}

func (s *deviceMgrInstallModeSuite) addModelToState(modelAs *asserts.Model) {
s.setupBrands()
assertstatetest.AddMany(s.state, modelAs)

devicestatetest.SetDevice(s.state, &auth.DeviceState{
Brand: modelAs.BrandID(),
Model: modelAs.Model(),
// no serial in install mode
})
}

func (s *deviceMgrInstallModeSuite) makeMockInstallModelExtras(c *C, grade string, extras map[string]interface{}) *asserts.Model {
mockModel := s.makeModelAssertionInState(c, "my-brand", "my-model", extras)
devicestatetest.SetDevice(s.state, &auth.DeviceState{
Expand Down Expand Up @@ -1047,10 +1059,10 @@ func (s *deviceMgrInstallModeSuite) TestInstallRestoresPreseedArtifact(c *C) {
preseedArtifact: true,
types: []snap.Type{snap.TypeKernel},
}
_, _, _, _, _, rawModel := s.mockSystemSeedWithLabel(c, "20200105", seedCopyFn, seedOpts)
_, _, _, _, _, model := s.mockSystemSeedWithLabel(c, "20200105", seedCopyFn, seedOpts)

s.state.Lock()
s.makeMockInstallModelExtras(c, "dangerous", rawModel)
s.addModelToState(model)
s.makeMockInstalledPcKernelAndGadget(c, "", "", core24SnapID)
devicestate.SetSystemMode(s.mgr, "install")

Expand Down Expand Up @@ -1150,10 +1162,10 @@ func (s *deviceMgrInstallModeSuite) TestInstallRestoresPreseedArtifactError(c *C
preseedArtifact: true,
types: []snap.Type{snap.TypeKernel},
}
_, _, _, _, _, rawModel := s.mockSystemSeedWithLabel(c, "20200105", seedCopyFn, seedOpts)
_, _, _, _, _, model := s.mockSystemSeedWithLabel(c, "20200105", seedCopyFn, seedOpts)

s.state.Lock()
s.makeMockInstallModelExtras(c, "dangerous", rawModel)
s.addModelToState(model)
s.makeMockInstalledPcKernelAndGadget(c, "", "", core24SnapID)
devicestate.SetSystemMode(s.mgr, "install")

Expand Down Expand Up @@ -2520,10 +2532,10 @@ func (s *deviceMgrInstallModeSuite) testFactoryResetNoEncryptionHappyFull(c *C,
types: []snap.Type{snap.TypeKernel},
kModsRevs: kModsRevs,
}
_, _, _, _, _, rawModel := s.mockSystemSeedWithLabel(c, "20191218", seedCopyFn, seedOpts)
_, _, _, _, _, model := s.mockSystemSeedWithLabel(c, "20191218", seedCopyFn, seedOpts)

s.state.Lock()
model := s.makeMockInstallModelExtras(c, "dangerous", rawModel)
s.addModelToState(model)
s.state.Unlock()

// for debug timinigs
Expand Down Expand Up @@ -2620,10 +2632,10 @@ func (s *deviceMgrInstallModeSuite) testFactoryResetEncryptionHappyFull(c *C, wi
types: []snap.Type{snap.TypeKernel},
kModsRevs: kModsRevs,
}
_, _, _, _, _, rawModel := s.mockSystemSeedWithLabel(c, "20191218", seedCopyFn, seedOpts)
_, _, _, _, _, model := s.mockSystemSeedWithLabel(c, "20191218", seedCopyFn, seedOpts)

s.state.Lock()
model := s.makeMockInstallModelExtras(c, "dangerous", rawModel)
s.addModelToState(model)
s.state.Unlock()

// for debug timinigs
Expand Down Expand Up @@ -2695,10 +2707,10 @@ func (s *deviceMgrInstallModeSuite) TestFactoryResetEncryptionHappyAfterReboot(c
preseedArtifact: false,
types: []snap.Type{snap.TypeKernel},
}
_, _, _, _, _, rawModel := s.mockSystemSeedWithLabel(c, "20191218", seedCopyFn, seedOpts)
_, _, _, _, _, model := s.mockSystemSeedWithLabel(c, "20191218", seedCopyFn, seedOpts)

s.state.Lock()
model := s.makeMockInstallModelExtras(c, "dangerous", rawModel)
s.addModelToState(model)
s.state.Unlock()

// for debug timinigs
Expand Down Expand Up @@ -2771,10 +2783,10 @@ func (s *deviceMgrInstallModeSuite) TestFactoryResetSerialsWithoutKey(c *C) {
preseedArtifact: false,
types: []snap.Type{snap.TypeKernel},
}
_, _, _, _, _, rawModel := s.mockSystemSeedWithLabel(c, "20191218", seedCopyFn, seedOpts)
_, _, _, _, _, model := s.mockSystemSeedWithLabel(c, "20191218", seedCopyFn, seedOpts)

s.state.Lock()
model := s.makeMockInstallModelExtras(c, "dangerous", rawModel)
s.addModelToState(model)
s.state.Unlock()

// pretend snap-bootstrap mounted ubuntu-save
Expand Down Expand Up @@ -2818,10 +2830,10 @@ func (s *deviceMgrInstallModeSuite) TestFactoryResetNoSerials(c *C) {
preseedArtifact: false,
types: []snap.Type{snap.TypeKernel},
}
_, _, _, _, _, rawModel := s.mockSystemSeedWithLabel(c, "20191218", seedCopyFn, seedOpts)
_, _, _, _, _, model := s.mockSystemSeedWithLabel(c, "20191218", seedCopyFn, seedOpts)

s.state.Lock()
model := s.makeMockInstallModelExtras(c, "dangerous", rawModel)
s.addModelToState(model)
s.state.Unlock()

// pretend snap-bootstrap mounted ubuntu-save
Expand Down Expand Up @@ -2855,10 +2867,10 @@ func (s *deviceMgrInstallModeSuite) TestFactoryResetNoSave(c *C) {
preseedArtifact: false,
types: []snap.Type{snap.TypeKernel},
}
_, _, _, _, _, rawModel := s.mockSystemSeedWithLabel(c, "20191218", seedCopyFn, seedOpts)
_, _, _, _, _, model := s.mockSystemSeedWithLabel(c, "20191218", seedCopyFn, seedOpts)

s.state.Lock()
model := s.makeMockInstallModelExtras(c, "dangerous", rawModel)
s.addModelToState(model)
s.state.Unlock()

// no ubuntu-save directory, what makes the whole process behave like reinstall
Expand Down Expand Up @@ -2939,10 +2951,10 @@ func (s *deviceMgrInstallModeSuite) TestFactoryResetSerialManyOneValid(c *C) {
preseedArtifact: false,
types: []snap.Type{snap.TypeKernel},
}
_, _, _, _, _, rawModel := s.mockSystemSeedWithLabel(c, "20191218", seedCopyFn, seedOpts)
_, _, _, _, _, model := s.mockSystemSeedWithLabel(c, "20191218", seedCopyFn, seedOpts)

s.state.Lock()
model := s.makeMockInstallModelExtras(c, "dangerous", rawModel)
s.addModelToState(model)
s.state.Unlock()

// pretend snap-bootstrap mounted ubuntu-save
Expand Down Expand Up @@ -3013,7 +3025,7 @@ func (s *deviceMgrInstallModeSuite) TestFactoryResetExpectedTasks(c *C) {
preseedArtifact: false,
types: []snap.Type{snap.TypeKernel},
}
_, _, _, _, _, rawModel := s.mockSystemSeedWithLabel(c, "20191218", seedCopyFn, seedOpts)
_, _, _, _, _, model := s.mockSystemSeedWithLabel(c, "20191218", seedCopyFn, seedOpts)

restore := release.MockOnClassic(false)
defer restore()
Expand Down Expand Up @@ -3041,7 +3053,7 @@ func (s *deviceMgrInstallModeSuite) TestFactoryResetExpectedTasks(c *C) {
c.Assert(m.WriteTo(""), IsNil)

s.state.Lock()
s.makeMockInstallModelExtras(c, "dangerous", rawModel)
s.addModelToState(model)
s.makeMockInstalledPcKernelAndGadget(c, "", "", core24SnapID)
devicestate.SetSystemMode(s.mgr, "factory-reset")
s.state.Unlock()
Expand Down Expand Up @@ -3086,7 +3098,7 @@ func (s *deviceMgrInstallModeSuite) TestFactoryResetInstallDeviceHook(c *C) {
preseedArtifact: false,
types: []snap.Type{snap.TypeKernel},
}
_, _, _, _, _, rawModel := s.mockSystemSeedWithLabel(c, "20191218", seedCopyFn, seedOpts)
_, _, _, _, _, model := s.mockSystemSeedWithLabel(c, "20191218", seedCopyFn, seedOpts)

restore := release.MockOnClassic(false)
defer restore()
Expand Down Expand Up @@ -3124,7 +3136,7 @@ func (s *deviceMgrInstallModeSuite) TestFactoryResetInstallDeviceHook(c *C) {
c.Assert(m.WriteTo(""), IsNil)

s.state.Lock()
s.makeMockInstallModelExtras(c, "dangerous", rawModel)
s.addModelToState(model)
s.makeMockInstalledPcKernelAndGadget(c, "install-device-hook-content", "", core24SnapID)
devicestate.SetSystemMode(s.mgr, "factory-reset")
s.state.Unlock()
Expand Down Expand Up @@ -3187,10 +3199,10 @@ func (s *deviceMgrInstallModeSuite) TestFactoryResetRunsPrepareRunSystemData(c *
preseedArtifact: false,
types: []snap.Type{snap.TypeKernel},
}
_, _, _, _, _, rawModel := s.mockSystemSeedWithLabel(c, "20191218", seedCopyFn, seedOpts)
_, _, _, _, _, model := s.mockSystemSeedWithLabel(c, "20191218", seedCopyFn, seedOpts)

s.state.Lock()
model := s.makeMockInstallModelExtras(c, "dangerous", rawModel)
s.addModelToState(model)
s.state.Unlock()

// pretend snap-bootstrap mounted ubuntu-save
Expand Down

0 comments on commit 749db47

Please sign in to comment.