Skip to content

Commit

Permalink
Merge pull request #20 from christian-pinto/unittest
Browse files Browse the repository at this point in the history
Unittest
  • Loading branch information
mgazz committed Aug 11, 2023
2 parents 0191734 + 56aff0f commit 59f075f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 98 deletions.
1 change: 1 addition & 0 deletions .github/workflows/repo-test.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name: Testing OFMF
on:
workflow_dispatch:
push:
branches: master
pull_request:
Expand Down
1 change: 1 addition & 0 deletions setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
97 changes: 31 additions & 66 deletions tests/test.py
Original file line number Diff line number Diff line change
@@ -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()
unittest.main()
# main()
34 changes: 2 additions & 32 deletions tests/test.sh
Original file line number Diff line number Diff line change
@@ -1,42 +1,12 @@
#!/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
source ./venv/bin/activate

python ${SCRIPT_DIR}/test.py
python -m unittest discover -s tests

0 comments on commit 59f075f

Please sign in to comment.