From 956f7363d100abe6c1f58b36d7aea59b9e41cd04 Mon Sep 17 00:00:00 2001 From: Raman Tenneti Date: Mon, 27 Sep 2021 10:55:44 -0700 Subject: [PATCH] superproject: Log argv parameter of syncstate as 'data-json'. Fixed: "we need to make a special case for logging the argv; it should probably be a "data-json" event so that we log this directly as an array rather than an encoded string. Tested: $ repo_dev sync Verified event logged for argv is "data-json". $./run_tests Bug: [google internal] b/201102002 Change-Id: I18ccec79c73c8dc931cb8afc472b2361db8aea4c Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/319055 Reviewed-by: Josh Steadmon Reviewed-by: Xin Li Tested-by: Raman Tenneti --- git_trace2_event_log.py | 6 +++++- tests/test_git_trace2_event_log.py | 11 +++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/git_trace2_event_log.py b/git_trace2_event_log.py index 92c4cc6b..0d4f23f4 100644 --- a/git_trace2_event_log.py +++ b/git_trace2_event_log.py @@ -167,6 +167,10 @@ class EventLog(object): repo_config = {k: v for k, v in config.items() if k.startswith('repo.')} self.LogConfigEvents(repo_config, 'def_param') + def GetDataEventName(self, key): + """Returns the 'data-json' if the key is argv else returns 'data'.""" + return 'data-json' if key.endswith('sys.argv') else 'data' + def LogDataConfigEvents(self, config, prefix): """Append a 'data' event for each config key/value in |config| to the current log. @@ -178,7 +182,7 @@ class EventLog(object): prefix: Prefix for each key that is logged. """ for key, value in config.items(): - event = self._CreateEventDict('data') + event = self._CreateEventDict(self.GetDataEventName(key)) event['key'] = f'{prefix}/{key}' event['value'] = value self._log.append(event) diff --git a/tests/test_git_trace2_event_log.py b/tests/test_git_trace2_event_log.py index a5a6cbba..6131c7b3 100644 --- a/tests/test_git_trace2_event_log.py +++ b/tests/test_git_trace2_event_log.py @@ -42,7 +42,7 @@ class EventLogTestCase(unittest.TestCase): self._event_log_module = git_trace2_event_log.EventLog(env=env) self._log_data = None - def verifyCommonKeys(self, log_entry, expected_event_name, full_sid=True): + def verifyCommonKeys(self, log_entry, expected_event_name=None, full_sid=True): """Helper function to verify common event log keys.""" self.assertIn('event', log_entry) self.assertIn('sid', log_entry) @@ -50,7 +50,8 @@ class EventLogTestCase(unittest.TestCase): self.assertIn('time', log_entry) # Do basic data format validation. - self.assertEqual(expected_event_name, log_entry['event']) + if expected_event_name: + self.assertEqual(expected_event_name, log_entry['event']) if full_sid: self.assertRegex(log_entry['sid'], self.FULL_SID_REGEX) else: @@ -246,6 +247,7 @@ class EventLogTestCase(unittest.TestCase): 'git.foo': 'bar', 'repo.partialclone': 'false', 'repo.syncstate.superproject.hassuperprojecttag': 'true', + 'repo.syncstate.superproject.sys.argv': ['--', 'sync', 'protobuf'], } prefix_value = 'prefix' self._event_log_module.LogDataConfigEvents(config, prefix_value) @@ -254,17 +256,18 @@ class EventLogTestCase(unittest.TestCase): log_path = self._event_log_module.Write(path=tempdir) self._log_data = self.readLog(log_path) - self.assertEqual(len(self._log_data), 4) + self.assertEqual(len(self._log_data), 5) data_events = self._log_data[1:] self.verifyCommonKeys(self._log_data[0], expected_event_name='version') for event in data_events: - self.verifyCommonKeys(event, expected_event_name='data') + self.verifyCommonKeys(event) # Check for 'data' event specific fields. self.assertIn('key', event) self.assertIn('value', event) key = event['key'].removeprefix(f'{prefix_value}/') value = event['value'] + self.assertEqual(self._event_log_module.GetDataEventName(key), event['event']) self.assertTrue(key in config and value == config[key]) def test_error_event(self):