Module: PuppetlabsSpecHelper::RakeTasks

Defined in:
lib/puppetlabs_spec_helper/rake_tasks.rb

Instance Method Summary collapse

Instance Method Details



127
128
129
# File 'lib/puppetlabs_spec_helper/rake_tasks.rb', line 127

def auto_symlink
  { File.basename(Dir.pwd).split('-').last => '#{source_dir}' }
end

#beaker_node_setsArray<String>

get the array of Beaker set names

Returns:

  • (Array<String>)


95
96
97
98
99
100
101
# File 'lib/puppetlabs_spec_helper/rake_tasks.rb', line 95

def beaker_node_sets
  return @beaker_nodes if @beaker_nodes
  @beaker_nodes = Dir['spec/acceptance/nodesets/*.yml'].sort.map do |node_set|
    node_set.slice!('.yml')
    File.basename(node_set)
  end
end


303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/puppetlabs_spec_helper/rake_tasks.rb', line 303

def check_directory_for_symlinks(dir='.')
  dir = Pathname.new(dir) unless dir.is_a?(Pathname)
  results = []

  dir.each_child(true) do |child|
    if child.symlink?
      results << child
    elsif child.directory? && child.basename.to_s != '.git'
      results.concat(check_directory_for_symlinks(child))
    end
  end

  results
end

#clone_repo(scm, remote, target, subdir = nil, ref = nil, branch = nil, flags = nil) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/puppetlabs_spec_helper/rake_tasks.rb', line 204

def clone_repo(scm, remote, target, subdir=nil, ref=nil, branch=nil, flags = nil)
  args = []
  case scm
  when 'hg'
    args.push('clone')
    args.push('-b', branch) if branch
    args.push(flags) if flags
    args.push(remote, target)
  when 'git'
    args.push('clone')
    args.push('--depth 1') unless ref
    args.push('-b', branch) if branch
    args.push(flags) if flags
    args.push(remote, target)
  else
    fail "Unfortunately #{scm} is not supported yet"
  end
  result = system("#{scm} #{args.flatten.join ' '}")
  unless File::exists?(target)
    fail "Failed to clone #{scm} repository #{remote} into #{target}"
  end
  result
end

#current_thread_count(items) ⇒ Object

returns the current thread count that is currently active a status of false or nil means the thread completed so when anything else we count that as a active thread



276
277
278
279
280
281
282
283
284
285
286
# File 'lib/puppetlabs_spec_helper/rake_tasks.rb', line 276

def current_thread_count(items)
  active_threads = items.find_all do |item, opts|
    if opts[:thread]
      opts[:thread].status
    else
      false
    end
  end
  logger.debug "Current thread count #{active_threads.count}"
  active_threads.count
end

#fixtures(category) ⇒ Object



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
# File 'lib/puppetlabs_spec_helper/rake_tasks.rb', line 131

def fixtures(category)
  if ENV['FIXTURES_YML']
    fixtures_yaml = ENV['FIXTURES_YML']
  elsif File.exists?('.fixtures.yml')
    fixtures_yaml = '.fixtures.yml'
  elsif File.exists?('.fixtures.yaml')
    fixtures_yaml = '.fixtures.yaml'
  else
    fixtures_yaml = false
  end

  begin
    if fixtures_yaml
      fixtures = YAML.load_file(fixtures_yaml) || { 'fixtures' => {} }
    else
      fixtures = { 'fixtures' => {} }
    end
  rescue Errno::ENOENT
    fail("Fixtures file not found: '#{fixtures_yaml}'")
  rescue Psych::SyntaxError => e
    fail("Found malformed YAML in '#{fixtures_yaml}' on line #{e.line} column #{e.column}: #{e.problem}")
  end

  unless fixtures.include?('fixtures')
    # File is non-empty, but does not specify fixtures
    fail("No 'fixtures' entries found in '#{fixtures_yaml}'; required")
  end

  if fixtures.include? 'defaults'
    fixture_defaults = fixtures['defaults']
  else
    fixture_defaults = {}
  end

  fixtures = fixtures['fixtures']

  if fixtures['symlinks'].nil?
    fixtures['symlinks'] = auto_symlink
  end

  result = {}
  if fixtures.include? category and fixtures[category] != nil

    defaults = { "target" => "spec/fixtures/modules" }

    # load defaults from the `.fixtures.yml` `defaults` section
    # for the requested category and merge them into my defaults
    if fixture_defaults.include? category
      defaults = defaults.merge(fixture_defaults[category])
    end

    fixtures[category].each do |fixture, opts|
      # convert a simple string fixture to a hash, by
      # using the string fixture as the `repo` option of the hash.
      if opts.instance_of?(String)
        opts = { "repo" => opts }
      end
      # there should be a warning or something if it's not a hash...
      if opts.instance_of?(Hash)
        # merge our options into the defaults to get the
        # final option list
        opts = defaults.merge(opts)

        real_target = eval('"'+opts["target"]+'"')
        real_source = eval('"'+opts["repo"]+'"')

        result[real_source] = { "target" => File.join(real_target,fixture), "ref" => opts["ref"], "branch" => opts["branch"], "scm" => opts["scm"], "flags" => opts["flags"], "subdir" => opts["subdir"]}
      end
    end
  end
  return result
