File: metadata_spec.rb

package info (click to toggle)
puppet 5.5.22-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 21,316 kB
  • sloc: ruby: 254,925; sh: 1,608; xml: 219; makefile: 153; sql: 103
file content (551 lines) | stat: -rw-r--r-- 19,568 bytes parent folder | download
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
require 'spec_helper'
require 'puppet/file_serving/metadata'
require 'matchers/json'

describe Puppet::FileServing::Metadata do
  let(:foobar) { File.expand_path('/foo/bar') }

  it "should be a subclass of Base" do
    expect(Puppet::FileServing::Metadata.superclass).to equal(Puppet::FileServing::Base)
  end

  it "should indirect file_metadata" do
    expect(Puppet::FileServing::Metadata.indirection.name).to eq(:file_metadata)
  end

  it "should have a method that triggers attribute collection" do
    expect(Puppet::FileServing::Metadata.new(foobar)).to respond_to(:collect)
  end

  it "should default to json" do
    expect(Puppet::FileServing::Metadata.default_format).to eq(:json)
  end

  it "should support json, pson, yaml" do
    # msgpack is optional, so using include instead of eq
    expect(Puppet::FileServing::Metadata.supported_formats).to include(:json, :pson, :yaml)
  end

  it "should support deserialization" do
    expect(Puppet::FileServing::Metadata).to respond_to(:from_data_hash)
  end

  describe "when serializing" do
    let(:metadata) { Puppet::FileServing::Metadata.new(foobar) }

    it "the data should include the path, relative_path, links, owner, group, mode, checksum, type, and destination" do
      expect(metadata.to_data_hash.keys.sort).to eq(%w{ path relative_path links owner group mode checksum type destination }.sort)
    end

    it "should pass the path in the hash verbatim" do
      expect(metadata.to_data_hash['path']).to eq(metadata.path)
    end

    it "should pass the relative_path in the hash verbatim" do
      expect(metadata.to_data_hash['relative_path']).to eq(metadata.relative_path)
    end

    it "should pass the links in the hash as a string" do
      expect(metadata.to_data_hash['links']).to eq(metadata.links.to_s)
    end

    it "should pass the path owner in the hash verbatim" do
      expect(metadata.to_data_hash['owner']).to eq(metadata.owner)
    end

    it "should pass the group in the hash verbatim" do
      expect(metadata.to_data_hash['group']).to eq(metadata.group)
    end

    it "should pass the mode in the hash verbatim" do
      expect(metadata.to_data_hash['mode']).to eq(metadata.mode)
    end

    it "should pass the ftype in the hash verbatim as the 'type'" do
      expect(metadata.to_data_hash['type']).to eq(metadata.ftype)
    end

    it "should pass the destination verbatim" do
      expect(metadata.to_data_hash['destination']).to eq(metadata.destination)
    end

    it "should pass the checksum in the hash as a nested hash" do
      expect(metadata.to_data_hash['checksum']).to be_is_a(Hash)
    end

    it "should pass the checksum_type in the hash verbatim as the checksum's type" do
      expect(metadata.to_data_hash['checksum']['type']).to eq(metadata.checksum_type)
    end

    it "should pass the checksum in the hash verbatim as the checksum's value" do
      expect(metadata.to_data_hash['checksum']['value']).to eq(metadata.checksum)
    end

    describe "when a source and content_uri are set" do
      before do
        metadata.source = '/foo'
        metadata.content_uri = 'puppet:///foo'
      end

      it "the data should include the path, relative_path, links, owner, group, mode, checksum, type, destination, source, and content_uri" do
        expect(metadata.to_data_hash.keys.sort).to eq(%w{ path relative_path links owner group mode checksum type destination source content_uri }.sort)
      end

      it "should pass the source in the hash verbatim" do
        expect(metadata.to_data_hash['source']).to eq(metadata.source)
      end

      it "should pass the content_uri in the hash verbatim" do
        expect(metadata.to_data_hash['content_uri']).to eq(metadata.content_uri)
      end
    end

    describe "when assigning a content_uri" do
      it "should fail if uri is invalid" do
        expect { metadata.content_uri = '://' }.to raise_error ArgumentError, /Could not understand URI :\/\//
      end

      it "should accept characters that require percent-encoding" do
        uri = 'puppet:///modules/foo/files/ %:?#[]@!$&\'()*+,;='
        metadata.content_uri = uri
        expect(metadata.content_uri).to eq(uri)
      end

      it "should accept UTF-8 characters" do
        # different UTF-8 widths
        # 1-byte A
        # 2-byte ۿ - http://www.fileformat.info/info/unicode/char/06ff/index.htm - 0xDB 0xBF / 219 191
        # 3-byte ᚠ - http://www.fileformat.info/info/unicode/char/16A0/index.htm - 0xE1 0x9A 0xA0 / 225 154 160
        # 4-byte <U+070E> - http://www.fileformat.info/info/unicode/char/2070E/index.htm - 0xF0 0xA0 0x9C 0x8E / 240 160 156 142
        mixed_utf8 = "A\u06FF\u16A0\u{2070E}" # Aۿᚠ<U+070E>

        uri = "puppet:///modules/foo/files/ #{mixed_utf8}"
        metadata.content_uri = uri
        expect(metadata.content_uri).to eq(uri)
        expect(metadata.content_uri.encoding).to eq(Encoding::UTF_8)
      end

      it "should always set it as UTF-8" do
        uri = "puppet:///modules/foo/files/".encode(Encoding::ASCII)
        metadata.content_uri = uri
        expect(metadata.content_uri).to eq(uri)
        expect(metadata.content_uri.encoding).to eq(Encoding::UTF_8)
      end

      it "should fail if uri is opaque" do
        expect { metadata.content_uri = 'scheme:www.example.com' }.to raise_error ArgumentError, "Cannot use opaque URLs 'scheme:www.example.com'"
      end

      it "should fail if uri is not a puppet scheme" do
        expect { metadata.content_uri = 'http://www.example.com' }.to raise_error ArgumentError, "Must use URLs of type puppet as content URI"
      end
    end
  end
