sysmon: Change git/commit_time to a gauge
The git/commit_time metric should not be a String, because we feed it ints.
TEST=Added a unit test. Changing from String -> Gauge makes the test pass.
BUG=chromium:703827
Change-Id: I8300ecee2b37676020495a3c100593d2188a1d68
Reviewed-on: https://chromium-review.googlesource.com/457621
Trybot-Ready: Paul Hobbs <phobbs@google.com>
Reviewed-by: Paul Hobbs <phobbs@google.com>
Tested-by: Paul Hobbs <phobbs@google.com>
diff --git a/scripts/sysmon/git_metrics_unittest.py b/scripts/sysmon/git_metrics_unittest.py
new file mode 100644
index 0000000..c042360
--- /dev/null
+++ b/scripts/sysmon/git_metrics_unittest.py
@@ -0,0 +1,71 @@
+# Copyright 2016 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""Unit tests for git_metrics."""
+
+from __future__ import absolute_import
+from __future__ import print_function
+from __future__ import unicode_literals
+
+import mock
+import os
+
+from chromite.lib import cros_test_lib, osutils
+from chromite.scripts.sysmon import git_metrics
+
+# pylint: disable=protected-access
+# pylint: disable=attribute-defined-outside-init
+
+class TestGitMetrics(cros_test_lib.TempDirTestCase):
+ """Tests for git metrics."""
+
+ def setUp(self):
+ self._InitRepo()
+
+ def _InitRepo(self):
+ """Initializes a repo in the temp directory."""
+ self.git_dir = os.path.join(self.tempdir, '.git')
+ # with osutils.ChdirContext(self.tempdir):
+ self.git_repo = git_metrics._GitRepo(self.git_dir)
+ self.git_repo._check_output(['init'])
+ self.git_repo._check_output(['commit', '--allow-empty', '-m', 'hi'])
+
+ def testCollectGitHashTypesAreCorrect(self):
+ """Tests that collecting the git hash doesn't have type conflicts."""
+ collector = git_metrics._GitMetricCollector(self.git_dir, '/foo/bar')
+
+ # This has the side-effect of checking the types are correct.
+ collector._collect_commit_hash_metric()
+
+ def testCollectGitTimeTypesAreCorrect(self):
+ """Tests that collecting the git commit time works."""
+ collector = git_metrics._GitMetricCollector(self.git_dir, '/foo/bar')
+
+ # This has the side-effect of checking the types are correct.
+ collector._collect_commit_time_metric()
+
+ def testCollectGitHashCallsSet(self):
+ collector = git_metrics._GitMetricCollector(self.git_dir, '/foo/bar')
+
+ with mock.patch.object(git_metrics._GitMetricCollector,
+ '_commit_hash_metric',
+ autospec=True) as hash_metric:
+ collector._collect_commit_hash_metric()
+
+ commit_hash = self.git_repo.get_commit_hash()
+ self.assertEqual(hash_metric.set.call_args_list, [
+ mock.call(commit_hash, {'repo': self.git_dir})
+ ])
+
+ def testCollectGitTimeCallsSet(self):
+ collector = git_metrics._GitMetricCollector(self.git_dir, '/foo/bar')
+ with mock.patch.object(git_metrics._GitMetricCollector,
+ '_commit_time_metric',
+ autospec=True) as time_metric:
+ collector._collect_commit_time_metric()
+
+ commit_time = self.git_repo.get_commit_time()
+ self.assertEqual(time_metric.set.call_args_list, [
+ mock.call(commit_time, {'repo': self.git_dir})
+ ])