Class: RSpecPuppetSupport

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet-check/rspec_puppet_support.rb

Overview

class to prepare spec directory for rspec puppet testing

Class Method Summary collapse

Class Method Details

.dependency_setupObject

setup the module dependencies for rspec-puppet testing



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/puppet-check/rspec_puppet_support.rb', line 54

def self.dependency_setup
  require 'json'

  # parse the metadata.json (assumes DataParser.json has already given it a pass)
  parsed = JSON.parse(File.read('metadata.json'))

  # grab dependencies if they exist
  unless parsed['dependencies'].empty?
    parsed['dependencies'].each do |dependency_hash|
      # determine how the user wants to download the module dependency
      if dependency_hash.key?('git')
        git(dependency_hash['git'], dependency_hash['args'])
      elsif dependency_hash.key?('forge')
        forge(dependency_hash['forge'], dependency_hash['args'])
      elsif dependency_hash.key?('hg')
        hg(dependency_hash['hg'], dependency_hash['args'])
      else
        warn "#{dependency_hash['name']} has an unspecified, or specified but unsupported, download method."
      end
    end
  end
end

.file_setup(module_name) ⇒ Object

setup the files, directories, and symlinks for rspec-puppet testing



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/puppet-check/rspec_puppet_support.rb', line 31

def self.file_setup(module_name)
  require 'fileutils'

  # create all the necessary fixture dirs that are missing
  ['spec/fixtures', 'spec/fixtures/manifests', 'spec/fixtures/modules', "spec/fixtures/modules/#{module_name}"].each do |dir|
    FileUtils.mkdir(dir) unless File.directory?(dir)
  end

  # create empty site.pp if missing
  FileUtils.touch('spec/fixtures/manifests/site.pp') unless File.file?('spec/fixtures/manifests/site.pp')

  # symlink over everything the module needs for compilation
  %w(hiera.yaml data hieradata functions manifests lib files templates).each do |file|
    FileUtils.ln_s("../../../../#{file}", "spec/fixtures/modules/#{module_name}/#{file}") if File.exist?(file) && !File.exist?("spec/fixtures/modules/#{module_name}/#{file}")
  end

  # create spec_helper if missing
  unless File.file?('spec/spec_helper.rb')
    File.open('spec/spec_helper.rb', 'w') { |file| file.puts "require 'rspec-puppet/spec_helper'\n" }
  end
end

.forge(forge_name, args = '') ⇒ Object

download external module dependency with forge



83
84
85
# File 'lib/puppet-check/rspec_puppet_support.rb', line 83

def self.forge(forge_name, args = '')
  system("puppet module install --modulepath spec/fixtures/modules/ --force #{args} #{forge_name}")
end

.git(git_url, args = '') ⇒ Object

download external module dependency with git



78
79
80
# File 'lib/puppet-check/rspec_puppet_support.rb', line 78

def self.git(git_url, args = '')
  system("git -C spec/fixtures/modules/ clone #{args} #{git_url}")
end

.hg(hg_url, args = '') ⇒ Object

download external module dependency with hg



88
89
90
# File 'lib/puppet-check/rspec_puppet_support.rb', line 88

def self.hg(hg_url, args = '')
  system("hg --cwd spec/fixtures/modules/ clone #{args} #{hg_url}")
end

.runObject

prepare the spec fixtures directory for rspec-puppet testing



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/puppet-check/rspec_puppet_support.rb', line 9

def self.run
  # ensure this method does not do anything inside module dependencies
  specdirs = Dir.glob('**/spec').reject { |dir| dir =~ /fixtures/ }
  return if specdirs.class.to_s == 'NilClass'

  # setup fixtures for rspec-puppet testing
  specdirs.each do |specdir|
    # skip to next specdir if it does not seem like a puppet module
    next unless File.directory?(specdir + '/../manifests')

    # move up to module directory
    Dir.chdir(specdir + '/..')

    # grab the module name from the directory name of the module to pass to file_setup
    file_setup(File.basename(Dir.pwd))

    # invoke dependency_setup for module dependencies if metadata.json present
    dependency_setup if File.file?('metadata.json')
  end
end