summaryrefslogtreecommitdiff
path: root/spec/unit/plugins/windows/network_spec.rb
blob: 38d2c17ab8544230f3c1018544e50e97fed3d9e3 (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
#
#  Author:: Nimesh Pathi <nimesh.patni@msystechnologies.com>
#  Copyright:: Copyright (c) 2018 Chef Software, Inc.
#  License:: Apache License, Version 2.0
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
#

require "spec_helper"
require "ipaddress" unless defined?(IPAddress)

describe Ohai::System, "Windows Network Plugin" do
  let(:plugin) { get_plugin("windows/network") }

  before do
    allow(plugin).to receive(:collect_os).and_return(:windows)
  end

  describe "#interface_code" do
    let(:interface_idx) { 1 }
    let(:index) { 2 }

    context "when interface index is given" do
      it "Returns a valid string having hexadecimal interface_index" do
        index = nil
        expect(plugin.interface_code(interface_idx, index)).to eq("0x1")
      end
    end

    context "when interface index is not given" do
      it "Returns a valid string having hexadecimal index" do
        interface_idx = nil
        expect(plugin.interface_code(interface_idx, index)).to eq("0x2")
      end
    end
  end

  describe "#prefer_ipv4" do
    let(:inet4) { "192.168.1.1" }
    let(:inet6) { "fe80::2fe:c8ff:fef5:c88f" }

    context "when Array is not passed" do
      it "Returns nil" do
        expect(plugin.prefer_ipv4("Invalid")).to be_nil
      end
    end

    context "when no address is passed in Array" do
      it "Returns nil" do
        expect(plugin.prefer_ipv4([])).to be_nil
      end
    end

    context "preferred chances of IPV4 address" do
      it "Returns the address when only IPV4 address is passed" do
        expect(plugin.prefer_ipv4([inet4])).to eq(inet4)
      end

      it "returns the address when IPV6 is also present at latter place" do
        expect(plugin.prefer_ipv4([inet4, inet6])).to eq(inet4)
      end

      it "returns the address when IPV6 is also present at former place" do
        expect(plugin.prefer_ipv4([inet6, inet4])).to eq(inet4)
      end
    end

    context "Preferred chances of IPV6 address" do
      it "returns the address when only IPV6 address is passed" do
        expect(plugin.prefer_ipv4([inet4])).to eq(inet4)
      end

      it "does not return the address if IPV4 is also present at former place" do
        expect(plugin.prefer_ipv4([inet4, inet6])).not_to eq(inet6)
      end

      it "does not return the address if IPV4 is also present at latter place" do
        expect(plugin.prefer_ipv4([inet6, inet4])).not_to eq(inet6)
      end
    end
  end

  describe "#favored_default_route_windows" do
    let(:interface1) do
      { "index" => 1,
        "interface_index" => 1,
        "ip_connection_metric" => 10,
        "default_ip_gateway" => ["fe80::2fe:c8ff:fef5:c88f", "192.168.1.1"] }
    end
    let(:iface_config) { { 1 => interface1 } }

    context "when a hash is not passed" do
      it "returns nil" do
        expect(plugin.favored_default_route_windows("Invalid")).to be_nil
      end
    end

    context "when no interface is passed in Hash" do
      it "returns nil" do
        expect(plugin.favored_default_route_windows({})).to be_nil
      end
    end

    context "when an interface configuration is passed" do
      context "without default_ip_gateway" do
        it "returns nil" do
          interface1["default_ip_gateway"] = nil
          expect(plugin.favored_default_route_windows(iface_config)).to be_nil
        end
      end

      context "with default_ip_gateway" do
        it "returns a hash with details" do
          expect(plugin.favored_default_route_windows(iface_config)).to be_a(Hash)
          expect(plugin.favored_default_route_windows(iface_config)).not_to be_empty
        end

        it "returns the default_gateway in IPV4 format" do
          expect(plugin.favored_default_route_windows(iface_config)).to include(default_ip_gateway: "192.168.1.1")
        end
      end
    end

    context "when multiple interfaces are passed" do
      let(:interface2) do
        { "index" => 2,
          "interface_index" => 3,
          "ip_connection_metric" => 20,
          "default_ip_gateway" => ["192.168.1.2"] }
      end
      let(:iface_config) do
        { 1 => interface1,
          2 => interface2 }
      end

      it "returns the default route as least metric interface" do
        expect(plugin.favored_default_route_windows(iface_config)).to include(interface_index: 1)
      end

      it "returns its default_gateway in IPV4 format" do
        expect(plugin.favored_default_route_windows(iface_config)).to include(default_ip_gateway: "192.168.1.1")
      end
    end
  end
end