Repository navigation
Expand file tree
/
Copy pathtest_basic.py
More file actions
235 lines (184 loc) · 7.17 KB
/
Copy pathtest_basic.py
File metadata and controls
235 lines (184 loc) · 7.17 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
#!/usr/bin/env python3
"""
Basic test to verify the BSub Wizard components work correctly
"""
import sys
from pathlib import Path
# Add wizard to path
sys.path.insert(0, str(Path(__file__).parent))
from wizard.models.job_config import JobConfiguration, JobType, GPUConfiguration
from wizard.models.cluster_info import ClusterConfiguration
from wizard.utils.command_builder import BsubCommandBuilder
from wizard.utils.validators import JobValidator
def test_job_configuration():
"""Test job configuration creation and validation"""
print("Testing Job Configuration...")
# Test CPU job
cpu_job = JobConfiguration(
job_type=JobType.CPU,
job_name="test_cpu_job",
command="python script.py",
slots=4,
queue="local",
runtime_limit="2:00"
)
is_valid, errors = cpu_job.is_valid()
assert is_valid, f"CPU job validation failed: {errors}"
print("✓ CPU job configuration valid")
# Test GPU job
gpu_job = JobConfiguration(
job_type=JobType.GPU,
job_name="test_gpu_job",
command="python train.py",
slots=12,
queue="gpu_a100",
runtime_limit="4:00",
gpu_config=GPUConfiguration(
gpu_type="NVIDIAA100_SXM4_80GB",
num_gpus=1
)
)
is_valid, errors = gpu_job.is_valid()
assert is_valid, f"GPU job validation failed: {errors}"
print("✓ GPU job configuration valid")
def test_cluster_configuration():
"""Test cluster configuration loading"""
print("Testing Cluster Configuration...")
cluster = ClusterConfiguration()
# Test queue access
assert len(cluster.queues) > 0, "No queues configured"
assert "local" in cluster.queues, "Local queue not found"
assert "gpu_a100" in cluster.queues, "GPU A100 queue not found"
print("✓ Cluster queues loaded")
# Test GPU access
assert len(cluster.gpus) > 0, "No GPUs configured"
assert "NVIDIAA100_SXM4_80GB" in cluster.gpus, "A100 GPU not found"
print("✓ GPU configurations loaded")
# Test queue filtering
cpu_queues = cluster.get_queues_for_job_type("cpu")
gpu_queues = cluster.get_queues_for_job_type("gpu")
assert len(cpu_queues) > 0, "No CPU queues found"
assert len(gpu_queues) > 0, "No GPU queues found"
print("✓ Queue filtering works")
def test_command_builder():
"""Test bsub command generation"""
print("Testing Command Builder...")
cluster = ClusterConfiguration()
builder = BsubCommandBuilder(cluster.general_config)
# Test CPU command
cpu_job = JobConfiguration(
job_type=JobType.CPU,
job_name="test_cpu",
command="echo hello",
slots=4,
queue="local",
runtime_limit="1:00",
output_file="/dev/null"
)
cpu_command = builder.build_command(cpu_job)
expected_parts = ["bsub", "-J", "test_cpu", "-n", "4", "-q", "local", "-W", "1:00", "-o", "/dev/null", "echo hello"]
for part in expected_parts:
assert part in cpu_command, f"Missing '{part}' in CPU command: {cpu_command}"
print("✓ CPU command generation works")
# Test GPU command
gpu_job = JobConfiguration(
job_type=JobType.GPU,
job_name="test_gpu",
command="python train.py",
slots=12,
queue="gpu_a100",
runtime_limit="2:00",
gpu_config=GPUConfiguration(
gpu_type="NVIDIAA100_SXM4_80GB",
num_gpus=1
),
output_file="/dev/null"
)
gpu_command = builder.build_command(gpu_job)
expected_gpu_parts = ["bsub", "-J", "test_gpu", "-n", "12", "-q", "gpu_a100", "-gpu", "num=1", "-W", "2:00"]
for part in expected_gpu_parts:
assert part in gpu_command, f"Missing '{part}' in GPU command: {gpu_command}"
print("✓ GPU command generation works")
# Test cost estimation
cost = builder.estimate_cost(gpu_job)
assert cost > 0, f"Cost estimation failed: {cost}"
print(f"✓ Cost estimation works: ${cost:.2f}")
def test_validators():
"""Test input validation"""
print("Testing Validators...")
# Test job name validation
valid, error = JobValidator.validate_job_name("valid_job_name")
assert valid, f"Valid job name rejected: {error}"
valid, error = JobValidator.validate_job_name("invalid job name") # has space
assert not valid, "Invalid job name (with space) was accepted"
valid, error = JobValidator.validate_job_name("spark_job") # reserved word
assert not valid, "Invalid job name (reserved word) was accepted"
print("✓ Job name validation works")
# Test time format validation
valid, error = JobValidator.validate_time_format("1:30")
assert valid, f"Valid time format rejected: {error}"
valid, error = JobValidator.validate_time_format("90")
assert valid, f"Valid time format (minutes) rejected: {error}"
valid, error = JobValidator.validate_time_format("invalid")
assert not valid, "Invalid time format was accepted"
print("✓ Time format validation works")
# Test slots validation
valid, error = JobValidator.validate_slots(8)
assert valid, f"Valid slot count rejected: {error}"
valid, error = JobValidator.validate_slots(0)
assert not valid, "Invalid slot count (0) was accepted"
valid, error = JobValidator.validate_slots(100)
assert not valid, "Invalid slot count (too high) was accepted"
print("✓ Slots validation works")
def main():
"""Run all tests"""
print("=" * 60)
print("BSub Wizard - Basic Component Tests")
print("=" * 60)
try:
test_job_configuration()
print()
test_cluster_configuration()
print()
test_command_builder()
print()
test_validators()
print()
print("=" * 60)
print("🎉 All tests passed! BSub Wizard components are working correctly.")
print("=" * 60)
print()
print("To run the full application:")
print(" python main.py")
print()
print("Example commands that would be generated:")
# Show example commands
cluster = ClusterConfiguration()
builder = BsubCommandBuilder(cluster.general_config)
cpu_example = JobConfiguration(
job_type=JobType.CPU,
job_name="my_analysis",
command="python analyze_data.py",
slots=8,
queue="local",
runtime_limit="4:00"
)
print(f"CPU Job: {builder.build_command(cpu_example)}")
gpu_example = JobConfiguration(
job_type=JobType.GPU,
job_name="ml_training",
command="python train_model.py",
slots=12,
queue="gpu_a100",
runtime_limit="12:00",
gpu_config=GPUConfiguration(gpu_type="NVIDIAA100_SXM4_80GB", num_gpus=1)
)
print(f"GPU Job: {builder.build_command(gpu_example)}")
except Exception as e:
print(f"❌ Test failed: {e}")
import traceback
traceback.print_exc()
return 1
return 0
if __name__ == "__main__":
sys.exit(main())