blob: 66913b616f9016b5842cc8a8c8ddc7a6c4cccc5f [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):
Mike Frysinger8d6a51d2021-02-12 07:40:03 -050021 patcher = mock.patch(
22 'chromite.third_party.infra_libs.ts_mon.common.interface.state.store',
23 autospec=True)
Allen Lid78af972017-07-13 12:48:01 -070024 self.store = patcher.start()
25 self.addCleanup(patcher.stop)
26
27 def test_collect(self):
Mike Frysingeraa0faf02021-04-23 03:21:47 -040028 distro = ('Ubuntu', '14.04', 'trusty')
29 # This is removed in Python 3.8+.
30 try:
31 dist = mock.patch('platform.dist', autospec=True, return_value=distro)
32 dist.start()
33 except AttributeError:
34 distro = ('', '', '')
35 dist = None
36
Allen Lid78af972017-07-13 12:48:01 -070037 with mock.patch('platform.system', autospec=True) as system, \
Allen Lid78af972017-07-13 12:48:01 -070038 mock.patch('sys.maxsize', 2**64):
39 system.return_value = 'Linux'
Mike Frysingeraa0faf02021-04-23 03:21:47 -040040 if dist is not None:
41 dist.return_value = distro
Allen Lid78af972017-07-13 12:48:01 -070042 osinfo_metrics.collect_os_info()
43
Mike Frysingeraa0faf02021-04-23 03:21:47 -040044 if dist is not None:
45 dist.stop()
46
Allen Lid78af972017-07-13 12:48:01 -070047 setter = self.store.set
Mike Frysingeraa0faf02021-04-23 03:21:47 -040048 print(setter.mock_calls)
Allen Lid78af972017-07-13 12:48:01 -070049 calls = [
Mike Frysingeraa0faf02021-04-23 03:21:47 -040050 mock.call('proc/os/name', (), None, distro[0].lower(),
51 enforce_ge=mock.ANY),
52 mock.call('proc/os/version', (), None, distro[1], enforce_ge=mock.ANY),
Allen Lid78af972017-07-13 12:48:01 -070053 mock.call('proc/os/arch', (), None, 'x86_64', enforce_ge=mock.ANY),
54 mock.call('proc/python/arch', (), None, '64', enforce_ge=mock.ANY),
55 ]
56 setter.assert_has_calls(calls)
57 self.assertEqual(len(setter.mock_calls), len(calls))