Skip to content

Commit

Permalink
Fix a somewhat buggy testUpdateManyNoConflicts (as metadata is update…
Browse files Browse the repository at this point in the history
…d old metadata is no longer current.

Also rename a variable to be more clear, and make invalid arguments to the integration/testdb scripts fail faster.

Signed-off-by: Ying Li <[email protected]>
  • Loading branch information
cyli committed Jul 14, 2016
1 parent f9caac5 commit 985502d
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 28 deletions.
4 changes: 4 additions & 0 deletions buildscripts/dbtests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ case ${db} in
dbContainerOpts="--name rethinkdb_tests rdb-01 --bind all --driver-tls-key /tls/key.pem --driver-tls-cert /tls/cert.pem"
DBURL="rethinkdb_tests"
;;
*)
echo "Usage: $0 (mysql|rethink)"
exit 1
;;
esac

composeFile="development.${db}.yml"
Expand Down
13 changes: 13 additions & 0 deletions buildscripts/integrationtest.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
#!/usr/bin/env bash

db="$1"
case ${db} in
mysql*)
db="mysql"
;;
rethink*)
db="rethink"
;;
*)
echo "Usage: $0 (mysql|rethink)"
exit 1
;;
esac

composeFile="development.${db}.yml"
project=integration

Expand Down
20 changes: 16 additions & 4 deletions server/storage/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
"sort"
"strings"
"sync"
"time"
Expand All @@ -20,19 +21,29 @@ type ver struct {
createupdate time.Time
}

// we want to keep these sorted by version so that it's in increasing version
// order
type verList []ver

func (k verList) Len() int { return len(k) }
func (k verList) Swap(i, j int) { k[i], k[j] = k[j], k[i] }
func (k verList) Less(i, j int) bool {
return k[i].version < k[j].version
}

// MemStorage is really just designed for dev and testing. It is very
// inefficient in many scenarios
type MemStorage struct {
lock sync.Mutex
tufMeta map[string][]*ver
tufMeta map[string]verList
keys map[string]map[string]*key
checksums map[string]map[string]ver
}

// NewMemStorage instantiates a memStorage instance
func NewMemStorage() *MemStorage {
return &MemStorage{
tufMeta: make(map[string][]*ver),
tufMeta: make(map[string]verList),
keys: make(map[string]map[string]*key),
checksums: make(map[string]map[string]ver),
}
Expand All @@ -51,7 +62,7 @@ func (st *MemStorage) UpdateCurrent(gun string, update MetaUpdate) error {
}
}
version := ver{version: update.Version, data: update.Data, createupdate: time.Now()}
st.tufMeta[id] = append(st.tufMeta[id], &version)
st.tufMeta[id] = append(st.tufMeta[id], version)
checksumBytes := sha256.Sum256(update.Data)
checksum := hex.EncodeToString(checksumBytes[:])

Expand Down Expand Up @@ -97,7 +108,8 @@ func (st *MemStorage) UpdateMany(gun string, updates []MetaUpdate) error {
id := entryKey(gun, u.Role)

version := ver{version: u.Version, data: u.Data, createupdate: time.Now()}
st.tufMeta[id] = append(st.tufMeta[id], &version)
st.tufMeta[id] = append(st.tufMeta[id], version)
sort.Sort(st.tufMeta[id]) // ensure that it's sorted
checksumBytes := sha256.Sum256(u.Data)
checksum := hex.EncodeToString(checksumBytes[:])

Expand Down
17 changes: 9 additions & 8 deletions server/storage/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ import (
)

func assertExpectedMemoryTUFMeta(t *testing.T, expected []StoredTUFMeta, s *MemStorage) {
counter := make(map[string]int)
for _, tufObj := range expected {
k := entryKey(tufObj.Gun, tufObj.Role)
gun, ok := s.tufMeta[k]
require.True(t, len(gun) >= counter[k])
v := gun[counter[k]]
require.True(t, ok, "Did not find gun in store")
require.Equal(t, tufObj.Version, v.version, "Version mismatch. Expected %d, found %d",
tufObj.Version, v.version)
versionList, ok := s.tufMeta[k]
require.True(t, ok, "Did not find this gun+role in store")
byVersion := make(map[int]ver)
for _, v := range versionList {
byVersion[v.version] = v
}

v, ok := byVersion[tufObj.Version]
require.True(t, ok, "Did not find version %d in store", tufObj.Version)
require.Equal(t, tufObj.Data, v.data, "Data was incorrect")
counter[k]++
}
}

Expand Down
47 changes: 31 additions & 16 deletions server/storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,18 @@ func SampleCustomUpdate(role string, tufdata []byte, version int) MetaUpdate {
}
}

