deb-mock/tests/conftest.py
robojerk c51819c836
Some checks failed
Build Deb-Mock Package / build (push) Failing after 1m9s
Lint Code / Lint All Code (push) Failing after 1s
Test Deb-Mock Build / test (push) Failing after 35s
Add comprehensive testing framework, performance monitoring, and plugin system
- Add complete pytest testing framework with conftest.py and test files
- Add performance monitoring and benchmarking capabilities
- Add plugin system with ccache plugin example
- Add comprehensive documentation (API, deployment, testing, etc.)
- Add Docker API wrapper for service deployment
- Add advanced configuration examples
- Remove old wget package file
- Update core modules with enhanced functionality
2025-08-19 20:49:32 -07:00

324 lines
8.7 KiB
Python

"""
Common pytest fixtures and configuration for deb-mock tests
"""
import pytest
import tempfile
import shutil
import os
from pathlib import Path
from unittest.mock import Mock, patch
from deb_mock.config import Config
from deb_mock.core import DebMock
@pytest.fixture
def temp_dir():
"""Create a temporary directory for tests"""
temp_dir = tempfile.mkdtemp(prefix="deb_mock_test_")
yield temp_dir
shutil.rmtree(temp_dir, ignore_errors=True)
@pytest.fixture
def test_config(temp_dir):
"""Create a test configuration"""
config = Config(
chroot_name="test-chroot",
architecture="amd64",
suite="trixie",
chroot_dir=os.path.join(temp_dir, "chroots"),
cache_dir=os.path.join(temp_dir, "cache"),
work_dir=os.path.join(temp_dir, "work"),
enable_performance_monitoring=False, # Disable for tests
parallel_builds=1,
use_namespaces=False, # Disable for tests
mount_proc=False, # Disable for tests
mount_sys=False, # Disable for tests
mount_dev=False, # Disable for tests
mount_tmp=False, # Disable for tests
plugins=[],
plugin_conf={}
)
return config
@pytest.fixture
def mock_deb_mock(test_config):
"""Create a DebMock instance for testing"""
return DebMock(test_config)
@pytest.fixture
def sample_source_package(temp_dir):
"""Create a minimal Debian source package for testing"""
package_dir = os.path.join(temp_dir, "test-package")
os.makedirs(package_dir)
# Create debian/control
debian_dir = os.path.join(package_dir, "debian")
os.makedirs(debian_dir)
control_content = """Source: test-package
Section: devel
Priority: optional
Maintainer: Test User <test@example.com>
Build-Depends: debhelper-compat (= 13)
Package: test-package
Architecture: any
Depends: ${shlibs:Depends}, ${misc:Depends}
Description: Test package for deb-mock testing
This is a test package used for testing deb-mock functionality.
"""
with open(os.path.join(debian_dir, "control"), "w") as f:
f.write(control_content)
# Create debian/rules
rules_content = """#!/usr/bin/make -f
%:
dh $@
"""
with open(os.path.join(debian_dir, "rules"), "w") as f:
f.write(rules_content)
# Make rules executable
os.chmod(os.path.join(debian_dir, "rules"), 0o755)
# Create debian/changelog
changelog_content = """test-package (1.0-1) trixie; urgency=medium
* Initial release for testing
-- Test User <test@example.com> Mon, 19 Aug 2024 12:00:00 +0000
"""
with open(os.path.join(debian_dir, "changelog"), "w") as f:
f.write(changelog_content)
# Create simple Makefile
makefile_content = """all:
@echo "Building test package..."
clean:
@echo "Cleaning test package..."
"""
with open(os.path.join(package_dir, "Makefile"), "w") as f:
f.write(makefile_content)
return package_dir
@pytest.fixture
def mock_chroot_manager():
"""Mock chroot manager for testing"""
mock_manager = Mock()
mock_manager.chroot_exists.return_value = True
mock_manager.create_chroot.return_value = True
mock_manager.clean_chroot.return_value = True
mock_manager.get_chroot_path.return_value = "/tmp/test-chroot"
return mock_manager
@pytest.fixture
def mock_cache_manager():
"""Mock cache manager for testing"""
mock_manager = Mock()
mock_manager.restore_root_cache.return_value = True
mock_manager.create_root_cache.return_value = True
mock_manager.get_cache_path.return_value = "/tmp/test-cache"
return mock_manager
@pytest.fixture
def mock_sbuild_wrapper():
"""Mock sbuild wrapper for testing"""
mock_wrapper = Mock()
mock_wrapper.build_package.return_value = {
"success": True,
"output": "Build completed successfully",
"artifacts": ["test-package_1.0-1_amd64.deb"]
}
mock_wrapper.check_dependencies.return_value = {
"satisfied": True,
"missing": [],
"installed": ["build-essential", "debhelper"]
}
mock_wrapper.install_build_dependencies.return_value = True
return mock_wrapper
@pytest.fixture
def mock_plugin_manager():
"""Mock plugin manager for testing"""
mock_manager = Mock()
mock_manager.init_plugins.return_value = True
mock_manager.call_hooks.return_value = []
return mock_manager
@pytest.fixture
def mock_performance_monitor():
"""Mock performance monitor for testing"""
mock_monitor = Mock()
mock_monitor.monitor_operation.return_value.__enter__ = Mock(return_value="op_123")
mock_monitor.monitor_operation.return_value.__exit__ = Mock(return_value=None)
mock_monitor.create_build_profile.return_value = "profile_123"
mock_monitor.add_phase_metrics.return_value = None
mock_monitor.finalize_build_profile.return_value = Mock()
return mock_monitor
@pytest.fixture
def test_environment():
"""Set up test environment variables"""
original_env = os.environ.copy()
# Set test environment variables
test_env = {
"DEB_MOCK_TEST": "1",
"DEB_MOCK_DEBUG": "0",
"PATH": os.environ.get("PATH", ""),
"HOME": os.environ.get("HOME", "/tmp"),
"TMPDIR": "/tmp"
}
os.environ.update(test_env)
yield test_env
# Restore original environment
os.environ.clear()
os.environ.update(original_env)
@pytest.fixture
def isolated_filesystem(temp_dir):
"""Create an isolated filesystem for testing"""
original_cwd = os.getcwd()
# Change to test directory
os.chdir(temp_dir)
yield temp_dir
# Restore original working directory
os.chdir(original_cwd)
@pytest.fixture
def mock_system_commands():
"""Mock system commands for testing"""
with patch("subprocess.run") as mock_run:
mock_run.return_value.returncode = 0
mock_run.return_value.stdout = b"Mock output"
mock_run.return_value.stderr = b""
yield mock_run
@pytest.fixture
def mock_file_operations():
"""Mock file operations for testing"""
with patch("shutil.copy2") as mock_copy, \
patch("shutil.rmtree") as mock_rmtree, \
patch("os.makedirs") as mock_makedirs:
mock_copy.return_value = None
mock_rmtree.return_value = None
mock_makedirs.return_value = None
yield {
"copy": mock_copy,
"rmtree": mock_rmtree,
"makedirs": mock_makedirs
}
@pytest.fixture
def performance_test_config(temp_dir):
"""Configuration for performance testing"""
config = Config(
chroot_name="perf-test-chroot",
architecture="amd64",
suite="trixie",
chroot_dir=os.path.join(temp_dir, "chroots"),
cache_dir=os.path.join(temp_dir, "cache"),
work_dir=os.path.join(temp_dir, "work"),
enable_performance_monitoring=True,
performance_metrics_dir=os.path.join(temp_dir, "metrics"),
performance_auto_optimization=False,
parallel_builds=2,
plugins=[],
plugin_conf={}
)
return config
@pytest.fixture
def plugin_test_config(temp_dir):
"""Configuration for plugin testing"""
config = Config(
chroot_name="plugin-test-chroot",
architecture="amd64",
suite="trixie",
chroot_dir=os.path.join(temp_dir, "chroots"),
cache_dir=os.path.join(temp_dir, "cache"),
work_dir=os.path.join(temp_dir, "work"),
enable_performance_monitoring=False,
plugins=["test_plugin"],
plugin_conf={
"test_plugin": {
"enabled": True,
"config_option": "test_value"
}
},
plugin_dir=os.path.join(temp_dir, "plugins")
)
return config
# Test data fixtures
@pytest.fixture
def test_package_data():
"""Test data for package operations"""
return {
"name": "test-package",
"version": "1.0-1",
"architecture": "amd64",
"suite": "trixie",
"dependencies": ["build-essential", "debhelper"],
"build_depends": ["debhelper-compat (= 13)"]
}
@pytest.fixture
def test_build_result():
"""Test data for build results"""
return {
"success": True,
"package_name": "test-package",
"version": "1.0-1",
"architecture": "amd64",
"build_time": 45.23,
"artifacts": ["test-package_1.0-1_amd64.deb"],
"log_file": "/tmp/build.log",
"chroot_used": "test-chroot"
}
@pytest.fixture
def test_performance_metrics():
"""Test data for performance metrics"""
return {
"operation": "test_operation",
"duration": 12.34,
"cpu_percent": 75.5,
"memory_mb": 512.0,
"disk_io_read_mb": 25.6,
"disk_io_write_mb": 15.3,
"network_io_mb": 2.1
}