summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2013-11-11 13:47:33 -0800
committerLamont Granquist <lamont@scriptkiddie.org>2013-11-11 13:47:33 -0800
commit9ea2171212e770228caa8a45a28edbec3c2d6919 (patch)
tree61f16e558866c82d126e47736cfaf27dc17f2694
parent1eb4ae02a49ba7dfee8e65fcd26777c9098329a7 (diff)
downloadchef-9ea2171212e770228caa8a45a28edbec3c2d6919.tar.gz
spec tests for checksum
- plus stubbing of things that weren't stubed - and changing stub! to stub
-rw-r--r--spec/support/shared/unit/provider/file.rb189
-rw-r--r--spec/unit/provider/cookbook_file_spec.rb3
-rw-r--r--spec/unit/provider/file_spec.rb4
-rw-r--r--spec/unit/provider/remote_file_spec.rb7
-rw-r--r--spec/unit/provider/template_spec.rb8
5 files changed, 160 insertions, 51 deletions
diff --git a/spec/support/shared/unit/provider/file.rb b/spec/support/shared/unit/provider/file.rb
index 8753937fc3..dfe3638687 100644
--- a/spec/support/shared/unit/provider/file.rb
+++ b/spec/support/shared/unit/provider/file.rb
@@ -36,45 +36,61 @@ def normalized_path
end
def setup_normal_file
- File.stub!(:exists?).with(resource_path).and_return(true)
- File.stub!(:directory?).with(resource_path).and_return(false)
- File.stub!(:directory?).with(enclosing_directory).and_return(true)
- File.stub!(:writable?).with(resource_path).and_return(true)
- file_symlink_class.stub!(:symlink?).with(resource_path).and_return(false)
- file_symlink_class.stub!(:symlink?).with(normalized_path).and_return(false)
+ File.stub(:file?).with(resource_path).and_return(true)
+ File.stub(:realpath?).with(resource_path).and_return(resource_path)
+ File.stub(:exists?).with(resource_path).and_return(true)
+ File.stub(:exist?).with(resource_path).and_return(true)
+ File.stub(:directory?).with(resource_path).and_return(false)
+ File.stub(:directory?).with(enclosing_directory).and_return(true)
+ File.stub(:writable?).with(resource_path).and_return(true)
+ file_symlink_class.stub(:symlink?).with(resource_path).and_return(false)
+ file_symlink_class.stub(:symlink?).with(normalized_path).and_return(false)
end
def setup_missing_file
- File.stub!(:exists?).with(resource_path).and_return(false)
- File.stub!(:directory?).with(resource_path).and_return(false)
- File.stub!(:directory?).with(enclosing_directory).and_return(true)
- File.stub!(:writable?).with(resource_path).and_return(false)
- file_symlink_class.stub!(:symlink?).with(resource_path).and_return(false)
+ File.stub(:file?).with(resource_path).and_return(false)
+ File.stub(:realpath?).with(resource_path).and_return(resource_path)
+ File.stub(:exists?).with(resource_path).and_return(false)
+ File.stub(:exist?).with(resource_path).and_return(false)
+ File.stub(:directory?).with(resource_path).and_return(false)
+ File.stub(:directory?).with(enclosing_directory).and_return(true)
+ File.stub(:writable?).with(resource_path).and_return(false)
+ file_symlink_class.stub(:symlink?).with(resource_path).and_return(false)
end
def setup_symlink
- File.stub!(:exists?).with(resource_path).and_return(true)
- File.stub!(:directory?).with(normalized_path).and_return(false)
- File.stub!(:directory?).with(enclosing_directory).and_return(true)
- File.stub!(:writable?).with(resource_path).and_return(true)
- file_symlink_class.stub!(:symlink?).with(resource_path).and_return(true)
- file_symlink_class.stub!(:symlink?).with(normalized_path).and_return(true)
+ File.stub(:file?).with(normalized_path).and_return(true)
+ File.stub(:file?).with(resource_path).and_return(true)
+ File.stub(:realpath?).with(resource_path).and_return(normalized_path)
+ File.stub(:exists?).with(resource_path).and_return(true)
+ File.stub(:exist?).with(resource_path).and_return(true)
+ File.stub(:directory?).with(normalized_path).and_return(false)
+ File.stub(:directory?).with(enclosing_directory).and_return(true)
+ File.stub(:writable?).with(resource_path).and_return(true)
+ file_symlink_class.stub(:symlink?).with(resource_path).and_return(true)
+ file_symlink_class.stub(:symlink?).with(normalized_path).and_return(true)
end
def setup_unwritable_file
- File.stub!(:exists?).with(resource_path).and_return(true)
- File.stub!(:directory?).with(resource_path).and_return(false)
- File.stub!(:directory?).with(enclosing_directory).and_return(true)
- File.stub!(:writable?).with(resource_path).and_return(false)
- file_symlink_class.stub!(:symlink?).with(resource_path).and_return(false)
+ File.stub(:file?).with(resource_path).and_return(false)
+ File.stub(:realpath?).with(resource_path).and_raise(Errno::ENOENT)
+ File.stub(:exists?).with(resource_path).and_return(true)
+ File.stub(:exist?).with(resource_path).and_return(true)
+ File.stub(:directory?).with(resource_path).and_return(false)
+ File.stub(:directory?).with(enclosing_directory).and_return(true)
+ File.stub(:writable?).with(resource_path).and_return(false)
+ file_symlink_class.stub(:symlink?).with(resource_path).and_return(false)
end
def setup_missing_enclosing_directory
- File.stub!(:exists?).with(resource_path).and_return(false)
- File.stub!(:directory?).with(resource_path).and_return(false)
- File.stub!(:directory?).with(enclosing_directory).and_return(false)
- File.stub!(:writable?).with(resource_path).and_return(false)
- file_symlink_class.stub!(:symlink?).with(resource_path).and_return(false)
+ File.stub(:file?).with(resource_path).and_return(false)
+ File.stub(:realpath?).with(resource_path).and_raise(Errno::ENOENT)
+ File.stub(:exists?).with(resource_path).and_return(false)
+ File.stub(:exist?).with(resource_path).and_return(false)
+ File.stub(:directory?).with(resource_path).and_return(false)
+ File.stub(:directory?).with(enclosing_directory).and_return(false)
+ File.stub(:writable?).with(resource_path).and_return(false)
+ file_symlink_class.stub(:symlink?).with(resource_path).and_return(false)
end
shared_examples_for Chef::Provider::File do
@@ -116,61 +132,86 @@ shared_examples_for Chef::Provider::File do
context "when running load_current_resource and the file exists" do
before do
setup_normal_file
- provider.load_current_resource
end
+ let(:tempfile_sha256) { "42971f0ddce0cb20cf7660a123ffa1a1543beb2f1e7cd9d65858764a27f3201d" }
+
it "should load a current resource based on the one specified at construction" do
+ provider.load_current_resource
provider.current_resource.should be_a_kind_of(Chef::Resource::File)
end
it "the loaded current_resource name should be the same as the resource name" do
+ provider.load_current_resource
provider.current_resource.name.should eql(resource.name)
end
it "the loaded current_resource path should be the same as the resoure path" do
+ provider.load_current_resource
provider.current_resource.path.should eql(resource.path)
end
it "the loaded current_resource content should be nil" do
+ provider.load_current_resource
provider.current_resource.content.should eql(nil)
end
+ it "it should call checksum if we are managing content" do
+ provider.should_receive(:managing_content?).at_least(:once).and_return(true)
+ provider.should_receive(:checksum).with(resource.path).and_return(tempfile_sha256)
+ provider.load_current_resource
+ end
+
+ it "it should not call checksum if we are not managing content" do
+ provider.should_receive(:managing_content?).at_least(:once).and_return(false)
+ provider.should_not_receive(:checksum)
+ provider.load_current_resource
+ end
end
context "when running load_current_resource and the file does not exist" do
before do
setup_missing_file
- provider.load_current_resource
end
it "the current_resource should be a Chef::Resource::File" do
+ provider.load_current_resource
provider.current_resource.should be_a_kind_of(Chef::Resource::File)
end
it "the current_resource name should be the same as the resource name" do
+ provider.load_current_resource
provider.current_resource.name.should eql(resource.name)
end
it "the current_resource path should be the same as the resource path" do
+ provider.load_current_resource
provider.current_resource.path.should eql(resource.path)
end
it "the loaded current_resource content should be nil" do
+ provider.load_current_resource
provider.current_resource.content.should eql(nil)
end
+
+ it "it should not call checksum if we are not managing content" do
+ provider.should_not_receive(:managing_content?)
+ provider.should_not_receive(:checksum)
+ provider.load_current_resource
+ end
end
context "examining file security metadata on Unix with a file that exists" do
before do
# fake that we're on unix even if we're on windows
- Chef::Platform.stub!(:windows?).and_return(false)
+ Chef::Platform.stub(:windows?).and_return(false)
# mock up the filesystem to behave like unix
setup_normal_file
stat_struct = mock("::File.stat", :mode => 0600, :uid => 0, :gid => 0, :mtime => 10000)
resource_real_path = File.realpath(resource.path)
File.should_receive(:stat).with(resource_real_path).at_least(:once).and_return(stat_struct)
- Etc.stub!(:getgrgid).with(0).and_return(mock("Group Ent", :name => "wheel"))
- Etc.stub!(:getpwuid).with(0).and_return(mock("User Ent", :name => "root"))
+ Etc.stub(:getgrgid).with(0).and_return(mock("Group Ent", :name => "wheel"))
+ Etc.stub(:getpwuid).with(0).and_return(mock("User Ent", :name => "root"))
end
context "when the new_resource does not specify any state" do
@@ -239,7 +280,7 @@ shared_examples_for Chef::Provider::File do
context "examining file security metadata on Unix with a file that does not exist" do
before do
# fake that we're on unix even if we're on windows
- Chef::Platform.stub!(:windows?).and_return(false)
+ Chef::Platform.stub(:windows?).and_return(false)
setup_missing_file
end
@@ -288,14 +329,14 @@ shared_examples_for Chef::Provider::File do
before do
# fake that we're on unix even if we're on windows
- Chef::Platform.stub!(:windows?).and_return(false)
+ Chef::Platform.stub(:windows?).and_return(false)
# mock up the filesystem to behave like unix
setup_normal_file
stat_struct = mock("::File.stat", :mode => 0600, :uid => 0, :gid => 0, :mtime => 10000)
resource_real_path = File.realpath(resource.path)
- File.stub!(:stat).with(resource_real_path).and_return(stat_struct)
- Etc.stub!(:getgrgid).with(0).and_return(mock("Group Ent", :name => "wheel"))
- Etc.stub!(:getpwuid).with(0).and_return(mock("User Ent", :name => "root"))
+ File.stub(:stat).with(resource_real_path).and_return(stat_struct)
+ Etc.stub(:getgrgid).with(0).and_return(mock("Group Ent", :name => "wheel"))
+ Etc.stub(:getpwuid).with(0).and_return(mock("User Ent", :name => "root"))
provider.send(:load_resource_attributes_from_file, resource)
end
@@ -389,7 +430,7 @@ shared_examples_for Chef::Provider::File do
setup_normal_file
provider.load_current_resource
tempfile = double('Tempfile', :path => "/tmp/foo-bar-baz")
- content.stub!(:tempfile).and_return(tempfile)
+ content.stub(:tempfile).and_return(tempfile)
File.should_receive(:exists?).with("/tmp/foo-bar-baz").and_return(true)
tempfile.should_receive(:unlink).once
end
@@ -399,10 +440,10 @@ shared_examples_for Chef::Provider::File do
let(:tempfile_sha256) { "42971f0ddce0cb20cf7660a123ffa1a1543beb2f1e7cd9d65858764a27f3201d" }
let(:diff_for_reporting) { "+++\n---\n+foo\n-bar\n" }
before do
- provider.stub!(:contents_changed?).and_return(true)
+ provider.stub(:contents_changed?).and_return(true)
diff = double('Diff', :for_output => ['+++','---','+foo','-bar'],
:for_reporting => diff_for_reporting )
- diff.stub!(:diff).with(resource_path, tempfile_path).and_return(true)
+ diff.stub(:diff).with(resource_path, tempfile_path).and_return(true)
provider.should_receive(:diff).at_least(:once).and_return(diff)
provider.should_receive(:managing_content?).at_least(:once).and_return(true)
provider.should_receive(:checksum).with(tempfile_path).and_return(tempfile_sha256)
@@ -428,7 +469,7 @@ shared_examples_for Chef::Provider::File do
end
it "does nothing when the contents have not changed" do
- provider.stub!(:contents_changed?).and_return(false)
+ provider.stub(:contents_changed?).and_return(false)
provider.should_not_receive(:diff)
provider.send(:do_contents_changes)
end
@@ -465,7 +506,7 @@ shared_examples_for Chef::Provider::File do
before do
setup_normal_file
provider.load_current_resource
- provider.stub!(:resource_updated?).and_return(true)
+ provider.stub(:resource_updated?).and_return(true)
end
it "should check for selinux_enabled? by default" do
@@ -530,7 +571,7 @@ shared_examples_for Chef::Provider::File do
context "when resource is not updated" do
before do
- provider.stub!(:resource_updated?).and_return(false)
+ provider.stub(:resource_updated?).and_return(false)
end
it "should not check for selinux_enabled?" do
@@ -629,3 +670,65 @@ shared_examples_for Chef::Provider::File do
end
+shared_examples_for "a file provider with content field" do
+ context "when testing managing_content?" do
+ it "should be false when creating a file without content" do
+ provider.action = :create
+ resource.stub(:content).and_return(nil)
+ resource.stub(:checksum).and_return(nil)
+ expect(provider.send(:managing_content?)).to be_false
+ end
+ it "should be true when creating a file with content" do
+ provider.action = :create
+ resource.stub(:content).and_return("flurbleblobbleblooble")
+ resource.stub(:checksum).and_return(nil)
+ expect(provider.send(:managing_content?)).to be_true
+ end
+ it "should be true when checksum is set on the content (no matter how crazy)" do
+ provider.action = :create_if_missing
+ resource.stub(:checksum).and_return("1234123234234234")
+ resource.stub(:content).and_return(nil)
+ expect(provider.send(:managing_content?)).to be_true
+ end
+ it "should be false when action is create_if_missing" do
+ provider.action = :create_if_missing
+ resource.stub(:content).and_return("flurbleblobbleblooble")
+ resource.stub(:checksum).and_return(nil)
+ expect(provider.send(:managing_content?)).to be_false
+ end
+ end
+end
+
+shared_examples_for "a file provider with source field" do
+ context "when testing managing_content?" do
+ it "should be false when creating a file without content" do
+ provider.action = :create
+ resource.stub(:content).and_return(nil)
+ resource.stub(:source).and_return(nil)
+ resource.stub(:checksum).and_return(nil)
+ expect(provider.send(:managing_content?)).to be_false
+ end
+ it "should be true when creating a file with content" do
+ provider.action = :create
+ resource.stub(:content).and_return(nil)
+ resource.stub(:source).and_return("http://somewhere.com/something.php")
+ resource.stub(:checksum).and_return(nil)
+ expect(provider.send(:managing_content?)).to be_true
+ end
+ it "should be true when checksum is set on the content (no matter how crazy)" do
+ provider.action = :create_if_missing
+ resource.stub(:content).and_return(nil)
+ resource.stub(:source).and_return(nil)
+ resource.stub(:checksum).and_return("1234123234234234")
+ expect(provider.send(:managing_content?)).to be_true
+ end
+ it "should be false when action is create_if_missing" do
+ provider.action = :create_if_missing
+ resource.stub(:content).and_return(nil)
+ resource.stub(:source).and_return("http://somewhere.com/something.php")
+ resource.stub(:checksum).and_return(nil)
+ expect(provider.send(:managing_content?)).to be_false
+ end
+ end
+end
+
diff --git a/spec/unit/provider/cookbook_file_spec.rb b/spec/unit/provider/cookbook_file_spec.rb
index 44569075de..05509fbae4 100644
--- a/spec/unit/provider/cookbook_file_spec.rb
+++ b/spec/unit/provider/cookbook_file_spec.rb
@@ -37,7 +37,7 @@ describe Chef::Provider::CookbookFile do
let(:provider) do
provider = described_class.new(resource, run_context)
- provider.stub!(:content).and_return(content)
+ provider.stub(:content).and_return(content)
provider
end
@@ -54,4 +54,5 @@ describe Chef::Provider::CookbookFile do
it_behaves_like Chef::Provider::File
+ it_behaves_like "a file provider with source field"
end
diff --git a/spec/unit/provider/file_spec.rb b/spec/unit/provider/file_spec.rb
index 291f510067..6c401e3030 100644
--- a/spec/unit/provider/file_spec.rb
+++ b/spec/unit/provider/file_spec.rb
@@ -46,10 +46,12 @@ describe Chef::Provider::File do
let(:provider) do
provider = described_class.new(resource, run_context)
- provider.stub!(:content).and_return(content)
+ provider.stub(:content).and_return(content)
provider
end
it_behaves_like Chef::Provider::File
+
+ it_behaves_like "a file provider with content field"
end
diff --git a/spec/unit/provider/remote_file_spec.rb b/spec/unit/provider/remote_file_spec.rb
index 3b01b45983..4562083e7e 100644
--- a/spec/unit/provider/remote_file_spec.rb
+++ b/spec/unit/provider/remote_file_spec.rb
@@ -47,16 +47,17 @@ describe Chef::Provider::RemoteFile do
subject(:provider) do
provider = described_class.new(resource, run_context)
- provider.stub!(:content).and_return(content)
- provider.stub!(:update_new_resource_checksum).and_return(nil) # Otherwise it doesn't behave like a File provider
+ provider.stub(:content).and_return(content)
+ provider.stub(:update_new_resource_checksum).and_return(nil) # Otherwise it doesn't behave like a File provider
provider
end
before do
- Chef::FileCache.stub!(:load).with("remote_file/#{resource.name}").and_raise(Chef::Exceptions::FileNotFound)
+ Chef::FileCache.stub(:load).with("remote_file/#{resource.name}").and_raise(Chef::Exceptions::FileNotFound)
end
it_behaves_like Chef::Provider::File
+ it_behaves_like "a file provider with source field"
end
diff --git a/spec/unit/provider/template_spec.rb b/spec/unit/provider/template_spec.rb
index ca8f1db5e2..78d924aa41 100644
--- a/spec/unit/provider/template_spec.rb
+++ b/spec/unit/provider/template_spec.rb
@@ -39,7 +39,7 @@ describe Chef::Provider::Template do
let(:provider) do
provider = described_class.new(resource, run_context)
- provider.stub!(:content).and_return(content)
+ provider.stub(:content).and_return(content)
provider
end
@@ -73,16 +73,18 @@ describe Chef::Provider::Template do
let(:provider) do
provider = described_class.new(resource, run_context)
- provider.stub!(:content).and_return(content)
+ provider.stub(:content).and_return(content)
provider
end
it "stops executing when the local template source can't be found" do
setup_normal_file
- content.stub!(:template_location).and_return("/baz/bar/foo")
+ content.stub(:template_location).and_return("/baz/bar/foo")
File.stub(:exists?).with("/baz/bar/foo").and_return(false)
lambda { provider.run_action(:create) }.should raise_error Chef::Mixin::WhyRun::ResourceRequirements::Assertion::AssertionFailure
end
end
+
+ it_behaves_like "a file provider with source field"
end