class ChefSpec::ZeroServer

ChefZero instance and reset it for every test case.
Rather than create a ChefZero instance per test case, simply create one

def cookbook_uploader_for(loader)

Returns:
  • (Chef::CookbookUploader) -

Parameters:
  • loader (Chef::CookbookLoader) --
def cookbook_uploader_for(loader)
  Chef::CookbookUploader.new(loader.cookbooks)
end

def data_store(option)


Generate the DataStore object to be passed in to the ChefZero::Server object
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

def initialize

Create the ChefZero Server
def initialize
  nuke!
end

def load_data(name, key, data)

Parameters:
  • data (Hash) --
  • key (String, Symbol) --
  • name (String) --
def load_data(name, key, data)
  @data_loaded[key] ||= []
  @data_loaded[key] << name
  @server.load_data({ key => { name => data } })
end

def nuke!


Really reset everything and reload the configuration
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

def reset!


Remove all the data we just loaded from the ChefZero server
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

def setup!


Start the ChefZero Server
def setup!
  @server.start_background unless @server.running?
end

def teardown!


Teardown the ChefZero Server
def teardown!
  @server.stop if @server.running?
end

def upload_cookbooks!


Upload the cookbooks to the Chef Server.
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