Class: Umami::Test::Integration

Inherits:
Umami::Test show all
Includes:
Helper::FileTools, Helper::InSpec
Defined in:
lib/chef-umami/test/integration.rb

Instance Attribute Summary collapse

Attributes inherited from Umami::Test

#root_dir

Instance Method Summary collapse

Methods included from Helper::FileTools

#enforce_styling, #write_file

Methods included from Helper::InSpec

#desciption, #test_chef_gem, #test_cron, #test_file, #test_gem_package, #test_group, #test_package, #test_user

Constructor Details

#initialize(root_dir) ⇒ Integration

Returns a new instance of Integration.



26
27
28
29
# File 'lib/chef-umami/test/integration.rb', line 26

def initialize(root_dir)
  super
  @test_root = File.join(self.root_dir, 'umami', 'integration')
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

If the test framework’s helper module doesn’t provide support for a given test-related method, return a friendly message. Raise NoMethodError for any other failed calls.



59
60
61
62
63
64
65
66
# File 'lib/chef-umami/test/integration.rb', line 59

def method_missing(meth, *args, &block)
  case meth
  when /^test_/
    "# #{meth} is not currently defined. Stay tuned for updates."
  else
    raise NoMethodError
  end
end

Instance Attribute Details

#test_rootObject (readonly)

Returns the value of attribute test_root.



25
26
27
# File 'lib/chef-umami/test/integration.rb', line 25

def test_root
  @test_root
end

Instance Method Details

#frameworkObject

InSpec doesn’t need a require statement to use its tests. We define #framework here for completeness.



33
34
35
# File 'lib/chef-umami/test/integration.rb', line 33

def framework
  'inspec'
end

#generate(recipe_resources = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/chef-umami/test/integration.rb', line 68

def generate(recipe_resources = {})
  test_files_written = []
  recipe_resources.each do |canonical_recipe, resources|
    (cookbook, recipe) = canonical_recipe.split('::')
    content = [preamble(cookbook, recipe)]
    resources.each do |resource|
      content << write_test(resource)
    end
    test_file_name = test_file_path(cookbook, recipe)
    test_file_content = content.join("\n") + "\n"
    write_file(test_file_name, test_file_content)
    test_files_written << test_file_name
  end

  enforce_styling(test_root)

  unless test_files_written.empty?
    puts 'Wrote the following integration tests:'
    test_files_written.each do |f|
      puts "\t#{f}"
    end
  end
end

#preamble(cookbook = '', recipe = '') ⇒ Object



41
42
43
# File 'lib/chef-umami/test/integration.rb', line 41

def preamble(cookbook = '', recipe = '')
  "# #{test_file_path(cookbook, recipe)} - Originally written by Umami!"
end

#test_file_path(cookbook = '', recipe = '') ⇒ Object



37
38
39
# File 'lib/chef-umami/test/integration.rb', line 37

def test_file_path(cookbook = '', recipe = '')
  "#{test_root}/#{cookbook}_#{recipe}_spec.rb"
end

#write_test(resource = nil) ⇒ Object

Call on the apprpriate method from the Umami::Helper::InSpec module to generate our test.



47
48
49
50
51
52
53
54
# File 'lib/chef-umami/test/integration.rb', line 47

def write_test(resource = nil)
  if resource.action.is_a? Array
    return if resource.action.include?(:delete)
  end
  return if resource.action == :delete

  "\n" + send("test_#{resource.declared_type}", resource)
end