-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathhelm_deployment_test.py
More file actions
380 lines (337 loc) · 12.6 KB
/
helm_deployment_test.py
File metadata and controls
380 lines (337 loc) · 12.6 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/usr/bin/env python3
import unittest
import subprocess
import time
import json
import base64
import requests
import atexit
from contextlib import contextmanager
"""
End-to-end integration tests for the BBOT Server Helm chart running on minikube.
Run it like so:
# all tests
uv run python test_helm_deployment.py
# specific test
uv run python -m unittest test_helm_deployment.TestHelmDeployment.test_swagger_ui
To bring up the test environment manually, run:
minikube start
docker build -t blacklanternsecurity/bbot-server:test .
minikube image load blacklanternsecurity/bbot-server:test
helm dependency update helm/
helm install bbot helm/ --set image.tag=test --set image.pullPolicy=Never
"""
class TestHelmDeployment(unittest.TestCase):
release_name = "bbot"
ctx = ["--context", "minikube"]
kube_ctx = ["--kube-context", "minikube"]
_cleanup_registered = False
@classmethod
def run_command(cls, command, timeout=120, **kwargs):
print(f"Running: {' '.join(command)}")
if "check" not in kwargs:
kwargs["check"] = True
return subprocess.run(command, text=True, timeout=timeout, **kwargs)
@classmethod
def kubectl(cls, *command, **kwargs):
return cls.run_command(["kubectl", *cls.ctx, *command], **kwargs)
@classmethod
def helm(cls, *command, **kwargs):
return cls.run_command(["helm", *cls.kube_ctx, *command], **kwargs)
@classmethod
@contextmanager
def port_forward(cls, service, local_port, remote_port):
"""Context manager for kubectl port-forward"""
process = subprocess.Popen(
["kubectl", *cls.ctx, "port-forward", f"svc/{service}", f"{local_port}:{remote_port}"],
)
try:
# Wait for port-forward to establish
time.sleep(3)
yield f"http://localhost:{local_port}"
finally:
process.terminate()
try:
process.wait(timeout=5)
except subprocess.TimeoutExpired:
process.kill()
process.wait()
@classmethod
def cleanup_resources(cls):
"""Clean up Helm charts and PVCs - ALWAYS runs"""
print("\nCLEANING UP RESOURCES...")
print("-" * 40)
# Uninstall Helm chart
print("Uninstalling Helm chart...")
cls.helm("uninstall", cls.release_name, check=False)
# Delete any existing PVCs to ensure clean state
print("Deleting any existing PVCs...")
cls.kubectl("delete", "pvc", "--all", check=False)
# Delete leftover secrets with keep policy
for suffix in ("mongodb", "redis", "api-key"):
cls.kubectl("delete", "secret", f"{cls.release_name}-{suffix}", check=False)
print("Cleanup completed")
@classmethod
def register_cleanup(cls):
"""Register cleanup to run on exit"""
if not cls._cleanup_registered:
atexit.register(cls.cleanup_resources)
cls._cleanup_registered = True
@classmethod
def dump_cluster_debug_info(cls):
"""Print pods, logs, services, and events for debugging"""
print("\n" + "=" * 80)
print("ALL PODS STATUS:")
print("-" * 40)
try:
result = cls.kubectl("get", "pods", "-o", "wide", capture_output=True, check=False)
print(result.stdout)
except Exception as e:
print(f"Failed to get pods: {e}")
print("\nPOD LOGS:")
print("-" * 40)
try:
result = cls.kubectl(
"get",
"pods",
"-o",
"jsonpath='{.items[*].metadata.name}'",
capture_output=True,
check=False,
)
if result.returncode == 0 and result.stdout.strip():
pod_names = result.stdout.strip().strip("'").split()
for pod_name in pod_names:
print(f"\nLogs for pod: {pod_name}")
print("-" * 30)
try:
log_result = cls.kubectl(
"logs",
pod_name,
"--all-containers",
"--tail=200",
capture_output=True,
check=False,
)
if log_result.returncode == 0:
print(log_result.stdout)
else:
print(f"Failed to get logs: {log_result.stderr}")
except Exception as e:
print(f"Error getting logs for {pod_name}: {e}")
else:
print("Failed to list pods or no pods found")
except Exception as e:
print(f"Failed to get pod names: {e}")
print("\nSERVICES STATUS:")
print("-" * 40)
try:
result = cls.kubectl("get", "services", "-o", "wide", capture_output=True, check=False)
print(result.stdout)
except Exception as e:
print(f"Failed to get services: {e}")
print("\nRECENT EVENTS:")
print("-" * 40)
try:
result = cls.kubectl(
"get",
"events",
"--sort-by=.metadata.creationTimestamp",
capture_output=True,
check=False,
)
print(result.stdout)
except Exception as e:
print(f"Failed to get events: {e}")
print("\n" + "=" * 80)
@classmethod
def setUpClass(cls):
"""Build image, deploy to minikube, wait for readiness"""
# Ensure minikube is running
print("Checking if minikube is running...")
result = subprocess.run(
["minikube", "status"],
capture_output=True,
text=True,
check=False,
)
if result.returncode != 0 or "Running" not in result.stdout:
print("Starting minikube...")
subprocess.run(["minikube", "start"], check=True)
print("Minikube started")
else:
print("Minikube is already running")
# Register cleanup to ALWAYS run, even on test failure
cls.register_cleanup()
# Clean up any previous deployments
cls.cleanup_resources()
# Build image locally and load into minikube
print("Building Docker image...")
cls.run_command(
[
"docker",
"build",
"-t",
"blacklanternsecurity/bbot-server:test",
".",
],
timeout=300,
)
print("Loading image into minikube...")
cls.run_command(
[
"minikube",
"image",
"load",
"blacklanternsecurity/bbot-server:test",
],
timeout=120,
)
print("Image loaded successfully")
# Add Helm repositories and update dependencies
print("Adding Helm repositories...")
cls.helm("repo", "add", "bitnami", "https://charts.bitnami.com/bitnami", timeout=30)
print("Building Helm dependencies (will fail if Chart.lock is out of sync)...")
cls.helm("dependency", "build", "helm/", timeout=60)
# Deploy the helm chart
print("Deploying helm chart...")
cls.helm(
"install",
cls.release_name,
"helm/",
"--set",
"image.tag=test",
"--set",
"image.pullPolicy=Never",
timeout=60,
)
# Wait for server pod to be ready
print("Waiting for server pod to be ready...")
result = cls.kubectl(
"wait",
"--for=condition=ready",
"pod",
"-l",
"app=bbot-server",
"--timeout=90s",
check=False,
capture_output=True,
timeout=100,
)
if result.returncode == 0:
print("Server pod is ready")
return
print("\n" + "=" * 80)
print("TIMEOUT: Server pod failed to become ready")
print("=" * 80)
cls.dump_cluster_debug_info()
raise RuntimeError("Timed out waiting for server pod to be ready")
def test_swagger_ui(self):
"""Test that the swagger-ui loads properly at /v1/docs"""
with self.port_forward(f"{self.release_name}-server", 8807, 8807) as base_url:
response = requests.get(f"{base_url}/v1/docs", timeout=10)
print(f"GET /v1/docs status: {response.status_code}")
print(f"Content length: {len(response.text)}")
self.assertEqual(response.status_code, 200, "Swagger UI should return 200")
self.assertIn("swagger-ui", response.text.lower(), "Response should contain swagger-ui")
def test_ingest_and_query_assets(self):
"""Test ingesting events from evilcorp.json.gz and querying assets via bbctl"""
# Get the server pod name
result = self.kubectl(
"get",
"pods",
"-l",
"app=bbot-server",
"-o",
"jsonpath={.items[0].metadata.name}",
capture_output=True,
)
pod_name = result.stdout.strip()
self.assertTrue(pod_name, "Should find a server pod")
print(f"Server pod: {pod_name}")
# Get the API key from the kubernetes secret
result = self.kubectl(
"get",
"secret",
f"{self.release_name}-api-key",
"-o",
"jsonpath={.data.api-key}",
capture_output=True,
)
api_key = base64.b64decode(result.stdout).decode()
self.assertTrue(api_key, "Should retrieve API key from secret")
# Write a bbctl config file inside the pod
server_url = f"http://{self.release_name}-server:8807/v1/"
config_content = f'url: "{server_url}"\napi_keys:\n - "{api_key}"'
self.kubectl(
"exec",
pod_name,
"--",
"sh",
"-c",
f"printf '%s\\n' '{config_content}' > /tmp/bbctl.yaml",
)
bbctl = "bbctl --no-color --config /tmp/bbctl.yaml"
# Copy test data into the pod
self.kubectl("cp", "tests/evilcorp.json.gz", f"{pod_name}:/tmp/evilcorp.json.gz")
# Ingest events using bbctl
print("Ingesting events...")
result = self.kubectl(
"exec",
pod_name,
"--",
"sh",
"-c",
f"gunzip -c /tmp/evilcorp.json.gz | {bbctl} event ingest",
capture_output=True,
timeout=120,
)
print(f"Ingest stdout: {result.stdout}")
print(f"Ingest stderr: {result.stderr}")
self.assertEqual(result.returncode, 0, f"Event ingest failed: {result.stderr}")
# Wait for the worker to process events into assets
print("Waiting for worker to process events into assets...")
assets = []
for i in range(30):
result = self.kubectl(
"exec",
pod_name,
"--",
"sh",
"-c",
f"{bbctl} asset list --json",
capture_output=True,
check=False,
timeout=30,
)
if result.returncode == 0 and result.stdout.strip():
assets = [json.loads(line) for line in result.stdout.strip().splitlines() if line.strip()]
if assets:
break
print(f" Attempt {i + 1}/30: {len(assets)} assets so far...")
time.sleep(2)
print(f"Found {len(assets)} assets")
self.assertGreater(len(assets), 0, "Should have ingested some assets from evilcorp.json.gz")
def tearDown(self):
"""If a test fails, dump cluster debug info"""
failed = False
outcome = getattr(self, "_outcome", None)
try:
if outcome is not None:
if hasattr(outcome, "errors"):
errors = outcome.errors
elif hasattr(outcome, "result"):
result = outcome.result
errors = list(getattr(result, "errors", [])) + list(getattr(result, "failures", []))
else:
errors = []
failed = any(err for _, err in errors)
except Exception:
failed = False
if failed:
print("\n" + "=" * 80)
print(f"Test failed: {self.id()} - dumping cluster debug info")
print("=" * 80)
self.__class__.dump_cluster_debug_info()
if __name__ == "__main__":
unittest.main()