blob: 8b2190fd163ef18a58fe2e2eed7c5bdd3379568a [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
Allen Lid78af972017-07-13 12:48:01 -070011from chromite.lib import cros_test_lib
12from chromite.scripts.sysmon import osinfo_metrics
Mike Frysinger40ffb532021-02-12 07:36:08 -050013from chromite.third_party import mock
Allen Lid78af972017-07-13 12:48:01 -070014
15
Allen Lid78af972017-07-13 12:48:01 -070016class TestOSInfoMetrics(cros_test_lib.TestCase):
17 """Tests for osinfo_metrics."""
18
19 def setUp(self):
20 patcher = mock.patch('infra_libs.ts_mon.common.interface.state.store',
21 autospec=True)
22 self.store = patcher.start()
23 self.addCleanup(patcher.stop)
24
25 def test_collect(self):
Mike Frysingeraa0faf02021-04-23 03:21:47 -040026 distro = ('Ubuntu', '14.04', 'trusty')
27 # This is removed in Python 3.8+.
28 try:
29 dist = mock.patch('platform.dist', autospec=True, return_value=distro)
30 dist.start()
31 except AttributeError:
32 distro = ('', '', '')
33 dist = None
34
Allen Lid78af972017-07-13 12:48:01 -070035 with mock.patch('platform.system', autospec=True) as system, \
Allen Lid78af972017-07-13 12:48:01 -070036 mock.patch('sys.maxsize', 2**64):
37 system.return_value = 'Linux'
Mike Frysingeraa0faf02021-04-23 03:21:47 -040038 if dist is not None:
39 dist.return_value = distro
Allen Lid78af972017-07-13 12:48:01 -070040 osinfo_metrics.collect_os_info()
41
Mike Frysingeraa0faf02021-04-23 03:21:47 -040042 if dist is not None:
43 dist.stop()
44
Allen Lid78af972017-07-13 12:48:01 -070045 setter = self.store.set
Mike Frysingeraa0faf02021-04-23 03:21:47 -040046 print(setter.mock_calls)
Allen Lid78af972017-07-13 12:48:01 -070047 calls = [
Mike Frysingeraa0faf02021-04-23 03:21:47 -040048 mock.call('proc/os/name', (), None, distro[0].lower(),
49 enforce_ge=mock.ANY),
50 mock.call('proc/os/version', (), None, distro[1], enforce_ge=mock.ANY),
Allen Lid78af972017-07-13 12:48:01 -070051 mock.call('proc/os/arch', (), None, 'x86_64', enforce_ge=mock.ANY),
52 mock.call('proc/python/arch', (), None, '64', enforce_ge=mock.ANY),
53 ]
54 setter.assert_has_calls(calls)
55 self.assertEqual(len(setter.mock_calls), len(calls))