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