PageRenderTime 186ms CodeModel.GetById 12ms RepoModel.GetById 3ms app.codeStats 0ms

/spec/unit/plugins/network_spec.rb

https://github.com/wistia/ohai
Ruby | 1004 lines | 918 code | 64 blank | 22 comment | 44 complexity | 50ec0e292bd297d1f7262ffaa77fe0bb MD5 | raw file
Possible License(s): Apache-2.0
  1. #
  2. # Author:: Laurent Desarmes <laurent.desarmes@u-picardie.fr>
  3. # Copyright:: Copyright (c) 2012 Laurent Desarmes
  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. def it_doesnt_fail
  20. it "doesnt fail" do
  21. Ohai::Log.stub(:warn)
  22. Ohai::Log.should_not_receive(:debug).with(/^Plugin network threw exception/)
  23. @plugin.run
  24. end
  25. end
  26. def it_populates_ipaddress_attributes
  27. source = caller[0]
  28. it "populates ipaddress, macaddress and ip6address" do
  29. begin
  30. Ohai::Log.stub(:warn)
  31. Ohai::Log.should_not_receive(:debug).with(/^Plugin network threw exception/)
  32. @plugin.run
  33. %w{ ipaddress macaddress ip6address }.each do |attribute|
  34. @plugin.should have_key(attribute)
  35. end
  36. rescue Exception
  37. puts "RSpec context: #{source}"
  38. raise
  39. end
  40. end
  41. end
  42. describe Ohai::System, "Network Plugin" do
  43. basic_data = {
  44. "freebsd" => {
  45. "network" => {
  46. "interfaces" => {
  47. "vr0" => {
  48. "type" => "vr",
  49. "number" => "0",
  50. "flags" => ["UP", "BROADCAST", "RUNNING", "SIMPLEX", "MULTICAST"],
  51. "addresses" => {
  52. "00:00:24:c9:5e:b8" => {"family" => "lladdr"},
  53. "fe80::200:24ff:fec9:5eb8" => {
  54. "family" => "inet6",
  55. "zoneid" => "vr0",
  56. "prefixlen" => "64",
  57. "scopeid" => "0x1"
  58. },
  59. "76.91.1.255" => {
  60. "family" => "inet",
  61. "netmask" => "255.255.252.0",
  62. "broadcast" => "255.255.255.255"
  63. }
  64. },
  65. "arp" => {
  66. "76.91.1.255" => "00:00:24:c9:5e:b8",
  67. "76.91.0.1" => "00:01:5c:24:8c:01"
  68. }
  69. },
  70. "vr1" => {
  71. "type" => "vr",
  72. "number" => "1",
  73. "flags" => ["UP", "BROADCAST", "RUNNING", "PROMISC", "SIMPLEX", "MULTICAST"],
  74. "addresses" => {
  75. "00:00:24:c9:5e:b9" => {"family" => "lladdr"},
  76. "fe80::200:24ff:fec9:5eb9" => {
  77. "family" => "inet6",
  78. "zoneid" => "vr1",
  79. "prefixlen" => "64",
  80. "scopeid" => "0x2"
  81. }
  82. }
  83. },
  84. "vr2" => {
  85. "type" => "vr",
  86. "number" => "2",
  87. "flags" => ["UP", "BROADCAST", "RUNNING", "PROMISC", "SIMPLEX", "MULTICAST"],
  88. "addresses" => {
  89. "00:00:24:c9:5e:ba" => {"family" => "lladdr"},
  90. "fe80::200:24ff:fec9:5eba" => {
  91. "family" => "inet6",
  92. "zoneid" => "vr2",
  93. "prefixlen" => "64",
  94. "scopeid" => "0x3"
  95. }
  96. }
  97. },
  98. "vr3" => {
  99. "type" => "vr",
  100. "number" => "3",
  101. "flags" => ["UP", "BROADCAST", "RUNNING", "PROMISC", "SIMPLEX", "MULTICAST"],
  102. "addresses" => {
  103. "00:00:24:c9:5e:bb" => {"family" => "lladdr"},
  104. "fe80::200:24ff:fec9:5ebb" => {
  105. "family" => "inet6",
  106. "zoneid" => "vr3",
  107. "prefixlen" => "64",
  108. "scopeid" => "0x4"
  109. }
  110. }
  111. },
  112. "ipfw0" => {
  113. # OHAI-492: Ensure network plugin works with interfaces without addresses.
  114. "type" => "ipfw",
  115. "number" => "0",
  116. "flags" => ["UP", "SIMPLEX", "MULTICAST"]
  117. },
  118. "lo0" => {
  119. "type" => "lo",
  120. "number" => "0",
  121. "flags" => ["UP", "LOOPBACK", "RUNNING", "MULTICAST"],
  122. "addresses" => {
  123. "127.0.0.1" => {"family" => "inet", "netmask" => "255.0.0.0"},
  124. "::1" => {"family" => "inet6", "prefixlen" => "128"},
  125. "fe80::1" => {
  126. "family" => "inet6",
  127. "zoneid" => "lo0",
  128. "prefixlen" => "64",
  129. "scopeid" => "0x8"
  130. }
  131. }
  132. },
  133. "bridge0" => {
  134. "type" => "bridge",
  135. "number" => "0",
  136. "flags" => ["LEARNING", "DISCOVER", "AUTOEDGE", "AUTOPTP"],
  137. "addresses" => {
  138. "02:20:6f:d2:c4:00" => {"family"=>"lladdr"},
  139. "192.168.2.1" => {
  140. "family" => "inet",
  141. "netmask" => "255.255.255.0",
  142. "broadcast" => "192.168.2.255"
  143. },
  144. "2001:470:d:cb4::1" => {"family" => "inet6", "prefixlen" => "64"},
  145. "fe80::cafe:babe:dead:beef" => {
  146. "family" => "inet6",
  147. "zoneid" => "bridge0",
  148. "prefixlen" => "64",
  149. "scopeid" => "0x9"
  150. }
  151. },
  152. "arp" => {
  153. "192.168.2.142" => "60:67:20:75:a2:0c",
  154. "192.168.2.205" => "c0:c1:c0:f9:40:ed",
  155. "192.168.2.160" => "cc:3a:61:cf:67:13",
  156. "192.168.2.1" => "02:20:6f:d2:c4:00",
  157. "192.168.2.135" => "f8:0c:f3:d7:c6:b6",
  158. "192.168.2.165" => "f8:8f:ca:24:49:ad",
  159. "192.168.2.158" => "48:5d:60:1f:ea:d1",
  160. "192.168.2.150" => "60:a4:4c:60:b3:d9"
  161. }
  162. },
  163. "gif0" => {
  164. "type" => "gif",
  165. "number" => "0",
  166. "flags" => ["UP", "POINTOPOINT", "RUNNING", "MULTICAST"],
  167. "addresses" => {
  168. "fe80::200:24ff:fec9:5eb8" => {
  169. "family" => "inet6",
  170. "zoneid" => "gif0",
  171. "prefixlen" => "64",
  172. "scopeid" => "0xa"
  173. }
  174. }
  175. }
  176. },
  177. "default_gateway" => "76.91.0.1",
  178. "default_interface" => "vr0"
  179. }
  180. },
  181. "linux" => {
  182. "network" => {
  183. # pp Hash[node['network']] from shef to get the network data
  184. # have just removed the neighbour and route entries by hand
  185. "interfaces" => {
  186. "lo" => {
  187. "flags" => ["LOOPBACK", "UP"],
  188. "addresses" => {
  189. "::1" => {
  190. "scope" => "Node",
  191. "prefixlen" => "128",
  192. "family" => "inet6"
  193. },
  194. "127.0.0.1" => {
  195. "scope" => "Node",
  196. "netmask" => "255.0.0.0",
  197. "prefixlen" => "8",
  198. "family" => "inet"
  199. }
  200. },
  201. "mtu" => "16436",
  202. "encapsulation" => "Loopback"
  203. },
  204. "eth0" => {
  205. "flags" => ["BROADCAST", "MULTICAST", "UP"],
  206. "number" => "0",
  207. "addresses" => {
  208. "fe80::216:3eff:fe2f:3679" => {
  209. "scope" => "Link",
  210. "prefixlen" => "64",
  211. "family" => "inet6"
  212. },
  213. "00:16:3E:2F:36:79" => {"family" => "lladdr"},
  214. "192.168.66.33" => {
  215. "scope" => "Global",
  216. "netmask" => "255.255.255.0",
  217. "broadcast" => "192.168.66.255",
  218. "prefixlen" => "24",
  219. "family" => "inet"
  220. },
  221. "3ffe:1111:2222::33" => {
  222. "prefixlen" => "48",
  223. "family" => "inet6",
  224. "scope" => "Global"
  225. }
  226. },
  227. "mtu" => "1500",
  228. "type" => "eth",
  229. "encapsulation" => "Ethernet"
  230. },
  231. "eth1" => {
  232. "flags" => ["BROADCAST", "MULTICAST", "UP"],
  233. "number" => "1",
  234. "addresses" => {
  235. "fe80::216:3eff:fe2f:3680" => {
  236. "scope" => "Link",
  237. "prefixlen" => "64",
  238. "family" => "inet6"
  239. },
  240. "00:16:3E:2F:36:80" => {"family" => "lladdr"},
  241. "192.168.99.11" => {
  242. "scope" => "Global",
  243. "netmask" => "255.255.255.0",
  244. "broadcast" => "192.168.99.255",
  245. "prefixlen" => "24",
  246. "family" => "inet"
  247. },
  248. "3ffe:1111:3333::1" => {
  249. "prefixlen" => "48",
  250. "family" => "inet6",
  251. "scope" => "Global"
  252. }
  253. },
  254. "mtu" => "1500",
  255. "type" => "eth",
  256. "encapsulation" => "Ethernet"
  257. }
  258. },
  259. "default_gateway" => "192.168.66.15",
  260. "default_interface" => "eth0",
  261. "default_inet6_gateway" => "3ffe:1111:2222::",
  262. "default_inet6_interface" => "eth0"
  263. }
  264. },
  265. "windows" => {
  266. "network" => {
  267. "interfaces" => {
  268. "0xb" => {
  269. "addresses" => {
  270. "172.19.0.130" => {
  271. "prefixlen" => "24",
  272. "netmask" => "255.255.255.0",
  273. "broadcast" => "172.19.0.255",
  274. "family" => "inet"
  275. },
  276. "fe80::698d:3e37:7950:b28c" => {
  277. "prefixlen" => "64",
  278. "family" => "inet6",
  279. "scope" => "Link"
  280. },
  281. "52:54:44:66:66:02" => {
  282. "family" => "lladdr"
  283. }
  284. },
  285. "mtu" => nil,
  286. "type" => "Ethernet 802.3",
  287. "encapsulation" => "Ethernet"
  288. }
  289. },
  290. "default_gateway" => "172.19.0.1",
  291. "default_interface" => "0xb"
  292. }
  293. }
  294. }
  295. describe "with linux" do
  296. before(:each) do
  297. @plugin = get_plugin("network")
  298. @plugin["network"] = basic_data["linux"]["network"]
  299. end
  300. describe "when the linux::network plugin hasn't set any of {ip,ip6,mac}address attributes" do
  301. describe "simple setup" do
  302. it_populates_ipaddress_attributes
  303. it "detects {ip,ip6,mac}address" do
  304. @plugin.run
  305. @plugin["ipaddress"].should == "192.168.66.33"
  306. @plugin["macaddress"].should == "00:16:3E:2F:36:79"
  307. @plugin["ip6address"].should == "3ffe:1111:2222::33"
  308. end
  309. end
  310. describe "default ipv4 and ipv6 gateway on different interfaces" do
  311. describe "both interfaces have an ARP" do
  312. before do
  313. @plugin["network"]["default_inet6_gateway"] = "3ffe:1111:3333::"
  314. @plugin["network"]["default_inet6_interface"] = "eth1"
  315. end
  316. it_populates_ipaddress_attributes
  317. it "detects {ip,ip6}address" do
  318. @plugin.run
  319. @plugin["ipaddress"].should == "192.168.66.33"
  320. @plugin["ip6address"].should == "3ffe:1111:3333::1"
  321. end
  322. it "set macaddress from the ipv4 setup" do
  323. @plugin.run
  324. @plugin["macaddress"].should == "00:16:3E:2F:36:79"
  325. end
  326. it "informs about this setup" do
  327. Ohai::Log.should_receive(:debug).with(/^ipaddress and ip6address are set from different interfaces/)
  328. Ohai::Log.stub(:debug)
  329. @plugin.run
  330. end
  331. end
  332. describe "ipv4 interface has no ARP" do
  333. before do
  334. @plugin["network"]["interfaces"]["eth0"]["addresses"].delete_if{|k,kv| kv["family"] == "lladdr" }
  335. # not really checked by this pluging
  336. @plugin["network"]["interfaces"]["eth0"]["flags"] << "NOARP"
  337. @plugin["network"]["default_inet6_gateway"] = "3ffe:1111:3333::"
  338. @plugin["network"]["default_inet6_interface"] = "eth1"
  339. end
  340. it_populates_ipaddress_attributes
  341. it "detects {ip,ip6}address" do
  342. @plugin.run
  343. @plugin["ipaddress"].should == "192.168.66.33"
  344. @plugin["ip6address"].should == "3ffe:1111:3333::1"
  345. end
  346. it "doesn't set macaddress, ipv4 setup is valid and has precedence over ipv6" do
  347. Ohai::Log.should_not_receive(:warn).with(/^unable to detect macaddress/)
  348. @plugin.run
  349. @plugin["macaddress"].should be_nil
  350. end
  351. it "informs about this setup" do
  352. Ohai::Log.should_receive(:debug).with(/^ipaddress and ip6address are set from different interfaces/)
  353. Ohai::Log.stub(:debug)
  354. @plugin.run
  355. end
  356. end
  357. end
  358. describe "conflicting results from the linux::network plugin" do
  359. describe "default interface doesn't match the default_gateway" do
  360. before do
  361. @plugin["network"]["default_interface"] = "eth1"
  362. @plugin["network"]["default_inet6_interface"] = "eth1"
  363. end
  364. it_populates_ipaddress_attributes
  365. it "picks {ip,ip6,mac}address" do
  366. Ohai::Log.stub(:warn)
  367. @plugin.run
  368. @plugin["ipaddress"].should == "192.168.99.11"
  369. @plugin["macaddress"].should == "00:16:3E:2F:36:80"
  370. @plugin["ip6address"].should == "3ffe:1111:3333::1"
  371. end
  372. it "warns about this conflict" do
  373. Ohai::Log.should_receive(:warn).with(/^\[inet\] no ipaddress\/mask on eth1/).once
  374. Ohai::Log.should_receive(:warn).with(/^\[inet6\] no ipaddress\/mask on eth1/).once
  375. @plugin.run
  376. end
  377. end
  378. describe "there's a default gateway, none of the configured ip/mask theorically allows to reach it" do
  379. before do
  380. @plugin["network"]["default_gateway"] = "172.16.12.42"
  381. @plugin["network"]["default_inet6_gateway"] = "3ffe:12:42::7070"
  382. end
  383. it "picks {ip,ip6,mac}address" do
  384. Ohai::Log.stub(:warn)
  385. @plugin.run
  386. @plugin["ipaddress"].should == "192.168.66.33"
  387. @plugin["macaddress"].should == "00:16:3E:2F:36:79"
  388. @plugin["ip6address"].should == "3ffe:1111:2222::33"
  389. end
  390. end
  391. describe "no ip address for the given default interface/gateway" do
  392. before do
  393. @plugin["network"]["interfaces"]["eth0"]["addresses"].delete_if{|k,v| %w[inet inet6].include? v["family"]}
  394. end
  395. it_doesnt_fail
  396. it "doesn't detect {ip,ip6,mac}address" do
  397. Ohai::Log.stub(:warn)
  398. @plugin.run
  399. @plugin["ipaddress"].should be_nil
  400. @plugin["macaddress"].should be_nil
  401. @plugin["ip6address"].should be_nil
  402. end
  403. it "warns about this conflict" do
  404. Ohai::Log.should_receive(:warn).with(/^unable to detect ipaddress/).once
  405. Ohai::Log.should_receive(:warn).with(/^unable to detect macaddress/).once
  406. Ohai::Log.should_receive(:warn).with(/^\[inet\] no ip address on eth0/).once
  407. Ohai::Log.should_receive(:debug).with(/^unable to detect ip6address/).once
  408. Ohai::Log.should_receive(:warn).with(/^\[inet6\] no ip address on eth0/).once
  409. @plugin.run
  410. end
  411. end
  412. describe "no ip at all" do
  413. before do
  414. @plugin["network"]["default_gateway"] = nil
  415. @plugin["network"]["default_interface"] = nil
  416. @plugin["network"]["default_inet6_gateway"] = nil
  417. @plugin["network"]["default_inet6_interface"] = nil
  418. @plugin["network"]["interfaces"].each do |i,iv|
  419. iv["addresses"].delete_if{|k,kv| %w[inet inet6].include? kv["family"]}
  420. end
  421. end
  422. it_doesnt_fail
  423. it "doesn't detect {ip,ip6,mac}address" do
  424. Ohai::Log.stub(:warn)
  425. @plugin.run
  426. @plugin["ipaddress"].should be_nil
  427. @plugin["macaddress"].should be_nil
  428. @plugin["ip6address"].should be_nil
  429. end
  430. it "should warn about it" do
  431. Ohai::Log.should_receive(:warn).with(/^unable to detect ipaddress/).once
  432. Ohai::Log.should_receive(:warn).with(/^unable to detect macaddress/).once
  433. Ohai::Log.should_receive(:debug).with(/^unable to detect ip6address/).once
  434. @plugin.run
  435. end
  436. end
  437. end
  438. describe "several ipaddresses matching the default route" do
  439. describe "bigger prefix not set on the default interface" do
  440. before do
  441. @plugin["network"]["interfaces"]["eth2"] = {
  442. "flags" => ["BROADCAST", "MULTICAST", "UP"],
  443. "number" => "2",
  444. "addresses" => {
  445. "fe80::216:3eff:fe2f:3681" => {
  446. "scope" => "Link",
  447. "prefixlen" => "64",
  448. "family" => "inet6"
  449. },
  450. "00:16:3E:2F:36:81" => {"family" => "lladdr"},
  451. "192.168.66.99" => {
  452. "scope" => "Global",
  453. "netmask" => "255.255.255.128",
  454. "broadcast" => "192.168.99.127",
  455. "prefixlen" => "25",
  456. "family" => "inet"
  457. },
  458. "3ffe:1111:2222:0:4444::1" => {
  459. "prefixlen" => "64",
  460. "family" => "inet6",
  461. "scope" => "Global"
  462. }
  463. }
  464. }
  465. end
  466. it_populates_ipaddress_attributes
  467. it "sets {ip,ip6,mac}address correctly" do
  468. @plugin.run
  469. @plugin["ipaddress"].should == "192.168.66.33"
  470. @plugin["macaddress"].should == "00:16:3E:2F:36:79"
  471. @plugin["ip6address"].should == "3ffe:1111:2222::33"
  472. end
  473. end
  474. describe "bigger prefix set on the default interface" do
  475. before do
  476. @plugin["network"]["interfaces"]["eth0"]["addresses"]["192.168.66.99"] = {
  477. "scope" => "Global",
  478. "netmask" => "255.255.255.128",
  479. "broadcast" => "192.168.66.127",
  480. "prefixlen" => "25",
  481. "family" => "inet"
  482. }
  483. @plugin["network"]["interfaces"]["eth0"]["addresses"]["3ffe:1111:2222:0:4444::1"] = {
  484. "prefixlen" => "64",
  485. "family" => "inet6",
  486. "scope" => "Global"
  487. }
  488. end
  489. it_populates_ipaddress_attributes
  490. it "sets {ip,ip6,mac}address correctly" do
  491. @plugin.run
  492. @plugin["ipaddress"].should == "192.168.66.99"
  493. @plugin["macaddress"].should == "00:16:3E:2F:36:79"
  494. @plugin["ip6address"].should == "3ffe:1111:2222:0:4444::1"
  495. end
  496. end
  497. describe "smallest ip not set on the default_interface" do
  498. before do
  499. @plugin["network"]["interfaces"]["eth2"] = {
  500. "flags" => ["BROADCAST", "MULTICAST", "UP"],
  501. "number" => "2",
  502. "addresses" => {
  503. "fe80::216:3eff:fe2f:3681" => {
  504. "scope" => "Link",
  505. "prefixlen" => "64",
  506. "family" => "inet6"
  507. },
  508. "00:16:3E:2F:36:81" => {"family" => "lladdr"},
  509. "192.168.66.32" => {
  510. "scope" => "Global",
  511. "netmask" => "255.255.255.0",
  512. "broadcast" => "192.168.66.255",
  513. "prefixlen" => "24",
  514. "family" => "inet"
  515. },
  516. "3ffe:1111:2222::32" => {
  517. "prefixlen" => "48",
  518. "family" => "inet6",
  519. "scope" => "Global"
  520. }
  521. }
  522. }
  523. end
  524. it_populates_ipaddress_attributes
  525. it "sets {ip,ip6,mac}address correctly" do
  526. @plugin.run
  527. @plugin["ipaddress"].should == "192.168.66.33"
  528. @plugin["macaddress"].should == "00:16:3E:2F:36:79"
  529. @plugin["ip6address"].should == "3ffe:1111:2222::33"
  530. end
  531. end
  532. describe "smallest ip set on the default_interface" do
  533. before do
  534. @plugin["network"]["interfaces"]["eth0"]["addresses"]["192.168.66.32"] = {
  535. "scope" => "Global",
  536. "netmask" => "255.255.255.0",
  537. "broadcast" => "192.168.66.255",
  538. "prefixlen" => "24",
  539. "family" => "inet"
  540. }
  541. @plugin["network"]["interfaces"]["eth0"]["addresses"]["3ffe:1111:2222::32"] = {
  542. "prefixlen" => "48",
  543. "family" => "inet6",
  544. "scope" => "Global"
  545. }
  546. end
  547. it_populates_ipaddress_attributes
  548. it "sets {ip,ip6,mac}address correctly" do
  549. @plugin.run
  550. @plugin["ipaddress"].should == "192.168.66.32"
  551. @plugin["macaddress"].should == "00:16:3E:2F:36:79"
  552. @plugin["ip6address"].should == "3ffe:1111:2222::32"
  553. end
  554. end
  555. end
  556. describe "no default route" do
  557. describe "first interface is not the best choice" do
  558. before do
  559. @plugin["network"]["default_gateway"] = nil
  560. @plugin["network"]["default_interface"] = nil
  561. @plugin["network"]["default_inet6_gateway"] = nil
  562. @plugin["network"]["default_inet6_interface"] = nil
  563. # removing inet* addresses from eth0, to complicate things a bit
  564. @plugin["network"]["interfaces"]["eth0"]["addresses"].delete_if{|k,v| %w[inet inet6].include? v["family"]}
  565. end
  566. it_populates_ipaddress_attributes
  567. it "picks {ip,mac,ip6}address from the first interface" do
  568. Ohai::Log.should_receive(:debug).with(/^\[inet\] no default interface/).once
  569. Ohai::Log.should_receive(:debug).with(/^\[inet6\] no default interface/).once
  570. Ohai::Log.stub(:debug)
  571. @plugin.run
  572. @plugin["ipaddress"].should == "192.168.99.11"
  573. @plugin["macaddress"].should == "00:16:3E:2F:36:80"
  574. @plugin["ip6address"].should == "3ffe:1111:3333::1"
  575. end
  576. end
  577. describe "can choose from addresses with different scopes" do
  578. before do
  579. @plugin["network"]["default_gateway"] = nil
  580. @plugin["network"]["default_interface"] = nil
  581. @plugin["network"]["default_inet6_gateway"] = nil
  582. @plugin["network"]["default_inet6_interface"] = nil
  583. # just changing scopes to lInK for eth0 addresses
  584. @plugin["network"]["interfaces"]["eth0"]["addresses"].each{|k,v| v[:scope]="lInK" if %w[inet inet6].include? v["family"]}
  585. end
  586. it_populates_ipaddress_attributes
  587. it "prefers global scope addressses to set {ip,mac,ip6}address" do
  588. Ohai::Log.should_receive(:debug).with(/^\[inet\] no default interface/).once
  589. Ohai::Log.should_receive(:debug).with(/^\[inet6\] no default interface/).once
  590. Ohai::Log.stub(:debug)
  591. @plugin.run
  592. @plugin["ipaddress"].should == "192.168.99.11"
  593. @plugin["macaddress"].should == "00:16:3E:2F:36:80"
  594. @plugin["ip6address"].should == "3ffe:1111:3333::1"
  595. end
  596. end
  597. end
  598. describe "link level default route" do
  599. describe "simple setup" do
  600. before do
  601. @plugin["network"]["default_gateway"] = "0.0.0.0"
  602. @plugin["network"]["default_interface"] = "eth1"
  603. @plugin["network"]["default_inet6_gateway"] = "::"
  604. @plugin["network"]["default_inet6_interface"] = "eth1"
  605. end
  606. it_populates_ipaddress_attributes
  607. it "picks {ip,mac,ip6}address from the default interface" do
  608. @plugin.run
  609. @plugin["ipaddress"].should == "192.168.99.11"
  610. @plugin["macaddress"].should == "00:16:3E:2F:36:80"
  611. @plugin["ip6address"].should == "3ffe:1111:3333::1"
  612. end
  613. end
  614. describe "fe80::1 as a default gateway" do
  615. before do
  616. @plugin["network"]["default_inet6_gateway"] = "fe80::1"
  617. end
  618. it_populates_ipaddress_attributes
  619. it "picks {ip,mac,ip6}address from the default interface" do
  620. @plugin.run
  621. @plugin["ip6address"].should == "3ffe:1111:2222::33"
  622. end
  623. end
  624. describe "can choose from addresses with different scopes" do
  625. before do
  626. @plugin["network"]["default_gateway"] = "0.0.0.0"
  627. @plugin["network"]["default_interface"] = "eth1"
  628. @plugin["network"]["default_inet6_gateway"] = "::"
  629. @plugin["network"]["default_inet6_interface"] = "eth1"
  630. @plugin["network"]["interfaces"]["eth1"]["addresses"]["127.0.0.2"] = {
  631. "scope" => "host",
  632. "netmask" => "255.255.255.255",
  633. "prefixlen" => "32",
  634. "family" => "inet"
  635. }
  636. end
  637. it_populates_ipaddress_attributes
  638. it "picks {ip,mac,ip6}address from the default interface" do
  639. @plugin.run
  640. @plugin["ipaddress"].should == "192.168.99.11"
  641. @plugin["macaddress"].should == "00:16:3E:2F:36:80"
  642. @plugin["ip6address"].should == "3ffe:1111:3333::1"
  643. end
  644. end
  645. end
  646. describe "point to point address" do
  647. before do
  648. @plugin["network"]["interfaces"]["eth2"] = {
  649. "flags" => ["POINTOPOINT", "BROADCAST", "MULTICAST", "UP"],
  650. "number" => "2",
  651. "addresses" => {
  652. "fe80::216:3eff:fe2f:3681" => {
  653. "scope" => "Link",
  654. "prefixlen" => "64",
  655. "family" => "inet6"
  656. },
  657. "00:16:3E:2F:36:81" => {"family" => "lladdr"},
  658. "192.168.66.99" => {
  659. "scope" => "Global",
  660. "netmask" => "255.255.255.255",
  661. "peer" => "192.168.99.126",
  662. "prefixlen" => "32",
  663. "family" => "inet"
  664. },
  665. "3ffe:1111:2222:0:4444::1" => {
  666. "prefixlen" => "128",
  667. "peer" => "3ffe:1111:2222:0:4444::2",
  668. "family" => "inet6",
  669. "scope" => "Global"
  670. }
  671. }
  672. }
  673. @plugin["network"]["default_gateway"] = "192.168.99.126"
  674. @plugin["network"]["default_interface"] = "eth2"
  675. @plugin["network"]["default_inet6_gateway"] = "3ffe:1111:2222:0:4444::2"
  676. @plugin["network"]["default_inet6_interface"] = "eth2"
  677. end
  678. it_populates_ipaddress_attributes
  679. it "picks {ip,mac,ip6}address from the default interface" do
  680. @plugin.run
  681. @plugin["ipaddress"].should == "192.168.66.99"
  682. @plugin["macaddress"].should == "00:16:3E:2F:36:81"
  683. @plugin["ip6address"].should == "3ffe:1111:2222:0:4444::1"
  684. end
  685. end
  686. describe "ipv6 only node" do
  687. before do
  688. @plugin["network"]["default_gateway"] = nil
  689. @plugin["network"]["default_interface"] = nil
  690. @plugin["network"]["interfaces"].each do |i,iv|
  691. iv["addresses"].delete_if{|k,kv| kv["family"] == "inet" }
  692. end
  693. end
  694. it_doesnt_fail
  695. it "can't detect ipaddress" do
  696. Ohai::Log.stub(:warn)
  697. @plugin.run
  698. @plugin["ipaddress"].should be_nil
  699. end
  700. it "warns about not being able to set {ip,mac}address (ipv4)" do
  701. Ohai::Log.should_receive(:warn).with(/^unable to detect ipaddress/).once
  702. Ohai::Log.should_receive(:warn).with(/^unable to detect macaddress/).once
  703. @plugin.run
  704. end
  705. it "sets {ip6,mac}address" do
  706. Ohai::Log.stub(:warn)
  707. @plugin.run
  708. @plugin["ip6address"].should == "3ffe:1111:2222::33"
  709. @plugin["macaddress"].should == "00:16:3E:2F:36:79"
  710. end
  711. it "informs about macaddress being set using the ipv6 setup" do
  712. Ohai::Log.should_receive(:debug).with(/^macaddress set to 00:16:3E:2F:36:79 from the ipv6 setup/).once
  713. Ohai::Log.stub(:debug)
  714. @plugin.run
  715. end
  716. end
  717. end
  718. basic_data.keys.sort.each do |os|
  719. describe "the #{os}::network has already set some of the {ip,mac,ip6}address attributes" do
  720. before(:each) do
  721. @plugin["network"] = basic_data[os]["network"]
  722. end
  723. describe "{ip,mac}address are already set" do
  724. before do
  725. @plugin["ipaddress"] = "10.11.12.13"
  726. @plugin["macaddress"] = "00:AA:BB:CC:DD:EE"
  727. @expected_results = {
  728. "freebsd" => {
  729. "ip6address" => "::1"
  730. },
  731. "linux" => {
  732. "ip6address" => "3ffe:1111:2222::33"
  733. },
  734. "windows" => {
  735. "ip6address" => "fe80::698d:3e37:7950:b28c"
  736. }
  737. }
  738. end
  739. it_populates_ipaddress_attributes
  740. it "detects ip6address" do
  741. @plugin.run
  742. @plugin["ip6address"].should == @expected_results[os]["ip6address"]
  743. end
  744. it "doesn't overwrite {ip,mac}address" do
  745. @plugin.run
  746. @plugin["ipaddress"].should == "10.11.12.13"
  747. @plugin["macaddress"].should == "00:AA:BB:CC:DD:EE"
  748. end
  749. end
  750. describe "ip6address is already set" do
  751. describe "node has ipv4 and ipv6" do
  752. before do
  753. @plugin["ip6address"] = "3ffe:8888:9999::1"
  754. @expected_results = {
  755. "freebsd" => {
  756. "ipaddress" => "76.91.1.255",
  757. "macaddress" => "00:00:24:c9:5e:b8"
  758. },
  759. "linux" => {
  760. "ipaddress" => "192.168.66.33",
  761. "macaddress" => "00:16:3E:2F:36:79"
  762. },
  763. "windows" => {
  764. "ipaddress" => "172.19.0.130",
  765. "macaddress" => "52:54:44:66:66:02"
  766. }
  767. }
  768. end
  769. it_populates_ipaddress_attributes
  770. it "detects {ip,mac}address" do
  771. @plugin.run
  772. @plugin["ipaddress"].should == @expected_results[os]["ipaddress"]
  773. @plugin["macaddress"].should == @expected_results[os]["macaddress"]
  774. end
  775. it "doesn't overwrite ip6address" do
  776. @plugin.run
  777. @plugin["ip6address"].should == "3ffe:8888:9999::1"
  778. end
  779. end
  780. describe "ipv6 only node" do
  781. before do
  782. @plugin["network"]["default_gateway"] = nil
  783. @plugin["network"]["default_interface"] = nil
  784. @plugin["network"]["interfaces"].each do |i,iv|
  785. if iv.has_key? "addresses"
  786. iv["addresses"].delete_if{|k,kv| kv["family"] == "inet" }
  787. end
  788. end
  789. @plugin["ip6address"] = "3ffe:8888:9999::1"
  790. end
  791. it_doesnt_fail
  792. it "can't detect ipaddress (ipv4)" do
  793. Ohai::Log.stub(:warn)
  794. @plugin.run
  795. @plugin["ipaddress"].should be_nil
  796. end
  797. it "can't detect macaddress either" do
  798. Ohai::Log.stub(:warn)
  799. @plugin.run
  800. @plugin["macaddress"].should be_nil
  801. end
  802. it "warns about not being able to set {ip,mac}address" do
  803. Ohai::Log.should_receive(:warn).with(/^unable to detect ipaddress/).once
  804. Ohai::Log.should_receive(:warn).with(/^unable to detect macaddress/).once
  805. @plugin.run
  806. end
  807. it "doesn't overwrite ip6address" do
  808. Ohai::Log.stub(:warn)
  809. @plugin.run
  810. @plugin["ip6address"].should == "3ffe:8888:9999::1"
  811. end
  812. end
  813. end
  814. describe "{mac,ip6}address are already set" do
  815. describe "valid ipv4 setup" do
  816. before do
  817. @plugin["macaddress"] = "00:AA:BB:CC:DD:EE"
  818. @plugin["ip6address"] = "3ffe:8888:9999::1"
  819. @expected_results = {
  820. "freebsd" => {
  821. "ipaddress" => "76.91.1.255",
  822. "macaddress" => "00:00:24:c9:5e:b8"
  823. },
  824. "linux" => {
  825. "ipaddress" => "192.168.66.33",
  826. "macaddress" => "00:16:3E:2F:36:79"
  827. },
  828. "windows" => {
  829. "ipaddress" => "172.19.0.130",
  830. "macaddress" => "52:54:44:66:66:02"
  831. }
  832. }
  833. end
  834. it_populates_ipaddress_attributes
  835. it "detects ipaddress and overwrite macaddress" do
  836. @plugin.run
  837. @plugin["ipaddress"].should == @expected_results[os]["ipaddress"]
  838. @plugin["macaddress"].should == @expected_results[os]["macaddress"]
  839. end
  840. it "doesn't overwrite ip6address" do
  841. @plugin.run
  842. @plugin["ip6address"].should == "3ffe:8888:9999::1"
  843. end
  844. end
  845. describe "ipv6 only node" do
  846. before do
  847. @plugin["network"]["default_gateway"] = nil
  848. @plugin["network"]["default_interface"] = nil
  849. @plugin["network"]["interfaces"].each do |i,iv|
  850. if iv.has_key? "addresses"
  851. iv["addresses"].delete_if{|k,kv| kv["family"] == "inet" }
  852. end
  853. end
  854. @plugin["macaddress"] = "00:AA:BB:CC:DD:EE"
  855. @plugin["ip6address"] = "3ffe:8888:9999::1"
  856. end
  857. it_doesnt_fail
  858. it "can't set ipaddress" do
  859. Ohai::Log.stub(:warn)
  860. @plugin.run
  861. @plugin["ipaddress"].should be_nil
  862. end
  863. it "doesn't overwrite {ip6,mac}address" do
  864. Ohai::Log.stub(:warn)
  865. @plugin.run
  866. @plugin["ip6address"].should == "3ffe:8888:9999::1"
  867. @plugin["macaddress"].should == "00:AA:BB:CC:DD:EE"
  868. end
  869. end
  870. end
  871. describe "{ip,mac,ip6}address are already set" do
  872. before do
  873. @plugin["ipaddress"] = "10.11.12.13"
  874. @plugin["macaddress"] = "00:AA:BB:CC:DD:EE"
  875. @plugin["ip6address"] = "3ffe:8888:9999::1"
  876. end
  877. it_populates_ipaddress_attributes
  878. it "doesn't overwrite {ip,mac,ip6}address" do
  879. @plugin.run
  880. @plugin["ipaddress"].should == "10.11.12.13"
  881. @plugin["macaddress"].should == "00:AA:BB:CC:DD:EE"
  882. @plugin["ip6address"].should == "3ffe:8888:9999::1"
  883. end
  884. end
  885. describe "{ip,ip6}address are already set" do
  886. before do
  887. @plugin["ipaddress"] = "10.11.12.13"
  888. @plugin["ip6address"] = "3ffe:8888:9999::1"
  889. end
  890. it_doesnt_fail
  891. it "doesn't overwrite {ip,mac,ip6}address" do
  892. @plugin.run
  893. @plugin["ipaddress"].should == "10.11.12.13"
  894. @plugin["macaddress"].should == nil
  895. @plugin["ip6address"].should == "3ffe:8888:9999::1"
  896. end
  897. end
  898. end
  899. end
  900. end
  901. end