Implement Storage::backend_opened and Storage::backend_lost events

This commit is contained in:
Tim Wojtulewicz 2025-03-03 18:19:52 -07:00
parent a99a13dc4c
commit cc7b2dc890
10 changed files with 117 additions and 15 deletions

View file

@ -0,0 +1,4 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
open_result, [code=Storage::SUCCESS, error_str=<uninitialized>, value=<opaque of BackendHandleVal>]
Storage::backend_opened, Storage::REDIS, [redis=[server_host=127.0.0.1, server_port=xxxx/tcp, server_unix_socket=<uninitialized>, key_prefix=testing]]
Storage::backend_lost, Storage::REDIS, [redis=[server_host=127.0.0.1, server_port=xxxx/tcp, server_unix_socket=<uninitialized>, key_prefix=testing]], Server closed the connection

View file

@ -6,3 +6,5 @@ get result same as inserted, T
overwrite put result, [code=Storage::SUCCESS, error_str=<uninitialized>, value=<uninitialized>]
get result, [code=Storage::SUCCESS, error_str=<uninitialized>, value=value5678]
get result same as inserted, T
Storage::backend_opened, Storage::REDIS, [redis=[server_host=127.0.0.1, server_port=xxxx/tcp, server_unix_socket=<uninitialized>, key_prefix=testing]]
Storage::backend_lost, Storage::REDIS, [redis=[server_host=127.0.0.1, server_port=xxxx/tcp, server_unix_socket=<uninitialized>, key_prefix=testing]], Client disconnected

View file

@ -1,4 +1,9 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
Storage::backend_opened, Storage::SQLITE, [sqlite=[database_path=test.sqlite, table_name=testing, tuning_params={
[synchronous] = normal,
[temp_store] = memory,
[journal_mode] = WAL
}]]
open result, [code=Storage::SUCCESS, error_str=<uninitialized>, value=<opaque of BackendHandleVal>]
put result, [code=Storage::SUCCESS, error_str=<uninitialized>, value=<uninitialized>]
get result, [code=Storage::SUCCESS, error_str=<uninitialized>, value=value5678]

View file

@ -0,0 +1,44 @@
# @TEST-DOC: Tests basic Redis storage backend functions in sync mode, including overwriting
# @TEST-REQUIRES: have-redis
# @TEST-PORT: REDIS_PORT
# @TEST-EXEC: btest-bg-run redis-server run-redis-server ${REDIS_PORT%/tcp}
# @TEST-EXEC: zeek -b %INPUT | sed 's|=[0-9]*/tcp|=xxxx/tcp|g' > out
# @TEST-EXEC: btest-bg-wait -k 0
# @TEST-EXEC: btest-diff out
@load base/frameworks/storage/sync
@load policy/frameworks/storage/backend/redis
redef exit_only_after_terminate = T;
# Create a typename here that can be passed down into open_backend()
type str: string;
event Storage::backend_opened(tag: string, config: any) {
print "Storage::backend_opened", tag, config;
}
event Storage::backend_lost(tag: string, config: any, reason: string) {
print "Storage::backend_lost", tag, config, reason;
terminate();
}
event zeek_init()
{
local opts: Storage::BackendOptions;
opts$redis = [ $server_host="127.0.0.1", $server_port=to_port(getenv(
"REDIS_PORT")), $key_prefix="testing" ];
local key = "key1234";
local value = "value1234";
local open_res = Storage::Sync::open_backend(Storage::REDIS, opts, str, str);
print "open_result", open_res;
# Kill the redis server so the backend will disconnect and fire the backend_lost event.
system("cat redis-server/redis.pid");
system("kill $(cat redis-server/redis.pid)");
}

View file

@ -4,7 +4,7 @@
# @TEST-PORT: REDIS_PORT
# @TEST-EXEC: btest-bg-run redis-server run-redis-server ${REDIS_PORT%/tcp}
# @TEST-EXEC: zeek -b %INPUT > out
# @TEST-EXEC: zeek -b %INPUT | sed 's|=[0-9]*/tcp|=xxxx/tcp|g' > out
# @TEST-EXEC: btest-bg-wait -k 0
# @TEST-EXEC: btest-diff out
@ -15,6 +15,15 @@
# Create a typename here that can be passed down into open_backend()
type str: string;
event Storage::backend_opened(tag: string, config: any) {
print "Storage::backend_opened", tag, config;
}
event Storage::backend_lost(tag: string, config: any, reason: string) {
print "Storage::backend_lost", tag, config, reason;
terminate();
}
event zeek_init()
{
local opts: Storage::BackendOptions;

View file

@ -11,6 +11,10 @@ redef exit_only_after_terminate = T;
# Create a typename here that can be passed down into get().
type str: string;
event Storage::backend_opened(tag: string, config: any) {
print "Storage::backend_opened", tag, config;
}
event zeek_init()
{
# Create a database file in the .tmp directory with a 'testing' table