From 426068030e077736c47dd9139e4dd5a3ce2fc680 Mon Sep 17 00:00:00 2001 From: danielsdeleo Date: Tue, 6 Nov 2012 15:04:39 -0800 Subject: assert starting state, remove atime checks Chef will nearly always change the atime when inspecting the current state of the file; previous tests were passing by accident. --- spec/functional/resource/file_spec.rb | 7 ++--- .../shared/functional/directory_resource.rb | 7 +++++ spec/support/shared/functional/file_resource.rb | 32 ++++++++++++++++++---- .../shared/functional/securable_resource.rb | 4 +++ 4 files changed, 40 insertions(+), 10 deletions(-) (limited to 'spec') diff --git a/spec/functional/resource/file_spec.rb b/spec/functional/resource/file_spec.rb index 903c4cf22f..8e481d9fc4 100644 --- a/spec/functional/resource/file_spec.rb +++ b/spec/functional/resource/file_spec.rb @@ -69,16 +69,15 @@ describe Chef::Resource::File do @expected_checksum = sha256_checksum(path) + now = Time.now.to_i + File.utime(now - 9000, now - 9000, path) @expected_mtime = File.stat(path).mtime - @expected_atime = File.stat(path).atime - sleep 1 resource.run_action(:touch) end - it "updates the mtime/atime of the file" do + it "updates the mtime of the file" do File.stat(path).mtime.should > @expected_mtime - File.stat(path).atime.should > @expected_atime end it "does not change the content" do diff --git a/spec/support/shared/functional/directory_resource.rb b/spec/support/shared/functional/directory_resource.rb index c0407d25c9..1eaa173c06 100644 --- a/spec/support/shared/functional/directory_resource.rb +++ b/spec/support/shared/functional/directory_resource.rb @@ -18,6 +18,11 @@ shared_examples_for "a directory resource" do context "when the target directory does not exist" do + before do + # assert pre-condition + File.should_not exist(path) + end + describe "when running action :create" do context "and the recursive option is not set" do before do @@ -35,6 +40,8 @@ shared_examples_for "a directory resource" do context "and the recursive option is set" do before do + File.should_not exist(path) + resource.recursive(true) @recursive_path = File.join(path, 'red-headed-stepchild') resource.path(@recursive_path) diff --git a/spec/support/shared/functional/file_resource.rb b/spec/support/shared/functional/file_resource.rb index abd486453c..fcadc2bd10 100644 --- a/spec/support/shared/functional/file_resource.rb +++ b/spec/support/shared/functional/file_resource.rb @@ -17,11 +17,18 @@ # shared_examples_for "a file with the wrong content" do + before do + # Assert starting state is as expected + File.should exist(path) + # Kinda weird, in this case @expected_checksum is the cksum of the file + # with incorrect content. + sha256_checksum(path).should == @expected_checksum + end + context "when running action :create" do context "with backups enabled" do before do Chef::Config[:file_backup_path] = CHEF_SPEC_BACKUP_PATH - sleep 1 resource.run_action(:create) end @@ -54,7 +61,6 @@ shared_examples_for "a file with the wrong content" do describe "when running action :create_if_missing" do before do - sleep 1 resource.run_action(:create_if_missing) end @@ -84,18 +90,22 @@ shared_examples_for "a file with the wrong content" do end shared_examples_for "a file with the correct content" do + before do + # Assert starting state is as expected + File.should exist(path) + sha256_checksum(path).should == @expected_checksum + end + describe "when running action :create" do before do - sleep 1 resource.run_action(:create) end it "does not overwrite the original when the :create action is run" do sha256_checksum(path).should == @expected_checksum end - it "does not update the mtime/atime of the file when the :create action is run" do + it "does not update the mtime of the file when the :create action is run" do File.stat(path).mtime.should == @expected_mtime - File.stat(path).atime.should be_within(2).of(@expected_atime) end it "is not marked as updated by last action" do @@ -137,6 +147,11 @@ shared_examples_for "a file resource" do let(:backup_glob) { File.join(CHEF_SPEC_BACKUP_PATH, Dir.tmpdir.sub(/^([A-Za-z]:)/, ""), "#{file_base}*") } context "when the target file does not exist" do + before do + # Assert starting state is expected + File.should_not exist(path) + end + describe "when running action :create" do before do resource.run_action(:create) @@ -197,6 +212,9 @@ shared_examples_for "a file resource" do context "when the target file has the wrong content" do before(:each) do File.open(path, "w") { |f| f.print "This is so wrong!!!" } + now = Time.now.to_i + File.utime(now - 9000, now - 9000, path) + @expected_mtime = File.stat(path).mtime @expected_checksum = sha256_checksum(path) end @@ -221,8 +239,10 @@ shared_examples_for "a file resource" do context "when the target file has the correct content" do before(:each) do File.open(path, "w") { |f| f.print expected_content } + now = Time.now.to_i + File.utime(now - 9000, now - 9000, path) + @expected_mtime = File.stat(path).mtime - @expected_atime = File.stat(path).atime @expected_checksum = sha256_checksum(path) end diff --git a/spec/support/shared/functional/securable_resource.rb b/spec/support/shared/functional/securable_resource.rb index 2eeb16c784..3c95cce2dc 100644 --- a/spec/support/shared/functional/securable_resource.rb +++ b/spec/support/shared/functional/securable_resource.rb @@ -74,12 +74,14 @@ shared_examples_for "a securable resource" do it "should set an owner", :requires_root do resource.owner expected_user_name resource.run_action(:create) + resource.should be_updated_by_last_action File.lstat(path).uid.should == expected_uid end it "should set a group", :requires_root do resource.group desired_gid resource.run_action(:create) + resource.should be_updated_by_last_action File.lstat(path).gid.should == expected_gid end @@ -87,6 +89,7 @@ shared_examples_for "a securable resource" do mode_string = '776' resource.mode mode_string resource.run_action(:create) + resource.should be_updated_by_last_action pending('Linux does not support lchmod', :if => resource.instance_of?(Chef::Resource::Link) && !os_x? && !freebsd?) do (File.lstat(path).mode & 007777).should == (mode_string.oct & 007777) end @@ -96,6 +99,7 @@ shared_examples_for "a securable resource" do mode_integer = 0776 resource.mode mode_integer resource.run_action(:create) + resource.should be_updated_by_last_action pending('Linux does not support lchmod', :if => resource.instance_of?(Chef::Resource::Link) && !os_x? && !freebsd?) do (File.lstat(path).mode & 007777).should == (mode_integer & 007777) end -- cgit v1.2.1