summaryrefslogtreecommitdiffstats
path: root/test/lib/puppettest.rb
blob: a60092cf73833866013299dd7b0a0e8af8987568 (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
# Add .../test/lib
testlib = File.expand_path(File.dirname(__FILE__))
$LOAD_PATH.unshift(testlib) unless $LOAD_PATH.include?(testlib)
# Add .../lib
mainlib = File.expand_path(File.join(File.dirname(__FILE__), '../../lib'))
$LOAD_PATH.unshift(mainlib) unless $LOAD_PATH.include?(mainlib)

require 'puppet'
require 'mocha'

# Only load the test/unit class if we're not in the spec directory.
# Else we get the bogus 'no tests, no failures' message.
unless Dir.getwd =~ /spec/
  require 'test/unit'
end

# Yay; hackish but it works
if ARGV.include?("-d")
  ARGV.delete("-d")
  $console = true
end

require File.expand_path(File.join(File.dirname(__FILE__), '../../spec/monkey_patches/publicize_methods'))

module PuppetTest
  # These need to be here for when rspec tests use these
  # support methods.
  @@tmpfiles = []

  # Munge cli arguments, so we can enable debugging if we want
  # and so we can run just specific methods.
  def self.munge_argv
    require 'getoptlong'


      result = GetoptLong.new(

        [ "--debug",    "-d", GetoptLong::NO_ARGUMENT       ],
        [ "--resolve",  "-r", GetoptLong::REQUIRED_ARGUMENT ],
        [ "-n",               GetoptLong::REQUIRED_ARGUMENT ],

        [ "--help",     "-h", GetoptLong::NO_ARGUMENT       ]
    )

    usage = "USAGE: TESTOPTS='[-n <method> -n <method> ...] [-d]' rake [target] [target] ..."

    opts = []

    dir = method = nil
    result.each { |opt,arg|
      case opt
      when "--resolve"
        dir, method = arg.split(",")
      when "--debug"
        $puppet_debug = true
        Puppet::Util::Log.level = :debug
        Puppet::Util::Log.newdestination(:console)
      when "--help"
        puts usage
        exit
      else
        opts << opt << arg
      end
    }
    suites = nil

    args = ARGV.dup

    # Reset the options, so the test suite can deal with them (this is
    # what makes things like '-n' work).
    opts.each { |o| ARGV << o }

    args
  end

  # Find the root of the Puppet tree; this is not the test directory, but
  # the parent of that dir.
  def basedir(*list)
    unless defined?(@@basedir)
      Dir.chdir(File.dirname(__FILE__)) do
        @@basedir = File.dirname(File.dirname(Dir.getwd))
      end
    end
    if list.empty?
      @@basedir
    else
      File.join(@@basedir, *list)
    end
  end

  def datadir(*list)
    File.join(basedir, "test", "data", *list)
  end

  def exampledir(*args)
    @@exampledir = File.join(basedir, "examples") unless defined?(@@exampledir)

    if args.empty?
      return @@exampledir
    else
      return File.join(@@exampledir, *args)
    end
  end

  module_function :basedir, :datadir, :exampledir

  def cleanup(&block)
    @@cleaners << block
  end

  # Rails clobbers RUBYLIB, thanks
  def libsetup
    curlibs = ENV["RUBYLIB"].split(":")
    $LOAD_PATH.reject do |dir| dir =~ /^\/usr/ end.each do |dir|
      curlibs << dir unless curlibs.include?(dir)
    end

    ENV["RUBYLIB"] = curlibs.join(":")
  end

  def logcollector
    collector = []
    Puppet::Util::Log.newdestination(collector)
    cleanup do
      Puppet::Util::Log.close(collector)
    end
    collector
  end

  def rake?
    $0 =~ /test_loader/
  end

  # Redirect stdout and stderr
  def redirect
    @stderr = tempfile
    @stdout = tempfile
    $stderr = File.open(@stderr, "w")
    $stdout = File.open(@stdout, "w")

    cleanup do
      $stderr = STDERR
      $stdout = STDOUT
    end
  end

  def setup
    ENV["PATH"] += File::PATH_SEPARATOR + "/usr/sbin" unless ENV["PATH"].split(File::PATH_SEPARATOR).include?("/usr/sbin")
    @memoryatstart = Puppet::Util.memory
    if defined?(@@testcount)
      @@testcount += 1
    else
      @@testcount = 0
    end


      @configpath = File.join(
        tmpdir,

      "configdir" + @@testcount.to_s + "/"
    )

    unless defined? $user and $group
      $user = nonrootuser.uid.to_s
      $group = nonrootgroup.gid.to_s
    end

    Puppet.settings.clear
    Puppet[:user] = $user
    Puppet[:group] = $group

    Puppet[:confdir] = @configpath
    Puppet[:vardir] = @configpath

    Dir.mkdir(@configpath) unless File.exists?(@configpath)

    @@tmpfiles << @configpath << tmpdir
    @@tmppids = []

    @@cleaners = []

    @logs = []

    # If we're running under rake, then disable debugging and such.
    #if rake? or ! Puppet[:debug]
    #if defined?($puppet_debug) or ! rake?
      Puppet[:color] = false if textmate?
      Puppet::Util::Log.newdestination(Puppet::Test::LogCollector.new(@logs))
      if defined? $console
        Puppet.info @method_name
        Puppet::Util::Log.newdestination(:console)
        Puppet[:trace] = true
      end
      Puppet::Util::Log.level = :debug
      #$VERBOSE = 1
    #else
    #    Puppet::Util::Log.close
    #    Puppet::Util::Log.newdestination(@logs)
    #    Puppet[:httplog] = tempfile
    #end

    Puppet[:ignoreschedules] = true

    #@start = Time.now

    #Facter.stubs(:value).returns "stubbed_value"
    #Facter.stubs(:to_hash).returns({})
  end

  def tempfile(suffix = '')
    if defined?(@@tmpfilenum)
      @@tmpfilenum += 1
    else
      @@tmpfilenum = 1
    end

    f = File.join(self.tmpdir, "tempfile_" + @@tmpfilenum.to_s + suffix)
    @@tmpfiles ||= []
    @@tmpfiles << f
    f
  end

  def textmate?
    !!ENV["TM_FILENAME"]
  end

  def tstdir
    dir = tempfile
    Dir.mkdir(dir)
    dir
  end

  def tmpdir
    unless @tmpdir
      @tmpdir = case Facter["operatingsystem"].value
        when "Darwin"; "/private/tmp"
        when "SunOS"; "/var/tmp"
        else
          "/tmp"
            end


      @tmpdir = File.join(@tmpdir, "puppettesting#{Process.pid}")

      unless File.exists?(@tmpdir)
        FileUtils.mkdir_p(@tmpdir)
        File.chmod(01777, @tmpdir)
      end
    end
    @tmpdir
  end

  def remove_tmp_files
    @@tmpfiles.each { |file|
      unless file =~ /tmp/
        puts "Not deleting tmpfile #{file}"
        next
      end
      if FileTest.exists?(file)
        system("chmod -R 755 #{file}")
        system("rm -rf #{file}")
      end
    }
    @@tmpfiles.clear
  end

  def teardown
    #@stop = Time.now
    #File.open("/tmp/test_times.log", ::File::WRONLY|::File::CREAT|::File::APPEND) { |f| f.puts "%0.4f %s %s" % [@stop - @start, @method_name, self.class] }
    @@cleaners.each { |cleaner| cleaner.call }

    remove_tmp_files

    @@tmppids.each { |pid|
      %x{kill -INT #{pid} 2>/dev/null}
    }

    @@tmppids.clear

    Puppet::Util::Storage.clear
    Puppet.clear
    Puppet.settings.clear
    Puppet::Util::Cacher.expire

    @memoryatend = Puppet::Util.memory
    diff = @memoryatend - @memoryatstart

    Puppet.info "#{self.class}##{@method_name} memory growth (#{@memoryatstart} to #{@memoryatend}): #{diff}" if diff > 1000

    # reset all of the logs
    Puppet::Util::Log.close_all
    @logs.clear

    # Just in case there are processes waiting to die...
    require 'timeout'

    begin
      Timeout::timeout(5) do
        Process.waitall
      end
    rescue Timeout::Error
      # just move on
    end
  end

  def logstore
    @logs = []
    Puppet::Util::Log.newdestination(Puppet::Test::LogCollector.new(@logs))
  end
end

require 'puppettest/support'
require 'puppettest/filetesting'
require 'puppettest/fakes'
require 'puppettest/exetest'
require 'puppettest/parsertesting'
require 'puppettest/servertest'
require 'puppettest/testcase'