end

describe Puppet::FileServing::Metadata, :uses_checksums => true do
  include JSONMatchers
  include PuppetSpec::Files

  shared_examples_for "metadata collector" do
    let(:metadata) do
      data = described_class.new(path)
      data.collect
      data
    end

    describe "when collecting attributes" do
      describe "when managing files" do
        let(:path) { tmpfile('file_serving_metadata') }
        let(:time) { Time.now }

        before :each do
          FileUtils.touch(path)
        end

        describe "checksumming" do
          with_digest_algorithms do
            before :each do
              File.open(path, "wb") {|f| f.print(plaintext)}
            end

            it "should default to a checksum of the proper type with the file's current checksum" do
              expect(metadata.checksum).to eq("{#{digest_algorithm}}#{checksum}")
            end

            it "should give a #{Puppet[:digest_algorithm]} when checksum_type is set" do
              Puppet[:digest_algorithm] = nil
              metadata.checksum_type = digest_algorithm
              metadata.collect
              expect(metadata.checksum).to eq("{#{digest_algorithm}}#{checksum}")
            end
          end

          it "should give a mtime checksum when checksum_type is set" do
            metadata.checksum_type = "mtime"
            expect(metadata).to receive(:mtime_file).and_return(time)
            metadata.collect
            expect(metadata.checksum).to eq("{mtime}#{time}")
          end

          it "should give a ctime checksum when checksum_type is set" do
            metadata.checksum_type = "ctime"
            expect(metadata).to receive(:ctime_file).and_return(time)
            metadata.collect
            expect(metadata.checksum).to eq("{ctime}#{time}")
          end
        end

        it "should validate against the schema" do
          expect(metadata.to_json).to validate_against('api/schemas/file_metadata.json')
        end

        describe "when a source and content_uri are set" do
          before do
            metadata.source = '/foo'
            metadata.content_uri = 'puppet:///foo'
          end

          it "should validate against the schema" do
            expect(metadata.to_json).to validate_against('api/schemas/file_metadata.json')
          end
        end
      end

      describe "when managing directories" do
        let(:path) { tmpdir('file_serving_metadata_dir') }
        let(:time) { Time.now }

        before :each do
          expect(metadata).to receive(:ctime_file).and_return(time)
        end

        it "should only use checksums of type 'ctime' for directories" do
          metadata.collect
          expect(metadata.checksum).to eq("{ctime}#{time}")
        end

        it "should only use checksums of type 'ctime' for directories even if checksum_type set" do
          metadata.checksum_type = "mtime"
          expect(metadata).not_to receive(:mtime_file)
          metadata.collect
          expect(metadata.checksum).to eq("{ctime}#{time}")
        end

        it "should validate against the schema" do
          metadata.collect
          expect(metadata.to_json).to validate_against('api/schemas/file_metadata.json')
        end
      end
    end
  end

  describe "WindowsStat", :if => Puppet.features.microsoft_windows? do
    include PuppetSpec::Files

    it "should return default owner, group and mode when the given path has an invalid DACL (such as a non-NTFS volume)" do
      invalid_error = Puppet::Util::Windows::Error.new('Invalid DACL', 1336)
      path = tmpfile('foo')
      FileUtils.touch(path)

      allow(Puppet::Util::Windows::Security).to receive(:get_owner).with(path).and_raise(invalid_error)
      allow(Puppet::Util::Windows::Security).to receive(:get_group).with(path).and_raise(invalid_error)
      allow(Puppet::Util::Windows::Security).to receive(:get_mode).with(path).and_raise(invalid_error)

      stat = Puppet::FileSystem.stat(path)

      win_stat = Puppet::FileServing::Metadata::WindowsStat.new(stat, path, :ignore)

      expect(win_stat.owner).to eq('S-1-5-32-544')
      expect(win_stat.group).to eq('S-1-0-0')
      expect(win_stat.mode).to eq(0644)
    end

    it "should still raise errors that are not the result of an 'Invalid DACL'" do
      invalid_error = ArgumentError.new('bar')
      path = tmpfile('bar')
      FileUtils.touch(path)

      allow(Puppet::Util::Windows::Security).to receive(:get_owner).with(path, :use).and_raise(invalid_error)
      allow(Puppet::Util::Windows::Security).to receive(:get_group).with(path, :use).and_raise(invalid_error)
      allow(Puppet::Util::Windows::Security).to receive(:get_mode).with(path, :use).and_raise(invalid_error)

      stat = Puppet::FileSystem.stat(path)

      expect { Puppet::FileServing::Metadata::WindowsStat.new(stat, path, :use) }.to raise_error("Unsupported Windows source permissions option use")
    end
  end

  shared_examples_for "metadata collector symlinks" do

    let(:metadata) do
      data = described_class.new(path)
      data.collect
      data
    end

    describe "when collecting attributes" do
      describe "when managing links" do
        # 'path' is a link that points to 'target'
        let(:path) { tmpfile('file_serving_metadata_link') }
        let(:target) { tmpfile('file_serving_metadata_target') }
        let(:fmode) { Puppet::FileSystem.lstat(path).mode & 0777 }

        before :each do
          File.open(target, "wb") {|f| f.print('some content')}
          set_mode(0644, target)

          Puppet::FileSystem.symlink(target, path)
        end

        it "should read links instead of returning their checksums" do
          expect(metadata.destination).to eq(target)
        end

        it "should validate against the schema" do
          expect(metadata.to_json).to validate_against('api/schemas/file_metadata.json')
        end
      end
    end

    describe Puppet::FileServing::Metadata, " when finding the file to use for setting attributes" do
      let(:path) { tmpfile('file_serving_metadata_find_file') }

      before :each do
        File.open(path, "wb") {|f| f.print('some content')}
        set_mode(0755, path)
      end

      it "should accept a base path to which the file should be relative" do
        dir = tmpdir('metadata_dir')
        metadata = described_class.new(dir)
        metadata.relative_path = 'relative_path'

        FileUtils.touch(metadata.full_path)

        metadata.collect
      end

      it "should use the set base path if one is not provided" do
        metadata.collect
      end

      it "should raise an exception if the file does not exist" do
        File.delete(path)

        expect { metadata.collect}.to raise_error(Errno::ENOENT)
      end

      it "should validate against the schema" do
        expect(metadata.to_json).to validate_against('api/schemas/file_metadata.json')
      end
    end
  end

  describe "on POSIX systems", :if => Puppet.features.posix? do
    let(:owner) {10}
    let(:group) {20}

    before :each do
      allow_any_instance_of(File::Stat).to receive(:uid).and_return(owner)
      allow_any_instance_of(File::Stat).to receive(:gid).and_return(group)
    end

    describe "when collecting attributes when managing files" do
      let(:metadata) do
        data = described_class.new(path)
        data.collect
        data
      end

      let(:path) { tmpfile('file_serving_metadata') }

      before :each do
        FileUtils.touch(path)
      end

      it "should set the owner to the Process's current owner" do
        expect(metadata.owner).to eq(Process.euid)
      end

      it "should set the group to the Process's current group" do
        expect(metadata.group).to eq(Process.egid)
      end

      it "should set the mode to the default mode" do
        set_mode(33261, path)

        expect(metadata.mode).to eq(0644)
      end
    end

    it_should_behave_like "metadata collector"
    it_should_behave_like "metadata collector symlinks"

    def set_mode(mode, path)
      File.chmod(mode, path)
    end
  end

  describe "on Windows systems", :if => Puppet.features.microsoft_windows? do
    let(:owner) {'S-1-1-50'}
    let(:group) {'S-1-1-51'}

    before :each do
      require 'puppet/util/windows/security'
      allow(Puppet::Util::Windows::Security).to receive(:get_owner).and_return(owner)
      allow(Puppet::Util::Windows::Security).to receive(:get_group).and_return(group)
    end

    describe "when collecting attributes when managing files" do
      let(:metadata) do
        data = described_class.new(path)
        data.collect
        data
      end

      let(:path) { tmpfile('file_serving_metadata') }

      before :each do
        FileUtils.touch(path)
      end

      it "should set the owner to the Process's current owner" do
        expect(metadata.owner).to eq("S-1-5-32-544")
      end

      it "should set the group to the Process's current group" do
        expect(metadata.group).to eq("S-1-0-0")
      end

      it "should set the mode to the default mode" do
        set_mode(33261, path)

        expect(metadata.mode).to eq(0644)
      end
    end

    it_should_behave_like "metadata collector"
    it_should_behave_like "metadata collector symlinks" if Puppet.features.manages_symlinks?

    describe "if ACL metadata cannot be collected" do
      let(:path) { tmpdir('file_serving_metadata_acl') }
      let(:metadata) do
        data = described_class.new(path)
        data.collect
        data
      end
      let (:invalid_dacl_error) do
        Puppet::Util::Windows::Error.new('Invalid DACL', 1336)
      end

      it "should default owner" do
        allow(Puppet::Util::Windows::Security).to receive(:get_owner).and_return(nil)

        expect(metadata.owner).to eq('S-1-5-32-544')
      end

      it "should default group" do
        allow(Puppet::Util::Windows::Security).to receive(:get_group).and_return(nil)

        expect(metadata.group).to eq('S-1-0-0')
      end

      it "should default mode" do
        allow(Puppet::Util::Windows::Security).to receive(:get_mode).and_return(nil)

        expect(metadata.mode).to eq(0644)
      end

      describe "when the path raises an Invalid ACL error" do
        # these simulate the behavior of a symlink file whose target does not support ACLs
        it "should default owner" do
          allow(Puppet::Util::Windows::Security).to receive(:get_owner).and_raise(invalid_dacl_error)

          expect(metadata.owner).to eq('S-1-5-32-544')
        end

        it "should default group" do
          allow(Puppet::Util::Windows::Security).to receive(:get_group).and_raise(invalid_dacl_error)

          expect(metadata.group).to eq('S-1-0-0')
        end

        it "should default mode" do
          allow(Puppet::Util::Windows::Security).to receive(:get_mode).and_raise(invalid_dacl_error)

          expect(metadata.mode).to eq(0644)
        end
      end
    end

    def set_mode(mode, path)
      Puppet::Util::Windows::Security.set_mode(mode, path)
    end
  end
