blob: 7a6426c2e85eb7d9bcd0d43b0918feb95a121e4f [file] [log] [blame]
Paul Hobbse46a42b2017-03-21 14:04:13 -07001# 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
7from __future__ import absolute_import
8from __future__ import print_function
9from __future__ import unicode_literals
10
11import mock
12import os
13
14from chromite.lib import cros_test_lib, osutils
15from chromite.scripts.sysmon import git_metrics
16
17# pylint: disable=protected-access
18# pylint: disable=attribute-defined-outside-init
19
Allen Li73b14b02017-04-10 13:18:06 -070020class TestGitMetricsWithTempdir(cros_test_lib.TempDirTestCase):
21 """Tests for git metrics using a Git fixture."""
Paul Hobbse46a42b2017-03-21 14:04:13 -070022
23 def setUp(self):
Paul Hobbse46a42b2017-03-21 14:04:13 -070024 self.git_dir = os.path.join(self.tempdir, '.git')
Allen Li5fc64b32017-04-10 12:53:47 -070025 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 Hobbse46a42b2017-03-21 14:04:13 -070029
Allen Li512d7042017-04-10 13:14:22 -070030 def test_collect_git_hash_types_are_correct(self):
Paul Hobbse46a42b2017-03-21 14:04:13 -070031 """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 Li512d7042017-04-10 13:14:22 -070037 def test_collect_git_time_types_are_correct(self):
Paul Hobbse46a42b2017-03-21 14:04:13 -070038 """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 Li3a2e1802017-04-10 12:48:36 -070042 collector._collect_timestamp_metric()
Paul Hobbse46a42b2017-03-21 14:04:13 -070043
Allen Li512d7042017-04-10 13:14:22 -070044 def test_collect_git_hash_calls_set(self):
Paul Hobbse46a42b2017-03-21 14:04:13 -070045 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 Li512d7042017-04-10 13:14:22 -070057 def test_collect_git_time_calls_set(self):
Paul Hobbse46a42b2017-03-21 14:04:13 -070058 collector = git_metrics._GitMetricCollector(self.git_dir, '/foo/bar')
59 with mock.patch.object(git_metrics._GitMetricCollector,
Allen Li3a2e1802017-04-10 12:48:36 -070060 '_timestamp_metric',
Paul Hobbse46a42b2017-03-21 14:04:13 -070061 autospec=True) as time_metric:
Allen Li3a2e1802017-04-10 12:48:36 -070062 collector._collect_timestamp_metric()
Paul Hobbse46a42b2017-03-21 14:04:13 -070063
Allen Li3a2e1802017-04-10 12:48:36 -070064 timestamp = self.git_repo.get_commit_time()
Paul Hobbse46a42b2017-03-21 14:04:13 -070065 self.assertEqual(time_metric.set.call_args_list, [
Allen Li3a2e1802017-04-10 12:48:36 -070066 mock.call(timestamp, {'repo': self.git_dir})
Paul Hobbse46a42b2017-03-21 14:04:13 -070067 ])