Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

slicer/test: Support for custom packages #80

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 123 additions & 15 deletions internal/slicer/slicer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,30 @@ import (
"github.com/canonical/chisel/internal/testutil"
)

var (
Reg = testutil.Reg
Dir = testutil.Dir
Lnk = testutil.Lnk
)

type testPackageData struct {
name string
content []byte
}

type testArchiveData struct {
packages []testPackageData
}

type slicerTest struct {
summary string
arch string
release map[string]string
slices []setup.SliceKey
hackopt func(c *C, opts *slicer.RunOptions)
result map[string]string
error string
summary string
arch string
archives map[string]testArchiveData
release map[string]string
slices []setup.SliceKey
hackopt func(c *C, opts *slicer.RunOptions)
result map[string]string
error string
}

var packageEntries = map[string][]testutil.TarEntry{
Expand Down Expand Up @@ -515,6 +531,79 @@ var slicerTests = []slicerTest{{
"/usr/bin/": "dir 0755",
"/usr/bin/hello": "file 0775 eaf29575",
},
}, {
summary: "Custom archives with custom packages",
archives: map[string]testArchiveData{
"leptons": testArchiveData{
packages: []testPackageData{{
name: "electron",
content: testutil.MustMakeDeb([]testutil.TarEntry{
Dir(0755, "./"),
Dir(0755, "./mass/"),
Reg(0644, "./mass/electron", "9.1093837015E−31 kg\n"),
Dir(0755, "./usr/"),
Dir(0755, "./usr/share/"),
Dir(0755, "./usr/share/doc/"),
Dir(0755, "./usr/share/doc/electron/"),
Reg(0644, "./usr/share/doc/electron/copyright", ""),
}),
}},
},
"hadrons": testArchiveData{
packages: []testPackageData{{
name: "proton",
content: testutil.MustMakeDeb([]testutil.TarEntry{
Dir(0755, "./"),
Dir(0755, "./mass/"),
Reg(0644, "./mass/proton", "1.67262192369E−27 kg\n"),
}),
}},
},
},
release: map[string]string{
"chisel.yaml": `
format: chisel-v1
archives:
leptons:
version: 1
suites: [main]
components: [main, universe]
default: true
hadrons:
version: 1
suites: [main]
components: [main]
`,
"slices/mydir/electron.yaml": `
package: electron
slices:
mass:
contents:
/mass/electron:
`,
"slices/mydir/proton.yaml": `
package: proton
archive: hadrons
slices:
mass:
contents:
/mass/proton:
`,
},
slices: []setup.SliceKey{
{"electron", "mass"},
{"proton", "mass"},
},
result: map[string]string{
"/mass/": "dir 0755",
"/mass/electron": "file 0644 a1258e30",
"/mass/proton": "file 0644 a2390d10",
"/usr/": "dir 0755",
"/usr/share/": "dir 0755",
"/usr/share/doc/": "dir 0755",
"/usr/share/doc/electron/": "dir 0755",
"/usr/share/doc/electron/copyright": "file 0644 empty",
},
}}

const defaultChiselYaml = `
Expand All @@ -527,7 +616,7 @@ const defaultChiselYaml = `

type testArchive struct {
options archive.Options
pkgs map[string][]byte
pkgs map[string]testPackageData
}

func (a *testArchive) Options() *archive.Options {
Expand All @@ -536,7 +625,7 @@ func (a *testArchive) Options() *archive.Options {

func (a *testArchive) Fetch(pkg string) (io.ReadCloser, error) {
if data, ok := a.pkgs[pkg]; ok {
return io.NopCloser(bytes.NewBuffer(data)), nil
return io.NopCloser(bytes.NewBuffer(data.content)), nil
}
return nil, fmt.Errorf("attempted to open %q package", pkg)
}
Expand Down Expand Up @@ -569,16 +658,28 @@ func (s *S) TestRun(c *C) {
selection, err := setup.Select(release, test.slices)
c.Assert(err, IsNil)

pkgs := map[string][]byte{
"base-files": testutil.PackageData["base-files"],
pkgs := map[string]testPackageData{
"base-files": testPackageData{content: testutil.PackageData["base-files"]},
}
for name, entries := range packageEntries {
deb, err := testutil.MakeDeb(entries)
c.Assert(err, IsNil)
pkgs[name] = deb
pkgs[name] = testPackageData{content: deb}
}
archives := map[string]archive.Archive{}
for name, setupArchive := range release.Archives {
archivePkgs := pkgs
if archiveData, ok := test.archives[name]; ok {
archivePkgs = make(map[string]testPackageData, len(archiveData.packages))
for _, pkgData := range archiveData.packages {
// Currently we enforce that package names are unique per archive. This can be
// lifted in future e.g. when dealing with multiple package versions.
if _, exists := archivePkgs[pkgData.name]; exists {
panic(fmt.Sprintf("duplicate package %s in archive %s", pkgData.name, name))
}
archivePkgs[pkgData.name] = pkgData
}
}
archive := &testArchive{
options: archive.Options{
Label: setupArchive.Name,
Expand All @@ -587,7 +688,7 @@ func (s *S) TestRun(c *C) {
Components: setupArchive.Components,
Arch: test.arch,
},
pkgs: pkgs,
pkgs: archivePkgs,
}
archives[name] = archive
}
Expand All @@ -611,8 +712,15 @@ func (s *S) TestRun(c *C) {

if test.result != nil {
result := make(map[string]string, len(copyrightEntries)+len(test.result))
for k, v := range copyrightEntries {
result[k] = v
if test.archives == nil {
// This was added in order to not specify copyright entries for each
// existing test. These tests use only the base-files embedded
// package. Custom packages may not include copyright entries
// though. So if a test defines any custom packages, it must include
// copyright entries explicitly in the results.
for k, v := range copyrightEntries {
result[k] = v
}
}
for k, v := range test.result {
result[k] = v
Expand Down