# Copyright (c) 2023-2026 Arista Networks, Inc. # Use of this source code is governed by the Apache License 2.0 # that can be found in the LICENSE file. """Unit tests for the anta.settings module.""" from __future__ import annotations import logging import os import sys from unittest.mock import patch import pytest from pydantic import ValidationError from anta.device import AsyncEOSDevice from anta.settings import DEFAULT_HTTPX_TRUST_ENV, DEFAULT_MAX_CONCURRENCY, DEFAULT_NOFILE, AntaHttpxSettings, AntaRunnerSettings, get_httpx_settings if os.name == "posix": # The function is not defined on non-POSIX system import resource # Some type ignores for https://github.com/pydantic/pydantic-settings/issues/201 class TestAntaRunnerSettings: """Tests for the AntaRunnerSettings class.""" def test_defaults(self, setenvvar: pytest.MonkeyPatch) -> None: """Test defaults for ANTA runner settings.""" settings = AntaRunnerSettings() assert settings.nofile == DEFAULT_NOFILE assert settings.max_concurrency == DEFAULT_MAX_CONCURRENCY def test_env_var(self, setenvvar: pytest.MonkeyPatch) -> None: """Test setting different ANTA runner settings.""" setenvvar.setenv("ANTA_NOFILE", "20480") settings = AntaRunnerSettings() assert settings.nofile == 20480 assert settings.max_concurrency == DEFAULT_MAX_CONCURRENCY def test_validation(self, setenvvar: pytest.MonkeyPatch) -> None: """Test validation of ANTA runner settings.""" setenvvar.setenv("ANTA_NOFILE", "-1") with pytest.raises(ValidationError): AntaRunnerSettings() setenvvar.setenv("ANTA_MAX_CONCURRENCY", "0") with pytest.raises(ValidationError): AntaRunnerSettings() @pytest.mark.skipif(os.name == "posix", reason="Run this test on Windows only") def test_file_descriptor_limit_windows(self, caplog: pytest.LogCaptureFixture) -> None: """Test file_descriptor_limit on Windows.""" caplog.set_level(logging.INFO) settings = AntaRunnerSettings() assert settings.file_descriptor_limit == sys.maxsize assert "Running on a non-POSIX system, cannot adjust the maximum number of file descriptors." in caplog.text @pytest.mark.skipif(os.name != "posix", reason="Fake non-posix for coverage") async def test_file_descriptor_limit_fake_windows(self, caplog: pytest.LogCaptureFixture) -> None: """Test file_descriptor_limit on fake Windows.""" caplog.set_level(logging.INFO) with patch("os.name", new="win32"): settings = AntaRunnerSettings() assert settings.file_descriptor_limit == sys.maxsize assert "Running on a non-POSIX system, cannot adjust the maximum number of file descriptors." in caplog.records[0].message @pytest.mark.skipif(os.name != "posix", reason="Cannot run this test on Windows") def test_file_descriptor_limit_posix(self, caplog: pytest.LogCaptureFixture) -> None: """Test file_descriptor_limit on POSIX systems.""" with ( caplog.at_level(logging.DEBUG), patch.dict("os.environ", {"ANTA_NOFILE": "20480"}), patch("resource.getrlimit") as getrlimit_mock, patch("resource.setrlimit") as setrlimit_mock, ): # Simulate the default system limits system_limits = (8192, 1048576) # Setup getrlimit mock return value getrlimit_mock.return_value = system_limits # Simulate setrlimit behavior def side_effect_setrlimit(_resource_id: int, limits: tuple[int, int]) -> None: getrlimit_mock.return_value = (limits[0], limits[1]) setrlimit_mock.side_effect = side_effect_setrlimit settings = AntaRunnerSettings() # Assert the limits were updated as expected assert settings.file_descriptor_limit == 20480 assert "Initial file descriptor limits for the current ANTA process: Soft Limit: 8192 | Hard Limit: 1048576" in caplog.text assert "Setting file descriptor soft limit to 20480" in caplog.text setrlimit_mock.assert_called_once_with(resource.RLIMIT_NOFILE, (20480, 1048576)) # pylint: disable=possibly-used-before-assignment # pyright: ignore[reportPossiblyUnboundVariable] @pytest.mark.skipif(os.name != "posix", reason="Cannot run this test on Windows") def test_file_descriptor_limit_value_error(self, caplog: pytest.LogCaptureFixture) -> None: """Test file_descriptor_limit with invalid environment variables.""" with ( caplog.at_level(logging.DEBUG), patch.dict("os.environ", {"ANTA_NOFILE": "666"}), patch("resource.getrlimit") as getrlimit_mock, patch("resource.setrlimit") as setrlimit_mock, ): # Simulate the default system limits system_limits = (32768, 131072) # Setup getrlimit mock return value getrlimit_mock.return_value = system_limits # Simulate setrlimit behavior raising ValueError def side_effect_setrlimit(_resource_id: int, _limits: tuple[int, int]) -> None: msg = "not allowed to raise maximum limit" raise ValueError(msg) setrlimit_mock.side_effect = side_effect_setrlimit _ = AntaRunnerSettings() # Assert the limits were *NOT* updated as expected assert caplog.records[-1].levelname == "WARNING" assert "Failed to set file descriptor soft limit for the current ANTA process" in caplog.records[-1].getMessage() setrlimit_mock.assert_called_once_with(resource.RLIMIT_NOFILE, (666, 131072)) # pyright: ignore[reportPossiblyUnboundVariable] class TestAntaHttpxSettings: """Tests for the AntaHttpxSettings class.""" def test_defaults(self, setenvvar: pytest.MonkeyPatch) -> None: """Test that AntaHttpxSettings uses default values when no environment variables are set.""" httpx_settings = AntaHttpxSettings() assert httpx_settings.trust_env == DEFAULT_HTTPX_TRUST_ENV def test_env_var(self, setenvvar: pytest.MonkeyPatch) -> None: """Test that the ANTA_HTTPX_TRUST_ENV environment variable overrides the default trust_env value.""" setenvvar.setenv("ANTA_HTTPX_TRUST_ENV", "False") httpx_settings = AntaHttpxSettings() assert httpx_settings.trust_env is False @pytest.mark.skipif(os.name != "posix", reason="Cannot run this test on Windows") def test_env_var_attached_to_session(self, setenvvar: pytest.MonkeyPatch) -> None: """Test that the trust_env value from ANTA_HTTPX_SETTINGS singleton is passed to the asynceapi.Device session.""" get_httpx_settings.cache_clear() setenvvar.setenv("ANTA_HTTPX_TRUST_ENV", "False") device = AsyncEOSDevice(host="test", username="test", password="test", port=80) assert device._client.trust_env is False get_httpx_settings.cache_clear() def test_validation_error(self, setenvvar: pytest.MonkeyPatch) -> None: """Test that get_httpx_settings raises ValueError when an env var is invalid.""" get_httpx_settings.cache_clear() setenvvar.setenv("ANTA_HTTPX_TRUST_ENV", "not_a_valid_bool") with pytest.raises(ValueError, match=r"Failed to load ANTA HTTPX settings\. Check ANTA_HTTPX_\* environment variables:"): get_httpx_settings() get_httpx_settings.cache_clear()