Commit b4b719a1 authored by Daniele Venzano's avatar Daniele Venzano

Use pytest for unit tests

parent b715597d
......@@ -3,3 +3,4 @@ vulture
doc8
mypy-lang
coverage
pytest
......@@ -3,5 +3,7 @@
set -e
pylint *.py zoe_* tests/*.py
pytest --ignore tests pytest --tb=short
doc8 docs/
sh ./build_docs.sh
......@@ -3,28 +3,36 @@
"size": 512,
"services": [
{
"docker_image": "nginx:alpine",
"image": "nginx:alpine",
"volumes": [],
"environment": [],
"essential_count": 1,
"monitor": true,
"name": "nginx",
"ports": [
{
"expose": false,
"is_main_endpoint": true,
"name": "Nginx Webserver",
"path": "/",
"url_template": "http://{ip_port}/",
"port_number": 80,
"protocol": "http"
"protocol": "tcp"
}
],
"required_resources": {
"memory": 536870912
"resources": {
"memory": {
"min": 536870912,
"max": 536870912
},
"cores": {
"min": null,
"max": null
}
},
"startup_order": 0,
"total_count": 1
"total_count": 1,
"replicas": 1,
"command": null
}
],
"version": 2,
"version": 3,
"will_end": false
}
#!/usr/bin/env python3
# Copyright (c) 2017, Jordan Kuhn
#
# Licensed under the Apache License, Version 2.0 (the "License");
......@@ -17,54 +15,27 @@
"""Unit tests for zoe_lib/applications.py"""
import unittest
from unittest.mock import patch, MagicMock
import json
import pytest
from zoe_lib import applications
from zoe_lib import version
class TestApplicationsMethods(unittest.TestCase):
class TestApplicationsMethods:
"""Application validation tests."""
app_validate_data = {}
port_check_data = {}
def setUp(self):
"""Set up tests."""
current_version = version.ZOE_APPLICATION_FORMAT_VERSION
self.app_validate_data = {
"name": "test name",
"will_end": True,
"size": 3,
"version": current_version,
"services": []
}
self.port_check_data = {
"name": "test name",
"protocol": "http",
"port_number": 3,
"is_main_endpoint": True
}
def test_pass_for_zapp(self):
"""Test zapp validation code."""
zapp_fp = json.load(open('tests/zapp.json', 'r'))
applications.app_validate(zapp_fp)
def test_pass_for_valid_port(self):
"""Test port validation code."""
try:
applications._port_check(self.port_check_data) # pylint: disable=protected-access
except Exception:
self.fail("_port_check threw an exception for valid data")
def test_pass_for_complex_zapp(self):
"""Test zapp validation code."""
zapp_fp = json.load(open('contrib/zoeapps/eurecom_aml_lab.json', 'r'))
applications.app_validate(zapp_fp)
def test_fails_for_bad_port_number(self):
def test_fails_for_zapp(self):
"""Test validation of port number."""
self.port_check_data["port_number"] = "not_a_number"
self.assertRaises(applications.InvalidApplicationDescription, applications._port_check, self.port_check_data) # pylint: disable=protected-access
def test_fails_for_missing_key(self):
"""Test missing name key from the port definition."""
del self.port_check_data["name"]
self.assertRaises(applications.InvalidApplicationDescription, applications._port_check, self.port_check_data) # pylint: disable=protected-access
@patch("zoe_lib.applications._validate_all_services", MagicMock())
@patch("zoe_lib.applications._port_check", MagicMock())
def test_pass_for_valid_app(self):
"""Test validation of entire app."""
applications.app_validate(self.app_validate_data)
bad_fp = open('/dev/random', 'r')
with pytest.raises(applications.InvalidApplicationDescription):
applications.app_validate(bad_fp)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment