Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into bboozzoo/systemd-wa…
Browse files Browse the repository at this point in the history
…tchdog-wip
  • Loading branch information
bboozzoo committed Feb 19, 2018
2 parents 72c49cc + 410afe8 commit 2ec062a
Show file tree
Hide file tree
Showing 437 changed files with 14,117 additions and 3,783 deletions.
2 changes: 2 additions & 0 deletions HACKING.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ To run the various tests that we have to ensure a high quality source just run:
This will check if the source format is consistent, that it builds, all tests
work as expected and that "go vet" has nothing to complain.

The source format follows the `gofmt -s` formating. Please run this on your sources files if `run-checks` complains about the format.

You can run individual test for a sub-package by changing into that directory and:

go test -check.f $testname
Expand Down
75 changes: 58 additions & 17 deletions advisor/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,27 @@ import (
"strings"
"time"

"github.com/boltdb/bolt"
"github.com/snapcore/bolt"

"github.com/snapcore/snapd/dirs"
)

var cmdBucketKey = []byte("Commands")
var (
cmdBucketKey = []byte("Commands")
pkgBucketKey = []byte("Snaps")
)

type writer struct {
db *bolt.DB
tx *bolt.Tx
bucket *bolt.Bucket
db *bolt.DB
tx *bolt.Tx
cmdBucket *bolt.Bucket
pkgBucket *bolt.Bucket
}

