summaryrefslogtreecommitdiff
path: root/spec/unit/type/mount_spec.rb
blob: 75b2f505d2ed685c41f09995bc34a6f79a4d8dbc (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
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
#! /usr/bin/env ruby
require 'spec_helper'

describe Puppet::Type.type(:mount), :unless => Puppet.features.microsoft_windows? do

  before :each do
    Puppet::Type.type(:mount).stubs(:defaultprovider).returns providerclass
  end

  let :providerclass do
    described_class.provide(:fake_mount_provider) do
      attr_accessor :property_hash
      def create; end
      def destroy; end
      def exists?
        get(:ensure) != :absent
      end
      def mount; end
      def umount; end
      def mounted?
        [:mounted, :ghost].include?(get(:ensure))
      end
      mk_resource_methods
    end
  end

  let :provider do
    providerclass.new(:name => 'yay')
  end

  let :resource do
    described_class.new(:name => "yay", :audit => :ensure, :provider => provider)
  end

  let :ensureprop do
    resource.property(:ensure)
  end

  it "should have a :refreshable feature that requires the :remount method" do
    described_class.provider_feature(:refreshable).methods.should == [:remount]
  end

  it "should have no default value for :ensure" do
    mount = described_class.new(:name => "yay")
    mount.should(:ensure).should be_nil
  end

  it "should have :name as the only keyattribut" do
    described_class.key_attributes.should == [:name]
  end

  describe "when validating attributes" do
    [:name, :remounts, :provider].each do |param|
      it "should have a #{param} parameter" do
        described_class.attrtype(param).should == :param
      end
    end

    [:ensure, :device, :blockdevice, :fstype, :options, :pass, :dump, :atboot, :target].each do |param|
      it "should have a #{param} property" do
        described_class.attrtype(param).should == :property
      end
    end
  end

  describe "when validating values" do

    describe "for name" do
      it "should allow full qualified paths" do
        described_class.new(:name => "/mnt/foo")[:name].should == '/mnt/foo'
      end

      it "should remove trailing slashes" do
        described_class.new(:name => '/')[:name].should == '/'
        described_class.new(:name => '//')[:name].should == '/'
        described_class.new(:name => '/foo/')[:name].should == '/foo'
        described_class.new(:name => '/foo/bar/')[:name].should == '/foo/bar'
        described_class.new(:name => '/foo/bar/baz//')[:name].should == '/foo/bar/baz'
      end

      it "should not allow spaces" do
        expect { described_class.new(:name => "/mnt/foo bar") }.to raise_error Puppet::Error, /name.*whitespace/
      end

      it "should allow pseudo mountpoints (e.g. swap)" do
        described_class.new(:name => 'none')[:name].should == 'none'
      end
    end

    describe "for ensure" do
      it "should alias :present to :defined as a value to :ensure" do
        mount = described_class.new(:name => "yay", :ensure => :present)
        mount.should(:ensure).should == :defined
      end

      it "should support :present as a value to :ensure" do
        expect { described_class.new(:name => "yay", :ensure => :present) }.to_not raise_error
      end

      it "should support :defined as a value to :ensure" do
        expect { described_class.new(:name => "yay", :ensure => :defined) }.to_not raise_error
      end

      it "should support :unmounted as a value to :ensure" do
        expect { described_class.new(:name => "yay", :ensure => :unmounted) }.to_not raise_error
      end

      it "should support :absent as a value to :ensure" do
        expect { described_class.new(:name => "yay", :ensure => :absent) }.to_not raise_error
      end

      it "should support :mounted as a value to :ensure" do
        expect { described_class.new(:name => "yay", :ensure => :mounted) }.to_not raise_error
      end

      it "should not support other values for :ensure" do
        expect { described_class.new(:name => "yay", :ensure => :mount) }.to raise_error Puppet::Error, /Invalid value/
      end
    end

    describe "for device" do
      it "should support normal /dev paths for device" do
        expect { described_class.new(:name => "/foo", :ensure => :present, :device => '/dev/hda1') }.to_not raise_error
        expect { described_class.new(:name => "/foo", :ensure => :present, :device => '/dev/dsk/c0d0s0') }.to_not raise_error
      end

      it "should support labels for device" do
        expect { described_class.new(:name => "/foo", :ensure => :present, :device => 'LABEL=/boot') }.to_not raise_error
        expect { described_class.new(:name => "/foo", :ensure => :present, :device => 'LABEL=SWAP-hda6') }.to_not raise_error
      end

      it "should support pseudo devices for device" do
        expect { described_class.new(:name => "/foo", :ensure => :present, :device => 'ctfs') }.to_not raise_error
        expect { described_class.new(:name => "/foo", :ensure => :present, :device => 'swap') }.to_not raise_error
        expect { described_class.new(:name => "/foo", :ensure => :present, :device => 'sysfs') }.to_not raise_error
        expect { described_class.new(:name => "/foo", :ensure => :present, :device => 'proc') }.to_not raise_error
      end

      it 'should not support whitespace in device' do
        expect { described_class.new(:name => "/foo", :ensure => :present, :device => '/dev/my dev/foo') }.to raise_error Puppet::Error, /device.*whitespace/
        expect { described_class.new(:name => "/foo", :ensure => :present, :device => "/dev/my\tdev/foo") }.to raise_error Puppet::Error, /device.*whitespace/
      end
    end

    describe "for blockdevice" do
      before :each do
        # blockdevice is only used on Solaris
        Facter.stubs(:value).with(:operatingsystem).returns 'Solaris'
        Facter.stubs(:value).with(:osfamily).returns 'Solaris'
      end

      it "should support normal /dev/rdsk paths for blockdevice" do
        expect { described_class.new(:name => "/foo", :ensure => :present, :blockdevice => '/dev/rdsk/c0d0s0') }.to_not raise_error
      end

      it "should support a dash for blockdevice" do
        expect { described_class.new(:name => "/foo", :ensure => :present, :blockdevice => '-') }.to_not raise_error
      end

      it "should not support whitespace in blockdevice" do
        expect { described_class.new(:name => "/foo", :ensure => :present, :blockdevice => '/dev/my dev/foo') }.to raise_error Puppet::Error, /blockdevice.*whitespace/
        expect { described_class.new(:name => "/foo", :ensure => :present, :blockdevice => "/dev/my\tdev/foo") }.to raise_error Puppet::Error, /blockdevice.*whitespace/
      end

      it "should default to /dev/rdsk/DEVICE if device is /dev/dsk/DEVICE" do
        obj = described_class.new(:name => "/foo", :device => '/dev/dsk/c0d0s0')
        obj[:blockdevice].should == '/dev/rdsk/c0d0s0'
      end

      it "should default to - if it is an nfs-share" do
        obj = described_class.new(:name => "/foo", :device => "server://share", :fstype => 'nfs')
        obj[:blockdevice].should == '-'
      end

      it "should have no default otherwise" do
        described_class.new(:name => "/foo")[:blockdevice].should == nil
        described_class.new(:name => "/foo", :device => "/foo")[:blockdevice].should == nil
      end

      it "should overwrite any default if blockdevice is explicitly set" do
        described_class.new(:name => "/foo", :device => '/dev/dsk/c0d0s0', :blockdevice => '/foo')[:blockdevice].should == '/foo'
        described_class.new(:name => "/foo", :device => "server://share", :fstype => 'nfs', :blockdevice => '/foo')[:blockdevice].should == '/foo'
      end
    end

    describe "for fstype" do
      it "should support valid fstypes" do
        expect { described_class.new(:name => "/foo", :ensure => :present, :fstype => 'ext3') }.to_not raise_error
        expect { described_class.new(:name => "/foo", :ensure => :present, :fstype => 'proc') }.to_not raise_error
        expect { described_class.new(:name => "/foo", :ensure => :present, :fstype => 'sysfs') }.to_not raise_error
      end

      it "should support auto as a special fstype" do
        expect { described_class.new(:name => "/foo", :ensure => :present, :fstype => 'auto') }.to_not raise_error
      end

      it "should not support whitespace in fstype" do
        expect { described_class.new(:name => "/foo", :ensure => :present, :fstype => 'ext 3') }.to raise_error Puppet::Error, /fstype.*whitespace/
      end
    end

    describe "for options" do
      it "should support a single option" do
        expect { described_class.new(:name => "/foo", :ensure => :present, :options => 'ro') }.to_not raise_error
      end

      it "should support muliple options as a comma separated list" do
        expect { described_class.new(:name => "/foo", :ensure => :present, :options => 'ro,rsize=4096') }.to_not raise_error
      end

      it "should not support whitespace in options" do
        expect { described_class.new(:name => "/foo", :ensure => :present, :options => ['ro','foo bar','intr']) }.to raise_error Puppet::Error, /option.*whitespace/
      end
    end

    describe "for pass" do
      it "should support numeric values" do
        expect { described_class.new(:name => "/foo", :ensure => :present, :pass => '0') }.to_not raise_error
        expect { described_class.new(:name => "/foo", :ensure => :present, :pass => '1') }.to_not raise_error
        expect { described_class.new(:name => "/foo", :ensure => :present, :pass => '2') }.to_not raise_error
      end

      it "should support - on Solaris" do
        Facter.stubs(:value).with(:operatingsystem).returns 'Solaris'
        Facter.stubs(:value).with(:osfamily).returns 'Solaris'
        expect { described_class.new(:name => "/foo", :ensure => :present, :pass => '-') }.to_not raise_error
      end

      it "should default to 0 on non Solaris" do
        Facter.stubs(:value).with(:osfamily).returns nil
        Facter.stubs(:value).with(:operatingsystem).returns 'HP-UX'
        described_class.new(:name => "/foo", :ensure => :present)[:pass].should == 0
      end

      it "should default to - on Solaris" do
        Facter.stubs(:value).with(:operatingsystem).returns 'Solaris'
        Facter.stubs(:value).with(:osfamily).returns 'Solaris'
        described_class.new(:name => "/foo", :ensure => :present)[:pass].should == '-'
      end
    end

    describe "for dump" do
      it "should support 0 as a value for dump" do
        expect { described_class.new(:name => "/foo", :ensure => :present, :dump => '0') }.to_not raise_error
      end

      it "should support 1 as a value for dump" do
        expect { described_class.new(:name => "/foo", :ensure => :present, :dump => '1') }.to_not raise_error
      end

      # Unfortunately the operatingsystem is evaluatet at load time so I am unable to stub operatingsystem
      it "should support 2 as a value for dump on FreeBSD", :if => Facter.value(:operatingsystem) == 'FreeBSD' do
        expect { described_class.new(:name => "/foo", :ensure => :present, :dump => '2') }.to_not raise_error
      end

      it "should not support 2 as a value for dump when not on FreeBSD", :if => Facter.value(:operatingsystem) != 'FreeBSD' do
        expect { described_class.new(:name => "/foo", :ensure => :present, :dump => '2') }.to raise_error Puppet::Error, /Invalid value/
      end

      it "should default to 0" do
        described_class.new(:name => "/foo", :ensure => :present)[:dump].should == 0
      end
    end

    describe "for atboot" do
      it "does not allow non-boolean values" do
        expect { described_class.new(:name => "/foo", :ensure => :present, :atboot => 'unknown') }.to raise_error Puppet::Error, /expected a boolean value/
      end

      it "interprets yes as yes" do
        resource = described_class.new(:name => "/foo", :ensure => :present, :atboot => :yes)

        expect(resource[:atboot]).to eq(:yes)
      end

      it "interprets true as yes" do
        resource = described_class.new(:name => "/foo", :ensure => :present, :atboot => :true)

        expect(resource[:atboot]).to eq(:yes)
      end

      it "interprets no as no" do
        resource = described_class.new(:name => "/foo", :ensure => :present, :atboot => :no)

        expect(resource[:atboot]).to eq(:no)
      end

      it "interprets false as no" do
        resource = described_class.new(:name => "/foo", :ensure => :present, :atboot => false)

        expect(resource[:atboot]).to eq(:no)
      end
    end
  end


  describe "when changing the host" do
    def test_ensure_change(options)
      provider.set(:ensure => options[:from])
      provider.expects(:create).times(options[:create] || 0)
      provider.expects(:destroy).times(options[:destroy] || 0)
      provider.expects(:mount).never
      provider.expects(:unmount).times(options[:unmount] || 0)
      ensureprop.stubs(:syncothers)
      ensureprop.should = options[:to]
      ensureprop.sync
      (!!provider.property_hash[:needs_mount]).should == (!!options[:mount])
    end

    it "should create itself when changing from :ghost to :present" do
      test_ensure_change(:from => :ghost, :to => :present, :create => 1)
    end

    it "should create itself when changing from :absent to :present" do
      test_ensure_change(:from => :absent, :to => :present, :create => 1)
    end

    it "should create itself and unmount when changing from :ghost to :unmounted" do
      test_ensure_change(:from => :ghost, :to => :unmounted, :create => 1, :unmount => 1)
    end

    it "should unmount resource when changing from :mounted to :unmounted" do
      test_ensure_change(:from => :mounted, :to => :unmounted, :unmount => 1)
    end

    it "should create itself when changing from :absent to :unmounted" do
      test_ensure_change(:from => :absent, :to => :unmounted, :create => 1)
    end

    it "should unmount resource when changing from :ghost to :absent" do
      test_ensure_change(:from => :ghost, :to => :absent, :unmount => 1)
    end

    it "should unmount and destroy itself when changing from :mounted to :absent" do
      test_ensure_change(:from => :mounted, :to => :absent, :destroy => 1, :unmount => 1)
    end

    it "should destroy itself when changing from :unmounted to :absent" do
      test_ensure_change(:from => :unmounted, :to => :absent, :destroy => 1)
    end

    it "should create itself when changing from :ghost to :mounted" do
      test_ensure_change(:from => :ghost, :to => :mounted, :create => 1)
    end

    it "should create itself and mount when changing from :absent to :mounted" do
      test_ensure_change(:from => :absent, :to => :mounted, :create => 1, :mount => 1)
    end

    it "should mount resource when changing from :unmounted to :mounted" do
      test_ensure_change(:from => :unmounted, :to => :mounted, :mount => 1)
    end


    it "should be in sync if it is :absent and should be :absent" do
      ensureprop.should = :absent
      ensureprop.safe_insync?(:absent).should == true
    end

    it "should be out of sync if it is :absent and should be :defined" do
      ensureprop.should = :defined
      ensureprop.safe_insync?(:absent).should == false
    end

    it "should be out of sync if it is :absent and should be :mounted" do
      ensureprop.should = :mounted
      ensureprop.safe_insync?(:absent).should == false
    end

    it "should be out of sync if it is :absent and should be :unmounted" do
      ensureprop.should = :unmounted
      ensureprop.safe_insync?(:absent).should == false
    end

    it "should be out of sync if it is :mounted and should be :absent" do
      ensureprop.should = :absent
      ensureprop.safe_insync?(:mounted).should == false
    end

    it "should be in sync if it is :mounted and should be :defined" do
      ensureprop.should = :defined
      ensureprop.safe_insync?(:mounted).should == true
    end

    it "should be in sync if it is :mounted and should be :mounted" do
      ensureprop.should = :mounted
      ensureprop.safe_insync?(:mounted).should == true
    end

    it "should be out in sync if it is :mounted and should be :unmounted" do
      ensureprop.should = :unmounted
      ensureprop.safe_insync?(:mounted).should == false
    end


    it "should be out of sync if it is :unmounted and should be :absent" do
      ensureprop.should = :absent
      ensureprop.safe_insync?(:unmounted).should == false
    end

    it "should be in sync if it is :unmounted and should be :defined" do
      ensureprop.should = :defined
      ensureprop.safe_insync?(:unmounted).should == true
    end

    it "should be out of sync if it is :unmounted and should be :mounted" do
      ensureprop.should = :mounted
      ensureprop.safe_insync?(:unmounted).should == false
    end

    it "should be in sync if it is :unmounted and should be :unmounted" do
      ensureprop.should = :unmounted
      ensureprop.safe_insync?(:unmounted).should == true
    end


    it "should be out of sync if it is :ghost and should be :absent" do
      ensureprop.should = :absent
      ensureprop.safe_insync?(:ghost).should == false
    end

    it "should be out of sync if it is :ghost and should be :defined" do
      ensureprop.should = :defined
      ensureprop.safe_insync?(:ghost).should == false
    end

    it "should be out of sync if it is :ghost and should be :mounted" do
      ensureprop.should = :mounted
      ensureprop.safe_insync?(:ghost).should == false
    end

    it "should be out of sync if it is :ghost and should be :unmounted" do
      ensureprop.should = :unmounted
      ensureprop.safe_insync?(:ghost).should == false
    end
  end

  describe "when responding to refresh" do
    pending "2.6.x specifies slightly different behavior and the desired behavior needs to be clarified and revisited.  See ticket #4904" do
      it "should remount if it is supposed to be mounted" do
        resource[:ensure] = "mounted"
        provider.expects(:remount)

        resource.refresh
      end

      it "should not remount if it is supposed to be present" do
        resource[:ensure] = "present"
        provider.expects(:remount).never

        resource.refresh
      end

      it "should not remount if it is supposed to be absent" do
        resource[:ensure] = "absent"
        provider.expects(:remount).never

        resource.refresh
      end

      it "should not remount if it is supposed to be defined" do
        resource[:ensure] = "defined"
        provider.expects(:remount).never

        resource.refresh
      end

      it "should not remount if it is supposed to be unmounted" do
        resource[:ensure] = "unmounted"
        provider.expects(:remount).never

        resource.refresh
      end

      it "should not remount swap filesystems" do
        resource[:ensure] = "mounted"
        resource[:fstype] = "swap"
        provider.expects(:remount).never

        resource.refresh
      end
    end
  end

  describe "when modifying an existing mount entry" do

    let :initial_values do
      {
        :ensure      => :mounted,
        :name        => '/mnt/foo',
        :device      => "/foo/bar",
        :blockdevice => "/other/bar",
        :target      => "/what/ever",
        :options     => "soft",
        :pass        => 0,
        :dump        => 0,
        :atboot      => :no,
      }
    end


    let :resource do
      described_class.new(initial_values.merge(:provider => provider))
    end

    let :provider do
      providerclass.new(initial_values)
    end

    def run_in_catalog(*resources)
      Puppet::Util::Storage.stubs(:store)
      catalog = Puppet::Resource::Catalog.new
      catalog.add_resource *resources
      catalog.apply
    end

    it "should use the provider to change the dump value" do
      provider.expects(:dump=).with(1)

      resource[:dump] = 1

      run_in_catalog(resource)
    end

    it "should umount before flushing changes to disk" do
      syncorder = sequence('syncorder')

      provider.expects(:unmount).in_sequence(syncorder)
      provider.expects(:options=).in_sequence(syncorder).with 'hard'
      resource.expects(:flush).in_sequence(syncorder) # Call inside syncothers
      resource.expects(:flush).in_sequence(syncorder) # I guess transaction or anything calls flush again

      resource[:ensure] = :unmounted
      resource[:options] = 'hard'

      run_in_catalog(resource)
    end
  end

  describe "establishing autorequires" do

    def create_resource(path)
      described_class.new(
        :name => path,
        :provider => providerclass.new(path)
      )
    end

    def create_catalog(*resources)
      catalog = Puppet::Resource::Catalog.new
      resources.each do |resource|
        catalog.add_resource resource
      end

      catalog
    end

    let(:root_mount) { create_resource("/") }
    let(:var_mount)  { create_resource("/var") }
    let(:log_mount)  { create_resource("/var/log") }

    before do
      create_catalog(root_mount, var_mount, log_mount)
    end

    it "adds no autorequires for the root mount" do
      expect(root_mount.autorequire).to be_empty
    end

    it "adds the parent autorequire for a mount with one parent" do
      parent_relationship = var_mount.autorequire[0]

      expect(var_mount.autorequire).to have_exactly(1).item

      expect(parent_relationship.source).to eq root_mount
      expect(parent_relationship.target).to eq var_mount
    end

    it "adds both parent autorequires for a mount with two parents" do
      grandparent_relationship = log_mount.autorequire[0]
      parent_relationship = log_mount.autorequire[1]

      expect(log_mount.autorequire).to have_exactly(2).items

      expect(grandparent_relationship.source).to eq root_mount
      expect(grandparent_relationship.target).to eq log_mount

      expect(parent_relationship.source).to eq var_mount
      expect(parent_relationship.target).to eq log_mount
    end
  end
end