blob: b4d1d5897107c44d13ae96ca2233d716a34b8d29 [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(
22 "chromite.third_party.infra_libs.ts_mon.common.interface.state.store",
23 autospec=True,
24 )
25 self.store = patcher.start()
26 self.addCleanup(patcher.stop)
Allen Lid78af972017-07-13 12:48:01 -070027
Alex Klein1699fab2022-09-08 08:46:06 -060028 def test_collect(self):
29 distro = ("Ubuntu", "14.04", "trusty")
30 # This is removed in Python 3.8+.
31 try:
32 dist = mock.patch(
33 "platform.dist", autospec=True, return_value=distro
34 )
35 dist.start()
36 except AttributeError:
37 distro = ("", "", "")
38 dist = None
Mike Frysingeraa0faf02021-04-23 03:21:47 -040039
Alex Klein1699fab2022-09-08 08:46:06 -060040 with mock.patch("platform.system", autospec=True) as system, mock.patch(
41 "sys.maxsize", 2**64
42 ):
43 system.return_value = "Linux"
44 if dist is not None:
45 dist.return_value = distro
46 osinfo_metrics.collect_os_info()
Allen Lid78af972017-07-13 12:48:01 -070047
Alex Klein1699fab2022-09-08 08:46:06 -060048 if dist is not None:
49 dist.stop()
Mike Frysingeraa0faf02021-04-23 03:21:47 -040050
Alex Klein1699fab2022-09-08 08:46:06 -060051 setter = self.store.set
52 print(setter.mock_calls)
53 calls = [
54 mock.call(
55 "proc/os/name", (), None, distro[0].lower(), enforce_ge=mock.ANY
56 ),
57 mock.call(
58 "proc/os/version", (), None, distro[1], enforce_ge=mock.ANY
59 ),
60 mock.call("proc/os/arch", (), None, "x86_64", enforce_ge=mock.ANY),
61 mock.call("proc/python/arch", (), None, "64", enforce_ge=mock.ANY),
62 ]
63 setter.assert_has_calls(calls)
64 self.assertEqual(len(setter.mock_calls), len(calls))