blob: 0529cd33f7ce0a68dc063c26c8517351ef0b1d7f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
#!/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") }
content = Puppet::Type.type(:file).attrclass(:content)
describe content do
before do
# Wow that's a messy interface to the resource.
@resource = stub 'resource', :[] => nil, :[]= => nil, :property => nil, :newattr => nil, :parameter => nil
end
it "should be a subclass of Property" do
content.superclass.must == Puppet::Property
end
describe "when determining the checksum type" do
it "should use the type specified in the source checksum if a source is set" do
source = mock 'source'
source.expects(:checksum).returns "{litemd5}eh"
@resource.expects(:parameter).with(:source).returns source
@content = content.new(:resource => @resource)
@content.checksum_type.should == :litemd5
end
it "should use the type specified by the checksum parameter if no source is set" do
checksum = mock 'checksum'
checksum.expects(:checktype).returns :litemd5
@resource.expects(:parameter).with(:source).returns nil
@resource.expects(:parameter).with(:checksum).returns checksum
@content = content.new(:resource => @resource)
@content.checksum_type.should == :litemd5
end
it "should only return the checksum type from the checksum parameter if the parameter returns a whole checksum" do
checksum = mock 'checksum'
checksum.expects(:checktype).returns "{md5}something"
@resource.expects(:parameter).with(:source).returns nil
@resource.expects(:parameter).with(:checksum).returns checksum
@content = content.new(:resource => @resource)
@content.checksum_type.should == :md5
end
it "should use md5 if neither a source nor a checksum parameter is available" do
@content = content.new(:resource => @resource)
@content.checksum_type.should == :md5
end
end
describe "when determining the actual content to write" do
it "should use the set content if available" do
@content = content.new(:resource => @resource)
@content.should = "ehness"
@content.actual_content.should == "ehness"
end
it "should use the content from the source if the source is set" do
source = mock 'source'
source.expects(:content).returns "scont"
@resource.expects(:parameter).with(:source).returns source
@content = content.new(:resource => @resource)
@content.actual_content.should == "scont"
end
it "should return nil if no source is available and no content is set" do
@content = content.new(:resource => @resource)
@content.actual_content.should be_nil
end
end
describe "when setting the desired content" do
it "should make the actual content available via an attribute" do
@content = content.new(:resource => @resource)
@content.stubs(:checksum_type).returns "md5"
@content.should = "this is some content"
@content.actual_content.should == "this is some content"
end
it "should store the checksum as the desired content" do
@content = content.new(:resource => @resource)
digest = Digest::MD5.hexdigest("this is some content")
@content.stubs(:checksum_type).returns "md5"
@content.should = "this is some content"
@content.should.must == "{md5}#{digest}"
end
it "should not checksum 'absent'" do
@content = content.new(:resource => @resource)
@content.should = :absent
@content.should.must == :absent
end
end
describe "when retrieving the current content" do
it "should return :absent if the file does not exist" do
@content = content.new(:resource => @resource)
@resource.expects(:stat).returns nil
@content.retrieve.should == :absent
end
it "should not manage content on directories" do
@content = content.new(:resource => @resource)
stat = mock 'stat', :ftype => "directory"
@resource.expects(:stat).returns stat
@content.retrieve.should be_nil
end
it "should always return the checksum as a string" do
@content = content.new(:resource => @resource)
@content.stubs(:checksum_type).returns "mtime"
stat = mock 'stat', :ftype => "file"
@resource.expects(:stat).returns stat
@resource.expects(:[]).with(:path).returns "/my/file"
time = Time.now
@content.expects(:mtime_file).with("/my/file").returns time
@content.retrieve.should == "{mtime}%s" % time
end
it "should return the checksum of the file if it exists and is a normal file" do
@content = content.new(:resource => @resource)
@content.stubs(:checksum_type).returns "md5"
stat = mock 'stat', :ftype => "file"
@resource.expects(:stat).returns stat
@resource.expects(:[]).with(:path).returns "/my/file"
@content.expects(:md5_file).with("/my/file").returns "mysum"
@content.retrieve.should == "{md5}mysum"
end
end
describe "when testing whether the content is in sync" do
before do
@resource.stubs(:[]).with(:ensure).returns :file
@resource.stubs(:replace?).returns true
@resource.stubs(:should_be_file?).returns true
@content = content.new(:resource => @resource)
@content.stubs(:checksum_type).returns "md5"
end
it "should return true if the resource shouldn't be a regular file" do
@resource.expects(:should_be_file?).returns false
@content.must be_insync("whatever")
end
it "should return false if the current content is :absent" do
@content.should_not be_insync(:absent)
end
it "should return false if the file should be a file but is not present" do
@resource.expects(:should_be_file?).returns true
@content.should_not be_insync(:absent)
end
describe "and the file exists" do
before do
@resource.stubs(:stat).returns mock("stat")
end
it "should return false if the current contents are different from the desired content" do
@content.should = "some content"
@content.should_not be_insync("other content")
end
it "should return true if the sum for the current contents is the same as the sum for the desired content" do
@content.should = "some content"
@content.must be_insync("{md5}" + Digest::MD5.hexdigest("some content"))
end
describe "and the content is specified via a remote source" do
before do
@metadata = stub 'metadata'
@source = stub 'source', :metadata => @metadata
@resource.stubs(:parameter).with(:source).returns @source
end
it "should use checksums to compare remote content, rather than downloading the content" do
@source.stubs(:checksum).returns "{md5}whatever"
@content.insync?("{md5}eh")
end
it "should return false if the current content is different from the remote content" do
@source.stubs(:checksum).returns "{md5}whatever"
@content.should_not be_insync("some content")
end
it "should return true if the current content is the same as the remote content" do
@source.stubs(:checksum).returns("{md5}something")
@content.must be_insync("{md5}something")
end
end
end
describe "and :replace is false" do
before do
@resource.stubs(:replace?).returns false
end
it "should be insync if the file exists and the content is different" do
@resource.stubs(:stat).returns mock('stat')
@content.must be_insync("whatever")
end
it "should be insync if the file exists and the content is right" do
@resource.stubs(:stat).returns mock('stat')
@content.must be_insync("something")
end
it "should not be insync if the file does not exist" do
@content.should_not be_insync(:absent)
end
end
end
describe "when changing the content" do
before do
@content = content.new(:resource => @resource)
@content.should = "some content"
@resource.stubs(:[]).with(:path).returns "/boo"
@resource.stubs(:stat).returns "eh"
end
it "should use the file's :write method to write the content" do
@resource.expects(:write).with("some content", :content)
@content.sync
end
it "should return :file_changed if the file already existed" do
@resource.expects(:stat).returns "something"
@resource.stubs(:write)
@content.sync.should == :file_changed
end
it "should return :file_created if the file did not exist" do
@resource.expects(:stat).returns nil
@resource.stubs(:write)
@content.sync.should == :file_created
end
end
end
|