type CommandDB interface {
// AddSnap adds the entries for commands pointing to the given
// snap name to the commands database.
AddSnap(snapName string, commands []string) error
AddSnap(snapName, summary string, commands []string) error
// Commit persist the changes, and closes the database. If the
// database has already been committed/rollbacked, does nothing.
Commit() error
Expand All @@ -64,12 +68,23 @@ func Create() (CommandDB, error) {

t.tx, err = t.db.Begin(true)
if err == nil {
err = t.tx.DeleteBucket(cmdBucketKey)
err := t.tx.DeleteBucket(cmdBucketKey)
if err == nil || err == bolt.ErrBucketNotFound {
t.bucket, err = t.tx.CreateBucket(cmdBucketKey)
t.cmdBucket, err = t.tx.CreateBucket(cmdBucketKey)
}
if err != nil {
t.tx.Rollback()

}

if err == nil {
err := t.tx.DeleteBucket(pkgBucketKey)
if err == nil || err == bolt.ErrBucketNotFound {
t.pkgBucket, err = t.tx.CreateBucket(pkgBucketKey)
}
if err != nil {
t.tx.Rollback()
}
}
}

Expand All @@ -81,22 +96,26 @@ func Create() (CommandDB, error) {
return t, nil
}

func (t *writer) AddSnap(snapName string, commands []string) error {
func (t *writer) AddSnap(snapName, summary string, commands []string) error {
bname := []byte(snapName)

for _, cmd := range commands {
bcmd := []byte(cmd)
row := t.bucket.Get(bcmd)
row := t.cmdBucket.Get(bcmd)
if row == nil {
row = bname
} else {
row = append(append(row, ','), bname...)
}
if err := t.bucket.Put(bcmd, row); err != nil {
if err := t.cmdBucket.Put(bcmd, row); err != nil {
return err
}
}

if err := t.pkgBucket.Put([]byte(snapName), []byte(summary)); err != nil {
return err
}

return nil
}

Expand All @@ -111,7 +130,8 @@ func (t *writer) Rollback() error {
func (t *writer) done(commit bool) error {
var e1, e2 error

t.bucket = nil
t.cmdBucket = nil
t.pkgBucket = nil
if t.tx != nil {
if commit {
e1 = t.tx.Commit()
Expand All @@ -130,9 +150,10 @@ func (t *writer) done(commit bool) error {
return e1
}

// Dump returns the whole database as a map. For use in testing and debugging.
func Dump() (map[string][]string, error) {
db, err := bolt.Open(dirs.SnapCommandsDB, 0600, &bolt.Options{
// DumpCommands returns the whole database as a map. For use in
// testing and debugging.
func DumpCommands() (map[string][]string, error) {
db, err := bolt.Open(dirs.SnapCommandsDB, 0644, &bolt.Options{
ReadOnly: true,
Timeout: 1 * time.Second,
})
Expand Down Expand Up @@ -167,7 +188,7 @@ type boltFinder struct {

// Open the database for reading.
func Open() (Finder, error) {
db, err := bolt.Open(dirs.SnapCommandsDB, 0600, &bolt.Options{
db, err := bolt.Open(dirs.SnapCommandsDB, 0644, &bolt.Options{
ReadOnly: true,
Timeout: 1 * time.Second,
})
Expand All @@ -178,7 +199,7 @@ func Open() (Finder, error) {
return &boltFinder{*db}, nil
}

func (f *boltFinder) Find(command string) ([]Command, error) {
func (f *boltFinder) FindCommand(command string) ([]Command, error) {
tx, err := f.Begin(false)
if err != nil {
return nil, err
Expand Down Expand Up @@ -206,3 +227,23 @@ func (f *boltFinder) Find(command string) ([]Command, error) {

return cmds, nil
}

func (f *boltFinder) FindPackage(pkgName string) (*Package, error) {
tx, err := f.Begin(false)
if err != nil {
return nil, err
}
defer tx.Rollback()

b := tx.Bucket(pkgBucketKey)
if b == nil {
return nil, nil
}

bsummary := b.Get([]byte(pkgName))
if bsummary == nil {
return nil, nil
}

return &Package{Snap: pkgName, Summary: string(bsummary)}, nil
}
19 changes: 2 additions & 17 deletions advisor/cmdfinder.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,14 @@ type Command struct {
Command string
}

var newFinder = Open

func FindCommand(command string) ([]Command, error) {
finder, err := newFinder()
if err != nil {
return nil, err
}
defer finder.Close()

return finder.Find(command)
return finder.FindCommand(command)
}

const (
Expand Down Expand Up @@ -88,7 +86,7 @@ func FindMisspelledCommand(command string) ([]Command, error) {

alternatives := make([]Command, 0, 32)
for _, w := range similarWords(command) {
res, err := finder.Find(w)
res, err := finder.FindCommand(w)
if err != nil {
return nil, err
}
Expand All @@ -99,16 +97,3 @@ func FindMisspelledCommand(command string) ([]Command, error) {

return alternatives, nil
}

type Finder interface {
Find(command string) ([]Command, error)
Close() error
}

func ReplaceCommandsFinder(constructor func() (Finder, error)) (restore func()) {
old := newFinder
newFinder = constructor
return func() {
newFinder = old
}
}
8 changes: 4 additions & 4 deletions advisor/cmdfinder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func (s *cmdfinderSuite) SetUpTest(c *C) {

db, err := advisor.Create()
c.Assert(err, IsNil)
c.Assert(db.AddSnap("foo", []string{"foo", "meh"}), IsNil)
c.Assert(db.AddSnap("bar", []string{"bar", "meh"}), IsNil)
c.Assert(db.AddSnap("foo", "foo summary", []string{"foo", "meh"}), IsNil)
c.Assert(db.AddSnap("bar", "bar summary", []string{"bar", "meh"}), IsNil)
c.Assert(db.Commit(), IsNil)
}

Expand Down Expand Up @@ -125,8 +125,8 @@ func (s *cmdfinderSuite) TestFindMisspelledCommandMiss(c *C) {
c.Check(cmds, HasLen, 0)
}

func (s *cmdfinderSuite) TestDump(c *C) {
cmds, err := advisor.Dump()
func (s *cmdfinderSuite) TestDumpCommands(c *C) {
cmds, err := advisor.DumpCommands()
c.Assert(err, IsNil)
c.Check(cmds, DeepEquals, map[string][]string{
"foo": {"foo"},
Expand Down
36 changes: 36 additions & 0 deletions advisor/finder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
* Copyright (C) 2018 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 advisor

var newFinder = Open

type Finder interface {
FindCommand(command string) ([]Command, error)
FindPackage(pkgName string) (*Package, error)
Close() error
}

func ReplaceCommandsFinder(constructor func() (Finder, error)) (restore func()) {
old := newFinder
newFinder = constructor
return func() {
newFinder = old
}
}
35 changes: 35 additions & 0 deletions advisor/pkgfinder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
* Copyright (C) 2018 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 advisor

type Package struct {
Snap string
Summary string
}

func FindPackage(pkgName string) (*Package, error) {
finder, err := newFinder()
if err != nil {
return nil, err
}
defer finder.Close()

return finder.FindPackage(pkgName)
}
1 change: 1 addition & 0 deletions arch/arch.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func ubuntuArchFromKernelArch(utsMachine string) string {
"i686": "i386",
"x86_64": "amd64",
"armv7l": "armhf",
"armv8l": "arm64",
"aarch64": "arm64",
"ppc64le": "ppc64el",
"s390x": "s390x",
Expand Down
8 changes: 3 additions & 5 deletions boot/kernel_os_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package boot_test

import (
"io/ioutil"
"path/filepath"
"testing"

Expand All @@ -34,6 +33,7 @@ import (
"github.com/snapcore/snapd/release"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snap/snaptest"
"github.com/snapcore/snapd/testutil"
)

func TestBoot(t *testing.T) { TestingT(t) }
Expand Down Expand Up @@ -97,9 +97,7 @@ func (s *kernelOSSuite) TestExtractKernelAssetsAndRemove(c *C) {
}

fullFn := filepath.Join(kernelAssetsDir, def[0])
content, err := ioutil.ReadFile(fullFn)
c.Assert(err, IsNil)
c.Assert(string(content), Equals, def[1])
c.Check(fullFn, testutil.FileEquals, def[1])
}

// remove
Expand Down Expand Up @@ -152,7 +150,7 @@ func (s *kernelOSSuite) TestSetNextBootOnClassic(c *C) {
defer restore()

// Create a fake OS snap that we try to update
snapInfo := snaptest.MockSnap(c, "name: os\ntype: os", "SNAP", &snap.SideInfo{Revision: snap.R(42)})
snapInfo := snaptest.MockSnap(c, "name: os\ntype: os", &snap.SideInfo{Revision: snap.R(42)})
err := boot.SetNextBoot(snapInfo)
c.Assert(err, IsNil)

Expand Down
6 changes: 5 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,12 @@ type OSRelease struct {
VersionID string `json:"version-id,omitempty"`
}

// RefreshInfo contains information about refreshes.
type RefreshInfo struct {
Schedule string `json:"schedule"`
// Timer contains the refresh.timer setting.
Timer string `json:"timer,omitempty"`
// Schedule contains the legacy refresh.schedule setting.
Schedule string `json:"schedule,omitempty"`
Last string `json:"last,omitempty"`
Next string `json:"next,omitempty"`
}
Expand Down
Loading

0 comments on commit 2ec062a

Please sign in to comment.