File: exec.rb

package info (click to toggle)
puppet 2.6.2-5%2Bsqueeze9
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 13,728 kB
  • ctags: 8,726
  • sloc: ruby: 110,196; sh: 934; lisp: 263; xml: 122; sql: 103; makefile: 90; python: 84
file content (847 lines) | stat: -rwxr-xr-x 19,493 bytes parent folder | download | duplicates (2)
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../../lib/puppettest'

require 'puppettest'

class TestExec < Test::Unit::TestCase
  include PuppetTest
  def test_numvsstring
    [0, "0"].each { |val|
      command = nil
      output = nil
      assert_nothing_raised {

        command = Puppet::Type.type(:exec).new(

          :command => "/bin/echo",

          :returns => val
        )
      }
      assert_events([:executed_command], command)
    }
  end

  def test_path_or_qualified
    command = nil
    output = nil
    assert_raise(Puppet::Error) {
      command = Puppet::Type.type(:exec).new(
        :command => "echo"
      )
    }
    assert_nothing_raised {

      command = Puppet::Type.type(:exec).new(

        :command => "echo",

        :path => "/usr/bin:/bin:/usr/sbin:/sbin"
      )
    }
    assert_nothing_raised {
      command = Puppet::Type.type(:exec).new(
        :command => "/bin/echo"
      )
    }
    assert_nothing_raised {

      command = Puppet::Type.type(:exec).new(

        :command => "/bin/echo",

        :path => "/usr/bin:/bin:/usr/sbin:/sbin"
      )
    }
  end

  def test_nonzero_returns
    assert_nothing_raised {

      command = Puppet::Type.type(:exec).new(

        :command => "mkdir /this/directory/does/not/exist",
        :path => "/usr/bin:/bin:/usr/sbin:/sbin",

        :returns => 1
      )
    }
    assert_nothing_raised {

      command = Puppet::Type.type(:exec).new(

        :command => "touch /etc",
        :path => "/usr/bin:/bin:/usr/sbin:/sbin",

        :returns => 1
      )
    }
    assert_nothing_raised {

      command = Puppet::Type.type(:exec).new(

        :command => "thiscommanddoesnotexist",
        :path => "/usr/bin:/bin:/usr/sbin:/sbin",

        :returns => 127
      )
    }
  end

  def test_cwdsettings
    command = nil
    dir = "/tmp"
    wd = Dir.chdir(dir) {
      Dir.getwd
    }
    assert_nothing_raised {

      command = Puppet::Type.type(:exec).new(

        :command => "pwd",
        :cwd => dir,
        :path => "/usr/bin:/bin:/usr/sbin:/sbin",

        :returns => 0
      )
    }
    assert_events([:executed_command], command)
    assert_equal(wd,command.output.chomp)
  end

  def test_refreshonly_functional
    file = nil
    cmd = nil
    tmpfile = tempfile
    @@tmpfiles.push tmpfile
    trans = nil

      file = Puppet::Type.type(:file).new(

        :path => tmpfile,

        :content => "yay"
    )
    # Get the file in sync
    assert_apply(file)

    # Now make an exec
    maker = tempfile
    assert_nothing_raised {

      cmd = Puppet::Type.type(:exec).new(

        :command => "touch #{maker}",
        :path => "/usr/bin:/bin:/usr/sbin:/sbin",
        :subscribe => file,

        :refreshonly => true
      )
    }

    assert(cmd, "did not make exec")

    assert_nothing_raised do
      assert(! cmd.check, "Check passed when refreshonly is set")
    end

    assert_events([], file, cmd)
    assert(! FileTest.exists?(maker), "made file without refreshing")

    # Now change our content, so we throw a refresh
    file[:content] = "yayness"
    assert_events([:content_changed, :restarted], file, cmd)
    assert(FileTest.exists?(maker), "file was not made in refresh")
  end

  def test_refreshonly
    cmd = true
    assert_nothing_raised {

      cmd = Puppet::Type.type(:exec).new(

        :command => "pwd",
        :path => "/usr/bin:/bin:/usr/sbin:/sbin",

        :refreshonly => true
      )
    }

    # Checks should always fail when refreshonly is enabled
    assert(!cmd.check, "Check passed with refreshonly true")

    # Now make sure it passes if we pass in "true"
    assert(cmd.check(true), "Check failed with refreshonly true while refreshing")

    # Now set it to false
    cmd[:refreshonly] = false
    assert(cmd.check, "Check failed with refreshonly false")
  end

  def test_creates
    file = tempfile
    exec = nil
    assert(! FileTest.exists?(file), "File already exists")
    assert_nothing_raised {

      exec = Puppet::Type.type(:exec).new(

        :command => "touch #{file}",
        :path => "/usr/bin:/bin:/usr/sbin:/sbin",

        :creates => file
      )
    }

    comp = mk_catalog("createstest", exec)
    assert_events([:executed_command], comp, "creates")
    assert_events([], comp, "creates")
  end

  # Verify that we can download the file that we're going to execute.
  def test_retrievethenmkexe
    exe = tempfile
    oexe = tempfile
    sh = %x{which sh}
    File.open(exe, "w") { |f| f.puts "#!#{sh}\necho yup" }


      file = Puppet::Type.type(:file).new(

        :path => oexe,
        :source => exe,

        :mode => 0755
    )


      exec = Puppet::Type.type(:exec).new(

        :command => oexe,

        :require => Puppet::Resource.new(:file, oexe)
    )

    comp = mk_catalog("Testing", file, exec)

    assert_events([:file_created, :executed_command], comp)
  end

  # Verify that we auto-require any managed scripts.
  def test_autorequire_files
    exe = tempfile
    oexe = tempfile
    sh = %x{which sh}
    File.open(exe, "w") { |f| f.puts "#!#{sh}\necho yup" }


      file = Puppet::Type.type(:file).new(

        :path => oexe,
        :source => exe,

        :mode => 755
    )

    basedir = File.dirname(oexe)

      baseobj = Puppet::Type.type(:file).new(

        :path => basedir,
        :source => exe,

        :mode => 755
    )


      ofile = Puppet::Type.type(:file).new(

        :path => exe,

        :mode => 755
    )


      exec = Puppet::Type.type(:exec).new(

        :command => oexe,
        :path => ENV["PATH"],

        :cwd => basedir
    )


      cat = Puppet::Type.type(:exec).new(

        :command => "cat #{exe} #{oexe}",

        :path => ENV["PATH"]
    )

    catalog = mk_catalog(file, baseobj, ofile, exec, cat)

    rels = nil
    assert_nothing_raised do
      rels = exec.autorequire
    end

    # Verify we get the script itself
    assert(rels.detect { |r| r.source == file }, "Exec did not autorequire its command")

    # Verify we catch the cwd
    assert(rels.detect { |r| r.source == baseobj }, "Exec did not autorequire its cwd")

    # Verify we don't require ourselves
    assert(! rels.detect { |r| r.source == ofile }, "Exec incorrectly required mentioned file")

    # We not longer autorequire inline files
    assert_nothing_raised do
      rels = cat.autorequire
    end
    assert(! rels.detect { |r| r.source == ofile }, "Exec required second inline file")
    assert(! rels.detect { |r| r.source == file }, "Exec required inline file")
  end

  def test_ifonly
    afile = tempfile
    bfile = tempfile

    exec = nil
    assert_nothing_raised {

      exec = Puppet::Type.type(:exec).new(

        :command => "touch #{bfile}",
        :onlyif => "test -f #{afile}",

        :path => ENV['PATH']
      )
    }

    assert_events([], exec)
    system("touch #{afile}")
    assert_events([:executed_command], exec)
    assert_events([:executed_command], exec)
    system("rm #{afile}")
    assert_events([], exec)
  end

  def test_unless
    afile = tempfile
    bfile = tempfile

    exec = nil
    assert_nothing_raised {

      exec = Puppet::Type.type(:exec).new(

        :command => "touch #{bfile}",
        :unless => "test -f #{afile}",

        :path => ENV['PATH']
      )
    }
    comp = mk_catalog(exec)

    assert_events([:executed_command], comp)
    assert_events([:executed_command], comp)
    system("touch #{afile}")
    assert_events([], comp)
    assert_events([], comp)
    system("rm #{afile}")
    assert_events([:executed_command], comp)
    assert_events([:executed_command], comp)
  end

  if Puppet.features.root?
    # Verify that we can execute commands as a special user
    def mknverify(file, user, group = nil, id = true)
      File.umask(0022)

      args = {
        :command => "touch #{file}",
        :path => "/usr/bin:/bin:/usr/sbin:/sbin",
      }

      if user
        #Puppet.warning "Using user #{user.name}"
        if id
          # convert to a string, because that's what the object expects
          args[:user] = user.uid.to_s
        else
          args[:user] = user.name
        end
      end

      if group
        #Puppet.warning "Using group #{group.name}"
        if id
          args[:group] = group.gid.to_s
        else
          args[:group] = group.name
        end
      end
      exec = nil
      assert_nothing_raised {
        exec = Puppet::Type.type(:exec).new(args)
      }

      comp = mk_catalog("usertest", exec)
      assert_events([:executed_command], comp, "usertest")

      assert(FileTest.exists?(file), "File does not exist")
      assert_equal(user.uid, File.stat(file).uid, "File UIDs do not match") if user

      # We can't actually test group ownership, unfortunately, because
      # behaviour changes wildlly based on platform.
      Puppet::Type.allclear
    end

    def test_userngroup
      file = tempfile
      [
        [nonrootuser], # just user, by name
        [nonrootuser, nil, true], # user, by uid
        [nil, nonrootgroup], # just group
        [nil, nonrootgroup, true], # just group, by id
        [nonrootuser, nonrootgroup], # user and group, by name
        [nonrootuser, nonrootgroup, true], # user and group, by id
      ].each { |ary|
        mknverify(file, *ary) {
        }
      }
    end
  end

  def test_logoutput
    exec = nil
    assert_nothing_raised {

      exec = Puppet::Type.type(:exec).new(

        :title => "logoutputesting",
        :path => "/usr/bin:/bin",
        :command => "echo logoutput is false",

        :logoutput => false
      )
    }

    assert_apply(exec)

    assert_nothing_raised {
      exec[:command] = "echo logoutput is true"
      exec[:logoutput] = true
    }

    assert_apply(exec)

    assert_nothing_raised {
      exec[:command] = "echo logoutput is on_failure"
      exec[:logoutput] = "on_failure"
    }

    assert_apply(exec)
  end

  def test_execthenfile
    exec = nil
    file = nil
    basedir = tempfile
    path = File.join(basedir, "subfile")
    assert_nothing_raised {

      exec = Puppet::Type.type(:exec).new(

        :title => "mkdir",
        :path => "/usr/bin:/bin",
        :creates => basedir,

        :command => "mkdir #{basedir}; touch #{path}"

      )
    }

    assert_nothing_raised {

      file = Puppet::Type.type(:file).new(

        :path => basedir,
        :recurse => true,
        :mode => "755",

        :require => Puppet::Resource.new("exec", "mkdir")
      )
    }

    comp = mk_catalog(file, exec)
    comp.finalize
    assert_events([:executed_command, :mode_changed], comp)

    assert(FileTest.exists?(path), "Exec ran first")
    assert(File.stat(path).mode & 007777 == 0755)
  end

  # Make sure all checks need to be fully qualified.
  def test_falsevals
    exec = nil
    assert_nothing_raised do
      exec = Puppet::Type.type(:exec).new(
        :command => "/bin/touch yayness"
      )
    end

    Puppet::Type.type(:exec).checks.each do |check|
      klass = Puppet::Type.type(:exec).paramclass(check)
      next if klass.value_collection.values.include? :false
      assert_raise(Puppet::Error, "Check '#{check}' did not fail on false") do
        exec[check] = false
      end
    end
  end

  def test_createcwdandexe
    exec1 = exec2 = nil
    dir = tempfile
    file = tempfile

    assert_nothing_raised {

      exec1 = Puppet::Type.type(:exec).new(

        :title => "one",
        :path => ENV["PATH"],

        :command => "mkdir #{dir}"
      )
    }

    assert_nothing_raised("Could not create exec w/out existing cwd") {

      exec2 = Puppet::Type.type(:exec).new(

        :title => "two",
        :path => ENV["PATH"],
        :command => "touch #{file}",

        :cwd => dir
      )
    }

    # Throw a check in there with our cwd and make sure it works
    assert_nothing_raised("Could not check with a missing cwd") do
      exec2[:unless] = "test -f /this/file/does/not/exist"
      exec2.retrieve
    end

    assert_raise(Puppet::Error) do
      exec2.property(:returns).sync
    end

    assert_nothing_raised do
      exec2[:require] = exec1
    end

    assert_apply(exec1, exec2)

    assert(FileTest.exists?(file))
  end

  def test_checkarrays
    exec = nil
    file = tempfile

    test = "test -f #{file}"

    assert_nothing_raised {

      exec = Puppet::Type.type(:exec).new(

        :path => ENV["PATH"],

        :command => "touch #{file}"
      )
    }

    assert_nothing_raised {
      exec[:unless] = test
    }

    assert_nothing_raised {
      assert(exec.check, "Check did not pass")
    }

    assert_nothing_raised {
      exec[:unless] = [test, test]
    }


    assert_nothing_raised {
      exec.finish
    }

    assert_nothing_raised {
      assert(exec.check, "Check did not pass")
    }

    assert_apply(exec)

    assert_nothing_raised {
      assert(! exec.check, "Check passed")
    }
  end

  def test_missing_checks_cause_failures
    # Solaris's sh exits with 1 here instead of 127
    return if Facter.value(:operatingsystem) == "Solaris"

      exec = Puppet::Type.type(:exec).new(

        :command => "echo true",
        :path => ENV["PATH"],

        :onlyif => "/bin/nosuchthingexists"
          )

    assert_raise(ArgumentError, "Missing command did not raise error") {
      exec.run("/bin/nosuchthingexists")
    }
  end

  def test_envparam

    exec = Puppet::Type.newexec(

      :command => "echo $envtest",
      :path => ENV["PATH"],

      :env => "envtest=yayness"
    )

    assert(exec, "Could not make exec")

    output = status = nil
    assert_nothing_raised {
      output, status = exec.run("echo $envtest")
    }

    assert_equal("yayness\n", output)

    # Now check whether we can do multiline settings
    assert_nothing_raised do
      exec[:env] = "envtest=a list of things
