2024-03-05 03:12:37 +00:00
|
|
|
# Nim-RocksDB
|
|
|
|
# Copyright 2024 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
#
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * GPL license, version 2.0, ([LICENSE-GPLv2](LICENSE-GPLv2) or https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html)
|
|
|
|
#
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
{.used.}
|
|
|
|
|
2024-06-26 15:00:10 +00:00
|
|
|
import std/os, tempfile, unittest2, ../rocksdb/backup, ./test_helper
|
2024-03-05 03:12:37 +00:00
|
|
|
|
|
|
|
suite "BackupEngineRef Tests":
|
|
|
|
let
|
|
|
|
key = @[byte(1), 2, 3, 4, 5]
|
|
|
|
val = @[byte(1), 2, 3, 4, 5]
|
|
|
|
|
|
|
|
setup:
|
|
|
|
let
|
|
|
|
dbPath = mkdtemp() / "data"
|
|
|
|
dbBackupPath = mkdtemp() / "backup"
|
2024-06-26 15:00:10 +00:00
|
|
|
dbRestorePath = mkdtemp() / "restore"
|
2024-07-03 15:46:42 +00:00
|
|
|
db = initReadWriteDb(dbPath)
|
2024-03-05 03:12:37 +00:00
|
|
|
|
|
|
|
teardown:
|
|
|
|
db.close()
|
|
|
|
removeDir($dbPath)
|
|
|
|
removeDir($dbBackupPath)
|
2024-07-03 15:46:42 +00:00
|
|
|
removeDir($dbRestorePath)
|
2024-03-05 03:12:37 +00:00
|
|
|
|
|
|
|
test "Test backup":
|
2024-07-03 15:46:42 +00:00
|
|
|
let engine = initBackupEngine(dbBackupPath)
|
2024-03-05 03:12:37 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
db.put(key, val).isOk()
|
|
|
|
db.keyExists(key).value()
|
|
|
|
|
|
|
|
check engine.createNewBackup(db).isOk()
|
|
|
|
|
|
|
|
check:
|
|
|
|
db.delete(key).isOk()
|
|
|
|
not db.keyExists(key).value()
|
|
|
|
|
|
|
|
check engine.restoreDbFromLatestBackup(dbRestorePath).isOk()
|
|
|
|
|
|
|
|
let db2 = initReadWriteDb(dbRestorePath)
|
|
|
|
check db2.keyExists(key).value()
|
2024-07-03 15:46:42 +00:00
|
|
|
db2.close()
|
2024-03-05 03:12:37 +00:00
|
|
|
|
|
|
|
engine.close()
|
|
|
|
|
|
|
|
test "Test close":
|
2024-07-03 15:46:42 +00:00
|
|
|
let engine = openBackupEngine(dbPath).get()
|
2024-03-05 03:12:37 +00:00
|
|
|
|
|
|
|
check not engine.isClosed()
|
|
|
|
engine.close()
|
|
|
|
check engine.isClosed()
|
|
|
|
engine.close()
|
|
|
|
check engine.isClosed()
|
2024-07-02 04:44:05 +00:00
|
|
|
|
|
|
|
test "Test auto close enabled":
|
|
|
|
let
|
2024-07-03 15:46:42 +00:00
|
|
|
backupOpts = defaultBackupEngineOptions(dbPath, autoClose = true)
|
2024-07-02 04:44:05 +00:00
|
|
|
backupEngine = openBackupEngine(dbPath, backupOpts).get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
backupOpts.isClosed() == false
|
|
|
|
backupEngine.isClosed() == false
|
|
|
|
|
|
|
|
backupEngine.close()
|
|
|
|
|
|
|
|
check:
|
|
|
|
backupOpts.isClosed() == true
|
|
|
|
backupEngine.isClosed() == true
|
|
|
|
|
|
|
|
test "Test auto close disabled":
|
|
|
|
let
|
2024-07-03 15:46:42 +00:00
|
|
|
backupOpts = defaultBackupEngineOptions(dbPath, autoClose = false)
|
2024-07-02 04:44:05 +00:00
|
|
|
backupEngine = openBackupEngine(dbPath, backupOpts).get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
backupOpts.isClosed() == false
|
|
|
|
backupEngine.isClosed() == false
|
|
|
|
|
|
|
|
backupEngine.close()
|
|
|
|
|
|
|
|
check:
|
|
|
|
backupOpts.isClosed() == false
|
|
|
|
backupEngine.isClosed() == true
|