\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/mount/

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 "ostruct"

describe Chef::Provider::Mount::Aix do

  before(:all) do
    @mounted_output = <<~MOUNT
        node       mounted        mounted over    vfs       date        options
      -------- ---------------  ---------------  ------ ------------ ---------------
               /dev/sdz1         /tmp/foo         jfs2   Jul 17 13:22 rw,log=/dev/hd8
    MOUNT

    @unmounted_output = <<~UNMOUNTED
        node       mounted        mounted over    vfs       date        options
      -------- ---------------  ---------------  ------ ------------ ---------------
               /dev/sdz2         /                jfs2   Jul 17 13:22 rw,log=/dev/hd8
    UNMOUNTED

    @conflict_mounted_output = <<~MOUNT
        node       mounted        mounted over    vfs       date        options
      -------- ---------------  ---------------  ------ ------------ ---------------
               /dev/sdz3         /tmp/foo         jfs2   Jul 17 13:22 rw,log=/dev/hd8
    MOUNT

    @enabled_output = <<~ENABLED
      #MountPoint:Device:Vfs:Nodename:Type:Size:Options:AutoMount:Acct
      /tmp/foo:/dev/sdz1:jfs2::bootfs:10485760:rw:yes:no
    ENABLED

    @test_wrong_output = <<~WRONG
      #MountPoint:Device:Vfs:Nodename:Type:Size:Options:AutoMount:Acct
      /tmp/foo::/dev/sdz1:jfs2:bootfs:10485760:rw:yes:no
    WRONG
  end

  before(:each) do
    @node = Chef::Node.new
    @events = Chef::EventDispatch::Dispatcher.new
    @run_context = Chef::RunContext.new(@node, {}, @events)

    @new_resource = Chef::Resource::Mount.new("/tmp/foo")
    @new_resource.device      "/dev/sdz1"
    @new_resource.device_type :device
    @new_resource.fstype      "jfs2"

    @new_resource.supports remount: false

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

    allow(::File).to receive(:exists?).with("/dev/sdz1").and_return true
    allow(::File).to receive(:exists?).with("/tmp/foo").and_return true
  end

  def stub_mounted(provider, mounted_output)
    response = double("Mixlib::ShellOut command", exitstatus: 0, stdout: mounted_output, stderr: "")
    expect(provider).to receive(:shell_out_compacted!).with("mount").and_return(response)
  end

  def stub_enabled(provider, enabled_output)
    response = double("Mixlib::ShellOut command", exitstatus: 0, stdout: enabled_output, stderr: "")
    expect(provider).to receive(:shell_out_compacted).with("lsfs", "-c", @new_resource.mount_point).and_return(response)
  end

  def stub_mounted_enabled(provider, mounted_output, enabled_output)
    stub_mounted(provider, mounted_output)
    stub_enabled(provider, enabled_output)
  end

  describe "when discovering the current fs state" do
    it "should set current_resource.mounted to true if device is already mounted" do
      stub_mounted_enabled(@provider, @mounted_output, "")
      @provider.load_current_resource

      expect(@provider.current_resource.mounted).to be_truthy
    end

    it "should set current_resource.mounted to false if device is not mounted" do
      stub_mounted_enabled(@provider, @unmounted_output, "")

      @provider.load_current_resource

      expect(@provider.current_resource.mounted).to be_falsey
    end

    it "should set current_resource.mounted to false if the mount point is used for another device" do
      stub_mounted_enabled(@provider, @conflict_mounted_output, "")

      @provider.load_current_resource

      expect(@provider.current_resource.mounted).to be_falsey
    end

    context "mount_options_unchanged?" do
      it "should return true if mounted device is the same" do
        stub_mounted_enabled(@provider, @mounted_output, @enabled_output)
        @provider.load_current_resource

        allow(@provider.current_resource).to receive(:fstype).and_return("jfs2")
        expect(@provider.send :mount_options_unchanged?).to be true
      end

      it "should return false if mounted device has changed" do
        stub_mounted_enabled(@provider, @mounted_output, @enabled_output)
        @provider.load_current_resource

        allow(@provider.current_resource).to receive(:fstype).and_return("XXXX")
        expect(@provider.send :mount_options_unchanged?).to be false
      end
    end

  end

  # tests for #enabled?
  it "should load current_resource with properties if device is already mounted and enabled" do
    stub_mounted_enabled(@provider, @mounted_output, @enabled_output)

    @provider.load_current_resource

    expect(@provider.current_resource.enabled).to be_truthy
    expect(@provider.current_resource.mounted).to be_truthy
    expect(@provider.current_resource.mount_point).to eql(@new_resource.mount_point)
    expect(@provider.current_resource.fstype).to eql("jfs2")
    expect(@provider.current_resource.options).to eql(["rw"])
  end

  describe "mount_fs" do
    it "should mount resource if it is not mounted" do
      stub_mounted_enabled(@provider, @unmounted_output, "")

      expect(@provider).to receive(:shell_out_compacted!).with("mount", "-v", @new_resource.fstype, @new_resource.device, @new_resource.mount_point)

      @provider.run_action(:mount)
    end

    it "should not mount resource if it is already mounted" do
      stub_mounted_enabled(@provider, @mounted_output, "")

      expect(@provider).not_to receive(:mount_fs)

      @provider.run_action(:mount)
    end
  end

  describe "umount_fs" do
    it "should umount resource if it is already mounted" do
      stub_mounted_enabled(@provider, @mounted_output, "")

      expect(@provider).to receive(:shell_out_compacted!).with("umount", @new_resource.mount_point)

      @provider.run_action(:umount)
    end

    it "should not umount resource if it is not mounted" do
      stub_mounted_enabled(@provider, @unmounted_output, "")

      expect(@provider).not_to receive(:umount_fs)

      @provider.run_action(:umount)
    end
  end

  describe "remount_fs" do
    it "should remount resource if it is already mounted and it supports remounting" do
      @new_resource.supports({ remount: true })
      stub_mounted_enabled(@provider, @mounted_output, "")

      expect(@provider).to receive(:shell_out_compacted!).with("mount", "-o", "remount", @new_resource.device, @new_resource.mount_point)

      @provider.run_action(:remount)
    end

    it "should remount with new mount options if it is already mounted and it supports remounting" do
      @new_resource.supports({ remount: true })
      @new_resource.options("nodev,rw")
      stub_mounted_enabled(@provider, @mounted_output, "")

      expect(@provider).to receive(:shell_out_compacted!).with("mount", "-o", "remount,nodev,rw", @new_resource.device, @new_resource.mount_point)

      @provider.run_action(:remount)
    end
  end

  describe "enable_fs" do
    it "should enable mount if it is mounted and not enabled" do
      @new_resource.options("nodev,rw")
      stub_mounted_enabled(@provider, @mounted_output, "")
      # Add existing mount to test enable action appends additional mount with seperating blank line
      filesystems = StringIO.new
      filesystems.puts <<~ETCFILESYSTEMS
        /tmp/abc:
          dev   = /dev/sdz2
          vfs   = jfs2
          mount   = true
          options   = rw
      ETCFILESYSTEMS
      allow(::File).to receive(:open).with("/etc/filesystems", "a").and_yield(filesystems)

      @provider.run_action(:enable)

      expect(filesystems.string).to match(%r{^\n\n/tmp/foo:\n\tdev\t\t= /dev/sdz1\n\tvfs\t\t= jfs2\n\tmount\t\t= false\n\toptions\t\t= nodev,rw\n$})
    end

    it "should not enable mount if it is mounted and already enabled and mount options are unchanged" do
      stub_mounted_enabled(@provider, @mounted_output, @enabled_output)

      expect(@provider).not_to receive(:enable_fs)

      @provider.run_action(:enable)
    end

    it "should return false if enabled_output is given in wrong syntax" do
      stub_mounted_enabled(@provider, @mounted_output, @test_wrong_output)

      expect(@provider).to receive(:enable_fs)

      @provider.run_action(:enable)
    end

  end

  describe "disable_fs" do
    it "should disable mount if it is mounted and enabled" do
      stub_mounted_enabled(@provider, @mounted_output, @enabled_output)

      allow(::File).to receive(:open).with("/etc/filesystems", "r").and_return(<<~ETCFILESYSTEMS)
        /tmp/foo:
          dev   = /dev/sdz1
          vfs   = jfs2
          log   = /dev/hd8
          mount   = true
          check   = true
          vol   = /opt
          free    = false
          quota   = no

        /tmp/abc:
          dev   = /dev/sdz2
          vfs   = jfs2
          mount   = true
          options   = rw
      ETCFILESYSTEMS

      filesystems = StringIO.new
      allow(::File).to receive(:open).with("/etc/filesystems", "w").and_yield(filesystems)

      @provider.run_action(:disable)

      expect(filesystems.string).to match(%r{^/tmp/abc:\s+dev\s+= /dev/sdz2\s+vfs\s+= jfs2\s+mount\s+= true\s+options\s+= rw\n$})
    end

    it "should not disable mount if it is not mounted" do
      stub_mounted_enabled(@provider, @unmounted_output, "")

      expect(@provider).not_to receive(:disable_fs)

      @provider.run_action(:disable)
    end
  end
end