From e34420b35476e6a0dd9bfeb849fbe93cd5f76448 Mon Sep 17 00:00:00 2001 From: "John R. Lenton" Date: Tue, 15 Aug 2017 16:12:08 +0100 Subject: [PATCH] many tests: move all panicing fake store methods to a common place 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 ... :-) --- daemon/api_test.go | 17 +----- overlord/assertstate/assertstate_test.go | 40 +------------ overlord/devicestate/devicestate_test.go | 41 +------------- overlord/snapstate/backend_test.go | 24 +------- store/storetest/storetest.go | 72 ++++++++++++++++++++++++ store/storetest/storetest_test.go | 28 +++++++++ 6 files changed, 111 insertions(+), 111 deletions(-) create mode 100644 store/storetest/storetest.go create mode 100644 store/storetest/storetest_test.go diff --git a/daemon/api_test.go b/daemon/api_test.go index e5854615a54..1144ad05169 100644 --- a/daemon/api_test.go +++ b/daemon/api_test.go @@ -44,7 +44,6 @@ import ( "time" "golang.org/x/crypto/sha3" - "golang.org/x/net/context" "gopkg.in/check.v1" "gopkg.in/macaroon.v1" @@ -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 @@ -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 @@ -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 } diff --git a/overlord/assertstate/assertstate_test.go b/overlord/assertstate/assertstate_test.go index ea04ce27d4f..a15710ce2ad 100644 --- a/overlord/assertstate/assertstate_test.go +++ b/overlord/assertstate/assertstate_test.go @@ -29,7 +29,6 @@ import ( "time" "golang.org/x/crypto/sha3" - "golang.org/x/net/context" . "gopkg.in/check.v1" @@ -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) } @@ -63,6 +62,7 @@ type assertMgrSuite struct { var _ = Suite(&assertMgrSuite{}) type fakeStore struct { + storetest.Store state *state.State db asserts.RODatabase } @@ -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()) diff --git a/overlord/devicestate/devicestate_test.go b/overlord/devicestate/devicestate_test.go index ef8344af845..a565b20beaa 100644 --- a/overlord/devicestate/devicestate_test.go +++ b/overlord/devicestate/devicestate_test.go @@ -31,7 +31,6 @@ import ( "testing" "time" - "golang.org/x/net/context" . "gopkg.in/check.v1" "gopkg.in/tomb.v2" "gopkg.in/yaml.v2" @@ -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) } @@ -76,6 +75,8 @@ var _ = Suite(&deviceMgrSuite{}) var testKeyLength = 1024 type fakeStore struct { + storetest.Store + state *state.State db asserts.RODatabase } @@ -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) diff --git a/overlord/snapstate/backend_test.go b/overlord/snapstate/backend_test.go index a21e3711099..b1ba2f5d786 100644 --- a/overlord/snapstate/backend_test.go +++ b/overlord/snapstate/backend_test.go @@ -27,7 +27,6 @@ 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" @@ -35,6 +34,7 @@ import ( "github.com/snapcore/snapd/progress" "github.com/snapcore/snapd/snap" "github.com/snapcore/snapd/store" + "github.com/snapcore/snapd/store/storetest" ) type fakeOp struct { @@ -90,6 +90,8 @@ type fakeDownload struct { } type fakeStore struct { + storetest.Store + downloads []fakeDownload fakeBackend *fakeSnappyBackend fakeCurrentProgress int @@ -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() @@ -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 diff --git a/store/storetest/storetest.go b/store/storetest/storetest.go new file mode 100644 index 00000000000..cce7fcb342a --- /dev/null +++ b/store/storetest/storetest.go @@ -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 . + * + */ + +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") +} diff --git a/store/storetest/storetest_test.go b/store/storetest/storetest_test.go new file mode 100644 index 00000000000..86154dfc4c6 --- /dev/null +++ b/store/storetest/storetest_test.go @@ -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 . + * + */ + +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{}