diff options
author | jkeiser <jkeiser@opscode.com> | 2012-12-31 11:28:26 -0800 |
---|---|---|
committer | jkeiser <jkeiser@opscode.com> | 2012-12-31 11:28:26 -0800 |
commit | a29fc6d3c23aa5a48f3ad9f3fee35085cf36d610 (patch) | |
tree | e21df45639f237bdc602c2765b5d4dfa025f647d | |
parent | 765cd4ac6800769d147593825d16ae4297cfdb97 (diff) | |
download | chef-zero-a29fc6d3c23aa5a48f3ad9f3fee35085cf36d610.tar.gz |
Make rspec api only start up server once
-rw-r--r-- | lib/chef_zero/rspec.rb | 74 | ||||
-rw-r--r-- | lib/chef_zero/server.rb | 40 |
2 files changed, 67 insertions, 47 deletions
diff --git a/lib/chef_zero/rspec.rb b/lib/chef_zero/rspec.rb index 11b2e73..95e8325 100644 --- a/lib/chef_zero/rspec.rb +++ b/lib/chef_zero/rspec.rb @@ -5,62 +5,78 @@ require 'chef/config' module ChefZero module RSpec + def self.server + @server + end + def self.server=(value) + @server = value + end + def self.client_key + @client_key + end + def self.client_key=(value) + @client_key = value + end + def when_the_chef_server(description, &block) context "When the Chef server #{description}" do before :each do - raise "Attempt to create multiple servers in one test" if @server - # Set up configuration so that clients will point to the server - Thin::Logging.silent = true - @chef_zero_server = ChefZero::Server.new(:port => 8889) - Chef::Config.chef_server_url = @chef_zero_server.url - Chef::Config.node_name = 'admin' - @chef_zero_client_key = Tempfile.new(['chef_zero_client_key', '.pem']) - @chef_zero_client_key.write(ChefZero::PRIVATE_KEY) - @chef_zero_client_key.close - Chef::Config.client_key = @chef_zero_client_key + unless ChefZero::RSpec.server + # Set up configuration so that clients will point to the server + Thin::Logging.silent = true + ChefZero::RSpec.server = ChefZero::Server.new(:port => 8889) + ChefZero::RSpec.client_key = Tempfile.new(['chef_zero_client_key', '.pem']) + ChefZero::RSpec.client_key.write(ChefZero::PRIVATE_KEY) + ChefZero::RSpec.client_key.close + # Start the server + ChefZero::RSpec.server.start_background + else + ChefZero::RSpec.server.clear_data + end - # Start the server - @chef_zero_server.start_background + Chef::Config.chef_server_url = ChefZero::RSpec.server.url + Chef::Config.node_name = 'admin' + Chef::Config.client_key = ChefZero::RSpec.client_key end def self.client(name, client) - before(:each) { @chef_zero_server.load_data({ 'clients' => { name => client }}) } + before(:each) { ChefZero::RSpec.server.load_data({ 'clients' => { name => client }}) } end def self.cookbook(name, version, cookbook) - before(:each) { @chef_zero_server.load_data({ 'cookbooks' => { "#{name}-#{version}" => cookbook }}) } + before(:each) { ChefZero::RSpec.server.load_data({ 'cookbooks' => { "#{name}-#{version}" => cookbook }}) } end def self.data_bag(name, data_bag) - before(:each) { @chef_zero_server.load_data({ 'data' => { name => data_bag }}) } + before(:each) { ChefZero::RSpec.server.load_data({ 'data' => { name => data_bag }}) } end def self.environment(name, environment) - before(:each) { @chef_zero_server.load_data({ 'environments' => { name => environment }}) } + before(:each) { ChefZero::RSpec.server.load_data({ 'environments' => { name => environment }}) } end def self.node(name, node) - before(:each) { @chef_zero_server.load_data({ 'nodes' => { name => node }}) } + before(:each) { ChefZero::RSpec.server.load_data({ 'nodes' => { name => node }}) } end def self.role(name, role) - before(:each) { @chef_zero_server.load_data({ 'roles' => { name => role }}) } + before(:each) { ChefZero::RSpec.server.load_data({ 'roles' => { name => role }}) } end def self.user(name, user) - before(:each) { @chef_zero_server.load_data({ 'users' => { name => user }}) } + before(:each) { ChefZero::RSpec.server.load_data({ 'users' => { name => user }}) } end - after :each do - if @chef_zero_server - @chef_zero_server.stop - @chef_zero_server = nil - end - if @chef_zero_client_key - @chef_zero_client_key.unlink - @chef_zero_client_key = nil - end - end +# after :each do +# if @@ChefZero::RSpec.server +# @@ChefZero::RSpec.server.stop +# @@ChefZero::RSpec.server = nil +# end +# if @@ChefZero::RSpec.client_key +# @@ChefZero::RSpec.client_key.unlink +# @@ChefZero::RSpec.client_key = nil +# end +# end instance_eval(&block) end diff --git a/lib/chef_zero/server.rb b/lib/chef_zero/server.rb index 42774e8..70a552c 100644 --- a/lib/chef_zero/server.rb +++ b/lib/chef_zero/server.rb @@ -61,24 +61,7 @@ module ChefZero options[:generate_real_keys] = true if !options.has_key?(:generate_real_keys) @server = Thin::Server.new(options[:host], options[:port], make_app) @generate_real_keys = options[:generate_real_keys] - @data = { - 'clients' => { - 'chef-validator' => '{ "validator": true }', - 'chef-webui' => '{ "admin": true }' - }, - 'cookbooks' => {}, - 'data' => {}, - 'environments' => { - '_default' => '{ "description": "The default Chef environment" }' - }, - 'file_store' => {}, - 'nodes' => {}, - 'roles' => {}, - 'sandboxes' => {}, - 'users' => { - 'admin' => '{ "admin": true }' - } - } + clear_data end attr_reader :server @@ -196,6 +179,27 @@ module ChefZero end end + def clear_data + @data = { + 'clients' => { + 'chef-validator' => '{ "validator": true }', + 'chef-webui' => '{ "admin": true }' + }, + 'cookbooks' => {}, + 'data' => {}, + 'environments' => { + '_default' => '{ "description": "The default Chef environment" }' + }, + 'file_store' => {}, + 'nodes' => {}, + 'roles' => {}, + 'sandboxes' => {}, + 'users' => { + 'admin' => '{ "admin": true }' + } + } + end + private def make_app |