Added Get test

This commit is contained in:
Amin Ben Romdhane 2024-02-16 18:07:54 +01:00
parent 0e5106a38b
commit ff8deafeae
3 changed files with 108 additions and 0 deletions

View file

@ -24,6 +24,9 @@ exec_cmd ubus wait_for bbfdm
# Test 'bbfdm.AddObj' & 'bbfdm.DelObj' event
./test/python-test-cases/python/validate_add_del_event.py test/python-test-cases/json/add_del_event.json
# Test ubus bbfdm 'get' method
./test/python-test-cases/python/validate_ubus_get_method.py test/python-test-cases/json/ubus_get_method.json
supervisorctl stop all
supervisorctl status

View file

@ -0,0 +1,33 @@
{
"object": "bbfdm",
"test-cases": [
{
"description": "Test-Case-1: Get Parameter with correct path",
"input": {
"path": "Device.DeviceInfo.Manufacturer",
"optional": {"format": "raw", "proto": "usp"}
},
"expected_error": 0,
"output": {
"results": [
{
"path": "Device.DeviceInfo.Manufacturer",
"data": "iopsys",
"type": "xsd:string"
}
]
}
},
{
"description": "Test-Case-2: Get Parameter with wrong path",
"input": {
"path": "Device.DeviceInfo.Manufacture",
"optional": {"format": "raw", "proto": "usp"}
},
"expected_error": 7026,
"output": {}
}
]
}

View file

@ -0,0 +1,72 @@
#!/usr/bin/python3
import sys
import ubus
import json
class TestArguments:
"""
Class to hold test arguments.
"""
def __init__(self, description, service, input, expected_error, output):
self.description = description
self.service = service
self.input = input
self.expected_error = expected_error
self.output = output
def run_test(args):
"""
Run a single test.
"""
print("Running: " + args.description)
out = ubus.call(args.service, "get", args.input)
if isinstance(out, list) and out:
out = out[0]
else:
print("FAIL: " + args.description)
return
if "fault" in out["results"][0] and out["results"][0]["fault"] != args.expected_error:
print("FAIL: " + args.description)
return
# Check if output matches expected output
if args.output != {}:
if out == args.output:
print("PASS: " + args.description)
else:
print("FAIL: " + args.description)
else:
print("PASS: " + args.description)
if __name__ == "__main__":
# Check for correct command line arguments
if len(sys.argv) != 2:
print("Usage: {} <test_arguments_json>".format(sys.argv[0]))
sys.exit(1)
test_arguments_file = sys.argv[1]
try:
with open(test_arguments_file, 'r') as f:
test_arguments_data = json.load(f)
service_name = test_arguments_data.get("object", "bbfdm")
args_list = [TestArguments(**test_case, service=service_name) for test_case in test_arguments_data["test-cases"]]
except FileNotFoundError:
print("File not found:", test_arguments_file)
sys.exit(1)
except json.JSONDecodeError as e:
print("Error parsing JSON:", e)
sys.exit(1)
ubus.connect()
# Run tests for each set of arguments
for args in args_list:
run_test(args)
ubus.disconnect()