chef-zero 4.1.0 → 4.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 960c8c81923f89a55fd64b0de21517f5c2fde8e5
4
- data.tar.gz: ace9ada169514cdf654d7f359131c2103367da2d
3
+ metadata.gz: d33b522041ae43c415622aa4256a1fdb6e1c12dd
4
+ data.tar.gz: b9486a52dd93f3ee4e2a27c5195410b61acc486b
5
5
  SHA512:
6
- metadata.gz: 07af5a8a644ac4476ae551d4512e51ca5ba3a069bce3dae3afcbbf4299c9ef8b593113acbe15699da3fa7a991a7c0689606099642ff7c3773386bd279a024f86
7
- data.tar.gz: 92f6a384a72be04439eeb29f1046217a835b26741336dfad4a6d3dd6e1cb88e8860f234d55d2e2daca36a6a822f2792d89d4a267666412d9ce18fff17ee53a16
6
+ metadata.gz: 7da539d3289e42b8abec5960a9fbb30495410a42f820ae558b9b289e6bfeeae5a037b679d3f7d059482be7d48a45b08bd5032b038488ffeb6461ac3826daac02
7
+ data.tar.gz: 9d108127793484ae21fb50d87143486ad484ee9035c57391474467be9d3c624ddd23c51ab5a2f8963d8733e376efda52be8072f90e86517fdf964dbf77096d5d
@@ -4,71 +4,81 @@ require 'chef_zero/rest_request'
4
4
 
5
5
  module ChefZero
6
6
  module RSpec
7
- def self.server
8
- @server
9
- end
10
- def self.server=(value)
11
- @server = value
12
- end
13
- def self.client_key
14
- @client_key
15
- end
16
- def self.client_key=(value)
17
- @client_key = value
18
- end
19
- def self.request_log
20
- @request_log ||= []
21
- end
22
- def self.clear_request_log
23
- @request_log = []
7
+ module RSpecClassMethods
8
+ attr_accessor :server
9
+ attr_accessor :client_key
10
+ attr_reader :request_log
11
+
12
+ def clear_request_log
13
+ @request_log = []
14
+ end
15
+
16
+ def set_server_options(chef_server_options)
17
+ if server && chef_server_options != server.options
18
+ server.stop
19
+ self.server = nil
20
+ end
21
+
22
+ unless server
23
+ # TODO: can this be logged easily?
24
+ # pp :zero_opts => chef_server_options
25
+
26
+ # Set up configuration so that clients will point to the server
27
+ self.server = ChefZero::Server.new(chef_server_options)
28
+ self.client_key = Tempfile.new(['chef_zero_client_key', '.pem'])
29
+ client_key.write(ChefZero::PRIVATE_KEY)
30
+ client_key.close
31
+ # Start the server
32
+ server.start_background
33
+ server.on_response do |request, response|
34
+ request_log << [ request, response ]
35
+ end
36
+ else
37
+ server.clear_data
38
+ end
39
+ clear_request_log
40
+ end
24
41
  end
42
+ extend RSpecClassMethods
25
43
 
26
44
  def when_the_chef_server(description, *tags, &block)
27
- if tags.last.is_a?(Hash)
28
- opts = tags.last
29
- else
30
- opts = {}
31
- end
32
45
  context "When the Chef server #{description}", *tags do
33
- before :each do
46
+ extend WhenTheChefServerClassMethods
47
+ include WhenTheChefServerInstanceMethods
34
48
 
35
- default_opts = {:port => 8900, :signals => false, :log_requests => true}
36
- server_opts = if self.respond_to?(:chef_zero_opts)
37
- default_opts.merge(chef_zero_opts)
38
- else
39
- default_opts
40
- end
41
- server_opts = server_opts.merge(opts)
49
+ def self.chef_server_options
50
+ @@chef_server_options
51
+ end
52
+ def chef_server_options
53
+ self.class.chef_server_options
54
+ end
42
55
 
