\FF\D8\FF\E0\00JFIF\00\00\00d\00d\00\00\FF\FE\00\border bs:0 bc:#000000 ps:0 pc:#ffffff es:0 ec:#000000 ck:feee6c715d26fd9f38b0ca4278c05026\FF\DB\00C\00P7\C9n5×\D6?\BDê\9Ds\EBp\9F[`8m\B7)o\B5\E8\E6I\99\FE3]]A2\BA\8Cw\D6E\93\\DEv\C8\009\F2\F1NI?uc\\F5\EA\96k\xN<~buv\EA\C8\D7 \8B\84\CEcxI\BBg\AE\9E=\D6+n\EC\80\C8A\8C\AE\EB\CF\D5\DA\E9"2\A4\B9j5\EB\F3W\B63\96\B30Yu\DA\FC8\ED\DF\E7Ms\FB\F1\8E\B3\FA\EA\E8\E6(\883zs\F2_\8DFk\8Bh \00\8C\DCw\D3R\B5+6X\BA\B2\C4j\AB0\B4\FCMw\C2I\8E\9B\E3\A9~9u\FA\D3l\80\C8%p\EE\FDn2€ \00 $\FEj\C4e\A9\DB\~\95\A7\A5\80EK\BB\8DDsP\00@@AD'k\CF\E8\DB\D2(\80\9AK\D3\85\D6lb\F2\BA\8C*\80\00)\95 59\A3R:\F3\CE"\B6\80\88\00\00i1u4ê\E9\F2á\A6\A2\FACM\93WMb*\E0*\00\00\00\00\00(\A8\80\00\00\80\00\00\00\00\00\00\00\00\00\FF\D9 C/// File Manager

File Manager

Path: /opt/chef/embedded/lib/ruby/gems/2.7.0/gems/chef-16.3.45/spec/unit/provider/ifconfig/

Viewing File: aix_spec.rb

#
# Author:: Kaustubh Deorukhkar (<kaustubh@clogeny.com>)
# Copyright:: Copyright (c) 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 "chef/exceptions"

