mirror of https://github.com/docker/docs.git
Add more tests for UpdateCurrent
Signed-off-by: Ying Li <ying.li@docker.com>
This commit is contained in:
parent
31bb0b0525
commit
5f70699937
|
@ -38,6 +38,25 @@ func (g GormTimestampKey) TableName() string {
|
|||
return "timestamp_keys"
|
||||
}
|
||||
|
||||
// SampleTUF returns a sample GormTUFFile with the given Version (ID will have
|
||||
// to be set independently)
|
||||
func SampleTUF(version int) GormTUFFile {
|
||||
return GormTUFFile{
|
||||
Gun: "testGUN",
|
||||
Role: "root",
|
||||
Version: version,
|
||||
Data: []byte("1"),
|
||||
}
|
||||
}
|
||||
|
||||
func SampleUpdate(version int) MetaUpdate {
|
||||
return MetaUpdate{
|
||||
Role: "root",
|
||||
Version: version,
|
||||
Data: []byte("1"),
|
||||
}
|
||||
}
|
||||
|
||||
// SetUpSQLite creates a sqlite database for testing
|
||||
func SetUpSQLite(t *testing.T) (*gorm.DB, *MySQLStorage) {
|
||||
tempBaseDir, err := ioutil.TempDir("", "notary-test-")
|
||||
|
@ -68,79 +87,83 @@ func SetUpSQLite(t *testing.T) (*gorm.DB, *MySQLStorage) {
|
|||
return &gormDB, NewMySQLStorage(db)
|
||||
}
|
||||
|
||||
func TestMySQLUpdateCurrent(t *testing.T) {
|
||||
// TestMySQLUpdateCurrent asserts that UpdateCurrent will add a new TUF file
|
||||
// if no previous version existed.
|
||||
func TestMySQLUpdateCurrentNew(t *testing.T) {
|
||||
gormDB, dbStore := SetUpSQLite(t)
|
||||
|
||||
// UpdateCurrent should succeed
|
||||
update := MetaUpdate{
|
||||
Role: "root",
|
||||
Version: 0,
|
||||
Data: []byte("1"),
|
||||
}
|
||||
// Adding a new TUF file should succeed
|
||||
err := dbStore.UpdateCurrent("testGUN", SampleUpdate(0))
|
||||
assert.NoError(t, err, "Creating a row in an empty DB failed.")
|
||||
|
||||
// There should just be one row
|
||||
var rows []GormTUFFile
|
||||
query := gormDB.Model(&GormTUFFile{}).Find(&rows)
|
||||
assert.NoError(t, query.Error)
|
||||
|
||||
expected := SampleTUF(0)
|
||||
expected.ID = 1
|
||||
assert.Equal(t, []GormTUFFile{expected}, rows)
|
||||
}
|
||||
|
||||
// TestMySQLUpdateCurrentNewVersion asserts that UpdateCurrent will add a
|
||||
// new (higher) version of an existing TUF file
|
||||
func TestMySQLUpdateCurrentNewVersion(t *testing.T) {
|
||||
gormDB, dbStore := SetUpSQLite(t)
|
||||
|
||||
// insert row
|
||||
oldVersion := SampleTUF(0)
|
||||
query := gormDB.Create(&oldVersion)
|
||||
assert.NoError(t, query.Error, "Creating a row in an empty DB failed.")
|
||||
|
||||
// UpdateCurrent with a newer version should succeed
|
||||
update := SampleUpdate(2)
|
||||
err := dbStore.UpdateCurrent("testGUN", update)
|
||||
assert.NoError(t, err, "Creating a row in an empty DB failed.")
|
||||
|
||||
// There should just be one row
|
||||
var rows []GormTUFFile
|
||||
query := gormDB.Model(&GormTUFFile{}).Find(&rows)
|
||||
assert.NoError(t, query.Error)
|
||||
assert.Equal(
|
||||
t,
|
||||
[]GormTUFFile{
|
||||
GormTUFFile{ID: 1, Gun: "testGUN", Role: "root", Version: 0,
|
||||
Data: []byte("1")},
|
||||
},
|
||||
rows)
|
||||
// There should just be one row
|
||||
var rows []GormTUFFile
|
||||
query = gormDB.Model(&GormTUFFile{}).Find(&rows)
|
||||
assert.NoError(t, query.Error)
|
||||
|
||||
dbStore.DB.Close()
|
||||
oldVersion.ID = 1
|
||||
expected := SampleTUF(2)
|
||||
expected.ID = 2
|
||||
assert.Equal(t, []GormTUFFile{oldVersion, expected}, rows)
|
||||
}
|
||||
|
||||
func TestMySQLUpdateCurrentError(t *testing.T) {
|
||||
gormDB, dbStore := SetUpSQLite(t)
|
||||
// TestMySQLUpdateCurrentOldVersionError asserts that an error is raised if
|
||||
// trying to update to an older version of a TUF file.
|
||||
func TestMySQLUpdateCurrentOldVersionError(t *testing.T) {
|
||||
gormDB, dbStore := SetUpSQLite(t)
|
||||
|
||||
// insert row
|
||||
query := gormDB.Create(&GormTUFFile{
|
||||
Gun: "testGUN",
|
||||
Role: "root",
|
||||
Version: 0,
|
||||
Data: []byte("1"),
|
||||
})
|
||||
assert.NoError(t, query.Error, "Creating a row in an empty DB failed.")
|
||||
// insert row
|
||||
newVersion := SampleTUF(3)
|
||||
query := gormDB.Create(&newVersion)
|
||||
assert.NoError(t, query.Error, "Creating a row in an empty DB failed.")
|
||||
|
||||
// UpdateCurrent should fail due to clash with prevoius row
|
||||
update := MetaUpdate{
|
||||
Role: "root",
|
||||
Version: 0,
|
||||
Data: []byte("1"),
|
||||
}
|
||||
err := dbStore.UpdateCurrent("testGUN", update)
|
||||
assert.Error(t, err, "Error should not be nil")
|
||||
assert.IsType(t, &ErrOldVersion{}, err,
|
||||
"Expected ErrOldVersion error type, got: %v", err)
|
||||
// UpdateCurrent should fail due to the version being lower than the
|
||||
// previous row
|
||||
err := dbStore.UpdateCurrent("testGUN", SampleUpdate(0))
|
||||
assert.Error(t, err, "Error should not be nil")
|
||||
assert.IsType(t, &ErrOldVersion{}, err,
|
||||
"Expected ErrOldVersion error type, got: %v", err)
|
||||
|
||||
// There should just be one row
|
||||
var rows []GormTUFFile
|
||||
query = gormDB.Model(&GormTUFFile{}).Find(&rows)
|
||||
assert.NoError(t, query.Error)
|
||||
assert.Equal(
|
||||
t,
|
||||
[]GormTUFFile{
|
||||
GormTUFFile{ID: 1, Gun: "testGUN", Role: "root", Version: 0,
|
||||
Data: []byte("1")},
|
||||
},
|
||||
rows)
|
||||
// There should just be one row
|
||||
var rows []GormTUFFile
|
||||
query = gormDB.Model(&GormTUFFile{}).Find(&rows)
|
||||
assert.NoError(t, query.Error)
|
||||
|
||||
dbStore.DB.Close()
|
||||
newVersion.ID = 1
|
||||
assert.Equal(t, []GormTUFFile{newVersion}, rows)
|
||||
|
||||
dbStore.DB.Close()
|
||||
}
|
||||
|
||||
func TestMySQLUpdateMany(t *testing.T) {
|
||||
gormDB, dbStore := SetUpSQLite(t)
|
||||
|
||||
update1 := MetaUpdate{
|
||||
Role: "root",
|
||||
Version: 0,
|
||||
Data: []byte("1"),
|
||||
}
|
||||
update1 := SampleUpdate(0)
|
||||
update2 := MetaUpdate{
|
||||
Role: "targets",
|
||||
Version: 1,
|
||||
|
@ -169,12 +192,7 @@ func TestMySQLUpdateMany(t *testing.T) {
|
|||
func TestMySQLUpdateManyDuplicateRollback(t *testing.T) {
|
||||
gormDB, dbStore := SetUpSQLite(t)
|
||||
|
||||
update := MetaUpdate{
|
||||
Role: "root",
|
||||
Version: 0,
|
||||
Data: []byte("1"),
|
||||
}
|
||||
|
||||
update := SampleUpdate(0)
|
||||
err := dbStore.UpdateMany("testGUN", []MetaUpdate{update, update})
|
||||
assert.Error(t, err, "There should be an error updating twice.")
|
||||
// sqlite3 error and mysql error aren't compatible
|
||||
|
@ -199,12 +217,8 @@ func TestMySQLGetCurrent(t *testing.T) {
|
|||
assert.Error(t, err, "There should be an error Getting an empty table")
|
||||
assert.IsType(t, &ErrNotFound{}, err, "Should get a not found error")
|
||||
|
||||
query := gormDB.Create(&GormTUFFile{
|
||||
Gun: "testGUN",
|
||||
Role: "root",
|
||||
Version: 0,
|
||||
Data: []byte("1"),
|
||||
})
|
||||
tuf := SampleTUF(0)
|
||||
query := gormDB.Create(&tuf)
|
||||
assert.NoError(t, query.Error, "Creating a row in an empty DB failed.")
|
||||
|
||||
byt, err = dbStore.GetCurrent("testGUN", "root")
|
||||
|
@ -221,12 +235,8 @@ func TestMySQLDelete(t *testing.T) {
|
|||
// in SQLite3
|
||||
|
||||
// use UpdateCurrent to create one and test GetCurrent
|
||||
query := gormDB.Create(&GormTUFFile{
|
||||
Gun: "testGUN",
|
||||
Role: "root",
|
||||
Version: 0,
|
||||
Data: []byte("1"),
|
||||
})
|
||||
tuf := SampleTUF(0)
|
||||
query := gormDB.Create(&tuf)
|
||||
assert.NoError(t, query.Error, "Creating a row in an empty DB failed.")
|
||||
|
||||
err := dbStore.Delete("testGUN")
|
||||
|
|
Loading…
Reference in New Issue