summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrett Holman <brett.holman@canonical.com>2023-01-03 09:08:23 -0700
committerGitHub <noreply@github.com>2023-01-03 09:08:23 -0700
commit0bcb8048a66959bcddd99f5959f69917b3bc5bc9 (patch)
treebab2bdd8d4d2b9cae982220338f7ebcec783b66b
parent7368071568f444b576b49ffe9ef83e3928c9e8bb (diff)
downloadcloud-init-git-0bcb8048a66959bcddd99f5959f69917b3bc5bc9.tar.gz
test: mock dns calls (#1922)
Add fixture to disallow dns lookups by default in a common utility function.
-rw-r--r--tests/unittests/config/test_apt_source_v3.py3
-rw-r--r--tests/unittests/conftest.py16
-rw-r--r--tests/unittests/sources/test_aliyun.py3
-rw-r--r--tests/unittests/sources/test_ec2.py7
-rw-r--r--tests/unittests/sources/test_openstack.py7
-rw-r--r--tox.ini1
6 files changed, 36 insertions, 1 deletions
diff --git a/tests/unittests/config/test_apt_source_v3.py b/tests/unittests/config/test_apt_source_v3.py
index 5bb87385..8d7ba5dc 100644
--- a/tests/unittests/config/test_apt_source_v3.py
+++ b/tests/unittests/config/test_apt_source_v3.py
@@ -14,6 +14,8 @@ import tempfile
from unittest import TestCase, mock
from unittest.mock import call
+import pytest
+
from cloudinit import gpg, subp, util
from cloudinit.config import cc_apt_configure
from tests.unittests import helpers as t_help
@@ -955,6 +957,7 @@ class TestAptSourceConfig(t_help.FilesystemMockingTestCase):
self.assertEqual(mirrors["PRIMARY"], pmir)
self.assertEqual(mirrors["SECURITY"], smir)
+ @pytest.mark.allow_dns_lookup
def test_apt_v3_url_resolvable(self):
"""test_apt_v3_url_resolvable - Test resolving urls"""
diff --git a/tests/unittests/conftest.py b/tests/unittests/conftest.py
index 1ab17e8b..a3daaf22 100644
--- a/tests/unittests/conftest.py
+++ b/tests/unittests/conftest.py
@@ -2,6 +2,7 @@ import builtins
import glob
import os
from pathlib import Path
+from unittest import mock
import pytest
@@ -58,6 +59,21 @@ def fake_filesystem(mocker, tmpdir):
mocker.patch.object(mod, f, trap_func)
+@pytest.fixture(autouse=True)
+def disable_dns_lookup(request):
+ if "allow_dns_lookup" in request.keywords:
+ yield
+ return
+
+ def side_effect(args, *other_args, **kwargs):
+ raise AssertionError("Unexpectedly used util.is_resolvable")
+
+ with mock.patch(
+ "cloudinit.util.is_resolvable", side_effect=side_effect, autospec=True
+ ):
+ yield
+
+
PYTEST_VERSION_TUPLE = tuple(map(int, pytest.__version__.split(".")))
if PYTEST_VERSION_TUPLE < (3, 9, 0):
diff --git a/tests/unittests/sources/test_aliyun.py b/tests/unittests/sources/test_aliyun.py
index 6ceaf8f4..f95923a4 100644
--- a/tests/unittests/sources/test_aliyun.py
+++ b/tests/unittests/sources/test_aliyun.py
@@ -161,8 +161,9 @@ class TestAliYunDatasource(test_helpers.ResponsesTestCase):
self.default_metadata["hostname"], self.ds.get_hostname().hostname
)
+ @mock.patch("cloudinit.sources.DataSourceEc2.util.is_resolvable")
@mock.patch("cloudinit.sources.DataSourceAliYun._is_aliyun")
- def test_with_mock_server(self, m_is_aliyun):
+ def test_with_mock_server(self, m_is_aliyun, m_resolv):
m_is_aliyun.return_value = True
self.regist_default_server()
ret = self.ds.get_data()
diff --git a/tests/unittests/sources/test_ec2.py b/tests/unittests/sources/test_ec2.py
index 4dd7c497..3fe525e3 100644
--- a/tests/unittests/sources/test_ec2.py
+++ b/tests/unittests/sources/test_ec2.py
@@ -5,6 +5,7 @@ import json
import threading
from unittest import mock
+import pytest
import requests
import responses
@@ -223,6 +224,12 @@ TAGS_METADATA_2021_03_23: dict = {
}
+@pytest.fixture(autouse=True)
+def disable_is_resolvable():
+ with mock.patch("cloudinit.sources.DataSourceEc2.util.is_resolvable"):
+ yield
+
+
def _register_ssh_keys(rfunc, base_url, keys_data):
"""handle ssh key inconsistencies.
diff --git a/tests/unittests/sources/test_openstack.py b/tests/unittests/sources/test_openstack.py
index 8bcecae7..75a0dda1 100644
--- a/tests/unittests/sources/test_openstack.py
+++ b/tests/unittests/sources/test_openstack.py
@@ -10,6 +10,7 @@ import re
from io import StringIO
from urllib.parse import urlparse
+import pytest
import responses
from cloudinit import helpers, settings, util
@@ -76,6 +77,12 @@ EC2_VERSIONS = [
MOCK_PATH = "cloudinit.sources.DataSourceOpenStack."
+@pytest.fixture(autouse=True)
+def mock_is_resolvable():
+ with mock.patch(f"{MOCK_PATH}util.is_resolvable"):
+ yield
+
+
# TODO _register_uris should leverage test_ec2.register_mock_metaserver.
def _register_uris(version, ec2_files, ec2_meta, os_files, *, responses_mock):
"""Registers a set of url patterns into responses that will mimic the
diff --git a/tox.ini b/tox.ini
index dd7973b7..3b668bc0 100644
--- a/tox.ini
+++ b/tox.ini
@@ -318,3 +318,4 @@ markers =
ubuntu: this test should run on Ubuntu
unstable: skip this test because it is flakey
user_data: the user data to be passed to the test instance
+ allow_dns_lookup: disable autochecking for host network configuration