blob: bcb4e3efbf38d8998ff2e2a9c321ae7b07db7184 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2017 The ChromiumOS Authors
Allen Lid78af972017-07-13 12:48:01 -07002# 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 osinfo_metrics."""
6
7# pylint: disable=protected-access
8
9from __future__ import absolute_import
Allen Lid78af972017-07-13 12:48:01 -070010
Mike Frysinger166fea02021-02-12 05:30:33 -050011from unittest import mock
12
Allen Lid78af972017-07-13 12:48:01 -070013from chromite.lib import cros_test_lib
14from chromite.scripts.sysmon import osinfo_metrics
15
16
Allen Lid78af972017-07-13 12:48:01 -070017class TestOSInfoMetrics(cros_test_lib.TestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060018 """Tests for osinfo_metrics."""
Allen Lid78af972017-07-13 12:48:01 -070019
Alex Klein1699fab2022-09-08 08:46:06 -060020 def setUp(self):
21 patcher = mock.patch(
Trent Aptedc4f366a2023-05-16 15:32:48 +100022 "chromite.third_party.infra_libs.ts_mon.common.interface.state."
23 "store",
Alex Klein1699fab2022-09-08 08:46:06 -060024 autospec=True,
25 )
26 self.store = patcher.start()
27 self.addCleanup(patcher.stop)
Allen Lid78af972017-07-13 12:48:01 -070028
Alex Klein1699fab2022-09-08 08:46:06 -060029 def test_collect(self):
30 distro = ("Ubuntu", "14.04", "trusty")
31 # This is removed in Python 3.8+.
32 try:
33 dist = mock.patch(
34 "platform.dist", autospec=True, return_value=distro
35 )
36 dist.start()
37 except AttributeError:
38 distro = ("", "", "")
39 dist = None
Mike Frysingeraa0faf02021-04-23 03:21:47 -040040
Alex Klein1699fab2022-09-08 08:46:06 -060041 with mock.patch("platform.system", autospec=True) as system, mock.patch(
42 "sys.maxsize", 2**64
43 ):
44 system.return_value = "Linux"
45 if dist is not None:
46 dist.return_value = distro
47 osinfo_metrics.collect_os_info()
Allen Lid78af972017-07-13 12:48:01 -070048
Alex Klein1699fab2022-09-08 08:46:06 -060049 if dist is not None:
50 dist.stop()
Mike Frysingeraa0faf02021-04-23 03:21:47 -040051
Alex Klein1699fab2022-09-08 08:46:06 -060052 setter = self.store.set
53 print(setter.mock_calls)
54 calls = [
55 mock.call(
56 "proc/os/name", (), None, distro[0].lower(), enforce_ge=mock.ANY
57 ),
58 mock.call(
59 "proc/os/version", (), None, distro[1], enforce_ge=mock.ANY
60 ),
61 mock.call("proc/os/arch", (), None, "x86_64", enforce_ge=mock.ANY),
62 mock.call("proc/python/arch", (), None, "64", enforce_ge=mock.ANY),
63 ]
64 setter.assert_has_calls(calls)
65 self.assertEqual(len(setter.mock_calls), len(calls))