43
- if ChefZero::RSpec.server && server_opts != ChefZero::RSpec.server.options
44
- ChefZero::RSpec.server.stop
45
- ChefZero::RSpec.server = nil
46
- end
56
+ @@chef_server_options = { port: 8900, signals: false, log_requests: true, server_scope: :each }
57
+ chef_server_options.merge!(chef_zero_opts) if self.respond_to?(:chef_zero_opts)
58
+ chef_server_options.merge!(tags.last) if tags.last.is_a?(Hash)
47
59
 
48
- unless ChefZero::RSpec.server
49
- # TODO: can this be logged easily?
50
- # pp :zero_opts => server_opts
51
-
52
- # Set up configuration so that clients will point to the server
53
- ChefZero::RSpec.server = ChefZero::Server.new(server_opts)
54
- ChefZero::RSpec.client_key = Tempfile.new(['chef_zero_client_key', '.pem'])
55
- ChefZero::RSpec.client_key.write(ChefZero::PRIVATE_KEY)
56
- ChefZero::RSpec.client_key.close
57
- # Start the server
58
- ChefZero::RSpec.server.start_background
59
- ChefZero::RSpec.server.on_response do |request, response|
60
- ChefZero::RSpec.request_log << [ request, response ]
61
- end
62
- else
63
- ChefZero::RSpec.server.clear_data
60
+ Log.debug("Starting Chef server with options #{chef_server_options}")
61
+
62
+ old_chef_server_url = nil
63
+ old_node_name = nil
64
+ old_client_key = nil
65
+
66
+ before chef_server_options[:server_scope] do
67
+ ChefZero::RSpec.set_server_options(chef_server_options)
68
+
69
+ if chef_server_options[:organization]
70
+ organization chef_server_options[:organization]
64
71
  end
65
- ChefZero::RSpec.clear_request_log
66
72
 
67
73
  if defined?(Chef::Config)
68
- @old_chef_server_url = Chef::Config.chef_server_url
69
- @old_node_name = Chef::Config.node_name
70
- @old_client_key = Chef::Config.client_key
71
- Chef::Config.chef_server_url = ChefZero::RSpec.server.url
74
+ old_chef_server_url = Chef::Config.chef_server_url
75
+ old_node_name = Chef::Config.node_name
76
+ old_client_key = Chef::Config.client_key
77
+ if chef_server_options[:organization]
78
+ Chef::Config.chef_server_url = "#{ChefZero::RSpec.server.url}/organizations/#{chef_server_options[:organization]}"
79
+ else
80
+ Chef::Config.chef_server_url = ChefZero::RSpec.server.url
81
+ end
72
82
  Chef::Config.node_name = 'admin'
73
83
  Chef::Config.client_key = ChefZero::RSpec.client_key.path
74
84
  Chef::Config.http_retry_count = 0
@@ -76,224 +86,217 @@ module ChefZero
76
86
  end
77
87
 
78
88
  if defined?(Chef::Config)
79
- after :each do
80
- Chef::Config.chef_server_url = @old_chef_server_url
81
- Chef::Config.node_name = @old_node_name
82
- Chef::Config.client_key = @old_client_key
89
+ after chef_server_options[:server_scope] do
90
+ Chef::Config.chef_server_url = old_chef_server_url
91
+ Chef::Config.node_name = old_node_name
92
+ Chef::Config.client_key = old_client_key
83
93
  end
84
94
  end
85
95
 
86
- def self.organization(name, org = '{}', &block)
87
- before(:each) { organization(name, org, &block) }
88
- end
96
+ instance_eval(&block)
97
+ end
98
+ end
89
99
 
