Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shrared state Async ubus interface #1086

Merged
merged 8 commits into from
Mar 15, 2024
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env lua

--[[
Shared State Async

Copyright (c) 2024 Javier Jorge <[email protected]>
Copyright (c) 2024 Instituto Nacional de Tecnolog..a Industrial
Copyright (C) 2024 Asociacion Civil Altermundi <[email protected]>

This is free software, licensed under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3
]]
--
local utils = require('lime.utils')
local json = require 'luci.jsonc'

local function get(msg)
local error = os.execute("shared-state-async get " ..
msg.data_type .. " 2>/dev/null ")
-- if os.execute dont fail will print the output and rpcd wont execute
-- the following lines. If there is an error the above code wont print
-- anything and this code will return the error code.
utils.printJson({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That the error get printed only on failure happens implicitely, please add at least a comment documentig this behaviour, so we cna understand what's happening when we look at this code in a few months or years ;)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done ! it is a weird behavior that gives a lot of headaches. Any output that is printed in the console is used as output. If its a json everything goes fine, if it is not a json you get "invalid input" as a response.

error = error
})
end

local function sync(msg)
local error = os.execute("shared-state-async sync " ..
msg.data_type .. " " .. table.concat(msg.peers_ip or {}, " ") .. " 2>/dev/null ")
utils.printJson({
error = error
})
end

--{"data_type":"data","peers_ip":["10.0.0.1","10.0.0.2"]}
--{"data_type":"data","peers_ip":["10.0.0.1"]}
local methods = {
get = {
data_type = 'value'
},
sync = {
data_type = 'value',
peers_ip = 'value'
}
}

if arg[1] == 'list' then
utils.printJson(methods)
end

if arg[1] == 'call' then
local msg = utils.rpcd_readline()
msg = json.parse(msg)
if arg[2] == 'get' then
get(msg)
elseif arg[2] == 'sync' then
sync(msg)
else
utils.printJson({
error = "Method not found"
})
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"lime-app": {
"description": "lime-app public access",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we explicitely mention lime-app here? other components could benefit to access shared-state-async via ubus too

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While logged in via ssh, you have direct, full access to ubus. This file is to enable lime app to use it. other users should enable other profiles.

"read": {
"ubus": {
"shared-state-async": [ "*" ]
}
},
"write": {
"ubus": {
"shared-state-async": [ "*" ]
}
}
}
}
34 changes: 34 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,34 @@
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

--since there is no Shared State async binary, testing possiblities are reduced
--manual testing can be done on a router with bat-hosts package using this commands:
--ubus -S call shared-state-async get "{'data_type': 'bat-hosts'}"
--ubus -S call shared-state-async sync "{'data_type': 'bat-hosts'}"
--ubus -S call shared-state-async sync "{'data_type': 'bat-hosts' ,'peers_ip':['10.0.0.1','10.0.0.2']}'"


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.get.data_type)
assert.is.equal("value", response.sync.data_type)
assert.is.equal("value", response.sync.peers_ip)

end)
end)
Loading