mirror of
https://github.com/logos-storage/logos-storage-nim.git
synced 2026-07-31 22:13:16 +00:00
fix: crash when config is invalid (#1494)
Co-authored-by: Giuliano Mega <giuliano.mega@gmail.com>
This commit is contained in:
parent
179c4d058e
commit
7fd4e39a3d
@ -104,6 +104,7 @@ proc createStorage(
|
||||
version = storageFullVersion,
|
||||
envVarsPrefix = "storage",
|
||||
cmdLine = @[],
|
||||
quitOnFailure = false,
|
||||
secondarySources = proc(
|
||||
config: StorageConf, sources: auto
|
||||
) {.gcsafe, raises: [ConfigurationError].} =
|
||||
@ -112,7 +113,8 @@ proc createStorage(
|
||||
,
|
||||
)
|
||||
except ConfigurationError as e:
|
||||
return err("Failed to create Storage: unable to load configuration: " & e.msg)
|
||||
# We cannot use e.msg because it is not populated by config-utils
|
||||
return err("Failed to create Storage: unable to load configuration.")
|
||||
|
||||
let logFile = conf.setupLogging()
|
||||
|
||||
|
||||
@ -61,9 +61,6 @@ proc buildConfig(
|
||||
return StorageConf.load(cmdLine = config.cliArgs, quitOnFailure = false)
|
||||
except ConfigurationError as e:
|
||||
raiseStorageConfigError msg & e.msg.postFix
|
||||
except Exception as e:
|
||||
## TODO: remove once proper exception handling added to nim-confutils
|
||||
raiseStorageConfigError msg & e.msg.postFix
|
||||
|
||||
proc addCliOption*(
|
||||
config: var StorageConfig, group = StartUpCmd.noCmd, cliOption: CliOption
|
||||
|
||||
52
tests/libstorage/config.nim
Normal file
52
tests/libstorage/config.nim
Normal file
@ -0,0 +1,52 @@
|
||||
import std/json
|
||||
import std/monotimes
|
||||
import std/os
|
||||
import std/strutils
|
||||
|
||||
import pkg/chronos
|
||||
import pkg/results
|
||||
|
||||
import ../asynctest
|
||||
import ../checktest
|
||||
import ../../library/storage_thread_requests/requests/node_lifecycle_request
|
||||
|
||||
from ../../storage/storage import StorageServer
|
||||
|
||||
asyncchecksuite "Libstorage - config":
|
||||
var server: StorageServer
|
||||
|
||||
test "rejects malformed JSON":
|
||||
let request =
|
||||
NodeLifecycleRequest.createShared(CREATE_NODE, """{"log-level": "debug"""")
|
||||
let res = await request.process(addr server)
|
||||
|
||||
check res.isErr
|
||||
|
||||
if res.isErr:
|
||||
check "unable to load configuration" in res.error
|
||||
|
||||
test "rejects an unknown option":
|
||||
let request =
|
||||
NodeLifecycleRequest.createShared(CREATE_NODE, """{"unknown": "debug"}""")
|
||||
let res = await request.process(addr server)
|
||||
|
||||
check res.isErr
|
||||
|
||||
if res.isErr:
|
||||
check "unable to load configuration" in res.error
|
||||
|
||||
test "accepts a valid config":
|
||||
let dataDir = getTempDir() / "libstorage-config" / $getMonoTime()
|
||||
|
||||
defer:
|
||||
removeDir(dataDir)
|
||||
|
||||
# %* escapes the path so that it can be used in JSON.
|
||||
let config = $ %*{"data-dir": dataDir}
|
||||
let request = NodeLifecycleRequest.createShared(CREATE_NODE, config.cstring)
|
||||
let res = await request.process(addr server)
|
||||
|
||||
check res.isOk
|
||||
|
||||
let closeRequest = NodeLifecycleRequest.createShared(CLOSE_NODE)
|
||||
check (await closeRequest.process(addr server)).isOk
|
||||
@ -1,4 +1,5 @@
|
||||
# Tests the Nim side of libstorage.
|
||||
import ./libstorage/config
|
||||
import ./libstorage/logosmetrics
|
||||
|
||||
{.warning[UnusedImport]: off.}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user