From a5b40a28450c965bb4b77656820fdd0a78768fe4 Mon Sep 17 00:00:00 2001 From: Raman Tenneti Date: Tue, 16 Mar 2021 14:24:14 -0700 Subject: [PATCH] repo: Add a new "command" event type to git trace2 logging in repo. Add a new "event": "command", which is emitted at when all command arguments have been processed. Additional fields: "name": Name of the primary command (ex: repo, git) "subcommands"': List of the sub-commands once command-line arguments are processed Examples: Command: repo --version Event: {"event": "command", , "name": "repo", "subcommands": ["version"] } Bug: [google internal] b/178507266 Testing: - Unit tests - Verified repo git trace2 logs had expected data Change-Id: I825bd0ecedee45135382461a4ba10f987f09aef3 Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/300343 Reviewed-by: Ian Kasprzak Reviewed-by: Mike Frysinger Tested-by: Raman Tenneti --- git_trace2_event_log.py | 12 ++++++++++++ main.py | 1 + tests/test_git_trace2_event_log.py | 24 ++++++++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/git_trace2_event_log.py b/git_trace2_event_log.py index 8c33d80b..8f12d1a9 100644 --- a/git_trace2_event_log.py +++ b/git_trace2_event_log.py @@ -132,6 +132,18 @@ class EventLog(object): exit_event['code'] = result self._log.append(exit_event) + def CommandEvent(self, name, subcommands): + """Append a 'command' event to the current log. + + Args: + name: Name of the primary command (ex: repo, git) + subcommands: List of the sub-commands (ex: version, init, sync) + """ + command_event = self._CreateEventDict('command') + command_event['name'] = name + command_event['subcommands'] = subcommands + self._log.append(command_event) + def DefParamRepoEvents(self, config): """Append a 'def_param' event for each repo.* config key to the current log. diff --git a/main.py b/main.py index ba5d9d20..9abda6a9 100755 --- a/main.py +++ b/main.py @@ -254,6 +254,7 @@ class _Repo(object): cmd_event = cmd.event_log.Add(name, event_log.TASK_COMMAND, start) cmd.event_log.SetParent(cmd_event) git_trace2_event_log.StartEvent() + git_trace2_event_log.CommandEvent(name='repo', subcommands=[name]) try: cmd.ValidateOptions(copts, cargs) diff --git a/tests/test_git_trace2_event_log.py b/tests/test_git_trace2_event_log.py index 3c5cb150..4a3a4c48 100644 --- a/tests/test_git_trace2_event_log.py +++ b/tests/test_git_trace2_event_log.py @@ -161,6 +161,30 @@ class EventLogTestCase(unittest.TestCase): self.assertIn('code', exit_event) self.assertEqual(exit_event['code'], 2) + def test_command_event(self): + """Test and validate 'command' event data is valid. + + Expected event log: + + + """ + name = 'repo' + subcommands = ['init' 'this'] + self._event_log_module.CommandEvent(name='repo', subcommands=subcommands) + with tempfile.TemporaryDirectory(prefix='event_log_tests') as tempdir: + log_path = self._event_log_module.Write(path=tempdir) + self._log_data = self.readLog(log_path) + + self.assertEqual(len(self._log_data), 2) + command_event = self._log_data[1] + self.verifyCommonKeys(self._log_data[0], expected_event_name='version') + self.verifyCommonKeys(command_event, expected_event_name='command') + # Check for 'command' event specific fields. + self.assertIn('name', command_event) + self.assertIn('subcommands', command_event) + self.assertEqual(command_event['name'], name) + self.assertEqual(command_event['subcommands'], subcommands) + def test_def_params_event_repo_config(self): """Test 'def_params' event data outputs only repo config keys.