From 854a71e38a03f65ad25c29092a55be88f57ce4eb Mon Sep 17 00:00:00 2001 From: Joshua Timberman Date: Sun, 22 Feb 2009 20:25:17 -0700 Subject: writing specs for mount provider. --- chef/lib/chef/provider/mount/mount.rb | 115 +++++++++++---------- chef/spec/unit/provider/mount/mount_spec.rb | 151 ++++++++++++++++++++++++++++ chef/spec/unit/provider/mount_spec.rb | 111 ++++++++++++++++++++ 3 files changed, 323 insertions(+), 54 deletions(-) create mode 100644 chef/spec/unit/provider/mount/mount_spec.rb create mode 100644 chef/spec/unit/provider/mount_spec.rb diff --git a/chef/lib/chef/provider/mount/mount.rb b/chef/lib/chef/provider/mount/mount.rb index 25e13bdf6b..af0d567eaf 100644 --- a/chef/lib/chef/provider/mount/mount.rb +++ b/chef/lib/chef/provider/mount/mount.rb @@ -16,78 +16,85 @@ # limitations under the License. # +require 'chef/provider/mount' require 'chef/log' require 'chef/mixin/command' -require 'chef/provider' class Chef class Provider - class Mount < Chef::Provider - - include Chef::Mixin::Command + class Mount + class Mount < Chef::Provider::Mount + + include Chef::Mixin::Command - def initialize(node, new_resource) - super(node, new_resource) - @mounted = false - end + def initialize(node, new_resource) + super(node, new_resource) + @mounted = false + end - def load_current_resource - @current_resource = Chef::Resource::Mount.new(@new_resource.name) - Chef::Log.debug("Checking for mount point #{@current_resource.mount_point}") - popen4("mount") do |pid, stdin, stdout, stderr| - stdout.each do |line| - case line - when /^#{@new_resource.device}\s+on\s+#{@new_resource.mount_point}/ - @mounted = true - @current_resource.mount_point(@new_resource.mount_point) - Chef::Log.debug("Special device #{@new_resource.device} mounted as #{@new_resource.mount_point}") + def load_current_resource + @current_resource = Chef::Resource::Mount.new(@new_resource.name) + @current_resource.mount_point(@new_resource.mount_point) + Chef::Log.debug("Checking for mount point #{@current_resource.mount_point}") + popen4("mount") do |pid, stdin, stdout, stderr| + stdout.each do |line| + case line + when /^#{@new_resource.device}\s+on\s+#{@new_resource.mount_point}/ + @mounted = true + Chef::Log.debug("Special device #{@new_resource.device} mounted as #{@new_resource.mount_point}") + end end end + # revisit for enable/disable + # File.read("/etc/fstab").each do |line| + # case line + # when /^[#\s]/ + # next + # when /^[\/\w]+#{@new_resource.mount_point}/ + # @mount_exists = true + # Chef::Log.debug("Found mount point #{@new_resource.mount_point} in /etc/fstab") + # end + # end + @current_resource end - File.read("/etc/fstab").each do |line| - case line - when /^[#\s]/ - next - when /^[\/\w]+#{@new_resource.mount_point}/ - @mount_exists = true - Chef::Log.debug("Found mount point #{@new_resource.mount_point} in /etc/fstab") - end - end - @current_resource - end - def mount_fs - unless @mounted - if @new_resource.options - command = "mount -t #{@new_resource.fstype} -o #{@new_resource.options} " - else - command = "mount -t #{@new_resource.fstype} " + def mount_fs + unless @mounted + if @new_resource.options + command = "mount -t #{@new_resource.fstype} -o #{@new_resource.options} " + else + command = "mount -t #{@new_resource.fstype} " + end + command << "#{@new_resource.device} " + command << "#{@new_resource.mount_point} " + run_command(:command => command) + Chef::Log.info("Mounted #{@new_resource.mount_point}") end - command << "#{@new_resource.device} " - command << "#{@new_resource.mount_point} " - run_command(:command => command) - Chef::Log.debug("Mounted #{@new_resource.mount_point}") end - end - def umount_fs - if @mounted - command = "umount #{@new_resource.mount_point}" - run_command(:command => command) - Chef::Log.debug("Unmounted #{@new_resource.mount_point}") + def umount_fs + if @mounted + command = "umount #{@new_resource.mount_point}" + run_command(:command => command) + Chef::Log.info("Unmounted #{@new_resource.mount_point}") + end end - end - def remount_fs - if @mounted and @new_resource.supports[:remount] - command = "mount -o remount #{@new_resource.mount_point}" - run_command(:command => command) - Chef::Log.debug("Remounted #{@new_resource.mount_point}") + def remount_fs + if @mounted and @new_resource.supports[:remount] + command = "mount -o remount #{@new_resource.mount_point}" + run_command(:command => command) + Chef::Log.info("Remounted #{@new_resource.mount_point}") + # elsif @mounted + # umount_fs + # mount_fs + # else + # Chef::Log.info("#{@new_resource.mount_point} is not mounted.") + end end - end - - # def action_enable, action_disable, future feature/improvement. + # def action_enable, action_disable, future feature/improvement. + end end end end diff --git a/chef/spec/unit/provider/mount/mount_spec.rb b/chef/spec/unit/provider/mount/mount_spec.rb new file mode 100644 index 0000000000..d81720c80a --- /dev/null +++ b/chef/spec/unit/provider/mount/mount_spec.rb @@ -0,0 +1,151 @@ +# +# Author:: Joshua Timberman () +# Copyright:: Copyright (c) 2008 OpsCode, 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 File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper")) + +describe Chef::Provider::Mount::Mount, "load_current_resource" do + before(:each) do + @node = mock("Chef::Node", :null_object => true) + @new_resource = mock("Chef::Resource::Mount", + :null_object => true, + :name => "/tmp/foo", + :mount_point => "/tmp/foo", + :fstype => "ext3", + :mounted => false + ) + @new_resource.stub!(:supports).and_return({:remount => false}) + + @current_resource = mock("Chef::Resource::Mount", + :null_object => true, + :name => "/tmp/foo", + :mount_point => "/tmp/foo", + :fstype => "ext3", + :mounted => false + ) + + @provider = Chef::Provider::Mount::Mount.new(@node, @new_resource) + Chef::Resource::Mount.stub!(:new).and_return(@current_resource) + + # @status = mock("Status", :exitstatus => 0) + # @provider.stub!(:popen4).and_return(@status) + # @stdin = mock("STDIN", :null_object => true) + # @stdout = mock("STDOUT", :null_object => true) + # @stdout.stub!(:each).and_yield("#{@new_resource.device} on #{@new_resource.mount_point}") + # @stderr = mock("STDERR", :null_object => true) + # @pid = mock("PID", :null_object => true) + end + + it "should create a current resource with the name of the new resource" do + Chef::Resource::Mount.should_receive(:new).and_return(@current_resource) + @provider.load_current_resource + end + + it "should set the current resources mount point to the new resources mount point" do + @current_resource.should_receive(:mount_point).with(@new_resource.mount_point) + @provider.load_current_resource + end + + it "should set mounted true if the mount point is found in the mounts list" do + @stdout.stub!(:each_line).and_yield("#{@new_resource.device} on #{@new_resource.mount_point}") + @current_resource.should_receive(:mounted).with(true) + @provider.load_current_resource + end + +end + +describe Chef::Provider::Mount::Mount, "mount_fs" do + before(:each) do + @new_resource = mock("Chef::Resource::Mount", + :null_object => true, + :name => "/tmp/foo", + :mount_point => "/tmp/foo", + :fstype => "ext3", + :mounted => false + ) + @new_resource.stub!(:supports).and_return({:remount => false}) + + @provider = Chef::Provider::Mount::Mount.new(@node, @new_resource) + Chef::Resource::Mount.stub!(:new).and_return(@current_resource) + end + + # it "should mount the filesystem if it is not mounted" do + # @new_resource.stub(:mounted).and_return(false) + # @provider.should_receive(:run_command).with({:command => "mount -t #{@new_resource.fstype} #{@new_resource.mount_point}"}) + # end + + # it "should mount the filesystem with options if options were passed" do + # + # end + # + # it "should log to info for mounting if the filesystem is mounted" do + # + # end +end + +describe Chef::Provider::Mount::Mount, "umount_fs" do + before(:each) do + @new_resource = mock("Chef::Resource::Mount", + :null_object => true, + :name => "/tmp/foo", + :mount_point => "/tmp/foo", + :fstype => "ext3", + :mounted => false + ) + @new_resource.stub!(:remount_fs).and_return(false) + @new_resource.stub!(:supports).and_return({:remount => false}) + + @provider = Chef::Provider::Mount::Mount.new(@node, @new_resource) + Chef::Resource::Mount.stub!(:new).and_return(@current_resource) + end + + # it "should umount the filesystem if it is mounted" do + # + # end + # + # it "should log to info for unmounting if the filesystem is not mounted" do + # + # end +end + +describe Chef::Provider::Mount::Mount, "remount_fs" do + before(:each) do + @new_resource = mock("Chef::Resource::Mount", + :null_object => true, + :name => "/tmp/foo", + :mount_point => "/tmp/foo", + :mounted => false + ) + @new_resource.stub!(:remount_fs).and_return(false) + @new_resource.stub!(:supports).and_return({:remount => false}) + + @provider = Chef::Provider::Mount::Mount.new(@node, @new_resource) + Chef::Resource::Mount.stub!(:new).and_return(@current_resource) + end + + # it "should use mount -o remount if remount is supported" do + # @new_resource.stub!(:supports).and_return({:remount => true}) + # @provider.should_receive(:run_command).with({:command => "mount -o remount #{@new_resource.mount_point}"}) + # @provider.remount_fs + # end + # it "should umount and mount if remount is not supported" do + # end + + # it "should not try to remount at all if mounted is false" do + # @new_resource.stub!(:mounted => false) + # end +end diff --git a/chef/spec/unit/provider/mount_spec.rb b/chef/spec/unit/provider/mount_spec.rb new file mode 100644 index 0000000000..9a338468c2 --- /dev/null +++ b/chef/spec/unit/provider/mount_spec.rb @@ -0,0 +1,111 @@ +# +# Author:: Joshua Timberman () +# Copyright:: Copyright (c) 2008 OpsCode, 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 File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper")) + +describe Chef::Provider::Mount, "initialize" do + + before(:each) do + @node = mock("Chef::Node", :null_object => true) + @new_resource = mock("Chef::Resource", :null_object => true) + end + + it "should return a Chef::Provider::Mount object" do + provider = Chef::Provider::Mount.new(@node, @new_resource) + provider.should be_a_kind_of(Chef::Provider::Mount) + end + +end + +describe Chef::Provider::Mount, "action_mount" do + before(:each) do + @node = mock("Chef::Node", :null_object => true) + @new_resource = mock("Chef::Resource::Mount", + :null_object => true, + :name => "chef", + :mount_point => "chef" + ) + @current_resource = mock("Chef::Resource::Mount", + :null_object => true, + :name => "chef", + :mount_point => "chef" + ) + @provider = Chef::Provider::Mount.new(@node, @new_resource) + @provider.current_resource = @current_resource + @provider.stub!(:mount_fs).and_return(true) + end + + it "should execute the mount command" do + @current_resource.stub!(:mounted).and_return(false) + @provider.should_receive(:mount_fs).with.and_return(true) + @provider.action_mount + end +end + +describe Chef::Provider::Mount, "action_umount" do + before(:each) do + @node = mock("Chef::Node", :null_object => true) + @new_resource = mock("Chef::Resource::Mount", + :null_object => true, + :name => "chef", + :mount_point => "chef" + ) + @current_resource = mock("Chef::Resource::Mount", + :null_object => true, + :name => "chef", + :mount_point => "chef" + ) + @provider = Chef::Provider::Mount.new(@node, @new_resource) + @provider.current_resource = @current_resource + @provider.stub!(:umount_fs).and_return(true) + end + + it "should execute the umount command" do + @current_resource.stub!(:mounted).and_return(true) + @provider.should_receive(:umount_fs).with.and_return(true) + @provider.action_umount + end +end + +describe Chef::Provider::Mount, "action_remount" do + before(:each) do + @node = mock("Chef::Node", :null_object => true) + @new_resource = mock("Chef::Resource::Mount", + :null_object => true, + :name => "chef", + :mount_point => "chef" + ) + @current_resource = mock("Chef::Resource::Mount", + :null_object => true, + :name => "chef", + :mount_point => "chef", + :supports => { :remount => false } + ) + @provider = Chef::Provider::Mount.new(@node, @new_resource) + @provider.current_resource = @current_resource + @provider.stub!(:remount_fs).and_return(true) + @current_resource.stub!(:supports).and_return({:remount => true}) + end + + it "should execute command for remount if remount is supported" do + @current_resource.stub!(:mounted).and_return(true) + @provider.should_receive(:remount_fs).and_return(true) + @provider.remount_fs + end +end + -- cgit v1.2.1