blob: 400d51fc9bb8ef9fb4cc2fb66c17b49bcab08668 [file] [log] [blame]
Mike Frysingere58c0e22017-10-04 15:43:30 -04001# -*- coding: utf-8 -*-
Allen Lieb107022017-05-04 13:19:17 -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
Allen Lia9c52e32017-07-13 12:26:39 -07006"""Unit tests for net_metrics."""
7
8# pylint: disable=protected-access
Allen Lieb107022017-05-04 13:19:17 -07009
10from __future__ import absolute_import
11from __future__ import print_function
Allen Lieb107022017-05-04 13:19:17 -070012
Prathmesh Prabhu07f80872017-08-21 15:09:33 -070013import socket
Allen Lia9c52e32017-07-13 12:26:39 -070014
Mike Frysinger6db648e2018-07-24 19:57:58 -040015import mock
Mike Frysingercb56b642019-08-25 15:33:08 -040016import psutil # pylint: disable=import-error
Allen Lia9c52e32017-07-13 12:26:39 -070017
Allen Lieb107022017-05-04 13:19:17 -070018from chromite.lib import cros_test_lib
19from chromite.scripts.sysmon import net_metrics
20
21
Allen Lia9c52e32017-07-13 12:26:39 -070022snetio = psutil._common.snetio
23snicstats = psutil._common.snicstats
Prathmesh Prabhu07f80872017-08-21 15:09:33 -070024snic = psutil._common.snic
Allen Lieb107022017-05-04 13:19:17 -070025
Allen Lia9c52e32017-07-13 12:26:39 -070026
27class TestNetMetrics(cros_test_lib.TestCase):
28 """Tests for net_metrics."""
29
30 def setUp(self):
31 patcher = mock.patch('infra_libs.ts_mon.common.interface.state.store',
32 autospec=True)
33 self.store = patcher.start()
34 self.addCleanup(patcher.stop)
35
36 def test_collect(self):
37 with mock.patch('psutil.net_io_counters', autospec=True) \
38 as net_io_counters, \
Allen Lib6bb5f82017-08-21 15:11:57 -070039 mock.patch('psutil.net_if_stats', autospec=True) as net_if_stats, \
Prathmesh Prabhu07f80872017-08-21 15:09:33 -070040 mock.patch('socket.getfqdn', autospec=True) as getfqdn, \
41 mock.patch('psutil.net_if_addrs', autospec=True) as net_if_addrs:
Allen Lia9c52e32017-07-13 12:26:39 -070042 net_io_counters.return_value = {
43 'lo': snetio(
44 bytes_sent=17247495681, bytes_recv=172474956,
45 packets_sent=109564455, packets_recv=1095644,
46 errin=10, errout=1, dropin=20, dropout=2),
47 }
48 net_if_stats.return_value = {
49 'lo': snicstats(isup=True, duplex=0, speed=0, mtu=65536),
50 }
Allen Lib6bb5f82017-08-21 15:11:57 -070051 getfqdn.return_value = 'foo.example.com'
Prathmesh Prabhu07f80872017-08-21 15:09:33 -070052 net_if_addrs.return_value = {
53 'lo': [
54 snic(family=psutil.AF_LINK, address='11:22:33:44:55:66',
55 netmask=None, broadcast=None, ptp=None),
56 snic(family=socket.AF_INET, address='10.1.1.1', netmask=None,
57 broadcast=None, ptp=None),
58 snic(family=socket.AF_INET6,
59 address='fc00:0000:0000:0000:0000:0000:0000:0001',
60 netmask=None, broadcast=None, ptp=None),
61 ],
62 }
Allen Lia9c52e32017-07-13 12:26:39 -070063 net_metrics.collect_net_info()
64
65 setter = self.store.set
66 calls = [
Allen Lia9c52e32017-07-13 12:26:39 -070067 mock.call('dev/net/bytes', ('up', 'lo'), None,
68 17247495681, enforce_ge=mock.ANY),
69 mock.call('dev/net/bytes', ('down', 'lo'), None,
70 172474956, enforce_ge=mock.ANY),
71 mock.call('dev/net/packets', ('up', 'lo'), None,
72 109564455, enforce_ge=mock.ANY),
73 mock.call('dev/net/packets', ('down', 'lo'), None,
74 1095644, enforce_ge=mock.ANY),
75 mock.call('dev/net/errors', ('up', 'lo'), None,
76 1, enforce_ge=mock.ANY),
77 mock.call('dev/net/errors', ('down', 'lo'), None,
78 10, enforce_ge=mock.ANY),
79 mock.call('dev/net/dropped', ('up', 'lo'), None,
80 2, enforce_ge=mock.ANY),
81 mock.call('dev/net/dropped', ('down', 'lo'), None,
82 20, enforce_ge=mock.ANY),
83 mock.call('dev/net/isup', ('lo',), None,
84 True, enforce_ge=mock.ANY),
85 mock.call('dev/net/duplex', ('lo',), None,
86 0, enforce_ge=mock.ANY),
87 mock.call('dev/net/speed', ('lo',), None,
88 0, enforce_ge=mock.ANY),
89 mock.call('dev/net/mtu', ('lo',), None,
90 65536, enforce_ge=mock.ANY),
Allen Lib6bb5f82017-08-21 15:11:57 -070091 mock.call('net/fqdn', (), None,
92 'foo.example.com', enforce_ge=mock.ANY),
Prathmesh Prabhu07f80872017-08-21 15:09:33 -070093 mock.call('dev/net/address', ('AF_LINK', 'lo'), None,
94 '11:22:33:44:55:66', enforce_ge=mock.ANY),
95 mock.call('dev/net/address', ('AF_INET', 'lo',), None,
96 '10.1.1.1', enforce_ge=mock.ANY),
97 mock.call('dev/net/address', ('AF_INET6', 'lo'), None,
98 'fc00:0000:0000:0000:0000:0000:0000:0001',
99 enforce_ge=mock.ANY),
Allen Lia9c52e32017-07-13 12:26:39 -0700100 ]
101 setter.assert_has_calls(calls)
102 self.assertEqual(len(setter.mock_calls), len(calls))