blob: 913b291d5feba5d45fd2c12600bc6eca3e928452 [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
Allen Lieb107022017-05-04 13:19:17 -070010
Prathmesh Prabhu07f80872017-08-21 15:09:33 -070011import socket
Mike Frysinger166fea02021-02-12 05:30:33 -050012from unittest import mock
Allen Lia9c52e32017-07-13 12:26:39 -070013
Mike Frysingercb56b642019-08-25 15:33:08 -040014import psutil # pylint: disable=import-error
Allen Lia9c52e32017-07-13 12:26:39 -070015
Allen Lieb107022017-05-04 13:19:17 -070016from chromite.lib import cros_test_lib
17from chromite.scripts.sysmon import net_metrics
18
Mike Frysinger1ca14432020-02-16 00:18:56 -050019
Allen Lia9c52e32017-07-13 12:26:39 -070020snetio = psutil._common.snetio
21snicstats = psutil._common.snicstats
Alex Klein1699fab2022-09-08 08:46:06 -060022snic = getattr(psutil._common, "snic", None)
Greg Edelstona4c9b3b2020-01-07 17:51:13 -070023
Alex Klein1699fab2022-09-08 08:46:06 -060024pytestmark = cros_test_lib.pytestmark_skipif(
25 snic is None, reason="Wrong version of psutil"
26)
Allen Lieb107022017-05-04 13:19:17 -070027
Allen Lia9c52e32017-07-13 12:26:39 -070028
29class TestNetMetrics(cros_test_lib.TestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060030 """Tests for net_metrics."""
Allen Lia9c52e32017-07-13 12:26:39 -070031
Alex Klein1699fab2022-09-08 08:46:06 -060032 def setUp(self):
33 patcher = mock.patch(
34 "chromite.third_party.infra_libs.ts_mon.common.interface.state.store",
35 autospec=True,
36 )
37 self.store = patcher.start()
38 self.addCleanup(patcher.stop)
Allen Lia9c52e32017-07-13 12:26:39 -070039
Alex Klein1699fab2022-09-08 08:46:06 -060040 def test_collect(self):
41 with mock.patch(
42 "psutil.net_io_counters", autospec=True
43 ) as net_io_counters, mock.patch(
44 "psutil.net_if_stats", autospec=True
45 ) as net_if_stats, mock.patch(
46 "socket.getfqdn", autospec=True
47 ) as getfqdn, mock.patch(
48 "psutil.net_if_addrs", autospec=True
49 ) as net_if_addrs:
50 net_io_counters.return_value = {
51 "lo": snetio(
52 bytes_sent=17247495681,
53 bytes_recv=172474956,
54 packets_sent=109564455,
55 packets_recv=1095644,
56 errin=10,
57 errout=1,
58 dropin=20,
59 dropout=2,
60 ),
61 }
62 net_if_stats.return_value = {
63 "lo": snicstats(isup=True, duplex=0, speed=0, mtu=65536),
64 }
65 getfqdn.return_value = "foo.example.com"
66 net_if_addrs.return_value = {
67 "lo": [
68 # pylint: disable=not-callable
69 snic(
70 family=psutil.AF_LINK,
71 address="11:22:33:44:55:66",
72 netmask=None,
73 broadcast=None,
74 ptp=None,
75 ),
76 snic(
77 family=socket.AF_INET,
78 address="10.1.1.1",
79 netmask=None,
80 broadcast=None,
81 ptp=None,
82 ),
83 snic(
84 family=socket.AF_INET6,
85 address="fc00:0000:0000:0000:0000:0000:0000:0001",
86 netmask=None,
87 broadcast=None,
88 ptp=None,
89 ),
90 ],
91 }
92 net_metrics.collect_net_info()
Allen Lia9c52e32017-07-13 12:26:39 -070093
Alex Klein1699fab2022-09-08 08:46:06 -060094 setter = self.store.set
95 calls = [
96 mock.call(
97 "dev/net/bytes",
98 ("up", "lo"),
99 None,
100 17247495681,
101 enforce_ge=mock.ANY,
102 ),
103 mock.call(
104 "dev/net/bytes",
105 ("down", "lo"),
106 None,
107 172474956,
108 enforce_ge=mock.ANY,
109 ),
110 mock.call(
111 "dev/net/packets",
112 ("up", "lo"),
113 None,
114 109564455,
115 enforce_ge=mock.ANY,
116 ),
117 mock.call(
118 "dev/net/packets",
119 ("down", "lo"),
120 None,
121 1095644,
122 enforce_ge=mock.ANY,
123 ),
124 mock.call(
125 "dev/net/errors", ("up", "lo"), None, 1, enforce_ge=mock.ANY
126 ),
127 mock.call(
128 "dev/net/errors", ("down", "lo"), None, 10, enforce_ge=mock.ANY
129 ),
130 mock.call(
131 "dev/net/dropped", ("up", "lo"), None, 2, enforce_ge=mock.ANY
132 ),
133 mock.call(
134 "dev/net/dropped", ("down", "lo"), None, 20, enforce_ge=mock.ANY
135 ),
136 mock.call("dev/net/isup", ("lo",), None, True, enforce_ge=mock.ANY),
137 mock.call("dev/net/duplex", ("lo",), None, 0, enforce_ge=mock.ANY),
138 mock.call("dev/net/speed", ("lo",), None, 0, enforce_ge=mock.ANY),
139 mock.call("dev/net/mtu", ("lo",), None, 65536, enforce_ge=mock.ANY),
140 mock.call(
141 "net/fqdn", (), None, "foo.example.com", enforce_ge=mock.ANY
142 ),
143 mock.call(
144 "dev/net/address",
145 ("AF_LINK", "lo"),
146 None,
147 "11:22:33:44:55:66",
148 enforce_ge=mock.ANY,
149 ),
150 mock.call(
151 "dev/net/address",
152 (
153 "AF_INET",
154 "lo",
155 ),
156 None,
157 "10.1.1.1",
158 enforce_ge=mock.ANY,
159 ),
160 mock.call(
161 "dev/net/address",
162 ("AF_INET6", "lo"),
163 None,
164 "fc00:0000:0000:0000:0000:0000:0000:0001",
165 enforce_ge=mock.ANY,
166 ),
167 ]
168 setter.assert_has_calls(calls)
169 self.assertEqual(len(setter.mock_calls), len(calls))