Add a few tests for services add/remove/status ...

This commit is contained in:
Alexandre Aubin 2020-05-06 18:53:06 +02:00
parent e74f49f001
commit 6fc5b41302
2 changed files with 103 additions and 0 deletions

View file

@ -92,6 +92,12 @@ test-regenconf:
- cd src/yunohost - cd src/yunohost
- py.test tests/test_regenconf.py - py.test tests/test_regenconf.py
test-service:
extends: .test-stage
script:
- cd src/yunohost
- py.test tests/test_service.py
######################################## ########################################
# LINTER # LINTER
######################################## ########################################

View file

@ -0,0 +1,97 @@
import os
from conftest import message, raiseYunohostError
from yunohost.service import _get_services, _save_services, service_status, service_add, service_remove
def setup_function(function):
clean()
def teardown_function(function):
clean()
def clean():
# To run these tests, we assume ssh(d) service exists and is running
assert os.system("pgrep sshd >/dev/null") == 0
services = _get_services()
assert "ssh" in services
if "dummyservice" in services:
del services["dummyservice"]
_save_services(services)
def test_service_status_all():
status = service_status()
assert "ssh" in status.keys()
assert status["ssh"]["status"] == "running"
def test_service_status_single():
status = service_status("ssh")
assert "status" in status.keys()
assert status["status"] == "running"
def test_service_status_unknown_service(mocker):
with raiseYunohostError(mocker, 'service_unknown'):
service_status(["ssh", "doesnotexists"])
def test_service_add():
service_add("dummyservice", description="A dummy service to run tests")
assert "dummyservice" in service_status().keys()
def test_service_remove():
service_add("dummyservice", description="A dummy service to run tests")
assert "dummyservice" in service_status().keys()
service_remove("dummyservice")
assert "dummyservice" not in service_status().keys()
def test_service_remove_service_that_doesnt_exists(mocker):
assert "dummyservice" not in service_status().keys()
with raiseYunohostError(mocker, 'service_unknown'):
service_remove("dummyservice")
assert "dummyservice" not in service_status().keys()
def test_service_update_to_add_properties():
service_add("dummyservice", description="")
assert not _get_services()["dummyservice"].get("test_status")
service_add("dummyservice", description="", test_status="true")
assert _get_services()["dummyservice"].get("test_status") == "true"
def test_service_update_to_change_properties():
service_add("dummyservice", description="", test_status="false")
assert _get_services()["dummyservice"].get("test_status") == "false"
service_add("dummyservice", description="", test_status="true")
assert _get_services()["dummyservice"].get("test_status") == "true"
def test_service_update_to_remove_properties():
service_add("dummyservice", description="", test_status="false")
assert _get_services()["dummyservice"].get("test_status") == "false"
service_add("dummyservice", description="", test_status="")
assert not _get_services()["dummyservice"].get("test_status")