Skip to content

Commit

Permalink
Merge pull request #1937 from mvo5/bugfix/prepare-image-ordering
Browse files Browse the repository at this point in the history
image: ensure local snaps are put last in seed.yaml
  • Loading branch information
mvo5 authored Sep 19, 2016
2 parents 5d4ffbc + 1e73236 commit 48d6e8f
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 33 deletions.
28 changes: 18 additions & 10 deletions image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ func (li *localInfos) Name(pathOrName string) string {
return pathOrName
}

func (li *localInfos) PreferLocal(name string) string {
if path := li.Path(name); path != "" {
return path
}
return name
}

func (li *localInfos) Path(name string) string {
return li.nameToPath[name]
}
Expand Down Expand Up @@ -248,22 +255,23 @@ func bootstrapToRootDir(sto Store, model *asserts.Model, opts *Options, local *l
DevMode: false, // XXX: should this be true?
}

snaps := []string{}
// opts.Snaps need to be considered first to support local overrides
// of snaps mentioned in the model assertion whose fetching
// from the store will be then skipped
snaps = append(snaps, opts.Snaps...)
snaps = append(snaps, model.Gadget())
snaps = append(snaps, defaultCore)
snaps = append(snaps, model.Kernel())
snaps = append(snaps, model.RequiredSnaps()...)

for _, d := range []string{snapSeedDir, assertSeedDir} {
if err := os.MkdirAll(d, 0755); err != nil {
return err
}
}

snaps := []string{}
// core,kernel,gadget first
snaps = append(snaps, local.PreferLocal(defaultCore))
snaps = append(snaps, local.PreferLocal(model.Kernel()))
snaps = append(snaps, local.PreferLocal(model.Gadget()))
// then required and the user requested stuff
for _, snapName := range model.RequiredSnaps() {
snaps = append(snaps, local.PreferLocal(snapName))
}
snaps = append(snaps, opts.Snaps...)

