Skip to content

Commit

Permalink
archive: Add Info() method
Browse files Browse the repository at this point in the history
At some point we will need to get information about (deb) packages from
(Go) packages other than archive, namely from the slicer package.
For instance:
- Package entries in Chisel DB will include version, digest and
  architecture.
- Multi-archive package selection will need to know versions of a
  package in all configured archives.

Add Info() method to the Archive interface that returns a new
PackageInfo interface that contains accessors for the underlying
control.Section of the package. Currently these accessors are Name(),
Version(), Arch() and SHA256() methods.

The reason to introduce PackageInfo interface instead of returing
control.Section directly is keep the archive abstraction and not leak
implementation details.
  • Loading branch information
woky committed Sep 19, 2023
1 parent 61d2770 commit ddd61ef
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
24 changes: 24 additions & 0 deletions internal/archive/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@ import (
"github.com/canonical/chisel/internal/deb"
)

type PackageInfo interface {
Name() string
Version() string
Arch() string
SHA256() string
}

type Archive interface {
Options() *Options
Fetch(pkg string) (io.ReadCloser, error)
Info(pkg string) PackageInfo
Exists(pkg string) bool
}

Expand Down Expand Up @@ -86,6 +94,22 @@ func (a *ubuntuArchive) Exists(pkg string) bool {
return err == nil
}

type pkgInfo struct{ control.Section }

var _ PackageInfo = pkgInfo{}

func (info pkgInfo) Name() string { return info.Get("Package") }
func (info pkgInfo) Version() string { return info.Get("Version") }
func (info pkgInfo) Arch() string { return info.Get("Architecture") }
func (info pkgInfo) SHA256() string { return info.Get("SHA256") }

func (a *ubuntuArchive) Info(pkg string) PackageInfo {
if section, _, _ := a.selectPackage(pkg); section != nil {
return &pkgInfo{section}
}
return nil
}

func (a *ubuntuArchive) selectPackage(pkg string) (control.Section, *ubuntuIndex, error) {
var selectedVersion string
var selectedSection control.Section
Expand Down
33 changes: 33 additions & 0 deletions internal/archive/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,39 @@ func (s *httpSuite) TestArchiveLabels(c *C) {
c.Assert(err, ErrorMatches, `.*\bno Ubuntu section`)
}

func (s *httpSuite) TestPackageInfo(c *C) {
s.prepareArchive("jammy", "22.04", "amd64", []string{"main", "universe"})

options := archive.Options{
Label: "ubuntu",
Version: "22.04",
Arch: "amd64",
Suites: []string{"jammy"},
Components: []string{"main", "universe"},
CacheDir: c.MkDir(),
}

archive, err := archive.Open(&options)
c.Assert(err, IsNil)

info1 := archive.Info("mypkg1")
c.Assert(info1, NotNil)
c.Assert(info1.Name(), Equals, "mypkg1")
c.Assert(info1.Version(), Equals, "1.1")
c.Assert(info1.Arch(), Equals, "amd64")
c.Assert(info1.SHA256(), Equals, "1f08ef04cfe7a8087ee38a1ea35fa1810246648136c3c42d5a61ad6503d85e05")

info3 := archive.Info("mypkg3")
c.Assert(info3, NotNil)
c.Assert(info3.Name(), Equals, "mypkg3")
c.Assert(info3.Version(), Equals, "1.3")
c.Assert(info3.Arch(), Equals, "amd64")
c.Assert(info3.SHA256(), Equals, "fe377bf13ba1a5cb287cb4e037e6e7321281c929405ae39a72358ef0f5d179aa")

info99 := archive.Info("mypkg99")
c.Assert(info99, IsNil)
}

func read(r io.Reader) string {
data, err := ioutil.ReadAll(r)
if err != nil {
Expand Down
29 changes: 29 additions & 0 deletions internal/slicer/slicer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var (
)

type testPackage struct {
info map[string]string
content []byte
}

Expand Down Expand Up @@ -608,6 +609,22 @@ const defaultChiselYaml = `
components: [main, universe]
`

type testPackageInfo map[string]string

var _ archive.PackageInfo = (testPackageInfo)(nil)

func (info testPackageInfo) Name() string { return info["Package"] }
func (info testPackageInfo) Version() string { return info["Version"] }
func (info testPackageInfo) Arch() string { return info["Architecture"] }
func (info testPackageInfo) SHA256() string { return info["SHA256"] }

func (s testPackageInfo) Get(key string) (value string) {
if s != nil {
value = s[key]
}
return
}

type testArchive struct {
options archive.Options
pkgs map[string]testPackage
Expand All @@ -629,6 +646,18 @@ func (a *testArchive) Exists(pkg string) bool {
return ok
}

func (a *testArchive) Info(pkg string) archive.PackageInfo {
var info map[string]string
if pkgData, ok := a.pkgs[pkg]; ok {
if info = pkgData.info; info == nil {
info = map[string]string{
"Version": "1.0",
}
}
}
return testPackageInfo(info)
}

func (s *S) TestRun(c *C) {
for _, test := range slicerTests {
c.Logf("Summary: %s", test.summary)
Expand Down

0 comments on commit ddd61ef

Please sign in to comment.