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 | |
Allen Li | 73b14b0 | 2017-04-10 13:18:06 -0700 | [diff] [blame] | 20 | class TestGitMetricsWithTempdir(cros_test_lib.TempDirTestCase): |
| 21 | """Tests for git metrics using a Git fixture.""" |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 22 | |
| 23 | def setUp(self): |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 24 | self.git_dir = os.path.join(self.tempdir, '.git') |
Allen Li | 5fc64b3 | 2017-04-10 12:53:47 -0700 | [diff] [blame] | 25 | with osutils.ChdirContext(self.tempdir): |
| 26 | self.git_repo = git_metrics._GitRepo(self.git_dir) |
| 27 | self.git_repo._check_output(['init']) |
| 28 | self.git_repo._check_output(['commit', '--allow-empty', '-m', 'hi']) |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 29 | |
Allen Li | 512d704 | 2017-04-10 13:14:22 -0700 | [diff] [blame] | 30 | def test_collect_git_hash_types_are_correct(self): |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 31 | """Tests that collecting the git hash doesn't have type conflicts.""" |
| 32 | collector = git_metrics._GitMetricCollector(self.git_dir, '/foo/bar') |
| 33 | |
| 34 | # This has the side-effect of checking the types are correct. |
| 35 | collector._collect_commit_hash_metric() |
| 36 | |
Allen Li | 512d704 | 2017-04-10 13:14:22 -0700 | [diff] [blame] | 37 | def test_collect_git_time_types_are_correct(self): |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 38 | """Tests that collecting the git commit time works.""" |
| 39 | collector = git_metrics._GitMetricCollector(self.git_dir, '/foo/bar') |
| 40 | |
| 41 | # This has the side-effect of checking the types are correct. |
Allen Li | 3a2e180 | 2017-04-10 12:48:36 -0700 | [diff] [blame] | 42 | collector._collect_timestamp_metric() |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 43 | |
Allen Li | 512d704 | 2017-04-10 13:14:22 -0700 | [diff] [blame] | 44 | def test_collect_git_hash_calls_set(self): |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 45 | collector = git_metrics._GitMetricCollector(self.git_dir, '/foo/bar') |
| 46 | |
| 47 | with mock.patch.object(git_metrics._GitMetricCollector, |
| 48 | '_commit_hash_metric', |
| 49 | autospec=True) as hash_metric: |
| 50 | collector._collect_commit_hash_metric() |
| 51 | |
| 52 | commit_hash = self.git_repo.get_commit_hash() |
| 53 | self.assertEqual(hash_metric.set.call_args_list, [ |
| 54 | mock.call(commit_hash, {'repo': self.git_dir}) |
| 55 | ]) |
| 56 | |
Allen Li | 512d704 | 2017-04-10 13:14:22 -0700 | [diff] [blame] | 57 | def test_collect_git_time_calls_set(self): |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 58 | collector = git_metrics._GitMetricCollector(self.git_dir, '/foo/bar') |
| 59 | with mock.patch.object(git_metrics._GitMetricCollector, |
Allen Li | 3a2e180 | 2017-04-10 12:48:36 -0700 | [diff] [blame] | 60 | '_timestamp_metric', |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 61 | autospec=True) as time_metric: |
Allen Li | 3a2e180 | 2017-04-10 12:48:36 -0700 | [diff] [blame] | 62 | collector._collect_timestamp_metric() |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 63 | |
Allen Li | 3a2e180 | 2017-04-10 12:48:36 -0700 | [diff] [blame] | 64 | timestamp = self.git_repo.get_commit_time() |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 65 | self.assertEqual(time_metric.set.call_args_list, [ |
Allen Li | 3a2e180 | 2017-04-10 12:48:36 -0700 | [diff] [blame] | 66 | mock.call(timestamp, {'repo': self.git_dir}) |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 67 | ]) |
Allen Li | bdb9f04 | 2017-04-10 13:25:47 -0700 | [diff] [blame^] | 68 | |
| 69 | |
| 70 | class TestMetricCollector(cros_test_lib.TestCase): |
| 71 | """Tests for _GitMetricCollector.""" |
| 72 | |
| 73 | def test__gitdir_expand_user(self): |
| 74 | """Test that _gitdir is expanded for user.""" |
| 75 | with mock.patch.dict(os.environ, HOME='/home/arciel'): |
| 76 | collector = git_metrics._GitMetricCollector('~/solciel', 'dummy') |
| 77 | self.assertEqual(collector._gitdir, '~/solciel') |
| 78 | self.assertEqual(collector._gitrepo._gitdir, '/home/arciel/solciel') |