describe Chef::Provider::Ifconfig::Aix do

  before(:all) do
    @ifconfig_output = <<~IFCONFIG
      en1: flags=1e080863,480<UP,BROADCAST,NOTRAILERS,RUNNING,SIMPLEX,MULTICAST,GROUPRT,64BIT,CHECKSUM_OFFLOAD(ACTIVE),CHAIN>
              inet 10.153.11.59 netmask 0xffff0000 broadcast 10.153.255.255
               tcp_sendspace 262144 tcp_recvspace 262144 rfc1323 1
      en0: flags=1e080863,480<UP,BROADCAST,NOTRAILERS,RUNNING,SIMPLEX,MULTICAST,GROUPRT,64BIT,CHECKSUM_OFFLOAD(ACTIVE),CHAIN> metric 1
              inet 172.29.174.58 netmask 0xffffc000 broadcast 172.29.191.255
               tcp_sendspace 262144 tcp_recvspace 262144 rfc1323 1
      lo0: flags=e08084b,c0<UP,BROADCAST,LOOPBACK,RUNNING,SIMPLEX,MULTICAST,GROUPRT,64BIT,LARGESEND,CHAIN>
              inet 127.0.0.1 netmask 0xff000000 broadcast 127.255.255.255
              inet6 ::1%1/0
    IFCONFIG
  end

  before(:each) do
    @node = Chef::Node.new
    @cookbook_collection = Chef::CookbookCollection.new([])
    @events = Chef::EventDispatch::Dispatcher.new
    @run_context = Chef::RunContext.new(@node, @cookbook_collection, @events)

    @new_resource = Chef::Resource::Ifconfig.new("10.0.0.1", @run_context)

    @provider = Chef::Provider::Ifconfig::Aix.new(@new_resource, @run_context)
  end

  describe "#load_current_resource" do
    before do
      @status = double(stdout: @ifconfig_output, exitstatus: 0)
      allow(@provider).to receive(:shell_out_compacted).and_return(@status)
      @new_resource.device "en0"
    end

    it "should load given interface with properties." do
      current_resource = @provider.load_current_resource
      expect(current_resource.inet_addr).to eq("172.29.174.58")
      expect(current_resource.target).to eq(@new_resource.target)
      expect(current_resource.mask).to eq("255.255.192.0")
      expect(current_resource.bcast).to eq("172.29.191.255")
      expect(current_resource.metric).to eq("1")
    end
  end

  describe "#action_add" do

    it "should add an interface if it does not exist" do
      @new_resource.device "en10"
      allow(@provider).to receive(:load_current_resource) do
        @provider.instance_variable_set("@status", double("Status", exitstatus: 0))
        @provider.instance_variable_set("@current_resource", Chef::Resource::Ifconfig.new("10.0.0.1", @run_context))
      end
      command = "chdev -l #{@new_resource.device} -a netaddr=#{@new_resource.name}"
      expect(@provider).to receive(:shell_out_compacted!).with(*command.split(" "))

      @provider.run_action(:add)
      expect(@new_resource).to be_updated
    end

    it "should raise exception if metric property is set" do
      @new_resource.device "en0"
      @new_resource.metric "1"
      allow(@provider).to receive(:load_current_resource) do
        @provider.instance_variable_set("@status", double("Status", exitstatus: 0))
        @provider.instance_variable_set("@current_resource", Chef::Resource::Ifconfig.new("10.0.0.1", @run_context))
      end

      expect { @provider.run_action(:add) }.to raise_error(Chef::Exceptions::Ifconfig, "interface metric property cannot be set for :add action")
    end
  end

  describe "#action_enable" do
    it "should enable an interface if it does not exist" do
      @new_resource.device "en10"
      allow(@provider).to receive(:load_current_resource) do
        @provider.instance_variable_set("@status", double("Status", exitstatus: 0))
        @provider.instance_variable_set("@current_resource", Chef::Resource::Ifconfig.new("10.0.0.1", @run_context))
      end
      command = "ifconfig #{@new_resource.device} #{@new_resource.name}"
      expect(@provider).to receive(:shell_out_compacted!).with(*command.split(" "))

      @provider.run_action(:enable)
      expect(@new_resource).to be_updated
    end
  end

  describe "#action_disable" do

    it "should not disable an interface if it does not exist" do
      @new_resource.device "en10"
      allow(@provider).to receive(:load_current_resource) do
        @provider.instance_variable_set("@status", double("Status", exitstatus: 0))
        @provider.instance_variable_set("@current_resource", Chef::Resource::Ifconfig.new("10.0.0.1", @run_context))
      end

      expect(@provider).not_to receive(:shell_out_compacted!)

      @provider.run_action(:disable)
      expect(@new_resource).not_to be_updated
    end

    context "interface exists" do
      before do
        @new_resource.device "en10"
        allow(@provider).to receive(:load_current_resource) do
          @provider.instance_variable_set("@status", double("Status", exitstatus: 0))
          current_resource = Chef::Resource::Ifconfig.new("10.0.0.1", @run_context)
          current_resource.device @new_resource.device
          @provider.instance_variable_set("@current_resource", current_resource)
        end
      end

      it "should disable an interface if it exists" do
        command = "ifconfig #{@new_resource.device} down"
        expect(@provider).to receive(:shell_out_compacted!).with(*command.split(" "))

        @provider.run_action(:disable)
        expect(@new_resource).to be_updated
      end

    end
  end

  describe "#action_delete" do

    it "should not delete an interface if it does not exist" do
      @new_resource.device "en10"
      allow(@provider).to receive(:load_current_resource) do
        @provider.instance_variable_set("@status", double("Status", exitstatus: 0))
        @provider.instance_variable_set("@current_resource", Chef::Resource::Ifconfig.new("10.0.0.1", @run_context))
      end

      expect(@provider).not_to receive(:shell_out_compacted!)

      @provider.run_action(:delete)
      expect(@new_resource).not_to be_updated
    end

    context "interface exists" do
      before do
        @new_resource.device "en10"
        allow(@provider).to receive(:load_current_resource) do
          @provider.instance_variable_set("@status", double("Status", exitstatus: 0))
          current_resource = Chef::Resource::Ifconfig.new("10.0.0.1", @run_context)
          current_resource.device @new_resource.device
          @provider.instance_variable_set("@current_resource", current_resource)
        end
      end

      it "should delete an interface if it exists" do
        command = "chdev -l #{@new_resource.device} -a state=down"
        expect(@provider).to receive(:shell_out_compacted!).with(*command.split(" "))

        @provider.run_action(:delete)
        expect(@new_resource).to be_updated
      end
    end
  end
end