summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Jacob <adam@hjksolutions.com>2008-04-28 22:46:39 -0700
committerAdam Jacob <adam@hjksolutions.com>2008-04-28 22:46:39 -0700
commit2307eb8a7abbbf87676359c9bdffe14758d7c1ad (patch)
tree9cf86efa34705af6d9697a5a62d6a6671c1efda7
parent21cc606e60acc25d8741757eacc2c5459cdd1472 (diff)
downloadchef-2307eb8a7abbbf87676359c9bdffe14758d7c1ad.tar.gz
Adding a directory resource and provider
-rw-r--r--examples/config/cookbooks/tempfile/recipes/default.rb6
-rw-r--r--lib/chef/platform.rb3
-rw-r--r--lib/chef/provider.rb2
-rw-r--r--lib/chef/provider/directory.rb62
-rw-r--r--spec/unit/provider/directory_spec.rb100
5 files changed, 171 insertions, 2 deletions
diff --git a/examples/config/cookbooks/tempfile/recipes/default.rb b/examples/config/cookbooks/tempfile/recipes/default.rb
index 7b0c3e278a..140323e7dc 100644
--- a/examples/config/cookbooks/tempfile/recipes/default.rb
+++ b/examples/config/cookbooks/tempfile/recipes/default.rb
@@ -3,3 +3,9 @@ file "/tmp/glen" do
mode 0755
action "create"
end
+
+directory "/tmp/marginal" do
+ owner "adam"
+ mode 0755
+ action :create
+end
diff --git a/lib/chef/platform.rb b/lib/chef/platform.rb
index bebdcfa297..a37bb105c8 100644
--- a/lib/chef/platform.rb
+++ b/lib/chef/platform.rb
@@ -32,7 +32,8 @@ class Chef
:gentoo => {},
:solaris => {},
:default => {
- :file => Chef::Provider::File
+ :file => Chef::Provider::File,
+ :directory => Chef::Provider::Directory
}
}
diff --git a/lib/chef/provider.rb b/lib/chef/provider.rb
index 018f687069..32e2491028 100644
--- a/lib/chef/provider.rb
+++ b/lib/chef/provider.rb
@@ -30,7 +30,7 @@ class Chef
end
def action_nothing
- Chef::Log.debug("Doing nothing for #{self.to_s}")
+ Chef::Log.debug("Doing nothing for #{@new_resource.to_s}")
true
end
diff --git a/lib/chef/provider/directory.rb b/lib/chef/provider/directory.rb
new file mode 100644
index 0000000000..e30ca1894d
--- /dev/null
+++ b/lib/chef/provider/directory.rb
@@ -0,0 +1,62 @@
+#
+# Author:: Adam Jacob (<adam@hjksolutions.com>)
+# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
+# License:: GNU General Public License version 2 or later
+#
+# This program and entire repository is free software; you can
+# redistribute it and/or modify it under the terms of the GNU
+# General Public License as published by the Free Software
+# Foundation; either version 2 of the License, or any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+#
+
+require 'digest/md5'
+require 'etc'
+require File.join(File.dirname(__FILE__), "file")
+
+class Chef
+ class Provider
+ class Directory < Chef::Provider::File
+ def load_current_resource
+ @current_resource = Chef::Resource::Directory.new(@new_resource.name)
+ @current_resource.path(@new_resource.path)
+ if ::File.exist?(@current_resource.path) && ::File.directory?(@current_resource.path)
+ cstats = ::File.stat(@current_resource.path)
+ @current_resource.owner(cstats.uid)
+ @current_resource.group(cstats.gid)
+ @current_resource.mode("%o" % (cstats.mode & 007777))
+ end
+ @current_resource
+ end
+
+ def action_create
+ unless ::File.exists?(@new_resource.path)
+ Chef::Log.info("Creating #{@new_resource} at #{@new_resource.path}")
+ ::Dir.mkdir(@new_resource.path)
+ @new_resource.updated = true
+ end
+ set_owner if @new_resource.owner != nil
+ set_group if @new_resource.group != nil
+ set_mode if @new_resource.mode != nil
+ end
+
+ def action_delete
+ if ::File.exists?(@new_resource.path) && ::File.writable?(@new_resource.path)
+ Chef::Log.info("Deleting #{@new_resource} at #{@new_resource.path}")
+ ::Dir.delete(@new_resource.path)
+ @new_resource.updated = true
+ else
+ raise RuntimeError, "Cannot delete #{@new_resource} at #{@new_resource_path}!" if ::File.exists?(@new_resource.path)
+ end
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/spec/unit/provider/directory_spec.rb b/spec/unit/provider/directory_spec.rb
new file mode 100644
index 0000000000..2ba6c301d9
--- /dev/null
+++ b/spec/unit/provider/directory_spec.rb
@@ -0,0 +1,100 @@
+#
+# Author:: Adam Jacob (<adam@hjksolutions.com>)
+# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
+# License:: GNU General Public License version 2 or later
+#
+# This program and entire repository is free software; you can
+# redistribute it and/or modify it under the terms of the GNU
+# General Public License as published by the Free Software
+# Foundation; either version 2 of the License, or any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+#
+
+require 'ostruct'
+
+require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
+
+describe Chef::Provider::Directory do
+ before(:each) do
+ @new_resource = mock("New Resource", :null_object => true)
+ @new_resource.stub!(:name).and_return("directory")
+ @new_resource.stub!(:path).and_return("/tmp")
+ @new_resource.stub!(:owner).and_return(500)
+ @new_resource.stub!(:group).and_return(500)
+ @new_resource.stub!(:mode).and_return(0644)
+ @new_resource.stub!(:updated).and_return(false)
+ @node = Chef::Node.new
+ @node.name "latte"
+ @directory = Chef::Provider::Directory.new(@node, @new_resource)
+ end
+
+ it "should load the current resource based on the new resource" do
+ File.should_receive(:exist?).once.and_return(true)
+ File.should_receive(:directory?).once.and_return(true)
+ cstats = mock("stats", :null_object => true)
+ cstats.stub!(:uid).and_return(500)
+ cstats.stub!(:gid).and_return(500)
+ cstats.stub!(:mode).and_return(0755)
+ File.should_receive(:stat).once.and_return(cstats)
+ @directory.load_current_resource
+ @directory.current_resource.path.should eql(@new_resource.path)
+ @directory.current_resource.owner.should eql(500)
+ @directory.current_resource.group.should eql(500)
+ @directory.current_resource.mode.should eql("755")
+ end
+
+ it "should create a new directory on create, setting updated to true" do
+ load_mock_provider
+ File.should_receive(:exists?).once.and_return(false)
+ Dir.should_receive(:mkdir).with(@new_resource.path).once.and_return(true)
+ @directory.new_resource.should_receive(:updated=).with(true)
+ @directory.should_receive(:set_owner).once.and_return(true)
+ @directory.should_receive(:set_group).once.and_return(true)
+ @directory.should_receive(:set_mode).once.and_return(true)
+ @directory.action_create
+ end
+
+ it "should not create the directory if it already exists" do
+ load_mock_provider
+ File.should_receive(:exists?).once.and_return(true)
+ Dir.should_not_receive(:mkdir).with(@new_resource.path)
+ @directory.stub!(:set_owner).and_return(true)
+ @directory.stub!(:set_group).and_return(true)
+ @directory.stub!(:set_mode).and_return(true)
+ @directory.action_create
+ end
+
+ it "should delete the directory if it exists, and is writable with action_delete" do
+ load_mock_provider
+ File.should_receive(:exists?).once.and_return(true)
+ File.should_receive(:writable?).once.and_return(true)
+ Dir.should_receive(:delete).with(@new_resource.path).once.and_return(true)
+ @directory.action_delete
+ end
+
+ it "should raise an exception if it cannot delete the file due to bad permissions" do
+ load_mock_provider
+ File.stub!(:exists?).and_return(true)
+ File.stub!(:writable?).and_return(false)
+ lambda { @directory.action_delete }.should raise_error(RuntimeError)
+ end
+
+ def load_mock_provider
+ File.stub!(:exist?).and_return(true)
+ File.stub!(:directory?).and_return(true)
+ cstats = mock("stats", :null_object => true)
+ cstats.stub!(:uid).and_return(500)
+ cstats.stub!(:gid).and_return(500)
+ cstats.stub!(:mode).and_return(0755)
+ File.stub!(:stat).once.and_return(cstats)
+ @directory.load_current_resource
+ end
+end \ No newline at end of file