chef 12.5.0.alpha.1 → 12.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (275) hide show
  1. checksums.yaml +4 -4
  2. data/CONTRIBUTING.md +21 -25
  3. data/Gemfile +46 -0
  4. data/README.md +4 -4
  5. data/Rakefile +5 -110
  6. data/distro/common/html/knife_cookbook_site.html +18 -18
  7. data/distro/common/man/man1/knife-cookbook-site.1 +11 -11
  8. data/lib/chef/application.rb +1 -1
  9. data/lib/chef/application/apply.rb +19 -1
  10. data/lib/chef/application/client.rb +11 -5
  11. data/lib/chef/application/knife.rb +2 -2
  12. data/lib/chef/application/windows_service_manager.rb +9 -7
  13. data/lib/chef/chef_class.rb +39 -0
  14. data/lib/chef/chef_fs/data_handler/client_data_handler.rb +3 -1
  15. data/lib/chef/chef_fs/file_system/acl_dir.rb +3 -4
  16. data/lib/chef/chef_fs/file_system/acls_dir.rb +5 -1
  17. data/lib/chef/chef_fs/file_system/base_fs_dir.rb +0 -5
  18. data/lib/chef/chef_fs/file_system/base_fs_object.rb +5 -2
  19. data/lib/chef/chef_fs/file_system/chef_repository_file_system_cookbook_dir.rb +2 -9
  20. data/lib/chef/chef_fs/file_system/chef_repository_file_system_cookbook_entry.rb +2 -9
  21. data/lib/chef/chef_fs/file_system/chef_repository_file_system_cookbooks_dir.rb +10 -17
  22. data/lib/chef/chef_fs/file_system/chef_repository_file_system_entry.rb +1 -12
  23. data/lib/chef/chef_fs/file_system/chef_repository_file_system_root_dir.rb +15 -11
  24. data/lib/chef/chef_fs/file_system/chef_server_root_dir.rb +8 -2
  25. data/lib/chef/chef_fs/file_system/cookbook_dir.rb +3 -4
  26. data/lib/chef/chef_fs/file_system/cookbooks_acl_dir.rb +1 -1
  27. data/lib/chef/chef_fs/file_system/cookbooks_dir.rb +3 -11
  28. data/lib/chef/chef_fs/file_system/data_bags_dir.rb +3 -5
  29. data/lib/chef/chef_fs/file_system/environments_dir.rb +1 -1
  30. data/lib/chef/chef_fs/file_system/file_system_entry.rb +7 -4
  31. data/lib/chef/chef_fs/file_system/memory_dir.rb +2 -3
  32. data/lib/chef/chef_fs/file_system/multiplexed_dir.rb +15 -0
  33. data/lib/chef/chef_fs/file_system/nodes_dir.rb +1 -1
  34. data/lib/chef/chef_fs/file_system/organization_members_entry.rb +2 -2
  35. data/lib/chef/chef_fs/file_system/rest_list_dir.rb +4 -9
  36. data/lib/chef/client.rb +2 -3
  37. data/lib/chef/config.rb +34 -2
  38. data/lib/chef/cookbook/metadata.rb +25 -3
  39. data/lib/chef/cookbook/synchronizer.rb +1 -1
  40. data/lib/chef/cookbook_site_streaming_uploader.rb +1 -1
  41. data/lib/chef/cookbook_version.rb +3 -3
  42. data/lib/chef/deprecation/mixin/template.rb +1 -2
  43. data/lib/chef/deprecation/provider/cookbook_file.rb +1 -1
  44. data/lib/chef/deprecation/provider/file.rb +1 -1
  45. data/lib/chef/deprecation/provider/remote_directory.rb +52 -0
  46. data/lib/chef/deprecation/provider/remote_file.rb +1 -2
  47. data/lib/chef/deprecation/provider/template.rb +1 -1
  48. data/lib/chef/deprecation/warnings.rb +3 -4
  49. data/lib/chef/dsl/reboot_pending.rb +3 -2
  50. data/lib/chef/dsl/recipe.rb +6 -5
  51. data/lib/chef/dsl/resources.rb +2 -2
  52. data/lib/chef/event_dispatch/base.rb +12 -7
  53. data/lib/chef/event_dispatch/dispatcher.rb +21 -6
  54. data/lib/chef/exceptions.rb +22 -0
  55. data/lib/chef/file_content_management/tempfile.rb +1 -1
  56. data/lib/chef/formatters/base.rb +3 -0
  57. data/lib/chef/formatters/doc.rb +53 -5
  58. data/lib/chef/formatters/error_inspectors/compile_error_inspector.rb +36 -0
  59. data/lib/chef/formatters/minimal.rb +2 -2
  60. data/lib/chef/knife.rb +35 -55
  61. data/lib/chef/knife/bootstrap.rb +41 -0
  62. data/lib/chef/knife/bootstrap/chef_vault_handler.rb +1 -0
  63. data/lib/chef/knife/bootstrap/client_builder.rb +16 -0
  64. data/lib/chef/knife/bootstrap/templates/README.md +3 -4
  65. data/lib/chef/knife/cookbook_create.rb +1 -1
  66. data/lib/chef/knife/cookbook_site_download.rb +1 -1
  67. data/lib/chef/knife/cookbook_site_install.rb +1 -1
  68. data/lib/chef/knife/cookbook_site_share.rb +6 -6
  69. data/lib/chef/knife/cookbook_site_unshare.rb +2 -2
  70. data/lib/chef/knife/core/bootstrap_context.rb +12 -4
  71. data/lib/chef/knife/core/custom_manifest_loader.rb +69 -0
  72. data/lib/chef/knife/core/gem_glob_loader.rb +138 -0
  73. data/lib/chef/knife/core/hashed_command_loader.rb +80 -0
  74. data/lib/chef/knife/core/node_presenter.rb +24 -1
  75. data/lib/chef/knife/core/object_loader.rb +1 -0
  76. data/lib/chef/knife/core/subcommand_loader.rb +131 -146
  77. data/lib/chef/knife/node_run_list_remove.rb +12 -1
  78. data/lib/chef/knife/null.rb +10 -0
  79. data/lib/chef/knife/rehash.rb +62 -0
  80. data/lib/chef/knife/search.rb +3 -3
  81. data/lib/chef/knife/ssh.rb +52 -30
  82. data/lib/chef/local_mode.rb +5 -0
  83. data/lib/chef/log.rb +5 -1
  84. data/lib/chef/mixin/deprecation.rb +8 -8
  85. data/lib/chef/mixin/params_validate.rb +2 -2
  86. data/lib/chef/mixin/template.rb +48 -0
  87. data/lib/chef/mixin/which.rb +1 -1
  88. data/lib/chef/mixin/wide_string.rb +72 -0
  89. data/lib/chef/mixin/windows_architecture_helper.rb +15 -39
  90. data/lib/chef/mixin/windows_env_helper.rb +4 -1
  91. data/lib/chef/monkey_patches/webrick-utils.rb +51 -0
  92. data/lib/chef/monkey_patches/win32/registry.rb +72 -0
  93. data/lib/chef/node.rb +97 -3
  94. data/lib/chef/node_map.rb +2 -2
  95. data/lib/chef/platform/provider_mapping.rb +5 -6
  96. data/lib/chef/platform/query_helpers.rb +46 -4
  97. data/lib/chef/platform/service_helpers.rb +30 -32
  98. data/lib/chef/policy_builder.rb +1 -8
  99. data/lib/chef/policy_builder/dynamic.rb +186 -0
  100. data/lib/chef/policy_builder/expand_node_object.rb +30 -15
  101. data/lib/chef/policy_builder/policyfile.rb +155 -18
  102. data/lib/chef/property.rb +38 -9
  103. data/lib/chef/provider.rb +104 -12
  104. data/lib/chef/provider/batch.rb +8 -0
  105. data/lib/chef/provider/deploy.rb +2 -2
  106. data/lib/chef/provider/directory.rb +14 -2
  107. data/lib/chef/provider/dsc_resource.rb +5 -9
  108. data/lib/chef/provider/group/pw.rb +1 -1
  109. data/lib/chef/provider/ifconfig.rb +2 -2
  110. data/lib/chef/provider/mount.rb +7 -3
  111. data/lib/chef/provider/package/dpkg.rb +5 -11
  112. data/lib/chef/provider/package/rpm.rb +2 -2
  113. data/lib/chef/provider/package/rubygems.rb +1 -1
  114. data/lib/chef/provider/package/windows/msi.rb +2 -2
  115. data/lib/chef/provider/package/yum.rb +17 -5
  116. data/lib/chef/provider/powershell_script.rb +27 -4
  117. data/lib/chef/provider/remote_directory.rb +190 -102
  118. data/lib/chef/provider/service.rb +12 -2
  119. data/lib/chef/provider/service/aix.rb +1 -1
  120. data/lib/chef/provider/service/debian.rb +3 -5
  121. data/lib/chef/provider/service/freebsd.rb +1 -1
  122. data/lib/chef/provider/service/gentoo.rb +3 -3
  123. data/lib/chef/provider/service/init.rb +3 -3
  124. data/lib/chef/provider/service/insserv.rb +2 -4
  125. data/lib/chef/provider/service/invokercd.rb +2 -4
  126. data/lib/chef/provider/service/openbsd.rb +2 -1
  127. data/lib/chef/provider/service/redhat.rb +52 -16
  128. data/lib/chef/provider/service/simple.rb +2 -2
  129. data/lib/chef/provider/service/systemd.rb +3 -5
  130. data/lib/chef/provider/service/upstart.rb +4 -6
  131. data/lib/chef/provider/subversion.rb +13 -7
  132. data/lib/chef/provider/template/content.rb +16 -6
  133. data/lib/chef/provider/user/solaris.rb +32 -4
  134. data/lib/chef/provider/windows_script.rb +3 -5
  135. data/lib/chef/provider_resolver.rb +2 -2
  136. data/lib/chef/resource.rb +85 -27
  137. data/lib/chef/resource/action_class.rb +83 -0
  138. data/lib/chef/resource/chef_gem.rb +3 -3
  139. data/lib/chef/resource/deploy.rb +8 -2
  140. data/lib/chef/resource/dsc_script.rb +2 -0
  141. data/lib/chef/resource/file/verification.rb +1 -1
  142. data/lib/chef/resource/registry_key.rb +1 -1
  143. data/lib/chef/resource/service.rb +10 -2
  144. data/lib/chef/resource/subversion.rb +5 -0
  145. data/lib/chef/resource/windows_script.rb +6 -2
  146. data/lib/chef/resource/yum_package.rb +10 -1
  147. data/lib/chef/resource_resolver.rb +3 -3
  148. data/lib/chef/run_context.rb +6 -8
  149. data/lib/chef/run_list/versioned_recipe_list.rb +15 -0
  150. data/lib/chef/run_lock.rb +30 -21
  151. data/lib/chef/util/powershell/ps_credential.rb +4 -0
  152. data/lib/chef/util/windows.rb +0 -32
  153. data/lib/chef/util/windows/net_group.rb +85 -106
  154. data/lib/chef/util/windows/net_use.rb +35 -71
  155. data/lib/chef/util/windows/net_user.rb +0 -1
  156. data/lib/chef/util/windows/volume.rb +19 -19
  157. data/lib/chef/version.rb +3 -3
  158. data/lib/chef/win32/api.rb +1 -0
  159. data/lib/chef/win32/api/file.rb +20 -0
  160. data/lib/chef/win32/api/net.rb +163 -43
  161. data/lib/chef/win32/api/registry.rb +51 -0
  162. data/lib/chef/win32/api/system.rb +23 -0
  163. data/lib/chef/win32/api/unicode.rb +0 -43
  164. data/lib/chef/win32/crypto.rb +2 -1
  165. data/lib/chef/win32/file.rb +28 -3
  166. data/lib/chef/win32/mutex.rb +1 -2
  167. data/lib/chef/win32/net.rb +162 -8
  168. data/lib/chef/win32/process.rb +13 -0
  169. data/lib/chef/win32/registry.rb +25 -28
  170. data/lib/chef/win32/security.rb +1 -1
  171. data/lib/chef/win32/security/token.rb +1 -1
  172. data/lib/chef/win32/system.rb +62 -0
  173. data/lib/chef/win32/unicode.rb +7 -2
  174. data/lib/chef/win32/version.rb +0 -4
  175. data/spec/data/cookbooks/openldap/templates/default/helpers.erb +14 -0
  176. data/spec/data/cookbooks/openldap/templates/default/nested_openldap_partials.erb +1 -0
  177. data/spec/data/cookbooks/openldap/templates/default/nested_partial.erb +1 -0
  178. data/spec/data/dsc_lcm.pfx +0 -0
  179. data/spec/functional/dsl/reboot_pending_spec.rb +33 -43
  180. data/spec/functional/knife/cookbook_delete_spec.rb +17 -7
  181. data/spec/functional/knife/ssh_spec.rb +16 -0
  182. data/spec/functional/resource/deploy_revision_spec.rb +1 -1
  183. data/spec/functional/resource/dsc_resource_spec.rb +2 -0
  184. data/spec/functional/resource/dsc_script_spec.rb +91 -2
  185. data/spec/functional/resource/group_spec.rb +67 -44
  186. data/spec/functional/resource/powershell_script_spec.rb +104 -15
  187. data/spec/functional/resource/windows_service_spec.rb +1 -1
  188. data/spec/functional/run_lock_spec.rb +368 -189
  189. data/spec/functional/win32/{registry_helper_spec.rb → registry_spec.rb} +4 -23
  190. data/spec/integration/client/client_spec.rb +51 -0
  191. data/spec/integration/knife/download_spec.rb +4 -0
  192. data/spec/integration/knife/list_spec.rb +8 -0
  193. data/spec/integration/knife/upload_spec.rb +1 -1
  194. data/spec/integration/recipes/recipe_dsl_spec.rb +1 -1
  195. data/spec/integration/recipes/remote_directory.rb +74 -0
  196. data/spec/integration/recipes/resource_action_spec.rb +42 -22
  197. data/spec/integration/recipes/resource_converge_if_changed_spec.rb +423 -0
  198. data/spec/integration/recipes/resource_load_spec.rb +206 -0
  199. data/spec/spec_helper.rb +8 -0
  200. data/spec/support/platform_helpers.rb +13 -0
  201. data/{lib/chef/mixin/wstring.rb → spec/support/shared/context/win32.rb} +15 -12
  202. data/spec/support/shared/functional/windows_script.rb +63 -26
  203. data/spec/support/shared/unit/mock_shellout.rb +46 -0
  204. data/spec/support/shared/unit/provider/file.rb +10 -4
  205. data/spec/unit/application/client_spec.rb +16 -3
  206. data/spec/unit/application/knife_spec.rb +2 -2
  207. data/spec/unit/chef_class_spec.rb +0 -4
  208. data/spec/unit/client_spec.rb +6 -1
  209. data/spec/unit/config_spec.rb +31 -0
  210. data/spec/unit/cookbook/metadata_spec.rb +23 -3
  211. data/spec/unit/cookbook/syntax_check_spec.rb +3 -0
  212. data/spec/unit/deprecation_spec.rb +3 -6
  213. data/spec/unit/dsl/reboot_pending_spec.rb +12 -6
  214. data/spec/unit/event_dispatch/dispatcher_spec.rb +65 -3
  215. data/spec/unit/event_dispatch/dsl_spec.rb +0 -4
  216. data/spec/unit/formatters/doc_spec.rb +32 -0
  217. data/spec/unit/formatters/error_inspectors/compile_error_inspector_spec.rb +26 -0
  218. data/spec/unit/knife/bootstrap/client_builder_spec.rb +27 -0
  219. data/spec/unit/knife/bootstrap_spec.rb +55 -3
  220. data/spec/unit/knife/cookbook_site_share_spec.rb +3 -3
  221. data/spec/unit/knife/core/bootstrap_context_spec.rb +21 -4
  222. data/spec/unit/knife/core/custom_manifest_loader_spec.rb +41 -0
  223. data/spec/unit/knife/core/gem_glob_loader_spec.rb +210 -0
  224. data/spec/unit/knife/core/hashed_command_loader_spec.rb +93 -0
  225. data/spec/unit/knife/core/subcommand_loader_spec.rb +16 -192
  226. data/spec/unit/knife/node_run_list_remove_spec.rb +17 -0
  227. data/spec/unit/mixin/enforce_ownership_and_permissions_spec.rb +10 -10
  228. data/spec/unit/mixin/template_spec.rb +5 -1
  229. data/spec/unit/mixin/windows_architecture_helper_spec.rb +13 -8
  230. data/spec/unit/node_spec.rb +213 -0
  231. data/spec/unit/platform/query_helpers_spec.rb +146 -3
  232. data/spec/unit/policy_builder/dynamic_spec.rb +275 -0
  233. data/spec/unit/policy_builder/expand_node_object_spec.rb +37 -38
  234. data/spec/unit/policy_builder/policyfile_spec.rb +260 -46
  235. data/spec/unit/property/validation_spec.rb +5 -0
  236. data/spec/unit/property_spec.rb +152 -26
  237. data/spec/unit/provider/deploy_spec.rb +5 -5
  238. data/spec/unit/provider/directory_spec.rb +35 -0
  239. data/spec/unit/provider/dsc_resource_spec.rb +3 -10
  240. data/spec/unit/provider/ifconfig_spec.rb +22 -2
  241. data/spec/unit/provider/mount/aix_spec.rb +2 -1
  242. data/spec/unit/provider/mount/mount_spec.rb +6 -0
  243. data/spec/unit/provider/mount/windows_spec.rb +14 -0
  244. data/spec/unit/provider/mount_spec.rb +12 -1
  245. data/spec/unit/provider/package/dpkg_spec.rb +8 -1
  246. data/spec/unit/provider/package/rpm_spec.rb +18 -1
  247. data/spec/unit/provider/package/rubygems_spec.rb +18 -0
  248. data/spec/unit/provider/package/yum_spec.rb +97 -24
  249. data/spec/unit/provider/powershell_script_spec.rb +52 -26
  250. data/spec/unit/provider/remote_directory_spec.rb +1 -2
  251. data/spec/unit/provider/service/aix_service_spec.rb +3 -3
  252. data/spec/unit/provider/service/gentoo_service_spec.rb +4 -4
  253. data/spec/unit/provider/service/openbsd_service_spec.rb +10 -8
  254. data/spec/unit/provider/service/redhat_spec.rb +88 -8
  255. data/spec/unit/provider/service/upstart_service_spec.rb +11 -7
  256. data/spec/unit/provider/service/windows_spec.rb +211 -200
  257. data/spec/unit/provider/subversion_spec.rb +50 -31
  258. data/spec/unit/provider/template/content_spec.rb +93 -2
  259. data/spec/unit/provider/user/solaris_spec.rb +66 -9
  260. data/spec/unit/provider_resolver_spec.rb +707 -650
  261. data/spec/unit/resource/deploy_spec.rb +7 -1
  262. data/spec/unit/resource/dsc_script_spec.rb +4 -0
  263. data/spec/unit/resource/file/verification_spec.rb +1 -1
  264. data/spec/unit/resource/powershell_script_spec.rb +17 -13
  265. data/spec/unit/resource/service_spec.rb +4 -4
  266. data/spec/unit/resource/subversion_spec.rb +4 -0
  267. data/spec/unit/resource/yum_package_spec.rb +10 -1
  268. data/spec/unit/run_list/versioned_recipe_list_spec.rb +5 -0
  269. data/spec/unit/win32/registry_spec.rb +394 -0
  270. data/tasks/external_tests.rb +47 -23
  271. data/tasks/maintainers.rb +155 -14
  272. metadata +51 -26
  273. data/lib/chef/knife/bootstrap/templates/archlinux-gems.erb +0 -76
  274. data/lib/chef/knife/bootstrap/templates/chef-aix.erb +0 -72
  275. data/spec/unit/registry_helper_spec.rb +0 -390