func assertExpectedTUFMetaInStore(t *testing.T, s MetaStore, expected []StoredTUFMeta) {
func assertExpectedTUFMetaInStore(t *testing.T, s MetaStore, expected []StoredTUFMeta, current bool) {
for _, tufObj := range expected {
_, tufdata, err := s.GetCurrent(tufObj.Gun, tufObj.Role)
require.NoError(t, err)
require.Equal(t, tufObj.Data, tufdata)
if current {
_, tufdata, err := s.GetCurrent(tufObj.Gun, tufObj.Role)
require.NoError(t, err)
require.Equal(t, tufObj.Data, tufdata)
}

checksumBytes := sha256.Sum256(tufObj.Data)
checksum := hex.EncodeToString(checksumBytes[:])

_, tufdata, err = s.GetChecksum(tufObj.Gun, tufObj.Role, checksum)
_, tufdata, err := s.GetChecksum(tufObj.Gun, tufObj.Role, checksum)
require.NoError(t, err)
require.Equal(t, tufObj.Data, tufdata)
}
Expand All @@ -66,7 +68,7 @@ func testUpdateCurrentEmptyStore(t *testing.T, s MetaStore) []StoredTUFMeta {
}
}

assertExpectedTUFMetaInStore(t, s, expected)
assertExpectedTUFMetaInStore(t, s, expected, true)
return expected
}

Expand Down Expand Up @@ -95,44 +97,57 @@ func testUpdateCurrentVersionCheck(t *testing.T, s MetaStore) []StoredTUFMeta {
SampleCustomTUFObj(role, gun, tufdata, 2),
SampleCustomTUFObj(role, gun, tufdata, 4),
}
assertExpectedTUFMetaInStore(t, s, expected)
assertExpectedTUFMetaInStore(t, s, expected, true)
return expected
}

// UpdateMany succeeds if the updates do not conflict with each other or with what's
// already in the DB
func testUpdateManyNoConflicts(t *testing.T, s MetaStore) []StoredTUFMeta {
gun, tufdata := "testGUN", []byte("many")
expected := make([]StoredTUFMeta, 4)
gun := "testGUN"
firstBatch := make([]StoredTUFMeta, 4)
updates := make([]MetaUpdate, 4)
for i, role := range data.BaseRoles {
expected[i] = SampleCustomTUFObj(role, gun, tufdata, 1)
tufdata := []byte(fmt.Sprintf("%s_%s_1", gun, role))
firstBatch[i] = SampleCustomTUFObj(role, gun, tufdata, 1)
updates[i] = SampleCustomUpdate(role, tufdata, 1)
}

require.NoError(t, s.UpdateMany(gun, updates))
assertExpectedTUFMetaInStore(t, s, firstBatch, true)

secondBatch := make([]StoredTUFMeta, 4)
// no conflicts with what's in DB or with itself
for i, role := range data.BaseRoles {
expected = append(expected, SampleCustomTUFObj(role, gun, tufdata, 2))
tufdata := []byte(fmt.Sprintf("%s_%s_2", gun, role))
secondBatch[i] = SampleCustomTUFObj(role, gun, tufdata, 2)
updates[i] = SampleCustomUpdate(role, tufdata, 2)
}

require.NoError(t, s.UpdateMany(gun, updates))
// the first batch is still there, but are no longer the current ones
assertExpectedTUFMetaInStore(t, s, firstBatch, false)
assertExpectedTUFMetaInStore(t, s, secondBatch, true)

// and no conflicts if the same role and gun but different version is included
// in the same update. Even if they're out of order.
thirdBatch := make([]StoredTUFMeta, 2)
role := data.CanonicalRootRole
updates = updates[:2]
for i, version := range []int{4, 3} {
role := data.CanonicalRootRole
expected = append(expected, SampleCustomTUFObj(role, gun, tufdata, version))
tufdata := []byte(fmt.Sprintf("%s_%s_%d", gun, role, version))
thirdBatch[i] = SampleCustomTUFObj(role, gun, tufdata, version)
updates[i] = SampleCustomUpdate(role, tufdata, version)
}

require.NoError(t, s.UpdateMany(gun, updates))

assertExpectedTUFMetaInStore(t, s, expected)
return expected
// all the other data is still there, but are no longer the current ones
assertExpectedTUFMetaInStore(t, s, append(firstBatch, secondBatch...), false)
assertExpectedTUFMetaInStore(t, s, thirdBatch[:1], true)
assertExpectedTUFMetaInStore(t, s, thirdBatch[1:], false)

return append(append(firstBatch, secondBatch...), thirdBatch...)
}

// UpdateMany does not insert any rows (or at least rolls them back) if there
Expand Down Expand Up @@ -172,7 +187,7 @@ func testUpdateManyConflictRollback(t *testing.T, s MetaStore) []StoredTUFMeta {
require.Error(t, err)
require.IsType(t, &ErrOldVersion{}, err)

assertExpectedTUFMetaInStore(t, s, successBatch)
assertExpectedTUFMetaInStore(t, s, successBatch, true)

for _, tufObj := range append(badBatch, duplicate) {
checksumBytes := sha256.Sum256(tufObj.Data)
Expand Down

0 comments on commit 985502d

Please sign in to comment.