|
| 1 | +"""Test Windows compatibility fixes for pytest-postgresql.""" |
| 2 | + |
| 3 | +import platform |
| 4 | +import pytest |
| 5 | +from unittest.mock import Mock, patch, MagicMock |
| 6 | +import subprocess |
| 7 | +import signal |
| 8 | + |
| 9 | +from pytest_postgresql.executor import PostgreSQLExecutor |
| 10 | + |
| 11 | + |
| 12 | +class TestWindowsCompatibility: |
| 13 | + """Test Windows-specific functionality.""" |
| 14 | + |
| 15 | + def test_get_base_command_windows(self): |
| 16 | + """Test that Windows base command doesn't use single quotes.""" |
| 17 | + executor = PostgreSQLExecutor( |
| 18 | + executable="/path/to/pg_ctl", |
| 19 | + host="localhost", |
| 20 | + port=5432, |
| 21 | + datadir="/tmp/data", |
| 22 | + unixsocketdir="/tmp/socket", |
| 23 | + logfile="/tmp/log", |
| 24 | + startparams="-w", |
| 25 | + dbname="test", |
| 26 | + ) |
| 27 | + |
| 28 | + with patch('platform.system', return_value='Windows'): |
| 29 | + command = executor._get_base_command() |
| 30 | + # Windows command should not have single quotes around stderr |
| 31 | + assert "log_destination=stderr" in command |
| 32 | + assert "log_destination='stderr'" not in command |
| 33 | + assert "unix_socket_directories={unixsocketdir}" in command |
| 34 | + assert "unix_socket_directories='{unixsocketdir}'" not in command |
| 35 | + |
| 36 | + def test_get_base_command_unix(self): |
| 37 | + """Test that Unix base command uses single quotes.""" |
| 38 | + executor = PostgreSQLExecutor( |
| 39 | + executable="/path/to/pg_ctl", |
| 40 | + host="localhost", |
| 41 | + port=5432, |
| 42 | + datadir="/tmp/data", |
| 43 | + unixsocketdir="/tmp/socket", |
| 44 | + logfile="/tmp/log", |
| 45 | + startparams="-w", |
| 46 | + dbname="test", |
| 47 | + ) |
| 48 | + |
| 49 | + with patch('platform.system', return_value='Linux'): |
| 50 | + command = executor._get_base_command() |
| 51 | + # Unix command should have single quotes around stderr |
| 52 | + assert "log_destination='stderr'" in command |
| 53 | + assert "unix_socket_directories='{unixsocketdir}'" in command |
| 54 | + |
| 55 | + def test_windows_terminate_process(self): |
| 56 | + """Test Windows process termination.""" |
| 57 | + executor = PostgreSQLExecutor( |
| 58 | + executable="/path/to/pg_ctl", |
| 59 | + host="localhost", |
| 60 | + port=5432, |
| 61 | + datadir="/tmp/data", |
| 62 | + unixsocketdir="/tmp/socket", |
| 63 | + logfile="/tmp/log", |
| 64 | + startparams="-w", |
| 65 | + dbname="test", |
| 66 | + ) |
| 67 | + |
| 68 | + # Mock process |
| 69 | + mock_process = MagicMock() |
| 70 | + executor.process = mock_process |
| 71 | + |
| 72 | + with patch('platform.system', return_value='Windows'): |
| 73 | + executor._windows_terminate_process() |
| 74 | + |
| 75 | + # Should call terminate first |
| 76 | + mock_process.terminate.assert_called_once() |
| 77 | + mock_process.wait.assert_called() |
| 78 | + |
| 79 | + def test_windows_terminate_process_force_kill(self): |
| 80 | + """Test Windows process termination with force kill on timeout.""" |
| 81 | + executor = PostgreSQLExecutor( |
| 82 | + executable="/path/to/pg_ctl", |
| 83 | + host="localhost", |
| 84 | + port=5432, |
| 85 | + datadir="/tmp/data", |
| 86 | + unixsocketdir="/tmp/socket", |
| 87 | + logfile="/tmp/log", |
| 88 | + startparams="-w", |
| 89 | + dbname="test", |
| 90 | + ) |
| 91 | + |
| 92 | + # Mock process that times out |
| 93 | + mock_process = MagicMock() |
| 94 | + mock_process.wait.side_effect = [subprocess.TimeoutExpired(cmd="test", timeout=5), None] |
| 95 | + executor.process = mock_process |
| 96 | + |
| 97 | + with patch('platform.system', return_value='Windows'): |
| 98 | + executor._windows_terminate_process() |
| 99 | + |
| 100 | + # Should call terminate, wait (timeout), then kill, then wait again |
| 101 | + mock_process.terminate.assert_called_once() |
| 102 | + mock_process.kill.assert_called_once() |
| 103 | + assert mock_process.wait.call_count == 2 |
| 104 | + |
| 105 | + def test_stop_method_windows(self): |
| 106 | + """Test stop method on Windows.""" |
| 107 | + executor = PostgreSQLExecutor( |
| 108 | + executable="/path/to/pg_ctl", |
| 109 | + host="localhost", |
| 110 | + port=5432, |
| 111 | + datadir="/tmp/data", |
| 112 | + unixsocketdir="/tmp/socket", |
| 113 | + logfile="/tmp/log", |
| 114 | + startparams="-w", |
| 115 | + dbname="test", |
| 116 | + ) |
| 117 | + |
| 118 | + # Mock subprocess and process |
| 119 | + with patch('subprocess.check_output') as mock_subprocess, \ |
| 120 | + patch('platform.system', return_value='Windows'), \ |
| 121 | + patch.object(executor, '_windows_terminate_process') as mock_terminate: |
| 122 | + |
| 123 | + result = executor.stop() |
| 124 | + |
| 125 | + # Should call pg_ctl stop and Windows terminate |
| 126 | + mock_subprocess.assert_called_once() |
| 127 | + mock_terminate.assert_called_once_with(None) |
| 128 | + assert result is executor |
| 129 | + |
| 130 | + def test_stop_method_unix(self): |
| 131 | + """Test stop method on Unix systems.""" |
| 132 | + executor = PostgreSQLExecutor( |
| 133 | + executable="/path/to/pg_ctl", |
| 134 | + host="localhost", |
| 135 | + port=5432, |
| 136 | + datadir="/tmp/data", |
| 137 | + unixsocketdir="/tmp/socket", |
| 138 | + logfile="/tmp/log", |
| 139 | + startparams="-w", |
| 140 | + dbname="test", |
| 141 | + ) |
| 142 | + |
| 143 | + # Mock subprocess and super().stop |
| 144 | + with patch('subprocess.check_output') as mock_subprocess, \ |
| 145 | + patch('platform.system', return_value='Linux'), \ |
| 146 | + patch('pytest_postgresql.executor.TCPExecutor.stop') as mock_super_stop: |
| 147 | + |
| 148 | + mock_super_stop.return_value = executor |
| 149 | + result = executor.stop() |
| 150 | + |
| 151 | + # Should call pg_ctl stop and parent class stop |
| 152 | + mock_subprocess.assert_called_once() |
| 153 | + mock_super_stop.assert_called_once_with(None, None) |
| 154 | + assert result is executor |
| 155 | + |
| 156 | + def test_stop_method_fallback_on_killpg_error(self): |
| 157 | + """Test stop method falls back to Windows termination on killpg AttributeError.""" |
| 158 | + executor = PostgreSQLExecutor( |
| 159 | + executable="/path/to/pg_ctl", |
| 160 | + host="localhost", |
| 161 | + port=5432, |
| 162 | + datadir="/tmp/data", |
| 163 | + unixsocketdir="/tmp/socket", |
| 164 | + logfile="/tmp/log", |
| 165 | + startparams="-w", |
| 166 | + dbname="test", |
| 167 | + ) |
| 168 | + |
| 169 | + # Mock subprocess and super().stop to raise AttributeError |
| 170 | + with patch('subprocess.check_output') as mock_subprocess, \ |
| 171 | + patch('platform.system', return_value='Linux'), \ |
| 172 | + patch('pytest_postgresql.executor.TCPExecutor.stop', |
| 173 | + side_effect=AttributeError("module 'os' has no attribute 'killpg'")), \ |
| 174 | + patch.object(executor, '_windows_terminate_process') as mock_terminate: |
| 175 | + |
| 176 | + result = executor.stop() |
| 177 | + |
| 178 | + # Should call pg_ctl stop, fail on super().stop, then use Windows terminate |
| 179 | + mock_subprocess.assert_called_once() |
| 180 | + mock_terminate.assert_called_once_with(None) |
| 181 | + assert result is executor |
| 182 | + |
| 183 | + def test_command_formatting_windows(self): |
| 184 | + """Test that command is properly formatted for Windows.""" |
| 185 | + with patch('platform.system', return_value='Windows'): |
| 186 | + executor = PostgreSQLExecutor( |
| 187 | + executable="C:/Program Files/PostgreSQL/bin/pg_ctl.exe", |
| 188 | + host="localhost", |
| 189 | + port=5555, |
| 190 | + datadir="C:/temp/data", |
| 191 | + unixsocketdir="C:/temp/socket", |
| 192 | + logfile="C:/temp/log.txt", |
| 193 | + startparams="-w -s", |
| 194 | + dbname="testdb", |
| 195 | + postgres_options="-c shared_preload_libraries=test" |
| 196 | + ) |
| 197 | + |
| 198 | + # The command should be properly formatted without quotes around stderr |
| 199 | + expected_parts = [ |
| 200 | + "C:/Program Files/PostgreSQL/bin/pg_ctl.exe start", |
| 201 | + '-D "C:/temp/data"', |
| 202 | + "-o \"-F -p 5555 -c log_destination=stderr", |
| 203 | + "-c logging_collector=off", |
| 204 | + "-c unix_socket_directories=C:/temp/socket", |
| 205 | + "-c shared_preload_libraries=test\"", |
| 206 | + '-l "C:/temp/log.txt"', |
| 207 | + "-w -s" |
| 208 | + ] |
| 209 | + |
| 210 | + # Check if all expected parts are in the command |
| 211 | + command = executor.command |
| 212 | + for part in expected_parts: |
| 213 | + assert part in command, f"Expected '{part}' in command: {command}" |
0 commit comments