end

#loggerObject

creates a logger so we can log events with certain levels



252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/puppetlabs_spec_helper/rake_tasks.rb', line 252

def logger
  unless @logger
    require 'logger'
    if ENV['ENABLE_LOGGER']
       level = Logger::DEBUG
     else
       level = Logger::INFO
    end
    @logger = Logger.new(STDERR)
    @logger.level = level
  end
  @logger
end

#max_thread_limitObject

returns the max_thread_count because we may want to limit ssh or https connections



290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/puppetlabs_spec_helper/rake_tasks.rb', line 290

def max_thread_limit
  unless @max_thread_limit
    # the default thread count is 10 but can be
    # raised by using environment variable MAX_FIXTURE_THREAD_COUNT
    if ENV['MAX_FIXTURE_THREAD_COUNT'].to_i > 0
      @max_thread_limit = ENV['MAX_FIXTURE_THREAD_COUNT'].to_i
    else
      @max_thread_limit = 10 # the default
    end
  end
  @max_thread_limit
end

#module_working_directoryObject



266
267
268
269
270
271
# File 'lib/puppetlabs_spec_helper/rake_tasks.rb', line 266

def module_working_directory
  # The problem with the relative path is that PMT doesn't expand the path properly and so passing in a relative path here
  # becomes something like C:\somewhere\backslashes/spec/fixtures/work-dir on Windows, and then PMT barfs itself.
  # This has been reported as https://tickets.puppetlabs.com/browse/PUP-4884
  File.expand_path(ENV['MODULE_WORKING_DIR'] ? ENV['MODULE_WORKING_DIR'] : 'spec/fixtures/work-dir')
end

#remove_subdirectory(target, subdir) ⇒ Object



241
242
243
244
245
246
247
248
249
# File 'lib/puppetlabs_spec_helper/rake_tasks.rb', line 241

def remove_subdirectory(target, subdir)
  unless subdir.nil?
    Dir.mktmpdir {|tmpdir|
       FileUtils.mv(Dir.glob("#{target}/#{subdir}/{.[^\.]*,*}"), tmpdir)
       FileUtils.rm_rf("#{target}/#{subdir}")
       FileUtils.mv(Dir.glob("#{tmpdir}/{.[^\.]*,*}"), "#{target}")
    }
  end
end

#repositoriesObject

cache the repositories and return a hash object



86
87
88
89
90
91
# File 'lib/puppetlabs_spec_helper/rake_tasks.rb', line 86

def repositories
  unless @repositories
    @repositories = fixtures('repositories')
  end
  @repositories
end

#revision(scm, target, ref) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/puppetlabs_spec_helper/rake_tasks.rb', line 228

def revision(scm, target, ref)
  args = []
  case scm
  when 'hg'
    args.push('update', '--clean', '-r', ref)
  when 'git'
    args.push('reset', '--hard', ref)
  else
    fail "Unfortunately #{scm} is not supported yet"
  end
  system("cd #{target} && #{scm} #{args.flatten.join ' '}")
end

#source_dirObject

This is a helper for the self-symlink entry of fixtures.yml



81
82
83
# File 'lib/puppetlabs_spec_helper/rake_tasks.rb', line 81

def source_dir
  Dir.pwd
end

#vagrant_ssh(set, node = nil) ⇒ Object

Use “vagrant ssh” to login to the given node in the node set

Parameters:

  • set (String)

    The name of the node set (yml file)

  • node (String) (defaults to: nil)

    The name of the node in the set. For multi-node sets.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/puppetlabs_spec_helper/rake_tasks.rb', line 106

def vagrant_ssh(set, node = nil)
  vagrant_yml_dir = File.join '.vagrant', 'beaker_vagrant_files', "#{set}.yml"
  vagrant_file = File.join vagrant_yml_dir, 'Vagrantfile'
  unless File.file? vagrant_file
    puts "There is no Vagrantfile at: '#{vagrant_file}'. Perhaps, the node is not created or is destroyed."
    exit 1
  end
  Dir.chdir(vagrant_yml_dir) do
    command = 'vagrant ssh'
    command += " #{node}" if node
    # Vagrant is not distributed as a normal gem
    # and we should protect it from the current Ruby environment
    env = {
        'RUBYLIB' => nil,
        'GEM_PATH' => nil,
        'BUNDLE_BIN_PATH' => nil,
    }
    system env, command
  end
end