|
|
|
@ -9,6 +9,7 @@ import (
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
portainer "github.com/portainer/portainer/api"
|
|
|
|
|
"github.com/portainer/portainer/api/database/boltdb"
|
|
|
|
|
"github.com/portainer/portainer/api/database/models"
|
|
|
|
|
|
|
|
|
@ -55,111 +56,108 @@ func TestMigrateData(t *testing.T) {
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// t.Run("MigrateData for New Store & Re-Open Check", func(t *testing.T) {
|
|
|
|
|
// newStore, store, teardown := MustNewTestStore(t, true, false)
|
|
|
|
|
// defer teardown()
|
|
|
|
|
t.Run("MigrateData for New Store & Re-Open Check", func(t *testing.T) {
|
|
|
|
|
newStore, store := MustNewTestStore(t, true, false)
|
|
|
|
|
|
|
|
|
|
// if !newStore {
|
|
|
|
|
// t.Error("Expect a new DB")
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// testVersion(store, portainer.APIVersion, t)
|
|
|
|
|
// store.Close()
|
|
|
|
|
|
|
|
|
|
// newStore, _ = store.Open()
|
|
|
|
|
// if newStore {
|
|
|
|
|
// t.Error("Expect store to NOT be new DB")
|
|
|
|
|
// }
|
|
|
|
|
// })
|
|
|
|
|
|
|
|
|
|
// tests := []struct {
|
|
|
|
|
// version string
|
|
|
|
|
// expectedVersion string
|
|
|
|
|
// }{
|
|
|
|
|
// {version: "1.24.1", expectedVersion: portainer.APIVersion},
|
|
|
|
|
// {version: "2.0.0", expectedVersion: portainer.APIVersion},
|
|
|
|
|
// }
|
|
|
|
|
// for _, tc := range tests {
|
|
|
|
|
// _, store, teardown := MustNewTestStore(t, true, true)
|
|
|
|
|
// defer teardown()
|
|
|
|
|
|
|
|
|
|
// // Setup data
|
|
|
|
|
// v := models.Version{SchemaVersion: tc.version}
|
|
|
|
|
// store.VersionService.UpdateVersion(&v)
|
|
|
|
|
if !newStore {
|
|
|
|
|
t.Error("Expect a new DB")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// // Required roles by migrations 22.2
|
|
|
|
|
// store.RoleService.Create(&portainer.Role{ID: 1})
|
|
|
|
|
// store.RoleService.Create(&portainer.Role{ID: 2})
|
|
|
|
|
// store.RoleService.Create(&portainer.Role{ID: 3})
|
|
|
|
|
// store.RoleService.Create(&portainer.Role{ID: 4})
|
|
|
|
|
testVersion(store, portainer.APIVersion, t)
|
|
|
|
|
store.Close()
|
|
|
|
|
|
|
|
|
|
// t.Run(fmt.Sprintf("MigrateData for version %s", tc.version), func(t *testing.T) {
|
|
|
|
|
// store.MigrateData()
|
|
|
|
|
// testVersion(store, tc.expectedVersion, t)
|
|
|
|
|
// })
|
|
|
|
|
newStore, _ = store.Open()
|
|
|
|
|
if newStore {
|
|
|
|
|
t.Error("Expect store to NOT be new DB")
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// t.Run(fmt.Sprintf("Restoring DB after migrateData for version %s", tc.version), func(t *testing.T) {
|
|
|
|
|
// store.Rollback(true)
|
|
|
|
|
// store.Open()
|
|
|
|
|
// testVersion(store, tc.version, t)
|
|
|
|
|
// })
|
|
|
|
|
// }
|
|
|
|
|
tests := []struct {
|
|
|
|
|
version string
|
|
|
|
|
expectedVersion string
|
|
|
|
|
}{
|
|
|
|
|
{version: "1.24.1", expectedVersion: portainer.APIVersion},
|
|
|
|
|
{version: "2.0.0", expectedVersion: portainer.APIVersion},
|
|
|
|
|
}
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
|
_, store := MustNewTestStore(t, true, true)
|
|
|
|
|
|
|
|
|
|
// Setup data
|
|
|
|
|
v := models.Version{SchemaVersion: tc.version, Edition: int(portainer.PortainerCE)}
|
|
|
|
|
store.VersionService.UpdateVersion(&v)
|
|
|
|
|
|
|
|
|
|
// Required roles by migrations 22.2
|
|
|
|
|
store.RoleService.Create(&portainer.Role{ID: 1})
|
|
|
|
|
store.RoleService.Create(&portainer.Role{ID: 2})
|
|
|
|
|
store.RoleService.Create(&portainer.Role{ID: 3})
|
|
|
|
|
store.RoleService.Create(&portainer.Role{ID: 4})
|
|
|
|
|
|
|
|
|
|
t.Run(fmt.Sprintf("MigrateData for version %s", tc.version), func(t *testing.T) {
|
|
|
|
|
store.MigrateData()
|
|
|
|
|
testVersion(store, tc.expectedVersion, t)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// t.Run("Error in MigrateData should restore backup before MigrateData", func(t *testing.T) {
|
|
|
|
|
// _, store, teardown := MustNewTestStore(t, false, true)
|
|
|
|
|
// defer teardown()
|
|
|
|
|
t.Run(fmt.Sprintf("Restoring DB after migrateData for version %s", tc.version), func(t *testing.T) {
|
|
|
|
|
store.Rollback(true)
|
|
|
|
|
store.Open()
|
|
|
|
|
testVersion(store, tc.version, t)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// v := models.Version{SchemaVersion: "1.24.1"}
|
|
|
|
|
// store.VersionService.UpdateVersion(&v)
|
|
|
|
|
t.Run("Error in MigrateData should restore backup before MigrateData", func(t *testing.T) {
|
|
|
|
|
_, store := MustNewTestStore(t, false, false)
|
|
|
|
|
|
|
|
|
|
// store.MigrateData()
|
|
|
|
|
v := models.Version{SchemaVersion: "1.24.1", Edition: int(portainer.PortainerCE)}
|
|
|
|
|
store.VersionService.UpdateVersion(&v)
|
|
|
|
|
|
|
|
|
|
// testVersion(store, v.SchemaVersion, t)
|
|
|
|
|
// })
|
|
|
|
|
store.MigrateData()
|
|
|
|
|
|
|
|
|
|
// t.Run("MigrateData should create backup file upon update", func(t *testing.T) {
|
|
|
|
|
// _, store, teardown := MustNewTestStore(t, false, true)
|
|
|
|
|
// defer teardown()
|
|
|
|
|
testVersion(store, v.SchemaVersion, t)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// v := models.Version{SchemaVersion: "0.0.0"}
|
|
|
|
|
// store.VersionService.UpdateVersion(&v)
|
|
|
|
|
t.Run("MigrateData should create backup file upon update", func(t *testing.T) {
|
|
|
|
|
_, store := MustNewTestStore(t, false, false)
|
|
|
|
|
|
|
|
|
|
// store.MigrateData()
|
|
|
|
|
v := models.Version{SchemaVersion: "0.0.0", Edition: int(portainer.PortainerCE)}
|
|
|
|
|
store.VersionService.UpdateVersion(&v)
|
|
|
|
|
|
|
|
|
|
// options := store.setupOptions(getBackupRestoreOptions(store.commonBackupDir()))
|
|
|
|
|
store.MigrateData()
|
|
|
|
|
|
|
|
|
|
// if !isFileExist(options.BackupPath) {
|
|
|
|
|
// t.Errorf("Backup file should exist; file=%s", options.BackupPath)
|
|
|
|
|
// }
|
|
|
|
|
// })
|
|
|
|
|
options := store.setDefaultBackupOptions(getBackupRestoreOptions(store.commonBackupDir()))
|
|
|
|
|
|
|
|
|
|
// t.Run("MigrateData should fail to create backup if database file is set to updating", func(t *testing.T) {
|
|
|
|
|
// _, store, teardown := MustNewTestStore(t, false, true)
|
|
|
|
|
// defer teardown()
|
|
|
|
|
if !isFileExist(options.BackupPath) {
|
|
|
|
|
t.Errorf("Backup file should exist; file=%s", options.BackupPath)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// store.VersionService.StoreIsUpdating(true)
|
|
|
|
|
t.Run("MigrateData should fail to create backup if database file is set to updating", func(t *testing.T) {
|
|
|
|
|
_, store := MustNewTestStore(t, false, false)
|
|
|
|
|
|
|
|
|
|
// store.MigrateData()
|
|
|
|
|
store.VersionService.StoreIsUpdating(true)
|
|
|
|
|
|
|
|
|
|
// options := store.setupOptions(getBackupRestoreOptions(store.commonBackupDir()))
|
|
|
|
|
store.MigrateData()
|
|
|
|
|
|
|
|
|
|
// if isFileExist(options.BackupPath) {
|
|
|
|
|
// t.Errorf("Backup file should not exist for dirty database; file=%s", options.BackupPath)
|
|
|
|
|
// }
|
|
|
|
|
// })
|
|
|
|
|
options := store.setDefaultBackupOptions(getBackupRestoreOptions(store.commonBackupDir()))
|
|
|
|
|
|
|
|
|
|
// t.Run("MigrateData should not create backup on startup if portainer version matches db", func(t *testing.T) {
|
|
|
|
|
// _, store, teardown := MustNewTestStore(t, false, true)
|
|
|
|
|
// defer teardown()
|
|
|
|
|
if isFileExist(options.BackupPath) {
|
|
|
|
|
t.Errorf("Backup file should not exist for dirty database; file=%s", options.BackupPath)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// store.MigrateData()
|
|
|
|
|
t.Run("MigrateData should recover and restore backup during migration critical failure", func(t *testing.T) {
|
|
|
|
|
os.Setenv("PORTAINER_TEST_MIGRATE_FAIL", "FAIL")
|
|
|
|
|
|
|
|
|
|
// options := store.setupOptions(getBackupRestoreOptions(store.commonBackupDir()))
|
|
|
|
|
version := "2.15"
|
|
|
|
|
_, store := MustNewTestStore(t, true, false)
|
|
|
|
|
store.VersionService.UpdateVersion(&models.Version{SchemaVersion: version, Edition: int(portainer.PortainerCE)})
|
|
|
|
|
err := store.MigrateData()
|
|
|
|
|
if err == nil {
|
|
|
|
|
t.Errorf("Expect migration to fail")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if isFileExist(options.BackupPath) {
|
|
|
|
|
// t.Errorf("Backup file should not exist for dirty database; file=%s", options.BackupPath)
|
|
|
|
|
// }
|
|
|
|
|
// })
|
|
|
|
|
store.Open()
|
|
|
|
|
testVersion(store, version, t)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Test_getBackupRestoreOptions(t *testing.T) {
|
|
|
|
|