From 08ce9bd3d5f0a32b5fde579bae1aea295a5b75ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Virtus?= Date: Fri, 5 May 2023 00:44:51 +0200 Subject: [PATCH] Support archive priorities --- cmd/chisel/cmd_cut.go | 1 + internal/archive/archive.go | 1 + internal/setup/setup.go | 3 + internal/setup/setup_test.go | 22 +++--- internal/slicer/slicer.go | 21 +++++- internal/slicer/slicer_test.go | 118 ++++++++++++++++++++++++++++----- 6 files changed, 139 insertions(+), 27 deletions(-) diff --git a/cmd/chisel/cmd_cut.go b/cmd/chisel/cmd_cut.go index 1a79ec83..2b0e9204 100644 --- a/cmd/chisel/cmd_cut.go +++ b/cmd/chisel/cmd_cut.go @@ -92,6 +92,7 @@ func (cmd *cmdCut) Execute(args []string) error { Components: archiveInfo.Components, CacheDir: cache.DefaultDir("chisel"), Pro: archiveInfo.Pro, + Priority: archiveInfo.Priority, }) if err != nil { return err diff --git a/internal/archive/archive.go b/internal/archive/archive.go index e2b663ce..3b01d0f5 100644 --- a/internal/archive/archive.go +++ b/internal/archive/archive.go @@ -28,6 +28,7 @@ type Options struct { Components []string CacheDir string Pro string + Priority int } func Open(options *Options) (Archive, error) { diff --git a/internal/setup/setup.go b/internal/setup/setup.go index f0b66274..0f343a9f 100644 --- a/internal/setup/setup.go +++ b/internal/setup/setup.go @@ -30,6 +30,7 @@ type Archive struct { Suites []string Components []string Pro string + Priority int } // Package holds a collection of slices that represent parts of themselves. @@ -323,6 +324,7 @@ type yamlArchive struct { Suites []string `yaml:"suites"` Components []string `yaml:"components"` Pro string `yaml:"pro"` + Priority int `yaml:"priority"` } type yamlPackage struct { @@ -435,6 +437,7 @@ func parseRelease(baseDir, filePath string, data []byte) (*Release, error) { Suites: details.Suites, Components: details.Components, Pro: details.Pro, + Priority: details.Priority, } } diff --git a/internal/setup/setup_test.go b/internal/setup/setup_test.go index 7b7361db..a1e01ce2 100644 --- a/internal/setup/setup_test.go +++ b/internal/setup/setup_test.go @@ -62,7 +62,7 @@ var setupTests = []setupTest{{ `, }, release: &setup.Release{ - Archives: map[string]*setup.Archive{"ubuntu": {"ubuntu", "22.04", []string{"jammy", "jammy-security"}, []string{"main", "other"}, ""}}, + Archives: map[string]*setup.Archive{"ubuntu": {"ubuntu", "22.04", []string{"jammy", "jammy-security"}, []string{"main", "other"}, "", 0}}, Packages: map[string]*setup.Package{ "mypkg": { Name: "mypkg", @@ -95,7 +95,7 @@ var setupTests = []setupTest{{ `, }, release: &setup.Release{ - Archives: map[string]*setup.Archive{"ubuntu": {"ubuntu", "22.04", []string{"jammy"}, []string{"main", "universe"}, ""}}, + Archives: map[string]*setup.Archive{"ubuntu": {"ubuntu", "22.04", []string{"jammy"}, []string{"main", "universe"}, "", 0}}, Packages: map[string]*setup.Package{ "mypkg": { Name: "mypkg", @@ -146,7 +146,7 @@ var setupTests = []setupTest{{ `, }, release: &setup.Release{ - Archives: map[string]*setup.Archive{"ubuntu": {"ubuntu", "22.04", []string{"jammy"}, []string{"main", "universe"}, ""}}, + Archives: map[string]*setup.Archive{"ubuntu": {"ubuntu", "22.04", []string{"jammy"}, []string{"main", "universe"}, "", 0}}, Packages: map[string]*setup.Package{ "mypkg": { Name: "mypkg", @@ -396,7 +396,7 @@ var setupTests = []setupTest{{ `, }, release: &setup.Release{ - Archives: map[string]*setup.Archive{"ubuntu": {"ubuntu", "22.04", []string{"jammy"}, []string{"main", "universe"}, ""}}, + Archives: map[string]*setup.Archive{"ubuntu": {"ubuntu", "22.04", []string{"jammy"}, []string{"main", "universe"}, "", 0}}, Packages: map[string]*setup.Package{ "mypkg": { Name: "mypkg", @@ -600,7 +600,7 @@ var setupTests = []setupTest{{ `, }, release: &setup.Release{ - Archives: map[string]*setup.Archive{"ubuntu": {"ubuntu", "22.04", []string{"jammy"}, []string{"main", "universe"}, ""}}, + Archives: map[string]*setup.Archive{"ubuntu": {"ubuntu", "22.04", []string{"jammy"}, []string{"main", "universe"}, "", 0}}, Packages: map[string]*setup.Package{ "mypkg": { Name: "mypkg", @@ -629,7 +629,7 @@ var setupTests = []setupTest{{ `, }, release: &setup.Release{ - Archives: map[string]*setup.Archive{"ubuntu": {"ubuntu", "22.04", []string{"jammy"}, []string{"main", "universe"}, ""}}, + Archives: map[string]*setup.Archive{"ubuntu": {"ubuntu", "22.04", []string{"jammy"}, []string{"main", "universe"}, "", 0}}, Packages: map[string]*setup.Package{ "mypkg": { Name: "mypkg", @@ -659,7 +659,7 @@ var setupTests = []setupTest{{ `, }, release: &setup.Release{ - Archives: map[string]*setup.Archive{"ubuntu": {"ubuntu", "22.04", []string{"jammy"}, []string{"main", "universe"}, ""}}, + Archives: map[string]*setup.Archive{"ubuntu": {"ubuntu", "22.04", []string{"jammy"}, []string{"main", "universe"}, "", 0}}, Packages: map[string]*setup.Package{ "mypkg": { Name: "mypkg", @@ -698,8 +698,8 @@ var setupTests = []setupTest{{ }, release: &setup.Release{ Archives: map[string]*setup.Archive{ - "foo": {"foo", "22.04", []string{"jammy"}, []string{"main", "universe"}, ""}, - "bar": {"bar", "22.04", []string{"jammy-updates"}, []string{"universe"}, ""}, + "foo": {"foo", "22.04", []string{"jammy"}, []string{"main", "universe"}, "", 0}, + "bar": {"bar", "22.04", []string{"jammy-updates"}, []string{"universe"}, "", 0}, }, Packages: map[string]*setup.Package{ "mypkg": { @@ -731,8 +731,8 @@ var setupTests = []setupTest{{ }, release: &setup.Release{ Archives: map[string]*setup.Archive{ - "ubuntu": {"ubuntu", "22.04", []string{"jammy", "jammy-updates", "jammy-security"}, []string{"main", "universe"}, ""}, - "ubuntu-fips": {"ubuntu-fips", "22.04", []string{"jammy"}, []string{"main"}, "fips"}, + "ubuntu": {"ubuntu", "22.04", []string{"jammy", "jammy-updates", "jammy-security"}, []string{"main", "universe"}, "", 0}, + "ubuntu-fips": {"ubuntu-fips", "22.04", []string{"jammy"}, []string{"main"}, "fips", 0}, }, Packages: map[string]*setup.Package{ "mypkg": { diff --git a/internal/slicer/slicer.go b/internal/slicer/slicer.go index 8b1d4973..2b17e458 100644 --- a/internal/slicer/slicer.go +++ b/internal/slicer/slicer.go @@ -5,8 +5,10 @@ import ( "bytes" "fmt" "io" + "math" "os" "path/filepath" + "sort" "strings" "syscall" @@ -69,13 +71,30 @@ func Run(options *RunOptions) error { targetDirAbs = filepath.Join(dir, targetDir) } + orderedArchives := make([]archive.Archive, 0, len(options.Archives)) + for _, archive := range options.Archives { + orderedArchives = append(orderedArchives, archive) + } + sort.Slice(orderedArchives, func(a, b int) bool { + prioA := orderedArchives[a].Options().Priority + prioB := orderedArchives[b].Options().Priority + return prioA > prioB + }) + // Build information to process the selection. for _, slice := range options.Selection.Slices { extractPackage := extract[slice.Package] if extractPackage == nil { var selectedVersion string var selectedArchive archive.Archive - for _, archive := range options.Archives { + currentPrio := math.MaxInt + for _, archive := range orderedArchives { + if prio := archive.Options().Priority; prio < currentPrio { + if selectedVersion != "" { + break + } + currentPrio = prio + } version := archive.Version(slice.Package) if version == "" { continue diff --git a/internal/slicer/slicer_test.go b/internal/slicer/slicer_test.go index 9b519ab9..4d719c9e 100644 --- a/internal/slicer/slicer_test.go +++ b/internal/slicer/slicer_test.go @@ -669,6 +669,84 @@ var slicerTests = []slicerTest{{ "/speed/cheetah": "file 0644 e98b0879", "/speed/ostrich": "file 0644 c8fa2806", }, +}, { + summary: "Pick package from archive with highest priority", + pkgs: []testPackageInfo{ + { + name: "hello", + version: "2.0-beta", + archive: "edge", + content: []testutil.TarEntry{ + {Header: tar.Header{Name: "./"}}, + {Header: tar.Header{Name: "./hello"}, Content: []byte("Hello, The Edge\n")}, + }, + }, + { + name: "hello", + version: "1.8", + archive: "candidate", + content: []testutil.TarEntry{ + {Header: tar.Header{Name: "./"}}, + {Header: tar.Header{Name: "./hello"}, Content: []byte("Hello, The Candidate\n")}, + }, + }, + { + name: "hello", + version: "1.2", + archive: "stable", + content: []testutil.TarEntry{ + {Header: tar.Header{Name: "./"}}, + {Header: tar.Header{Name: "./hello"}, Content: []byte("Hello, The Stable\n")}, + }, + }, + { + name: "hello", + version: "1.0", + archive: "obsolete", + content: []testutil.TarEntry{ + {Header: tar.Header{Name: "./"}}, + {Header: tar.Header{Name: "./hello"}, Content: []byte("Hello, The Obsolete\n")}, + }, + }, + }, + release: map[string]string{ + "chisel.yaml": ` + format: chisel-v1 + archives: + edge: + version: 1 + suites: [main] + components: [main] + candidate: + version: 1 + suites: [main] + components: [main] + priority: 5 + stable: + version: 1 + suites: [main] + components: [main] + priority: 10 + obsolete: + version: 1 + suites: [main] + components: [main] + priority: 10 + `, + "slices/mydir/hello.yaml": ` + package: hello + slices: + all: + contents: + /hello: + `, + }, + slices: []setup.SliceKey{ + {"hello", "all"}, + }, + result: map[string]string{ + "/hello": "file 0644 b5621b65", + }, }} const defaultChiselYaml = ` @@ -685,12 +763,12 @@ type testPackage struct { } type testArchive struct { - arch string - pkgs map[string]testPackage + options archive.Options + pkgs map[string]testPackage } func (a *testArchive) Options() *archive.Options { - return &archive.Options{Arch: a.arch} + return &a.options } func (a *testArchive) Fetch(pkg string) (io.ReadCloser, error) { @@ -731,17 +809,28 @@ func (s *S) TestRun(c *C) { c.Assert(err, IsNil) archives := map[string]archive.Archive{} + for name, setupArchive := range release.Archives { + archive := &testArchive{ + options: archive.Options{ + Label: setupArchive.Name, + Version: setupArchive.Version, + Arch: test.arch, + Suites: setupArchive.Suites, + Components: setupArchive.Components, + Pro: setupArchive.Pro, + Priority: setupArchive.Priority, + }, + } + if test.pkgs != nil { + archive.pkgs = map[string]testPackage{} + } + archives[name] = archive + } if test.pkgs != nil { for _, pkgInfo := range test.pkgs { - archive, ok := archives[pkgInfo.archive].(*testArchive) - if !ok { - archive = &testArchive{ - arch: test.arch, - pkgs: map[string]testPackage{}, - } - archives[pkgInfo.archive] = archive - } + archive := archives[pkgInfo.archive].(*testArchive) + c.Assert(archive, NotNil) deb, err := testutil.MakeDeb(pkgInfo.content) c.Assert(err, IsNil) archive.pkgs[pkgInfo.name] = testPackage{pkgInfo.version, deb} @@ -755,10 +844,9 @@ func (s *S) TestRun(c *C) { c.Assert(err, IsNil) pkgs[name] = testPackage{"1", deb} } - archives["ubuntu"] = &testArchive{ - arch: test.arch, - pkgs: pkgs, - } + archive := archives["ubuntu"].(*testArchive) + c.Assert(archive, NotNil) + archive.pkgs = pkgs } targetDir := c.MkDir()