summaryrefslogtreecommitdiffstats
path: root/spec/unit/util/ip_spec.rb
blob: 0374e75c774049a3b076902d538bc804373b22e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/env ruby

require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')

require 'facter/util/ip'

describe Facter::Util::IP do
    [:freebsd, :linux, :netbsd, :openbsd, :sunos, :darwin, :"hp-ux", :"gnu/kfreebsd"].each do |platform|
        it "should be supported on #{platform}" do
            Facter::Util::IP.supported_platforms.should be_include(platform)
        end
    end

    it "should return a list of interfaces" do
        Facter::Util::IP.should respond_to(:get_interfaces)
    end

    it "should return an empty list of interfaces on an unknown kernel" do
        Facter.stubs(:value).returns("UnknownKernel")
        Facter::Util::IP.get_interfaces().should == []
    end

    it "should return a list with a single interface and the loopback interface on Linux with a single interface" do
        sample_output_file = File.dirname(__FILE__) + '/../data/linux_ifconfig_all_with_single_interface'
        linux_ifconfig = File.new(sample_output_file).read()
        Facter::Util::IP.stubs(:get_all_interface_output).returns(linux_ifconfig)
        Facter::Util::IP.get_interfaces().should == ["eth0", "lo"]
    end

    it "should return a list two interfaces on Darwin with two interfaces" do
        sample_output_file = File.dirname(__FILE__) + '/../data/darwin_ifconfig_all_with_multiple_interfaces'
        darwin_ifconfig = File.new(sample_output_file).read()
        Facter::Util::IP.stubs(:get_all_interface_output).returns(darwin_ifconfig)
        Facter::Util::IP.get_interfaces().should == ["lo0", "en0"]
    end

    it "should return a list two interfaces on Solaris with two interfaces multiply reporting" do
        sample_output_file = File.dirname(__FILE__) + '/../data/solaris_ifconfig_all_with_multiple_interfaces'
        solaris_ifconfig = File.new(sample_output_file).read()
        Facter::Util::IP.stubs(:get_all_interface_output).returns(solaris_ifconfig)
        Facter::Util::IP.get_interfaces().should == ["lo0", "e1000g0"]
    end

    it "should return a list three interfaces on HP-UX with three interfaces multiply reporting" do
        sample_output_file = File.dirname(__FILE__) + '/../data/hpux_netstat_all_interfaces'
        hpux_netstat = File.new(sample_output_file).read()
        Facter::Util::IP.stubs(:get_all_interface_output).returns(hpux_netstat)
        Facter::Util::IP.get_interfaces().should == ["lan1", "lan0", "lo0"]
    end

    it "should return a list of six interfaces on a GNU/kFreeBSD with six interfaces" do
        sample_output_file = File.dirname(__FILE__) + '/../data/debian_kfreebsd_ifconfig'
        kfreebsd_ifconfig = File.new(sample_output_file).read()
        Facter::Util::IP.stubs(:get_all_interface_output).returns(kfreebsd_ifconfig)
        Facter::Util::IP.get_interfaces().should == ["em0", "em1", "bge0", "bge1", "lo0", "vlan0"]
    end

    it "should return a value for a specific interface" do
        Facter::Util::IP.should respond_to(:get_interface_value)
    end

    it "should not return interface information for unsupported platforms" do
        Facter.stubs(:value).with(:kernel).returns("bleah")
        Facter::Util::IP.get_interface_value("e1000g0", "netmask").should == []
    end

    it "should return ipaddress information for Solaris" do
        sample_output_file = File.dirname(__FILE__) + "/../data/solaris_ifconfig_single_interface"
        solaris_ifconfig_interface = File.new(sample_output_file).read()

        Facter::Util::IP.expects(:get_single_interface_output).with("e1000g0").returns(solaris_ifconfig_interface)
        Facter.stubs(:value).with(:kernel).returns("SunOS")

        Facter::Util::IP.get_interface_value("e1000g0", "ipaddress").should == "172.16.15.138"
    end

    it "should return netmask information for Solaris" do
        sample_output_file = File.dirname(__FILE__) + "/../data/solaris_ifconfig_single_interface"
        solaris_ifconfig_interface = File.new(sample_output_file).read()

        Facter::Util::IP.expects(:get_single_interface_output).with("e1000g0").returns(solaris_ifconfig_interface)
        Facter.stubs(:value).with(:kernel).returns("SunOS")

        Facter::Util::IP.get_interface_value("e1000g0", "netmask").should == "255.255.255.0"
    end

    it "should return calculated network information for Solaris" do
        sample_output_file = File.dirname(__FILE__) + "/../data/solaris_ifconfig_single_interface"
        solaris_ifconfig_interface = File.new(sample_output_file).read()

        Facter::Util::IP.stubs(:get_single_interface_output).with("e1000g0").returns(solaris_ifconfig_interface)
        Facter.stubs(:value).with(:kernel).returns("SunOS")

        Facter::Util::IP.get_network_value("e1000g0").should == "172.16.15.0"
    end

    it "should return ipaddress information for HP-UX" do
        sample_output_file = File.dirname(__FILE__) + "/../data/hpux_ifconfig_single_interface"
        hpux_ifconfig_interface = File.new(sample_output_file).read()

        Facter::Util::IP.expects(:get_single_interface_output).with("lan0").returns(hpux_ifconfig_interface)
        Facter.stubs(:value).with(:kernel).returns("HP-UX")

        Facter::Util::IP.get_interface_value("lan0", "ipaddress").should == "168.24.80.71"
    end

    it "should return macaddress information for HP-UX" do
        sample_output_file = File.dirname(__FILE__) + "/../data/hpux_ifconfig_single_interface"
        hpux_ifconfig_interface = File.new(sample_output_file).read()

        Facter::Util::IP.expects(:get_single_interface_output).with("lan0").returns(hpux_ifconfig_interface)
        Facter.stubs(:value).with(:kernel).returns("HP-UX")

        Facter::Util::IP.get_interface_value("lan0", "macaddress").should == "00:13:21:BD:9C:B7"
    end

    it "should return macaddress with leading zeros stripped off for GNU/kFreeBSD" do
        sample_output_file = File.dirname(__FILE__) + "/../data/debian_kfreebsd_ifconfig"
        kfreebsd_ifconfig = File.new(sample_output_file).read()

        Facter::Util::IP.expects(:get_single_interface_output).with("em0").returns(kfreebsd_ifconfig)
        Facter.stubs(:value).with(:kernel).returns("GNU/kFreeBSD")

        Facter::Util::IP.get_interface_value("em0", "macaddress").should == "0:11:a:59:67:90"
    end

    it "should return netmask information for HP-UX" do
        sample_output_file = File.dirname(__FILE__) + "/../data/hpux_ifconfig_single_interface"
        hpux_ifconfig_interface = File.new(sample_output_file).read()

        Facter::Util::IP.expects(:get_single_interface_output).with("lan0").returns(hpux_ifconfig_interface)
        Facter.stubs(:value).with(:kernel).returns("HP-UX")

        Facter::Util::IP.get_interface_value("lan0", "netmask").should == "255.255.255.0"
    end

    it "should return calculated network information for HP-UX" do
        sample_output_file = File.dirname(__FILE__) + "/../data/hpux_ifconfig_single_interface"
        hpux_ifconfig_interface = File.new(sample_output_file).read()

        Facter::Util::IP.stubs(:get_single_interface_output).with("lan0").returns(hpux_ifconfig_interface)
        Facter.stubs(:value).with(:kernel).returns("HP-UX")

        Facter::Util::IP.get_network_value("lan0").should == "168.24.80.0"
    end

    it "should return interface information for FreeBSD supported via an alias" do
        sample_output_file = File.dirname(__FILE__) + "/../data/6.0-STABLE_FreeBSD_ifconfig"
        ifconfig_interface = File.new(sample_output_file).read()

        Facter::Util::IP.expects(:get_single_interface_output).with("fxp0").returns(ifconfig_interface)
        Facter.stubs(:value).with(:kernel).returns("FreeBSD")

        Facter::Util::IP.get_interface_value("fxp0", "macaddress").should == "00:0e:0c:68:67:7c"
    end

    it "should return macaddress information for OS X" do
        sample_output_file = File.dirname(__FILE__) + "/../data/Mac_OS_X_10.5.5_ifconfig"
        ifconfig_interface = File.new(sample_output_file).read()

        Facter::Util::IP.expects(:get_single_interface_output).with("en1").returns(ifconfig_interface)
        Facter.stubs(:value).with(:kernel).returns("Darwin")

        Facter::Util::IP.get_interface_value("en1", "macaddress").should == "00:1b:63:ae:02:66"
    end

    it "should return all interfaces correctly on OS X" do
        sample_output_file = File.dirname(__FILE__) + "/../data/Mac_OS_X_10.5.5_ifconfig"
        ifconfig_interface = File.new(sample_output_file).read()

        Facter::Util::IP.expects(:get_all_interface_output).returns(ifconfig_interface)
        Facter.stubs(:value).with(:kernel).returns("Darwin")

        Facter::Util::IP.get_interfaces().should == ["lo0", "gif0", "stf0", "en0", "fw0", "en1", "vmnet8", "vmnet1"]
    end

    it "should return a human readable netmask on Solaris" do
        sample_output_file = File.dirname(__FILE__) + "/../data/solaris_ifconfig_single_interface"
        solaris_ifconfig_interface = File.new(sample_output_file).read()

        Facter::Util::IP.expects(:get_single_interface_output).with("e1000g0").returns(solaris_ifconfig_interface)
        Facter.stubs(:value).with(:kernel).returns("SunOS")

        Facter::Util::IP.get_interface_value("e1000g0", "netmask").should == "255.255.255.0"
    end

    it "should return a human readable netmask on HP-UX" do
        sample_output_file = File.dirname(__FILE__) + "/../data/hpux_ifconfig_single_interface"
        hpux_ifconfig_interface = File.new(sample_output_file).read()

        Facter::Util::IP.expects(:get_single_interface_output).with("lan0").returns(hpux_ifconfig_interface)
        Facter.stubs(:value).with(:kernel).returns("HP-UX")

        Facter::Util::IP.get_interface_value("lan0", "netmask").should == "255.255.255.0"
    end

    it "should return a human readable netmask on Darwin" do
        sample_output_file = File.dirname(__FILE__) + "/../data/darwin_ifconfig_single_interface"

        darwin_ifconfig_interface = File.new(sample_output_file).read()

        Facter::Util::IP.expects(:get_single_interface_output).with("en1").returns(darwin_ifconfig_interface)
        Facter.stubs(:value).with(:kernel).returns("Darwin")

        Facter::Util::IP.get_interface_value("en1", "netmask").should == "255.255.255.0"
    end

    it "should return a human readable netmask on GNU/kFreeBSD" do
        sample_output_file = File.dirname(__FILE__) + "/../data/debian_kfreebsd_ifconfig"

        kfreebsd_ifconfig = File.new(sample_output_file).read()

        Facter::Util::IP.expects(:get_single_interface_output).with("em1").returns(kfreebsd_ifconfig)
        Facter.stubs(:value).with(:kernel).returns("GNU/kFreeBSD")

        Facter::Util::IP.get_interface_value("em1", "netmask").should == "255.255.255.0"
    end

    it "should not get bonding master on interface aliases" do
        Facter.stubs(:value).with(:kernel).returns("Linux")

        Facter::Util::IP.get_bonding_master("eth0:1").should be_nil
    end

    [:freebsd, :netbsd, :openbsd, :sunos, :darwin, :"hp-ux"].each do |platform|
        it "should require conversion from hex on #{platform}" do
            Facter::Util::IP.convert_from_hex?(platform).should == true
        end
    end

    it "should return an arp address on Linux" do
        Facter.stubs(:value).with(:kernel).returns("Linux")

        Facter::Util::IP.expects(:get_arp_value).with("eth0").returns("00:00:0c:9f:f0:04")
        Facter::Util::IP.get_arp_value("eth0").should == "00:00:0c:9f:f0:04"
    end
end