Skip to content

Instantly share code, notes, and snippets.

@qubitrenegade
Created September 13, 2017 17:57
Show Gist options
  • Save qubitrenegade/5c3dda97faf76db893dd3868b404483f to your computer and use it in GitHub Desktop.
Save qubitrenegade/5c3dda97faf76db893dd3868b404483f to your computer and use it in GitHub Desktop.
Testing an Ohai Plugin generated from Template using burtlo/chefspec-ohai to test
# recipes/default.rb
ohai_plugin 'consul' do
resource :template
variables config_var: node['my_cookbook']['some_important_var']
end
# templates/default/my_plugin.rb
Ohai.plugin :MyPlugin do
provides 'my_plugin/foo'
def some_config_var
'<%= @config_var %>'
end
collect_data :default do
my_plugin Mash.new
consul[:path] = some_config_var
end
end
# spec/unit/ohai_plugins/my_plugin_spec.rb
require 'spec_helper'
require 'tempfile'
require 'erb'
@config_var = 'Go see a Star War!'
temp_file = Tempfile.new('foo')
# temp_file = 'test/integration/default/ohai_plugins/my_plugin_rendered.rb'
template = ERB.new(File.read('templates/default/my_plugin.rb')).result
File.write(temp_file, template)
describe_ohai_plugin :MyPlugin do
let(:plugin_file) { temp_file }
# Do tests here as normal
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment