summaryrefslogtreecommitdiff
path: root/spec/functional/resource/link_spec.rb
blob: 8e630d84f2d05afcd4857ebc933f0e375b54d70b (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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
#
# Author:: John Keiser (<jkeiser@opscode.com>)
# Copyright:: Copyright (c) 2011 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'spec_helper'

if windows?
  require 'chef/win32/file' #probably need this in spec_helper
end

describe Chef::Resource::Link do
  let(:file_base) { "file_spec" }

  let(:expect_updated?) {true}

  # We create the files in a different directory than tmp to exercise
  # different file deployment strategies more completely.
  let(:test_file_dir) do
    if windows?
      File.join(ENV['systemdrive'], "test-dir")
    else
      File.join(CHEF_SPEC_DATA, "test-dir")
    end
  end

  before do
    FileUtils::mkdir_p(test_file_dir)
  end

  after do
    FileUtils::rm_rf(test_file_dir)
  end

  let(:to) do
    File.join(test_file_dir, make_tmpname("to_spec"))
  end
  let(:target_file) do
    File.join(test_file_dir, make_tmpname("from_spec"))
  end

  after(:each) do
    begin
      cleanup_link(to) if File.exists?(to)
      cleanup_link(target_file) if File.exists?(target_file)
      cleanup_link(CHEF_SPEC_BACKUP_PATH) if File.exists?(CHEF_SPEC_BACKUP_PATH)
    rescue
      puts "Could not remove a file: #{$!}"
    end
  end

  def cleanup_link(path)
    if windows? && File.directory?(path)
      # If the link target is a directory rm_rf doesn't work all the
      # time on windows.
      system "rmdir '#{path}'"
    else
      FileUtils.rm_rf(path)
    end
  end

  def canonicalize(path)
    windows? ? path.gsub('/', '\\') : path
  end

  def symlink(a, b)
    if windows?
      Chef::ReservedNames::Win32::File.symlink(a, b)
    else
      File.symlink(a, b)
    end
  end
  def symlink?(file)
    if windows?
      Chef::ReservedNames::Win32::File.symlink?(file)
    else
      File.symlink?(file)
    end
  end
  def readlink(file)
    if windows?
      Chef::ReservedNames::Win32::File.readlink(file)
    else
      File.readlink(file)
    end
  end
  def link(a, b)
    if windows?
      Chef::ReservedNames::Win32::File.link(a, b)
    else
      File.link(a, b)
    end
  end

  def create_resource
    node = Chef::Node.new
    events = Chef::EventDispatch::Dispatcher.new
    cookbook_repo = File.expand_path(File.join(CHEF_SPEC_DATA, "cookbooks"))
    cookbook_collection = Chef::CookbookCollection.new(Chef::CookbookLoader.new(cookbook_repo))
    run_context = Chef::RunContext.new(node, cookbook_collection, events)
    resource = Chef::Resource::Link.new(target_file, run_context)
    resource.to(to)
    resource
  end

  let(:resource) do
    create_resource
  end

  describe "when supported on platform", :not_supported_on_win2k3 do
    shared_examples_for 'delete errors out' do
      it 'delete errors out' do
        lambda { resource.run_action(:delete) }.should raise_error(Chef::Exceptions::Link)
        (File.exist?(target_file) || symlink?(target_file)).should be_true
      end
    end

    shared_context 'delete is noop' do
      describe 'the :delete action' do
        before(:each) do
          @info = []
          Chef::Log.stub(:info) { |msg| @info << msg }
          resource.run_action(:delete)
        end

        it 'leaves the file deleted' do
          File.exist?(target_file).should be_false
          symlink?(target_file).should be_false
        end
        it 'does not mark the resource updated' do
          resource.should_not be_updated
        end
        it 'does not log that it deleted' do
          @info.include?("link[#{target_file}] deleted").should be_false
        end
      end
    end

    shared_context 'delete succeeds' do
      describe 'the :delete action' do
        before(:each) do
          @info = []
          Chef::Log.stub(:info) { |msg| @info << msg }
          resource.run_action(:delete)
        end

        it 'deletes the file' do
          File.exist?(target_file).should be_false
          symlink?(target_file).should be_false
        end
        it 'marks the resource updated' do
          resource.should be_updated
        end
        it 'logs that it deleted' do
          @info.include?("link[#{target_file}] deleted").should be_true
        end
      end
    end

    shared_context 'create symbolic link succeeds' do
      describe 'the :create action' do
        before(:each) do
          @info = []
          Chef::Log.stub(:info) { |msg| @info << msg }
          resource.run_action(:create)
        end

        it 'links to the target file' do
          symlink?(target_file).should be_true
          readlink(target_file).should == canonicalize(to)
        end
        it 'marks the resource updated' do
          resource.should be_updated
        end
        it 'logs that it created' do
          @info.include?("link[#{target_file}] created").should be_true
        end
      end
    end

    shared_context 'create symbolic link is noop' do
      describe 'the :create action' do
        before(:each) do
          @info = []
          Chef::Log.stub(:info) { |msg| @info << msg }
          resource.run_action(:create)
        end

        it 'leaves the file linked' do
          symlink?(target_file).should be_true
          readlink(target_file).should == canonicalize(to)
        end
        it 'does not mark the resource updated' do
          resource.should_not be_updated
        end
        it 'does not log that it created' do
          @info.include?("link[#{target_file}] created").should be_false
        end
      end
    end

    shared_context 'create hard link succeeds' do
      describe 'the :create action' do
        before(:each) do
          @info = []
          Chef::Log.stub(:info) { |msg| @info << msg }
          resource.run_action(:create)
        end
        it 'preserves the hard link' do
          File.exists?(target_file).should be_true
          symlink?(target_file).should be_false
          # Writing to one hardlinked file should cause both
          # to have the new value.
          IO.read(to).should == IO.read(target_file)
          File.open(to, "w") { |file| file.write('wowzers') }
          IO.read(target_file).should == 'wowzers'
        end
        it 'marks the resource updated' do
          resource.should be_updated
        end
        it 'logs that it created' do
          @info.include?("link[#{target_file}] created").should be_true
        end
      end
    end

    shared_context 'create hard link is noop' do
      describe 'the :create action' do
        before(:each) do
          @info = []
          Chef::Log.stub(:info) { |msg| @info << msg }
          resource.run_action(:create)
        end
        it 'links to the target file' do
          File.exists?(target_file).should be_true
          symlink?(target_file).should be_false
          # Writing to one hardlinked file should cause both
          # to have the new value.
          IO.read(to).should == IO.read(target_file)
          File.open(to, "w") { |file| file.write('wowzers') }
          IO.read(target_file).should == 'wowzers'
        end
        it 'does not mark the resource updated' do
          resource.should_not be_updated
        end
        it 'does not log that it created' do
          @info.include?("link[#{target_file}] created").should be_false
        end
      end
    end

    context "is symbolic" do

      context 'when the link destination is a file' do
        before(:each) do
          File.open(to, "w") do |file|
            file.write('woohoo')
          end
        end
        context 'and the link does not yet exist' do
          include_context 'create symbolic link succeeds'
          include_context 'delete is noop'
        end
        context 'and the link already exists and is a symbolic link' do
          context 'pointing at the target' do
            before(:each) do
              symlink(to, target_file)
              symlink?(target_file).should be_true
              readlink(target_file).should == canonicalize(to)
            end
            include_context 'create symbolic link is noop'
            include_context 'delete succeeds'
            it 'the :delete action does not delete the target file' do
              resource.run_action(:delete)
              File.exists?(to).should be_true
            end
          end
          context 'pointing somewhere else' do
            before(:each) do
              @other_target = File.join(test_file_dir, make_tmpname('other_spec'))
              File.open(@other_target, 'w') { |file| file.write('eek') }
              symlink(@other_target, target_file)
              symlink?(target_file).should be_true
              readlink(target_file).should == canonicalize(@other_target)
            end
            after(:each) do
              File.delete(@other_target)
            end
            include_context 'create symbolic link succeeds'
            include_context 'delete succeeds'
            it 'the :delete action does not delete the target file' do
              resource.run_action(:delete)
              File.exists?(to).should be_true
            end
          end
          context 'pointing nowhere' do
            before(:each) do
              nonexistent = File.join(test_file_dir, make_tmpname('nonexistent_spec'))
              symlink(nonexistent, target_file)
              symlink?(target_file).should be_true
              readlink(target_file).should == canonicalize(nonexistent)
            end
            include_context 'create symbolic link succeeds'
            include_context 'delete succeeds'
          end
        end
        context 'and the link already exists and is a hard link to the file' do
          before(:each) do
            link(to, target_file)
            File.exists?(target_file).should be_true
            symlink?(target_file).should be_false
          end
          include_context 'create symbolic link succeeds'
          it_behaves_like 'delete errors out'
        end
        context 'and the link already exists and is a file' do
          before(:each) do
            File.open(target_file, 'w') { |file| file.write('eek') }
          end
          include_context 'create symbolic link succeeds'
          it_behaves_like 'delete errors out'
        end
        context 'and the link already exists and is a directory' do
          before(:each) do
            Dir.mkdir(target_file)
          end
          it 'create errors out' do
            if windows?
              lambda { resource.run_action(:create) }.should raise_error(Errno::EACCES)
            elsif os_x? or solaris? or freebsd? or aix?
              lambda { resource.run_action(:create) }.should raise_error(Errno::EPERM)
            else
              lambda { resource.run_action(:create) }.should raise_error(Errno::EISDIR)
            end
          end
          it_behaves_like 'delete errors out'
        end
        context 'and the link already exists and is not writeable to this user', :pending do
        end
        it_behaves_like 'a securable resource without existing target' do
          let(:path) { target_file }
          def allowed_acl(sid, expected_perms)
            [ ACE.access_allowed(sid, expected_perms[:specific]) ]
          end
          def denied_acl(sid, expected_perms)
            [ ACE.access_denied(sid, expected_perms[:specific]) ]
          end
          def parent_inheritable_acls
            dummy_file_path = File.join(test_file_dir, "dummy_file")
            dummy_file = FileUtils.touch(dummy_file_path)
            dummy_desc = get_security_descriptor(dummy_file_path)
            FileUtils.rm_rf(dummy_file_path)
            dummy_desc
          end
        end
      end
      context 'when the link destination is a directory' do
        before(:each) do
          Dir.mkdir(to)
        end
        # On Windows, readlink fails to open the link.  FILE_FLAG_OPEN_REPARSE_POINT
        # might help, from http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx
        context 'and the link does not yet exist' do
          include_context 'create symbolic link succeeds'
          include_context 'delete is noop'
        end
        context 'and the link already exists and points to a different directory' do
          before(:each) do
            other_dir = File.join(test_file_dir, make_tmpname("other_dir"))
            Dir.mkdir(other_dir)
            symlink(other_dir, target_file)
          end
          include_context 'create symbolic link succeeds'
        end
      end
      context "when the link destination is a symbolic link" do
        context 'to a file that exists' do
          before(:each) do
            @other_target = File.join(test_file_dir, make_tmpname("other_spec"))
            File.open(@other_target, "w") { |file| file.write("eek") }
            symlink(@other_target, to)
            symlink?(to).should be_true
            readlink(to).should == canonicalize(@other_target)
          end
          after(:each) do
            File.delete(@other_target)
          end
          context 'and the link does not yet exist' do
            include_context 'create symbolic link succeeds'
            include_context 'delete is noop'
          end
        end
        context 'to a file that does not exist' do
          before(:each) do
            @other_target = File.join(test_file_dir, make_tmpname("other_spec"))
            symlink(@other_target, to)
            symlink?(to).should be_true
            readlink(to).should == canonicalize(@other_target)
          end
          context 'and the link does not yet exist' do
            include_context 'create symbolic link succeeds'
            include_context 'delete is noop'
          end
        end
      end
      context "when the link destination is not readable to this user", :pending do
      end
      context "when the link destination does not exist" do
        include_context 'create symbolic link succeeds'
        include_context 'delete is noop'
      end

      {
        '../' => 'with a relative link destination',
        '' => 'with a bare filename for the link destination'
      }.each do |prefix, desc|
        context desc do
          let(:to) { "#{prefix}#{File.basename(absolute_to)}" }
          let(:absolute_to) { File.join(test_file_dir, make_tmpname("to_spec")) }
          before(:each) do
            resource.to(to)
          end
          context 'when the link does not yet exist' do
            include_context 'create symbolic link succeeds'
            include_context 'delete is noop'
          end
          context 'when the link already exists and points at the target' do
            before(:each) do
              symlink(to, target_file)
              symlink?(target_file).should be_true
              readlink(target_file).should == canonicalize(to)
            end
            include_context 'create symbolic link is noop'
            include_context 'delete succeeds'
          end
          context 'when the link already exists and points at the target with an absolute path' do
            before(:each) do
              symlink(absolute_to, target_file)
              symlink?(target_file).should be_true
              readlink(target_file).should == canonicalize(absolute_to)
            end
            include_context 'create symbolic link succeeds'
            include_context 'delete succeeds'
          end
        end
      end
    end

    context "is a hard link" do
      before(:each) do
        resource.link_type(:hard)
      end

      context "when the link destination is a file" do
        before(:each) do
          File.open(to, "w") do |file|
            file.write('woohoo')
          end
        end
        context "and the link does not yet exist" do
          include_context 'create hard link succeeds'
          include_context 'delete is noop'
        end
        context "and the link already exists and is a symbolic link pointing at the same file" do
          before(:each) do
            symlink(to, target_file)
            symlink?(target_file).should be_true
            readlink(target_file).should == canonicalize(to)
          end
          include_context 'create hard link succeeds'
          it_behaves_like 'delete errors out'
        end
        context 'and the link already exists and is a hard link to the file' do
          before(:each) do
            link(to, target_file)
            File.exists?(target_file).should be_true
            symlink?(target_file).should be_false
          end
          include_context 'create hard link is noop'
          include_context 'delete succeeds'
          it 'the :delete action does not delete the target file' do
            resource.run_action(:delete)
            File.exists?(to).should be_true
          end
        end
        context "and the link already exists and is a file" do
          before(:each) do
            File.open(target_file, 'w') { |file| file.write('tomfoolery') }
          end
          include_context 'create hard link succeeds'
          it_behaves_like 'delete errors out'
        end
        context "and the link already exists and is a directory" do
          before(:each) do
            Dir.mkdir(target_file)
          end
          it 'errors out' do
            if windows?
              lambda { resource.run_action(:create) }.should raise_error(Errno::EACCES)
            elsif os_x? or solaris? or freebsd? or aix?
              lambda { resource.run_action(:create) }.should raise_error(Errno::EPERM)
            else
              lambda { resource.run_action(:create) }.should raise_error(Errno::EISDIR)
            end
          end
          it_behaves_like 'delete errors out'
        end
        context "and the link already exists and is not writeable to this user", :pending do
        end
        context "and specifies security attributes" do
          before(:each) do
            resource.owner(windows? ? 'Guest' : 'nobody')
          end
          it 'ignores them' do
            resource.run_action(:create)
            if windows?
              Chef::ReservedNames::Win32::Security.get_named_security_info(target_file).owner.should_not == SID.Guest
            else
              File.lstat(target_file).uid.should_not == Etc.getpwnam('nobody').uid
            end
          end
        end
      end
      context "when the link destination is a directory" do
        before(:each) do
          Dir.mkdir(to)
        end
        context 'and the link does not yet exist' do
          it 'create errors out' do
            lambda { resource.run_action(:create) }.should raise_error(windows? ? Chef::Exceptions::Win32APIError : Errno::EPERM)
          end
          include_context 'delete is noop'
        end
      end
      context "when the link destination is a symbolic link" do
        context 'to a real file' do
          before(:each) do
            @other_target = File.join(test_file_dir, make_tmpname("other_spec"))
            File.open(@other_target, "w") { |file| file.write("eek") }
            symlink(@other_target, to)
            symlink?(to).should be_true
            readlink(to).should == canonicalize(@other_target)
          end
          after(:each) do
            File.delete(@other_target)
          end
          context 'and the link does not yet exist' do
            it 'links to the target file' do
              resource.run_action(:create)
              File.exists?(target_file).should be_true
              # OS X gets angry about this sort of link.  Bug in OS X, IMO.
              pending('OS X/FreeBSD/AIX symlink? and readlink working on hard links to symlinks', :if => (os_x? or freebsd? or aix?)) do
                symlink?(target_file).should be_true
                readlink(target_file).should == canonicalize(@other_target)
              end
            end
            include_context 'delete is noop'
          end
        end
        context 'to a nonexistent file' do
          before(:each) do
            @other_target = File.join(test_file_dir, make_tmpname("other_spec"))
            symlink(@other_target, to)
            symlink?(to).should be_true
            readlink(to).should == canonicalize(@other_target)
          end
          context 'and the link does not yet exist' do
            it 'links to the target file' do
              pending('OS X/FreeBSD/AIX fails to create hardlinks to broken symlinks', :if => (os_x? or freebsd? or aix?)) do
                resource.run_action(:create)
                # Windows and Unix have different definitions of exists? here, and that's OK.
                if windows?
                  File.exists?(target_file).should be_true
                else
                  File.exists?(target_file).should be_false
                end
                symlink?(target_file).should be_true
                readlink(target_file).should == canonicalize(@other_target)
              end
            end
            include_context 'delete is noop'
          end
        end
      end
      context "when the link destination is not readable to this user", :pending do
      end
      context "when the link destination does not exist" do
        context 'and the link does not yet exist' do
          it 'create errors out' do
            lambda { resource.run_action(:create) }.should raise_error(Errno::ENOENT)
          end
          include_context 'delete is noop'
        end
      end
    end
  end

  describe "when not supported on platform", :win2k3_only do
    it "raises error" do
      lambda {resource}.should raise_error(Chef::Exceptions::Win32APIFunctionNotImplemented)
    end
  end
end