Skip to content

Commit

Permalink
image,seed: hide Seed16/Snap16, use seed.Open in image_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronis committed Oct 17, 2019
1 parent b16e384 commit 5b5ab6a
Show file tree
Hide file tree
Showing 11 changed files with 556 additions and 522 deletions.
896 changes: 435 additions & 461 deletions image/image_test.go

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions seed/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@

package seed

import (
"github.com/snapcore/snapd/seed/internal"
)

type InternalSnap16 = internal.Snap16

var (
LoadAssertions = loadAssertions
)
22 changes: 22 additions & 0 deletions seed/internal/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
* Copyright (C) 2019 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

// Package internal (of seed) provides types and helpers used
// internally by both seed and seed/seedwriter.
package internal
4 changes: 2 additions & 2 deletions seed/seed_yaml.go → seed/internal/seed_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*
*/

package seed
package internal

import (
"fmt"
Expand Down Expand Up @@ -62,7 +62,7 @@ type Seed16 struct {
Snaps []*Snap16 `yaml:"snaps"`
}

func ReadYaml(fn string) (*Seed16, error) {
func ReadSeedYaml(fn string) (*Seed16, error) {
errPrefix := "cannot read seed yaml"

yamlData, err := ioutil.ReadFile(fn)
Expand Down
24 changes: 12 additions & 12 deletions seed/seed_yaml_test.go → seed/internal/seed_yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*
*/

package seed_test
package internal_test

import (
"io/ioutil"
Expand All @@ -26,7 +26,7 @@ import (

. "gopkg.in/check.v1"

"github.com/snapcore/snapd/seed"
"github.com/snapcore/snapd/seed/internal"
)

func Test(t *testing.T) { TestingT(t) }
Expand All @@ -52,18 +52,18 @@ func (s *seedYamlTestSuite) TestSimple(c *C) {
err := ioutil.WriteFile(fn, mockSeedYaml, 0644)
c.Assert(err, IsNil)

seedYaml, err := seed.ReadYaml(fn)
seedYaml, err := internal.ReadSeedYaml(fn)
c.Assert(err, IsNil)
c.Assert(seedYaml.Snaps, HasLen, 2)
c.Assert(seedYaml.Snaps[0], DeepEquals, &seed.Snap16{
c.Assert(seedYaml.Snaps[0], DeepEquals, &internal.Snap16{
File: "foo_1.0_all.snap",
Name: "foo",
SnapID: "snapidsnapidsnapid",

Channel: "stable",
DevMode: true,
})
c.Assert(seedYaml.Snaps[1], DeepEquals, &seed.Snap16{
c.Assert(seedYaml.Snaps[1], DeepEquals, &internal.Snap16{
File: "local.snap",
Name: "local",
Unasserted: true,
Expand All @@ -81,7 +81,7 @@ func (s *seedYamlTestSuite) TestNoPathAllowed(c *C) {
err := ioutil.WriteFile(fn, badMockSeedYaml, 0644)
c.Assert(err, IsNil)

_, err = seed.ReadYaml(fn)
_, err = internal.ReadSeedYaml(fn)
c.Assert(err, ErrorMatches, `cannot read seed yaml: "foo/bar.snap" must be a filename, not a path`)
}

Expand All @@ -98,7 +98,7 @@ snaps:
`), 0644)
c.Assert(err, IsNil)

_, err = seed.ReadYaml(fn)
_, err = internal.ReadSeedYaml(fn)
c.Assert(err, ErrorMatches, `cannot read seed yaml: snap name "foo" must be unique`)
}

Expand All @@ -111,7 +111,7 @@ snaps:
`), 0644)
c.Assert(err, IsNil)

_, err = seed.ReadYaml(fn)
_, err = internal.ReadSeedYaml(fn)
c.Assert(err, ErrorMatches, `cannot read seed yaml: invalid risk in channel name: invalid/channel/`)
}

Expand All @@ -124,7 +124,7 @@ snaps:
`), 0644)
c.Assert(err, IsNil)

_, err = seed.ReadYaml(fn)
_, err = internal.ReadSeedYaml(fn)
c.Assert(err, ErrorMatches, `cannot read seed yaml: invalid snap name: "invalid--name"`)
}

Expand All @@ -137,7 +137,7 @@ snaps:
`), 0644)
c.Assert(err, IsNil)

_, err = seed.ReadYaml(fn)
_, err = internal.ReadSeedYaml(fn)
c.Assert(err, ErrorMatches, `cannot read seed yaml: invalid snap name: "foo_1"`)
}

Expand All @@ -149,7 +149,7 @@ snaps:
`), 0644)
c.Assert(err, IsNil)

_, err = seed.ReadYaml(fn)
_, err = internal.ReadSeedYaml(fn)
c.Assert(err, ErrorMatches, `cannot read seed yaml: invalid snap name: ""`)
}

Expand All @@ -161,6 +161,6 @@ snaps:
`), 0644)
c.Assert(err, IsNil)

_, err = seed.ReadYaml(fn)
_, err = internal.ReadSeedYaml(fn)
c.Assert(err, ErrorMatches, `cannot read seed yaml: "file" attribute for "foo" cannot be empty`)
}
7 changes: 4 additions & 3 deletions seed/seed16.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/asserts/snapasserts"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/seed/internal"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snap/channel"
"github.com/snapcore/snapd/snap/naming"
Expand Down Expand Up @@ -111,7 +112,7 @@ func (s *seed16) Model() (*asserts.Model, error) {
return s.model, nil
}

func (s *seed16) addSnap(sn *Snap16, pinnedTrack string, tm timings.Measurer) (*Snap, error) {
func (s *seed16) addSnap(sn *internal.Snap16, pinnedTrack string, tm timings.Measurer) (*Snap, error) {
path := filepath.Join(s.seedDir, "snaps", sn.File)
snapChannel := sn.Channel
if pinnedTrack != "" {
Expand Down Expand Up @@ -167,14 +168,14 @@ func (s *seed16) LoadMeta(tm timings.Measurer) error {
return ErrNoMeta
}

seedYaml, err := ReadYaml(seedYamlFile)
seedYaml, err := internal.ReadSeedYaml(seedYamlFile)
if err != nil {
return err
}
yamlSnaps := seedYaml.Snaps

required := naming.NewSnapSet(model.RequiredWithEssentialSnaps())
seeding := make(map[string]*Snap16, len(yamlSnaps))
seeding := make(map[string]*internal.Snap16, len(yamlSnaps))
for _, sn := range yamlSnaps {
seeding[sn.Name] = sn
}
Expand Down
55 changes: 29 additions & 26 deletions seed/seed16_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"testing"

. "gopkg.in/check.v1"
"gopkg.in/yaml.v2"
Expand All @@ -38,6 +39,8 @@ import (
"github.com/snapcore/snapd/timings"
)

func Test(t *testing.T) { TestingT(t) }

type seed16Suite struct {
testutil.BaseTest

Expand Down Expand Up @@ -235,7 +238,7 @@ func (s *seed16Suite) TestLoadMetaInvalidSeedYaml(c *C) {

// create a seed.yaml
content, err := yaml.Marshal(map[string]interface{}{
"snaps": []*seed.Snap16{{
"snaps": []*seed.InternalSnap16{{
Name: "core",
Channel: "track/not-a-risk",
}},
Expand Down Expand Up @@ -316,69 +319,69 @@ var snapPublishers = map[string]string{
}

var (
coreSeed = &seed.Snap16{
coreSeed = &seed.InternalSnap16{
Name: "core",
Channel: "stable",
}
kernelSeed = &seed.Snap16{
kernelSeed = &seed.InternalSnap16{
Name: "pc-kernel",
Channel: "stable",
}
gadgetSeed = &seed.Snap16{
gadgetSeed = &seed.InternalSnap16{
Name: "pc",
Channel: "stable",
}
requiredSeed = &seed.Snap16{
requiredSeed = &seed.InternalSnap16{
Name: "required",
Channel: "stable",
}
// Core 18
snapdSeed = &seed.Snap16{
snapdSeed = &seed.InternalSnap16{
Name: "snapd",
Channel: "stable",
}
core18Seed = &seed.Snap16{
core18Seed = &seed.InternalSnap16{
Name: "core18",
Channel: "stable",
}
kernel18Seed = &seed.Snap16{
kernel18Seed = &seed.InternalSnap16{
Name: "pc-kernel",
Channel: "18",
}
gadget18Seed = &seed.Snap16{
gadget18Seed = &seed.InternalSnap16{
Name: "pc",
Channel: "18",
}
required18Seed = &seed.Snap16{
required18Seed = &seed.InternalSnap16{
Name: "required18",
Channel: "stable",
}
classicSnapSeed = &seed.Snap16{
classicSnapSeed = &seed.InternalSnap16{
Name: "classic-snap",
Channel: "stable",
Classic: true,
}
classicGadgetSeed = &seed.Snap16{
classicGadgetSeed = &seed.InternalSnap16{
Name: "classic-gadget",
Channel: "stable",
}
classicGadget18Seed = &seed.Snap16{
classicGadget18Seed = &seed.InternalSnap16{
Name: "classic-gadget18",
Channel: "stable",
}
privateSnapSeed = &seed.Snap16{
privateSnapSeed = &seed.InternalSnap16{
Name: "private-snap",
Channel: "stable",
Private: true,
}
contactableSnapSeed = &seed.Snap16{
contactableSnapSeed = &seed.InternalSnap16{
Name: "contactable-snap",
Channel: "stable",
Contact: "author@example.com",
}
)

func (s *seed16Suite) makeSeed(c *C, modelHeaders map[string]interface{}, seedSnaps ...*seed.Snap16) []*seed.Snap16 {
func (s *seed16Suite) makeSeed(c *C, modelHeaders map[string]interface{}, seedSnaps ...*seed.InternalSnap16) []*seed.InternalSnap16 {
coreHeaders := map[string]interface{}{
"architecture": "amd64",
}
Expand All @@ -397,7 +400,7 @@ func (s *seed16Suite) makeSeed(c *C, modelHeaders map[string]interface{}, seedSn
err = os.Mkdir(s.SnapsDir, 0755)
c.Assert(err, IsNil)

var completeSeedSnaps []*seed.Snap16
var completeSeedSnaps []*seed.InternalSnap16
for _, seedSnap := range seedSnaps {
completeSeedSnap := *seedSnap
var snapFname string
Expand Down Expand Up @@ -430,7 +433,7 @@ func (s *seed16Suite) makeSeed(c *C, modelHeaders map[string]interface{}, seedSn
return completeSeedSnaps
}

func (s *seed16Suite) writeSeed(c *C, seedSnaps []*seed.Snap16) {
func (s *seed16Suite) writeSeed(c *C, seedSnaps []*seed.InternalSnap16) {
// create a seed.yaml
content, err := yaml.Marshal(map[string]interface{}{
"snaps": seedSnaps,
Expand Down Expand Up @@ -877,7 +880,7 @@ func (s *seed16Suite) TestLoadMetaClassicSnapdWithGadget18(c *C) {
}

func (s *seed16Suite) TestLoadMetaCore18Local(c *C) {
localRequired18Seed := &seed.Snap16{
localRequired18Seed := &seed.InternalSnap16{
Name: "required18",
Unasserted: true,
DevMode: true,
Expand Down Expand Up @@ -1066,9 +1069,9 @@ version: other-base
err = s.seed16.LoadAssertions(s.db, s.commitTo)
c.Assert(err, IsNil)

omit := func(which int) func([]*seed.Snap16) []*seed.Snap16 {
return func(snaps []*seed.Snap16) []*seed.Snap16 {
broken := make([]*seed.Snap16, 0, len(snaps)-1)
omit := func(which int) func([]*seed.InternalSnap16) []*seed.InternalSnap16 {
return func(snaps []*seed.InternalSnap16) []*seed.InternalSnap16 {
broken := make([]*seed.InternalSnap16, 0, len(snaps)-1)
for i, sn := range snaps {
if i == which {
continue
Expand All @@ -1078,8 +1081,8 @@ version: other-base
return broken
}
}
replaceFile := func(snapName, fname string) func([]*seed.Snap16) []*seed.Snap16 {
return func(snaps []*seed.Snap16) []*seed.Snap16 {
replaceFile := func(snapName, fname string) func([]*seed.InternalSnap16) []*seed.InternalSnap16 {
return func(snaps []*seed.InternalSnap16) []*seed.InternalSnap16 {
for i := range snaps {
if snaps[i].Name != snapName {
continue
Expand All @@ -1093,7 +1096,7 @@ version: other-base
}

tests := []struct {
breakSeed func([]*seed.Snap16) []*seed.Snap16
breakSeed func([]*seed.InternalSnap16) []*seed.InternalSnap16
err string
}{
{omit(0), `essential snap "snapd" required by the model is missing in the seed`},
Expand All @@ -1108,7 +1111,7 @@ version: other-base
}

for _, t := range tests {
testSeedSnap16s := make([]*seed.Snap16, 5)
testSeedSnap16s := make([]*seed.InternalSnap16, 5)
copy(testSeedSnap16s, seedSnap16s)

testSeedSnap16s = t.breakSeed(testSeedSnap16s)
Expand Down
28 changes: 28 additions & 0 deletions seed/seedwriter/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
* Copyright (C) 2019 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

package seedwriter

import (
"github.com/snapcore/snapd/seed/internal"
)

type InternalSnap16 = internal.Snap16

var InternalReadSeedYaml = internal.ReadSeedYaml
Loading

0 comments on commit 5b5ab6a

Please sign in to comment.