Skip to content

Commit

Permalink
many tests: move all panicing fake store methods to a common place
Browse files Browse the repository at this point in the history
Everything that needed a fake store in unit tests was doing its own
thing, stubbing out the unwanted methods with panics. We had four such
things in the code. This implements a single all-panicing store (in
store/storetest), and uses that as the base for the other stores.

The big win of this is that when you add something to
snapstate.StoreService you just have to stub it in one place instead
of N. As long as you remember ... :-)
  • Loading branch information
chipaca committed Aug 15, 2017
1 parent a2ea474 commit e34420b
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 111 deletions.
17 changes: 3 additions & 14 deletions daemon/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import (
"time"

"golang.org/x/crypto/sha3"
"golang.org/x/net/context"
"gopkg.in/check.v1"
"gopkg.in/macaroon.v1"

Expand All @@ -62,16 +61,18 @@ import (
"github.com/snapcore/snapd/overlord/ifacestate"
"github.com/snapcore/snapd/overlord/snapstate"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/progress"
"github.com/snapcore/snapd/release"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snap/snaptest"
"github.com/snapcore/snapd/store"
"github.com/snapcore/snapd/store/storetest"
"github.com/snapcore/snapd/systemd"
"github.com/snapcore/snapd/testutil"
)

type apiBaseSuite struct {
storetest.Store

rsnaps []*snap.Info
err error
vars map[string]string
Expand Down Expand Up @@ -136,10 +137,6 @@ func (s *apiBaseSuite) SuggestedCurrency() string {
return s.suggestedCurrency
}

func (s *apiBaseSuite) Download(context.Context, string, string, *snap.DownloadInfo, progress.Meter, *auth.UserState) error {
panic("Download not expected to be called")
}

func (s *apiBaseSuite) Buy(options *store.BuyOptions, user *auth.UserState) (*store.BuyResult, error) {
s.buyOptions = options
s.user = user
Expand All @@ -151,14 +148,6 @@ func (s *apiBaseSuite) ReadyToBuy(user *auth.UserState) error {
return s.err
}

func (s *apiBaseSuite) Assertion(*asserts.AssertionType, []string, *auth.UserState) (asserts.Assertion, error) {
panic("Assertion not expected to be called")
}

func (s *apiBaseSuite) Sections(*auth.UserState) ([]string, error) {
panic("Sections not expected to be called")
}

func (s *apiBaseSuite) muxVars(*http.Request) map[string]string {
return s.vars
}
Expand Down
40 changes: 2 additions & 38 deletions overlord/assertstate/assertstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"time"

"golang.org/x/crypto/sha3"
"golang.org/x/net/context"

. "gopkg.in/check.v1"

Expand All @@ -41,10 +40,10 @@ import (
"github.com/snapcore/snapd/overlord/auth"
"github.com/snapcore/snapd/overlord/snapstate"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/progress"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snap/snaptest"
"github.com/snapcore/snapd/store"
"github.com/snapcore/snapd/store/storetest"
)

func TestAssertManager(t *testing.T) { TestingT(t) }
Expand All @@ -63,6 +62,7 @@ type assertMgrSuite struct {
var _ = Suite(&assertMgrSuite{})

type fakeStore struct {
storetest.Store
state *state.State
db asserts.RODatabase
}
Expand All @@ -84,42 +84,6 @@ func (sto *fakeStore) Assertion(assertType *asserts.AssertionType, key []string,
return a, nil
}

func (*fakeStore) SnapInfo(store.SnapSpec, *auth.UserState) (*snap.Info, error) {
panic("fakeStore.SnapInfo not expected")
}

func (sto *fakeStore) Find(*store.Search, *auth.UserState) ([]*snap.Info, error) {
panic("fakeStore.Find not expected")
}

func (sto *fakeStore) LookupRefresh(*store.RefreshCandidate, *auth.UserState) (*snap.Info, error) {
panic("fakeStore.LookupRefresh not expected")
}

func (sto *fakeStore) ListRefresh([]*store.RefreshCandidate, *auth.UserState) ([]*snap.Info, error) {
panic("fakeStore.ListRefresh not expected")
}

func (sto *fakeStore) Download(context.Context, string, string, *snap.DownloadInfo, progress.Meter, *auth.UserState) error {
panic("fakeStore.Download not expected")
}

func (sto *fakeStore) SuggestedCurrency() string {
panic("fakeStore.SuggestedCurrency not expected")
}

func (sto *fakeStore) Buy(*store.BuyOptions, *auth.UserState) (*store.BuyResult, error) {
panic("fakeStore.Buy not expected")
}

func (sto *fakeStore) ReadyToBuy(*auth.UserState) error {
panic("fakeStore.ReadyToBuy not expected")
}

func (sto *fakeStore) Sections(*auth.UserState) ([]string, error) {
panic("fakeStore.Sections not expected")
}

func (s *assertMgrSuite) SetUpTest(c *C) {
dirs.SetRootDir(c.MkDir())

Expand Down
41 changes: 3 additions & 38 deletions overlord/devicestate/devicestate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"testing"
"time"

"golang.org/x/net/context"
. "gopkg.in/check.v1"
"gopkg.in/tomb.v2"
"gopkg.in/yaml.v2"
Expand All @@ -49,11 +48,11 @@ import (
"github.com/snapcore/snapd/overlord/snapstate"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/partition"
"github.com/snapcore/snapd/progress"
"github.com/snapcore/snapd/release"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snap/snaptest"
"github.com/snapcore/snapd/store"
"github.com/snapcore/snapd/store/storetest"
)

func TestDeviceManager(t *testing.T) { TestingT(t) }
Expand All @@ -76,6 +75,8 @@ var _ = Suite(&deviceMgrSuite{})
var testKeyLength = 1024

type fakeStore struct {
storetest.Store

state *state.State
db asserts.RODatabase
}
Expand All @@ -97,42 +98,6 @@ func (sto *fakeStore) Assertion(assertType *asserts.AssertionType, key []string,
return a, nil
}

func (*fakeStore) SnapInfo(store.SnapSpec, *auth.UserState) (*snap.Info, error) {
panic("fakeStore.SnapInfo not expected")
}

func (sto *fakeStore) Find(*store.Search, *auth.UserState) ([]*snap.Info, error) {
panic("fakeStore.Find not expected")
}

func (sto *fakeStore) LookupRefresh(*store.RefreshCandidate, *auth.UserState) (*snap.Info, error) {
panic("fakeStore.LookupRefresh not expected")
}

func (sto *fakeStore) ListRefresh([]*store.RefreshCandidate, *auth.UserState) ([]*snap.Info, error) {
panic("fakeStore.ListRefresh not expected")
}

func (sto *fakeStore) Download(context.Context, string, string, *snap.DownloadInfo, progress.Meter, *auth.UserState) error {
panic("fakeStore.Download not expected")
}

func (sto *fakeStore) SuggestedCurrency() string {
panic("fakeStore.SuggestedCurrency not expected")
}

func (sto *fakeStore) Buy(*store.BuyOptions, *auth.UserState) (*store.BuyResult, error) {
panic("fakeStore.Buy not expected")
}

func (sto *fakeStore) ReadyToBuy(*auth.UserState) error {
panic("fakeStore.ReadyToBuy not expected")
}

func (sto *fakeStore) Sections(*auth.UserState) ([]string, error) {
panic("fakeStore.Sections not expected")
}

func (s *deviceMgrSuite) SetUpTest(c *C) {
dirs.SetRootDir(c.MkDir())
os.MkdirAll(dirs.SnapRunDir, 0755)
Expand Down
24 changes: 3 additions & 21 deletions overlord/snapstate/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ import (

"golang.org/x/net/context"

"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/overlord/auth"
"github.com/snapcore/snapd/overlord/snapstate"
"github.com/snapcore/snapd/overlord/snapstate/backend"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/progress"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/store"
"github.com/snapcore/snapd/store/storetest"
)

type fakeOp struct {
Expand Down Expand Up @@ -90,6 +90,8 @@ type fakeDownload struct {
}

type fakeStore struct {
storetest.Store

downloads []fakeDownload
fakeBackend *fakeSnappyBackend
fakeCurrentProgress int
Expand Down Expand Up @@ -147,10 +149,6 @@ func (f *fakeStore) SnapInfo(spec store.SnapSpec, user *auth.UserState) (*snap.I
return info, nil
}

func (f *fakeStore) Find(search *store.Search, user *auth.UserState) ([]*snap.Info, error) {
panic("Find called")
}

func (f *fakeStore) LookupRefresh(cand *store.RefreshCandidate, user *auth.UserState) (*snap.Info, error) {
f.pokeStateLock()

Expand Down Expand Up @@ -271,22 +269,6 @@ func (f *fakeStore) Download(ctx context.Context, name, targetFn string, snapInf
return nil
}

func (f *fakeStore) Buy(options *store.BuyOptions, user *auth.UserState) (*store.BuyResult, error) {
panic("Never expected fakeStore.Buy to be called")
}

func (f *fakeStore) ReadyToBuy(user *auth.UserState) error {
panic("Never expected fakeStore.ReadyToBuy to be called")
}

func (f *fakeStore) Assertion(*asserts.AssertionType, []string, *auth.UserState) (asserts.Assertion, error) {
panic("Never expected fakeStore.Assertion to be called")
}

func (f *fakeStore) Sections(user *auth.UserState) ([]string, error) {
panic("Sections called")
}

type fakeSnappyBackend struct {
ops fakeOps

Expand Down
72 changes: 72 additions & 0 deletions store/storetest/storetest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
* Copyright (C) 2014-2017 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 storetest

import (
"golang.org/x/net/context"

"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/overlord/auth"
"github.com/snapcore/snapd/progress"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/store"
)

type Store struct{}

func (Store) SnapInfo(store.SnapSpec, *auth.UserState) (*snap.Info, error) {
panic("Store.SnapInfo not expected")
}

func (Store) Find(*store.Search, *auth.UserState) ([]*snap.Info, error) {
panic("Store.Find not expected")
}

func (Store) LookupRefresh(*store.RefreshCandidate, *auth.UserState) (*snap.Info, error) {
panic("Store.LookupRefresh not expected")
}

func (Store) ListRefresh([]*store.RefreshCandidate, *auth.UserState) ([]*snap.Info, error) {
panic("Store.ListRefresh not expected")
}

func (Store) Download(context.Context, string, string, *snap.DownloadInfo, progress.Meter, *auth.UserState) error {
panic("Store.Download not expected")
}

func (Store) SuggestedCurrency() string {
panic("Store.SuggestedCurrency not expected")
}

func (Store) Buy(*store.BuyOptions, *auth.UserState) (*store.BuyResult, error) {
panic("Store.Buy not expected")
}

func (Store) ReadyToBuy(*auth.UserState) error {
panic("Store.ReadyToBuy not expected")
}

func (Store) Sections(*auth.UserState) ([]string, error) {
panic("Store.Sections not expected")
}

func (Store) Assertion(assertType *asserts.AssertionType, key []string, _ *auth.UserState) (asserts.Assertion, error) {
panic("Store.Assertion not expected")
}
28 changes: 28 additions & 0 deletions store/storetest/storetest_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
* Copyright (C) 2017 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 storetest_test

import (
"github.com/snapcore/snapd/overlord/snapstate"
"github.com/snapcore/snapd/store/storetest"
)

// this is all we need to test
var _ snapstate.StoreService = storetest.Store{}

0 comments on commit e34420b

Please sign in to comment.