Class: Chef::Handler::Serverspec

Inherits:
Chef::Handler show all
Defined in:
lib/chef/handler/serverspec.rb

Overview

Serverspec handler

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Serverspec

Returns a new instance of Serverspec.



14
15
16
17
18
# File 'lib/chef/handler/serverspec.rb', line 14

def initialize(config = {})
  @config = Mash.new(config)
  @config[:output_dir] ||= '/tmp/serverspec'
  @config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/chef/handler/serverspec.rb', line 12

def config
  @config
end

Instance Method Details

#build_output_dir(name) ⇒ Object



37
38
39
40
41
42
# File 'lib/chef/handler/serverspec.rb', line 37

def build_output_dir(name)
  unless File.exist?(name)
    FileUtils.mkdir_p(name)
    File.chmod(00700, name)
  end
end

#reportObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/chef/handler/serverspec.rb', line 20

def report
  resource_tree.each do |cookbook, recipes|
    recipes.each do |recipe, resources|
      spec_dir = File.join(@config[:output_dir], cookbook)
      build_output_dir(spec_dir)

      File.open(File.join(spec_dir, "#{recipe}_spec.rb"), 'w') do |file|
        file.write("context 'recipe[#{cookbook}::#{recipe}]' do\n")
        resources.each do |r|
          file.write(r.to_serverspec)
        end
        file.write("end\n")
      end
    end
  end
end

#resource_treeObject



44
45
46
47
48
49
50
51
52
# File 'lib/chef/handler/serverspec.rb', line 44

def resource_tree
  resources = Hash.new { |h, k| h[k] = Hash.new { |h, k| h[k] = [] } }

  all_resources.each do |r|
    resources[r.cookbook_name || 'none'][r.recipe_name || 'none'] << r
  end

  resources
end