end

describe Puppet::FileServing::Metadata, " when pointing to a link", :if => Puppet.features.manages_symlinks?, :uses_checksums => true do
  with_digest_algorithms do
    describe "when links are managed" do
      before do
        path = "/base/path/my/file"
        @file = Puppet::FileServing::Metadata.new(path, :links => :manage)
        stat = double("stat", :uid => 1, :gid => 2, :ftype => "link", :mode => 0755)
        expect(Puppet::FileSystem).to receive(:lstat).with(path).and_return(stat)
        expect(Puppet::FileSystem).to receive(:readlink).with(path).and_return("/some/other/path")
        allow(@file).to receive("#{digest_algorithm}_file".intern).and_return(checksum) # Remove these when :managed links are no longer checksumed.

        if Puppet.features.microsoft_windows?
          win_stat = double('win_stat', :owner => 'snarf', :group => 'thundercats',
            :ftype => 'link', :mode => 0755)
          allow(Puppet::FileServing::Metadata::WindowsStat).to receive(:new).and_return(win_stat)
        end
      end

      it "should store the destination of the link in :destination if links are :manage" do
        @file.collect
        expect(@file.destination).to eq("/some/other/path")
      end

      pending "should not collect the checksum if links are :manage" do
        # 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.
        @file.collect
        expect(@file.checksum).to be_nil
      end

      it "should collect the checksum if links are :manage" do # see pending note above
        @file.collect
        expect(@file.checksum).to eq("{#{digest_algorithm}}#{checksum}")
      end
    end

    describe "when links are followed" do
      before do
        path = "/base/path/my/file"
        @file = Puppet::FileServing::Metadata.new(path, :links => :follow)
        stat = double("stat", :uid => 1, :gid => 2, :ftype => "file", :mode => 0755)
        expect(Puppet::FileSystem).to receive(:stat).with(path).and_return(stat)
        expect(Puppet::FileSystem).not_to receive(:readlink)

        if Puppet.features.microsoft_windows?
          win_stat = double('win_stat', :owner => 'snarf', :group => 'thundercats',
            :ftype => 'file', :mode => 0755)
          allow(Puppet::FileServing::Metadata::WindowsStat).to receive(:new).and_return(win_stat)
        end

        allow(@file).to receive("#{digest_algorithm}_file".intern).and_return(checksum)
      end

      it "should not store the destination of the link in :destination if links are :follow" do
        @file.collect
        expect(@file.destination).to be_nil
      end

      it "should collect the checksum if links are :follow" do
        @file.collect
        expect(@file.checksum).to eq("{#{digest_algorithm}}#{checksum}")
      end
    end
  end
end