From 09d45deae33a549bfec002dfc4a74427a22fa74c Mon Sep 17 00:00:00 2001 From: David Lawrence Date: Wed, 6 May 2015 11:15:55 -0700 Subject: [PATCH] moving test functions to better location Signed-off-by: David Lawrence (github: endophage) --- cmd/tools/main.go | 4 +-- repo_test.go | 43 +++++++++++++------------- store/dbstore_test.go | 30 +++++++++--------- util/testutils.go => testutil/utils.go | 2 +- 4 files changed, 40 insertions(+), 39 deletions(-) rename util/testutils.go => testutil/utils.go (98%) diff --git a/cmd/tools/main.go b/cmd/tools/main.go index 1ecd2705..c1c9040e 100644 --- a/cmd/tools/main.go +++ b/cmd/tools/main.go @@ -7,7 +7,7 @@ import ( "github.com/endophage/go-tuf" "github.com/endophage/go-tuf/signed" "github.com/endophage/go-tuf/store" - "github.com/endophage/go-tuf/util" + "github.com/endophage/go-tuf/testutil" "github.com/flynn/go-docopt" ) @@ -73,7 +73,7 @@ func runCommand(name string, args []string) error { return err } - db := util.GetSqliteDB() + db := testutil.GetSqliteDB() local := store.DBStore(db, "") signer := signed.Ed25519{} repo, err := tuf.NewRepo(&signer, local, "sha256") diff --git a/repo_test.go b/repo_test.go index 8b840d86..c5652ab3 100644 --- a/repo_test.go +++ b/repo_test.go @@ -17,6 +17,7 @@ import ( // "github.com/endophage/go-tuf/encrypted" tuferr "github.com/endophage/go-tuf/errors" "github.com/endophage/go-tuf/signed" + "github.com/endophage/go-tuf/testutil" "github.com/endophage/go-tuf/util" ) @@ -69,8 +70,8 @@ func (RepoSuite) TestNewRepo(c *C) { "signatures": [] }`), } - db := util.GetSqliteDB() - defer util.FlushDB(db) + db := testutil.GetSqliteDB() + defer testutil.FlushDB(db) local := store.DBStore(db, "") for k, v := range meta { @@ -112,14 +113,14 @@ func (RepoSuite) TestNewRepo(c *C) { func (RepoSuite) TestInit(c *C) { trust := signed.NewEd25519() - db := util.GetSqliteDB() - defer util.FlushDB(db) + db := testutil.GetSqliteDB() + defer testutil.FlushDB(db) local := store.DBStore( db, "", //map[string][]byte{"/foo.txt": []byte("foo")}, ) - local.AddBlob("/foo.txt", util.SampleMeta()) + local.AddBlob("/foo.txt", testutil.SampleMeta()) r, err := NewRepo(trust, local, "sha256") c.Assert(err, IsNil) @@ -146,8 +147,8 @@ func genKey(c *C, r *Repo, role string) string { func (RepoSuite) TestGenKey(c *C) { trust := signed.NewEd25519() - sqldb := util.GetSqliteDB() - defer util.FlushDB(sqldb) + sqldb := testutil.GetSqliteDB() + defer testutil.FlushDB(sqldb) local := store.DBStore(sqldb, "") r, err := NewRepo(trust, local, "sha256") c.Assert(err, IsNil) @@ -277,8 +278,8 @@ func (RepoSuite) TestGenKey(c *C) { func (RepoSuite) TestRevokeKey(c *C) { trust := signed.NewEd25519() - db := util.GetSqliteDB() - defer util.FlushDB(db) + db := testutil.GetSqliteDB() + defer testutil.FlushDB(db) local := store.DBStore(db, "") r, err := NewRepo(trust, local, "sha256") c.Assert(err, IsNil) @@ -330,8 +331,8 @@ func (RepoSuite) TestSign(c *C) { trust := signed.NewEd25519() baseMeta := map[string]json.RawMessage{"root.json": []byte(`{"signed":{},"signatures":[]}`)} - db := util.GetSqliteDB() - defer util.FlushDB(db) + db := testutil.GetSqliteDB() + defer testutil.FlushDB(db) local := store.DBStore(db, "") local.SetMeta("root.json", baseMeta["root.json"]) r, err := NewRepo(trust, local, "sha256") @@ -382,8 +383,8 @@ func (RepoSuite) TestCommit(c *C) { trust := signed.NewEd25519() //files := map[string][]byte{"/foo.txt": []byte("foo"), "/bar.txt": []byte("bar")} - db := util.GetSqliteDB() - defer util.FlushDB(db) + db := testutil.GetSqliteDB() + defer testutil.FlushDB(db) local := store.DBStore(db, "") r, err := NewRepo(trust, local, "sha256") c.Assert(err, IsNil) @@ -397,7 +398,7 @@ func (RepoSuite) TestCommit(c *C) { // commit without snapshot.json genKey(c, r, "targets") - local.AddBlob("/foo.txt", util.SampleMeta()) + local.AddBlob("/foo.txt", testutil.SampleMeta()) c.Assert(r.AddTargets(nil, "foo.txt"), IsNil) c.Assert(r.Commit(), DeepEquals, tuferr.ErrMissingMetadata{"snapshot.json"}) @@ -423,7 +424,7 @@ func (RepoSuite) TestCommit(c *C) { // commit with an invalid targets hash in snapshot.json c.Assert(r.Snapshot(CompressionTypeNone), IsNil) - local.AddBlob("/bar.txt", util.SampleMeta()) + local.AddBlob("/bar.txt", testutil.SampleMeta()) c.Assert(r.AddTargets(nil, "bar.txt"), IsNil) c.Assert(r.Commit(), DeepEquals, errors.New("tuf: invalid targets.json in snapshot.json: wrong length")) @@ -665,8 +666,8 @@ func (RepoSuite) TestExpiresAndVersion(c *C) { trust := signed.NewEd25519() //files := map[string][]byte{"/foo.txt": []byte("foo")} - db := util.GetSqliteDB() - defer util.FlushDB(db) + db := testutil.GetSqliteDB() + defer testutil.FlushDB(db) local := store.DBStore(db, "") r, err := NewRepo(trust, local, "sha256") c.Assert(err, IsNil) @@ -710,7 +711,7 @@ func (RepoSuite) TestExpiresAndVersion(c *C) { expires = time.Now().Add(6 * time.Hour) genKey(c, r, "targets") - local.AddBlob("/foo.txt", util.SampleMeta()) + local.AddBlob("/foo.txt", testutil.SampleMeta()) c.Assert(r.AddTargetsWithExpires(nil, expires, "foo.txt"), IsNil) targets, err := r.targets() c.Assert(err, IsNil) @@ -755,8 +756,8 @@ func (RepoSuite) TestHashAlgorithm(c *C) { trust := signed.NewEd25519() //files := map[string][]byte{"/foo.txt": []byte("foo")} - db := util.GetSqliteDB() - defer util.FlushDB(db) + db := testutil.GetSqliteDB() + defer testutil.FlushDB(db) local := store.DBStore(db, "docker.io/testImage") type hashTest struct { args []string @@ -773,7 +774,7 @@ func (RepoSuite) TestHashAlgorithm(c *C) { genKey(c, r, "root") genKey(c, r, "targets") genKey(c, r, "snapshot") - local.AddBlob("/foo.txt", util.SampleMeta()) + local.AddBlob("/foo.txt", testutil.SampleMeta()) c.Assert(r.AddTargets(nil, "foo.txt"), IsNil) c.Assert(r.Snapshot(CompressionTypeNone), IsNil) c.Assert(r.Timestamp(), IsNil) diff --git a/store/dbstore_test.go b/store/dbstore_test.go index e098c814..6db5c096 100644 --- a/store/dbstore_test.go +++ b/store/dbstore_test.go @@ -5,22 +5,22 @@ import ( "testing" "github.com/endophage/go-tuf/data" - "github.com/endophage/go-tuf/util" + "github.com/endophage/go-tuf/testutil" ) // TestDBStore just ensures we can initialize an empty store. // Nothing to test, just ensure no crashes :-) func TestDBStore(t *testing.T) { - db := util.GetSqliteDB() - defer util.FlushDB(db) + db := testutil.GetSqliteDB() + defer testutil.FlushDB(db) _ = DBStore(db, "") } func TestLoadFiles(t *testing.T) { - db := util.GetSqliteDB() - defer util.FlushDB(db) + db := testutil.GetSqliteDB() + defer testutil.FlushDB(db) store := DBStore(db, "docker.io/testImage") - testmeta := util.SampleMeta() + testmeta := testutil.SampleMeta() store.AddBlob("/foo.txt", testmeta) called := false @@ -52,10 +52,10 @@ func TestLoadFiles(t *testing.T) { } func TestAddBlob(t *testing.T) { - db := util.GetSqliteDB() - defer util.FlushDB(db) + db := testutil.GetSqliteDB() + defer testutil.FlushDB(db) store := DBStore(db, "docker.io/testImage") - testmeta := util.SampleMeta() + testmeta := testutil.SampleMeta() store.AddBlob("/foo.txt", testmeta) called := false @@ -100,10 +100,10 @@ func TestAddBlob(t *testing.T) { func TestRemoveBlob(t *testing.T) { testPath := "/foo.txt" - db := util.GetSqliteDB() - defer util.FlushDB(db) + db := testutil.GetSqliteDB() + defer testutil.FlushDB(db) store := DBStore(db, "docker.io/testImage") - meta := util.SampleMeta() + meta := testutil.SampleMeta() store.AddBlob(testPath, meta) @@ -124,10 +124,10 @@ func TestRemoveBlob(t *testing.T) { } func TestLoadFilesWithPath(t *testing.T) { - db := util.GetSqliteDB() - defer util.FlushDB(db) + db := testutil.GetSqliteDB() + defer testutil.FlushDB(db) store := DBStore(db, "docker.io/testImage") - meta := util.SampleMeta() + meta := testutil.SampleMeta() store.AddBlob("/foo.txt", meta) store.AddBlob("/bar.txt", meta) diff --git a/util/testutils.go b/testutil/utils.go similarity index 98% rename from util/testutils.go rename to testutil/utils.go index cf7fae5e..c1f1810b 100644 --- a/util/testutils.go +++ b/testutil/utils.go @@ -1,4 +1,4 @@ -package util +package testutil import ( "database/sql"