Skip to content

Commit

Permalink
Merge pull request #1086 from javierbrk/shrared-state-ubus
Browse files Browse the repository at this point in the history
Shrared state Async ubus interface
  • Loading branch information
G10h4ck committed Mar 15, 2024
2 parents 8d5d7de + 5356cbd commit d3d2086
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
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 <jjorge@inti.gob.ar>
Copyright (c) 2024 Instituto Nacional de Tecnolog..a Industrial
Copyright (C) 2024 Asociacion Civil Altermundi <info@altermundi.net>
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({
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",
"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)

0 comments on commit d3d2086

Please sign in to comment.