<commit_before>#
# Author:: <NAME> (<<EMAIL>>)
# Copyright:: Copyright 2008-2016, 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::Mount do
  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      "ext3"

    @new_resource.supports :remount => false

    @provider = Chef::Provider::Mount::Mount.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
    allow(::File).to receive(:realpath).with("/dev/sdz1").and_return "/dev/sdz1"
    allow(::File).to receive(:realpath).with("/tmp/foo").and_return "/tmp/foo"
  end

  describe "when discovering the current fs state" do
    before do
      allow(@provider).to receive(:shell_out!).and_return(OpenStruct.new(:stdout => ""))
      allow(::File).to receive(:foreach).with("/etc/fstab")
    end

    it "should create a current resource with the same mount point and device" do
      @provider.load_current_resource
      expect(@provider.current_resource.name).to eq("/tmp/foo")
      expect(@provider.current_resource.mount_point).to eq("/tmp/foo")
      expect(@provider.current_resource.device).to eq("/dev/sdz1")
    end

    it "should accecpt device_type :uuid", :not_supported_on_solaris do
      @status = double(:stdout => "/dev/sdz1\n", :exitstatus => 1)
      @new_resource.device_type :uuid
      @new_resource.device "d21afe51-a0fe-4dc6-9152-ac733763ae0a"
      @stdout_findfs = double("STDOUT", :first => "/dev/sdz1")
      expect(@provider).to receive(:shell_out).with("/sbin/findfs UUID=d21afe51-a0fe-4dc6-9152-ac733763ae0a").and_return(@status)
      @provider.load_current_resource()
      @provider.mountable?
    end

    describe "when dealing with network mounts" do
      { "nfs" => "nfsserver:/vol/path",
        "cifs" => "//cifsserver/share" }.each do |type, fs_spec|
        it "should detect network fs_spec (#{type})" do
          @new_resource.device fs_spec
          expect(@provider.network_device?).to be_truthy
        end

        it "should ignore trailing slash and set mounted to true for network mount (#{type})" do
          @new_resource.device fs_spec
          allow(@provider).to receive(:shell_out!).and_return(OpenStruct.new(:stdout => "#{fs_spec}/ on /tmp/foo type #{type} (rw)\n"))
          @provider.load_current_resource
          expect(@provider.current_resource.mounted).to be_truthy
        end
      end
    end

    it "should raise an error if the mount device does not exist" do
      allow(::File).to receive(:exists?).with("/dev/sdz1").and_return false
      expect { @provider.load_current_resource(); @provider.mountable? }.to raise_error(Chef::Exceptions::Mount)
    end

    it "should not call mountable? with load_current_resource - CHEF-1565" do
      allow(::File).to receive(:exists?).with("/dev/sdz1").and_return false
      expect(@provider).to receive(:mounted?).and_return(true)
      expect(@provider).to receive(:enabled?).and_return(true)
      expect(@provider).not_to receive(:mountable?)
      @provider.load_current_resource
    end

    it "should raise an error if the mount device (uuid) does not exist", :not_supported_on_solaris do
      status = double(:stdout => "", :exitstatus => 1)
      @new_resource.device_type :uuid
      @new_resource.device "d21afe51-a0fe-4dc6-9152-ac733763ae0a"
      expect(@provider).to receive(:shell_out).with("/sbin/findfs UUID=d21afe51-a0fe-4dc6-9152-ac733763ae0a").and_return(status)
      expect(::File).to receive(:exists?).with("").and_return(false)
      expect { @provider.load_current_resource(); @provider.mountable? }.to raise_error(Chef::Exceptions::Mount)
    end

    it "should raise an error if the mount point does not exist" do
      allow(::File).to receive(:exists?).with("/tmp/foo").and_return false
      expect { @provider.load_current_resource(); @provider.mountable? }.to raise_error(Chef::Exceptions::Mount)
    end

    %w{tmpfs fuse cgroup}.each do |fstype|
      it "does not expect the device to exist for #{fstype}" do<commit_msg>linux mount spec - skip device detection for zfs (and vboxsf)<commit_after>#
