blob: 08a0918b48b800360072594841f89afd9383d6f4 [file] [log] [blame]
Allen Lieb107022017-05-04 13:19:17 -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
Allen Lia9c52e32017-07-13 12:26:39 -07005"""Unit tests for net_metrics."""
6
7# pylint: disable=protected-access
Allen Lieb107022017-05-04 13:19:17 -07008
9from __future__ import absolute_import
10from __future__ import print_function
Allen Lieb107022017-05-04 13:19:17 -070011
Allen Lia9c52e32017-07-13 12:26:39 -070012import mock
13
14import psutil
15
Allen Lieb107022017-05-04 13:19:17 -070016from chromite.lib import cros_test_lib
17from chromite.scripts.sysmon import net_metrics
18
19
Allen Lia9c52e32017-07-13 12:26:39 -070020snetio = psutil._common.snetio
21snicstats = psutil._common.snicstats
Allen Lieb107022017-05-04 13:19:17 -070022
Allen Lia9c52e32017-07-13 12:26:39 -070023
24class TestNetMetrics(cros_test_lib.TestCase):
25 """Tests for net_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):
34 with mock.patch('psutil.net_io_counters', autospec=True) \
35 as net_io_counters, \
Allen Lib6bb5f82017-08-21 15:11:57 -070036 mock.patch('psutil.net_if_stats', autospec=True) as net_if_stats, \
37 mock.patch('socket.getfqdn', autospec=True) as getfqdn:
Allen Lia9c52e32017-07-13 12:26:39 -070038 net_io_counters.return_value = {
39 'lo': snetio(
40 bytes_sent=17247495681, bytes_recv=172474956,
41 packets_sent=109564455, packets_recv=1095644,
42 errin=10, errout=1, dropin=20, dropout=2),
43 }
44 net_if_stats.return_value = {
45 'lo': snicstats(isup=True, duplex=0, speed=0, mtu=65536),
46 }
Allen Lib6bb5f82017-08-21 15:11:57 -070047 getfqdn.return_value = 'foo.example.com'
Allen Lia9c52e32017-07-13 12:26:39 -070048 net_metrics.collect_net_info()
49
50 setter = self.store.set
51 calls = [
Allen Lia9c52e32017-07-13 12:26:39 -070052 mock.call('dev/net/bytes', ('up', 'lo'), None,
53 17247495681, enforce_ge=mock.ANY),
54 mock.call('dev/net/bytes', ('down', 'lo'), None,
55 172474956, enforce_ge=mock.ANY),
56 mock.call('dev/net/packets', ('up', 'lo'), None,
57 109564455, enforce_ge=mock.ANY),
58 mock.call('dev/net/packets', ('down', 'lo'), None,
59 1095644, enforce_ge=mock.ANY),
60 mock.call('dev/net/errors', ('up', 'lo'), None,
61 1, enforce_ge=mock.ANY),
62 mock.call('dev/net/errors', ('down', 'lo'), None,
63 10, enforce_ge=mock.ANY),
64 mock.call('dev/net/dropped', ('up', 'lo'), None,
65 2, enforce_ge=mock.ANY),
66 mock.call('dev/net/dropped', ('down', 'lo'), None,
67 20, enforce_ge=mock.ANY),
68 mock.call('dev/net/isup', ('lo',), None,
69 True, enforce_ge=mock.ANY),
70 mock.call('dev/net/duplex', ('lo',), None,
71 0, enforce_ge=mock.ANY),
72 mock.call('dev/net/speed', ('lo',), None,
73 0, enforce_ge=mock.ANY),
74 mock.call('dev/net/mtu', ('lo',), None,
75 65536, enforce_ge=mock.ANY),
Allen Lib6bb5f82017-08-21 15:11:57 -070076 mock.call('net/fqdn', (), None,
77 'foo.example.com', enforce_ge=mock.ANY),
Allen Lia9c52e32017-07-13 12:26:39 -070078 ]
79 setter.assert_has_calls(calls)
80 self.assertEqual(len(setter.mock_calls), len(calls))