blob: 148821f20baa57dbb00d4793498f5e6314436e2f [file] [log] [blame]
Allen Lid78af972017-07-13 12:48:01 -07001# Copyright 2017 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 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):
18 """Tests for osinfo_metrics."""
19
20 def setUp(self):
21 patcher = mock.patch('infra_libs.ts_mon.common.interface.state.store',
22 autospec=True)
23 self.store = patcher.start()
24 self.addCleanup(patcher.stop)
25
26 def test_collect(self):
Mike Frysingeraa0faf02021-04-23 03:21:47 -040027 distro = ('Ubuntu', '14.04', 'trusty')
28 # This is removed in Python 3.8+.
29 try:
30 dist = mock.patch('platform.dist', autospec=True, return_value=distro)
31 dist.start()
32 except AttributeError:
33 distro = ('', '', '')
34 dist = None
35
Allen Lid78af972017-07-13 12:48:01 -070036 with mock.patch('platform.system', autospec=True) as system, \
Allen Lid78af972017-07-13 12:48:01 -070037 mock.patch('sys.maxsize', 2**64):
38 system.return_value = 'Linux'
Mike Frysingeraa0faf02021-04-23 03:21:47 -040039 if dist is not None:
40 dist.return_value = distro
Allen Lid78af972017-07-13 12:48:01 -070041 osinfo_metrics.collect_os_info()
42
Mike Frysingeraa0faf02021-04-23 03:21:47 -040043 if dist is not None:
44 dist.stop()
45
Allen Lid78af972017-07-13 12:48:01 -070046 setter = self.store.set
Mike Frysingeraa0faf02021-04-23 03:21:47 -040047 print(setter.mock_calls)
Allen Lid78af972017-07-13 12:48:01 -070048 calls = [
Mike Frysingeraa0faf02021-04-23 03:21:47 -040049 mock.call('proc/os/name', (), None, distro[0].lower(),
50 enforce_ge=mock.ANY),
51 mock.call('proc/os/version', (), None, distro[1], enforce_ge=mock.ANY),
Allen Lid78af972017-07-13 12:48:01 -070052 mock.call('proc/os/arch', (), None, 'x86_64', enforce_ge=mock.ANY),
53 mock.call('proc/python/arch', (), None, '64', enforce_ge=mock.ANY),
54 ]
55 setter.assert_has_calls(calls)
56 self.assertEqual(len(setter.mock_calls), len(calls))