From ff8deafeae87bdfebddb6b899c0d3ace40712746 Mon Sep 17 00:00:00 2001 From: Amin Ben Romdhane Date: Fri, 16 Feb 2024 18:07:54 +0100 Subject: [PATCH] Added Get test --- gitlab-ci/functional-test.sh | 3 + .../json/ubus_get_method.json | 33 +++++++++ .../python/validate_ubus_get_method.py | 72 +++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 test/python-test-cases/json/ubus_get_method.json create mode 100755 test/python-test-cases/python/validate_ubus_get_method.py diff --git a/gitlab-ci/functional-test.sh b/gitlab-ci/functional-test.sh index bd54a519..e64638d0 100755 --- a/gitlab-ci/functional-test.sh +++ b/gitlab-ci/functional-test.sh @@ -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 diff --git a/test/python-test-cases/json/ubus_get_method.json b/test/python-test-cases/json/ubus_get_method.json new file mode 100644 index 00000000..858bbc7e --- /dev/null +++ b/test/python-test-cases/json/ubus_get_method.json @@ -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": {} + } + ] +} + + diff --git a/test/python-test-cases/python/validate_ubus_get_method.py b/test/python-test-cases/python/validate_ubus_get_method.py new file mode 100755 index 00000000..2a980492 --- /dev/null +++ b/test/python-test-cases/python/validate_ubus_get_method.py @@ -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: {} ".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()