Skip to content

Commit

Permalink
image: update signature of writeResolvedContentImpl()
Browse files Browse the repository at this point in the history
This now take gadget.Info instead of the asserts.Model. Thanks
to Samuele for the suggestion.
  • Loading branch information
mvo5 committed Mar 1, 2021
1 parent 6b0b4a1 commit acb726a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 19 deletions.
4 changes: 2 additions & 2 deletions image/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
package image

import (
"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/gadget"
"github.com/snapcore/snapd/overlord/auth"
"github.com/snapcore/snapd/store"
)
Expand Down Expand Up @@ -55,7 +55,7 @@ var (
WriteResolvedContent = writeResolvedContent
)

func MockWriteResolvedContent(f func(prepareImageDir, gadgetRoot, kernelRoot string, model *asserts.Model) error) (restore func()) {
func MockWriteResolvedContent(f func(prepareImageDir string, info *gadget.Info, gadgetRoot, kernelRoot string) error) (restore func()) {
oldWriteResolvedContent := writeResolvedContent
writeResolvedContent = f
return func() {
Expand Down
10 changes: 3 additions & 7 deletions image/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,24 +410,20 @@ func (tsto *ToolingStore) Find(at *asserts.AssertionType, headers map[string]str
// var so that it can be mocked for tests
var writeResolvedContent = writeResolvedContentImpl

// writeResolvedContent takes the unpacked gadget/kernel snaps and the
// model and outputs the resolved content from the
// writeResolvedContent takes gadget.Info and the unpacked
// gadget/kernel snaps and outputs the resolved content from the
// {gadget,kernel}.yaml into a filesystem tree with the structure:
// <prepareImageDir>/resolved-content/<volume-name>/<structure-name>/...
//
// E.g.
// /tmp/prep-img/resolved-content/pi/ubuntu-seed/{config.txt,bootcode.bin,...}
func writeResolvedContentImpl(prepareDir, gadgetUnpackDir, kernelUnpackDir string, model *asserts.Model) error {
func writeResolvedContentImpl(prepareDir string, info *gadget.Info, gadgetUnpackDir, kernelUnpackDir string) error {
fullPrepareDir, err := filepath.Abs(prepareDir)
if err != nil {
return err
}
targetDir := filepath.Join(fullPrepareDir, "resolved-content")

info, err := gadget.ReadInfoAndValidate(gadgetUnpackDir, model, nil)
if err != nil {
return err
}
constraints := gadget.LayoutConstraints{
NonMBRStartOffset: 1 * quantity.OffsetMiB,
// TODO:UC20: SectorSize is irrelevant here, we only care
Expand Down
6 changes: 5 additions & 1 deletion image/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"gopkg.in/check.v1"

"github.com/snapcore/snapd/gadget"
"github.com/snapcore/snapd/image"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/snap"
Expand Down Expand Up @@ -213,9 +214,12 @@ func (s *imageSuite) testWriteResolvedContent(c *check.C, prepareImageDir string
{"non-fs.img", "content of non-fs.img"},
})
kernelRoot := c.MkDir()

model := s.makeUC20Model(nil)
gadgetInfo, err := gadget.ReadInfoAndValidate(gadgetRoot, model, nil)
c.Assert(err, check.IsNil)

err = image.WriteResolvedContent(prepareImageDir, gadgetRoot, kernelRoot, model)
err = image.WriteResolvedContent(prepareImageDir, gadgetInfo, gadgetRoot, kernelRoot)
c.Assert(err, check.IsNil)

// XXX: add testutil.DirEquals([][]string)
Expand Down
12 changes: 6 additions & 6 deletions image/image_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,13 @@ func setupSeed(tsto *ToolingStore, model *asserts.Model, opts *Options) error {
return err
}

gadgetInfo, err := gadget.ReadInfoAndValidate(gadgetUnpackDir, model, nil)
if err != nil {
return err
}

// write resolved content to structure root
if err := writeResolvedContent(opts.PrepareDir, gadgetUnpackDir, kernelUnpackDir, model); err != nil {
if err := writeResolvedContent(opts.PrepareDir, gadgetInfo, gadgetUnpackDir, kernelUnpackDir); err != nil {
return err
}

Expand All @@ -438,11 +443,6 @@ func setupSeed(tsto *ToolingStore, model *asserts.Model, opts *Options) error {
return err
}

gadgetInfo, err := gadget.ReadInfoAndValidate(gadgetUnpackDir, model, nil)
if err != nil {
return err
}

defaultsDir := sysconfig.WritableDefaultsDir(rootDir)
defaults := gadget.SystemDefaults(gadgetInfo.Defaults)
if len(defaults) > 0 {
Expand Down
39 changes: 36 additions & 3 deletions image/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/snapcore/snapd/bootloader/bootloadertest"
"github.com/snapcore/snapd/bootloader/grubenv"
"github.com/snapcore/snapd/bootloader/ubootenv"
"github.com/snapcore/snapd/gadget"
"github.com/snapcore/snapd/image"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/overlord/auth"
Expand Down Expand Up @@ -119,7 +120,7 @@ func (s *imageSuite) SetUpTest(c *C) {
c2 := testutil.MockCommand(c, "umount", "")
s.AddCleanup(c2.Restore)

restore := image.MockWriteResolvedContent(func(_, _, _ string, _ *asserts.Model) error {
restore := image.MockWriteResolvedContent(func(_ string, _ *gadget.Info, _, _ string) error {
return nil
})
s.AddCleanup(restore)
Expand Down Expand Up @@ -486,6 +487,37 @@ const pcGadgetYaml = `
bootloader: grub
`

const pcUC20GadgetYaml = `
volumes:
pc:
bootloader: grub
structure:
- name: ubuntu-seed
role: system-seed
type: EF,C12A7328-F81F-11D2-BA4B-00A0C93EC93B
size: 100M
- name: ubuntu-data
role: system-data
type: 83,0FC63DAF-8483-4772-8E79-3D69D8477DE4
size: 200M
`

const piUC20GadgetYaml = `
volumes:
pi:
schema: mbr
bootloader: u-boot
structure:
- name: ubuntu-seed
role: system-seed
type: 0C
size: 100M
- name: ubuntu-data
role: system-data
type: 83,0FC63DAF-8483-4772-8E79-3D69D8477DE4
size: 200M
`

func (s *imageSuite) setupSnaps(c *C, publishers map[string]string, defaultsYaml string) {
gadgetYaml := pcGadgetYaml + defaultsYaml
if _, ok := publishers["pc"]; ok {
Expand Down Expand Up @@ -590,8 +622,7 @@ func (s *imageSuite) TestSetupSeed(c *C) {
}, "")

gadgetWriteResolvedContentCalled := 0
restore = image.MockWriteResolvedContent(func(prepareImageDir, gadgetRoot, kernelRoot string, model *asserts.Model) error {
c.Check(model, DeepEquals, s.model)
restore = image.MockWriteResolvedContent(func(prepareImageDir string, info *gadget.Info, gadgetRoot, kernelRoot string) error {
c.Check(prepareImageDir, Equals, preparedir)
c.Check(gadgetRoot, Equals, filepath.Join(preparedir, "gadget"))
c.Check(kernelRoot, Equals, filepath.Join(preparedir, "kernel"))
Expand Down Expand Up @@ -2605,6 +2636,7 @@ func (s *imageSuite) TestSetupSeedCore20Grub(c *C) {
gadgetContent := [][]string{
{"grub-recovery.conf", "# recovery grub.cfg"},
{"grub.conf", "# boot grub.cfg"},
{"meta/gadget.yaml", pcUC20GadgetYaml},
}
s.makeSnap(c, "pc=20", gadgetContent, snap.R(22), "")
s.makeSnap(c, "required20", nil, snap.R(21), "other")
Expand Down Expand Up @@ -2761,6 +2793,7 @@ func (s *imageSuite) TestSetupSeedCore20UBoot(c *C) {
// TODO:UC20: write this test with non-empty uboot.env when we support
// that
{"uboot.conf", ""},
{"meta/gadget.yaml", piUC20GadgetYaml},
}
s.makeSnap(c, "uboot-gadget=20", gadgetContent, snap.R(22), "")

Expand Down

0 comments on commit acb726a

Please sign in to comment.