PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/spec/unit/plugins/linux/network_spec.rb

https://github.com/wistia/ohai
Ruby | 910 lines | 809 code | 82 blank | 19 comment | 65 complexity | ba2675b1a23c1136d8c24dc77d18dfab MD5 | raw file
Possible License(s): Apache-2.0
  1. #
  2. # Author:: Caleb Tennis <caleb.tennis@gmail.com>
  3. # Copyright:: Copyright (c) 2011 Opscode, Inc.
  4. # License:: Apache License, Version 2.0
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper.rb')
  19. begin
  20. require 'ipaddress'
  21. rescue LoadError => e
  22. puts "The linux network plugin spec tests will fail without the 'ipaddress' library/gem.\n\n"
  23. raise e
  24. end
  25. def do_stubs
  26. @plugin.stub(:collect_os).and_return(:linux)
  27. @plugin.stub(:shell_out).with("ip addr").and_return(mock_shell_out(0, @linux_ip_addr, ""))
  28. @plugin.stub(:shell_out).with("ip -d -s link").and_return(mock_shell_out(0, @linux_ip_link_s_d, ""))
  29. @plugin.stub(:shell_out).with("ip -f inet neigh show").and_return(mock_shell_out(0, @linux_ip_neighbor_show, ""))
  30. @plugin.stub(:shell_out).with("ip -f inet6 neigh show").and_return(mock_shell_out(0, @linux_ip_inet6_neighbor_show, ""))
  31. @plugin.stub(:shell_out).with("ip -f inet route show").and_return(mock_shell_out(0, @linux_ip_route, ""))
  32. @plugin.stub(:shell_out).with("ip -f inet6 route show").and_return(mock_shell_out(0, @linux_ip_route_inet6, ""))
  33. @plugin.stub(:shell_out).with("route -n").and_return(mock_shell_out(0, @linux_route_n, ""))
  34. @plugin.stub(:shell_out).with("ifconfig -a").and_return(mock_shell_out(0, @linux_ifconfig, ""))
  35. @plugin.stub(:shell_out).with("arp -an").and_return(mock_shell_out(0, @linux_arp_an, ""))
  36. end
  37. describe Ohai::System, "Linux Network Plugin" do
  38. before do
  39. @linux_ifconfig = <<-ENDIFCONFIG
  40. eth0 Link encap:Ethernet HWaddr 12:31:3D:02:BE:A2
  41. inet addr:10.116.201.76 Bcast:10.116.201.255 Mask:255.255.255.0
  42. inet6 addr: fe80::1031:3dff:fe02:bea2/64 Scope:Link
  43. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  44. RX packets:2659966 errors:0 dropped:0 overruns:0 frame:0
  45. TX packets:1919690 errors:0 dropped:0 overruns:0 carrier:0
  46. collisions:0 txqueuelen:1000
  47. RX bytes:1392844460 (1.2 GiB) TX bytes:691785313 (659.7 MiB)
  48. Interrupt:16
  49. eth0:5 Link encap:Ethernet HWaddr 00:0c:29:41:71:45
  50. inet addr:192.168.5.1 Bcast:192.168.5.255 Mask:255.255.255.0
  51. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  52. eth0.11 Link encap:Ethernet HWaddr 00:aa:bb:cc:dd:ee
  53. inet addr:192.168.0.16 Bcast:192.168.0.255 Mask:255.255.255.0
  54. inet6 addr: fe80::2aa:bbff:fecc:ddee/64 Scope:Link
  55. inet6 addr: 1111:2222:3333:4444::2/64 Scope:Global
  56. inet6 addr: 1111:2222:3333:4444::3/64 Scope:Global
  57. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  58. RX packets:1208795008 errors:0 dropped:0 overruns:0 frame:0
  59. TX packets:3269635153 errors:0 dropped:0 overruns:0 carrier:0
  60. collisions:0 txqueuelen:0
  61. RX bytes:1751940374 (1.6 GiB) TX bytes:2195567597 (2.0 GiB)
  62. eth0.151 Link encap:Ethernet HWaddr 00:aa:bb:cc:dd:ee
  63. inet addr:10.151.0.16 Bcast:10.151.0.255 Mask:255.255.255.0
  64. inet6 addr: fe80::2aa:bbff:fecc:ddee/64 Scope:Link
  65. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  66. RX packets:206553677 errors:0 dropped:0 overruns:0 frame:0
  67. TX packets:163901336 errors:0 dropped:0 overruns:0 carrier:0
  68. collisions:0 txqueuelen:0
  69. RX bytes:3190792261 (2.9 GiB) TX bytes:755086548 (720.1 MiB)
  70. eth0.152 Link encap:Ethernet HWaddr 00:aa:bb:cc:dd:ee
  71. inet addr:10.152.1.16 Bcast:10.152.3.255 Mask:255.255.252.0
  72. inet6 addr: fe80::2aa:bbff:fecc:ddee/64 Scope:Link
  73. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  74. RX packets:14016741 errors:0 dropped:0 overruns:0 frame:0
  75. TX packets:55232 errors:0 dropped:0 overruns:0 carrier:0
  76. collisions:0 txqueuelen:0
  77. RX bytes:664957462 (634.1 MiB) TX bytes:4876434 (4.6 MiB)
  78. eth0.153 Link encap:Ethernet HWaddr 00:aa:bb:cc:dd:ee
  79. inet addr:10.153.1.16 Bcast:10.153.3.255 Mask:255.255.252.0
  80. inet6 addr: fe80::2aa:bbff:fecc:ddee/64 Scope:Link
  81. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  82. RX packets:2022667595 errors:0 dropped:0 overruns:0 frame:0
  83. TX packets:1798627472 errors:0 dropped:0 overruns:0 carrier:0
  84. collisions:0 txqueuelen:0
  85. RX bytes:4047036732 (3.7 GiB) TX bytes:3451231474 (3.2 GiB)
  86. foo:veth0@eth0 Link encap:Ethernet HWaddr ca:b3:73:8b:0c:e4
  87. BROADCAST MULTICAST MTU:1500 Metric:1
  88. tun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
  89. inet addr:172.16.19.39 P-t-P:172.16.19.1 Mask:255.255.255.255
  90. UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1418 Metric:1
  91. RX packets:57200 errors:0 dropped:0 overruns:0 frame:0
  92. TX packets:13782 errors:0 dropped:0 overruns:0 carrier:0
  93. collisions:0 txqueuelen:100
  94. RX bytes:7377600 (7.0 MiB) TX bytes:1175481 (1.1 MiB)
  95. venet0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
  96. UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1418 Metric:1
  97. RX packets:57200 errors:0 dropped:0 overruns:0 frame:0
  98. TX packets:13782 errors:0 dropped:0 overruns:0 carrier:0
  99. collisions:0 txqueuelen:100
  100. RX bytes:7377600 (7.0 MiB) TX bytes:1175481 (1.1 MiB)
  101. venet0:0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
  102. UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1418 Metric:1
  103. RX packets:57200 errors:0 dropped:0 overruns:0 frame:0
  104. TX packets:13782 errors:0 dropped:0 overruns:0 carrier:0
  105. collisions:0 txqueuelen:100
  106. RX bytes:7377600 (7.0 MiB) TX bytes:1175481 (1.1 MiB)
  107. lo Link encap:Local Loopback
  108. inet addr:127.0.0.1 Mask:255.0.0.0
  109. inet6 addr: ::1/128 Scope:Host
  110. UP LOOPBACK RUNNING MTU:16436 Metric:1
  111. RX packets:524 errors:0 dropped:0 overruns:0 frame:0
  112. TX packets:524 errors:0 dropped:0 overruns:0 carrier:0
  113. collisions:0 txqueuelen:0
  114. RX bytes:35224 (34.3 KiB) TX bytes:35224 (34.3 KiB)
  115. ENDIFCONFIG
  116. # Note that ifconfig shows foo:veth0@eth0 but fails to show any address information.
  117. # This was not a mistake collecting the output and Apparently ifconfig is broken in this regard.
  118. @linux_ip_addr = <<-IP_ADDR
  119. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
  120. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  121. inet 127.0.0.1/8 scope host lo
  122. inet6 ::1/128 scope host
  123. valid_lft forever preferred_lft forever
  124. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
  125. link/ether 12:31:3d:02:be:a2 brd ff:ff:ff:ff:ff:ff
  126. inet 10.116.201.76/24 brd 10.116.201.255 scope global eth0
  127. inet 10.116.201.75/32 scope global eth0
  128. inet 10.116.201.74/24 scope global secondary eth0
  129. inet 192.168.5.1/24 brd 192.168.5.255 scope global eth0:5
  130. inet6 fe80::1031:3dff:fe02:bea2/64 scope link
  131. valid_lft forever preferred_lft forever
  132. inet6 2001:44b8:4160:8f00:a00:27ff:fe13:eacd/64 scope global dynamic
  133. valid_lft 6128sec preferred_lft 2526sec
  134. 3: eth0.11@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
  135. link/ether 00:aa:bb:cc:dd:ee brd ff:ff:ff:ff:ff:ff
  136. inet 192.168.0.16/24 brd 192.168.0.255 scope global eth0.11
  137. inet6 fe80::2e0:81ff:fe2b:48e7/64 scope link
  138. inet6 1111:2222:3333:4444::2/64 scope global
  139. valid_lft forever preferred_lft forever
  140. inet6 1111:2222:3333:4444::3/64 scope global
  141. valid_lft forever preferred_lft forever
  142. 4: eth0.151@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
  143. link/ether 00:aa:bb:cc:dd:ee brd ff:ff:ff:ff:ff:ff
  144. inet 10.151.0.16/24 brd 10.151.0.255 scope global eth0.151
  145. inet 10.151.1.16/24 scope global eth0.151
  146. inet6 fe80::2e0:81ff:fe2b:48e7/64 scope link
  147. valid_lft forever preferred_lft forever
  148. 5: eth0.152@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
  149. link/ether 00:aa:bb:cc:dd:ee brd ff:ff:ff:ff:ff:ff
  150. inet 10.152.1.16/22 brd 10.152.3.255 scope global eth0.152
  151. inet6 fe80::2e0:81ff:fe2b:48e7/64 scope link
  152. valid_lft forever preferred_lft forever
  153. 6: eth0.153@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
  154. link/ether 00:aa:bb:cc:dd:ee brd ff:ff:ff:ff:ff:ff
  155. inet 10.153.1.16/22 brd 10.153.3.255 scope global eth0.153
  156. inet6 fe80::2e0:81ff:fe2b:48e7/64 scope link
  157. valid_lft forever preferred_lft forever
  158. 7: foo:veth0@eth0@veth0: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN
  159. link/ether ca:b3:73:8b:0c:e4 brd ff:ff:ff:ff:ff:ff
  160. inet 192.168.212.2/24 scope global foo:veth0@eth0
  161. 8: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
  162. link/none
  163. inet 172.16.19.39 peer 172.16.19.1 scope global tun0
  164. 9: venet0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
  165. link/void
  166. inet 127.0.0.2/32 scope host venet0
  167. inet 172.16.19.48/32 scope global venet0:0
  168. IP_ADDR
  169. @linux_ip_link_s_d = <<-IP_LINK_S
  170. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
  171. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  172. RX: bytes packets errors dropped overrun mcast
  173. 35224 524 0 0 0 0
  174. TX: bytes packets errors dropped carrier collsns
  175. 35224 524 0 0 0 0
  176. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
  177. link/ether 12:31:3d:02:be:a2 brd ff:ff:ff:ff:ff:ff
  178. RX: bytes packets errors dropped overrun mcast
  179. 1392844460 2659966 0 0 0 0
  180. TX: bytes packets errors dropped carrier collsns
  181. 691785313 1919690 0 0 0 0
  182. 3: eth0.11@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
  183. link/ether 00:0c:29:41:71:45 brd ff:ff:ff:ff:ff:ff
  184. vlan id 11 <REORDER_HDR>
  185. RX: bytes packets errors dropped overrun mcast
  186. 0 0 0 0 0 0
  187. TX: bytes packets errors dropped carrier collsns
  188. 0 0 0 0 0 0
  189. 4: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 100
  190. link/none
  191. RX: bytes packets errors dropped overrun mcast
  192. 1392844460 2659966 0 0 0 0
  193. TX: bytes packets errors dropped carrier collsns
  194. 691785313 1919690 0 0 0 0
  195. 5: venet0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
  196. link/void
  197. RX: bytes packets errors dropped overrun mcast
  198. 1392844460 2659966 0 0 0 0
  199. TX: bytes packets errors dropped carrier collsns
  200. 691785313 1919690 0 0 0 0
  201. IP_LINK_S
  202. @linux_route_n = <<-ROUTE_N
  203. Kernel IP routing table
  204. Destination Gateway Genmask Flags Metric Ref Use Iface
  205. 10.116.201.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
  206. 169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
  207. 0.0.0.0 10.116.201.1 0.0.0.0 UG 0 0 0 eth0
  208. ROUTE_N
  209. @linux_arp_an = <<-ARP_AN
  210. ? (10.116.201.1) at fe:ff:ff:ff:ff:ff [ether] on eth0
  211. ARP_AN
  212. @linux_ip_neighbor_show = <<-NEIGHBOR_SHOW
  213. 10.116.201.1 dev eth0 lladdr fe:ff:ff:ff:ff:ff REACHABLE
  214. NEIGHBOR_SHOW
  215. @linux_ip_inet6_neighbor_show = <<-NEIGHBOR_SHOW
  216. 1111:2222:3333:4444::1 dev eth0.11 lladdr 00:1c:0e:12:34:56 router REACHABLE
  217. fe80::21c:eff:fe12:3456 dev eth0.11 lladdr 00:1c:0e:30:28:00 router REACHABLE
  218. fe80::21c:eff:fe12:3456 dev eth0.153 lladdr 00:1c:0e:30:28:00 router REACHABLE
  219. NEIGHBOR_SHOW
  220. @linux_ip_route = <<-IP_ROUTE_SCOPE
  221. 10.116.201.0/24 dev eth0 proto kernel
  222. 192.168.5.0/24 dev eth0 proto kernel src 192.168.5.1
  223. 192.168.212.0/24 dev foo:veth0@eth0 proto kernel src 192.168.212.2
  224. 172.16.151.0/24 dev eth0 proto kernel src 172.16.151.100
  225. 192.168.0.0/24 dev eth0 proto kernel src 192.168.0.2
  226. default via 10.116.201.1 dev eth0
  227. IP_ROUTE_SCOPE
  228. @linux_ip_route_inet6 = <<-IP_ROUTE_SCOPE
  229. fe80::/64 dev eth0 proto kernel metric 256
  230. fe80::/64 dev eth0.11 proto kernel metric 256
  231. 1111:2222:3333:4444::/64 dev eth0.11 metric 1024 expires 86023sec
  232. default via 1111:2222:3333:4444::1 dev eth0.11 metric 1024
  233. IP_ROUTE_SCOPE
  234. @plugin = get_plugin("linux/network")
  235. @plugin.stub(:shell_out).with("ifconfig -a").and_return([0, @linux_ifconfig, ""])
  236. @plugin.stub(:shell_out).with("arp -an").and_return([0, @linux_arp_an, ""])
  237. end
  238. ["ifconfig","iproute2"].each do |network_method|
  239. describe "gathering IP layer address info via #{network_method}" do
  240. before do
  241. File.stub(:exist?).with("/sbin/ip").and_return( network_method == "iproute2" )
  242. do_stubs
  243. end
  244. it "completes the run" do
  245. Ohai::Log.should_not_receive(:debug).with(/Plugin linux::network threw exception/)
  246. @plugin.run
  247. @plugin['network'].should_not be_nil
  248. end
  249. it "detects the interfaces" do
  250. @plugin.run
  251. @plugin['network']['interfaces'].keys.sort.should == ["eth0", "eth0.11", "eth0.151", "eth0.152", "eth0.153", "eth0:5", "foo:veth0@eth0", "lo", "tun0", "venet0", "venet0:0"]
  252. end
  253. it "detects the ipv4 addresses of the ethernet interface" do
  254. @plugin.run
  255. @plugin['network']['interfaces']['eth0']['addresses'].keys.should include('10.116.201.76')
  256. @plugin['network']['interfaces']['eth0']['addresses']['10.116.201.76']['netmask'].should == '255.255.255.0'
  257. @plugin['network']['interfaces']['eth0']['addresses']['10.116.201.76']['broadcast'].should == '10.116.201.255'
  258. @plugin['network']['interfaces']['eth0']['addresses']['10.116.201.76']['family'].should == 'inet'
  259. end
  260. it "detects the ipv4 addresses of an ethernet subinterface" do
  261. @plugin.run
  262. @plugin['network']['interfaces']['eth0.11']['addresses'].keys.should include('192.168.0.16')
  263. @plugin['network']['interfaces']['eth0.11']['addresses']['192.168.0.16']['netmask'].should == '255.255.255.0'
  264. @plugin['network']['interfaces']['eth0.11']['addresses']['192.168.0.16']['broadcast'].should == '192.168.0.255'
  265. @plugin['network']['interfaces']['eth0.11']['addresses']['192.168.0.16']['family'].should == 'inet'
  266. end
  267. it "detects the ipv6 addresses of the ethernet interface" do
  268. @plugin.run
  269. @plugin['network']['interfaces']['eth0']['addresses'].keys.should include('fe80::1031:3dff:fe02:bea2')
  270. @plugin['network']['interfaces']['eth0']['addresses']['fe80::1031:3dff:fe02:bea2']['scope'].should == 'Link'
  271. @plugin['network']['interfaces']['eth0']['addresses']['fe80::1031:3dff:fe02:bea2']['prefixlen'].should == '64'
  272. @plugin['network']['interfaces']['eth0']['addresses']['fe80::1031:3dff:fe02:bea2']['family'].should == 'inet6'
  273. end
  274. it "detects the ipv6 addresses of an ethernet subinterface" do
  275. @plugin.run
  276. %w[ 1111:2222:3333:4444::2 1111:2222:3333:4444::3 ].each do |addr|
  277. @plugin['network']['interfaces']['eth0.11']['addresses'].keys.should include(addr)
  278. @plugin['network']['interfaces']['eth0.11']['addresses'][addr]['scope'].should == 'Global'
  279. @plugin['network']['interfaces']['eth0.11']['addresses'][addr]['prefixlen'].should == '64'
  280. @plugin['network']['interfaces']['eth0.11']['addresses'][addr]['family'].should == 'inet6'
  281. end
  282. end
  283. it "detects the mac addresses of the ethernet interface" do
  284. @plugin.run
  285. @plugin['network']['interfaces']['eth0']['addresses'].keys.should include('12:31:3D:02:BE:A2')
  286. @plugin['network']['interfaces']['eth0']['addresses']['12:31:3D:02:BE:A2']['family'].should == 'lladdr'
  287. end
  288. it "detects the encapsulation type of the ethernet interface" do
  289. @plugin.run
  290. @plugin['network']['interfaces']['eth0']['encapsulation'].should == 'Ethernet'
  291. end
  292. it "detects the flags of the ethernet interface" do
  293. @plugin.run
  294. if network_method == "ifconfig"
  295. @plugin['network']['interfaces']['eth0']['flags'].sort.should == ['BROADCAST','MULTICAST','RUNNING','UP']
  296. else
  297. @plugin['network']['interfaces']['eth0']['flags'].sort.should == ['BROADCAST','LOWER_UP','MULTICAST','UP']
  298. end
  299. end
  300. it "detects the number of the ethernet interface" do
  301. @plugin.run
  302. @plugin['network']['interfaces']['eth0']['number'].should == "0"
  303. end
  304. it "detects the mtu of the ethernet interface" do
  305. @plugin.run
  306. @plugin['network']['interfaces']['eth0']['mtu'].should == "1500"
  307. end
  308. it "detects the ipv4 addresses of the loopback interface" do
  309. @plugin.run
  310. @plugin['network']['interfaces']['lo']['addresses'].keys.should include('127.0.0.1')
  311. @plugin['network']['interfaces']['lo']['addresses']['127.0.0.1']['netmask'].should == '255.0.0.0'
  312. @plugin['network']['interfaces']['lo']['addresses']['127.0.0.1']['family'].should == 'inet'
  313. end
  314. it "detects the ipv6 addresses of the loopback interface" do
  315. @plugin.run
  316. @plugin['network']['interfaces']['lo']['addresses'].keys.should include('::1')
  317. @plugin['network']['interfaces']['lo']['addresses']['::1']['scope'].should == 'Node'
  318. @plugin['network']['interfaces']['lo']['addresses']['::1']['prefixlen'].should == '128'
  319. @plugin['network']['interfaces']['lo']['addresses']['::1']['family'].should == 'inet6'
  320. end
  321. it "detects the encapsulation type of the loopback interface" do
  322. @plugin.run
  323. @plugin['network']['interfaces']['lo']['encapsulation'].should == 'Loopback'
  324. end
  325. it "detects the flags of the ethernet interface" do
  326. @plugin.run
  327. if network_method == "ifconfig"
  328. @plugin['network']['interfaces']['lo']['flags'].sort.should == ['LOOPBACK','RUNNING','UP']
  329. else
  330. @plugin['network']['interfaces']['lo']['flags'].sort.should == ['LOOPBACK','LOWER_UP','UP']
  331. end
  332. end
  333. it "detects the mtu of the loopback interface" do
  334. @plugin.run
  335. @plugin['network']['interfaces']['lo']['mtu'].should == "16436"
  336. end
  337. it "detects the arp entries" do
  338. @plugin.run
  339. @plugin['network']['interfaces']['eth0']['arp']['10.116.201.1'].should == 'fe:ff:ff:ff:ff:ff'
  340. end
  341. end
  342. describe "gathering interface counters via #{network_method}" do
  343. before do
  344. File.stub(:exist?).with("/sbin/ip").and_return( network_method == "iproute2" )
  345. do_stubs
  346. @plugin.run
  347. end
  348. it "detects the ethernet counters" do
  349. @plugin['counters']['network']['interfaces']['eth0']['tx']['bytes'].should == "691785313"
  350. @plugin['counters']['network']['interfaces']['eth0']['tx']['packets'].should == "1919690"
  351. @plugin['counters']['network']['interfaces']['eth0']['tx']['collisions'].should == "0"
  352. @plugin['counters']['network']['interfaces']['eth0']['tx']['queuelen'].should == "1000"
  353. @plugin['counters']['network']['interfaces']['eth0']['tx']['errors'].should == "0"
  354. @plugin['counters']['network']['interfaces']['eth0']['tx']['carrier'].should == "0"
  355. @plugin['counters']['network']['interfaces']['eth0']['tx']['drop'].should == "0"
  356. @plugin['counters']['network']['interfaces']['eth0']['rx']['bytes'].should == "1392844460"
  357. @plugin['counters']['network']['interfaces']['eth0']['rx']['packets'].should == "2659966"
  358. @plugin['counters']['network']['interfaces']['eth0']['rx']['errors'].should == "0"
  359. @plugin['counters']['network']['interfaces']['eth0']['rx']['overrun'].should == "0"
  360. @plugin['counters']['network']['interfaces']['eth0']['rx']['drop'].should == "0"
  361. end
  362. it "detects the loopback counters" do
  363. @plugin['counters']['network']['interfaces']['lo']['tx']['bytes'].should == "35224"
  364. @plugin['counters']['network']['interfaces']['lo']['tx']['packets'].should == "524"
  365. @plugin['counters']['network']['interfaces']['lo']['tx']['collisions'].should == "0"
  366. @plugin['counters']['network']['interfaces']['lo']['tx']['errors'].should == "0"
  367. @plugin['counters']['network']['interfaces']['lo']['tx']['carrier'].should == "0"
  368. @plugin['counters']['network']['interfaces']['lo']['tx']['drop'].should == "0"
  369. @plugin['counters']['network']['interfaces']['lo']['rx']['bytes'].should == "35224"
  370. @plugin['counters']['network']['interfaces']['lo']['rx']['packets'].should == "524"
  371. @plugin['counters']['network']['interfaces']['lo']['rx']['errors'].should == "0"
  372. @plugin['counters']['network']['interfaces']['lo']['rx']['overrun'].should == "0"
  373. @plugin['counters']['network']['interfaces']['lo']['rx']['drop'].should == "0"
  374. end
  375. end
  376. describe "setting the node's default IP address attribute with #{network_method}" do
  377. before do
  378. File.stub(:exist?).with("/sbin/ip").and_return( network_method == "iproute2" )
  379. do_stubs
  380. end
  381. describe "without a subinterface" do
  382. before do
  383. @plugin.run
  384. end
  385. it "finds the default interface by asking which iface has the default route" do
  386. @plugin['network']['default_interface'].should == 'eth0'
  387. end
  388. it "finds the default gateway by asking which iface has the default route" do
  389. @plugin['network']['default_gateway'].should == '10.116.201.1'
  390. end
  391. end
  392. describe "with a link level default route" do
  393. before do
  394. @linux_ip_route = <<-IP_ROUTE
  395. 10.116.201.0/24 dev eth0 proto kernel
  396. default dev eth0 scope link
  397. IP_ROUTE
  398. @linux_route_n = <<-ROUTE_N
  399. Kernel IP routing table
  400. Destination Gateway Genmask Flags Metric Ref Use Iface
  401. 10.116.201.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
  402. 0.0.0.0 0.0.0.0 0.0.0.0 U 0 0 0 eth0
  403. ROUTE_N
  404. do_stubs
  405. @plugin.run
  406. end
  407. it "finds the default interface by asking which iface has the default route" do
  408. @plugin['network']['default_interface'].should == 'eth0'
  409. end
  410. it "finds the default interface by asking which iface has the default route" do
  411. @plugin['network']['default_gateway'].should == '0.0.0.0'
  412. end
  413. end
  414. describe "with a subinterface" do
  415. before do
  416. @linux_ip_route = <<-IP_ROUTE
  417. 192.168.0.0/24 dev eth0.11 proto kernel src 192.168.0.2
  418. default via 192.168.0.15 dev eth0.11
  419. IP_ROUTE
  420. @linux_route_n = <<-ROUTE_N
  421. Kernel IP routing table
  422. Destination Gateway Genmask Flags Metric Ref Use Iface
  423. 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0.11
  424. 0.0.0.0 192.168.0.15 0.0.0.0 UG 0 0 0 eth0.11
  425. ROUTE_N
  426. do_stubs
  427. @plugin.run
  428. end
  429. it "finds the default interface by asking which iface has the default route" do
  430. @plugin['network']["default_interface"].should == 'eth0.11'
  431. end
  432. it "finds the default interface by asking which iface has the default route" do
  433. @plugin['network']["default_gateway"].should == '192.168.0.15'
  434. end
  435. end
  436. end
  437. end
  438. describe "for newer network features using iproute2 only" do
  439. before do
  440. File.stub(:exist?).with("/sbin/ip").and_return(true) # iproute2 only
  441. do_stubs
  442. end
  443. it "completes the run" do
  444. Ohai::Log.should_not_receive(:debug).with(/Plugin linux::network threw exception/)
  445. @plugin.run
  446. @plugin['network'].should_not be_nil
  447. end
  448. it "finds the default inet6 interface if there's a inet6 default route" do
  449. @plugin.run
  450. @plugin['network']['default_inet6_interface'].should == 'eth0.11'
  451. end
  452. it "finds the default inet6 gateway if there's a inet6 default route" do
  453. @plugin.run
  454. @plugin['network']['default_inet6_gateway'].should == '1111:2222:3333:4444::1'
  455. end
  456. it "finds inet6 neighbours" do
  457. @plugin.run
  458. @plugin['network']['interfaces']['eth0.11']['neighbour_inet6']['1111:2222:3333:4444::1'].should == '00:1c:0e:12:34:56'
  459. end
  460. it "detects the ipv4 addresses of an ethernet interface with a crazy name" do
  461. @plugin.run
  462. @plugin['network']['interfaces']['foo:veth0@eth0']['addresses'].keys.should include('192.168.212.2')
  463. @plugin['network']['interfaces']['foo:veth0@eth0']['addresses']['192.168.212.2']['netmask'].should == '255.255.255.0'
  464. @plugin['network']['interfaces']['foo:veth0@eth0']['addresses']['192.168.212.2']['family'].should == 'inet'
  465. end
  466. it "generates a fake interface for ip aliases for backward compatibility" do
  467. @plugin.run
  468. @plugin['network']['interfaces']['eth0:5']['addresses'].keys.should include('192.168.5.1')
  469. @plugin['network']['interfaces']['eth0:5']['addresses']['192.168.5.1']['netmask'].should == '255.255.255.0'
  470. @plugin['network']['interfaces']['eth0:5']['addresses']['192.168.5.1']['family'].should == 'inet'
  471. end
  472. it "adds the vlan information of an interface" do
  473. @plugin.run
  474. @plugin['network']['interfaces']['eth0.11']['vlan']['id'].should == '11'
  475. @plugin['network']['interfaces']['eth0.11']['vlan']['flags'].should == [ 'REORDER_HDR' ]
  476. end
  477. it "adds the state of an interface" do
  478. @plugin.run
  479. @plugin['network']['interfaces']['eth0.11']['state'].should == 'up'
  480. end
  481. describe "when dealing with routes" do
  482. it "adds routes" do
  483. @plugin.run
  484. @plugin['network']['interfaces']['eth0']['routes'].should include Mash.new( :destination => "10.116.201.0/24", :proto => "kernel", :family =>"inet" )
  485. @plugin['network']['interfaces']['foo:veth0@eth0']['routes'].should include Mash.new( :destination => "192.168.212.0/24", :proto => "kernel", :src => "192.168.212.2", :family =>"inet" )
  486. @plugin['network']['interfaces']['eth0']['routes'].should include Mash.new( :destination => "fe80::/64", :metric => "256", :proto => "kernel", :family => "inet6" )
  487. @plugin['network']['interfaces']['eth0.11']['routes'].should include Mash.new( :destination => "1111:2222:3333:4444::/64", :metric => "1024", :family => "inet6" )
  488. @plugin['network']['interfaces']['eth0.11']['routes'].should include Mash.new( :destination => "default", :via => "1111:2222:3333:4444::1", :metric => "1024", :family => "inet6")
  489. end
  490. describe "when there isn't a source field in route entries " do
  491. it "doesn't set ipaddress" do
  492. @plugin.run
  493. @plugin['ipaddress'].should be nil
  494. end
  495. it "doesn't set macaddress" do
  496. @plugin.run
  497. @plugin['macaddress'].should be nil
  498. end
  499. it "doesn't set ip6address" do
  500. @plugin.run
  501. @plugin['ip6address'].should be nil
  502. end
  503. end
  504. describe "when there's a source field in the default route entry" do
  505. before do
  506. @linux_ip_route = <<-IP_ROUTE_SCOPE
  507. 10.116.201.0/24 dev eth0 proto kernel
  508. 192.168.5.0/24 dev eth0 proto kernel src 192.168.5.1
  509. 192.168.212.0/24 dev foo:veth0@eth0 proto kernel src 192.168.212.2
  510. 172.16.151.0/24 dev eth0 proto kernel src 172.16.151.100
  511. 192.168.0.0/24 dev eth0 proto kernel src 192.168.0.2
  512. default via 10.116.201.1 dev eth0 src 10.116.201.76
  513. IP_ROUTE_SCOPE
  514. @linux_ip_route_inet6 = <<-IP_ROUTE_SCOPE
  515. fe80::/64 dev eth0 proto kernel metric 256
  516. fe80::/64 dev eth0.11 proto kernel metric 256
  517. 1111:2222:3333:4444::/64 dev eth0.11 metric 1024
  518. default via 1111:2222:3333:4444::1 dev eth0.11 metric 1024 src 1111:2222:3333:4444::3
  519. IP_ROUTE_SCOPE
  520. do_stubs
  521. end
  522. it "completes the run" do
  523. Ohai::Log.should_not_receive(:debug).with(/Plugin linux::network threw exception/)
  524. @plugin.run
  525. @plugin['network'].should_not be_nil
  526. end
  527. it "sets ipaddress" do
  528. @plugin.run
  529. @plugin['ipaddress'].should == "10.116.201.76"
  530. end
  531. it "sets ip6address" do
  532. @plugin.run
  533. @plugin['ip6address'].should == "1111:2222:3333:4444::3"
  534. end
  535. end
  536. describe "when there're several default routes" do
  537. before do
  538. @linux_ip_route = <<-IP_ROUTE_SCOPE
  539. 10.116.201.0/24 dev eth0 proto kernel src 10.116.201.76
  540. 192.168.5.0/24 dev eth0 proto kernel src 192.168.5.1
  541. 192.168.212.0/24 dev foo:veth0@eth0 proto kernel src 192.168.212.2
  542. 172.16.151.0/24 dev eth0 proto kernel src 172.16.151.100
  543. 192.168.0.0/24 dev eth0 proto kernel src 192.168.0.2
  544. default via 10.116.201.1 dev eth0 metric 10
  545. default via 10.116.201.254 dev eth0 metric 9
  546. IP_ROUTE_SCOPE
  547. @linux_ip_route_inet6 = <<-IP_ROUTE_SCOPE
  548. fe80::/64 dev eth0 proto kernel metric 256
  549. fe80::/64 dev eth0.11 proto kernel metric 256
  550. 1111:2222:3333:4444::/64 dev eth0.11 metric 1024 src 1111:2222:3333:4444::3
  551. default via 1111:2222:3333:4444::1 dev eth0.11 metric 1024
  552. default via 1111:2222:3333:4444::ffff dev eth0.11 metric 1023
  553. IP_ROUTE_SCOPE
  554. do_stubs
  555. end
  556. it "completes the run" do
  557. Ohai::Log.should_not_receive(:debug).with(/Plugin linux::network threw exception/)
  558. @plugin.run
  559. @plugin['network'].should_not be_nil
  560. end
  561. it "sets default ipv4 interface and gateway" do
  562. @plugin.run
  563. @plugin['network']['default_interface'].should == 'eth0'
  564. @plugin['network']['default_gateway'].should == '10.116.201.254'
  565. end
  566. it "sets default ipv6 interface and gateway" do
  567. @plugin.run
  568. @plugin['network']['default_inet6_interface'].should == 'eth0.11'
  569. @plugin['network']['default_inet6_gateway'].should == '1111:2222:3333:4444::ffff'
  570. end
  571. end
  572. describe "when there're a mixed setup of routes that could be used to set ipaddress" do
  573. before do
  574. @linux_ip_route = <<-IP_ROUTE_SCOPE
  575. 10.116.201.0/24 dev eth0 proto kernel src 10.116.201.76
  576. 192.168.5.0/24 dev eth0 proto kernel src 192.168.5.1
  577. 192.168.212.0/24 dev foo:veth0@eth0 proto kernel src 192.168.212.2
  578. 172.16.151.0/24 dev eth0 proto kernel src 172.16.151.100
  579. 192.168.0.0/24 dev eth0 proto kernel src 192.168.0.2
  580. default via 10.116.201.1 dev eth0 metric 10
  581. default via 10.116.201.254 dev eth0 metric 9 src 10.116.201.74
  582. IP_ROUTE_SCOPE
  583. @linux_ip_route_inet6 = <<-IP_ROUTE_SCOPE
  584. fe80::/64 dev eth0 proto kernel metric 256
  585. fe80::/64 dev eth0.11 proto kernel metric 256
  586. 1111:2222:3333:4444::/64 dev eth0.11 metric 1024 src 1111:2222:3333:4444::3
  587. default via 1111:2222:3333:4444::1 dev eth0.11 metric 1024
  588. default via 1111:2222:3333:4444::ffff dev eth0.11 metric 1023 src 1111:2222:3333:4444::2
  589. IP_ROUTE_SCOPE
  590. do_stubs
  591. end
  592. it "completes the run" do
  593. Ohai::Log.should_not_receive(:debug).with(/Plugin linux::network threw exception/)
  594. @plugin.run
  595. @plugin['network'].should_not be_nil
  596. end
  597. it "sets ipaddress" do
  598. @plugin.run
  599. @plugin["ipaddress"].should == "10.116.201.74"
  600. end
  601. it "sets ip6address" do
  602. @plugin.run
  603. @plugin["ip6address"].should == "1111:2222:3333:4444::2"
  604. end
  605. end
  606. describe "when there's a source field in a local route entry " do
  607. before do
  608. @linux_ip_route = <<-IP_ROUTE_SCOPE
  609. 10.116.201.0/24 dev eth0 proto kernel src 10.116.201.76
  610. 192.168.5.0/24 dev eth0 proto kernel src 192.168.5.1
  611. 192.168.212.0/24 dev foo:veth0@eth0 proto kernel src 192.168.212.2
  612. 172.16.151.0/24 dev eth0 proto kernel src 172.16.151.100
  613. 192.168.0.0/24 dev eth0 proto kernel src 192.168.0.2
  614. default via 10.116.201.1 dev eth0
  615. IP_ROUTE_SCOPE
  616. @linux_ip_route_inet6 = <<-IP_ROUTE_SCOPE
  617. fe80::/64 dev eth0 proto kernel metric 256
  618. fe80::/64 dev eth0.11 proto kernel metric 256
  619. 1111:2222:3333:4444::/64 dev eth0.11 metric 1024 src 1111:2222:3333:4444::3
  620. default via 1111:2222:3333:4444::1 dev eth0.11 metric 1024
  621. IP_ROUTE_SCOPE
  622. do_stubs
  623. end
  624. it "completes the run" do
  625. Ohai::Log.should_not_receive(:debug).with(/Plugin linux::network threw exception/)
  626. @plugin.run
  627. @plugin['network'].should_not be_nil
  628. end
  629. it "sets ipaddress" do
  630. @plugin.run
  631. @plugin['ipaddress'].should == "10.116.201.76"
  632. end
  633. describe "when about to set macaddress" do
  634. it "sets macaddress" do
  635. @plugin.run
  636. @plugin['macaddress'].should == "12:31:3D:02:BE:A2"
  637. end
  638. describe "when then interface has the NOARP flag" do
  639. before do
  640. @linux_ip_route = <<-IP_ROUTE
  641. 10.118.19.1 dev tun0 proto kernel src 10.118.19.39
  642. default via 172.16.19.1 dev tun0
  643. IP_ROUTE
  644. do_stubs
  645. end
  646. it "completes the run" do
  647. Ohai::Log.should_not_receive(:debug).with(/Plugin linux::network threw exception/)
  648. @plugin.run
  649. @plugin['network'].should_not be_nil
  650. end
  651. it "doesn't set macaddress" do
  652. @plugin.run
  653. @plugin['macaddress'].should be_nil
  654. end
  655. end
  656. end
  657. it "sets ip6address" do
  658. @plugin.run
  659. @plugin['ip6address'].should == "1111:2222:3333:4444::3"
  660. end
  661. end
  662. describe "with a link level default route" do
  663. before do
  664. @linux_ip_route = <<-IP_ROUTE
  665. default dev venet0 scope link
  666. IP_ROUTE
  667. do_stubs
  668. end
  669. it "completes the run" do
  670. Ohai::Log.should_not_receive(:debug).with(/Plugin linux::network threw exception/)
  671. @plugin.run
  672. @plugin['network'].should_not be_nil
  673. end
  674. it "doesn't set ipaddress" do
  675. @plugin.run
  676. @plugin['ipaddress'].should be_nil
  677. end
  678. end
  679. describe "when not having a global scope ipv6 address" do
  680. before do
  681. @linux_ip_route_inet6 = <<-IP_ROUTE_SCOPE
  682. fe80::/64 dev eth0 proto kernel metric 256
  683. default via fe80::21c:eff:fe12:3456 dev eth0.153 src fe80::2e0:81ff:fe2b:48e7 metric 1024
  684. IP_ROUTE_SCOPE
  685. do_stubs
  686. end
  687. it "completes the run" do
  688. Ohai::Log.should_not_receive(:debug).with(/Plugin linux::network threw exception/)
  689. @plugin.run
  690. @plugin['network'].should_not be_nil
  691. end
  692. it "doesn't set ip6address" do
  693. @plugin.run
  694. @plugin['ip6address'].should be_nil
  695. end
  696. end
  697. describe "with no default route" do
  698. before do
  699. @linux_ip_route = <<-IP_ROUTE
  700. 10.116.201.0/24 dev eth0 proto kernel src 10.116.201.76
  701. 192.168.5.0/24 dev eth0 proto kernel src 192.168.5.1
  702. 192.168.212.0/24 dev foo:veth0@eth0 proto kernel src 192.168.212.2
  703. 172.16.151.0/24 dev eth0 proto kernel src 172.16.151.100
  704. 192.168.0.0/24 dev eth0 proto kernel src 192.168.0.2
  705. IP_ROUTE
  706. @linux_ip_route_inet6 = <<-IP_ROUTE
  707. fe80::/64 dev eth0 proto kernel metric 256
  708. fe80::/64 dev eth0.11 proto kernel metric 256
  709. 1111:2222:3333:4444::/64 dev eth0.11 metric 1024 src 1111:2222:3333:4444::3
  710. IP_ROUTE
  711. do_stubs
  712. end
  713. it "completes the run" do
  714. Ohai::Log.should_not_receive(:debug).with(/Plugin linux::network threw exception/)
  715. @plugin.run
  716. @plugin['network'].should_not be_nil
  717. end
  718. it "doesn't set ipaddress" do
  719. @plugin.run
  720. @plugin['ipaddress'].should be_nil
  721. end
  722. it "doesn't set ip6address" do
  723. @plugin.run
  724. @plugin['ip6address'].should be_nil
  725. end
  726. end
  727. describe "with irrelevant routes (container setups)" do
  728. before do
  729. @linux_ip_route = <<-IP_ROUTE
  730. 10.116.201.0/26 dev eth0 proto kernel src 10.116.201.39
  731. 10.116.201.0/26 dev if4 proto kernel src 10.116.201.45
  732. 10.118.19.0/26 dev eth0 proto kernel src 10.118.19.39
  733. 10.118.19.0/26 dev if5 proto kernel src 10.118.19.45
  734. default via 10.116.201.1 dev eth0 src 10.116.201.99
  735. IP_ROUTE
  736. @linux_ip_route_inet6 = <<-IP_ROUTE
  737. fe80::/64 dev eth0 proto kernel metric 256
  738. fe80::/64 dev eth0.11 proto kernel metric 256
  739. 1111:2222:3333:4444::/64 dev eth0.11 metric 1024 src 1111:2222:3333:4444::FFFF:2
  740. default via 1111:2222:3333:4444::1 dev eth0.11 metric 1024
  741. IP_ROUTE
  742. do_stubs
  743. end
  744. it "completes the run" do
  745. Ohai::Log.should_not_receive(:debug).with(/Plugin linux::network threw exception/)
  746. @plugin.run
  747. @plugin['network'].should_not be_nil
  748. end
  749. it "doesn't add bogus routes" do
  750. @plugin.run
  751. @plugin['network']['interfaces']['eth0']['routes'].should_not include Mash.new( :destination => "10.116.201.0/26", :proto => "kernel", :family => "inet", :via => "10.116.201.39" )
  752. @plugin['network']['interfaces']['eth0']['routes'].should_not include Mash.new( :destination => "10.118.19.0/26", :proto => "kernel", :family => "inet", :via => "10.118.19.39" )
  753. @plugin['network']['interfaces']['eth0']['routes'].should_not include Mash.new( :destination => "1111:2222:3333:4444::/64", :family => "inet6", :metric => "1024" )
  754. end
  755. it "doesn't set ipaddress" do
  756. @plugin.run
  757. @plugin['ipaddress'].should be_nil
  758. end
  759. it "doesn't set ip6address" do
  760. @plugin.run
  761. @plugin['ip6address'].should be_nil
  762. end
  763. end
  764. # This should never happen in the real world.
  765. describe "when encountering a surprise interface" do
  766. before do
  767. @linux_ip_route = <<-IP_ROUTE
  768. 192.168.122.0/24 dev virbr0 proto kernel src 192.168.122.1
  769. IP_ROUTE
  770. do_stubs
  771. end
  772. it "logs a message and skips previously unseen interfaces in 'ip route show'" do
  773. Ohai::Log.should_receive(:debug).with("Skipping previously unseen interface from 'ip route show': virbr0").once
  774. Ohai::Log.stub(:debug) # Catches the 'Loading plugin network' type messages
  775. @plugin.run
  776. end
  777. end
  778. end
  779. end
  780. end