90
- def organization(name, org = '{}', &block)
91
- ChefZero::RSpec.server.data_store.set([ 'organizations', name, 'org' ], dejsonize(org), :create_dir, :create)
92
- prev_org_name = @current_org
93
- @current_org = name
94
- prev_object_path = @current_object_path
95
- @current_object_path = "organizations/#{name}"
96
- if block_given?
97
- begin
98
- instance_eval(&block)
99
- ensure
100
- @current_org = prev_org_name
101
- @current_object_path = prev_object_path
102
- end
103
- end
104
- end
100
+ module WhenTheChefServerClassMethods
101
+ def organization(name, org = '{}', &block)
102
+ before(chef_server_options[:server_scope]) { organization(name, org, &block) }
103
+ end
105
104
 
106
- def self.acl_for(path, data)
107
- before(:each) { acl_for(path, data) }
108
- end
105
+ def acl_for(path, data)
106
+ before(chef_server_options[:server_scope]) { acl_for(path, data) }
107
+ end
109
108
 
110
- def acl_for(path, data)
111
- ChefZero::RSpec.server.load_data({ 'acls' => { path => data } }, current_org)
112
- end
109
+ def client(name, data, &block)
110
+ before(chef_server_options[:server_scope]) { client(name, data, &block) }
111
+ end
113
112
 
114
- def acl(data)
115
- acl_for(@current_object_path, data)
116
- end
113
+ def container(name, data, &block)
114
+ before(chef_server_options[:server_scope]) { container(name, data, &block) }
115
+ end
117
116
 
118
- def self.client(name, data, &block)
119
- before(:each) { client(name, data, &block) }
117
+ def cookbook(name, version, data = {}, options = {}, &block)
118
+ before(chef_server_options[:server_scope]) do
119
+ cookbook(name, version, data, &block)
120
120
  end
121
+ end
121
122
 
122
- def client(name, data, &block)
123
- with_object_path("clients/#{name}") do
124
- ChefZero::RSpec.server.load_data({ 'clients' => { name => data } }, current_org)
125
- instance_eval(&block) if block_given?
126
- end
127
- end
123
+ def data_bag(name, data, &block)
124
+ before(chef_server_options[:server_scope]) { data_bag(name, data, &block) }
125
+ end
128
126
 
129
- def self.container(name, data, &block)
130
- before(:each) { container(name, data, &block) }
131
- end
127
+ def environment(name, data, &block)
128
+ before(chef_server_options[:server_scope]) { environment(name, data, &block) }
129
+ end
132
130
 
133
- def container(name, data, &block)
134
- with_object_path("containers/#{name}") do
135
- ChefZero::RSpec.server.load_data({ 'containers' => { name => data } }, current_org)
136
- instance_eval(&block) if block_given?
137
- end
138
- end
131
+ def group(name, data, &block)
132
+ before(chef_server_options[:server_scope]) { group(name, data, &block) }
133
+ end
139
134
 
140
- def self.cookbook(name, version, data = {}, options = {}, &block)
141
- before(:each) do
142
- cookbook(name, version, data, &block)
143
- end
144
- end
135
+ def node(name, data, &block)
136
+ before(chef_server_options[:server_scope]) { node(name, data, &block) }
137
+ end
145
138
 
146
- def cookbook(name, version, data = {}, options = {}, &block)
147
- with_object_path("cookbooks/#{name}") do
148
- if data.has_key?('metadata.rb')
149
- if data['metadata.rb'].nil?
150
- data.delete('metadata.rb')
151
- end
152
- else
153
- data['metadata.rb'] = "name #{name.inspect}; version #{version.inspect}"
154
- end
155
- ChefZero::RSpec.server.load_data({ 'cookbooks' => { "#{name}-#{version}" => data.merge(options) }}, current_org)
156
- instance_eval(&block) if block_given?
157
- end
158
- end
139
+ def org_invite(*usernames)
140
+ before(chef_server_options[:server_scope]) { org_invite(*usernames) }
141
+ end
159
142
 
160
- def self.data_bag(name, data, &block)
161
- before(:each) { data_bag(name, data, &block) }
162
- end
143
+ def org_member(*usernames)
144
+ before(chef_server_options[:server_scope]) { org_member(*usernames) }
145
+ end
163
146
 
