Class: Puppet::Application::Spec

Inherits:
Puppet::Application
  • Object
show all
Defined in:
lib/puppet/application/spec.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#reporterObject (readonly)

Returns the value of attribute reporter.



10
11
12
# File 'lib/puppet/application/spec.rb', line 10

def reporter
  @reporter
end

Instance Method Details

#catalog(path) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/puppet/application/spec.rb', line 54

def catalog(path)
  Puppet::Test::TestHelper.before_each_test
  Puppet[:code] = File.read(path)

  node = Puppet::Node.new("spec")
  modulepath = get_modulepath(node)
  link_module(modulepath)
  catalog = Puppet::Resource::Catalog.indirection.find(node.name, :use_node => node)
  catalog.to_ral

  Puppet::Test::TestHelper.after_each_test
  catalog
end

#evaluate_assertionsObject



27
28
29
30
31
32
33
# File 'lib/puppet/application/spec.rb', line 27

def evaluate_assertions
  if options[:manifest]
    process_spec(options[:manifest])
  else
    process_spec_directory(specdir)
  end
end

#get_modulepath(node) ⇒ Object

Given a node object, return the first modulepath



70
71
72
# File 'lib/puppet/application/spec.rb', line 70

def get_modulepath(node)
  node.environment.full_modulepath[0]
end

Ensure that a symlink is present pointing from the node’s env to the current directory



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/puppet/application/spec.rb', line 77

def link_module(modulepath)
  pwd = Dir.pwd
  name = File.basename(pwd)
  symlink = File.join(modulepath, name)

  # Ensure that the modulepath exists
  # within the temp environment
  FileUtils.mkdir_p(modulepath) unless Dir.exist?(modulepath)

  # Ensure that a symlink to the
  # cwd exists
  FileUtils.ln_s(pwd, symlink) unless File.symlink?(symlink)
end

#process_spec(path) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/puppet/application/spec.rb', line 39

def process_spec(path)
  catalog = catalog(path)

  assertions = catalog.resources.select {|res| res.type == 'Assertion' }
  assertions.each do |res|
    # Get the subject resource from the catalog rather than the
    # reference provided from the parser. The reference's resource
    # object does not contain any parameters for whatever reason.
    catalog_subject = catalog.resource(res[:subject].to_s)
    res[:subject] = catalog_subject if catalog_subject

    reporter << res.to_ral
  end
end

#process_spec_directory(specdir) ⇒ Object



35
36
37
# File 'lib/puppet/application/spec.rb', line 35

def process_spec_directory(specdir)
  Dir.glob("#{specdir}/**/*_spec.pp").map { |spec| process_spec(spec) }
end

#run_commandObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/puppet/application/spec.rb', line 12

def run_command
  @reporter = Puppet::Util::Assertion::Reporter.new

  begin
    Puppet::Test::TestHelper.initialize
    evaluate_assertions
    reporter.print_footer
  rescue Exception => e
    reporter.print_error(e)
  end

  exit 1 unless reporter.failed == 0
  exit 0
end

#specdirObject

Return the specdir under the CWD or raise an error if not found.



94
95
96
97
98
99
100
101
# File 'lib/puppet/application/spec.rb', line 94

def specdir
  pwd = Dir.pwd
  specdir = File.join(pwd, 'spec')
  unless Dir.exist?(specdir)
    raise 'No spec directory was found under the CWD. A spec manifest can be specified with the --manifest flag'
  end
  specdir
end