blob: efcfff3141adef262470f642b047b61d3c5999ff [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 -070015import mock
16
17from chromite.lib import cros_test_lib
18from chromite.scripts.sysmon import osinfo_metrics
19
20
Mike Frysinger1ca14432020-02-16 00:18:56 -050021assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
22
23
Allen Lid78af972017-07-13 12:48:01 -070024class TestOSInfoMetrics(cros_test_lib.TestCase):
25 """Tests for osinfo_metrics."""
26
27 def setUp(self):
28 patcher = mock.patch('infra_libs.ts_mon.common.interface.state.store',
29 autospec=True)
30 self.store = patcher.start()
31 self.addCleanup(patcher.stop)
32
33 def test_collect(self):
Mike Frysingeraa0faf02021-04-23 03:21:47 -040034 distro = ('Ubuntu', '14.04', 'trusty')
35 # This is removed in Python 3.8+.
36 try:
37 dist = mock.patch('platform.dist', autospec=True, return_value=distro)
38 dist.start()
39 except AttributeError:
40 distro = ('', '', '')
41 dist = None
42
Allen Lid78af972017-07-13 12:48:01 -070043 with mock.patch('platform.system', autospec=True) as system, \
Allen Lid78af972017-07-13 12:48:01 -070044 mock.patch('sys.maxsize', 2**64):
45 system.return_value = 'Linux'
Mike Frysingeraa0faf02021-04-23 03:21:47 -040046 if dist is not None:
47 dist.return_value = distro
Allen Lid78af972017-07-13 12:48:01 -070048 osinfo_metrics.collect_os_info()
49
Mike Frysingeraa0faf02021-04-23 03:21:47 -040050 if dist is not None:
51 dist.stop()
52
Allen Lid78af972017-07-13 12:48:01 -070053 setter = self.store.set
Mike Frysingeraa0faf02021-04-23 03:21:47 -040054 print(setter.mock_calls)
Allen Lid78af972017-07-13 12:48:01 -070055 calls = [
Mike Frysingeraa0faf02021-04-23 03:21:47 -040056 mock.call('proc/os/name', (), None, distro[0].lower(),
57 enforce_ge=mock.ANY),
58 mock.call('proc/os/version', (), None, distro[1], enforce_ge=mock.ANY),
Allen Lid78af972017-07-13 12:48:01 -070059 mock.call('proc/os/arch', (), None, 'x86_64', enforce_ge=mock.ANY),
60 mock.call('proc/python/arch', (), None, '64', enforce_ge=mock.ANY),
61 ]
62 setter.assert_has_calls(calls)
63 self.assertEqual(len(setter.mock_calls), len(calls))