Allen Li | eb10702 | 2017-05-04 13:19:17 -0700 | [diff] [blame] | 1 | # 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 Li | a9c52e3 | 2017-07-13 12:26:39 -0700 | [diff] [blame] | 5 | """Unit tests for net_metrics.""" |
| 6 | |
| 7 | # pylint: disable=protected-access |
Allen Li | eb10702 | 2017-05-04 13:19:17 -0700 | [diff] [blame] | 8 | |
| 9 | from __future__ import absolute_import |
| 10 | from __future__ import print_function |
Allen Li | eb10702 | 2017-05-04 13:19:17 -0700 | [diff] [blame] | 11 | |
Allen Li | a9c52e3 | 2017-07-13 12:26:39 -0700 | [diff] [blame] | 12 | import mock |
| 13 | |
| 14 | import psutil |
| 15 | |
Allen Li | eb10702 | 2017-05-04 13:19:17 -0700 | [diff] [blame] | 16 | from chromite.lib import cros_test_lib |
| 17 | from chromite.scripts.sysmon import net_metrics |
| 18 | |
| 19 | |
Allen Li | a9c52e3 | 2017-07-13 12:26:39 -0700 | [diff] [blame] | 20 | snetio = psutil._common.snetio |
| 21 | snicstats = psutil._common.snicstats |
Allen Li | eb10702 | 2017-05-04 13:19:17 -0700 | [diff] [blame] | 22 | |
Allen Li | a9c52e3 | 2017-07-13 12:26:39 -0700 | [diff] [blame] | 23 | |
| 24 | class 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 Li | b6bb5f8 | 2017-08-21 15:11:57 -0700 | [diff] [blame] | 36 | mock.patch('psutil.net_if_stats', autospec=True) as net_if_stats, \ |
| 37 | mock.patch('socket.getfqdn', autospec=True) as getfqdn: |
Allen Li | a9c52e3 | 2017-07-13 12:26:39 -0700 | [diff] [blame] | 38 | 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 Li | b6bb5f8 | 2017-08-21 15:11:57 -0700 | [diff] [blame] | 47 | getfqdn.return_value = 'foo.example.com' |
Allen Li | a9c52e3 | 2017-07-13 12:26:39 -0700 | [diff] [blame] | 48 | net_metrics.collect_net_info() |
| 49 | |
| 50 | setter = self.store.set |
| 51 | calls = [ |
Allen Li | a9c52e3 | 2017-07-13 12:26:39 -0700 | [diff] [blame] | 52 | 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 Li | b6bb5f8 | 2017-08-21 15:11:57 -0700 | [diff] [blame] | 76 | mock.call('net/fqdn', (), None, |
| 77 | 'foo.example.com', enforce_ge=mock.ANY), |
Allen Li | a9c52e3 | 2017-07-13 12:26:39 -0700 | [diff] [blame] | 78 | ] |
| 79 | setter.assert_has_calls(calls) |
| 80 | self.assertEqual(len(setter.mock_calls), len(calls)) |