Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 1 | # Copyright 2016 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Unit tests for git_metrics.""" |
| 6 | |
Allen Li | 060e84c | 2017-07-12 18:42:48 -0700 | [diff] [blame] | 7 | # pylint: disable=protected-access |
| 8 | |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 9 | from __future__ import absolute_import |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 10 | |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 11 | import os |
Allen Li | 060e84c | 2017-07-12 18:42:48 -0700 | [diff] [blame] | 12 | import subprocess |
Mike Frysinger | 166fea0 | 2021-02-12 05:30:33 -0500 | [diff] [blame] | 13 | from unittest import mock |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 14 | |
Allen Li | 060e84c | 2017-07-12 18:42:48 -0700 | [diff] [blame] | 15 | from chromite.lib import cros_test_lib |
| 16 | from chromite.lib import osutils |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 17 | from chromite.scripts.sysmon import git_metrics |
| 18 | |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 19 | |
Allen Li | 060e84c | 2017-07-12 18:42:48 -0700 | [diff] [blame] | 20 | class TestGitMetricCollector(cros_test_lib.TestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 21 | """Tests for _GitMetricCollector.""" |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 22 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 23 | def setUp(self): |
| 24 | patcher = mock.patch( |
| 25 | "chromite.third_party.infra_libs.ts_mon.common.interface.state.store", |
| 26 | autospec=True, |
| 27 | ) |
| 28 | self.store = patcher.start() |
| 29 | self.addCleanup(patcher.stop) |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 30 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 31 | def test_collect(self): |
| 32 | with mock.patch.object( |
| 33 | git_metrics, "_GitRepo", autospec=True |
| 34 | ) as _GitRepo: |
| 35 | instance = _GitRepo("stub") |
| 36 | instance.get_commit_hash.return_value = ( |
| 37 | "2b1ce059425edc91e013c260e59019195f927a07" |
| 38 | ) |
| 39 | instance.get_commit_time.return_value = 1483257600 |
| 40 | instance.get_unstaged_changes.return_value = (0, 3) |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 41 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 42 | collector = git_metrics._GitMetricCollector("~/solciel", "stub") |
| 43 | collector.collect() |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 44 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 45 | setter = self.store.set |
| 46 | calls = [ |
| 47 | mock.call( |
| 48 | "git/hash", |
| 49 | ("~/solciel",), |
| 50 | None, |
| 51 | "2b1ce059425edc91e013c260e59019195f927a07", |
| 52 | enforce_ge=mock.ANY, |
| 53 | ), |
| 54 | mock.call( |
| 55 | "git/timestamp", |
| 56 | ("~/solciel",), |
| 57 | None, |
| 58 | 1483257600, |
| 59 | enforce_ge=mock.ANY, |
| 60 | ), |
| 61 | mock.call( |
| 62 | "git/unstaged_changes", |
| 63 | ("added", "~/solciel"), |
| 64 | None, |
| 65 | 0, |
| 66 | enforce_ge=mock.ANY, |
| 67 | ), |
| 68 | mock.call( |
| 69 | "git/unstaged_changes", |
| 70 | ("deleted", "~/solciel"), |
| 71 | None, |
| 72 | 3, |
| 73 | enforce_ge=mock.ANY, |
| 74 | ), |
| 75 | ] |
| 76 | setter.assert_has_calls(calls) |
| 77 | self.assertEqual(len(setter.mock_calls), len(calls)) |
Allen Li | bdb9f04 | 2017-04-10 13:25:47 -0700 | [diff] [blame] | 78 | |
| 79 | |
Allen Li | 8934e04 | 2017-06-21 15:54:42 -0700 | [diff] [blame] | 80 | class TestGitRepoWithTempdir(cros_test_lib.TempDirTestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 81 | """Tests for _GitRepo using a Git fixture.""" |
Allen Li | 8934e04 | 2017-06-21 15:54:42 -0700 | [diff] [blame] | 82 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 83 | def setUp(self): |
| 84 | self.git_dir = os.path.join(self.tempdir, ".git") |
Allen Li | 060e84c | 2017-07-12 18:42:48 -0700 | [diff] [blame] | 85 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 86 | def call(args, **kwargs): |
| 87 | subprocess.check_call( |
| 88 | args, |
| 89 | stdout=subprocess.DEVNULL, |
| 90 | stderr=subprocess.DEVNULL, |
| 91 | **kwargs, |
| 92 | ) |
Allen Li | 060e84c | 2017-07-12 18:42:48 -0700 | [diff] [blame] | 93 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 94 | with osutils.ChdirContext(self.tempdir): |
| 95 | call(["git", "init"]) |
| 96 | call(["git", "config", "user.name", "John Doe"]) |
| 97 | call(["git", "config", "user.email", "john@example.com"]) |
| 98 | with open("foo", "w") as f: |
| 99 | f.write("a\nb\nc\n") |
| 100 | call(["git", "add", "foo"]) |
| 101 | env = os.environ.copy() |
| 102 | env["GIT_AUTHOR_DATE"] = "2017-01-01T00:00:00Z" |
| 103 | env["GIT_COMMITTER_DATE"] = "2017-01-01T00:00:00Z" |
| 104 | call(["git", "commit", "-m", "Initial commit"], env=env) |
Allen Li | 060e84c | 2017-07-12 18:42:48 -0700 | [diff] [blame] | 105 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 106 | def test_get_commit_hash(self): |
| 107 | """Test get_commit_hash().""" |
| 108 | repo = git_metrics._GitRepo(self.git_dir) |
| 109 | got = repo.get_commit_hash() |
| 110 | self.assertEqual(got, "7c88f131e520e8455e2403b88ff4f723758c5dd6") |
Allen Li | 060e84c | 2017-07-12 18:42:48 -0700 | [diff] [blame] | 111 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 112 | def test_get_commit_time(self): |
| 113 | """Test get_commit_time().""" |
| 114 | repo = git_metrics._GitRepo(self.git_dir) |
| 115 | got = repo.get_commit_time() |
| 116 | self.assertEqual(got, 1483228800) |
Allen Li | 8934e04 | 2017-06-21 15:54:42 -0700 | [diff] [blame] | 117 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 118 | def test_get_unstaged_changes(self): |
| 119 | """Test get_unstaged_changes().""" |
| 120 | with open(os.path.join(self.tempdir, "spam"), "w") as f: |
| 121 | f.write("a\n") |
| 122 | os.remove(os.path.join(self.tempdir, "foo")) |
| 123 | repo = git_metrics._GitRepo(self.git_dir) |
| 124 | added, removed = repo.get_unstaged_changes() |
| 125 | self.assertEqual(added, 0) # Does not count untracked files |
| 126 | self.assertEqual(removed, 3) |