164
- def data_bag(name, data, &block)
165
- with_object_path("data/#{name}") do
166
- ChefZero::RSpec.server.load_data({ 'data' => { name => data }}, current_org)
167
- instance_eval(&block) if block_given?
168
- end
169
- end
147
+ def role(name, data, &block)
148
+ before(chef_server_options[:server_scope]) { role(name, data, &block) }
149
+ end
170
150
 
171
- def self.environment(name, data, &block)
172
- before(:each) { environment(name, data, &block) }
173
- end
151
+ def sandbox(name, data, &block)
152
+ before(chef_server_options[:server_scope]) { sandbox(name, data, &block) }
153
+ end
174
154
 
175
- def environment(name, data, &block)
176
- with_object_path("environments/#{name}") do
177
- ChefZero::RSpec.server.load_data({ 'environments' => { name => data } }, current_org)
178
- instance_eval(&block) if block_given?
155
+ def user(name, data, &block)
156
+ before(chef_server_options[:server_scope]) { user(name, data, &block) }
157
+ end
158
+ end
159
+
160
+ module WhenTheChefServerInstanceMethods
161
+ def organization(name, org = '{}', &block)
162
+ ChefZero::RSpec.server.data_store.set([ 'organizations', name, 'org' ], dejsonize(org), :create_dir, :create)
163
+ prev_org_name = @current_org
164
+ @current_org = name
165
+ prev_object_path = @current_object_path
166
+ @current_object_path = "organizations/#{name}"
167
+ if block_given?
168
+ begin
169
+ instance_eval(&block)
170
+ ensure
171
+ @current_org = prev_org_name
172
+ @current_object_path = prev_object_path
179
173
  end
180
174
  end
175
+ end
181
176
 
182
- def self.group(name, data, &block)
183
- before(:each) { group(name, data, &block) }
184
- end
177
+ def acl_for(path, data)
178
+ ChefZero::RSpec.server.load_data({ 'acls' => { path => data } }, current_org)
179
+ end
185
180
 
186
- def group(name, data, &block)
187
- with_object_path("groups/#{name}") do
188
- ChefZero::RSpec.server.load_data({ 'groups' => { name => data } }, current_org)
189
- instance_eval(&block) if block_given?
190
- end
181
+ def acl(data)
182
+ acl_for(@current_object_path, data)
183
+ end
184
+
185
+ def client(name, data, &block)
186
+ with_object_path("clients/#{name}") do
187
+ ChefZero::RSpec.server.load_data({ 'clients' => { name => data } }, current_org)
188
+ instance_eval(&block) if block_given?
191
189
  end
190
+ end
192
191
 
193
- def self.node(name, data, &block)
194
- before(:each) { node(name, data, &block) }
192
+ def container(name, data, &block)
193
+ with_object_path("containers/#{name}") do
194
+ ChefZero::RSpec.server.load_data({ 'containers' => { name => data } }, current_org)
195
+ instance_eval(&block) if block_given?
195
196
  end
197
+ end
196
198
 
197
- def node(name, data, &block)
198
- with_object_path("nodes/#{name}") do
199
- ChefZero::RSpec.server.load_data({ 'nodes' => { name => data } }, current_org)
200
- instance_eval(&block) if block_given?
199
+ def cookbook(name, version, data = {}, options = {}, &block)
200
+ with_object_path("cookbooks/#{name}") do
201
+ if data.has_key?('metadata.rb')
202
+ if data['metadata.rb'].nil?
203
+ data.delete('metadata.rb')
204
+ end
205
+ else
206
+ data['metadata.rb'] = "name #{name.inspect}; version #{version.inspect}"
201
207
  end
208
+ ChefZero::RSpec.server.load_data({ 'cookbooks' => { "#{name}-#{version}" => data.merge(options) }}, current_org)
209
+ instance_eval(&block) if block_given?
202
210
  end
