Skip to content

Commit

Permalink
Break the version check test into 2 tests - one where a version alrea…
Browse files Browse the repository at this point in the history
…dy exists in a DB, one where it doesn't

Signed-off-by: Ying Li <[email protected]>
  • Loading branch information
cyli committed Jul 20, 2016
1 parent 49d50ad commit 119dd7c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 19 deletions.
15 changes: 12 additions & 3 deletions server/storage/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,19 @@ func TestMemoryUpdateCurrentEmpty(t *testing.T) {
}

// UpdateCurrent will successfully add a new (higher) version of an existing TUF file,
// but will return an error if there is an older version of a TUF file.
func TestMemoryUpdateCurrentVersionCheck(t *testing.T) {
// but will return an error if the to-be-added version already exists in the DB.
func TestMemoryUpdateCurrentVersionCheckOldVersionExists(t *testing.T) {
s := NewMemStorage()
expected := testUpdateCurrentVersionCheck(t, s)
expected := testUpdateCurrentVersionCheck(t, s, true)
assertExpectedMemoryTUFMeta(t, expected, s)
}

// UpdateCurrent will successfully add a new (higher) version of an existing TUF file,
// but will return an error if the to-be-added version does not exist in the DB, but
// is older than an existing version in the DB.
func TestMemoryUpdateCurrentVersionCheckOldVersionNotExist(t *testing.T) {
s := NewMemStorage()
expected := testUpdateCurrentVersionCheck(t, s, false)
assertExpectedMemoryTUFMeta(t, expected, s)
}

Expand Down
17 changes: 13 additions & 4 deletions server/storage/rethink_realdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,23 @@ func TestRethinkUpdateCurrentEmpty(t *testing.T) {
}

// UpdateCurrent will add a new TUF file if the version is higher than previous, but fail
// if the version is equal to or less than the current, whether or not that previous
// version exists
func TestRethinkUpdateCurrentVersionCheck(t *testing.T) {
// if the version already exists in the DB
func TestRethinkUpdateCurrentVersionCheckOldVersionExists(t *testing.T) {
dbStore, cleanup := rethinkDBSetup(t)
defer cleanup()

testUpdateCurrentVersionCheck(t, dbStore, true)
}

// UpdateCurrent will successfully add a new (higher) version of an existing TUF file,
// but will return an error if the to-be-added version does not exist in the DB, but
// is older than an existing version in the DB.
func TestRethinkUpdateCurrentVersionCheckOldVersionNotExist(t *testing.T) {
t.Skip("Currently rethink only errors if the previous version exists - it doesn't check for strictly increasing")
dbStore, cleanup := rethinkDBSetup(t)
defer cleanup()

testUpdateCurrentVersionCheck(t, dbStore)
testUpdateCurrentVersionCheck(t, dbStore, false)
}

// UpdateMany succeeds if the updates do not conflict with each other or with what's
Expand Down
21 changes: 17 additions & 4 deletions server/storage/sqldb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,27 @@ func TestSQLUpdateCurrentEmpty(t *testing.T) {
dbStore.DB.Close()
}

// TestSQLUpdateCurrentNewVersion asserts that UpdateCurrent will add a
// TestSQLUpdateCurrentVersionCheckOldVersionExists asserts that UpdateCurrent will add a
// new (higher) version of an existing TUF file, and that an error is raised if
// trying to update to an older version of a TUF file.
func TestSQLUpdateCurrentNewVersion(t *testing.T) {
// trying to update to an older version of a TUF file that already exists.
func TestSQLUpdateCurrentVersionCheckOldVersionExists(t *testing.T) {
dbStore, cleanup := sqldbSetup(t)
defer cleanup()

expected := testUpdateCurrentVersionCheck(t, dbStore)
expected := testUpdateCurrentVersionCheck(t, dbStore, true)
assertExpectedGormTUFMeta(t, expected, dbStore.DB)

dbStore.DB.Close()
}

// TestSQLUpdateCurrentVersionCheckOldVersionNotExist asserts that UpdateCurrent will add a
// new (higher) version of an existing TUF file, and that an error is raised if
// trying to update to an older version of a TUF file that doesn't exist in the DB.
func TestSQLUpdateCurrentVersionCheckOldVersionNotExist(t *testing.T) {
dbStore, cleanup := sqldbSetup(t)
defer cleanup()

expected := testUpdateCurrentVersionCheck(t, dbStore, false)
assertExpectedGormTUFMeta(t, expected, dbStore.DB)

dbStore.DB.Close()
Expand Down
20 changes: 12 additions & 8 deletions server/storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ func testUpdateCurrentEmptyStore(t *testing.T, s MetaStore) []StoredTUFMeta {
}

// UpdateCurrent will successfully add a new (higher) version of an existing TUF file,
// but will return an error if there is an older version of a TUF file.
func testUpdateCurrentVersionCheck(t *testing.T, s MetaStore) []StoredTUFMeta {
// but will return an error if there is an older version of a TUF file. oldVersionExists
// specifies whether the older version should already exist in the DB or not.
func testUpdateCurrentVersionCheck(t *testing.T, s MetaStore, oldVersionExists bool) []StoredTUFMeta {
role, gun := data.CanonicalRootRole, "testGUN"

expected := []StoredTUFMeta{
Expand All @@ -109,14 +110,17 @@ func testUpdateCurrentVersionCheck(t *testing.T, s MetaStore) []StoredTUFMeta {
require.NoError(t, s.UpdateCurrent(gun, MakeUpdate(expected[2])))

// Inserting a version that already exists, or that is lower than the current version, will fail
for _, version := range []int{3, 4} {
tufObj := SampleCustomTUFObj(gun, role, version, nil)
err := s.UpdateCurrent(gun, MakeUpdate(tufObj))
require.Error(t, err, "Error should not be nil")
require.IsType(t, &ErrOldVersion{}, err,
"Expected ErrOldVersion error type, got: %v", err)
version := 3
if oldVersionExists {
version = 4
}

tufObj := SampleCustomTUFObj(gun, role, version, nil)
err := s.UpdateCurrent(gun, MakeUpdate(tufObj))
require.Error(t, err, "Error should not be nil")
require.IsType(t, &ErrOldVersion{}, err,
"Expected ErrOldVersion error type, got: %v", err)

assertExpectedTUFMetaInStore(t, s, expected[:2], false)
assertExpectedTUFMetaInStore(t, s, expected[2:], true)
return expected
Expand Down

0 comments on commit 119dd7c

Please sign in to comment.