seen := make(map[string]bool)
downloadedSnapsInfo := map[string]*snap.Info{}
var seedYaml snap.Seed
Expand Down
73 changes: 50 additions & 23 deletions image/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,15 @@ func (s *imageSuite) SetUpTest(c *C) {
s.storeSigning.Add(brandAccKey)

model, err := s.brandSigning.Sign(asserts.ModelType, map[string]interface{}{
"series": "16",
"authority-id": "my-brand",
"brand-id": "my-brand",
"model": "my-model",
"architecture": "amd64",
"gadget": "pc",
"kernel": "pc-kernel",
"timestamp": time.Now().Format(time.RFC3339),
"series": "16",
"authority-id": "my-brand",
"brand-id": "my-brand",
"model": "my-model",
"architecture": "amd64",
"gadget": "pc",
"kernel": "pc-kernel",
"required-snaps": []interface{}{"required-snap1"},
"timestamp": time.Now().Format(time.RFC3339),
}, nil, "")
c.Assert(err, IsNil)
s.model = model.(*asserts.Model)
Expand Down Expand Up @@ -169,13 +170,19 @@ name: ubuntu-core
version: 16.04
type: os
`

const devmodeSnap = `
name: devmode-snap
version: 1.0
type: app
confinement: devmode
`

const requiredSnap1 = `
name: required-snap1
version: 1.0
`

func (s *imageSuite) TestMissingModelAssertions(c *C) {
_, err := image.DecodeModelAssertion(&image.Options{})
c.Assert(err, ErrorMatches, "cannot read model assertion: open : no such file or directory")
Expand Down Expand Up @@ -322,6 +329,10 @@ func (s *imageSuite) setupSnaps(c *C, gadgetUnpackDir string) {
s.downloadedSnaps["ubuntu-core"] = snaptest.MakeTestSnapWithFiles(c, packageCore, nil)
s.storeSnapInfo["ubuntu-core"] = infoFromSnapYaml(c, packageCore, snap.R(3))
s.addSystemSnapAssertions(c, "ubuntu-core")

s.downloadedSnaps["required-snap1"] = snaptest.MakeTestSnapWithFiles(c, requiredSnap1, nil)
s.storeSnapInfo["required-snap1"] = infoFromSnapYaml(c, requiredSnap1, snap.R(3))
s.addSystemSnapAssertions(c, "required-snap1")
}

func (s *imageSuite) TestBootstrapToRootDir(c *C) {
Expand Down Expand Up @@ -355,10 +366,10 @@ func (s *imageSuite) TestBootstrapToRootDir(c *C) {
seed, err := snap.ReadSeedYaml(filepath.Join(rootdir, "var/lib/snapd/seed/seed.yaml"))
c.Assert(err, IsNil)

c.Check(seed.Snaps, HasLen, 3)
c.Check(seed.Snaps, HasLen, 4)

// check the files are in place
for i, name := range []string{"pc", "ubuntu-core", "pc-kernel"} {
for i, name := range []string{"ubuntu-core", "pc-kernel", "pc"} {
info := s.storeSnapInfo[name]
fn := filepath.Base(info.MountFile())
p := filepath.Join(rootdir, "var/lib/snapd/seed/snaps", fn)
Expand Down Expand Up @@ -425,7 +436,10 @@ func (s *imageSuite) TestBootstrapToRootDirLocalCore(c *C) {
defer c2.Restore()

opts := &image.Options{
Snaps: []string{s.downloadedSnaps["ubuntu-core"]},
Snaps: []string{
s.downloadedSnaps["ubuntu-core"],
s.downloadedSnaps["required-snap1"],
},
RootDir: rootdir,
GadgetUnpackDir: gadgetUnpackDir,
}
Expand All @@ -439,21 +453,31 @@ func (s *imageSuite) TestBootstrapToRootDirLocalCore(c *C) {
seed, err := snap.ReadSeedYaml(filepath.Join(rootdir, "var/lib/snapd/seed/seed.yaml"))
c.Assert(err, IsNil)

c.Check(seed.Snaps, HasLen, 3)
c.Check(seed.Snaps, HasLen, 4)

// check the files are in place
for i, name := range []string{"ubuntu-core_x1.snap", "pc", "pc-kernel"} {
for i, name := range []string{"ubuntu-core_x1.snap", "pc-kernel", "pc", "required-snap1_x1.snap"} {
unasserted := false
info := s.storeSnapInfo[name]
if info == nil {
// ubuntu-core
info = &snap.Info{
SideInfo: snap.SideInfo{
RealName: "ubuntu-core",
Revision: snap.R("x1"),
},
switch name {
case "ubuntu-core_x1.snap":
info = &snap.Info{
SideInfo: snap.SideInfo{
RealName: "ubuntu-core",
Revision: snap.R("x1"),
},
}
unasserted = true
case "required-snap1_x1.snap":
info = &snap.Info{
SideInfo: snap.SideInfo{
RealName: "required-snap1",
Revision: snap.R("x1"),
},
}
unasserted = true
}
unasserted = true
}

fn := filepath.Base(info.MountFile())
Expand All @@ -470,7 +494,7 @@ func (s *imageSuite) TestBootstrapToRootDirLocalCore(c *C) {

l, err := ioutil.ReadDir(filepath.Join(rootdir, "var/lib/snapd/seed/snaps"))
c.Assert(err, IsNil)
c.Check(l, HasLen, 3)
c.Check(l, HasLen, 4)

storeAccountKey := s.storeSigning.StoreAccountKey("")
brandPubKey, err := s.brandSigning.PublicKey("")
Expand Down Expand Up @@ -548,7 +572,7 @@ func (s *imageSuite) TestBootstrapToRootDirDevmodeSnap(c *C) {
seed, err := snap.ReadSeedYaml(filepath.Join(rootdir, "var/lib/snapd/seed/seed.yaml"))
c.Assert(err, IsNil)

c.Check(seed.Snaps, HasLen, 4)
c.Check(seed.Snaps, HasLen, 5)

// check devmode-snap
info := &snap.Info{
Expand All @@ -560,7 +584,10 @@ func (s *imageSuite) TestBootstrapToRootDirDevmodeSnap(c *C) {
fn := filepath.Base(info.MountFile())
p := filepath.Join(rootdir, "var/lib/snapd/seed/snaps", fn)
c.Check(osutil.FileExists(p), Equals, true)
c.Check(seed.Snaps[0], DeepEquals, &snap.SeedSnap{

// ensure local snaps are put last in seed.yaml
last := len(seed.Snaps) - 1
c.Check(seed.Snaps[last], DeepEquals, &snap.SeedSnap{
Name: "devmode-snap",
File: fn,
DevMode: true,
Expand Down

0 comments on commit 48d6e8f

Please sign in to comment.