blob: 4e88a785693a0f0d3677f4e6469e02bba64ba348 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2017 The ChromiumOS Authors
Allen Lieb107022017-05-04 13:19:17 -07002# 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(
Trent Aptedc4f366a2023-05-16 15:32:48 +100034 "chromite.third_party.infra_libs.ts_mon.common.interface.state."
35 "store",
Alex Klein1699fab2022-09-08 08:46:06 -060036 autospec=True,
37 )
38 self.store = patcher.start()
39 self.addCleanup(patcher.stop)
Allen Lia9c52e32017-07-13 12:26:39 -070040
Alex Klein1699fab2022-09-08 08:46:06 -060041 def test_collect(self):
42 with mock.patch(
43 "psutil.net_io_counters", autospec=True
44 ) as net_io_counters, mock.patch(
45 "psutil.net_if_stats", autospec=True
46 ) as net_if_stats, mock.patch(
47 "socket.getfqdn", autospec=True
48 ) as getfqdn, mock.patch(
49 "psutil.net_if_addrs", autospec=True
50 ) as net_if_addrs:
51 net_io_counters.return_value = {
52 "lo": snetio(
53 bytes_sent=17247495681,
54 bytes_recv=172474956,
55 packets_sent=109564455,
56 packets_recv=1095644,
57 errin=10,
58 errout=1,
59 dropin=20,
60 dropout=2,
61 ),
62 }
63 net_if_stats.return_value = {
64 "lo": snicstats(isup=True, duplex=0, speed=0, mtu=65536),
65 }
66 getfqdn.return_value = "foo.example.com"
67 net_if_addrs.return_value = {
68 "lo": [
69 # pylint: disable=not-callable
70 snic(
71 family=psutil.AF_LINK,
72 address="11:22:33:44:55:66",
73 netmask=None,
74 broadcast=None,
75 ptp=None,
76 ),
77 snic(
78 family=socket.AF_INET,
79 address="10.1.1.1",
80 netmask=None,
81 broadcast=None,
82 ptp=None,
83 ),
84 snic(
85 family=socket.AF_INET6,
86 address="fc00:0000:0000:0000:0000:0000:0000:0001",
87 netmask=None,
88 broadcast=None,
89 ptp=None,
90 ),
91 ],
92 }
93 net_metrics.collect_net_info()
Allen Lia9c52e32017-07-13 12:26:39 -070094
Alex Klein1699fab2022-09-08 08:46:06 -060095 setter = self.store.set
96 calls = [
97 mock.call(
98 "dev/net/bytes",
99 ("up", "lo"),
100 None,
101 17247495681,
102 enforce_ge=mock.ANY,
103 ),
104 mock.call(
105 "dev/net/bytes",
106 ("down", "lo"),
107 None,
108 172474956,
109 enforce_ge=mock.ANY,
110 ),
111 mock.call(
112 "dev/net/packets",
113 ("up", "lo"),
114 None,
115 109564455,
116 enforce_ge=mock.ANY,
117 ),
118 mock.call(
119 "dev/net/packets",
120 ("down", "lo"),
121 None,
122 1095644,
123 enforce_ge=mock.ANY,
124 ),
125 mock.call(
126 "dev/net/errors", ("up", "lo"), None, 1, enforce_ge=mock.ANY
127 ),
128 mock.call(
129 "dev/net/errors", ("down", "lo"), None, 10, enforce_ge=mock.ANY
130 ),
131 mock.call(
132 "dev/net/dropped", ("up", "lo"), None, 2, enforce_ge=mock.ANY
133 ),
134 mock.call(
135 "dev/net/dropped", ("down", "lo"), None, 20, enforce_ge=mock.ANY
136 ),
137 mock.call("dev/net/isup", ("lo",), None, True, enforce_ge=mock.ANY),
138 mock.call("dev/net/duplex", ("lo",), None, 0, enforce_ge=mock.ANY),
139 mock.call("dev/net/speed", ("lo",), None, 0, enforce_ge=mock.ANY),
140 mock.call("dev/net/mtu", ("lo",), None, 65536, enforce_ge=mock.ANY),
141 mock.call(
142 "net/fqdn", (), None, "foo.example.com", enforce_ge=mock.ANY
143 ),
144 mock.call(
145 "dev/net/address",
146 ("AF_LINK", "lo"),
147 None,
148 "11:22:33:44:55:66",
149 enforce_ge=mock.ANY,
150 ),
151 mock.call(
152 "dev/net/address",
153 (
154 "AF_INET",
155 "lo",
156 ),
157 None,
158 "10.1.1.1",
159 enforce_ge=mock.ANY,
160 ),
161 mock.call(
162 "dev/net/address",
163 ("AF_INET6", "lo"),
164 None,
165 "fc00:0000:0000:0000:0000:0000:0000:0001",
166 enforce_ge=mock.ANY,
167 ),
168 ]
169 setter.assert_has_calls(calls)
170 self.assertEqual(len(setter.mock_calls), len(calls))