Convert old tests to pytest

This commit is contained in:
ponas committed 2026-03-19 02:05:30 +01:00
1 parent a2fcc7321d
commit 8b002d4620
3 files changed
+88 -80

No files matched your search

+43
View File
@@ -0,0 +1,43 @@
import pytest
from django.contrib.auth.models import User
from django.core.management import call_command
from django.urls import reverse
from rest_framework.authtoken.models import Token
from rest_framework.test import APIClient
from mdb.models import Host
@pytest.fixture
def db_with_fixtures(db):
call_command("loaddata", "test_data", verbosity=0)
@pytest.fixture
def api_client():
return APIClient()
@pytest.fixture
def user_with_token(db_with_fixtures):
user = User.objects.create(username='admin')
token = Token.objects.create(user=user)
return user, token
@pytest.fixture
def host(db_with_fixtures):
return Host.objects.first()
@pytest.mark.django_db
def test_validate_puppet_host_by_secret(api_client, user_with_token, host):
_, token = user_with_token
data = {
'pxe_key': host.pxe_key,
'certname': '{}.{}'.format(host.hostname, host.interface_set.first().domain.domain_name),
}
api_client.credentials(HTTP_AUTHORIZATION='Token {}'.format(token.key))
res = api_client.post(reverse('api-validate-host-secret'), data, format='json')
assert res.status_code == 200, res.content
assert not Host.objects.get(pk=host.pk).pxe_installable
+45
View File
@@ -0,0 +1,45 @@
import pytest
from django.conf import settings
from django.core.management import call_command
from mdb.models import Host, OperatingSystem
from mdb.pxe import render_pxelinux_cfg
@pytest.fixture
def db_with_fixtures(db):
call_command("loaddata", "test_data", verbosity=0)
@pytest.fixture
def host(db_with_fixtures):
return Host.objects.first()
@pytest.mark.django_db
def test_render_pxeconfig(host):
interface = host.interface_set.exclude(ip4address__isnull=True).first()
config = render_pxelinux_cfg(interface, host)
assert settings.MDB_PXE_INITRD in config
assert settings.MDB_PXE_KERNEL in config
assert settings.MDB_PXE_PRESEED_URL in config
@pytest.mark.django_db
def test_render_pxeconfig_with_os_pxe_config(host):
_os = OperatingSystem.objects.get(pk=2) # OS with all pxe vars set
Host.objects.filter(pk=host.pk).update(operating_system=_os)
host = Host.objects.first()
interface = host.interface_set.exclude(ip4address__isnull=True).first()
config = render_pxelinux_cfg(interface, host)
assert _os.pxe_initrd in config
assert _os.pxe_kernel in config
assert _os.pxe_preseed_config_url in config
@pytest.mark.django_db
def test_host_as_pxe_files(host):
assert len(host.as_pxe_files()) == 1