From e10b081a0f36daafe8d1fa448881fb8a36456126 Mon Sep 17 00:00:00 2001 From: Christian Pinto Date: Tue, 25 Jul 2023 15:23:28 +0100 Subject: [PATCH 1/3] new unittest based testing framework Signed-off-by: Christian Pinto --- setup.sh | 1 + tests/test.py | 97 ++++++++++++++++----------------------------------- tests/test.sh | 34 +----------------- 3 files changed, 33 insertions(+), 99 deletions(-) diff --git a/setup.sh b/setup.sh index 7a09f8b7..6cc6e0eb 100755 --- a/setup.sh +++ b/setup.sh @@ -139,6 +139,7 @@ cp -r -f "$BASE_DIR"/emulator.py "$WORK_DIR"/ cp -r -f "$BASE_DIR"/ofmf-main.py "$WORK_DIR"/ cp -r -f "$BASE_DIR"/certificate_config.cnf "$WORK_DIR"/ cp -r -f "$BASE_DIR"/v3.ext "$WORK_DIR"/ +cp -r -f "$BASE_DIR"/tests "$WORK_DIR"/ # generating server key echo "Generating private key" diff --git a/tests/test.py b/tests/test.py index 21f513dd..494f9b56 100644 --- a/tests/test.py +++ b/tests/test.py @@ -1,69 +1,34 @@ -import json -import pprint -import requests import test_templates -import sys - -def send_post(url, payload): - headers = {'Content-type':'application/json', 'Accept':'text/plain'} - resp = requests.post (url, data = json.dumps(payload), headers = headers) - - return resp.status_code - -def send_get(url): - resp = requests.get(url) - - return resp.status_code, resp.json() - -def send_delete(url): - resp = requests.delete(url) - - return resp.status_code - -def test_create_chassis(url): - chassis_url = f"{url}/Chassis" - ret_code = send_post(chassis_url, test_templates.test_chassis) - - if ret_code == 200: - return True - else: - return False - -def test_create_computer_system(url): - # I am assuming we have already created the target Chassis - system_url = f"{url}/Systems" - ret_code = send_post(system_url, test_templates.test_system) - - if ret_code == 200: - return True - else: - return False - - -def test_create_computer_system_duplicate_id(): - # To Be Implemented - pass - - -def main(): - url = "http://localhost:5000/redfish/v1" - - print("##### Starting test Suite #####") - print() - print ("Testing creating a Chassis...") - if test_create_chassis(url): - print ("PASSED") - else: - print ("FAILED") - sys.exit(1) - - print("Testing creating a ComputerSystem...") - if test_create_computer_system(url): - print ("PASSED") - else: - print ("FAILED") - sys.exit(1) - +import unittest +from api_emulator.resource_manager import ResourceManager +import g + +REST_BASE = '/redfish/v1' +g.rest_base = REST_BASE + +class TestOFMF(unittest.TestCase): + @classmethod + def setUpClass(cls): + global resource_manager + global REST_BASE + global TRAYS + global SPEC + resource_manager = ResourceManager(None, None, None, "Disable", None) + g.app.testing = True + cls.client = g.app.test_client() + + def test_create_computer_system(self): + system_url = f"{REST_BASE}/Systems" + response = self.client.post(system_url, json=test_templates.test_system) + status_code = response.status_code + self.assertEqual(status_code, 200) + + def test_create_chassis(self): + chassis_url = f"{REST_BASE}/Chassis" + response = self.client.post(chassis_url, json=test_templates.test_chassis) + status_code = response.status_code + self.assertEqual(status_code, 200) if __name__ == '__main__': - main() \ No newline at end of file + unittest.main() +# main() \ No newline at end of file diff --git a/tests/test.sh b/tests/test.sh index 6ea20df9..728edbf3 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -1,42 +1,10 @@ #!/bin/bash -TIMEOUT=30 - -wait_ofmf () { - NOW=0 - ELAPSED=0 - START=$(date +"%s") - while [ $ELAPSED -lt $TIMEOUT ] - do - curl http://localhost:5000/redfish/v1 > /dev/null 2>&1 - if [ $? -eq 0 ] - then - return 0 - fi - # No need to go crazy here, let's sleep for a while - sleep 1 - NOW=$(date +"%s") - ELAPSED=$((${NOW} - ${START})) - done - - return 1 -} SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) echo "Script dir: $SCRIPT_DIR" ${SCRIPT_DIR}/../setup.sh -w ./emul -n -cp ${SCRIPT_DIR}/emulator-config-http.json ${SCRIPT_DIR}/../emul/emulator-config.json cd ${SCRIPT_DIR}/../emul -./venv/bin/python emulator.py & -cd - - -# wait for the OFMF service to be up of fail after TIMEOUT seconds -wait_ofmf -if [ $? -eq 1 ] -then - echo "Timing out, the OFMF failed to start" - exit 1 -fi -python ${SCRIPT_DIR}/test.py \ No newline at end of file +python -m unittest discover -s tests \ No newline at end of file From 3901cde53200ccde2995973ca0e150c62b534abe Mon Sep 17 00:00:00 2001 From: Christian Pinto Date: Tue, 25 Jul 2023 15:39:37 +0100 Subject: [PATCH 2/3] enable manual triggering Signed-off-by: Christian Pinto --- .github/workflows/repo-test.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/repo-test.yml b/.github/workflows/repo-test.yml index e7e10906..37de874b 100644 --- a/.github/workflows/repo-test.yml +++ b/.github/workflows/repo-test.yml @@ -1,5 +1,6 @@ name: Testing OFMF on: + workflow_dispatch: push: branches: master pull_request: From 56aff0f9b80c673cb5b109a74f3cdb02fb2da943 Mon Sep 17 00:00:00 2001 From: Christian Pinto Date: Thu, 27 Jul 2023 09:46:52 +0100 Subject: [PATCH 3/3] Activate venv in tests Signed-off-by: Christian Pinto --- tests/test.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test.sh b/tests/test.sh index 728edbf3..66a2d6a2 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -7,4 +7,6 @@ ${SCRIPT_DIR}/../setup.sh -w ./emul -n cd ${SCRIPT_DIR}/../emul +source ./venv/bin/activate + python -m unittest discover -s tests \ No newline at end of file