|
| 1 | +from unittest.mock import patch |
| 2 | +from django.test import SimpleTestCase |
| 3 | +from rest_framework.test import APIRequestFactory |
| 4 | + |
| 5 | +from kernelCI_app.views.treeDetailsSummaryView import TreeDetailsSummary |
| 6 | +from kernelCI_app.tests.unitTests.helpers.fixtures.tree_details_data import create_row |
| 7 | + |
| 8 | + |
| 9 | +COMMIT_HASH = "abc123def456rollupfallback" |
| 10 | + |
| 11 | +BASE_BUILD_ROW: dict[str, object] = { |
| 12 | + "build_id": "build_fallback_001", |
| 13 | + "build_origin": "maestro", |
| 14 | + "build_comment": None, |
| 15 | + "build_start_time": None, |
| 16 | + "build_duration": None, |
| 17 | + "build_architecture": "x86_64", |
| 18 | + "build_command": None, |
| 19 | + "build_compiler": "gcc-12", |
| 20 | + "build_config_name": "defconfig", |
| 21 | + "build_config_url": None, |
| 22 | + "build_log_url": None, |
| 23 | + "build_status": "PASS", |
| 24 | + "build_misc": None, |
| 25 | + "checkout_id": "checkout_fallback_001", |
| 26 | + "checkout_git_repository_url": "https://git.kernel.org", |
| 27 | + "checkout_git_repository_branch": "for-kernelci", |
| 28 | + "checkout_git_commit_tags": [], |
| 29 | + "checkout_origin": "maestro", |
| 30 | + "incident_id": None, |
| 31 | + "incident_test_id": None, |
| 32 | + "incident_present": None, |
| 33 | + "issue_id": None, |
| 34 | + "issue_version": None, |
| 35 | + "issue_comment": None, |
| 36 | + "issue_report_url": None, |
| 37 | +} |
| 38 | + |
| 39 | +# Legacy row representing a boot test (PASS, no issue) |
| 40 | +LEGACY_BOOT_ROW = create_row( |
| 41 | + test_id="legacy_boot_001", |
| 42 | + test_path="boot.test", |
| 43 | + test_status="PASS", |
| 44 | + incident_test_id="legacy_boot_001", |
| 45 | + issue_id=None, |
| 46 | + issue_version=None, |
| 47 | +) |
| 48 | + |
| 49 | +# Legacy row representing a non-boot test (FAIL, no linked issue → unknown issue) |
| 50 | +LEGACY_TEST_ROW = create_row( |
| 51 | + test_id="legacy_test_001", |
| 52 | + test_path="test.something", |
| 53 | + test_status="FAIL", |
| 54 | + incident_test_id="legacy_test_001", |
| 55 | + issue_id=None, |
| 56 | + issue_version=None, |
| 57 | +) |
| 58 | + |
| 59 | + |
| 60 | +class TestTreeDetailsSummaryRollupFallback(SimpleTestCase): |
| 61 | + """Tests for the rollup-empty fallback path in TreeDetailsSummary.""" |
| 62 | + |
| 63 | + def setUp(self): |
| 64 | + self.factory = APIRequestFactory() |
| 65 | + self.view = TreeDetailsSummary() |
| 66 | + self.url = f"/api/tree/{COMMIT_HASH}/summary" |
| 67 | + self.base_query = { |
| 68 | + "origin": "maestro", |
| 69 | + "git_url": "https://git.kernel.org", |
| 70 | + "git_branch": "for-kernelci", |
| 71 | + } |
| 72 | + |
| 73 | + def _make_request(self): |
| 74 | + return self.factory.get(self.url, self.base_query) |
| 75 | + |
| 76 | + @patch("kernelCI_app.views.treeDetailsSummaryView.out") |
| 77 | + @patch("kernelCI_app.views.treeDetailsSummaryView.get_tree_details_data") |
| 78 | + @patch("kernelCI_app.views.treeDetailsSummaryView.get_tree_details_builds") |
| 79 | + @patch("kernelCI_app.views.treeDetailsSummaryView.get_tree_details_rollup") |
| 80 | + def test_fallback_to_legacy_when_rollup_empty( |
| 81 | + self, |
| 82 | + mock_get_rollup, |
| 83 | + mock_get_builds, |
| 84 | + mock_get_legacy_data, |
| 85 | + mock_out, |
| 86 | + ): |
| 87 | + """When rollup is empty the endpoint falls back to legacy data and returns |
| 88 | + non-empty boot/test summaries.""" |
| 89 | + mock_get_rollup.return_value = [] |
| 90 | + mock_get_builds.return_value = [BASE_BUILD_ROW] |
| 91 | + mock_get_legacy_data.return_value = [LEGACY_BOOT_ROW, LEGACY_TEST_ROW] |
| 92 | + |
| 93 | + response = self.view.get(self._make_request(), commit_hash=COMMIT_HASH) |
| 94 | + |
| 95 | + self.assertEqual(response.status_code, 200) |
| 96 | + self.assertNotIn("error", response.data) |
| 97 | + |
| 98 | + # Fallback was triggered and logged |
| 99 | + mock_out.assert_called_once() |
| 100 | + |
| 101 | + # Boot summary is populated from legacy rows |
| 102 | + boot_status = response.data["summary"]["boots"]["status"] |
| 103 | + self.assertGreater(sum(boot_status.values()), 0) |
| 104 | + |
| 105 | + # Test summary is populated from legacy rows |
| 106 | + test_status = response.data["summary"]["tests"]["status"] |
| 107 | + self.assertGreater(sum(test_status.values()), 0) |
| 108 | + |
| 109 | + # Build summary is still correct (from _sanitize_builds_rows, not legacy) |
| 110 | + build_status = response.data["summary"]["builds"]["status"] |
| 111 | + self.assertGreater(sum(build_status.values()), 0) |
| 112 | + |
| 113 | + @patch("kernelCI_app.views.treeDetailsSummaryView.out") |
| 114 | + @patch("kernelCI_app.views.treeDetailsSummaryView.get_tree_details_data") |
| 115 | + @patch("kernelCI_app.views.treeDetailsSummaryView.get_tree_details_builds") |
| 116 | + @patch("kernelCI_app.views.treeDetailsSummaryView.get_tree_details_rollup") |
| 117 | + def test_build_filter_flags_not_contaminated_by_legacy_test_rows( |
| 118 | + self, |
| 119 | + mock_get_rollup, |
| 120 | + mock_get_builds, |
| 121 | + mock_get_legacy_data, |
| 122 | + mock_out, |
| 123 | + ): |
| 124 | + """Build filter metadata must not be altered by test-incident rows processed |
| 125 | + in the legacy fallback path (fixes double-processing of build filter flags).""" |
| 126 | + # Build PASSES → has_unknown_issue for builds must stay False |
| 127 | + mock_get_rollup.return_value = [] |
| 128 | + mock_get_builds.return_value = [BASE_BUILD_ROW] |
| 129 | + # A test row that has a FAIL status with incident_test_id set (test incident, |
| 130 | + # not a build incident). Without the fix this would incorrectly set the build |
| 131 | + # has_unknown_issue flag. |
| 132 | + mock_get_legacy_data.return_value = [LEGACY_TEST_ROW] |
| 133 | + |
| 134 | + response = self.view.get(self._make_request(), commit_hash=COMMIT_HASH) |
| 135 | + |
| 136 | + self.assertEqual(response.status_code, 200) |
| 137 | + self.assertNotIn("error", response.data) |
| 138 | + |
| 139 | + # The build filter has_unknown_issue should be False: the build passed, and |
| 140 | + # the test-incident rows must not bleed into build filter metadata. |
| 141 | + builds_filter = response.data["filters"]["builds"] |
| 142 | + self.assertFalse(builds_filter["has_unknown_issue"]) |
| 143 | + |
| 144 | + @patch("kernelCI_app.views.treeDetailsSummaryView.out") |
| 145 | + @patch("kernelCI_app.views.treeDetailsSummaryView.get_tree_details_data") |
| 146 | + @patch("kernelCI_app.views.treeDetailsSummaryView.get_tree_details_builds") |
| 147 | + @patch("kernelCI_app.views.treeDetailsSummaryView.get_tree_details_rollup") |
| 148 | + def test_no_legacy_query_when_both_empty( |
| 149 | + self, |
| 150 | + mock_get_rollup, |
| 151 | + mock_get_builds, |
| 152 | + mock_get_legacy_data, |
| 153 | + mock_out, |
| 154 | + ): |
| 155 | + """When both builds and rollup are empty the guard clause returns a no-results |
| 156 | + error without attempting the legacy query.""" |
| 157 | + mock_get_rollup.return_value = [] |
| 158 | + mock_get_builds.return_value = [] |
| 159 | + |
| 160 | + response = self.view.get(self._make_request(), commit_hash=COMMIT_HASH) |
| 161 | + |
| 162 | + self.assertEqual(response.status_code, 200) |
| 163 | + self.assertIn("error", response.data) |
| 164 | + |
| 165 | + mock_get_legacy_data.assert_not_called() |
| 166 | + mock_out.assert_not_called() |
0 commit comments