Class: RSpec::Puppet::Coverage

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Singleton
Defined in:
lib/rspec-puppet/coverage.rb

Defined Under Namespace

Classes: ResourceWrapper

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCoverage

Returns a new instance of Coverage.



14
15
16
17
# File 'lib/rspec-puppet/coverage.rb', line 14

def initialize
  @collection = {}
  @filters = ['Stage[main]', 'Class[Settings]', 'Class[main]']
end

Instance Attribute Details

#filtersObject

Returns the value of attribute filters.



4
5
6
# File 'lib/rspec-puppet/coverage.rb', line 4

def filters
  @filters
end

Instance Method Details

#add(resource) ⇒ Object



19
20
21
22
23
# File 'lib/rspec-puppet/coverage.rb', line 19

def add(resource)
  if !exists?(resource) && !filtered?(resource)
    @collection[resource.to_s] = ResourceWrapper.new(resource)
  end
end

#add_filter(type, title) ⇒ Object



25
26
27
# File 'lib/rspec-puppet/coverage.rb', line 25

def add_filter(type, title)
  @filters << "#{type.capitalize}[#{title.capitalize}]"
end

#add_from_catalog(catalog, test_module) ⇒ Object

add all resources from catalog declared in module test_module



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
# File 'lib/rspec-puppet/coverage.rb', line 30

def add_from_catalog(catalog, test_module)
  catalog.to_a.each do |resource|
    # check filters
    next if @filters.include?(resource.to_s)
    if resource.type == 'Class'
      # if the resource is a class, make sure the class belongs to
      # module test_module
      module_name = resource.title.split('::').first.downcase
      next if module_name != test_module
    elsif resource.file
      # otherwise, the source file should be available, so make
      # sure the manifest declaring the resource is in
      # test_module's directory tree or the site manifest(s)
      if Puppet.version.to_f >= 4.0
        paths = [
          (Pathname.new(Puppet[:environmentpath]) + 'fixtures' + test_module + 'manifests').to_s,
          (Pathname.new(Puppet[:environmentpath]) + 'fixtures' + 'manifests' + 'site.pp').to_s
        ]
      else
        paths = Puppet[:modulepath].split(File::PATH_SEPARATOR).map do |dir|
          (Pathname.new(dir) + test_module + 'manifests').to_s
        end
        paths << Puppet[:manifest]
      end
      next unless paths.any? { |path| resource.file.include?(path) }
    end
    add(resource)
  end
end

#cover!(resource) ⇒ Object



64
65
66
67
68
# File 'lib/rspec-puppet/coverage.rb', line 64

def cover!(resource)
  if !filtered?(resource) && (wrapper = find(resource))
    wrapper.touch!
  end
end

#filtered?(resource) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/rspec-puppet/coverage.rb', line 60

def filtered?(resource)
  filters.include?(resource.to_s)
end

#report!Object



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
# File 'lib/rspec-puppet/coverage.rb', line 70

def report!
  report = {}

  report[:total] = @collection.size
  report[:touched] = @collection.count { |_, resource| resource.touched? }
  report[:untouched] = report[:total] - report[:touched]
  report[:coverage] = sprintf("%5.2f", ((report[:touched].to_f/report[:total].to_f)*100))

  report[:detailed] = Hash[*@collection.map do |name, wrapper|
    [name, wrapper.to_hash]
  end.flatten]

  puts "\n    Total resources:   \#{report[:total]}\n    Touched resources: \#{report[:touched]}\n    Resource coverage: \#{report[:coverage]}%\n  EOH\n\n  if report[:coverage] != \"100.00\"\n    puts <<-EOH.gsub(/^ {10}/, '')\n      Untouched resources:\n\n      \#{\n        untouched_resources = report[:detailed].reject do |_,rsrc|\n          rsrc[\"touched\"]\n        end\n        untouched_resources.inject([]) do |memo, (name,_)|\n          memo << \"  \#{name}\"\n        end.sort.join(\"\\n\")\n      }\n    EOH\n  end\n\nend\n".gsub(/^ {8}/, '')