diff options
Diffstat (limited to 'tests/test_git_superproject.py')
-rw-r--r-- | tests/test_git_superproject.py | 134 |
1 files changed, 116 insertions, 18 deletions
diff --git a/tests/test_git_superproject.py b/tests/test_git_superproject.py index ba61a3d1..d612f4e7 100644 --- a/tests/test_git_superproject.py +++ b/tests/test_git_superproject.py | |||
@@ -14,6 +14,7 @@ | |||
14 | 14 | ||
15 | """Unittests for the git_superproject.py module.""" | 15 | """Unittests for the git_superproject.py module.""" |
16 | 16 | ||
17 | import json | ||
17 | import os | 18 | import os |
18 | import platform | 19 | import platform |
19 | import tempfile | 20 | import tempfile |
@@ -21,6 +22,7 @@ import unittest | |||
21 | from unittest import mock | 22 | from unittest import mock |
22 | 23 | ||
23 | import git_superproject | 24 | import git_superproject |
25 | import git_trace2_event_log | ||
24 | import manifest_xml | 26 | import manifest_xml |
25 | import platform_utils | 27 | import platform_utils |
26 | from test_manifest_xml import sort_attributes | 28 | from test_manifest_xml import sort_attributes |
@@ -29,6 +31,11 @@ from test_manifest_xml import sort_attributes | |||
29 | class SuperprojectTestCase(unittest.TestCase): | 31 | class SuperprojectTestCase(unittest.TestCase): |
30 | """TestCase for the Superproject module.""" | 32 | """TestCase for the Superproject module.""" |
31 | 33 | ||
34 | PARENT_SID_KEY = 'GIT_TRACE2_PARENT_SID' | ||
35 | PARENT_SID_VALUE = 'parent_sid' | ||
36 | SELF_SID_REGEX = r'repo-\d+T\d+Z-.*' | ||
37 | FULL_SID_REGEX = r'^%s/%s' % (PARENT_SID_VALUE, SELF_SID_REGEX) | ||
38 | |||
32 | def setUp(self): | 39 | def setUp(self): |
33 | """Set up superproject every time.""" | 40 | """Set up superproject every time.""" |
34 | self.tempdir = tempfile.mkdtemp(prefix='repo_tests') | 41 | self.tempdir = tempfile.mkdtemp(prefix='repo_tests') |
@@ -38,6 +45,13 @@ class SuperprojectTestCase(unittest.TestCase): | |||
38 | os.mkdir(self.repodir) | 45 | os.mkdir(self.repodir) |
39 | self.platform = platform.system().lower() | 46 | self.platform = platform.system().lower() |
40 | 47 | ||
48 | # By default we initialize with the expected case where | ||
49 | # repo launches us (so GIT_TRACE2_PARENT_SID is set). | ||
50 | env = { | ||
51 | self.PARENT_SID_KEY: self.PARENT_SID_VALUE, | ||
52 | } | ||
53 | self.git_event_log = git_trace2_event_log.EventLog(env=env) | ||
54 | |||
41 | # The manifest parsing really wants a git repo currently. | 55 | # The manifest parsing really wants a git repo currently. |
42 | gitdir = os.path.join(self.repodir, 'manifests.git') | 56 | gitdir = os.path.join(self.repodir, 'manifests.git') |
43 | os.mkdir(gitdir) | 57 | os.mkdir(gitdir) |
@@ -54,7 +68,8 @@ class SuperprojectTestCase(unittest.TestCase): | |||
54 | <project path="art" name="platform/art" groups="notdefault,platform-""" + self.platform + """ | 68 | <project path="art" name="platform/art" groups="notdefault,platform-""" + self.platform + """ |
55 | " /></manifest> | 69 | " /></manifest> |
56 | """) | 70 | """) |
57 | self._superproject = git_superproject.Superproject(manifest, self.repodir) | 71 | self._superproject = git_superproject.Superproject(manifest, self.repodir, |
72 | self.git_event_log) | ||
58 | 73 | ||
59 | def tearDown(self): | 74 | def tearDown(self): |
60 | """Tear down superproject every time.""" | 75 | """Tear down superproject every time.""" |
@@ -66,14 +81,56 @@ class SuperprojectTestCase(unittest.TestCase): | |||
66 | fp.write(data) | 81 | fp.write(data) |
67 | return manifest_xml.XmlManifest(self.repodir, self.manifest_file) | 82 | return manifest_xml.XmlManifest(self.repodir, self.manifest_file) |
68 | 83 | ||
84 | def verifyCommonKeys(self, log_entry, expected_event_name, full_sid=True): | ||
85 | """Helper function to verify common event log keys.""" | ||
86 | self.assertIn('event', log_entry) | ||
87 | self.assertIn('sid', log_entry) | ||
88 | self.assertIn('thread', log_entry) | ||
89 | self.assertIn('time', log_entry) | ||
90 | |||
91 | # Do basic data format validation. | ||
92 | self.assertEqual(expected_event_name, log_entry['event']) | ||
93 | if full_sid: | ||
94 | self.assertRegex(log_entry['sid'], self.FULL_SID_REGEX) | ||
95 | else: | ||
96 | self.assertRegex(log_entry['sid'], self.SELF_SID_REGEX) | ||
97 | self.assertRegex(log_entry['time'], r'^\d+-\d+-\d+T\d+:\d+:\d+\.\d+Z$') | ||
98 | |||
99 | def readLog(self, log_path): | ||
100 | """Helper function to read log data into a list.""" | ||
101 | log_data = [] | ||
102 | with open(log_path, mode='rb') as f: | ||
103 | for line in f: | ||
104 | log_data.append(json.loads(line)) | ||
105 | return log_data | ||
106 | |||
107 | def verifyErrorEvent(self): | ||
108 | """Helper to verify that error event is written.""" | ||
109 | |||
110 | with tempfile.TemporaryDirectory(prefix='event_log_tests') as tempdir: | ||
111 | log_path = self.git_event_log.Write(path=tempdir) | ||
112 | self.log_data = self.readLog(log_path) | ||
113 | |||
114 | self.assertEqual(len(self.log_data), 2) | ||
115 | error_event = self.log_data[1] | ||
116 | self.verifyCommonKeys(self.log_data[0], expected_event_name='version') | ||
117 | self.verifyCommonKeys(error_event, expected_event_name='error') | ||
118 | # Check for 'error' event specific fields. | ||
119 | self.assertIn('msg', error_event) | ||
120 | self.assertIn('fmt', error_event) | ||
121 | |||
69 | def test_superproject_get_superproject_no_superproject(self): | 122 | def test_superproject_get_superproject_no_superproject(self): |
70 | """Test with no url.""" | 123 | """Test with no url.""" |
71 | manifest = self.getXmlManifest(""" | 124 | manifest = self.getXmlManifest(""" |
72 | <manifest> | 125 | <manifest> |
73 | </manifest> | 126 | </manifest> |
74 | """) | 127 | """) |
75 | superproject = git_superproject.Superproject(manifest, self.repodir) | 128 | superproject = git_superproject.Superproject(manifest, self.repodir, self.git_event_log) |
76 | self.assertFalse(superproject.Sync()) | 129 | # Test that exit condition is false when there is no superproject tag. |
130 | sync_result = superproject.Sync() | ||
131 | self.assertFalse(sync_result.success) | ||
132 | self.assertFalse(sync_result.fatal) | ||
133 | self.verifyErrorEvent() | ||
77 | 134 | ||
78 | def test_superproject_get_superproject_invalid_url(self): | 135 | def test_superproject_get_superproject_invalid_url(self): |
79 | """Test with an invalid url.""" | 136 | """Test with an invalid url.""" |
@@ -84,8 +141,10 @@ class SuperprojectTestCase(unittest.TestCase): | |||
84 | <superproject name="superproject"/> | 141 | <superproject name="superproject"/> |
85 | </manifest> | 142 | </manifest> |
86 | """) | 143 | """) |
87 | superproject = git_superproject.Superproject(manifest, self.repodir) | 144 | superproject = git_superproject.Superproject(manifest, self.repodir, self.git_event_log) |
88 | self.assertFalse(superproject.Sync()) | 145 | sync_result = superproject.Sync() |
146 | self.assertFalse(sync_result.success) | ||
147 | self.assertTrue(sync_result.fatal) | ||
89 | 148 | ||
90 | def test_superproject_get_superproject_invalid_branch(self): | 149 | def test_superproject_get_superproject_invalid_branch(self): |
91 | """Test with an invalid branch.""" | 150 | """Test with an invalid branch.""" |
@@ -96,21 +155,28 @@ class SuperprojectTestCase(unittest.TestCase): | |||
96 | <superproject name="superproject"/> | 155 | <superproject name="superproject"/> |
97 | </manifest> | 156 | </manifest> |
98 | """) | 157 | """) |
99 | superproject = git_superproject.Superproject(manifest, self.repodir) | 158 | self._superproject = git_superproject.Superproject(manifest, self.repodir, |
159 | self.git_event_log) | ||
100 | with mock.patch.object(self._superproject, '_GetBranch', return_value='junk'): | 160 | with mock.patch.object(self._superproject, '_GetBranch', return_value='junk'): |
101 | self.assertFalse(superproject.Sync()) | 161 | sync_result = self._superproject.Sync() |
162 | self.assertFalse(sync_result.success) | ||
163 | self.assertTrue(sync_result.fatal) | ||
102 | 164 | ||
103 | def test_superproject_get_superproject_mock_init(self): | 165 | def test_superproject_get_superproject_mock_init(self): |
104 | """Test with _Init failing.""" | 166 | """Test with _Init failing.""" |
105 | with mock.patch.object(self._superproject, '_Init', return_value=False): | 167 | with mock.patch.object(self._superproject, '_Init', return_value=False): |
106 | self.assertFalse(self._superproject.Sync()) | 168 | sync_result = self._superproject.Sync() |
169 | self.assertFalse(sync_result.success) | ||
170 | self.assertTrue(sync_result.fatal) | ||
107 | 171 | ||
108 | def test_superproject_get_superproject_mock_fetch(self): | 172 | def test_superproject_get_superproject_mock_fetch(self): |
109 | """Test with _Fetch failing.""" | 173 | """Test with _Fetch failing.""" |
110 | with mock.patch.object(self._superproject, '_Init', return_value=True): | 174 | with mock.patch.object(self._superproject, '_Init', return_value=True): |
111 | os.mkdir(self._superproject._superproject_path) | 175 | os.mkdir(self._superproject._superproject_path) |
112 | with mock.patch.object(self._superproject, '_Fetch', return_value=False): | 176 | with mock.patch.object(self._superproject, '_Fetch', return_value=False): |
113 | self.assertFalse(self._superproject.Sync()) | 177 | sync_result = self._superproject.Sync() |
178 | self.assertFalse(sync_result.success) | ||
179 | self.assertTrue(sync_result.fatal) | ||
114 | 180 | ||
115 | def test_superproject_get_all_project_commit_ids_mock_ls_tree(self): | 181 | def test_superproject_get_all_project_commit_ids_mock_ls_tree(self): |
116 | """Test with LsTree being a mock.""" | 182 | """Test with LsTree being a mock.""" |
@@ -122,12 +188,13 @@ class SuperprojectTestCase(unittest.TestCase): | |||
122 | with mock.patch.object(self._superproject, '_Init', return_value=True): | 188 | with mock.patch.object(self._superproject, '_Init', return_value=True): |
123 | with mock.patch.object(self._superproject, '_Fetch', return_value=True): | 189 | with mock.patch.object(self._superproject, '_Fetch', return_value=True): |
124 | with mock.patch.object(self._superproject, '_LsTree', return_value=data): | 190 | with mock.patch.object(self._superproject, '_LsTree', return_value=data): |
125 | commit_ids = self._superproject._GetAllProjectsCommitIds() | 191 | commit_ids_result = self._superproject._GetAllProjectsCommitIds() |
126 | self.assertEqual(commit_ids, { | 192 | self.assertEqual(commit_ids_result.commit_ids, { |
127 | 'art': '2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea', | 193 | 'art': '2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea', |
128 | 'bootable/recovery': 'e9d25da64d8d365dbba7c8ee00fe8c4473fe9a06', | 194 | 'bootable/recovery': 'e9d25da64d8d365dbba7c8ee00fe8c4473fe9a06', |
129 | 'build/bazel': 'ade9b7a0d874e25fff4bf2552488825c6f111928' | 195 | 'build/bazel': 'ade9b7a0d874e25fff4bf2552488825c6f111928' |
130 | }) | 196 | }) |
197 | self.assertFalse(commit_ids_result.fatal) | ||
131 | 198 | ||
132 | def test_superproject_write_manifest_file(self): | 199 | def test_superproject_write_manifest_file(self): |
133 | """Test with writing manifest to a file after setting revisionId.""" | 200 | """Test with writing manifest to a file after setting revisionId.""" |
@@ -163,9 +230,10 @@ class SuperprojectTestCase(unittest.TestCase): | |||
163 | return_value=data): | 230 | return_value=data): |
164 | # Create temporary directory so that it can write the file. | 231 | # Create temporary directory so that it can write the file. |
165 | os.mkdir(self._superproject._superproject_path) | 232 | os.mkdir(self._superproject._superproject_path) |
166 | manifest_path = self._superproject.UpdateProjectsRevisionId(projects) | 233 | update_result = self._superproject.UpdateProjectsRevisionId(projects) |
167 | self.assertIsNotNone(manifest_path) | 234 | self.assertIsNotNone(update_result.manifest_path) |
168 | with open(manifest_path, 'r') as fp: | 235 | self.assertFalse(update_result.fatal) |
236 | with open(update_result.manifest_path, 'r') as fp: | ||
169 | manifest_xml_data = fp.read() | 237 | manifest_xml_data = fp.read() |
170 | self.assertEqual( | 238 | self.assertEqual( |
171 | sort_attributes(manifest_xml_data), | 239 | sort_attributes(manifest_xml_data), |
@@ -178,6 +246,34 @@ class SuperprojectTestCase(unittest.TestCase): | |||
178 | '<superproject name="superproject"/>' | 246 | '<superproject name="superproject"/>' |
179 | '</manifest>') | 247 | '</manifest>') |
180 | 248 | ||
249 | def test_superproject_update_project_revision_id_no_superproject_tag(self): | ||
250 | """Test update of commit ids of a manifest without superproject tag.""" | ||
251 | manifest = self.getXmlManifest(""" | ||
252 | <manifest> | ||
253 | <remote name="default-remote" fetch="http://localhost" /> | ||
254 | <default remote="default-remote" revision="refs/heads/main" /> | ||
255 | <project name="test-name"/> | ||
256 | </manifest> | ||
257 | """) | ||
258 | self.maxDiff = None | ||
259 | self._superproject = git_superproject.Superproject(manifest, self.repodir, | ||
260 | self.git_event_log) | ||
261 | self.assertEqual(len(self._superproject._manifest.projects), 1) | ||
262 | projects = self._superproject._manifest.projects | ||
263 | project = projects[0] | ||
264 | project.SetRevisionId('ABCDEF') | ||
265 | update_result = self._superproject.UpdateProjectsRevisionId(projects) | ||
266 | self.assertIsNone(update_result.manifest_path) | ||
267 | self.assertFalse(update_result.fatal) | ||
268 | self.verifyErrorEvent() | ||
269 | self.assertEqual( | ||
270 | sort_attributes(manifest.ToXml().toxml()), | ||
271 | '<?xml version="1.0" ?><manifest>' | ||
272 | '<remote fetch="http://localhost" name="default-remote"/>' | ||
273 | '<default remote="default-remote" revision="refs/heads/main"/>' | ||
274 | '<project name="test-name" revision="ABCDEF"/>' | ||
275 | '</manifest>') | ||
276 | |||
181 | def test_superproject_update_project_revision_id_from_local_manifest_group(self): | 277 | def test_superproject_update_project_revision_id_from_local_manifest_group(self): |
182 | """Test update of commit ids of a manifest that have local manifest no superproject group.""" | 278 | """Test update of commit ids of a manifest that have local manifest no superproject group.""" |
183 | local_group = manifest_xml.LOCAL_MANIFEST_GROUP_PREFIX + ':local' | 279 | local_group = manifest_xml.LOCAL_MANIFEST_GROUP_PREFIX + ':local' |
@@ -194,7 +290,8 @@ class SuperprojectTestCase(unittest.TestCase): | |||
194 | " /></manifest> | 290 | " /></manifest> |
195 | """) | 291 | """) |
196 | self.maxDiff = None | 292 | self.maxDiff = None |
197 | self._superproject = git_superproject.Superproject(manifest, self.repodir) | 293 | self._superproject = git_superproject.Superproject(manifest, self.repodir, |
294 | self.git_event_log) | ||
198 | self.assertEqual(len(self._superproject._manifest.projects), 2) | 295 | self.assertEqual(len(self._superproject._manifest.projects), 2) |
199 | projects = self._superproject._manifest.projects | 296 | projects = self._superproject._manifest.projects |
200 | data = ('160000 commit 2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea\tart\x00' | 297 | data = ('160000 commit 2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea\tart\x00' |
@@ -206,9 +303,10 @@ class SuperprojectTestCase(unittest.TestCase): | |||
206 | return_value=data): | 303 | return_value=data): |
207 | # Create temporary directory so that it can write the file. | 304 | # Create temporary directory so that it can write the file. |
208 | os.mkdir(self._superproject._superproject_path) | 305 | os.mkdir(self._superproject._superproject_path) |
209 | manifest_path = self._superproject.UpdateProjectsRevisionId(projects) | 306 | update_result = self._superproject.UpdateProjectsRevisionId(projects) |
210 | self.assertIsNotNone(manifest_path) | 307 | self.assertIsNotNone(update_result.manifest_path) |
211 | with open(manifest_path, 'r') as fp: | 308 | self.assertFalse(update_result.fatal) |
309 | with open(update_result.manifest_path, 'r') as fp: | ||
212 | manifest_xml_data = fp.read() | 310 | manifest_xml_data = fp.read() |
213 | # Verify platform/vendor/x's project revision hasn't changed. | 311 | # Verify platform/vendor/x's project revision hasn't changed. |
214 | self.assertEqual( | 312 | self.assertEqual( |