class ChefSpec::ZeroServer
Rather than create a ChefZero instance per test case, simply create one ChefZero instance and reset it for every test case.
Attributes
server[R]
Public Class Methods
new()
click to toggle source
Create the ChefZero Server
# File lib/chefspec/zero_server.rb, line 17 def initialize nuke! end
Public Instance Methods
load_data(name, key, data)
click to toggle source
Load (and track) data sent to the server
@param [String] name
the name or id of the item to load
@param [String, Symbol] key
the key to load
@param [Hash] data
the data for the object, which will be converted to JSON and uploaded to the server
# File lib/chefspec/zero_server.rb, line 95 def load_data(name, key, data) @data_loaded[key] ||= [] @data_loaded[key] << name @server.load_data({ key => { name => data } }) end
nuke!()
click to toggle source
Really reset everything and reload the configuration
# File lib/chefspec/zero_server.rb, line 52 def nuke! @server = ChefZero::Server.new( # Set the log level from RSpec, defaulting to warn log_level: RSpec.configuration.log_level || :warn, port: RSpec.configuration.server_runner_port, # Set the data store data_store: data_store(RSpec.configuration.server_runner_data_store) ) @cookbooks_uploaded = false @data_loaded = {} end
reset!()
click to toggle source
Remove all the data we just loaded from the ChefZero server
# File lib/chefspec/zero_server.rb, line 31 def reset! if RSpec.configuration.server_runner_clear_cookbooks @server.clear_data @cookbooks_uploaded = false else # If we don't want to do a full clear, iterate through each value that we # set and manually remove it. @data_loaded.each do |key, names| if key == "data" names.each { |n| @server.data_store.delete_dir(["organizations", "chef", key, n]) } else names.each { |n| @server.data_store.delete(["organizations", "chef", key, n]) } end end end @data_loaded = {} end
setup!()
click to toggle source
Start the ChefZero Server
# File lib/chefspec/zero_server.rb, line 24 def setup! @server.start_background unless @server.running? end
teardown!()
click to toggle source
Teardown the ChefZero Server
# File lib/chefspec/zero_server.rb, line 68 def teardown! @server.stop if @server.running? end
upload_cookbooks!()
click to toggle source
Upload the cookbooks to the Chef
Server
.
# File lib/chefspec/zero_server.rb, line 75 def upload_cookbooks! return if @cookbooks_uploaded loader = Chef::CookbookLoader.new(Chef::Config[:cookbook_path]) loader.load_cookbooks cookbook_uploader_for(loader).upload_cookbooks @cookbooks_uploaded = true end
Private Instance Methods
cookbook_uploader_for(loader)
click to toggle source
The uploader for the cookbooks.
@param [Chef::CookbookLoader] loader
the Chef cookbook loader
@return [Chef::CookbookUploader]
# File lib/chefspec/zero_server.rb, line 111 def cookbook_uploader_for(loader) Chef::CookbookUploader.new(loader.cookbooks) end
data_store(option)
click to toggle source
Generate the DataStore object to be passed in to the ChefZero::Server object
# File lib/chefspec/zero_server.rb, line 118 def data_store(option) require "chef_zero/data_store/default_facade" store = case option when :in_memory require "chef_zero/data_store/memory_store_v2" ChefZero::DataStore::MemoryStoreV2.new when :on_disk require "tmpdir" unless defined?(Dir.mktmpdir) require "chef_zero/data_store/raw_file_store" ChefZero::DataStore::RawFileStore.new(Dir.mktmpdir) else raise ArgumentError, ":#{option} is not a valid server_runner_data_store option. Please use either :in_memory or :on_disk." end ChefZero::DataStore::DefaultFacade.new(store, "chef", true) end