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



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

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
  return if 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

.file_setup(module_name) ⇒ Object

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



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

def self.file_setup(module_name)
  # 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|
    Dir.mkdir(dir) unless File.directory?(dir)
  end

  # create empty site.pp if missing
  File.write('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|
    File.symlink("../../../../#{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
  return if File.file?('spec/spec_helper.rb')
  File.open('spec/spec_helper.rb', 'w') { |file| file.puts "require 'rspec-puppet/spec_helper'\n" }
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



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

def self.git(git_url, args = '')
  # establish path to clone module to
  path = "spec/fixtures/modules/#{File.basename(git_url, '.git')}"
  # is the module present and already cloned with git? do a pull; otherwise, do a clone
  File.directory?("#{path}/.git") ? system("git -C #{path} pull") : system("git clone #{args} #{git_url} #{path}")
end

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

download external module dependency with hg



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

def self.hg(hg_url, args = '')
  # establish path to clone module to
  path = "spec/fixtures/modules/#{File.basename(hg_url)}"
  # is the module present and already cloned with hg? do a pull and update; otherwise do a clone
  File.directory?("#{path}/.hg") ? system("hg --cwd #{path} pull; hg --cwd #{path} update") : system("hg clone #{args} #{hg_url} #{path}")
end

.runObject

prepare the spec fixtures directory for rspec-puppet testing



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

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')

    # change 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