blob: 3917fe9c13763ecec42ff64fbb008d2a7af0465e [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
20class 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 Li3a2e1802017-04-10 12:48:36 -070046 collector._collect_timestamp_metric()
Paul Hobbse46a42b2017-03-21 14:04:13 -070047
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 Li3a2e1802017-04-10 12:48:36 -070064 '_timestamp_metric',
Paul Hobbse46a42b2017-03-21 14:04:13 -070065 autospec=True) as time_metric:
Allen Li3a2e1802017-04-10 12:48:36 -070066 collector._collect_timestamp_metric()
Paul Hobbse46a42b2017-03-21 14:04:13 -070067
Allen Li3a2e1802017-04-10 12:48:36 -070068 timestamp = self.git_repo.get_commit_time()
Paul Hobbse46a42b2017-03-21 14:04:13 -070069 self.assertEqual(time_metric.set.call_args_list, [
Allen Li3a2e1802017-04-10 12:48:36 -070070 mock.call(timestamp, {'repo': self.git_dir})
Paul Hobbse46a42b2017-03-21 14:04:13 -070071 ])