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

db: Add types #71

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
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
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 := io.ReadAll(r)
if err != nil {
Expand Down
80 changes: 80 additions & 0 deletions internal/db/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package db

import (
"fmt"
"os"
"path/filepath"

"github.com/canonical/chisel/internal/jsonwall"
"github.com/klauspost/compress/zstd"
)

const schema = "0.1"

// New creates a new Chisel DB writer with the proper schema.
func New() *jsonwall.DBWriter {
options := jsonwall.DBWriterOptions{Schema: schema}
return jsonwall.NewDBWriter(&options)
}

func getDBPath(root string) string {
return filepath.Join(root, ".chisel.db")
}

// Save uses the provided writer dbw to write the Chisel DB into the standard
// path under the provided root directory.
func Save(dbw *jsonwall.DBWriter, root string) (err error) {
dbPath := getDBPath(root)
defer func() {
if err != nil {
err = fmt.Errorf("cannot save state to %q: %w", dbPath, err)
}
}()
f, err := os.OpenFile(dbPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return
}
defer f.Close()
// chmod the existing file
if err = f.Chmod(0644); err != nil {
return
}
zw, err := zstd.NewWriter(f)
if err != nil {
return
}
if _, err = dbw.WriteTo(zw); err != nil {
return
}
return zw.Close()
}

// Load reads a Chisel DB from the standard path under the provided root
// directory. If the Chisel DB doesn't exist, the returned error satisfies
// errors.Is(err, fs.ErrNotExist))
func Load(root string) (db *jsonwall.DB, err error) {
dbPath := getDBPath(root)
defer func() {
if err != nil {
err = fmt.Errorf("cannot load state from %q: %w", dbPath, err)
}
}()
f, err := os.Open(dbPath)
if err != nil {
return
}
defer f.Close()
zr, err := zstd.NewReader(f)
if err != nil {
return
}
defer zr.Close()
db, err = jsonwall.ReadDB(zr)
if err != nil {
return nil, err
}
if s := db.Schema(); s != schema {
return nil, fmt.Errorf("invalid schema %#v", s)
}
return
}
74 changes: 74 additions & 0 deletions internal/db/db_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package db_test

import (
"sort"

"github.com/canonical/chisel/internal/db"
. "gopkg.in/check.v1"
)

type testEntry struct {
S string `json:"s,omitempty"`
I int64 `json:"i,omitempty"`
L []string `json:"l,omitempty"`
M map[string]bool `json:"m,omitempty"`
}

var saveLoadTestCase = []testEntry{
{"", 0, nil, nil},
{"hello", -1, nil, nil},
{"", 0, nil, nil},
{"", 100, []string{"a", "b"}, nil},
{"", 0, nil, map[string]bool{"a": true, "b": false}},
{"abc", 123, []string{"foo", "bar"}, nil},
}

func (s *S) TestSaveLoadRoundTrip(c *C) {
// To compare expected and obtained entries we first wrap the original
// entries in wrappers with increasing K. When we read the wrappers back
// they may be in different order because jsonwall sorts them serialized
// as JSON. So we sort them by K to compare them in the original order.

type wrapper struct {
// test values
testEntry
// sort key for comparison
K int `json:"key"`
}

// wrap the entries with increasing K
expected := make([]wrapper, len(saveLoadTestCase))
for i, entry := range saveLoadTestCase {
expected[i] = wrapper{entry, i}
}

workDir := c.MkDir()
dbw := db.New()
for _, entry := range expected {
err := dbw.Add(entry)
c.Assert(err, IsNil)
}
err := db.Save(dbw, workDir)
c.Assert(err, IsNil)

dbr, err := db.Load(workDir)
c.Assert(err, IsNil)
c.Assert(dbr.Schema(), Equals, db.Schema)

iter, err := dbr.Iterate(nil)
c.Assert(err, IsNil)

obtained := make([]wrapper, 0, len(expected))
for iter.Next() {
var wrapped wrapper
err := iter.Get(&wrapped)
c.Assert(err, IsNil)
obtained = append(obtained, wrapped)
}

// sort the entries by K to get the original order
sort.Slice(obtained, func(i, j int) bool {
return obtained[i].K < obtained[j].K
})
c.Assert(obtained, DeepEquals, expected)
}
3 changes: 3 additions & 0 deletions internal/db/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package db

var Schema = schema
15 changes: 15 additions & 0 deletions internal/db/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package db_test

import (
"testing"

. "gopkg.in/check.v1"
)

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

type S struct{}

var _ = Suite(&S{})
Loading