102
describe Puppet::FileServing::Metadata, " when finding the file to use for setting attributes" do
105
@metadata = Puppet::FileServing::Metadata.new(@path)
107
# Use a link because it's easier to test -- no checksumming
108
@stat = stub "stat", :uid => 10, :gid => 20, :mode => 0755, :ftype => "link"
110
# Not quite. We don't want to checksum links, but we must because they might be being followed.
111
@checksum = Digest::MD5.hexdigest("some content\n") # Remove these when :managed links are no longer checksumed.
112
@metadata.stubs(:md5_file).returns(@checksum) #
115
it "should accept a base path path to which the file should be relative" do
116
File.expects(:lstat).with(@path).returns @stat
117
File.expects(:readlink).with(@path).returns "/what/ever"
121
it "should use the set base path if one is not provided" do
122
File.expects(:lstat).with(@path).returns @stat
123
File.expects(:readlink).with(@path).returns "/what/ever"
127
it "should raise an exception if the file does not exist" do
128
File.expects(:lstat).with(@path).raises(Errno::ENOENT)
129
proc { @metadata.collect}.should raise_error(Errno::ENOENT)
133
describe Puppet::FileServing::Metadata, " when collecting attributes" do
136
# Use a real file mode, so we can validate the masking is done.
137
@stat = stub 'stat', :uid => 10, :gid => 20, :mode => 33261, :ftype => "file"
138
File.stubs(:lstat).returns(@stat)
139
@checksum = Digest::MD5.hexdigest("some content\n")
140
@metadata = Puppet::FileServing::Metadata.new("/my/file")
141
@metadata.stubs(:md5_file).returns(@checksum)
145
it "should be able to produce xmlrpc-style attribute information" do
146
@metadata.should respond_to(:attributes_with_tabs)
149
# LAK:FIXME This should actually change at some point
150
it "should set the owner by id" do
151
@metadata.owner.should be_instance_of(Fixnum)
154
# LAK:FIXME This should actually change at some point
155
it "should set the group by id" do
156
@metadata.group.should be_instance_of(Fixnum)
159
it "should set the owner to the file's current owner" do
160
@metadata.owner.should == 10
163
it "should set the group to the file's current group" do
164
@metadata.group.should == 20
167
it "should set the mode to the file's masked mode" do
168
@metadata.mode.should == 0755
171
it "should set the checksum to the file's current checksum" do
172
@metadata.checksum.should == "{md5}#{@checksum}"
175
describe "when managing files" do
176
it "should default to a checksum of type MD5" do
177
@metadata.checksum.should == "{md5}#{@checksum}"
180
it "should give a mtime checksum when checksum_type is set" do
182
@metadata.checksum_type = "mtime"
183
@metadata.expects(:mtime_file).returns(@time)
185
@metadata.checksum.should == "{mtime}#{@time}"
188
it "should produce tab-separated mode, type, owner, group, and checksum for xmlrpc" do
189
@metadata.attributes_with_tabs.should == "#{0755.to_s}\tfile\t10\t20\t{md5}#{@checksum}"
193
describe "when managing directories" do
195
@stat.stubs(:ftype).returns("directory")
197
@metadata.expects(:ctime_file).returns(@time)
200
it "should only use checksums of type 'ctime' for directories" do
202
@metadata.checksum.should == "{ctime}#{@time}"
205
it "should only use checksums of type 'ctime' for directories even if checksum_type set" do
206
@metadata.checksum_type = "mtime"
207
@metadata.expects(:mtime_file).never
209
@metadata.checksum.should == "{ctime}#{@time}"
212
it "should produce tab-separated mode, type, owner, group, and checksum for xmlrpc" do
214
@metadata.attributes_with_tabs.should == "#{0755.to_s}\tdirectory\t10\t20\t{ctime}#{@time.to_s}"
218
describe "when managing links" do
220
@stat.stubs(:ftype).returns("link")
221
File.expects(:readlink).with("/my/file").returns("/path/to/link")
224
@checksum = Digest::MD5.hexdigest("some content\n") # Remove these when :managed links are no longer checksumed.
225
@file.stubs(:md5_file).returns(@checksum) #
228
it "should read links instead of returning their checksums" do
229
@metadata.destination.should == "/path/to/link"
232
pending "should produce tab-separated mode, type, owner, group, and destination for xmlrpc" do
233
# "We'd like this to be true, but we need to always collect the checksum because in the server/client/server round trip we lose the distintion between manage and follow."
234
@metadata.attributes_with_tabs.should == "#{0755}\tlink\t10\t20\t/path/to/link"
237
it "should produce tab-separated mode, type, owner, group, checksum, and destination for xmlrpc" do
238
@metadata.attributes_with_tabs.should == "#{0755}\tlink\t10\t20\t{md5}eb9c2bf0eb63f3a7bc0ea37ef18aeba5\t/path/to/link"
243
describe Puppet::FileServing::Metadata, " when pointing to a link" do
102
describe Puppet::FileServing::Metadata do
103
include PuppetSpec::Files
105
shared_examples_for "metadata collector" do
107
data = described_class.new(path)
112
describe "when collecting attributes" do
113
describe "when managing files" do
114
let(:path) { tmpfile('file_serving_metadata') }
117
FileUtils.touch(path)
120
it "should be able to produce xmlrpc-style attribute information" do
121
metadata.should respond_to(:attributes_with_tabs)
124
it "should set the owner to the file's current owner" do
125
metadata.owner.should == owner
128
it "should set the group to the file's current group" do
129
metadata.group.should == group
132
it "should set the mode to the file's masked mode" do
133
set_mode(33261, path)
135
metadata.mode.should == 0755
138
describe "#checksum" do
139
let(:checksum) { Digest::MD5.hexdigest("some content\n") }
142
File.open(path, "w") {|f| f.print("some content\n")}
145
it "should default to a checksum of type MD5 with the file's current checksum" do
146
metadata.checksum.should == "{md5}#{checksum}"
149
it "should give a mtime checksum when checksum_type is set" do
151
metadata.checksum_type = "mtime"
152
metadata.expects(:mtime_file).returns(@time)
154
metadata.checksum.should == "{mtime}#{@time}"
157
it "should produce tab-separated mode, type, owner, group, and checksum for xmlrpc" do
160
metadata.attributes_with_tabs.should == "#{0755.to_s}\tfile\t#{owner}\t#{group}\t{md5}#{checksum}"
165
describe "when managing directories" do
166
let(:path) { tmpdir('file_serving_metadata_dir') }
167
let(:time) { Time.now }
170
metadata.expects(:ctime_file).returns(time)
173
it "should only use checksums of type 'ctime' for directories" do
175
metadata.checksum.should == "{ctime}#{time}"
178
it "should only use checksums of type 'ctime' for directories even if checksum_type set" do
179
metadata.checksum_type = "mtime"
180
metadata.expects(:mtime_file).never
182
metadata.checksum.should == "{ctime}#{time}"
185
it "should produce tab-separated mode, type, owner, group, and checksum for xmlrpc" do
189
metadata.attributes_with_tabs.should == "#{0755.to_s}\tdirectory\t#{owner}\t#{group}\t{ctime}#{time.to_s}"
193
describe "when managing links", :unless => Puppet.features.microsoft_windows? do
194
# 'path' is a link that points to 'target'
195
let(:path) { tmpfile('file_serving_metadata_link') }
196
let(:target) { tmpfile('file_serving_metadata_target') }
197
let(:checksum) { Digest::MD5.hexdigest("some content\n") }
198
let(:fmode) { File.lstat(path).mode & 0777 }
201
File.open(target, "w") {|f| f.print("some content\n")}
202
set_mode(0644, target)
204
FileUtils.symlink(target, path)
207
it "should read links instead of returning their checksums" do
208
metadata.destination.should == target
211
pending "should produce tab-separated mode, type, owner, group, and destination for xmlrpc" do
212
# "We'd like this to be true, but we need to always collect the checksum because in the server/client/server round trip we lose the distintion between manage and follow."
213
metadata.attributes_with_tabs.should == "#{0755}\tlink\t#{owner}\t#{group}\t#{target}"
216
it "should produce tab-separated mode, type, owner, group, checksum, and destination for xmlrpc" do
217
metadata.attributes_with_tabs.should == "#{fmode}\tlink\t#{owner}\t#{group}\t{md5}eb9c2bf0eb63f3a7bc0ea37ef18aeba5\t#{target}"
222
describe Puppet::FileServing::Metadata, " when finding the file to use for setting attributes" do
223
let(:path) { tmpfile('file_serving_metadata_find_file') }
226
File.open(path, "w") {|f| f.print("some content\n")}
230
it "should accept a base path to which the file should be relative" do
231
dir = tmpdir('metadata_dir')
232
metadata = described_class.new(dir)
233
metadata.relative_path = 'relative_path'
235
FileUtils.touch(metadata.full_path)
240
it "should use the set base path if one is not provided" do
244
it "should raise an exception if the file does not exist" do
247
proc { metadata.collect}.should raise_error(Errno::ENOENT)
252
describe "on POSIX systems", :if => Puppet.features.posix? do
257
File::Stat.any_instance.stubs(:uid).returns owner
258
File::Stat.any_instance.stubs(:gid).returns group
261
it_should_behave_like "metadata collector"
263
def set_mode(mode, path)
264
File.chmod(mode, path)
268
describe "on Windows systems", :if => Puppet.features.microsoft_windows? do
269
let(:owner) {'S-1-1-50'}
270
let(:group) {'S-1-1-51'}
273
require 'puppet/util/windows/security'
274
Puppet::Util::Windows::Security.stubs(:get_owner).returns owner
275
Puppet::Util::Windows::Security.stubs(:get_group).returns group
278
it_should_behave_like "metadata collector"
280
def set_mode(mode, path)
281
Puppet::Util::Windows::Security.set_mode(mode, path)
287
describe Puppet::FileServing::Metadata, " when pointing to a link", :unless => Puppet.features.microsoft_windows? do
244
288
describe "when links are managed" do
246
290
@file = Puppet::FileServing::Metadata.new("/base/path/my/file", :links => :manage)