Class: ChefSpec::ZeroServer

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Singleton
Defined in:
lib/chefspec/zero_server.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeZeroServer

Create the ChefZero Server


17
18
19
# File 'lib/chefspec/zero_server.rb', line 17

def initialize
  nuke!
end

Instance Attribute Details

#serverObject (readonly)

Returns the value of attribute server


14
15
16
# File 'lib/chefspec/zero_server.rb', line 14

def server
  @server
end

Instance Method Details

#load_data(name, key, data) ⇒ Object

Load (and track) data sent to the server


94
95
96
97
98
# File 'lib/chefspec/zero_server.rb', line 94

def load_data(name, key, data)
  @data_loaded[key] ||= []
  @data_loaded[key] << name
  @server.load_data({ key => { name => data } })
end

#nuke!Object

Really reset everything and reload the configuration


52
53
54
55
56
57
58
59
60
61
62
63
# 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!Object

Remove all the data we just loaded from the ChefZero server


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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!Object

Start the ChefZero Server


24
25
26
# File 'lib/chefspec/zero_server.rb', line 24

def setup!
  @server.start_background unless @server.running?
end

#teardown!Object

Teardown the ChefZero Server


68
69
70
# File 'lib/chefspec/zero_server.rb', line 68

def teardown!
  @server.stop if @server.running?
end

#upload_cookbooks!Object

Upload the cookbooks to the Chef Server.


75
76
77
78
79
80
81
# 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