@@ -76,8 +76,11 @@ describe Chef::PolicyBuilder::Policyfile do
76
76
 
77
77
  let(:policyfile_run_list) { ["recipe[example1::default]", "recipe[example2::server]"] }
78
78
 
79
- let(:parsed_policyfile_json) do
79
+ let(:basic_valid_policy_data) do
80
80
  {
81
+ "name" => "example-policy",
82
+ "revision_id" => "123abc",
83
+
81
84
  "run_list" => policyfile_run_list,
82
85
 
83
86
  "cookbook_locks" => {
@@ -90,6 +93,8 @@ describe Chef::PolicyBuilder::Policyfile do
90
93
  }
91
94
  end
92
95
 
96
+ let(:parsed_policyfile_json) { basic_valid_policy_data }
97
+
93
98
  let(:err_namespace) { Chef::PolicyBuilder::Policyfile }
94
99
 
95
100
  it "configures a Chef HTTP API client" do
@@ -181,19 +186,13 @@ describe Chef::PolicyBuilder::Policyfile do
181
186
  let(:error404) { Net::HTTPServerException.new("404 message", :body) }
182
187
 
183
188
  before do
184
- expect(Chef::Node).to receive(:find_or_create).with(node_name).and_return(node)
185
189
  expect(http_api).to receive(:get).
186
190
  with("data/policyfiles/example-policy-stage").
187
191
  and_raise(error404)
188
192
  end
189
193
 
190
194
  it "raises an error" do
191
- expect { policy_builder.load_node }.to raise_error(err_namespace::ConfigurationError)
192
- end
193
-
194
- it "sends error message to the event system" do
195
- expect(events).to receive(:node_load_failed).with(node_name, an_instance_of(err_namespace::ConfigurationError), Chef::Config)
196
- expect { policy_builder.load_node }.to raise_error(err_namespace::ConfigurationError)
195
+ expect { policy_builder.finish_load_node(node) }.to raise_error(err_namespace::ConfigurationError)
197
196
  end
198
197
 
199
198
  end
@@ -201,20 +200,12 @@ describe Chef::PolicyBuilder::Policyfile do
201
200
  context "when the deployment_group is not configured" do
202
201
  before do
203
202
  Chef::Config[:deployment_group] = nil
204
- expect(Chef::Node).to receive(:find_or_create).with(node_name).and_return(node)
205
203
  end
206
204
 
207
205
  it "errors while loading the node" do
208
- expect { policy_builder.load_node }.to raise_error(err_namespace::ConfigurationError)
206
+ expect { policy_builder.finish_load_node(node) }.to raise_error(err_namespace::ConfigurationError)
209
207
  end
210
208
 
211
-
212
- it "passes error information to the event system" do
213
- # TODO: also make sure something acceptable happens with the error formatters
214
- err_class = err_namespace::ConfigurationError
215
- expect(events).to receive(:node_load_failed).with(node_name, an_instance_of(err_class), Chef::Config)
216
- expect { policy_builder.load_node }.to raise_error(err_class)
217
- end
218
209
  end
219
210
 
220
211
  context "when deployment_group is correctly configured" do
@@ -307,8 +298,7 @@ describe Chef::PolicyBuilder::Policyfile do
307
298
  end
308
299
 
309
300
  it "implements #expand_run_list in a manner compatible with ExpandNodeObject" do
310
- expect(Chef::Node).to receive(:find_or_create).with(node_name).and_return(node)
311
- policy_builder.load_node
301
+ policy_builder.finish_load_node(node)
312
302
  expect(policy_builder.expand_run_list).to respond_to(:recipes)
313
303
  expect(policy_builder.expand_run_list.recipes).to eq(["example1::default", "example2::server"])
314
304
  expect(policy_builder.expand_run_list.roles).to eq([])
@@ -345,46 +335,261 @@ describe Chef::PolicyBuilder::Policyfile do
345
335
 
346
336
  describe "building the node object" do
347
337
 
338
+ let(:extra_chef_config) { {} }
339
+
348
340
  before do
349
- expect(Chef::Node).to receive(:find_or_create).with(node_name).and_return(node)
341
+ # must be set before #build_node is called to have the proper effect
342
+ extra_chef_config.each do |key, value|
343
+ Chef::Config[key] = value
344
+ end
350
345
 
351
- policy_builder.load_node
346
+ policy_builder.finish_load_node(node)
352
347
  policy_builder.build_node
353
348
  end
354
349
 
350
+ # it sets policy_name and policy_group in the following priority order:
351
+ # -j JSON > config file > node object
352
+
353
+ describe "selecting policy_name and policy_group from the various sources" do
354
+
355
+ context "when only set in node JSON" do
356
+
357
+ let(:json_attribs) do
358
+ {
359
+ "policy_name" => "policy_name_from_node_json",
360
+ "policy_group" => "policy_group_from_node_json"
361
+ }
362
+ end
363
+
364
+ it "sets policy_name and policy_group on Chef::Config" do
365
+ expect(Chef::Config[:policy_name]).to eq("policy_name_from_node_json")
366
+ expect(Chef::Config[:policy_group]).to eq("policy_group_from_node_json")
367
+ end
368
+
369
+ it "sets policy_name and policy_group on the node object" do
370
+ expect(node.policy_name).to eq("policy_name_from_node_json")
371
+ expect(node.policy_group).to eq("policy_group_from_node_json")
372
+ end
373
+
374
+ end
375
+
376
+ context "when only set in Chef::Config" do
377
+
378
+ let(:extra_chef_config) do
379
+ {
380
+ policy_name: "policy_name_from_config",
381
+ policy_group: "policy_group_from_config"
382
+ }
383
+ end
384
+
385
+ it "sets policy_name and policy_group on the node object" do
386
+ expect(node.policy_name).to eq("policy_name_from_config")
387
+ expect(node.policy_group).to eq("policy_group_from_config")
388
+ end
389
+
390
+ end
391
+
392
+ context "when only set on the node" do
393
+
394
+ let(:node) do
395
+ node = Chef::Node.new
396
+ node.name(node_name)
397
+ node.policy_name = "policy_name_from_node"
398
+ node.policy_group = "policy_group_from_node"
399
+ node
400
+ end
401
+
402
+ it "sets policy_name and policy_group on Chef::Config" do
403
+ expect(Chef::Config[:policy_name]).to eq("policy_name_from_node")
404
+ expect(Chef::Config[:policy_group]).to eq("policy_group_from_node")
405
+ end
406
+
407
+ end
408
+
409
+ context "when set in Chef::Config and the fetched node" do
410
+
411
+ let(:node) do
412
+ node = Chef::Node.new
413
+ node.name(node_name)
414
+ node.policy_name = "policy_name_from_node"
415
+ node.policy_group = "policy_group_from_node"
416
+ node
417
+ end
418
+
419
+ let(:extra_chef_config) do
420
+ {
421
+ policy_name: "policy_name_from_config",
422
+ policy_group: "policy_group_from_config"
423
+ }
424
+ end
425
+
426
+ it "prefers the policy_name and policy_group from Chef::Config" do
427
+ expect(node.policy_name).to eq("policy_name_from_config")
428
+ expect(node.policy_group).to eq("policy_group_from_config")
429
+ end
430
+
431
+ end
432
+
433
+ context "when set in node json and the fetched node" do
434
+
435
+ let(:json_attribs) do
436
+ {
437
+ "policy_name" => "policy_name_from_node_json",
438
+ "policy_group" => "policy_group_from_node_json"
439
+ }
440
+ end
441
+
442
+ let(:node) do
443
+ node = Chef::Node.new
444
+ node.name(node_name)
445
+ node.policy_name = "policy_name_from_node"
446
+ node.policy_group = "policy_group_from_node"
447
+ node
448
+ end
449
+
450
+
451
+ it "prefers the policy_name and policy_group from the node json" do
452
+ expect(policy_builder.policy_name).to eq("policy_name_from_node_json")
453
+ expect(policy_builder.policy_group).to eq("policy_group_from_node_json")
454
+
455
+ expect(Chef::Config[:policy_name]).to eq("policy_name_from_node_json")
456
+ expect(Chef::Config[:policy_group]).to eq("policy_group_from_node_json")
457
+ expect(node.policy_name).to eq("policy_name_from_node_json")
458
+ expect(node.policy_group).to eq("policy_group_from_node_json")
459
+ end
460
+
461
+ end
462
+
463
+ context "when set in all sources" do
464
+
465
+ let(:json_attribs) do
466
+ {
467
+ "policy_name" => "policy_name_from_node_json",
468
+ "policy_group" => "policy_group_from_node_json"
469
+ }
470
+ end
471
+
472
+ let(:node) do
473
+ node = Chef::Node.new
474
+ node.name(node_name)
475
+ node.policy_name = "policy_name_from_node"
476
+ node.policy_group = "policy_group_from_node"
477
+ node
478
+ end
479
+
480
+ let(:extra_chef_config) do
481
+ {
482
+ policy_name: "policy_name_from_config",
483
+ policy_group: "policy_group_from_config"
484
+ }
485
+ end
486
+
487
+ it "prefers the policy_name and group from node json" do
488
+ expect(policy_builder.policy_name).to eq("policy_name_from_node_json")
489
+ expect(policy_builder.policy_group).to eq("policy_group_from_node_json")
490
+
491
+ expect(Chef::Config[:policy_name]).to eq("policy_name_from_node_json")
492
+ expect(Chef::Config[:policy_group]).to eq("policy_group_from_node_json")
493
+ expect(node.policy_name).to eq("policy_name_from_node_json")
494
+ expect(node.policy_group).to eq("policy_group_from_node_json")
495
+ end
496
+
497
+ end
498
+
499
+ end
500
+
355
501
  it "resets default and override data" do
356
502
  expect(node["default_key"]).to be_nil
357
503
  expect(node["override_key"]).to be_nil
358
504
  end
359
505
 
360
- it "applies ohai data" do
361
- expect(ohai_data).to_not be_empty # ensure test is testing something
362
- ohai_data.each do |key, value|
363
- expect(node.automatic_attrs[key]).to eq(value)
506
+ describe "setting attribute values" do
507
+
508
+ before do
509
+ policy_builder.build_node
364
510
  end
365
- end
366
511
 
367
- it "applies attributes from json file" do
368
- expect(node["custom_attr"]).to eq("custom_attr_value")
369
- end
512
+ it "resets default and override data" do
513
+ expect(node["default_key"]).to be_nil
514
+ expect(node["override_key"]).to be_nil
515
+ end
370
516
 
371
- it "applies attributes from the policyfile" do
372
- expect(node["policyfile_default_attr"]).to eq("policyfile_default_value")
373
- expect(node["policyfile_override_attr"]).to eq("policyfile_override_value")
374
- end
517
+ it "applies ohai data" do
518
+ expect(ohai_data).to_not be_empty # ensure test is testing something
519
+ ohai_data.each do |key, value|
520
+ expect(node.automatic_attrs[key]).to eq(value)
521
+ end
522
+ end
375
523
 
376
- it "sets the policyfile's run_list on the node object" do
377
- expect(node.run_list).to eq(policyfile_run_list)
378
- end
524
+ it "applies attributes from json file" do
525
+ expect(node["custom_attr"]).to eq("custom_attr_value")
526
+ end
379
527
 
380
- it "creates node.automatic_attrs[:roles]" do
381
- expect(node.automatic_attrs[:roles]).to eq([])
382
- end
528
+ it "applies attributes from the policyfile" do
529
+ expect(node["policyfile_default_attr"]).to eq("policyfile_default_value")
530
+ expect(node["policyfile_override_attr"]).to eq("policyfile_override_value")
531
+ end
532
+
533
+ it "sets the policyfile's run_list on the node object" do
534
+ expect(node.run_list).to eq(policyfile_run_list)
535
+ end
536
+
537
+ it "creates node.automatic_attrs[:roles]" do
538
+ expect(node.automatic_attrs[:roles]).to eq([])
539
+ end
383
540
 
384
- it "create node.automatic_attrs[:recipes]" do
385
- expect(node.automatic_attrs[:recipes]).to eq(["example1::default", "example2::server"])
541
+ it "create node.automatic_attrs[:recipes]" do
542
+ expect(node.automatic_attrs[:recipes]).to eq(["example1::default", "example2::server"])
543
+ end
386
544
  end
387
545
 
546
+ context "when a named run_list is given" do
547
+
548
+ before do
549
+ Chef::Config[:named_run_list] = "deploy-app"
550
+ end
551
+
552
+ context "and the named run_list is not present in the policy" do
553
+
554
+ it "raises a ConfigurationError" do
555
+ err_class = Chef::PolicyBuilder::Policyfile::ConfigurationError
556
+ err_text = "Policy 'example-policy' revision '123abc' does not have named_run_list 'deploy-app'(available named_run_lists: [])"
557
+ expect { policy_builder.build_node }.to raise_error(err_class, err_text)
558
+ end
559
+
560
+ end
561
+
562
+ context "and the named run_list is present in the policy" do
563
+
564
+ let(:parsed_policyfile_json) do
565
+ basic_valid_policy_data.dup.tap do |p|
566
+ p["named_run_lists"] = {
567
+ "deploy-app" => [ "recipe[example1::default]" ]
568
+ }
569
+ end
570
+ end
571
+
572
+ before do
573
+ policy_builder.build_node
574
+ end
575
+
576
+ it "sets the run list to the desired named run list" do
577
+ expect(policy_builder.run_list).to eq([ "recipe[example1::default]" ])
578
+ expected_expansion = Chef::PolicyBuilder::Policyfile::RunListExpansionIsh.new([ "example1::default" ], [])
579
+ expect(policy_builder.run_list_expansion).to eq(expected_expansion)
580
+ expect(policy_builder.run_list_with_versions_for_display).to eq(["example1::default@2.3.5 (168d210)"])
581
+ expect(node.run_list).to eq([ Chef::RunList::RunListItem.new("recipe[example1::default]") ])
582
+ expect(node[:roles]).to eq( [] )
583
+ expect(node[:recipes]).to eq( ["example1::default"] )
584
+ end
585
+
586
+ it "disables the cookbook cache cleaner" do
587
+ expect(Chef::CookbookCacheCleaner.instance.skip_removal).to be(true)
588
+ end
589
+
590
+ end
591
+
592
+ end
388
593
  end
389
594
 
390
595
 
@@ -414,9 +619,7 @@ describe Chef::PolicyBuilder::Policyfile do
414
619
  let(:error404) { Net::HTTPServerException.new("404 message", :body) }
415
620
 
416
621
  before do
417
- expect(Chef::Node).to receive(:find_or_create).with(node_name).and_return(node)
418
-
419
- policy_builder.load_node
622
+ policy_builder.finish_load_node(node)
420
623
  policy_builder.build_node
421
624
 
422
625
  expect(http_api).to receive(:get).with(cookbook1_url).
@@ -433,9 +636,9 @@ describe Chef::PolicyBuilder::Policyfile do
433
636
  shared_examples_for "fetching cookbooks when they exist" do
434
637
  context "and the cookbooks can be fetched" do
435
638
  before do
436
- expect(Chef::Node).to receive(:find_or_create).with(node_name).and_return(node)
639
+ Chef.reset!
437
640
 
438
- policy_builder.load_node
641
+ policy_builder.finish_load_node(node)
439
642
  policy_builder.build_node
440
643
 
441
644
  allow(Chef::CookbookSynchronizer).to receive(:new).
@@ -443,6 +646,10 @@ describe Chef::PolicyBuilder::Policyfile do
443
646
  and_return(cookbook_synchronizer)
444
647
  end
445
648
 
649
+ after do
650
+ Chef.reset!
651
+ end
652
+
446
653
  it "builds a Hash of the form 'cookbook_name' => Chef::CookbookVersion" do
447
654
  expect(policy_builder.cookbooks_to_sync).to eq(expected_cookbook_hash)
448
655
  end
@@ -460,6 +667,13 @@ describe Chef::PolicyBuilder::Policyfile do
460
667
  expect(run_context.cookbook_collection.keys).to match_array(["example1", "example2"])
461
668
  end
462
669
 
670
+ it "makes the run context available via static method on Chef" do
671
+ expect(cookbook_synchronizer).to receive(:sync_cookbooks)
672
+ expect_any_instance_of(Chef::RunContext).to receive(:load).with(policy_builder.run_list_expansion_ish)
673
+ run_context = policy_builder.setup_run_context
674
+ expect(Chef.run_context).to eq(run_context)
675
+ end
676
+
463
677
  end
464
678
  end # shared_examples_for "fetching cookbooks"
465
679
 
@@ -402,6 +402,11 @@ describe "Chef::Resource.property validation" do
402
402
  [ 'gbh', 123 ],
403
403
  [ nil ]
404
404
 
405
+ validation_test 'regex: [ [ /z/, /abc/ ], [ /n/ ] ]',
406
+ [ 'xabcy', 'aza', 'ana' ],
407
+ [ 'gbh', 123 ],
408
+ [ nil ]
409
+
405
410
  validation_test 'regex: []',
406
411
  [],
407
412
  [ :a ],
@@ -107,6 +107,56 @@ describe "Chef::Resource.property" do
107
107
  end
108
108
  end
109
109
 
110
+ with_property ":x, name_property: true" do
111
+ context "and subclass" do
112
+ let(:subresource_class) do
113
+ new_resource_name = self.class.new_resource_name
114
+ Class.new(resource_class) do
115
+ resource_name new_resource_name
116
+ end
117
+ end
118
+ let(:subresource) do
119
+ subresource_class.new('blah')
120
+ end
121
+
122
+ context "with property :x on the subclass" do
123
+ before do
124
+ subresource_class.class_eval do
125
+ property :x
126
+ end
127
+ end
128
+
129
+ it "x is still name_property" do
130
+ expect(subresource.x).to eq 'blah'
131
+ end
132
+ end
133
+
134
+ context "with property :x, name_attribute: false on the subclass" do
135
+ before do
136
+ subresource_class.class_eval do
137
+ property :x, name_attribute: false
138
+ end
139
+ end
140
+
141
+ it "x is no longer name_property" do
142
+ expect(subresource.x).to be_nil
143
+ end
144
+ end
145
+
146
+ context "with property :x, default: 10 on the subclass" do
147
+ before do
148
+ subresource_class.class_eval do
149
+ property :x, default: 10
150
+ end
151
+ end
152
+
153
+ it "x is no longer name_property" do
154
+ expect(subresource.x).to eq(10)
155
+ end
156
+ end
157
+ end
158
+ end
159
+
110
160
  with_property ":x, Integer" do
111
161
  context "and subclass" do
112
162
  let(:subresource_class) do
@@ -462,23 +512,21 @@ describe "Chef::Resource.property" do
462
512
  end
463
513
 
464
514
  context "hash default" do
465
- with_property ':x, default: {}' do
466
- it "when x is not set, it returns {}" do
467
- expect(resource.x).to eq({})
468
- end
469
- it "The same exact value is returned multiple times in a row" do
470
- value = resource.x
471
- expect(value).to eq({})
472
- expect(resource.x.object_id).to eq(value.object_id)
473
- end
474
- it "Multiple instances of x receive the exact same value" do
475
- expect(resource.x.object_id).to eq(resource_class.new('blah2').x.object_id)
476
- end
477
- it "The default value is frozen" do
478
- expect(resource.x).to be_frozen
479
- end
480
- it "The default value cannot be written to" do
481
- expect { resource.x[:a] = 1 }.to raise_error RuntimeError, /frozen/
515
+ context "(deprecations allowed)" do
516
+ before { Chef::Config[:treat_deprecation_warnings_as_errors] = false }
517
+
518
+ with_property ':x, default: {}' do
519
+ it "when x is not set, it returns {}" do
520
+ expect(resource.x).to eq({})
521
+ end
522
+ it "The same exact value is returned multiple times in a row" do
523
+ value = resource.x
524
+ expect(value).to eq({})
525
+ expect(resource.x.object_id).to eq(value.object_id)
526
+ end
527
+ it "Multiple instances of x receive the exact same value" do
528
+ expect(resource.x.object_id).to eq(resource_class.new('blah2').x.object_id)
529
+ end
482
530
  end
483
531
  end
484
532
 
@@ -948,21 +996,99 @@ describe "Chef::Resource.property" do
948
996
  expect(resource.x).to eq 'blah'
949
997
  end
950
998
  end
951
- with_property ":x, default: 10, #{name}: true" do
952
- it "chooses default over #{name}" do
953
- expect(resource.x).to eq 10
999
+
1000
+ with_property ":x, #{name}: false" do
1001
+ it "defaults to nil" do
1002
+ expect(resource.x).to be_nil
954
1003
  end
955
1004
  end
956
- with_property ":x, #{name}: true, default: 10" do
957
- it "chooses default over #{name}" do
958
- expect(resource.x).to eq 10
1005
+
1006
+ with_property ":x, #{name}: nil" do
1007
+ it "defaults to nil" do
1008
+ expect(resource.x).to be_nil
959
1009
  end
960
1010
  end
961
- with_property ":x, #{name}: true, required: true" do
962
- it "defaults x to resource.name" do
963
- expect(resource.x).to eq 'blah'
1011
+
1012
+ context "default ordering deprecation warnings" do
1013
+ it "emits a deprecation warning for property :x, default: 10, #{name}: true" do
1014
+ expect { resource_class.property :x, :default => 10, name.to_sym => true }.to raise_error Chef::Exceptions::DeprecatedFeatureError,
1015
+ /Cannot specify both default and name_property together on property x of resource chef_resource_property_spec_(\d+). Only one \(default\) will be obeyed./
1016
+ end
1017
+ it "emits a deprecation warning for property :x, default: nil, #{name}: true" do
1018
+ expect { resource_class.property :x, :default => nil, name.to_sym => true }.to raise_error Chef::Exceptions::DeprecatedFeatureError,
1019
+ /Cannot specify both default and name_property together on property x of resource chef_resource_property_spec_(\d+). Only one \(name_property\) will be obeyed./
1020
+ end
1021
+ it "emits a deprecation warning for property :x, #{name}: true, default: 10" do
1022
+ expect { resource_class.property :x, name.to_sym => true, :default => 10 }.to raise_error Chef::Exceptions::DeprecatedFeatureError,
1023
+ /Cannot specify both default and name_property together on property x of resource chef_resource_property_spec_(\d+). Only one \(name_property\) will be obeyed./
1024
+ end
1025
+ it "emits a deprecation warning for property :x, #{name}: true, default: nil" do
1026
+ expect { resource_class.property :x, name.to_sym => true, :default => nil }.to raise_error Chef::Exceptions::DeprecatedFeatureError,
1027
+ /Cannot specify both default and name_property together on property x of resource chef_resource_property_spec_(\d+). Only one \(name_property\) will be obeyed./
1028
+ end
1029
+ end
1030
+
1031
+ context "default ordering" do
1032
+ before { Chef::Config[:treat_deprecation_warnings_as_errors] = false }
1033
+ with_property ":x, default: 10, #{name}: true" do
1034
+ it "chooses default over #{name}" do
1035
+ expect(resource.x).to eq 10
1036
+ end
1037
+ end
1038
+ with_property ":x, default: nil, #{name}: true" do
1039
+ it "chooses #{name} over default" do
1040
+ expect(resource.x).to eq 'blah'
1041
+ end
1042
+ end
1043
+ with_property ":x, #{name}: true, default: 10" do
1044
+ it "chooses #{name} over default" do
1045
+ expect(resource.x).to eq 'blah'
1046
+ end
1047
+ end
1048
+ with_property ":x, #{name}: true, default: nil" do
1049
+ it "chooses #{name} over default" do
1050
+ expect(resource.x).to eq 'blah'
1051
+ end
964
1052
  end
965
1053
  end
1054
+
1055
+ context "default ordering when #{name} is nil" do
1056
+ with_property ":x, #{name}: nil, default: 10" do
1057
+ it "chooses default" do
1058
+ expect(resource.x).to eq 10
1059
+ end
1060
+ end
1061
+ with_property ":x, default: 10, #{name}: nil" do
1062
+ it "chooses default" do
1063
+ expect(resource.x).to eq 10
1064
+ end
1065
+ end
1066
+ end
1067
+
1068
+ context "default ordering when #{name} is false" do
1069
+ with_property ":x, #{name}: false, default: 10" do
1070
+ it "chooses default" do
1071
+ expect(resource.x).to eq 10
1072
+ end
1073
+ end
1074
+ with_property ":x, default: 10, #{name}: nil" do
1075
+ it "chooses default" do
1076
+ expect(resource.x).to eq 10
1077
+ end
1078
+ end
1079
+ end
1080
+
966
1081
  end
967
1082
  end
1083
+
1084
+ it "raises an error if both name_property and name_attribute are specified" do
1085
+ expect { resource_class.property :x, :name_property => false, :name_attribute => 1 }.to raise_error ArgumentError,
1086
+ /Cannot specify both name_property and name_attribute together on property x of resource chef_resource_property_spec_(\d+)./
1087
+ expect { resource_class.property :x, :name_property => false, :name_attribute => nil }.to raise_error ArgumentError,
1088
+ /Cannot specify both name_property and name_attribute together on property x of resource chef_resource_property_spec_(\d+)./
1089
+ expect { resource_class.property :x, :name_property => false, :name_attribute => false }.to raise_error ArgumentError,
1090
+ /Cannot specify both name_property and name_attribute together on property x of resource chef_resource_property_spec_(\d+)./
1091
+ expect { resource_class.property :x, :name_property => true, :name_attribute => true }.to raise_error ArgumentError,
1092
+ /Cannot specify both name_property and name_attribute together on property x of resource chef_resource_property_spec_(\d+)./
1093
+ end
968
1094
  end