-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1086 from javierbrk/shrared-state-ubus
Shrared state Async ubus interface
- Loading branch information
Showing
3 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
packages/shared-state-async/files/usr/libexec/rpcd/shared-state-async
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({ | ||
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 |
15 changes: 15 additions & 0 deletions
15
packages/shared-state-async/files/usr/share/rpcd/acl.d/shared-state-async.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
34
packages/shared-state-async/tests/test_rpcd_shared-state-async.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |