~ubuntu-branches/ubuntu/trusty/puppet/trusty

« back to all changes in this revision

Viewing changes to spec/unit/file_serving/metadata_spec.rb

  • Committer: Package Import Robot
  • Author(s): Stig Sandbeck Mathisen
  • Date: 2011-10-22 14:08:22 UTC
  • mfrom: (1.1.25) (3.1.32 sid)
  • Revision ID: package-import@ubuntu.com-20111022140822-odxde5lohc45yhuz
Tags: 2.7.6-1
* New upstream release (CVE-2011-3872)
* Remove cherry-picked "groupadd_aix_warning" patch
* Install all new manpages

Show diffs side-by-side

added added

removed removed

Lines of Context:
99
99
  end
100
100
end
101
101
 
102
 
describe Puppet::FileServing::Metadata, " when finding the file to use for setting attributes" do
103
 
  before do
104
 
    @path = "/my/path"
105
 
    @metadata = Puppet::FileServing::Metadata.new(@path)
106
 
 
107
 
    # Use a link because it's easier to test -- no checksumming
108
 
    @stat = stub "stat", :uid => 10, :gid => 20, :mode => 0755, :ftype => "link"
109
 
 
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)           #
113
 
  end
114
 
 
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"
118
 
    @metadata.collect
119
 
  end
120
 
 
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"
124
 
    @metadata.collect
125
 
  end
126
 
 
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)
130
 
  end
131
 
end
132
 
 
133
 
describe Puppet::FileServing::Metadata, " when collecting attributes" do
134
 
  before do
135
 
    @path = "/my/file"
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)
142
 
    @metadata.collect
143
 
  end
144
 
 
145
 
  it "should be able to produce xmlrpc-style attribute information" do
146
 
    @metadata.should respond_to(:attributes_with_tabs)
147
 
  end
148
 
 
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)
152
 
  end
153
 
 
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)
157
 
  end
158
 
 
159
 
  it "should set the owner to the file's current owner" do
160
 
    @metadata.owner.should == 10
161
 
  end
162
 
 
163
 
  it "should set the group to the file's current group" do
164
 
    @metadata.group.should == 20
165
 
  end
166
 
 
167
 
  it "should set the mode to the file's masked mode" do
168
 
    @metadata.mode.should == 0755
169
 
  end
170
 
 
171
 
  it "should set the checksum to the file's current checksum" do
172
 
    @metadata.checksum.should == "{md5}#{@checksum}"
173
 
  end
174
 
 
175
 
  describe "when managing files" do
176
 
    it "should default to a checksum of type MD5" do
177
 
      @metadata.checksum.should == "{md5}#{@checksum}"
178
 
    end
179
 
 
180
 
    it "should give a mtime checksum when checksum_type is set" do
181
 
      time = Time.now
182
 
      @metadata.checksum_type = "mtime"
183
 
      @metadata.expects(:mtime_file).returns(@time)
184
 
      @metadata.collect
185
 
      @metadata.checksum.should == "{mtime}#{@time}"
186
 
    end
187
 
 
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}"
190
 
    end
191
 
  end
192
 
 
193
 
  describe "when managing directories" do
194
 
    before do
195
 
      @stat.stubs(:ftype).returns("directory")
196
 
      @time = Time.now
197
 
      @metadata.expects(:ctime_file).returns(@time)
198
 
    end
199
 
 
200
 
    it "should only use checksums of type 'ctime' for directories" do
201
 
      @metadata.collect
202
 
      @metadata.checksum.should == "{ctime}#{@time}"
203
 
    end
204
 
 
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
208
 
      @metadata.collect
209
 
      @metadata.checksum.should == "{ctime}#{@time}"
210
 
    end
211
 
 
212
 
    it "should produce tab-separated mode, type, owner, group, and checksum for xmlrpc" do
213
 
      @metadata.collect
214
 
      @metadata.attributes_with_tabs.should == "#{0755.to_s}\tdirectory\t10\t20\t{ctime}#{@time.to_s}"
215
 
    end
216
 
  end
217
 
 
218
 
  describe "when managing links" do
219
 
    before do
220
 
      @stat.stubs(:ftype).returns("link")
221
 
      File.expects(:readlink).with("/my/file").returns("/path/to/link")
222
 
      @metadata.collect
223
 
 
224
 
      @checksum = Digest::MD5.hexdigest("some content\n") # Remove these when :managed links are no longer checksumed.
225
 
      @file.stubs(:md5_file).returns(@checksum)           #
226
 
    end
227
 
 
228
 
    it "should read links instead of returning their checksums" do
229
 
      @metadata.destination.should == "/path/to/link"
230
 
    end
231
 
 
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"
235
 
    end
236
 
 
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"
239
 
    end
240
 
  end
241
 
end
242
 
 
243
 
describe Puppet::FileServing::Metadata, " when pointing to a link" do
 
102
describe Puppet::FileServing::Metadata do
 
103
  include PuppetSpec::Files
 
104
 
 
105
  shared_examples_for "metadata collector" do
 
106
    let(:metadata) do
 
107
      data = described_class.new(path)
 
108
      data.collect
 
109
      data
 
110
    end
 
111
 
 
112
    describe "when collecting attributes" do
 
113
      describe "when managing files" do
 
114
        let(:path) { tmpfile('file_serving_metadata') }
 
115
 
 
116
        before :each do
 
117
          FileUtils.touch(path)
 
118
        end
 
