5
5
require 'facter/util/ip'
7
7
describe Facter::Util::IP do
8
[:freebsd, :linux, :netbsd, :openbsd, :sunos, :darwin, :"hp-ux", :"gnu/kfreebsd"].each do |platform|
9
Facter::Util::Config.stubs(:is_windows?).returns(false)
12
[:freebsd, :linux, :netbsd, :openbsd, :sunos, :darwin, :"hp-ux", :"gnu/kfreebsd", :windows].each do |platform|
9
13
it "should be supported on #{platform}" do
14
Facter::Util::Config.stubs(:is_windows?).returns(platform == :windows)
10
15
Facter::Util::IP.supported_platforms.should be_include(platform)
23
28
it "should return a list with a single interface and the loopback interface on Linux with a single interface" do
24
29
sample_output_file = File.dirname(__FILE__) + '/../data/linux_ifconfig_all_with_single_interface'
25
linux_ifconfig = File.new(sample_output_file).read()
30
linux_ifconfig = File.read(sample_output_file)
26
31
Facter::Util::IP.stubs(:get_all_interface_output).returns(linux_ifconfig)
27
32
Facter::Util::IP.get_interfaces().should == ["eth0", "lo"]
30
35
it "should return a list two interfaces on Darwin with two interfaces" do
31
36
sample_output_file = File.dirname(__FILE__) + '/../data/darwin_ifconfig_all_with_multiple_interfaces'
32
darwin_ifconfig = File.new(sample_output_file).read()
37
darwin_ifconfig = File.read(sample_output_file)
33
38
Facter::Util::IP.stubs(:get_all_interface_output).returns(darwin_ifconfig)
34
39
Facter::Util::IP.get_interfaces().should == ["lo0", "en0"]
37
42
it "should return a list two interfaces on Solaris with two interfaces multiply reporting" do
38
43
sample_output_file = File.dirname(__FILE__) + '/../data/solaris_ifconfig_all_with_multiple_interfaces'
39
solaris_ifconfig = File.new(sample_output_file).read()
44
solaris_ifconfig = File.read(sample_output_file)
40
45
Facter::Util::IP.stubs(:get_all_interface_output).returns(solaris_ifconfig)
41
46
Facter::Util::IP.get_interfaces().should == ["lo0", "e1000g0"]
44
49
it "should return a list three interfaces on HP-UX with three interfaces multiply reporting" do
45
50
sample_output_file = File.dirname(__FILE__) + '/../data/hpux_netstat_all_interfaces'
46
hpux_netstat = File.new(sample_output_file).read()
51
hpux_netstat = File.read(sample_output_file)
47
52
Facter::Util::IP.stubs(:get_all_interface_output).returns(hpux_netstat)
48
53
Facter::Util::IP.get_interfaces().should == ["lan1", "lan0", "lo0"]
51
56
it "should return a list of six interfaces on a GNU/kFreeBSD with six interfaces" do
52
57
sample_output_file = File.dirname(__FILE__) + '/../data/debian_kfreebsd_ifconfig'
53
kfreebsd_ifconfig = File.new(sample_output_file).read()
58
kfreebsd_ifconfig = File.read(sample_output_file)
54
59
Facter::Util::IP.stubs(:get_all_interface_output).returns(kfreebsd_ifconfig)
55
60
Facter::Util::IP.get_interfaces().should == ["em0", "em1", "bge0", "bge1", "lo0", "vlan0"]
63
it "should return a list of only connected interfaces on Windows" do
64
Facter.fact(:kernel).stubs(:value).returns("windows")
65
sample_output_file = File.dirname(__FILE__) + '/../data/windows_netsh_all_interfaces'
66
windows_netsh = File.read(sample_output_file)
67
Facter::Util::IP.stubs(:get_all_interface_output).returns(windows_netsh)
68
Facter::Util::IP.get_interfaces().should == ["Loopback Pseudo-Interface 1", "Local Area Connection", "Teredo Tunneling Pseudo-Interface"]
58
71
it "should return a value for a specific interface" do
59
72
Facter::Util::IP.should respond_to(:get_interface_value)
67
80
it "should return ipaddress information for Solaris" do
68
81
sample_output_file = File.dirname(__FILE__) + "/../data/solaris_ifconfig_single_interface"
69
solaris_ifconfig_interface = File.new(sample_output_file).read()
82
solaris_ifconfig_interface = File.read(sample_output_file)
71
84
Facter::Util::IP.expects(:get_single_interface_output).with("e1000g0").returns(solaris_ifconfig_interface)
72
85
Facter.stubs(:value).with(:kernel).returns("SunOS")
77
90
it "should return netmask information for Solaris" do
78
91
sample_output_file = File.dirname(__FILE__) + "/../data/solaris_ifconfig_single_interface"
79
solaris_ifconfig_interface = File.new(sample_output_file).read()
92
solaris_ifconfig_interface = File.read(sample_output_file)
81
94
Facter::Util::IP.expects(:get_single_interface_output).with("e1000g0").returns(solaris_ifconfig_interface)
82
95
Facter.stubs(:value).with(:kernel).returns("SunOS")
87
100
it "should return calculated network information for Solaris" do
88
101
sample_output_file = File.dirname(__FILE__) + "/../data/solaris_ifconfig_single_interface"
89
solaris_ifconfig_interface = File.new(sample_output_file).read()
102
solaris_ifconfig_interface = File.read(sample_output_file)
91
104
Facter::Util::IP.stubs(:get_single_interface_output).with("e1000g0").returns(solaris_ifconfig_interface)
92
105
Facter.stubs(:value).with(:kernel).returns("SunOS")
97
110
it "should return ipaddress information for HP-UX" do
98
111
sample_output_file = File.dirname(__FILE__) + "/../data/hpux_ifconfig_single_interface"
99
hpux_ifconfig_interface = File.new(sample_output_file).read()
112
hpux_ifconfig_interface = File.read(sample_output_file)
101
114
Facter::Util::IP.expects(:get_single_interface_output).with("lan0").returns(hpux_ifconfig_interface)
102
115
Facter.stubs(:value).with(:kernel).returns("HP-UX")
107
120
it "should return macaddress information for HP-UX" do
108
121
sample_output_file = File.dirname(__FILE__) + "/../data/hpux_ifconfig_single_interface"
109
hpux_ifconfig_interface = File.new(sample_output_file).read()
122
hpux_ifconfig_interface = File.read(sample_output_file)
111
124
Facter::Util::IP.expects(:get_single_interface_output).with("lan0").returns(hpux_ifconfig_interface)
112
125
Facter.stubs(:value).with(:kernel).returns("HP-UX")
117
130
it "should return macaddress with leading zeros stripped off for GNU/kFreeBSD" do
118
131
sample_output_file = File.dirname(__FILE__) + "/../data/debian_kfreebsd_ifconfig"
119
kfreebsd_ifconfig = File.new(sample_output_file).read()
132
kfreebsd_ifconfig = File.read(sample_output_file)
121
134
Facter::Util::IP.expects(:get_single_interface_output).with("em0").returns(kfreebsd_ifconfig)
122
135
Facter.stubs(:value).with(:kernel).returns("GNU/kFreeBSD")
127
140
it "should return netmask information for HP-UX" do
128
141
sample_output_file = File.dirname(__FILE__) + "/../data/hpux_ifconfig_single_interface"
129
hpux_ifconfig_interface = File.new(sample_output_file).read()
142
hpux_ifconfig_interface = File.read(sample_output_file)
131
144
Facter::Util::IP.expects(:get_single_interface_output).with("lan0").returns(hpux_ifconfig_interface)
132
145
Facter.stubs(:value).with(:kernel).returns("HP-UX")
137
150
it "should return calculated network information for HP-UX" do
138
151
sample_output_file = File.dirname(__FILE__) + "/../data/hpux_ifconfig_single_interface"
139
hpux_ifconfig_interface = File.new(sample_output_file).read()
152
hpux_ifconfig_interface = File.read(sample_output_file)
141
154
Facter::Util::IP.stubs(:get_single_interface_output).with("lan0").returns(hpux_ifconfig_interface)
142
155
Facter.stubs(:value).with(:kernel).returns("HP-UX")
147
160
it "should return interface information for FreeBSD supported via an alias" do
148
161
sample_output_file = File.dirname(__FILE__) + "/../data/6.0-STABLE_FreeBSD_ifconfig"
149
ifconfig_interface = File.new(sample_output_file).read()
162
ifconfig_interface = File.read(sample_output_file)
151
164
Facter::Util::IP.expects(:get_single_interface_output).with("fxp0").returns(ifconfig_interface)
152
165
Facter.stubs(:value).with(:kernel).returns("FreeBSD")
157
170
it "should return macaddress information for OS X" do
158
171
sample_output_file = File.dirname(__FILE__) + "/../data/Mac_OS_X_10.5.5_ifconfig"
159
ifconfig_interface = File.new(sample_output_file).read()
172
ifconfig_interface = File.read(sample_output_file)
161
174
Facter::Util::IP.expects(:get_single_interface_output).with("en1").returns(ifconfig_interface)
162
175
Facter.stubs(:value).with(:kernel).returns("Darwin")
167
180
it "should return all interfaces correctly on OS X" do
168
181
sample_output_file = File.dirname(__FILE__) + "/../data/Mac_OS_X_10.5.5_ifconfig"
169
ifconfig_interface = File.new(sample_output_file).read()
182
ifconfig_interface = File.read(sample_output_file)
171
184
Facter::Util::IP.expects(:get_all_interface_output).returns(ifconfig_interface)
172
185
Facter.stubs(:value).with(:kernel).returns("Darwin")
177
190
it "should return a human readable netmask on Solaris" do
178
191
sample_output_file = File.dirname(__FILE__) + "/../data/solaris_ifconfig_single_interface"
179
solaris_ifconfig_interface = File.new(sample_output_file).read()
192
solaris_ifconfig_interface = File.read(sample_output_file)
181
194
Facter::Util::IP.expects(:get_single_interface_output).with("e1000g0").returns(solaris_ifconfig_interface)
182
195
Facter.stubs(:value).with(:kernel).returns("SunOS")
187
200
it "should return a human readable netmask on HP-UX" do
188
201
sample_output_file = File.dirname(__FILE__) + "/../data/hpux_ifconfig_single_interface"
189
hpux_ifconfig_interface = File.new(sample_output_file).read()
202
hpux_ifconfig_interface = File.read(sample_output_file)
191
204
Facter::Util::IP.expects(:get_single_interface_output).with("lan0").returns(hpux_ifconfig_interface)
192
205
Facter.stubs(:value).with(:kernel).returns("HP-UX")
197
210
it "should return a human readable netmask on Darwin" do
198
211
sample_output_file = File.dirname(__FILE__) + "/../data/darwin_ifconfig_single_interface"
200
darwin_ifconfig_interface = File.new(sample_output_file).read()
213
darwin_ifconfig_interface = File.read(sample_output_file)
202
215
Facter::Util::IP.expects(:get_single_interface_output).with("en1").returns(darwin_ifconfig_interface)
203
216
Facter.stubs(:value).with(:kernel).returns("Darwin")
208
221
it "should return a human readable netmask on GNU/kFreeBSD" do
209
222
sample_output_file = File.dirname(__FILE__) + "/../data/debian_kfreebsd_ifconfig"
211
kfreebsd_ifconfig = File.new(sample_output_file).read()
224
kfreebsd_ifconfig = File.read(sample_output_file)
213
226
Facter::Util::IP.expects(:get_single_interface_output).with("em1").returns(kfreebsd_ifconfig)
214
227
Facter.stubs(:value).with(:kernel).returns("GNU/kFreeBSD")
244
[:windows].each do |platform|
245
it "should not require conversion from hex on #{platform}" do
246
Facter::Util::IP.convert_from_hex?(platform).should be_false
231
250
it "should return an arp address on Linux" do
232
251
Facter.stubs(:value).with(:kernel).returns("Linux")
234
253
Facter::Util::IP.expects(:get_arp_value).with("eth0").returns("00:00:0c:9f:f0:04")
235
254
Facter::Util::IP.get_arp_value("eth0").should == "00:00:0c:9f:f0:04"
257
describe "on Windows" do
259
Facter.stubs(:value).with(:kernel).returns("windows")
262
it "should return ipaddress information" do
263
sample_output_file = File.dirname(__FILE__) + "/../data/windows_netsh_single_interface"
264
windows_netsh = File.read(sample_output_file)
266
Facter::Util::IP.expects(:get_output_for_interface_and_label).with("Local Area Connection", "ipaddress").returns(windows_netsh)
268
Facter::Util::IP.get_interface_value("Local Area Connection", "ipaddress").should == "172.16.138.216"
271
it "should return a human readable netmask" do
272
sample_output_file = File.dirname(__FILE__) + "/../data/windows_netsh_single_interface"
273
windows_netsh = File.read(sample_output_file)
275
Facter::Util::IP.expects(:get_output_for_interface_and_label).with("Local Area Connection", "netmask").returns(windows_netsh)
277
Facter::Util::IP.get_interface_value("Local Area Connection", "netmask").should == "255.255.255.0"
280
it "should return network information" do
281
sample_output_file = File.dirname(__FILE__) + "/../data/windows_netsh_single_interface"
282
windows_netsh = File.read(sample_output_file)
284
Facter::Util::IP.stubs(:get_output_for_interface_and_label).with("Local Area Connection", "ipaddress").returns(windows_netsh)
285
Facter::Util::IP.stubs(:get_output_for_interface_and_label).with("Local Area Connection", "netmask").returns(windows_netsh)
287
Facter::Util::IP.get_network_value("Local Area Connection").should == "172.16.138.0"
290
it "should return ipaddress6 information" do
291
sample_output_file = File.dirname(__FILE__) + "/../data/windows_netsh_single_interface6"
292
windows_netsh = File.read(sample_output_file)
294
Facter::Util::IP.expects(:get_output_for_interface_and_label).with("Teredo Tunneling Pseudo-Interface", "ipaddress6").returns(windows_netsh)
296
Facter::Util::IP.get_interface_value("Teredo Tunneling Pseudo-Interface", "ipaddress6").should == "2001:0:4137:9e76:2087:77a:53ef:7527"