# Author:: <NAME> (<<EMAIL>>)
# Copyright:: Copyright 2008-2016, 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::Mount do
  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      "ext3"

    @new_resource.supports :remount => false

    @provider = Chef::Provider::Mount::Mount.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
    allow(::File).to receive(:realpath).with("/dev/sdz1").and_return "/dev/sdz1"
    allow(::File).to receive(:realpath).with("/tmp/foo").and_return "/tmp/foo"
  end

  describe "when discovering the current fs state" do
    before do
      allow(@provider).to receive(:shell_out!).and_return(OpenStruct.new(:stdout => ""))
      allow(::File).to receive(:foreach).with("/etc/fstab")
    end

    it "should create a current resource with the same mount point and device" do
      @provider.load_current_resource
      expect(@provider.current_resource.name).to eq("/tmp/foo")
      expect(@provider.current_resource.mount_point).to eq("/tmp/foo")
      expect(@provider.current_resource.device).to eq("/dev/sdz1")
    end

    it "should accecpt device_type :uuid", :not_supported_on_solaris do
      @status = double(:stdout => "/dev/sdz1\n", :exitstatus => 1)
      @new_resource.device_type :uuid
      @new_resource.device "d21afe51-a0fe-4dc6-9152-ac733763ae0a"
      @stdout_findfs = double("STDOUT", :first => "/dev/sdz1")
      expect(@provider).to receive(:shell_out).with("/sbin/findfs UUID=d21afe51-a0fe-4dc6-9152-ac733763ae0a").and_return(@status)
      @provider.load_current_resource()
      @provider.mountable?
    end

    describe "when dealing with network mounts" do
      { "nfs" => "nfsserver:/vol/path",
        "cifs" => "//cifsserver/share" }.each do |type, fs_spec|
        it "should detect network fs_spec (#{type})" do
          @new_resource.device fs_spec
          expect(@provider.network_device?).to be_truthy
        end

        it "should ignore trailing slash and set mounted to true for network mount (#{type})" do
          @new_resource.device fs_spec
          allow(@provider).to receive(:shell_out!).and_return(OpenStruct.new(:stdout => "#{fs_spec}/ on /tmp/foo type #{type} (rw)\n"))
          @provider.load_current_resource
          expect(@provider.current_resource.mounted).to be_truthy
        end
      end
    end

    it "should raise an error if the mount device does not exist" do
      allow(::File).to receive(:exists?).with("/dev/sdz1").and_return false
      expect { @provider.load_current_resource(); @provider.mountable? }.to raise_error(Chef::Exceptions::Mount)
    end

    it "should not call mountable? with load_current_resource - CHEF-1565" do
      allow(::File).to receive(:exists?).with("/dev/sdz1").and_return false
      expect(@provider).to receive(:mounted?).and_return(true)
      expect(@provider).to receive(:enabled?).and_return(true)
      expect(@provider).not_to receive(:mountable?)
      @provider.load_current_resource
    end

    it "should raise an error if the mount device (uuid) does not exist", :not_supported_on_solaris do
      status = double(:stdout => "", :exitstatus => 1)
      @new_resource.device_type :uuid
      @new_resource.device "d21afe51-a0fe-4dc6-9152-ac733763ae0a"
      expect(@provider).to receive(:shell_out).with("/sbin/findfs UUID=d21afe51-a0fe-4dc6-9152-ac733763ae0a").and_return(status)
      expect(::File).to receive(:exists?).with("").and_return(false)
      expect { @provider.load_current_resource(); @provider.mountable? }.to raise_error(Chef::Exceptions::Mount)
    end

    it "should raise an error if the mount point does not exist" do
      allow(::File).to receive(:exists?).with("/tmp/foo").and_return false
      expect { @provider.load_current_resource(); @provider.mountable? }.to raise_error(Chef::Exceptions::Mount)
    end

    %w{tmpfs fuse cgroup vboxsf zfs}.each do |fstype|
      it "does not expect the device to exist for #{fstype}" do
{'fraction_non_alphanumeric': 0.1375539279622755, 'fraction_numerical': 0.02046754289154209, 'mean_word_length': 3.8388349514563105, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1674090', 'n_tokens_mistral': 3247, 'n_tokens_neox': 3067, 'n_words': 752}