-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_github_k8s_bridge.py
More file actions
672 lines (580 loc) · 24.1 KB
/
Copy pathtest_github_k8s_bridge.py
File metadata and controls
672 lines (580 loc) · 24.1 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
"""Unit tests for GitHub ↔ Kubernetes Bridge Forge."""
from datetime import UTC, datetime, timedelta
import pytest
from app.forges.base import ForgeConfig, ForgeMode, WebhookPayload
from app.forges.github_k8s_bridge import (
BuildDeploymentCorrelation,
BuildToDeploymentStatus,
GitHubKubernetesBridge,
get_github_kubernetes_bridge,
)
class TestBuildToDeploymentStatus:
"""Tests for BuildToDeploymentStatus enum."""
def test_all_statuses_exist(self):
"""Test all expected statuses exist."""
assert BuildToDeploymentStatus.BUILDING == "building"
assert BuildToDeploymentStatus.BUILD_PASSED == "build_passed"
assert BuildToDeploymentStatus.BUILD_FAILED == "build_failed"
assert BuildToDeploymentStatus.DEPLOYING == "deploying"
assert BuildToDeploymentStatus.DEPLOYED == "deployed"
assert BuildToDeploymentStatus.DEPLOYMENT_FAILED == "deployment_failed"
assert BuildToDeploymentStatus.HEALTHY == "healthy"
assert BuildToDeploymentStatus.DEGRADED == "degraded"
assert BuildToDeploymentStatus.UNKNOWN == "unknown"
class TestBuildDeploymentCorrelation:
"""Tests for BuildDeploymentCorrelation dataclass."""
def test_correlation_defaults(self):
"""Test default correlation values."""
correlation = BuildDeploymentCorrelation(
build_id="123",
workflow_name="CI/CD",
repo="user-service",
branch="main",
commit_sha="abc123",
)
assert correlation.build_status == "pending"
assert correlation.deployment_status == "pending"
assert correlation.namespace == "default"
assert correlation.current_replicas == 0
assert correlation.desired_replicas == 1
assert correlation.pod_statuses == []
assert correlation.pipeline_status == BuildToDeploymentStatus.UNKNOWN
def test_compute_status_building(self):
"""Test status computation when building."""
correlation = BuildDeploymentCorrelation(
build_id="123",
workflow_name="CI/CD",
repo="user-service",
branch="main",
commit_sha="abc123",
build_status="pending",
)
assert correlation.compute_status() == BuildToDeploymentStatus.BUILDING
def test_compute_status_build_failed(self):
"""Test status computation when build failed."""
correlation = BuildDeploymentCorrelation(
build_id="123",
workflow_name="CI/CD",
repo="user-service",
branch="main",
commit_sha="abc123",
build_status="failure",
)
assert correlation.compute_status() == BuildToDeploymentStatus.BUILD_FAILED
def test_compute_status_build_passed(self):
"""Test status computation when build passed but not deployed."""
correlation = BuildDeploymentCorrelation(
build_id="123",
workflow_name="CI/CD",
repo="user-service",
branch="main",
commit_sha="abc123",
build_status="success",
deployment_status="pending",
)
assert correlation.compute_status() == BuildToDeploymentStatus.BUILD_PASSED
def test_compute_status_deploying(self):
"""Test status computation when deploying."""
correlation = BuildDeploymentCorrelation(
build_id="123",
workflow_name="CI/CD",
repo="user-service",
branch="main",
commit_sha="abc123",
build_status="success",
deployment_status="progressing",
)
assert correlation.compute_status() == BuildToDeploymentStatus.DEPLOYING
def test_compute_status_deployment_failed(self):
"""Test status computation when deployment failed."""
correlation = BuildDeploymentCorrelation(
build_id="123",
workflow_name="CI/CD",
repo="user-service",
branch="main",
commit_sha="abc123",
build_status="success",
deployment_status="failed",
)
assert correlation.compute_status() == BuildToDeploymentStatus.DEPLOYMENT_FAILED
def test_compute_status_healthy(self):
"""Test status computation when deployment is healthy."""
correlation = BuildDeploymentCorrelation(
build_id="123",
workflow_name="CI/CD",
repo="user-service",
branch="main",
commit_sha="abc123",
build_status="success",
deployment_status="available",
pod_statuses=["Running", "Running", "Running"],
)
assert correlation.compute_status() == BuildToDeploymentStatus.HEALTHY
def test_compute_status_degraded(self):
"""Test status computation when pods are not all running."""
correlation = BuildDeploymentCorrelation(
build_id="123",
workflow_name="CI/CD",
repo="user-service",
branch="main",
commit_sha="abc123",
build_status="success",
deployment_status="available",
pod_statuses=["Running", "CrashLoopBackOff"],
)
assert correlation.compute_status() == BuildToDeploymentStatus.DEGRADED
def test_compute_duration_complete(self):
"""Test duration computation with complete lifecycle."""
start = datetime.now(UTC) - timedelta(minutes=5)
end = datetime.now(UTC)
correlation = BuildDeploymentCorrelation(
build_id="123",
workflow_name="CI/CD",
repo="user-service",
branch="main",
commit_sha="abc123",
build_started_at=start,
deployment_completed_at=end,
)
duration = correlation.compute_duration()
assert duration is not None
assert 299 < duration < 301 # ~5 minutes
def test_compute_duration_incomplete(self):
"""Test duration computation with incomplete lifecycle."""
correlation = BuildDeploymentCorrelation(
build_id="123",
workflow_name="CI/CD",
repo="user-service",
branch="main",
commit_sha="abc123",
build_started_at=datetime.now(UTC),
)
assert correlation.compute_duration() is None
def test_to_report(self):
"""Test report generation."""
start = datetime.now(UTC) - timedelta(minutes=5)
build_end = datetime.now(UTC) - timedelta(minutes=3)
deploy_start = datetime.now(UTC) - timedelta(minutes=2)
deploy_end = datetime.now(UTC)
correlation = BuildDeploymentCorrelation(
build_id="123",
workflow_name="CI/CD Pipeline",
repo="user-service",
branch="main",
commit_sha="abc123def",
image_tag="user-service:abc123d",
build_status="success",
build_started_at=start,
build_completed_at=build_end,
deployment_name="user-service",
namespace="production",
deployment_status="available",
current_replicas=3,
desired_replicas=3,
pod_statuses=["Running", "Running", "Running"],
deployment_started_at=deploy_start,
deployment_completed_at=deploy_end,
)
report = correlation.to_report()
assert report["correlation_id"] == "user-service/123"
assert report["status"] == "healthy"
assert report["build"]["id"] == "123"
assert report["build"]["workflow"] == "CI/CD Pipeline"
assert report["build"]["repo"] == "user-service"
assert report["build"]["status"] == "success"
assert report["deployment"]["name"] == "user-service"
assert report["deployment"]["namespace"] == "production"
assert report["deployment"]["replicas"] == "3/3"
assert report["metrics"]["end_to_end_duration_seconds"] is not None
class TestGitHubKubernetesBridge:
"""Tests for GitHubKubernetesBridge."""
@pytest.fixture
def bridge(self) -> GitHubKubernetesBridge:
"""Create a fresh bridge for each test."""
config = ForgeConfig(mode=ForgeMode.MOCK)
return GitHubKubernetesBridge(
config=config,
github_org="test-org",
default_namespace="production",
)
def test_forge_name(self, bridge: GitHubKubernetesBridge):
"""Test forge name property."""
assert bridge.forge_name == "github-kubernetes-bridge"
def test_supported_webhook_events(self, bridge: GitHubKubernetesBridge):
"""Test supported webhook events include both GitHub and K8s."""
events = bridge.supported_webhook_events
# GitHub events
assert "workflow_run.completed" in events
assert "workflow_run.in_progress" in events
assert "push" in events
# Kubernetes events
assert "deployment.progressing" in events
assert "deployment.available" in events
assert "deployment.failed" in events
assert "pod.running" in events
assert "pod.failed" in events
assert "pod.crashloop" in events
def test_register_repo_deployment_mapping(self, bridge: GitHubKubernetesBridge):
"""Test registering repo to deployment mapping."""
bridge.register_repo_deployment_mapping(
repo_name="user-service",
deployment_name="user-service-deploy",
namespace="production",
)
result = bridge.get_deployment_for_repo("user-service")
assert result == ("production", "user-service-deploy")
def test_register_repo_deployment_mapping_default_namespace(
self, bridge: GitHubKubernetesBridge
):
"""Test mapping uses default namespace when not specified."""
bridge.register_repo_deployment_mapping(
repo_name="api-gateway",
deployment_name="api-gateway-deploy",
)
result = bridge.get_deployment_for_repo("api-gateway")
assert result == ("production", "api-gateway-deploy")
def test_get_deployment_for_unknown_repo(self, bridge: GitHubKubernetesBridge):
"""Test getting deployment for unmapped repo."""
result = bridge.get_deployment_for_repo("unknown-repo")
assert result is None
@pytest.mark.asyncio
async def test_health_check(self, bridge: GitHubKubernetesBridge):
"""Test health check."""
health = await bridge.health_check()
assert health.healthy is True
assert health.name == "github-kubernetes-bridge"
@pytest.mark.asyncio
async def test_generate_mock_events(self, bridge: GitHubKubernetesBridge):
"""Test generating mock events."""
events = await bridge.generate_mock_events()
# Mock events may be empty if no correlations exist
assert isinstance(events, list)
# Bridge generates events based on existing correlations
# so empty list is valid for a fresh bridge
class TestGitHubKubernetesBridgeWebhookHandling:
"""Tests for bridge webhook handling."""
@pytest.fixture
def bridge(self) -> GitHubKubernetesBridge:
"""Create bridge with repo mapping."""
config = ForgeConfig(mode=ForgeMode.MOCK)
bridge = GitHubKubernetesBridge(config=config)
bridge.register_repo_deployment_mapping(
repo_name="user-service",
deployment_name="user-service",
namespace="production",
)
return bridge
@pytest.mark.asyncio
async def test_handle_build_started(self, bridge: GitHubKubernetesBridge):
"""Test handling build started event creates correlation."""
payload = WebhookPayload(
source="github",
event_type="workflow_run.in_progress",
timestamp=datetime.now(UTC),
raw_payload={
"workflow_run": {
"id": 11111,
"name": "CI/CD Pipeline",
"head_branch": "main",
"head_sha": "abc123def456",
"status": "in_progress",
},
"repository": {"name": "user-service"},
},
)
await bridge.on_webhook(payload)
# Verify correlation was created
correlation_id = "user-service/11111"
assert correlation_id in bridge._correlations
correlation = bridge._correlations[correlation_id]
assert correlation.build_id == "11111"
assert correlation.workflow_name == "CI/CD Pipeline"
assert correlation.repo == "user-service"
assert correlation.build_status == "pending"
assert correlation.deployment_name == "user-service"
assert correlation.namespace == "production"
@pytest.mark.asyncio
async def test_handle_build_completed_success(self, bridge: GitHubKubernetesBridge):
"""Test handling successful build completion."""
# First start the build
start_payload = WebhookPayload(
source="github",
event_type="workflow_run.in_progress",
timestamp=datetime.now(UTC),
raw_payload={
"workflow_run": {
"id": 22222,
"name": "CI/CD Pipeline",
"head_branch": "main",
"head_sha": "def456ghi789",
},
"repository": {"name": "user-service"},
},
)
await bridge.on_webhook(start_payload)
# Now complete it
complete_payload = WebhookPayload(
source="github",
event_type="workflow_run.completed",
timestamp=datetime.now(UTC),
raw_payload={
"workflow_run": {
"id": 22222,
"name": "CI/CD Pipeline",
"head_branch": "main",
"head_sha": "def456ghi789",
"status": "completed",
"conclusion": "success",
},
"repository": {"name": "user-service"},
},
)
await bridge.on_webhook(complete_payload)
correlation = bridge._correlations["user-service/22222"]
assert correlation.build_status == "success"
assert correlation.build_completed_at is not None
assert correlation.image_tag is not None
assert correlation.compute_status() == BuildToDeploymentStatus.BUILD_PASSED
@pytest.mark.asyncio
async def test_handle_build_completed_failure(self, bridge: GitHubKubernetesBridge):
"""Test handling failed build completion."""
# First start the build
start_payload = WebhookPayload(
source="github",
event_type="workflow_run.in_progress",
timestamp=datetime.now(UTC),
raw_payload={
"workflow_run": {"id": 33333, "name": "CI/CD", "head_sha": "xyz"},
"repository": {"name": "user-service"},
},
)
await bridge.on_webhook(start_payload)
# Now fail it
fail_payload = WebhookPayload(
source="github",
event_type="workflow_run.completed",
timestamp=datetime.now(UTC),
raw_payload={
"workflow_run": {
"id": 33333,
"conclusion": "failure",
},
"repository": {"name": "user-service"},
},
)
await bridge.on_webhook(fail_payload)
correlation = bridge._correlations["user-service/33333"]
assert correlation.build_status == "failure"
assert correlation.compute_status() == BuildToDeploymentStatus.BUILD_FAILED
@pytest.mark.asyncio
async def test_handle_deployment_progressing(self, bridge: GitHubKubernetesBridge):
"""Test handling deployment progressing event."""
# First create a build correlation
start_payload = WebhookPayload(
source="github",
event_type="workflow_run.in_progress",
timestamp=datetime.now(UTC),
raw_payload={
"workflow_run": {"id": 44444, "name": "CI/CD", "head_sha": "prog123"},
"repository": {"name": "user-service"},
},
)
await bridge.on_webhook(start_payload)
# Complete the build
complete_payload = WebhookPayload(
source="github",
event_type="workflow_run.completed",
timestamp=datetime.now(UTC),
raw_payload={
"workflow_run": {
"id": 44444,
"conclusion": "success",
"head_sha": "prog123",
},
"repository": {"name": "user-service"},
},
)
await bridge.on_webhook(complete_payload)
# Now deployment starts
deploy_payload = WebhookPayload(
source="kubernetes",
event_type="deployment.progressing",
timestamp=datetime.now(UTC),
raw_payload={
"deployment": {
"name": "user-service",
"namespace": "production",
},
"image": f"user-service:{bridge._correlations['user-service/44444'].commit_sha[:7]}",
},
)
await bridge.on_webhook(deploy_payload)
correlation = bridge._correlations["user-service/44444"]
assert correlation.deployment_status == "progressing"
assert correlation.compute_status() == BuildToDeploymentStatus.DEPLOYING
@pytest.mark.asyncio
async def test_handle_deployment_available(self, bridge: GitHubKubernetesBridge):
"""Test handling deployment available event."""
# Create correlation
start_payload = WebhookPayload(
source="github",
event_type="workflow_run.in_progress",
timestamp=datetime.now(UTC),
raw_payload={
"workflow_run": {"id": 55555, "name": "CI/CD", "head_sha": "avail123"},
"repository": {"name": "user-service"},
},
)
await bridge.on_webhook(start_payload)
complete_payload = WebhookPayload(
source="github",
event_type="workflow_run.completed",
timestamp=datetime.now(UTC),
raw_payload={
"workflow_run": {"id": 55555, "conclusion": "success", "head_sha": "avail123"},
"repository": {"name": "user-service"},
},
)
await bridge.on_webhook(complete_payload)
# Deployment becomes available
available_payload = WebhookPayload(
source="kubernetes",
event_type="deployment.available",
timestamp=datetime.now(UTC),
raw_payload={
"deployment": {
"name": "user-service",
"namespace": "production",
"replicas": 3,
"availableReplicas": 3,
},
"image": f"user-service:{bridge._correlations['user-service/55555'].commit_sha[:7]}",
},
)
await bridge.on_webhook(available_payload)
correlation = bridge._correlations["user-service/55555"]
assert correlation.deployment_status == "available"
assert correlation.deployment_completed_at is not None
@pytest.mark.asyncio
async def test_full_build_to_deploy_cycle(self, bridge: GitHubKubernetesBridge):
"""Test complete build→deploy cycle and correlation report."""
build_id = 99999
# 1. Build starts
await bridge.on_webhook(
WebhookPayload(
source="github",
event_type="workflow_run.in_progress",
timestamp=datetime.now(UTC),
raw_payload={
"workflow_run": {
"id": build_id,
"name": "CI/CD Pipeline",
"head_branch": "main",
"head_sha": "fullcycle123",
},
"repository": {"name": "user-service"},
},
)
)
# 2. Build completes
await bridge.on_webhook(
WebhookPayload(
source="github",
event_type="workflow_run.completed",
timestamp=datetime.now(UTC),
raw_payload={
"workflow_run": {
"id": build_id,
"conclusion": "success",
"head_sha": "fullcycle123",
},
"repository": {"name": "user-service"},
},
)
)
# 3. Deployment progresses
await bridge.on_webhook(
WebhookPayload(
source="kubernetes",
event_type="deployment.progressing",
timestamp=datetime.now(UTC),
raw_payload={
"deployment": {"name": "user-service", "namespace": "production"},
"image": "user-service:fullcyc",
},
)
)
# 4. Deployment available
await bridge.on_webhook(
WebhookPayload(
source="kubernetes",
event_type="deployment.available",
timestamp=datetime.now(UTC),
raw_payload={
"deployment": {
"name": "user-service",
"namespace": "production",
"replicas": 3,
"availableReplicas": 3,
},
"image": "user-service:fullcyc",
},
)
)
# Verify final state
correlation = bridge._correlations[f"user-service/{build_id}"]
assert correlation.build_status == "success"
assert correlation.deployment_status == "available"
# Generate report
report = correlation.to_report()
assert report["status"] in ("healthy", "deployed")
assert report["build"]["status"] == "success"
assert report["deployment"]["status"] == "available"
class TestGitHubKubernetesBridgeSingleton:
"""Tests for bridge singleton factory."""
def test_get_github_kubernetes_bridge_singleton(self):
"""Test that factory returns singleton."""
import app.forges.github_k8s_bridge as module
# Reset singleton
module._github_k8s_bridge = None
bridge1 = get_github_kubernetes_bridge()
bridge2 = get_github_kubernetes_bridge()
assert bridge1 is bridge2
class TestBridgeCorrelationLookup:
"""Tests for correlation lookup methods."""
@pytest.fixture
def bridge_with_correlations(self) -> GitHubKubernetesBridge:
"""Create bridge with pre-populated correlations."""
config = ForgeConfig(mode=ForgeMode.MOCK)
bridge = GitHubKubernetesBridge(config=config)
# Add some correlations
bridge._correlations["user-service/100"] = BuildDeploymentCorrelation(
build_id="100",
workflow_name="CI/CD",
repo="user-service",
branch="main",
commit_sha="abc123",
build_status="success",
deployment_status="available",
)
bridge._correlations["api-gateway/200"] = BuildDeploymentCorrelation(
build_id="200",
workflow_name="Build",
repo="api-gateway",
branch="main",
commit_sha="def456",
build_status="failure",
)
return bridge
def test_get_all_correlations(self, bridge_with_correlations: GitHubKubernetesBridge):
"""Test getting all correlations."""
correlations = bridge_with_correlations._correlations
assert len(correlations) == 2
assert "user-service/100" in correlations
assert "api-gateway/200" in correlations
def test_correlation_report_generation(self, bridge_with_correlations: GitHubKubernetesBridge):
"""Test generating reports from correlations."""
reports = [c.to_report() for c in bridge_with_correlations._correlations.values()]
assert len(reports) == 2
assert any(r["build"]["repo"] == "user-service" for r in reports)
assert any(r["build"]["repo"] == "api-gateway" for r in reports)