211
+ end
203
212
 
204
- def self.org_invite(*usernames)
205
- before(:each) { org_invite(*usernames) }
213
+ def data_bag(name, data, &block)
214
+ with_object_path("data/#{name}") do
215
+ ChefZero::RSpec.server.load_data({ 'data' => { name => data }}, current_org)
216
+ instance_eval(&block) if block_given?
206
217
  end
218
+ end
207
219
 
208
- def org_invite(*usernames)
209
- ChefZero::RSpec.server.load_data({ 'invites' => usernames }, current_org)
220
+ def environment(name, data, &block)
221
+ with_object_path("environments/#{name}") do
222
+ ChefZero::RSpec.server.load_data({ 'environments' => { name => data } }, current_org)
223
+ instance_eval(&block) if block_given?
210
224
  end
225
+ end
211
226
 
212
- def self.org_member(*usernames)
213
- before(:each) { org_member(*usernames) }
227
+ def group(name, data, &block)
228
+ with_object_path("groups/#{name}") do
229
+ ChefZero::RSpec.server.load_data({ 'groups' => { name => data } }, current_org)
230
+ instance_eval(&block) if block_given?
214
231
  end
232
+ end
215
233
 
216
- def org_member(*usernames)
217
- ChefZero::RSpec.server.load_data({ 'members' => usernames }, current_org)
234
+ def node(name, data, &block)
235
+ with_object_path("nodes/#{name}") do
236
+ ChefZero::RSpec.server.load_data({ 'nodes' => { name => data } }, current_org)
237
+ instance_eval(&block) if block_given?
218
238
  end
239
+ end
219
240
 
220
- def self.role(name, data, &block)
221
- before(:each) { role(name, data, &block) }
222
- end
241
+ def org_invite(*usernames)
242
+ ChefZero::RSpec.server.load_data({ 'invites' => usernames }, current_org)
243
+ end
223
244
 
224
- def role(name, data, &block)
225
- with_object_path("roles/#{name}") do
226
- ChefZero::RSpec.server.load_data({ 'roles' => { name => data } }, current_org)
227
- instance_eval(&block) if block_given?
228
- end
245
+ def org_member(*usernames)
246
+ ChefZero::RSpec.server.load_data({ 'members' => usernames }, current_org)
247
+ end
248
+
249
+ def role(name, data, &block)
250
+ with_object_path("roles/#{name}") do
251
+ ChefZero::RSpec.server.load_data({ 'roles' => { name => data } }, current_org)
252
+ instance_eval(&block) if block_given?
229
253
  end
254
+ end
230
255
 
231
- def self.sandbox(name, data, &block)
232
- before(:each) { sandbox(name, data, &block) }
256
+ def sandbox(name, data, &block)
257
+ with_object_path("sandboxes/#{name}") do
258
+ ChefZero::RSpec.server.load_data({ 'sandboxes' => { name => data } }, current_org)
259
+ instance_eval(&block) if block_given?
233
260
  end
261
+ end
234
262
 
235
- def sandbox(name, data, &block)
236
- with_object_path("sandboxes/#{name}") do
237
- ChefZero::RSpec.server.load_data({ 'sandboxes' => { name => data } }, current_org)
263
+ def user(name, data, &block)
264
+ if ChefZero::RSpec.server.options[:osc_compat]
265
+ with_object_path("users/#{name}") do
266
+ ChefZero::RSpec.server.load_data({ 'users' => { name => data }}, current_org)
238
267
  instance_eval(&block) if block_given?
239
268
  end
