diff --git a/packages/shared-state-async/files/usr/libexec/rpcd/shared-state-async b/packages/shared-state-async/files/usr/libexec/rpcd/shared-state-async new file mode 100755 index 000000000..a96d872f4 --- /dev/null +++ b/packages/shared-state-async/files/usr/libexec/rpcd/shared-state-async @@ -0,0 +1,63 @@ +#!/usr/bin/env lua + +--[[ +Shared State Async + +Copyright (c) 2024 Javier Jorge +Copyright (c) 2024 Instituto Nacional de Tecnolog..a Industrial +Copyright (C) 2024 Asociacion Civil Altermundi + +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 diff --git a/packages/shared-state-async/files/usr/share/rpcd/acl.d/shared-state-async.json b/packages/shared-state-async/files/usr/share/rpcd/acl.d/shared-state-async.json new file mode 100644 index 000000000..f1651104f --- /dev/null +++ b/packages/shared-state-async/files/usr/share/rpcd/acl.d/shared-state-async.json @@ -0,0 +1,15 @@ +{ + "lime-app": { + "description": "lime-app public access", + "read": { + "ubus": { + "shared-state-async": [ "*" ] + } + }, + "write": { + "ubus": { + "shared-state-async": [ "*" ] + } + } + } +} diff --git a/packages/shared-state-async/tests/test_rpcd_shared-state-async.lua b/packages/shared-state-async/tests/test_rpcd_shared-state-async.lua new file mode 100644 index 000000000..fcacfa3d9 --- /dev/null +++ b/packages/shared-state-async/tests/test_rpcd_shared-state-async.lua @@ -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)