Skip to content

Commit

Permalink
shared-state-async: improves format adds simple test
Browse files Browse the repository at this point in the history
  • Loading branch information
javierbrk committed Feb 16, 2024
1 parent 4397103 commit 9b37013
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,26 @@ local function parseErrors()
elseif string.find(error, "Invalid peer address:") ~= nil then
utils.printJson({
error = "invalid_peer_address"
})
})
end
end
end

local function getFromSharedState(msg)
print(utils.unsafe_shell("shared-state-async get " .. msg.data_type .. " 2> " .. ERROR_LOG_FILE_NAME))
print(utils.unsafe_shell("shared-state-async get " .. msg.data_type ..
" 2> " .. ERROR_LOG_FILE_NAME))
--if ther are errors the function wont return and wont print.
parseErrors()
end

local function sync(msg)
if (type(msg.peers_ip) == 'table') then
utils.unsafe_shell("shared-state-async sync " ..
msg.data_type .. " " .. table.concat(msg.peers_ip, " ") .. " 2> " .. ERROR_LOG_FILE_NAME)
msg.data_type .. " " .. table.concat(msg.peers_ip, " ") .. " 2> "
.. ERROR_LOG_FILE_NAME)
else
utils.unsafe_shell("shared-state-async sync " .. msg.data_type .. " " .. " 2> " .. ERROR_LOG_FILE_NAME)
utils.unsafe_shell("shared-state-async sync " .. msg.data_type .. " " ..
" 2> " .. ERROR_LOG_FILE_NAME)
end
parseErrors()
utils.printJson({
Expand All @@ -66,7 +69,7 @@ if arg[1] == 'list' then
end

if arg[1] == 'call' then
utils.unsafe_shell("rm " .. ERROR_LOG_FILE_NAME .."> /dev/null 2>&1")
utils.unsafe_shell("rm " .. ERROR_LOG_FILE_NAME .. "> /dev/null 2>&1")
local msg = utils.rpcd_readline()
msg = json.parse(msg)
if arg[2] == 'getFromSharedState' then
Expand Down
41 changes: 41 additions & 0 deletions packages/shared-state-async/tests/test_rpcd_shared-state-async.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
local testUtils = require "tests.utils"
local sharedState = require("shared-state")
local json = require("luci.jsonc")

local testFileName = "packages/shared-state-async/files/usr/libexec/rpcd/shared-state-async"
local sharedStateRpc = testUtils.load_lua_file_as_function(testFileName)
local rpcdCall = testUtils.rpcd_call

--sicce there is no Shared State async binary, testing posiblilites are reduced
--manual testing can be done on a routeer with bat-hosts package using this:
-- # ubus -S call shared-state-async getFromSharedState "{'data_type': 'bat-hosts'}"
-- # ubus -S call shared-state-async getFromSharedState "{'data_type': 'bat-hosss'}"
-- {"error":"invalid_data_type"}
-- # ubus -S call shared-state-async sync "{'data_type': 'bat-hosts' ,'peers_ip':['10.0.0.1','10.0.0.2']}'"
-- {"status":"success"}
-- # ubus -S call shared-state-async sync "{'data_type': 'bat-hosts' ,'peers_ip':['10.0.0.1','10.0..2']}'"
-- {"error":"invalid_peer_address"}
-- # ubus -S call shared-state-async sync "{'data_type': 'bat-hoss' ,'peers_ip':['10.0.0.1','10.0.0.2']}'"
-- {"error":"invalid_data_type"}



describe('ubus-shared-state tests #ubus-shared-state', function()
before_each('', function()
testDir = testUtils.setup_test_dir()
sharedState.DATA_DIR = testDir
sharedState.PERSISTENT_DATA_DIR = testDir
end)

after_each('', function()
testUtils.teardown_test_dir()
end)

it('test list methods', function()
local response = rpcdCall(sharedStateRpc, {'list'})
assert.is.equal("value", response.getFromSharedState.data_type)
assert.is.equal("value", response.sync.data_type)
assert.is.equal("value", response.sync.peers_ip)

end)
end)

0 comments on commit 9b37013

Please sign in to comment.