blob: 6f35d22ee1877a7f27da194324f22acc61e4a2d9 [file] [log] [blame]
Mike Frysingere58c0e22017-10-04 15:43:30 -04001# -*- coding: utf-8 -*-
Allen Lid78af972017-07-13 12:48:01 -07002# Copyright 2017 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Unit tests for osinfo_metrics."""
7
8# pylint: disable=protected-access
9
10from __future__ import absolute_import
11from __future__ import print_function
12
Mike Frysinger1ca14432020-02-16 00:18:56 -050013import sys
14
Allen Lid78af972017-07-13 12:48:01 -070015from chromite.lib import cros_test_lib
16from chromite.scripts.sysmon import osinfo_metrics
Mike Frysinger40ffb532021-02-12 07:36:08 -050017from chromite.third_party import mock
Allen Lid78af972017-07-13 12:48:01 -070018
19
Mike Frysinger1ca14432020-02-16 00:18:56 -050020assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
21
22
Allen Lid78af972017-07-13 12:48:01 -070023class TestOSInfoMetrics(cros_test_lib.TestCase):
24 """Tests for osinfo_metrics."""
25
26 def setUp(self):
27 patcher = mock.patch('infra_libs.ts_mon.common.interface.state.store',
28 autospec=True)
29 self.store = patcher.start()
30 self.addCleanup(patcher.stop)
31
32 def test_collect(self):
Mike Frysingeraa0faf02021-04-23 03:21:47 -040033 distro = ('Ubuntu', '14.04', 'trusty')
34 # This is removed in Python 3.8+.
35 try:
36 dist = mock.patch('platform.dist', autospec=True, return_value=distro)
37 dist.start()
38 except AttributeError:
39 distro = ('', '', '')
40 dist = None
41
Allen Lid78af972017-07-13 12:48:01 -070042 with mock.patch('platform.system', autospec=True) as system, \
Allen Lid78af972017-07-13 12:48:01 -070043 mock.patch('sys.maxsize', 2**64):
44 system.return_value = 'Linux'
Mike Frysingeraa0faf02021-04-23 03:21:47 -040045 if dist is not None:
46 dist.return_value = distro
Allen Lid78af972017-07-13 12:48:01 -070047 osinfo_metrics.collect_os_info()
48
Mike Frysingeraa0faf02021-04-23 03:21:47 -040049 if dist is not None:
50 dist.stop()
51
Allen Lid78af972017-07-13 12:48:01 -070052 setter = self.store.set
Mike Frysingeraa0faf02021-04-23 03:21:47 -040053 print(setter.mock_calls)
Allen Lid78af972017-07-13 12:48:01 -070054 calls = [
Mike Frysingeraa0faf02021-04-23 03:21:47 -040055 mock.call('proc/os/name', (), None, distro[0].lower(),
56 enforce_ge=mock.ANY),
57 mock.call('proc/os/version', (), None, distro[1], enforce_ge=mock.ANY),
Allen Lid78af972017-07-13 12:48:01 -070058 mock.call('proc/os/arch', (), None, 'x86_64', enforce_ge=mock.ANY),
59 mock.call('proc/python/arch', (), None, '64', enforce_ge=mock.ANY),
60 ]
61 setter.assert_has_calls(calls)
62 self.assertEqual(len(setter.mock_calls), len(calls))