summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Wolfe <jes5199@gmail.com>2010-12-20 12:17:52 -0800
committerJesse Wolfe <jes5199@gmail.com>2010-12-20 16:43:36 -0800
commit76fe2b3b24f6c04cd1cff7c8492d479d15258566 (patch)
tree2c30345fd6d82b446eb863eab2d3e40c52056978
parenta17f2616b4cb0a9231745090ace5bb25e7bc77c9 (diff)
Implement #5168 and #5169 ctime and mtime are properties
File ctime and mtime are now implemented as read-only properties, so they can be examined with audit.
-rw-r--r--lib/puppet/type/file.rb2
-rw-r--r--lib/puppet/type/file/ctime.rb18
-rw-r--r--lib/puppet/type/file/mtime.rb17
-rwxr-xr-xlib/puppet/type/file/type.rb17
-rw-r--r--spec/unit/type/file/ctime.rb35
-rw-r--r--spec/unit/type/file/mtime.rb35
-rw-r--r--spec/unit/type/file/type.rb20
7 files changed, 132 insertions, 12 deletions
diff --git a/lib/puppet/type/file.rb b/lib/puppet/type/file.rb
index 6523c99a0..eee948cd5 100644
--- a/lib/puppet/type/file.rb
+++ b/lib/puppet/type/file.rb
@@ -797,3 +797,5 @@ require 'puppet/type/file/group'
require 'puppet/type/file/mode'
require 'puppet/type/file/type'
require 'puppet/type/file/selcontext' # SELinux file context
+require 'puppet/type/file/ctime'
+require 'puppet/type/file/mtime'
diff --git a/lib/puppet/type/file/ctime.rb b/lib/puppet/type/file/ctime.rb
new file mode 100644
index 000000000..24b098703
--- /dev/null
+++ b/lib/puppet/type/file/ctime.rb
@@ -0,0 +1,18 @@
+module Puppet
+ Puppet::Type.type(:file).newproperty(:ctime) do
+ desc "A read-only state to check the file ctime."
+
+ def retrieve
+ current_value = :absent
+ if stat = @resource.stat(false)
+ current_value = stat.ctime
+ end
+ current_value
+ end
+
+ validate do
+ fail "ctime is read-only"
+ end
+ end
+end
+
diff --git a/lib/puppet/type/file/mtime.rb b/lib/puppet/type/file/mtime.rb
new file mode 100644
index 000000000..8ca7ed0d6
--- /dev/null
+++ b/lib/puppet/type/file/mtime.rb
@@ -0,0 +1,17 @@
+module Puppet
+ Puppet::Type.type(:file).newproperty(:mtime) do
+ desc "A read-only state to check the file mtime."
+
+ def retrieve
+ current_value = :absent
+ if stat = @resource.stat(false)
+ current_value = stat.mtime
+ end
+ current_value
+ end
+
+ validate do
+ fail "mtime is read-only"
+ end
+ end
+end
diff --git a/lib/puppet/type/file/type.rb b/lib/puppet/type/file/type.rb
index eb50b81f9..4da54e2cb 100755
--- a/lib/puppet/type/file/type.rb
+++ b/lib/puppet/type/file/type.rb
@@ -3,23 +3,16 @@ module Puppet
require 'etc'
desc "A read-only state to check the file type."
- #munge do |value|
- # raise Puppet::Error, ":type is read-only"
- #end
-
def retrieve
- currentvalue = :absent
+ current_value = :absent
if stat = @resource.stat(false)
- currentvalue = stat.ftype
+ current_value = stat.ftype
end
- # so this state is never marked out of sync
- @should = [currentvalue]
- currentvalue
+ current_value
end
-
- def sync
- raise Puppet::Error, ":type is read-only"
+ validate do
+ fail "type is read-only"
end
end
end
diff --git a/spec/unit/type/file/ctime.rb b/spec/unit/type/file/ctime.rb
new file mode 100644
index 000000000..6145cbfdc
--- /dev/null
+++ b/spec/unit/type/file/ctime.rb
@@ -0,0 +1,35 @@
+#!/usr/bin/env ruby
+
+Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") }
+
+describe Puppet::Type.type(:file).attrclass(:ctime) do
+ require 'puppet_spec/files'
+ include PuppetSpec::Files
+
+ before do
+ @filename = tmpfile('ctime')
+ @resource = Puppet::Type.type(:file).new({:name => @filename})
+ end
+
+ it "should be able to audit the file's ctime" do
+ File.open(@filename, "w"){ }
+
+ @resource[:audit] = [:ctime]
+
+ # this .to_resource audit behavior is magical :-(
+ @resource.to_resource[:ctime].should == File.stat(@filename).ctime
+ end
+
+ it "should return absent if auditing an absent file" do
+ @resource[:audit] = [:ctime]
+
+ @resource.to_resource[:ctime].should == :absent
+ end
+
+ it "should prevent the user from trying to set the ctime" do
+ lambda {
+ @resource[:ctime] = Time.now.to_s
+ }.should raise_error(Puppet::Error, /ctime is read-only/)
+ end
+
+end
diff --git a/spec/unit/type/file/mtime.rb b/spec/unit/type/file/mtime.rb
new file mode 100644
index 000000000..043156ceb
--- /dev/null
+++ b/spec/unit/type/file/mtime.rb
@@ -0,0 +1,35 @@
+#!/usr/bin/env ruby
+
+Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") }
+
+describe Puppet::Type.type(:file).attrclass(:mtime) do
+ require 'puppet_spec/files'
+ include PuppetSpec::Files
+
+ before do
+ @filename = tmpfile('mtime')
+ @resource = Puppet::Type.type(:file).new({:name => @filename})
+ end
+
+ it "should be able to audit the file's mtime" do
+ File.open(@filename, "w"){ }
+
+ @resource[:audit] = [:mtime]
+
+ # this .to_resource audit behavior is magical :-(
+ @resource.to_resource[:mtime].should == File.stat(@filename).mtime
+ end
+
+ it "should return absent if auditing an absent file" do
+ @resource[:audit] = [:mtime]
+
+ @resource.to_resource[:mtime].should == :absent
+ end
+
+ it "should prevent the user from trying to set the mtime" do
+ lambda {
+ @resource[:mtime] = Time.now.to_s
+ }.should raise_error(Puppet::Error, /mtime is read-only/)
+ end
+
+end
diff --git a/spec/unit/type/file/type.rb b/spec/unit/type/file/type.rb
new file mode 100644
index 000000000..e46f0e0b0
--- /dev/null
+++ b/spec/unit/type/file/type.rb
@@ -0,0 +1,20 @@
+#!/usr/bin/env ruby
+
+Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") }
+
+describe Puppet::Type.type(:file).attrclass(:type) do
+ require 'puppet_spec/files'
+ include PuppetSpec::Files
+
+ before do
+ @filename = tmpfile('type')
+ @resource = Puppet::Type.type(:file).new({:name => @filename})
+ end
+
+ it "should prevent the user from trying to set the type" do
+ lambda {
+ @resource[:type] = "fifo"
+ }.should raise_error(Puppet::Error, /type is read-only/)
+ end
+
+end