119
 
 
120
        it "should be able to produce xmlrpc-style attribute information" do
 
121
          metadata.should respond_to(:attributes_with_tabs)
 
122
        end
 
123
 
 
124
        it "should set the owner to the file's current owner" do
 
125
          metadata.owner.should == owner
 
126
        end
 
127
 
 
128
        it "should set the group to the file's current group" do
 
129
          metadata.group.should == group
 
130
        end
 
131
 
 
132
        it "should set the mode to the file's masked mode" do
 
133
          set_mode(33261, path)
 
134
 
 
135
          metadata.mode.should == 0755
 
136
        end
 
137
 
 
138
        describe "#checksum" do
 
139
          let(:checksum) { Digest::MD5.hexdigest("some content\n") }
 
140
 
 
141
          before :each do
 
142
            File.open(path, "w") {|f| f.print("some content\n")}
 
143
          end
 
144
 
 
145
          it "should default to a checksum of type MD5 with the file's current checksum" do
 
146
            metadata.checksum.should == "{md5}#{checksum}"
 
147
          end
 
148
 
 
149
          it "should give a mtime checksum when checksum_type is set" do
 
150
            time = Time.now
 
151
            metadata.checksum_type = "mtime"
 
152
            metadata.expects(:mtime_file).returns(@time)
 
153
            metadata.collect
 
154
            metadata.checksum.should == "{mtime}#{@time}"
 
155
          end
 
156
 
 
157
          it "should produce tab-separated mode, type, owner, group, and checksum for xmlrpc" do
 
158
            set_mode(0755, path)
 
159
 
 
160
            metadata.attributes_with_tabs.should == "#{0755.to_s}\tfile\t#{owner}\t#{group}\t{md5}#{checksum}"
 
161
          end
 
162
        end
 
163
      end
 
164
 
 
165
      describe "when managing directories" do
 
166
        let(:path) { tmpdir('file_serving_metadata_dir') }
 
167
        let(:time) { Time.now }
 
168
 
 
169
        before :each do
 
170
          metadata.expects(:ctime_file).returns(time)
 
171
        end
 
172
 
 
173
        it "should only use checksums of type 'ctime' for directories" do
 
174
          metadata.collect
 
175
          metadata.checksum.should == "{ctime}#{time}"
 
176
        end
 
177
 
 
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
 
181
          metadata.collect
 
182
          metadata.checksum.should == "{ctime}#{time}"
 
183
        end
 
184
 
 
185
        it "should produce tab-separated mode, type, owner, group, and checksum for xmlrpc" do
 
186
          set_mode(0755, path)
 
187
          metadata.collect
 
188
 
 
189
          metadata.attributes_with_tabs.should == "#{0755.to_s}\tdirectory\t#{owner}\t#{group}\t{ctime}#{time.to_s}"
 
190
        end
 
191
      end
 
192
 
 
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 }
 
199
 
 
200
        before :each do
 
201
          File.open(target, "w") {|f| f.print("some content\n")}
 
202
          set_mode(0644, target)
 
203
 
 
204
          FileUtils.symlink(target, path)
 
205
        end
 
206
 
 
207
        it "should read links instead of returning their checksums" do
 
208
          metadata.destination.should == target
 
209
        end
 
210
 
 
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}"
 
214
        end
 
215
 
 
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}"
 
218
        end
 
219
      end
 
220
    end
 
221
 
 
222
    describe Puppet::FileServing::Metadata, " when finding the file to use for setting attributes" do
 
223
      let(:path) { tmpfile('file_serving_metadata_find_file') }
 
224
 
 
225
      before :each do
 
226
        File.open(path, "w") {|f| f.print("some content\n")}
 
227
        set_mode(0755, path)
 
228
      end
 
229
 
 
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'
 
234
 
 
235
        FileUtils.touch(metadata.full_path)
 
236
 
 
237
        metadata.collect
 
238
      end
 
239
 
 
240
      it "should use the set base path if one is not provided" do
 
241
        metadata.collect
 
242
      end
 
243
 
 
244
      it "should raise an exception if the file does not exist" do
 
245
        File.delete(path)
 
246
 
 
247
        proc { metadata.collect}.should raise_error(Errno::ENOENT)
 
248
      end
 
249
    end
 
250
  end
 
251
 
 
252
  describe "on POSIX systems", :if => Puppet.features.posix? do
 
253
    let(:owner) {10}
 
254
    let(:group) {20}
 
255
 
 
256
    before :each do
 
257
      File::Stat.any_instance.stubs(:uid).returns owner
 
258
      File::Stat.any_instance.stubs(:gid).returns group
 
259
    end
 
260
 
 
261
    it_should_behave_like "metadata collector"
 
262
 
 
263
    def set_mode(mode, path)
 
264
      File.chmod(mode, path)
 
265
    end
 
266
  end
 
267
 
 
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'}
 
271
 
 
272
    before :each do
 
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
 
276
    end
 
277
 
 
278
    it_should_behave_like "metadata collector"
 
279
 
 
280
    def set_mode(mode, path)
 
281
      Puppet::Util::Windows::Security.set_mode(mode, path)
 
282
    end
 
283
  end
 
284
end
 
285
 
 
286
 
 
287
describe Puppet::FileServing::Metadata, " when pointing to a link", :unless => Puppet.features.microsoft_windows? do
244
288
  describe "when links are managed" do
245
289
    before do
246
290
      @file = Puppet::FileServing::Metadata.new("/base/path/my/file", :links => :manage)