summaryrefslogtreecommitdiffstats
path: root/spec/unit/util/ip.rb
blob: 60ec09e6d192c68de6ab8ddaa525135fb24d5ef3 (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
#!/usr/bin/env ruby

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

require 'facter/util/ip'

describe Facter::Util::IP do
    [:freebsd, :linux, :netbsd, :openbsd, :sunos, :darwin].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 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"]
    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 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 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 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

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