Class: ChefSpec::ZeroServer
- Inherits:
-
Object
- Object
- ChefSpec::ZeroServer
- 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 Method Summary collapse
-
#initialize ⇒ ZeroServer
constructor
Create the ChefZero Server.
-
#load_data(name, key, data) ⇒ Object
Load (and track) data sent to the server.
-
#reset! ⇒ Object
Remove all the data we just loaded from the ChefZero server.
-
#server ⇒ Object
The URL for the ChefZero Server.
-
#setup! ⇒ Object
Start the ChefZero Server.
-
#teardown! ⇒ Object
Teardown the ChefZero Server.
-
#upload_cookbooks! ⇒ Object
Upload the cookbooks to the Chef Server.
Constructor Details
#initialize ⇒ ZeroServer
Create the ChefZero Server
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/chefspec/zero_server.rb', line 15 def initialize @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 |
Instance Method Details
#load_data(name, key, data) ⇒ Object
Load (and track) data sent to the server
92 93 94 95 96 |
# File 'lib/chefspec/zero_server.rb', line 92 def load_data(name, key, data) @data_loaded[key] ||= [] @data_loaded[key] << name @server.load_data({ key => { name => data } }) end |
#reset! ⇒ Object
Remove all the data we just loaded from the ChefZero server
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/chefspec/zero_server.rb', line 38 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 |
#server ⇒ Object
The URL for the ChefZero Server
77 78 79 |
# File 'lib/chefspec/zero_server.rb', line 77 def server @server end |
#setup! ⇒ Object
Start the ChefZero Server
31 32 33 |
# File 'lib/chefspec/zero_server.rb', line 31 def setup! @server.start_background unless @server.running? end |
#teardown! ⇒ Object
Teardown the ChefZero Server
59 60 61 |
# File 'lib/chefspec/zero_server.rb', line 59 def teardown! @server.stop if @server.running? end |
#upload_cookbooks! ⇒ Object
Upload the cookbooks to the Chef Server.
66 67 68 69 70 71 72 |
# File 'lib/chefspec/zero_server.rb', line 66 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 |