and stuff"
  end

  output = status = nil
  assert_nothing_raised {
    output, status = exec.run('echo "$envtest"')
    }
    assert_equal("a list of things\nand stuff\n", output)

    # Now test arrays
    assert_nothing_raised do
      exec[:env] = ["funtest=A", "yaytest=B"]
    end

    output = status = nil
    assert_nothing_raised {
      output, status = exec.run('echo "$funtest" "$yaytest"')
    }
    assert_equal("A B\n", output)
  end

  def test_environmentparam

    exec = Puppet::Type.newexec(

      :command => "echo $environmenttest",
      :path => ENV["PATH"],

      :environment => "environmenttest=yayness"
    )

    assert(exec, "Could not make exec")

    output = status = nil
    assert_nothing_raised {
      output, status = exec.run("echo $environmenttest")
    }

    assert_equal("yayness\n", output)

    # Now check whether we can do multiline settings
    assert_nothing_raised do
      exec[:environment] = "environmenttest=a list of things
and stuff"
  end

  output = status = nil
  assert_nothing_raised {
    output, status = exec.run('echo "$environmenttest"')
    }
    assert_equal("a list of things\nand stuff\n", output)

    # Now test arrays
    assert_nothing_raised do
      exec[:environment] = ["funtest=A", "yaytest=B"]
    end

    output = status = nil
    assert_nothing_raised {
      output, status = exec.run('echo "$funtest" "$yaytest"')
    }
    assert_equal("A B\n", output)
  end

  def test_timeout
    exec = Puppet::Type.type(:exec).new(:command => "sleep 1", :path => ENV["PATH"], :timeout => "0.2")
    time = Time.now

    assert_raise(Timeout::Error) {
      exec.run("sleep 1")
    }
    Puppet.info "#{Time.now.to_f - time.to_f} seconds, vs a timeout of #{exec[:timeout]}"


    assert_apply(exec)
  end

  # Testing #470
  def test_run_as_created_user
    exec = nil
    if Process.uid == 0
      user = "nosuchuser"
      assert_nothing_raised("Could not create exec with non-existent user") do

        exec = Puppet::Type.type(:exec).new(

          :command => "/bin/echo yay",

          :user => user
        )
      end
    end

    # Now try the group
    group = "nosuchgroup"
    assert_nothing_raised("Could not create exec with non-existent user") do

      exec = Puppet::Type.type(:exec).new(

        :command => "/bin/echo yay",

        :group => group
      )
    end
  end

  # make sure paths work both as arrays and strings
  def test_paths_as_arrays
    path = %w{/usr/bin /usr/sbin /sbin}
    exec = nil
    assert_nothing_raised("Could not use an array for the path") do
      exec = Puppet::Type.type(:exec).new(:command => "echo yay", :path => path)
    end
    assert_equal(path, exec[:path], "array-based path did not match")
    assert_nothing_raised("Could not use a string for the path") do
      exec = Puppet::Type.type(:exec).new(:command => "echo yay", :path => path.join(":"))
    end
    assert_equal(path, exec[:path], "string-based path did not match")
    assert_nothing_raised("Could not use a colon-separated strings in an array for the path") do
      exec = Puppet::Type.type(:exec).new(:command => "echo yay", :path => ["/usr/bin", "/usr/sbin:/sbin"])
    end
    assert_equal(path, exec[:path], "colon-separated array path did not match")
  end

  def test_checks_apply_to_refresh
    file = tempfile
    maker = tempfile

      exec = Puppet::Type.type(:exec).new(

        :title => "maker",
        :command => "touch #{maker}",

        :path => ENV["PATH"]
    )

    # Make sure it runs normally
    assert_apply(exec)
    assert(FileTest.exists?(maker), "exec did not run")
    File.unlink(maker)

    # Now make sure it refreshes
    assert_nothing_raised("Failed to refresh exec") do
      exec.refresh
    end
    assert(FileTest.exists?(maker), "exec did not run refresh")
    File.unlink(maker)

    # Now add the checks
    exec[:creates] = file

    # Make sure it runs when the file doesn't exist
    assert_nothing_raised("Failed to refresh exec") do
      exec.refresh
    end
    assert(FileTest.exists?(maker), "exec did not refresh when checks passed")
    File.unlink(maker)

    # Now create the file and make sure it doesn't refresh
    File.open(file, "w") { |f| f.puts "" }
    assert_nothing_raised("Failed to refresh exec") do
      exec.refresh
    end
    assert(! FileTest.exists?(maker), "exec refreshed with failing checks")
  end

  def test_explicit_refresh
    refresher = tempfile
    maker = tempfile

      exec = Puppet::Type.type(:exec).new(

        :title => "maker",
        :command => "touch #{maker}",

        :path => ENV["PATH"]
    )

    # Call refresh normally
    assert_nothing_raised do
      exec.refresh
    end

    # Make sure it created the normal file
    assert(FileTest.exists?(maker), "normal refresh did not work")
    File.unlink(maker)

    # Now reset refresh, and make sure it wins
    assert_nothing_raised("Could not set refresh parameter") do
      exec[:refresh] = "touch #{refresher}"
    end
    assert_nothing_raised do
      exec.refresh
    end

    # Make sure it created the normal file
    assert(FileTest.exists?(refresher), "refresh param was ignored")
    assert(! FileTest.exists?(maker), "refresh param also ran command")
  end

  if Puppet.features.root?
    def test_autorequire_user
      user = Puppet::Type.type(:user).new(:name => "yay")
      exec = Puppet::Type.type(:exec).new(:command => "/bin/echo fun", :user => "yay")

      rels = nil
      assert_nothing_raised("Could not evaluate autorequire") do
        rels = exec.autorequire
      end
      assert(rels.find { |r| r.source == user and r.target == exec }, "Exec did not autorequire user")
    end
  end
end