From a3dbafe9294948708da9e5030f044f695a99b999 Mon Sep 17 00:00:00 2001 From: Adam Williamson Date: Thu, 25 Jan 2024 15:55:03 -0800 Subject: [PATCH] tests: mock network functions so tests work with no network There's a few places where the tests implicitly need a network connection. Mainly, since the TDL XML test blocks in test_guest are for a subclass of RedHatLinuxCDYumGuest and specify a URL, we wind up hitting RedHatLinuxCDYumGuest._check_url which tries to get the headers for the URL to figure out if the server supports byte ranges. Let's just mock out the header retrieval to make that method happy. Secondly, test-53-command-http-url.tdl specifies an HTTP URL, so we wind up trying to retrieve it; mock out the actual download, since that's not what this test is really exercising. Signed-off-by: Adam Williamson --- Containerfile.tests.el7 | 2 +- README | 2 +- tests/guest/test_guest.py | 9 ++++++++- tests/tdl/test_tdl.py | 7 ++++++- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Containerfile.tests.el7 b/Containerfile.tests.el7 index 06a9b78..406fbb8 100644 --- a/Containerfile.tests.el7 +++ b/Containerfile.tests.el7 @@ -4,7 +4,7 @@ FROM quay.io/centos/centos:7 RUN set -exo pipefail \ && yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm \ - && yum install -y python-requests m2crypto libvirt-python python-lxml python-libguestfs pytest python-monotonic libvirt \ + && yum install -y python-requests m2crypto libvirt-python python-lxml python-libguestfs pytest python-monotonic python-mock libvirt \ && yum clean all \ && rm -rf /var/cache/* /var/log/yum* diff --git a/README b/README index 5f2c983..c53e23d 100644 --- a/README +++ b/README @@ -28,7 +28,7 @@ dnf install python3-requests python3-m2crypto python3-libvirt python3-lxml pytho If you wish to test on EL 7, make that: -yum install python-requests m2crypto libvirt-python python-lxml python-libguestfs pytest python-monotonic +yum install python-requests m2crypto libvirt-python python-lxml python-libguestfs pytest python-monotonic python-mock then run the tests: diff --git a/tests/guest/test_guest.py b/tests/guest/test_guest.py index 625eb67..880807b 100644 --- a/tests/guest/test_guest.py +++ b/tests/guest/test_guest.py @@ -12,6 +12,10 @@ except: from io import StringIO import logging import os +try: + from unittest import mock +except ImportError: + import mock # Find oz library prefix = '.' @@ -72,7 +76,10 @@ def setup_guest(xml, macaddress=None): config = configparser.ConfigParser() config.read_file(StringIO("[libvirt]\nuri=qemu:///session\nbridge_name=%s" % route)) - guest = oz.GuestFactory.guest_factory(tdl, config, None, macaddress=macaddress) + # mock this - it's used in RedHatLinuxCDYumGuest._check_url() - + # so the tests can run without a network connection + with mock.patch("oz.ozutil.http_get_header", return_value={}): + guest = oz.GuestFactory.guest_factory(tdl, config, None, macaddress=macaddress) return guest tdlxml = """ diff --git a/tests/tdl/test_tdl.py b/tests/tdl/test_tdl.py index a94b2c1..badee63 100644 --- a/tests/tdl/test_tdl.py +++ b/tests/tdl/test_tdl.py @@ -2,6 +2,10 @@ import sys import os +try: + from unittest import mock +except ImportError: + import mock try: import lxml.etree @@ -97,7 +101,8 @@ tests = { } # Test that iterates over all .tdl files -def test_all(): +@mock.patch("oz.ozutil.http_download_file") +def test_all(fakedl): for (tdl, expected_pass) in list(tests.items()): # locate full path for tdl file -- 2.43.0