blob: 470124e7e9e03df40ce63810fe0be051f70e51f1 [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
Paul Hobbse46a42b2017-03-21 14:04:13 -07009
10import mock
11import os
12
13from chromite.lib import cros_test_lib, osutils
14from chromite.scripts.sysmon import git_metrics
15
16# pylint: disable=protected-access
17# pylint: disable=attribute-defined-outside-init
18
Allen Li73b14b02017-04-10 13:18:06 -070019class TestGitMetricsWithTempdir(cros_test_lib.TempDirTestCase):
20 """Tests for git metrics using a Git fixture."""
Paul Hobbse46a42b2017-03-21 14:04:13 -070021
22 def setUp(self):
Paul Hobbse46a42b2017-03-21 14:04:13 -070023 self.git_dir = os.path.join(self.tempdir, '.git')
Allen Li5fc64b32017-04-10 12:53:47 -070024 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 Hobbse46a42b2017-03-21 14:04:13 -070028
Allen Li512d7042017-04-10 13:14:22 -070029 def test_collect_git_hash_types_are_correct(self):
Paul Hobbse46a42b2017-03-21 14:04:13 -070030 """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 Li512d7042017-04-10 13:14:22 -070036 def test_collect_git_time_types_are_correct(self):
Paul Hobbse46a42b2017-03-21 14:04:13 -070037 """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 Li3a2e1802017-04-10 12:48:36 -070041 collector._collect_timestamp_metric()
Paul Hobbse46a42b2017-03-21 14:04:13 -070042
Allen Li512d7042017-04-10 13:14:22 -070043 def test_collect_git_hash_calls_set(self):
Paul Hobbse46a42b2017-03-21 14:04:13 -070044 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 Li512d7042017-04-10 13:14:22 -070056 def test_collect_git_time_calls_set(self):
Paul Hobbse46a42b2017-03-21 14:04:13 -070057 collector = git_metrics._GitMetricCollector(self.git_dir, '/foo/bar')
58 with mock.patch.object(git_metrics._GitMetricCollector,
Allen Li3a2e1802017-04-10 12:48:36 -070059 '_timestamp_metric',
Paul Hobbse46a42b2017-03-21 14:04:13 -070060 autospec=True) as time_metric:
Allen Li3a2e1802017-04-10 12:48:36 -070061 collector._collect_timestamp_metric()
Paul Hobbse46a42b2017-03-21 14:04:13 -070062
Allen Li3a2e1802017-04-10 12:48:36 -070063 timestamp = self.git_repo.get_commit_time()
Paul Hobbse46a42b2017-03-21 14:04:13 -070064 self.assertEqual(time_metric.set.call_args_list, [
Allen Li3a2e1802017-04-10 12:48:36 -070065 mock.call(timestamp, {'repo': self.git_dir})
Paul Hobbse46a42b2017-03-21 14:04:13 -070066 ])
Allen Libdb9f042017-04-10 13:25:47 -070067
68
69class 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')