-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_environment_system.py
More file actions
272 lines (203 loc) · 7.4 KB
/
test_environment_system.py
File metadata and controls
272 lines (203 loc) · 7.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/usr/bin/env python3
"""
Test Environment System - Tests for system environment configuration.
Tests system-level requirements, filesystem, and OS configuration.
"""
import os
import sys
from pathlib import Path
import pytest
# Add src to path for imports
sys.path.insert(0, str(Path(__file__).parent.parent))
class TestSystemPlatform:
"""Tests for system platform requirements."""
@pytest.mark.fast
def test_platform_identified(self) -> None:
"""Test platform can be identified."""
import platform
system = platform.system()
assert system in ('Darwin', 'Linux', 'Windows')
@pytest.mark.fast
def test_architecture_identified(self) -> None:
"""Test architecture can be identified."""
import platform
arch = platform.machine()
assert arch is not None
assert len(arch) > 0
@pytest.mark.fast
def test_os_name_available(self) -> None:
"""Test OS name is available."""
assert os.name in ('posix', 'nt')
class TestFilesystem:
"""Tests for filesystem requirements."""
@pytest.mark.fast
def test_temp_directory_available(self) -> None:
"""Test temporary directory is available."""
import tempfile
temp_dir = tempfile.gettempdir()
assert Path(temp_dir).exists()
assert Path(temp_dir).is_dir()
@pytest.mark.fast
def test_temp_file_creation(self) -> None:
"""Test temporary files can be created."""
import tempfile
with tempfile.NamedTemporaryFile(delete=True) as f:
f.write(b"test")
assert Path(f.name).exists()
@pytest.mark.fast
def test_directory_creation(self, tmp_path: Path) -> None:
"""Test directories can be created."""
new_dir = tmp_path / "test_dir" / "nested"
new_dir.mkdir(parents=True, exist_ok=True)
assert new_dir.exists()
assert new_dir.is_dir()
@pytest.mark.fast
def test_file_read_write(self, tmp_path: Path) -> None:
"""Test files can be read and written."""
test_file = tmp_path / "test.txt"
# Write
test_file.write_text("Hello, World!")
# Read
content = test_file.read_text()
assert content == "Hello, World!"
@pytest.mark.fast
def test_binary_file_operations(self, tmp_path: Path) -> None:
"""Test binary file operations work."""
test_file = tmp_path / "test.bin"
data = bytes(range(256))
test_file.write_bytes(data)
read_data = test_file.read_bytes()
assert read_data == data
class TestSystemResources:
"""Tests for system resource availability."""
@pytest.mark.fast
def test_memory_available(self) -> None:
"""Test memory allocation works."""
# Allocate 1MB
data = bytearray(1024 * 1024)
assert len(data) == 1024 * 1024
@pytest.mark.fast
def test_file_descriptors_available(self, tmp_path: Path) -> None:
"""Test file descriptors can be opened."""
files = []
try:
# Try to open several files
for i in range(10):
f = open(tmp_path / f"test_{i}.txt", 'w')
files.append(f)
assert len(files) == 10
finally:
for f in files:
f.close()
@pytest.mark.fast
def test_cpu_count_available(self) -> None:
"""Test CPU count can be determined."""
cpu_count = os.cpu_count()
assert cpu_count is not None
assert cpu_count >= 1
class TestSystemProcesses:
"""Tests for process management."""
@pytest.mark.fast
def test_subprocess_execution(self) -> None:
"""Test subprocess execution works."""
import subprocess # nosec B404 -- subprocess calls with controlled/trusted input
result = subprocess.run( # nosec B603 -- subprocess calls with controlled/trusted input
[sys.executable, "-c", "print('hello')"],
capture_output=True,
text=True,
timeout=10
)
assert result.returncode == 0
assert "hello" in result.stdout
@pytest.mark.fast
def test_process_id_available(self) -> None:
"""Test process ID is available."""
pid = os.getpid()
assert pid is not None
assert pid > 0
@pytest.mark.fast
def test_environment_variables(self) -> None:
"""Test environment variables work."""
# Set and get
os.environ["GNN_TEST_VAR"] = "test_value"
assert os.environ.get("GNN_TEST_VAR") == "test_value"
# Clean up
del os.environ["GNN_TEST_VAR"]
class TestSystemTime:
"""Tests for time and date functionality."""
@pytest.mark.fast
def test_time_available(self) -> None:
"""Test time functions work."""
import time
now = time.time()
assert now > 0
@pytest.mark.fast
def test_datetime_available(self) -> None:
"""Test datetime functions work."""
from datetime import datetime
now = datetime.now()
assert now.year >= 2024
@pytest.mark.fast
def test_timezone_available(self) -> None:
"""Test timezone functionality works."""
from datetime import datetime, timezone
utc_now = datetime.now(timezone.utc)
assert utc_now.tzinfo is not None
class TestSystemPath:
"""Tests for path operations."""
@pytest.mark.fast
def test_path_separator(self) -> None:
"""Test path separator is correct."""
sep = os.sep
if os.name == 'nt':
assert sep == '\\'
else:
assert sep == '/'
@pytest.mark.fast
def test_absolute_path_works(self) -> None:
"""Test absolute path resolution works."""
relative = Path(".")
absolute = relative.resolve()
assert absolute.is_absolute()
@pytest.mark.fast
def test_path_normalization(self) -> None:
"""Test path normalization works."""
messy_path = Path("a/b/../c/./d")
clean_parts = [p for p in messy_path.parts if p not in ('.', '..')]
# Should be able to normalize
assert isinstance(clean_parts, list)
class TestSystemLocale:
"""Tests for locale and encoding."""
@pytest.mark.fast
def test_utf8_encoding(self) -> None:
"""Test UTF-8 encoding works."""
text = "Hello, 世界! 🌍"
encoded = text.encode('utf-8')
decoded = encoded.decode('utf-8')
assert decoded == text
@pytest.mark.fast
def test_filesystem_encoding(self) -> None:
"""Test filesystem encoding is accessible."""
encoding = sys.getfilesystemencoding()
assert encoding is not None
assert encoding.lower() in ('utf-8', 'utf8', 'ascii', 'latin-1', 'mbcs')
class TestSystemNetwork:
"""Tests for basic network functionality."""
@pytest.mark.fast
def test_socket_module_available(self) -> None:
"""Test socket module is available."""
import socket
# Get hostname
hostname = socket.gethostname()
assert hostname is not None
assert len(hostname) > 0
@pytest.mark.fast
def test_localhost_resolvable(self) -> None:
"""Test localhost is resolvable."""
import socket
try:
addr = socket.gethostbyname('localhost')
assert addr in ('127.0.0.1', '::1') or addr.startswith('127.')
except socket.gaierror:
# May not resolve on all systems
pass