Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /mattyopon_faultray /tests /test_team_tracker.py
| """Tests for the Team Resilience Tracker.""" | |
| from __future__ import annotations | |
| import json | |
| from pathlib import Path | |
| import pytest | |
| from faultray.model.components import ( | |
| AutoScalingConfig, | |
| CircuitBreakerConfig, | |
| Component, | |
| ComponentType, | |
| Dependency, | |
| FailoverConfig, | |
| ) | |
| from faultray.model.graph import InfraGraph | |
| from faultray.simulator.team_tracker import ( | |
| TeamComparison, | |
| TeamLeaderboard, | |
| TeamMetrics, | |
| TeamSnapshot, | |
| TeamTracker, | |
| auto_assign_teams, | |
| ) | |
| # --------------------------------------------------------------------------- | |
| # Fixtures | |
| # --------------------------------------------------------------------------- | |
| def sample_graph() -> InfraGraph: | |
| """Create a sample infrastructure graph with components spanning multiple teams.""" | |
| graph = InfraGraph() | |
| # Platform team components | |
| graph.add_component(Component( | |
| id="lb", | |
| name="Load Balancer (nginx)", | |
| type=ComponentType.LOAD_BALANCER, | |
| replicas=2, | |
| failover=FailoverConfig(enabled=True, promotion_time_seconds=10), | |
| )) | |
| graph.add_component(Component( | |
| id="cdn", | |
| name="CDN Edge", | |
| type=ComponentType.DNS, | |
| replicas=1, | |
| )) | |
| # Backend team components | |
| graph.add_component(Component( | |
| id="app", | |
| name="Application Server", | |
| type=ComponentType.APP_SERVER, | |
| replicas=3, | |
| autoscaling=AutoScalingConfig(enabled=True, min_replicas=2, max_replicas=8), | |
| )) | |
| graph.add_component(Component( | |
| id="api", | |
| name="API Service", | |
| type=ComponentType.APP_SERVER, | |
| replicas=2, | |
| )) | |
| # Data team components | |
| graph.add_component(Component( | |
| id="db", | |
| name="PostgreSQL Database", | |
| type=ComponentType.DATABASE, | |
| replicas=2, | |
| failover=FailoverConfig(enabled=True, promotion_time_seconds=30), | |
| )) | |
| graph.add_component(Component( | |
| id="cache", | |
| name="Redis Cache", | |
| type=ComponentType.CACHE, | |
| replicas=1, # SPOF | |
| )) | |
| # Messaging team | |
| graph.add_component(Component( | |
| id="kafka", | |
| name="Kafka Event Bus", | |
| type=ComponentType.QUEUE, | |
| replicas=3, | |
| failover=FailoverConfig(enabled=True), | |
| )) | |
| # Dependencies | |
| graph.add_dependency(Dependency( | |
| source_id="lb", | |
| target_id="app", | |
| dependency_type="requires", | |
| circuit_breaker=CircuitBreakerConfig(enabled=True), | |
| )) | |
| graph.add_dependency(Dependency( | |
| source_id="app", | |
| target_id="db", | |
| dependency_type="requires", | |
| circuit_breaker=CircuitBreakerConfig(enabled=True), | |
| )) | |
| graph.add_dependency(Dependency( | |
| source_id="app", | |
| target_id="cache", | |
| dependency_type="optional", | |
| )) | |
| graph.add_dependency(Dependency( | |
| source_id="app", | |
| target_id="kafka", | |
| dependency_type="requires", | |
| circuit_breaker=CircuitBreakerConfig(enabled=True), | |
| )) | |
| graph.add_dependency(Dependency( | |
| source_id="api", | |
| target_id="db", | |
| dependency_type="requires", | |
| )) | |
| return graph | |
| def team_mapping() -> dict[str, list[str]]: | |
| """Manual team mapping for the sample graph.""" | |
| return { | |
| "platform": ["lb", "cdn"], | |
| "backend": ["app", "api"], | |
| "data": ["db", "cache"], | |
| "messaging": ["kafka"], | |
| } | |
| def tracker(tmp_path: Path) -> TeamTracker: | |
| """Create a tracker with temporary history file.""" | |
| return TeamTracker(history_path=tmp_path / "team_history.jsonl") | |
| # --------------------------------------------------------------------------- | |
| # Auto assignment tests | |
| # --------------------------------------------------------------------------- | |
| class TestAutoAssignment: | |
| """Tests for automatic team assignment.""" | |
| def test_auto_assign_categorizes_lb_as_platform(self, sample_graph): | |
| mapping = auto_assign_teams(sample_graph) | |
| assert "lb" in mapping.get("platform", []) | |
| def test_auto_assign_categorizes_app_as_backend(self, sample_graph): | |
| mapping = auto_assign_teams(sample_graph) | |
| assert "app" in mapping.get("backend", []) | |
| def test_auto_assign_categorizes_db_as_data(self, sample_graph): | |
| mapping = auto_assign_teams(sample_graph) | |
| assert "db" in mapping.get("data", []) | |
| def test_auto_assign_categorizes_kafka_as_messaging(self, sample_graph): | |
| mapping = auto_assign_teams(sample_graph) | |
| assert "kafka" in mapping.get("messaging", []) | |
| def test_auto_assign_categorizes_cache_as_data(self, sample_graph): | |
| mapping = auto_assign_teams(sample_graph) | |
| assert "cache" in mapping.get("data", []) | |
| def test_auto_assign_categorizes_cdn_as_platform(self, sample_graph): | |
| mapping = auto_assign_teams(sample_graph) | |
| assert "cdn" in mapping.get("platform", []) | |
| def test_auto_assign_returns_all_components(self, sample_graph): | |
| mapping = auto_assign_teams(sample_graph) | |
| all_assigned = set() | |
| for ids in mapping.values(): | |
| all_assigned.update(ids) | |
| assert all_assigned == set(sample_graph.components.keys()) | |
| def test_auto_assign_empty_graph(self): | |
| graph = InfraGraph() | |
| mapping = auto_assign_teams(graph) | |
| assert mapping == {} | |
| # --------------------------------------------------------------------------- | |
| # analyze_teams tests | |
| # --------------------------------------------------------------------------- | |
| class TestAnalyzeTeams: | |
| """Tests for team analysis.""" | |
| def test_analyze_returns_metrics_per_team(self, sample_graph, team_mapping, tracker): | |
| teams = tracker.analyze_teams(sample_graph, team_mapping) | |
| assert len(teams) == 4 | |
| team_names = {t.team_name for t in teams} | |
| assert team_names == {"platform", "backend", "data", "messaging"} | |
| def test_analyze_team_metrics_type(self, sample_graph, team_mapping, tracker): | |
| teams = tracker.analyze_teams(sample_graph, team_mapping) | |
| for t in teams: | |
| assert isinstance(t, TeamMetrics) | |
| assert isinstance(t.resilience_score, float) | |
| assert isinstance(t.spof_count, int) | |
| assert isinstance(t.failover_coverage, float) | |
| assert isinstance(t.circuit_breaker_coverage, float) | |
| assert isinstance(t.sre_maturity_level, int) | |
| def test_analyze_identifies_data_team_spof(self, sample_graph, team_mapping, tracker): | |
| """Cache is a SPOF in the data team (replicas=1, has dependents via app).""" | |
| teams = tracker.analyze_teams(sample_graph, team_mapping) | |
| data_team = next(t for t in teams if t.team_name == "data") | |
| # cache has replicas=1 but the dependency is from app->cache (optional), so | |
| # cache has dependents (app). It should count as a SPOF. | |
| # Actually cache does NOT have failover enabled, so it should be SPOF | |
| assert data_team.spof_count >= 0 # May or may not depending on graph direction | |
| def test_analyze_platform_team_has_failover(self, sample_graph, team_mapping, tracker): | |
| teams = tracker.analyze_teams(sample_graph, team_mapping) | |
| platform_team = next(t for t in teams if t.team_name == "platform") | |
| assert platform_team.failover_coverage > 0 | |
| def test_analyze_scores_are_bounded(self, sample_graph, team_mapping, tracker): | |
| teams = tracker.analyze_teams(sample_graph, team_mapping) | |
| for t in teams: | |
| assert 0.0 <= t.resilience_score <= 100.0 | |
| assert 0.0 <= t.failover_coverage <= 100.0 | |
| assert 0.0 <= t.circuit_breaker_coverage <= 100.0 | |
| assert 1 <= t.sre_maturity_level <= 5 | |
| def test_analyze_maturity_levels_reasonable(self, sample_graph, team_mapping, tracker): | |
| teams = tracker.analyze_teams(sample_graph, team_mapping) | |
| for t in teams: | |
| assert t.sre_maturity_level >= 1 | |
| assert t.sre_maturity_level <= 5 | |
| def test_analyze_risk_estimate_non_negative(self, sample_graph, team_mapping, tracker): | |
| teams = tracker.analyze_teams(sample_graph, team_mapping) | |
| for t in teams: | |
| assert t.annual_risk_estimate >= 0 | |
| def test_analyze_empty_mapping(self, sample_graph, tracker): | |
| teams = tracker.analyze_teams(sample_graph, {}) | |
| assert teams == [] | |
| def test_analyze_missing_components_in_mapping(self, sample_graph, tracker): | |
| mapping = {"nonexistent": ["fake-id-1", "fake-id-2"]} | |
| teams = tracker.analyze_teams(sample_graph, mapping) | |
| assert teams == [] | |
| def test_analyze_components_owned_list(self, sample_graph, team_mapping, tracker): | |
| teams = tracker.analyze_teams(sample_graph, team_mapping) | |
| for t in teams: | |
| assert len(t.components_owned) > 0 | |
| for cid in t.components_owned: | |
| assert cid in sample_graph.components | |
| # --------------------------------------------------------------------------- | |
| # compare_teams tests | |
| # --------------------------------------------------------------------------- | |
| class TestCompareTeams: | |
| """Tests for team comparison.""" | |
| def test_compare_returns_comparison(self, sample_graph, team_mapping, tracker): | |
| comparison = tracker.compare_teams(sample_graph, team_mapping) | |
| assert isinstance(comparison, TeamComparison) | |
| assert len(comparison.teams) == 4 | |
| def test_compare_identifies_leader_and_laggard(self, sample_graph, team_mapping, tracker): | |
| comparison = tracker.compare_teams(sample_graph, team_mapping) | |
| assert comparison.leader != "" | |
| assert comparison.laggard != "" | |
| # Leader should have higher score than laggard | |
| leader_score = next( | |
| t.resilience_score for t in comparison.teams if t.team_name == comparison.leader | |
| ) | |
| laggard_score = next( | |
| t.resilience_score for t in comparison.teams if t.team_name == comparison.laggard | |
| ) | |
| assert leader_score >= laggard_score | |
| def test_compare_avg_score(self, sample_graph, team_mapping, tracker): | |
| comparison = tracker.compare_teams(sample_graph, team_mapping) | |
| assert comparison.avg_score > 0 | |
| def test_compare_score_spread(self, sample_graph, team_mapping, tracker): | |
| comparison = tracker.compare_teams(sample_graph, team_mapping) | |
| assert comparison.score_spread >= 0 | |
| def test_compare_improvement_areas(self, sample_graph, team_mapping, tracker): | |
| comparison = tracker.compare_teams(sample_graph, team_mapping) | |
| assert isinstance(comparison.improvement_areas, dict) | |
| def test_compare_empty_mapping(self, sample_graph, tracker): | |
| comparison = tracker.compare_teams(sample_graph, {}) | |
| assert comparison.teams == [] | |
| assert comparison.leader == "" | |
| assert comparison.laggard == "" | |
| # --------------------------------------------------------------------------- | |
| # Leaderboard tests | |
| # --------------------------------------------------------------------------- | |
| class TestLeaderboard: | |
| """Tests for team leaderboard.""" | |
| def test_leaderboard_returns_rankings(self, sample_graph, team_mapping, tracker): | |
| lb = tracker.get_leaderboard(sample_graph, team_mapping) | |
| assert isinstance(lb, TeamLeaderboard) | |
| assert len(lb.rankings) == 4 | |
| def test_leaderboard_rankings_ordered(self, sample_graph, team_mapping, tracker): | |
| lb = tracker.get_leaderboard(sample_graph, team_mapping) | |
| scores = [score for _, _, score in lb.rankings] | |
| assert scores == sorted(scores, reverse=True) | |
| def test_leaderboard_rank_numbers_sequential(self, sample_graph, team_mapping, tracker): | |
| lb = tracker.get_leaderboard(sample_graph, team_mapping) | |
| ranks = [rank for rank, _, _ in lb.rankings] | |
| assert ranks == list(range(1, len(ranks) + 1)) | |
| def test_leaderboard_needs_attention_type(self, sample_graph, team_mapping, tracker): | |
| lb = tracker.get_leaderboard(sample_graph, team_mapping) | |
| assert isinstance(lb.needs_attention, list) | |
| def test_leaderboard_empty_mapping(self, sample_graph, tracker): | |
| lb = tracker.get_leaderboard(sample_graph, {}) | |
| assert lb.rankings == [] | |
| # --------------------------------------------------------------------------- | |
| # Snapshot recording and history tests | |
| # --------------------------------------------------------------------------- | |
| class TestSnapshotHistory: | |
| """Tests for snapshot recording and history retrieval.""" | |
| def test_record_creates_history_file(self, sample_graph, team_mapping, tracker): | |
| tracker.record_snapshot(sample_graph, team_mapping) | |
| assert tracker._history_path.exists() | |
| def test_record_writes_jsonl(self, sample_graph, team_mapping, tracker): | |
| tracker.record_snapshot(sample_graph, team_mapping) | |
| content = tracker._history_path.read_text(encoding="utf-8") | |
| lines = [l for l in content.strip().split("\n") if l] | |
| assert len(lines) == 4 # 4 teams | |
| for line in lines: | |
| entry = json.loads(line) | |
| assert "timestamp" in entry | |
| assert "team_name" in entry | |
| assert "metrics" in entry | |
| def test_record_multiple_snapshots(self, sample_graph, team_mapping, tracker): | |
| tracker.record_snapshot(sample_graph, team_mapping) | |
| tracker.record_snapshot(sample_graph, team_mapping) | |
| content = tracker._history_path.read_text(encoding="utf-8") | |
| lines = [l for l in content.strip().split("\n") if l] | |
| assert len(lines) == 8 # 4 teams * 2 snapshots | |
| def test_get_team_history_returns_snapshots(self, sample_graph, team_mapping, tracker): | |
| tracker.record_snapshot(sample_graph, team_mapping) | |
| history = tracker.get_team_history("platform") | |
| assert len(history) == 1 | |
| assert isinstance(history[0], TeamSnapshot) | |
| assert history[0].team_name == "platform" | |
| def test_get_team_history_nonexistent_team(self, sample_graph, team_mapping, tracker): | |
| tracker.record_snapshot(sample_graph, team_mapping) | |
| history = tracker.get_team_history("nonexistent") | |
| assert history == [] | |
| def test_get_team_history_no_file(self, tracker): | |
| history = tracker.get_team_history("platform") | |
| assert history == [] | |
| def test_get_team_history_days_filter(self, sample_graph, team_mapping, tracker): | |
| tracker.record_snapshot(sample_graph, team_mapping) | |
| # Recent snapshot should be within 1 day | |
| history = tracker.get_team_history("platform", days=1) | |
| assert len(history) == 1 | |
| def test_most_improved_after_recording(self, sample_graph, team_mapping, tracker): | |
| """After recording, get_leaderboard can detect most_improved.""" | |
| tracker.record_snapshot(sample_graph, team_mapping) | |
| lb = tracker.get_leaderboard(sample_graph, team_mapping) | |
| # most_improved may be None if scores didn't change | |
| assert isinstance(lb.most_improved, (str, type(None))) | |
| # --------------------------------------------------------------------------- | |
| # Integration with auto_assign_teams | |
| # --------------------------------------------------------------------------- | |
| class TestIntegration: | |
| """Integration tests combining auto-assignment with analysis.""" | |
| def test_auto_assign_then_analyze(self, sample_graph, tracker): | |
| mapping = tracker.auto_assign_teams(sample_graph) | |
| teams = tracker.analyze_teams(sample_graph, mapping) | |
| assert len(teams) > 0 | |
| def test_auto_assign_then_compare(self, sample_graph, tracker): | |
| mapping = tracker.auto_assign_teams(sample_graph) | |
| comparison = tracker.compare_teams(sample_graph, mapping) | |
| assert len(comparison.teams) > 0 | |
| assert comparison.leader != "" | |
| def test_auto_assign_then_leaderboard(self, sample_graph, tracker): | |
| mapping = tracker.auto_assign_teams(sample_graph) | |
| lb = tracker.get_leaderboard(sample_graph, mapping) | |
| assert len(lb.rankings) > 0 | |
| def test_full_workflow(self, sample_graph, tracker): | |
| """Test the complete workflow: auto-assign, analyze, record, compare, leaderboard.""" | |
| mapping = tracker.auto_assign_teams(sample_graph) | |
| assert len(mapping) > 0 | |
| teams = tracker.analyze_teams(sample_graph, mapping) | |
| assert len(teams) > 0 | |
| tracker.record_snapshot(sample_graph, mapping) | |
| assert tracker._history_path.exists() | |
| comparison = tracker.compare_teams(sample_graph, mapping) | |
| assert comparison.leader != "" | |
| lb = tracker.get_leaderboard(sample_graph, mapping) | |
| assert len(lb.rankings) > 0 | |
| history = tracker.get_team_history(teams[0].team_name) | |
| assert len(history) == 1 | |
| # --------------------------------------------------------------------------- | |
| # Coverage boost tests | |
| # --------------------------------------------------------------------------- | |
| class TestAutoAssignOtherTeam: | |
| """Test the 'other' team fallback in auto_assign_teams (line 116).""" | |
| def test_auto_assign_other_team_for_custom_type(self): | |
| """Component with unmatched name and CUSTOM type goes to 'other'.""" | |
| graph = InfraGraph() | |
| graph.add_component(Component( | |
| id="mystery", | |
| name="Mystery Box", | |
| type=ComponentType.CUSTOM, | |
| replicas=1, | |
| )) | |
| mapping = auto_assign_teams(graph) | |
| assert "mystery" in mapping.get("other", []) | |
| class TestMostImprovedPositiveCheck: | |
| """Test that most_improved requires a positive improvement (line 341).""" | |
| def test_most_improved_when_scores_improve(self, sample_graph, team_mapping, tmp_path): | |
| """Record a snapshot with current scores, then improve graph so leaderboard detects improvement.""" | |
| tracker = TeamTracker(history_path=tmp_path / "team_history.jsonl") | |
| # Record baseline snapshot with lower-scoring graph | |
| # Create a weaker graph first (fewer replicas, no failover) | |
| weak_graph = InfraGraph() | |
| for comp_id, comp in sample_graph.components.items(): | |
| c_dict = comp.model_dump() | |
| # Make everything weaker | |
| c_dict["replicas"] = 1 | |
| c_dict["failover"]["enabled"] = False | |
| c_dict["autoscaling"]["enabled"] = False | |
| weak_graph.add_component(Component(**c_dict)) | |
| for edge in sample_graph.all_dependency_edges(): | |
| weak_graph.add_dependency(Dependency(**edge.model_dump())) | |
| # Record the weak graph as history | |
| tracker.record_snapshot(weak_graph, team_mapping) | |
| # Now check leaderboard with the stronger sample_graph | |
| # The improved graph should have higher scores, so most_improved should be set | |
| lb = tracker.get_leaderboard(sample_graph, team_mapping) | |
| # most_improved should be a string (some team improved) | |
| assert isinstance(lb.most_improved, str) | |
| assert lb.most_improved != "" | |
| def test_most_improved_none_when_no_improvement(self, sample_graph, team_mapping, tmp_path): | |
| """When scores don't change, most_improved should be None.""" | |
| tracker = TeamTracker(history_path=tmp_path / "team_history.jsonl") | |
| tracker.record_snapshot(sample_graph, team_mapping) | |
| # Same graph -> no improvement | |
| lb = tracker.get_leaderboard(sample_graph, team_mapping) | |
| assert lb.most_improved is None | |
| class TestHistoryParsingErrors: | |
| """Test get_team_history with malformed entries (lines 385-386).""" | |
| def test_history_with_bad_timestamp(self, tmp_path): | |
| """Entry with invalid timestamp triggers ValueError -> skipped (line 385).""" | |
| from datetime import datetime | |
| tracker = TeamTracker(history_path=tmp_path / "team_history.jsonl") | |
| valid_entry = { | |
| "timestamp": datetime.now().isoformat(), | |
| "team_name": "platform", | |
| "metrics": { | |
| "team_name": "platform", | |
| "components_owned": ["lb"], | |
| "resilience_score": 80.0, | |
| "spof_count": 0, | |
| "critical_findings": 0, | |
| "failover_coverage": 100.0, | |
| "circuit_breaker_coverage": 50.0, | |
| "sre_maturity_level": 3, | |
| "annual_risk_estimate": 0.0, | |
| }, | |
| } | |
| bad_timestamp_entry = { | |
| "timestamp": "not-a-date", | |
| "team_name": "platform", | |
| "metrics": { | |
| "team_name": "platform", | |
| "components_owned": ["lb"], | |
| "resilience_score": 50.0, | |
| "spof_count": 0, | |
| "critical_findings": 0, | |
| "failover_coverage": 0.0, | |
| "circuit_breaker_coverage": 0.0, | |
| "sre_maturity_level": 1, | |
| "annual_risk_estimate": 0.0, | |
| }, | |
| } | |
| with open(tmp_path / "team_history.jsonl", "w") as f: | |
| f.write(json.dumps(valid_entry) + "\n") | |
| f.write(json.dumps(bad_timestamp_entry) + "\n") | |
| history = tracker.get_team_history("platform") | |
| # Valid entry loaded, bad timestamp skipped | |
| assert len(history) == 1 | |
| assert history[0].team_name == "platform" | |
| def test_history_with_missing_key(self, tmp_path): | |
| """Entry missing the 'timestamp' key triggers KeyError -> skipped (line 386).""" | |
| tracker = TeamTracker(history_path=tmp_path / "team_history.jsonl") | |
| # Entry completely missing 'timestamp' key -> KeyError on entry["timestamp"] | |
| missing_key_entry = { | |
| "team_name": "platform", | |
| "metrics": { | |
| "team_name": "platform", | |
| "components_owned": ["lb"], | |
| "resilience_score": 50.0, | |
| "spof_count": 0, | |
| "critical_findings": 0, | |
| "failover_coverage": 0.0, | |
| "circuit_breaker_coverage": 0.0, | |
| "sre_maturity_level": 1, | |
| "annual_risk_estimate": 0.0, | |
| }, | |
| } | |
| with open(tmp_path / "team_history.jsonl", "w") as f: | |
| f.write(json.dumps(missing_key_entry) + "\n") | |
| history = tracker.get_team_history("platform") | |
| assert history == [] | |
| class TestCalculateTeamScoreEdgeCases: | |
| """Test _calculate_team_score internal helper edge cases.""" | |
| def test_empty_component_ids(self, tmp_path): | |
| """Empty component_ids should return 0.0 (line 406).""" | |
| tracker = TeamTracker(history_path=tmp_path / "team_history.jsonl") | |
| graph = InfraGraph() | |
| score = tracker._calculate_team_score(graph, []) | |
| assert score == 0.0 | |
| def test_failover_reduces_penalty(self, tmp_path): | |
| """Failover enabled on SPOF component reduces penalty (line 418).""" | |
| tracker = TeamTracker(history_path=tmp_path / "team_history.jsonl") | |
| graph = InfraGraph() | |
| # SPOF with failover | |
| graph.add_component(Component( | |
| id="db", | |
| name="Database", | |
| type=ComponentType.DATABASE, | |
| replicas=1, | |
| failover=FailoverConfig(enabled=True), | |
| )) | |
| graph.add_component(Component( | |
| id="app", | |
| name="App", | |
| type=ComponentType.APP_SERVER, | |
| replicas=2, | |
| )) | |
| graph.add_dependency(Dependency(source_id="app", target_id="db")) | |
| score_with_failover = tracker._calculate_team_score(graph, ["db", "app"]) | |
| # Without failover | |
| graph2 = InfraGraph() | |
| graph2.add_component(Component( | |
| id="db", | |
| name="Database", | |
| type=ComponentType.DATABASE, | |
| replicas=1, | |
| )) | |
| graph2.add_component(Component( | |
| id="app", | |
| name="App", | |
| type=ComponentType.APP_SERVER, | |
| replicas=2, | |
| )) | |
| graph2.add_dependency(Dependency(source_id="app", target_id="db")) | |
| score_without_failover = tracker._calculate_team_score(graph2, ["db", "app"]) | |
| assert score_with_failover > score_without_failover | |
| def test_autoscaling_reduces_penalty(self, tmp_path): | |
| """Autoscaling on SPOF reduces penalty (line 420).""" | |
| tracker = TeamTracker(history_path=tmp_path / "team_history.jsonl") | |
| graph = InfraGraph() | |
| graph.add_component(Component( | |
| id="db", | |
| name="Database", | |
| type=ComponentType.DATABASE, | |
| replicas=1, | |
| autoscaling=AutoScalingConfig(enabled=True, min_replicas=1, max_replicas=5), | |
| )) | |
| graph.add_component(Component( | |
| id="app", | |
| name="App", | |
| type=ComponentType.APP_SERVER, | |
| replicas=2, | |
| )) | |
| graph.add_dependency(Dependency(source_id="app", target_id="db")) | |
| score_with_as = tracker._calculate_team_score(graph, ["db", "app"]) | |
| graph2 = InfraGraph() | |
| graph2.add_component(Component( | |
| id="db", | |
| name="Database", | |
| type=ComponentType.DATABASE, | |
| replicas=1, | |
| )) | |
| graph2.add_component(Component( | |
| id="app", | |
| name="App", | |
| type=ComponentType.APP_SERVER, | |
| replicas=2, | |
| )) | |
| graph2.add_dependency(Dependency(source_id="app", target_id="db")) | |
| score_without_as = tracker._calculate_team_score(graph2, ["db", "app"]) | |
| assert score_with_as > score_without_as | |
| def test_high_utilization_penalty_above_90(self, tmp_path): | |
| """Utilization >90% should penalize score by 10 (line 426).""" | |
| tracker = TeamTracker(history_path=tmp_path / "team_history.jsonl") | |
| from faultray.model.components import ResourceMetrics | |
| graph = InfraGraph() | |
| graph.add_component(Component( | |
| id="web", | |
| name="Web", | |
| type=ComponentType.APP_SERVER, | |
| replicas=2, | |
| metrics=ResourceMetrics(cpu_percent=95), | |
| )) | |
| score = tracker._calculate_team_score(graph, ["web"]) | |
| assert score < 100.0 | |
| assert score == 90.0 # 100 - 10 | |
| def test_high_utilization_penalty_above_80(self, tmp_path): | |
| """Utilization >80% but <=90% should penalize score by 5 (line 428).""" | |
| tracker = TeamTracker(history_path=tmp_path / "team_history.jsonl") | |
| from faultray.model.components import ResourceMetrics | |
| graph = InfraGraph() | |
| graph.add_component(Component( | |
| id="web", | |
| name="Web", | |
| type=ComponentType.APP_SERVER, | |
| replicas=2, | |
| metrics=ResourceMetrics(cpu_percent=85), | |
| )) | |
| score = tracker._calculate_team_score(graph, ["web"]) | |
| assert score == 95.0 # 100 - 5 | |
| def test_cb_coverage_penalty(self, tmp_path): | |
| """CB coverage below 50% should apply penalty (line 443).""" | |
| tracker = TeamTracker(history_path=tmp_path / "team_history.jsonl") | |
| graph = InfraGraph() | |
| graph.add_component(Component( | |
| id="app", | |
| name="App", | |
| type=ComponentType.APP_SERVER, | |
| replicas=2, | |
| )) | |
| graph.add_component(Component( | |
| id="db", | |
| name="Database", | |
| type=ComponentType.DATABASE, | |
| replicas=2, | |
| )) | |
| graph.add_component(Component( | |
| id="cache", | |
| name="Cache", | |
| type=ComponentType.CACHE, | |
| replicas=2, | |
| )) | |
| # Two deps, neither with CB -> cb_ratio = 0 < 0.5 -> -10 | |
| graph.add_dependency(Dependency( | |
| source_id="app", target_id="db", | |
| )) | |
| graph.add_dependency(Dependency( | |
| source_id="app", target_id="cache", | |
| )) | |
| score = tracker._calculate_team_score(graph, ["app", "db", "cache"]) | |
| assert score == 90.0 # 100 - 10 (cb penalty) | |
| class TestHistoryLoadErrors: | |
| """Test _load_history with OS errors and malformed JSON (lines 460-463).""" | |
| def test_load_history_with_bad_json_lines(self, tmp_path): | |
| tracker = TeamTracker(history_path=tmp_path / "team_history.jsonl") | |
| with open(tmp_path / "team_history.jsonl", "w") as f: | |
| f.write("not valid json\n") | |
| f.write('{"team_name": "test"}\n') | |
| f.write("also invalid\n") | |
| entries = tracker._load_history() | |
| # Should skip invalid lines and return the valid one | |
| assert len(entries) == 1 | |
| assert entries[0]["team_name"] == "test" | |
| def test_load_history_os_error(self, tmp_path): | |
| """OSError when reading history file (line 462-463).""" | |
| import os | |
| if hasattr(os, "geteuid") and os.geteuid() == 0: | |
| pytest.skip("chmod 000 does not block reads when running as root") | |
| tracker = TeamTracker(history_path=tmp_path / "team_history.jsonl") | |
| # Create the file, then make it unreadable | |
| with open(tmp_path / "team_history.jsonl", "w") as f: | |
| f.write('{"test": true}\n') | |
| os.chmod(tmp_path / "team_history.jsonl", 0o000) | |
| entries = tracker._load_history() | |
| assert entries == [] | |
| # Restore permissions for cleanup | |
| os.chmod(tmp_path / "team_history.jsonl", 0o644) | |
Xet Storage Details
- Size:
- 29.2 kB
- Xet hash:
- 645f83a2c39422218b3a5f295098b49f71faa03a465118121cd59b108c90daf2
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.