From 92c17b754cf745128dedd443ca96b5b6832558ee Mon Sep 17 00:00:00 2001 From: David Lawrence Date: Mon, 11 Apr 2016 13:19:06 -0700 Subject: [PATCH] make notary-signer tests work and add test for signer bootstrap Signed-off-by: David Lawrence (github: endophage) --- cmd/notary-server/bootstrap_test.go | 24 ++++++++++++++++++++++++ cmd/notary-server/config.go | 7 +++---- cmd/notary-server/main_test.go | 2 +- cmd/notary-signer/main.go | 2 +- cmd/notary-signer/main_test.go | 14 +++++++++++++- server/storage/tuf_store.go | 14 +++++++------- server/storage/tuf_store_test.go | 6 +++--- tuf/testutils/bootstrap.go | 10 ++++++++++ 8 files changed, 62 insertions(+), 17 deletions(-) create mode 100644 cmd/notary-server/bootstrap_test.go create mode 100644 tuf/testutils/bootstrap.go diff --git a/cmd/notary-server/bootstrap_test.go b/cmd/notary-server/bootstrap_test.go new file mode 100644 index 0000000000..122c76edab --- /dev/null +++ b/cmd/notary-server/bootstrap_test.go @@ -0,0 +1,24 @@ +package main + +import ( + "testing" + + "github.com/docker/notary/tuf/testutils" + "github.com/stretchr/testify/require" + "golang.org/x/net/context" +) + +func TestBootstrap(t *testing.T) { + ctx := context.Background() + err := bootstrap(ctx) + require.Error(t, err) + ctx = context.WithValue(ctx, "metaStore", 1) + err = bootstrap(ctx) + require.Error(t, err) + require.Contains(t, err.Error(), "does not support bootstrapping") + bs := &testutils.TestBootstrapper{} + ctx = context.WithValue(ctx, "metaStore", bs) + err = bootstrap(ctx) + require.NoError(t, err) + require.True(t, bs.Booted) +} diff --git a/cmd/notary-server/config.go b/cmd/notary-server/config.go index 989e1473e3..336736f774 100644 --- a/cmd/notary-server/config.go +++ b/cmd/notary-server/config.go @@ -80,9 +80,8 @@ func getStore(configuration *viper.Viper, hRegister healthRegister) ( if err != nil { return nil, err } - var s *storage.SQLStorage - s, err = storage.NewSQLStorage(storeConfig.Backend, storeConfig.Source) - store = *storage.NewTufMetaStorage(s) + s, err := storage.NewSQLStorage(storeConfig.Backend, storeConfig.Source) + store = *storage.NewTUFMetaStorage(s) hRegister("DB operational", s.CheckHealth, time.Second*60) case notary.RethinkDBBackend: var sess *gorethink.Session @@ -93,7 +92,7 @@ func getStore(configuration *viper.Viper, hRegister healthRegister) ( sess, err = rethinkdb.Connection(storeConfig.CA, storeConfig.Source) if err == nil { s := storage.NewRethinkDBStorage(storeConfig.DBName, sess) - store = *storage.NewTufMetaStorage(s) + store = *storage.NewTUFMetaStorage(s) hRegister("DB operational", s.CheckHealth, time.Second*60) } default: diff --git a/cmd/notary-server/main_test.go b/cmd/notary-server/main_test.go index d1c3824c8c..2a45ee510b 100644 --- a/cmd/notary-server/main_test.go +++ b/cmd/notary-server/main_test.go @@ -330,7 +330,7 @@ func TestGetStoreDBStore(t *testing.T) { store, err := getStore(configure(config), fakeRegister) require.NoError(t, err) - _, ok := store.(storage.TufMetaStorage) + _, ok := store.(storage.TUFMetaStorage) require.True(t, ok) // health function registered diff --git a/cmd/notary-signer/main.go b/cmd/notary-signer/main.go index 8870e67058..07cff4a31f 100644 --- a/cmd/notary-signer/main.go +++ b/cmd/notary-signer/main.go @@ -284,7 +284,7 @@ func debugServer(addr string) { } } -func bootstrap(s trustmanager.KeyStore) error { +func bootstrap(s interface{}) error { store, ok := s.(storage.Bootstrapper) if !ok { return fmt.Errorf("Store does not support bootstrapping.") diff --git a/cmd/notary-signer/main_test.go b/cmd/notary-signer/main_test.go index 9800b783e2..7c411d3667 100644 --- a/cmd/notary-signer/main_test.go +++ b/cmd/notary-signer/main_test.go @@ -11,7 +11,9 @@ import ( "github.com/docker/notary" "github.com/docker/notary/signer" "github.com/docker/notary/signer/keydbstore" + "github.com/docker/notary/trustmanager" "github.com/docker/notary/tuf/data" + "github.com/docker/notary/tuf/testutils" "github.com/jinzhu/gorm" _ "github.com/mattn/go-sqlite3" "github.com/spf13/viper" @@ -168,7 +170,7 @@ func TestSetupCryptoServicesMemoryStore(t *testing.T) { cryptoServices, err := setUpCryptoservices(config, []string{notary.SQLiteBackend, notary.MemoryBackend}) require.NoError(t, err) - assert.Len(t, cryptoServices, 2) + require.Len(t, cryptoServices, 2) edService, ok := cryptoServices[data.ED25519Key] require.True(t, ok) @@ -209,3 +211,13 @@ func TestSetupGRPCServerSuccess(t *testing.T) { require.Equal(t, "tcp", lis.Addr().Network()) require.NotNil(t, grpcServer) } + +func TestBootstrap(t *testing.T) { + var ks trustmanager.KeyStore + err := bootstrap(ks) + require.Error(t, err) + tb := &testutils.TestBootstrapper{} + err = bootstrap(tb) + require.NoError(t, err) + require.True(t, tb.Booted) +} diff --git a/server/storage/tuf_store.go b/server/storage/tuf_store.go index 9b52ea62cb..9f607a9fa2 100644 --- a/server/storage/tuf_store.go +++ b/server/storage/tuf_store.go @@ -11,17 +11,17 @@ import ( "github.com/docker/notary/tuf/data" ) -// TufMetaStorage wraps a MetaStore in order to walk the TUF tree for GetCurrent in a consistent manner, +// TUFMetaStorage wraps a MetaStore in order to walk the TUF tree for GetCurrent in a consistent manner, // by always starting from a current timestamp and then looking up other data by hash -type TufMetaStorage struct { +type TUFMetaStorage struct { MetaStore // cached metadata by checksum cachedMeta map[string]*storedMeta } -// NewTufMetaStorage instantiates a TufMetaStorage instance -func NewTufMetaStorage(m MetaStore) *TufMetaStorage { - return &TufMetaStorage{ +// NewTUFMetaStorage instantiates a TUFMetaStorage instance +func NewTUFMetaStorage(m MetaStore) *TUFMetaStorage { + return &TUFMetaStorage{ MetaStore: m, cachedMeta: make(map[string]*storedMeta), } @@ -33,7 +33,7 @@ type storedMeta struct { } // GetCurrent gets a specific TUF record, by walking from the current Timestamp to other metadata by checksum -func (tms TufMetaStorage) GetCurrent(gun, tufRole string) (*time.Time, []byte, error) { +func (tms TUFMetaStorage) GetCurrent(gun, tufRole string) (*time.Time, []byte, error) { timestampTime, timestampJSON, err := tms.MetaStore.GetCurrent(gun, data.CanonicalTimestampRole) if err != nil { return nil, nil, err @@ -107,7 +107,7 @@ func (tms TufMetaStorage) GetCurrent(gun, tufRole string) (*time.Time, []byte, e } // Bootstrap the store with tables if possible -func (tms TufMetaStorage) Bootstrap() error { +func (tms TUFMetaStorage) Bootstrap() error { if s, ok := tms.MetaStore.(storage.Bootstrapper); ok { return s.Bootstrap() } diff --git a/server/storage/tuf_store_test.go b/server/storage/tuf_store_test.go index 7525bf55ee..e40bd406ee 100644 --- a/server/storage/tuf_store_test.go +++ b/server/storage/tuf_store_test.go @@ -13,12 +13,12 @@ import ( "github.com/stretchr/testify/require" ) -// SetupTUFSQLite creates a sqlite database for testing, wrapped by a TufMetaStorage -func SetupTUFSQLite(t *testing.T, dbDir string) (*gorm.DB, *TufMetaStorage) { +// SetupTUFSQLite creates a sqlite database for testing, wrapped by a TUFMetaStorage +func SetupTUFSQLite(t *testing.T, dbDir string) (*gorm.DB, *TUFMetaStorage) { dbStore, err := NewSQLStorage("sqlite3", dbDir+"test_db") require.NoError(t, err) - consistentDBStore := NewTufMetaStorage(dbStore) + consistentDBStore := NewTUFMetaStorage(dbStore) embeddedDB := dbStore.DB // Create the DB tables diff --git a/tuf/testutils/bootstrap.go b/tuf/testutils/bootstrap.go new file mode 100644 index 0000000000..0c2d7b1f98 --- /dev/null +++ b/tuf/testutils/bootstrap.go @@ -0,0 +1,10 @@ +package testutils + +type TestBootstrapper struct { + Booted bool +} + +func (tb *TestBootstrapper) Bootstrap() error { + tb.Booted = true + return nil +}