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 | |
| 7 | from __future__ import absolute_import |
| 8 | from __future__ import print_function |
| 9 | from __future__ import unicode_literals |
| 10 | |
| 11 | import mock |
| 12 | import os |
| 13 | |
| 14 | from chromite.lib import cros_test_lib, osutils |
| 15 | from chromite.scripts.sysmon import git_metrics |
| 16 | |
| 17 | # pylint: disable=protected-access |
| 18 | # pylint: disable=attribute-defined-outside-init |
| 19 | |
| 20 | class TestGitMetrics(cros_test_lib.TempDirTestCase): |
| 21 | """Tests for git metrics.""" |
| 22 | |
| 23 | def setUp(self): |
| 24 | self._InitRepo() |
| 25 | |
| 26 | def _InitRepo(self): |
| 27 | """Initializes a repo in the temp directory.""" |
| 28 | self.git_dir = os.path.join(self.tempdir, '.git') |
| 29 | # with osutils.ChdirContext(self.tempdir): |
| 30 | self.git_repo = git_metrics._GitRepo(self.git_dir) |
| 31 | self.git_repo._check_output(['init']) |
| 32 | self.git_repo._check_output(['commit', '--allow-empty', '-m', 'hi']) |
| 33 | |
| 34 | def testCollectGitHashTypesAreCorrect(self): |
| 35 | """Tests that collecting the git hash doesn't have type conflicts.""" |
| 36 | collector = git_metrics._GitMetricCollector(self.git_dir, '/foo/bar') |
| 37 | |
| 38 | # This has the side-effect of checking the types are correct. |
| 39 | collector._collect_commit_hash_metric() |
| 40 | |
| 41 | def testCollectGitTimeTypesAreCorrect(self): |
| 42 | """Tests that collecting the git commit time works.""" |
| 43 | collector = git_metrics._GitMetricCollector(self.git_dir, '/foo/bar') |
| 44 | |
| 45 | # This has the side-effect of checking the types are correct. |
Allen Li | 3a2e180 | 2017-04-10 12:48:36 -0700 | [diff] [blame^] | 46 | collector._collect_timestamp_metric() |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 47 | |
| 48 | def testCollectGitHashCallsSet(self): |
| 49 | collector = git_metrics._GitMetricCollector(self.git_dir, '/foo/bar') |
| 50 | |
| 51 | with mock.patch.object(git_metrics._GitMetricCollector, |
| 52 | '_commit_hash_metric', |
| 53 | autospec=True) as hash_metric: |
| 54 | collector._collect_commit_hash_metric() |
| 55 | |
| 56 | commit_hash = self.git_repo.get_commit_hash() |
| 57 | self.assertEqual(hash_metric.set.call_args_list, [ |
| 58 | mock.call(commit_hash, {'repo': self.git_dir}) |
| 59 | ]) |
| 60 | |
| 61 | def testCollectGitTimeCallsSet(self): |
| 62 | collector = git_metrics._GitMetricCollector(self.git_dir, '/foo/bar') |
| 63 | with mock.patch.object(git_metrics._GitMetricCollector, |
Allen Li | 3a2e180 | 2017-04-10 12:48:36 -0700 | [diff] [blame^] | 64 | '_timestamp_metric', |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 65 | autospec=True) as time_metric: |
Allen Li | 3a2e180 | 2017-04-10 12:48:36 -0700 | [diff] [blame^] | 66 | collector._collect_timestamp_metric() |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 67 | |
Allen Li | 3a2e180 | 2017-04-10 12:48:36 -0700 | [diff] [blame^] | 68 | timestamp = self.git_repo.get_commit_time() |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 69 | self.assertEqual(time_metric.set.call_args_list, [ |
Allen Li | 3a2e180 | 2017-04-10 12:48:36 -0700 | [diff] [blame^] | 70 | mock.call(timestamp, {'repo': self.git_dir}) |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 71 | ]) |