240
- end
241
-
242
- def self.user(name, data, &block)
243
- before(:each) { user(name, data, &block) }
244
- end
245
-
246
- def user(name, data, &block)
247
- if ChefZero::RSpec.server.options[:osc_compat]
248
- with_object_path("users/#{name}") do
249
- ChefZero::RSpec.server.load_data({ 'users' => { name => data }}, current_org)
250
- instance_eval(&block) if block_given?
251
- end
252
- else
253
- old_object_path = @current_object_path
254
- @current_object_path = "users/#{name}"
255
- begin
256
- ChefZero::RSpec.server.load_data({ 'users' => { name => data }}, current_org)
257
- instance_eval(&block) if block_given?
258
- ensure
259
- @current_object_path = old_object_path
260
- end
269
+ else
270
+ old_object_path = @current_object_path
271
+ @current_object_path = "users/#{name}"
272
+ begin
273
+ ChefZero::RSpec.server.load_data({ 'users' => { name => data }}, current_org)
274
+ instance_eval(&block) if block_given?
275
+ ensure
276
+ @current_object_path = old_object_path
261
277
  end
262
278
  end
279
+ end
263
280
 
264
- def dejsonize(data)
265
- if data.is_a?(String)
266
- data
267
- else
268
- FFI_Yajl::Encoder.encode(data, :pretty => true)
269
- end
281
+ def dejsonize(data)
282
+ if data.is_a?(String)
283
+ data
284
+ else
285
+ FFI_Yajl::Encoder.encode(data, :pretty => true)
270
286
  end
287
+ end
271
288
 
272
- def current_org
273
- @current_org || ChefZero::RSpec.server.options[:single_org] || nil
274
- end
289
+ def current_org
290
+ @current_org || ChefZero::RSpec.server.options[:single_org] || nil
291
+ end
275
292
 
276
- def with_object_path(object_path)
277
- old_object_path = @current_object_path
278
- @current_object_path = object_path
279
- begin
280
- yield if block_given?
281
- end
282
- @current_object_path = old_object_path
293
+ def with_object_path(object_path)
294
+ old_object_path = @current_object_path
295
+ @current_object_path = object_path
296
+ begin
297
+ yield if block_given?
283
298
  end
284
-
285
- # after :each do
286
- # if @@ChefZero::RSpec.server
287
- # @@ChefZero::RSpec.server.stop
288
- # @@ChefZero::RSpec.server = nil
289
- # end
290
- # if @@ChefZero::RSpec.client_key
291
- # @@ChefZero::RSpec.client_key.unlink
292
- # @@ChefZero::RSpec.client_key = nil
293
- # end
294
- # end
295
-
296
- instance_eval(&block)
299
+ @current_object_path = old_object_path
297
300
  end
298
301
  end
299
302
  end
@@ -1,3 +1,3 @@
1
1
  module ChefZero
2
- VERSION = '4.1.0'
2
+ VERSION = '4.2.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chef-zero
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.0
4
+ version: 4.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Keiser
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-01 00:00:00.000000000 Z
11
+ date: 2015-04-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mixlib-log
@@ -56,16 +56,22 @@ dependencies:
56
56
  name: ffi-yajl
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '1.1'
62
+ - - "<"
63
+ - !ruby/object:Gem::Version
64
+ version: '3.0'
62
65
  type: :runtime
63
66
  prerelease: false
64
67
  version_requirements: !ruby/object:Gem::Requirement
65
68
  requirements:
66
- - - "~>"
69
+ - - ">="
67
70
  - !ruby/object:Gem::Version
68
71
  version: '1.1'
72
+ - - "<"
73
+ - !ruby/object:Gem::Version
74
+ version: '3.0'
69
75
  - !ruby/object:Gem::Dependency
70
76
  name: rack
71
77
  requirement: !ruby/object:Gem::Requirement
@@ -233,7 +239,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
233
239
  version: '0'
234
240
  requirements: []
235
241
  rubyforge_project:
236
- rubygems_version: 2.2.2
242
+ rubygems_version: 2.4.2
237
243
  signing_key:
238
244
  specification_version: 4
239
245
  summary: Self-contained, easy-setup, fast-start in-memory Chef server for testing