microwave 1.0.4 → 11.400.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (249) hide show
  1. data/CONTRIBUTING.md +155 -0
  2. data/README.md +89 -0
  3. data/Rakefile +2 -2
  4. data/bin/chef-apply +25 -0
  5. data/bin/chef-shell +34 -0
  6. data/bin/chef-solo +0 -2
  7. data/bin/shef +6 -5
  8. data/lib/chef.rb +2 -4
  9. data/spec/data/big_json.json +2 -1
  10. data/spec/data/big_json_plus_one.json +2 -1
  11. data/spec/data/cookbooks/chefignore +2 -0
  12. data/spec/data/cookbooks/openldap/attributes/default.rb +10 -9
  13. data/spec/data/cookbooks/openldap/attributes/smokey.rb +1 -1
  14. data/spec/data/git_bundles/sinatra-test-app-with-callback-files.gitbundle +0 -0
  15. data/spec/data/git_bundles/sinatra-test-app-with-symlinks.gitbundle +0 -0
  16. data/spec/data/git_bundles/sinatra-test-app.gitbundle +0 -0
  17. data/spec/data/lwrp/providers/inline_compiler.rb +26 -0
  18. data/spec/data/nodes/default.rb +3 -3
  19. data/spec/data/nodes/test.example.com.rb +3 -3
  20. data/spec/data/nodes/test.rb +3 -3
  21. data/spec/data/partial_one.erb +1 -0
  22. data/spec/data/run_context/cookbooks/circular-dep1/attributes/default.rb +4 -0
  23. data/spec/data/run_context/cookbooks/circular-dep1/definitions/circular_dep1_res.rb +1 -0
  24. data/spec/data/run_context/cookbooks/circular-dep1/libraries/lib.rb +2 -0
  25. data/spec/data/run_context/cookbooks/circular-dep1/metadata.rb +2 -0
  26. data/spec/data/run_context/cookbooks/circular-dep1/providers/provider.rb +1 -0
  27. data/spec/data/run_context/cookbooks/circular-dep1/recipes/default.rb +0 -0
  28. data/spec/data/run_context/cookbooks/circular-dep1/resources/resource.rb +1 -0
  29. data/spec/data/run_context/cookbooks/circular-dep2/attributes/default.rb +3 -0
  30. data/spec/data/run_context/cookbooks/circular-dep2/definitions/circular_dep2_res.rb +1 -0
  31. data/spec/data/run_context/cookbooks/circular-dep2/libraries/lib.rb +2 -0
  32. data/spec/data/run_context/cookbooks/circular-dep2/metadata.rb +2 -0
  33. data/spec/data/run_context/cookbooks/circular-dep2/providers/provider.rb +1 -0
  34. data/spec/data/run_context/cookbooks/circular-dep2/recipes/default.rb +0 -0
  35. data/spec/data/run_context/cookbooks/circular-dep2/resources/resource.rb +1 -0
  36. data/spec/data/run_context/cookbooks/dependency1/attributes/aa_first.rb +2 -0
  37. data/spec/data/run_context/cookbooks/dependency1/attributes/default.rb +2 -0
  38. data/spec/data/run_context/cookbooks/dependency1/attributes/zz_last.rb +3 -0
  39. data/spec/data/run_context/cookbooks/dependency1/definitions/dependency1_res.rb +1 -0
  40. data/spec/data/run_context/cookbooks/dependency1/libraries/lib.rb +2 -0
  41. data/spec/data/run_context/cookbooks/dependency1/providers/provider.rb +1 -0
  42. data/spec/data/run_context/cookbooks/dependency1/recipes/default.rb +0 -0
  43. data/spec/data/run_context/cookbooks/dependency1/resources/resource.rb +1 -0
  44. data/spec/data/run_context/cookbooks/dependency2/attributes/default.rb +3 -0
  45. data/spec/data/run_context/cookbooks/dependency2/definitions/dependency2_res.rb +1 -0
  46. data/spec/data/run_context/cookbooks/dependency2/libraries/lib.rb +2 -0
  47. data/spec/data/run_context/cookbooks/dependency2/providers/provider.rb +1 -0
  48. data/spec/data/run_context/cookbooks/dependency2/recipes/default.rb +0 -0
  49. data/spec/data/run_context/cookbooks/dependency2/resources/resource.rb +1 -0
  50. data/spec/data/run_context/cookbooks/no-default-attr/attributes/server.rb +3 -0
  51. data/spec/data/run_context/cookbooks/no-default-attr/definitions/no_default-attr_res.rb +1 -0
  52. data/spec/data/run_context/cookbooks/no-default-attr/providers/provider.rb +1 -0
  53. data/spec/data/run_context/cookbooks/no-default-attr/recipes/default.rb +0 -0
  54. data/spec/data/run_context/cookbooks/no-default-attr/resources/resource.rb +1 -0
  55. data/spec/data/run_context/cookbooks/test-with-circular-deps/attributes/default.rb +3 -0
  56. data/spec/data/run_context/cookbooks/test-with-circular-deps/definitions/test_with-circular-deps_res.rb +1 -0
  57. data/spec/data/run_context/cookbooks/test-with-circular-deps/libraries/lib.rb +2 -0
  58. data/spec/data/run_context/cookbooks/test-with-circular-deps/metadata.rb +2 -0
  59. data/spec/data/run_context/cookbooks/test-with-circular-deps/providers/provider.rb +1 -0
  60. data/spec/data/run_context/cookbooks/test-with-circular-deps/recipes/default.rb +0 -0
  61. data/spec/data/run_context/cookbooks/test-with-circular-deps/resources/resource.rb +1 -0
  62. data/spec/data/run_context/cookbooks/test-with-deps/attributes/default.rb +3 -0
  63. data/spec/data/run_context/cookbooks/test-with-deps/definitions/test_with-deps_res.rb +1 -0
  64. data/spec/data/run_context/cookbooks/test-with-deps/libraries/lib.rb +1 -0
  65. data/spec/data/run_context/cookbooks/test-with-deps/metadata.rb +3 -0
  66. data/spec/data/run_context/cookbooks/test-with-deps/providers/provider.rb +1 -0
  67. data/spec/data/run_context/cookbooks/test-with-deps/recipes/default.rb +0 -0
  68. data/spec/data/run_context/cookbooks/test-with-deps/recipes/server.rb +0 -0
  69. data/spec/data/run_context/cookbooks/test-with-deps/resources/resource.rb +1 -0
  70. data/spec/data/run_context/cookbooks/test/attributes/default.rb +0 -0
  71. data/spec/data/run_context/cookbooks/test/attributes/george.rb +1 -1
  72. data/spec/data/run_context/cookbooks/test/definitions/test_res.rb +1 -0
  73. data/spec/data/run_context/cookbooks/test/providers/provider.rb +1 -0
  74. data/spec/data/run_context/cookbooks/test/resources/resource.rb +1 -0
  75. data/spec/data/shef-config.rb +10 -0
  76. data/spec/functional/dsl/registry_helper_spec.rb +63 -0
  77. data/spec/functional/knife/cookbook_delete_spec.rb +0 -2
  78. data/spec/functional/knife/exec_spec.rb +4 -6
  79. data/spec/functional/knife/smoke_test.rb +34 -0
  80. data/spec/functional/knife/ssh_spec.rb +64 -3
  81. data/spec/functional/resource/cookbook_file_spec.rb +33 -2
  82. data/spec/functional/resource/deploy_revision_spec.rb +515 -0
  83. data/spec/functional/resource/directory_spec.rb +4 -0
  84. data/spec/functional/resource/file_spec.rb +56 -22
  85. data/spec/functional/resource/link_spec.rb +12 -10
  86. data/spec/functional/resource/registry_spec.rb +572 -0
  87. data/spec/functional/resource/remote_directory_spec.rb +142 -36
  88. data/spec/functional/resource/remote_file_spec.rb +28 -3
  89. data/spec/functional/resource/template_spec.rb +23 -2
  90. data/spec/functional/run_lock_spec.rb +238 -0
  91. data/spec/functional/shell_spec.rb +101 -0
  92. data/spec/functional/tiny_server_spec.rb +5 -4
  93. data/spec/functional/win32/registry_helper_spec.rb +632 -0
  94. data/spec/functional/win32/security_spec.rb +37 -0
  95. data/spec/spec_helper.rb +15 -3
  96. data/spec/stress/win32/security_spec.rb +5 -5
  97. data/spec/support/chef_helpers.rb +14 -3
  98. data/spec/support/lib/chef/resource/cat.rb +3 -5
  99. data/spec/support/lib/chef/resource/one_two_three_four.rb +8 -10
  100. data/spec/support/lib/chef/resource/zen_master.rb +8 -10
  101. data/spec/support/matchers/leak.rb +1 -1
  102. data/spec/support/platform_helpers.rb +18 -0
  103. data/spec/support/shared/functional/directory_resource.rb +85 -23
  104. data/spec/support/shared/functional/file_resource.rb +198 -53
  105. data/spec/support/shared/functional/securable_resource.rb +140 -105
  106. data/spec/support/shared/functional/securable_resource_with_reporting.rb +375 -0
  107. data/spec/support/shared/unit/file_system_support.rb +110 -0
  108. data/spec/support/shared/unit/platform_introspector.rb +162 -0
  109. data/spec/tiny_server.rb +29 -10
  110. data/spec/unit/api_client/registration_spec.rb +172 -0
  111. data/spec/unit/api_client_spec.rb +156 -103
  112. data/spec/unit/application/apply.rb +84 -0
  113. data/spec/unit/application/knife_spec.rb +5 -0
  114. data/spec/unit/application_spec.rb +57 -2
  115. data/spec/unit/chef_fs/diff_spec.rb +329 -0
  116. data/spec/unit/chef_fs/file_pattern_spec.rb +526 -0
  117. data/spec/unit/chef_fs/file_system/chef_server_root_dir_spec.rb +237 -0
  118. data/spec/unit/chef_fs/file_system/cookbooks_dir_spec.rb +568 -0
  119. data/spec/unit/chef_fs/file_system/data_bags_dir_spec.rb +220 -0
  120. data/spec/unit/chef_fs/file_system_spec.rb +136 -0
  121. data/spec/unit/client_spec.rb +188 -16
  122. data/spec/unit/config_spec.rb +54 -4
  123. data/spec/unit/cookbook/chefignore_spec.rb +2 -1
  124. data/spec/unit/cookbook/syntax_check_spec.rb +48 -109
  125. data/spec/unit/cookbook_loader_spec.rb +153 -91
  126. data/spec/unit/cookbook_manifest_spec.rb +81 -81
  127. data/spec/unit/cookbook_spec.rb +3 -20
  128. data/spec/unit/cookbook_version_spec.rb +23 -122
  129. data/spec/unit/digester_spec.rb +50 -0
  130. data/spec/unit/dsl/data_query_spec.rb +66 -0
  131. data/spec/unit/dsl/platform_introspection_spec.rb +130 -0
  132. data/spec/unit/dsl/regsitry_helper_spec.rb +55 -0
  133. data/spec/unit/encrypted_data_bag_item_spec.rb +126 -10
  134. data/spec/unit/environment_spec.rb +0 -130
  135. data/spec/unit/exceptions_spec.rb +2 -3
  136. data/spec/unit/formatters/error_inspectors/resource_failure_inspector_spec.rb +23 -3
  137. data/spec/unit/json_compat_spec.rb +69 -0
  138. data/spec/unit/knife/bootstrap_spec.rb +81 -28
  139. data/spec/unit/knife/client_reregister_spec.rb +23 -22
  140. data/spec/unit/knife/configure_spec.rb +29 -26
  141. data/spec/unit/knife/cookbook_metadata_spec.rb +11 -4
  142. data/spec/unit/knife/cookbook_site_install_spec.rb +12 -2
  143. data/spec/unit/knife/cookbook_test_spec.rb +1 -0
  144. data/spec/unit/knife/cookbook_upload_spec.rb +41 -2
  145. data/spec/unit/knife/core/bootstrap_context_spec.rb +8 -1
  146. data/spec/unit/knife/core/ui_spec.rb +156 -7
  147. data/spec/unit/knife/data_bag_create_spec.rb +14 -0
  148. data/spec/unit/knife/data_bag_edit_spec.rb +14 -4
  149. data/spec/unit/knife/data_bag_from_file_spec.rb +17 -5
  150. data/spec/unit/knife/data_bag_show_spec.rb +11 -4
  151. data/spec/unit/knife/index_rebuild_spec.rb +96 -33
  152. data/spec/unit/knife/knife_help.rb +7 -7
  153. data/spec/unit/knife/node_run_list_remove_spec.rb +2 -1
  154. data/spec/unit/knife/ssh_spec.rb +121 -15
  155. data/spec/unit/knife/status_spec.rb +2 -2
  156. data/spec/unit/knife/user_create_spec.rb +86 -0
  157. data/spec/unit/knife/user_delete_spec.rb +39 -0
  158. data/spec/unit/knife/user_edit_spec.rb +42 -0
  159. data/spec/unit/knife/user_list_spec.rb +32 -0
  160. data/spec/unit/knife/user_reregister_spec.rb +53 -0
  161. data/spec/unit/knife/user_show_spec.rb +41 -0
  162. data/spec/unit/knife_spec.rb +53 -0
  163. data/spec/unit/lwrp_spec.rb +59 -17
  164. data/spec/unit/mixin/checksum_spec.rb +2 -2
  165. data/spec/unit/mixin/deep_merge_spec.rb +56 -491
  166. data/spec/unit/mixin/deprecation_spec.rb +23 -0
  167. data/spec/unit/mixin/enforce_ownership_and_permissions_spec.rb +6 -1
  168. data/spec/unit/mixin/params_validate_spec.rb +4 -2
  169. data/spec/unit/mixin/securable_spec.rb +5 -3
  170. data/spec/unit/mixin/template_spec.rb +119 -0
  171. data/spec/unit/node/attribute_spec.rb +272 -137
  172. data/spec/unit/node/immutable_collections_spec.rb +139 -0
  173. data/spec/unit/node_spec.rb +411 -339
  174. data/spec/unit/platform_spec.rb +8 -8
  175. data/spec/unit/provider/breakpoint_spec.rb +8 -8
  176. data/spec/unit/provider/cookbook_file_spec.rb +4 -8
  177. data/spec/unit/provider/deploy/revision_spec.rb +2 -8
  178. data/spec/unit/provider/deploy_spec.rb +6 -40
  179. data/spec/unit/provider/directory_spec.rb +103 -68
  180. data/spec/unit/provider/erl_call_spec.rb +0 -2
  181. data/spec/unit/provider/file_spec.rb +69 -59
  182. data/spec/unit/provider/git_spec.rb +0 -10
  183. data/spec/unit/provider/group/groupadd_spec.rb +1 -1
  184. data/spec/unit/provider/group/usermod_spec.rb +2 -2
  185. data/spec/unit/provider/http_request_spec.rb +28 -69
  186. data/spec/unit/provider/ifconfig_spec.rb +2 -2
  187. data/spec/unit/provider/link_spec.rb +1 -1
  188. data/spec/unit/provider/ohai_spec.rb +4 -4
  189. data/spec/unit/provider/package/apt_spec.rb +0 -1
  190. data/spec/unit/provider/package/ips_spec.rb +0 -1
  191. data/spec/unit/provider/package/rubygems_spec.rb +0 -18
  192. data/spec/unit/provider/package/yum_spec.rb +79 -15
  193. data/spec/unit/provider/package_spec.rb +7 -5
  194. data/spec/unit/provider/registry_key_spec.rb +269 -0
  195. data/spec/unit/provider/remote_directory_spec.rb +24 -7
  196. data/spec/unit/provider/remote_file_spec.rb +36 -0
  197. data/spec/unit/provider/route_spec.rb +3 -6
  198. data/spec/unit/provider/ruby_block_spec.rb +8 -0
  199. data/spec/unit/provider/service/arch_service_spec.rb +4 -4
  200. data/spec/unit/provider/service/debian_service_spec.rb +1 -1
  201. data/spec/unit/provider/service/freebsd_service_spec.rb +4 -4
  202. data/spec/unit/provider/service/init_service_spec.rb +26 -3
  203. data/spec/unit/provider/service/insserv_service_spec.rb +1 -1
  204. data/spec/unit/provider/service/invokercd_service_spec.rb +3 -3
  205. data/spec/unit/provider/service/redhat_spec.rb +1 -1
  206. data/spec/unit/provider/service/simple_service_spec.rb +3 -3
  207. data/spec/unit/provider/service/upstart_service_spec.rb +7 -7
  208. data/spec/unit/provider/service_spec.rb +2 -2
  209. data/spec/unit/provider/subversion_spec.rb +1 -1
  210. data/spec/unit/provider/template_spec.rb +35 -11
  211. data/spec/unit/provider/user/dscl_spec.rb +57 -31
  212. data/spec/unit/provider/user_spec.rb +7 -16
  213. data/spec/unit/provider_spec.rb +4 -3
  214. data/spec/unit/recipe_spec.rb +10 -8
  215. data/spec/unit/registry_helper_spec.rb +376 -0
  216. data/spec/unit/resource/log_spec.rb +9 -0
  217. data/spec/unit/resource/registry_key_spec.rb +171 -0
  218. data/spec/unit/resource/remote_file_spec.rb +21 -23
  219. data/spec/unit/resource/ruby_block_spec.rb +7 -3
  220. data/spec/unit/resource/service_spec.rb +11 -0
  221. data/spec/unit/resource_spec.rb +27 -4
  222. data/spec/unit/rest/auth_credentials_spec.rb +2 -14
  223. data/spec/unit/rest_spec.rb +122 -187
  224. data/spec/unit/run_context/cookbook_compiler_spec.rb +181 -0
  225. data/spec/unit/run_context_spec.rb +18 -4
  226. data/spec/unit/run_list_spec.rb +0 -209
  227. data/spec/unit/run_lock_spec.rb +37 -0
  228. data/spec/unit/runner_spec.rb +101 -2
  229. data/spec/unit/scan_access_control_spec.rb +4 -4
  230. data/spec/unit/{shef → shell}/model_wrapper_spec.rb +5 -5
  231. data/spec/unit/{shef/shef_ext_spec.rb → shell/shell_ext_spec.rb} +21 -21
  232. data/spec/unit/{shef/shef_session_spec.rb → shell/shell_session_spec.rb} +12 -12
  233. data/spec/unit/shell_out_spec.rb +18 -0
  234. data/spec/unit/{shef_spec.rb → shell_spec.rb} +20 -20
  235. data/spec/unit/user_spec.rb +255 -0
  236. metadata +162 -157
  237. data/README.rdoc +0 -177
  238. data/spec/unit/certificate_spec.rb +0 -76
  239. data/spec/unit/checksum_cache_spec.rb +0 -209
  240. data/spec/unit/checksum_spec.rb +0 -94
  241. data/spec/unit/couchdb_spec.rb +0 -274
  242. data/spec/unit/index_queue_spec.rb +0 -391
  243. data/spec/unit/json_compat_spect.rb +0 -53
  244. data/spec/unit/mixin/language_spec.rb +0 -305
  245. data/spec/unit/openid_registration_spec.rb +0 -153
  246. data/spec/unit/solr_query/query_transform_spec.rb +0 -454
  247. data/spec/unit/solr_query/solr_http_request_spec.rb +0 -244
  248. data/spec/unit/solr_query_spec.rb +0 -203
  249. data/spec/unit/webui_user_spec.rb +0 -238
@@ -0,0 +1,101 @@
1
+ #
2
+ # Author:: Daniel DeLeo (<dan@opscode.com>)
3
+ # Copyright:: Copyright (c) 2012 Opscode, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'spec_helper'
20
+ require 'chef/version'
21
+
22
+ describe Chef::Shell do
23
+
24
+ # chef-shell's unit tests are by necessity very mock-heavy, and frequently do
25
+ # not catch cases where chef-shell fails to boot because of changes in
26
+ # chef/client.rb
27
+ describe "smoke tests", :unix_only => true do
28
+
29
+ def read_until(io, expected_value)
30
+ start = Time.new
31
+ buffer = ""
32
+ until buffer.include?(expected_value)
33
+ begin
34
+ buffer << io.read_nonblock(1)
35
+ rescue Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::EIO, EOFError
36
+ sleep 0.01
37
+ end
38
+ if Time.new - start > 30
39
+ STDERR.puts "did not read expected value `#{expected_value}' within 15s"
40
+ STDERR.puts "Buffer so far: `#{buffer}'"
41
+ break
42
+ end
43
+ end
44
+ buffer
45
+ end
46
+
47
+ def wait_or_die(pid)
48
+ start = Time.new
49
+
50
+ until exitstatus = Process.waitpid2(pid, Process::WNOHANG)
51
+ if Time.new - start > 5
52
+ STDERR.puts("chef-shell tty did not exit cleanly, killing it")
53
+ Process.kill(:KILL, pid)
54
+ end
55
+ sleep 0.01
56
+ end
57
+ exitstatus[1]
58
+ end
59
+
60
+ def run_chef_shell_with(options)
61
+ # Windows ruby installs don't (always?) have PTY,
62
+ # so hide the require here
63
+ require 'pty'
64
+ config = File.expand_path("shef-config.rb", CHEF_SPEC_DATA)
65
+ path_to_chef_shell = File.expand_path("../../../bin/chef-shell", __FILE__)
66
+ reader, writer, pid = PTY.spawn("#{path_to_chef_shell} -c #{config} #{options}")
67
+ read_until(reader, "chef >")
68
+ yield reader, writer if block_given?
69
+ writer.puts('"done"')
70
+ output = read_until(reader, '=> "done"')
71
+ writer.print("exit\n")
72
+ read_until(reader, "exit")
73
+ read_until(reader, "\n")
74
+ read_until(reader, "\n")
75
+ writer.close
76
+
77
+ exitstatus = wait_or_die(pid)
78
+
79
+ [output, exitstatus]
80
+ rescue PTY::ChildExited => e
81
+ [output, e.status]
82
+ end
83
+
84
+ it "boots correctly with -lauto" do
85
+ output, exitstatus = run_chef_shell_with("-lauto")
86
+ exitstatus.should be_success
87
+ end
88
+
89
+ it "sets the log_level from the command line" do
90
+ output, exitstatus = run_chef_shell_with("-lfatal") do |out, keyboard|
91
+ show_log_level_code = %q[puts "===#{Chef::Log.level}==="]
92
+ keyboard.puts(show_log_level_code)
93
+ read_until(out, show_log_level_code)
94
+ end
95
+ output.should include("===fatal===")
96
+ exitstatus.should be_success
97
+ end
98
+
99
+ end
100
+
101
+ end
@@ -37,15 +37,16 @@ describe TinyServer::API do
37
37
 
38
38
  it "creates a route for a GET request" do
39
39
  @api.get('/foo/bar', 200, 'hello foobar')
40
- response = @api.call("REQUEST_METHOD" => "GET", "REQUEST_URI" => '/foo/bar')
41
- response.should == [200, {'Content-Type' => 'application/json'}, 'hello foobar']
40
+ # WEBrick gives you the full URI with host, Thin only gave the part after scheme+host+port
41
+ response = @api.call("REQUEST_METHOD" => "GET", "REQUEST_URI" => 'http://localhost:1974/foo/bar')
42
+ response.should == [200, {'Content-Type' => 'application/json'}, [ 'hello foobar' ]]
42
43
  end
43
44
 
44
45
  it "creates a route for a request with a block" do
45
46
  block_called = false
46
47
  @api.get('/bar/baz', 200) { block_called = true; 'hello barbaz' }
47
- response = @api.call("REQUEST_METHOD" => "GET", "REQUEST_URI" => '/bar/baz')
48
- response.should == [200, {'Content-Type' => 'application/json'}, 'hello barbaz']
48
+ response = @api.call("REQUEST_METHOD" => "GET", "REQUEST_URI" => 'http://localhost:1974/bar/baz')
49
+ response.should == [200, {'Content-Type' => 'application/json'}, [ 'hello barbaz' ]]
49
50
  block_called.should be_true
50
51
  end
51
52
 
@@ -0,0 +1,632 @@
1
+ #
2
+ # Author:: Prajakta Purohit (<prajakta@opscode.com>)
3
+ # Author:: Lamont Granquist (<lamont@opscode.com>)
4
+ # Copyright:: Copyright (c) 2012 Opscode, Inc.
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ require 'spec_helper'
21
+ require 'chef/win32/registry'
22
+
23
+ describe Chef::Resource::RegistryKey, :unix_only do
24
+ before(:all) do
25
+ events = Chef::EventDispatch::Dispatcher.new
26
+ node = Chef::Node.new
27
+ ohai = Ohai::System.new
28
+ ohai.all_plugins
29
+ node.consume_external_attrs(ohai.data,{})
30
+ run_context = Chef::RunContext.new(node, {}, events)
31
+ @resource = Chef::Resource::RegistryKey.new("HKCU\\Software", run_context)
32
+ end
33
+ context "when load_current_resource is run on a non-windows node" do
34
+ it "throws an exception because you don't have a windows registry (derp)" do
35
+ @resource.key("HKCU\\Software\\Opscode")
36
+ @resource.values([{:name=>"Color", :type=>:string, :data=>"Orange"}])
37
+ lambda{@resource.run_action(:create)}.should raise_error(Chef::Exceptions::Win32NotWindows)
38
+ end
39
+ end
40
+ end
41
+
42
+ describe 'Chef::Win32::Registry', :windows_only do
43
+
44
+ before(:all) do
45
+ #Create a registry item
46
+ ::Win32::Registry::HKEY_CURRENT_USER.create "Software\\Root"
47
+ ::Win32::Registry::HKEY_CURRENT_USER.create "Software\\Root\\Branch"
48
+ ::Win32::Registry::HKEY_CURRENT_USER.create "Software\\Root\\Branch\\Flower"
49
+ ::Win32::Registry::HKEY_CURRENT_USER.open('Software\\Root', Win32::Registry::KEY_ALL_ACCESS) do |reg|
50
+ reg['RootType1', Win32::Registry::REG_SZ] = 'fibrous'
51
+ reg.write('Roots', Win32::Registry::REG_MULTI_SZ, ["strong roots", "healthy tree"])
52
+ end
53
+ ::Win32::Registry::HKEY_CURRENT_USER.open('Software\\Root\\Branch', Win32::Registry::KEY_ALL_ACCESS) do |reg|
54
+ reg['Strong', Win32::Registry::REG_SZ] = 'bird nest'
55
+ end
56
+ ::Win32::Registry::HKEY_CURRENT_USER.open('Software\\Root\\Branch\\Flower', Win32::Registry::KEY_ALL_ACCESS) do |reg|
57
+ reg['Petals', Win32::Registry::REG_MULTI_SZ] = ["Pink", "Delicate"]
58
+ end
59
+
60
+ #Create the node with ohai data
61
+ events = Chef::EventDispatch::Dispatcher.new
62
+ @node = Chef::Node.new
63
+ ohai = Ohai::System.new
64
+ ohai.all_plugins
65
+ @node.consume_external_attrs(ohai.data,{})
66
+ @run_context = Chef::RunContext.new(@node, {}, events)
67
+
68
+ #Create a registry object that has access ot the node previously created
69
+ @registry = Chef::Win32::Registry.new(@run_context)
70
+ end
71
+
72
+ #Delete what is left of the registry key-values previously created
73
+ after(:all) do
74
+ ::Win32::Registry::HKEY_CURRENT_USER.open("Software") do |reg|
75
+ reg.delete_key("Root", true)
76
+ end
77
+ end
78
+
79
+ # Server Versions
80
+ # it "succeeds if server versiion is 2003R2, 2008, 2008R2, 2012" do
81
+ # end
82
+ # it "falis if the server versions are anything else" do
83
+ # end
84
+
85
+ describe "hive_exists?" do
86
+ it "returns true if the hive exists" do
87
+ @registry.hive_exists?("HKCU\\Software\\Root").should == true
88
+ end
89
+
90
+ it "returns false if the hive does not exist" do
91
+ hive = @registry.hive_exists?("LYRU\\Software\\Root").should == false
92
+ end
93
+ end
94
+
95
+ describe "key_exists?" do
96
+ it "returns true if the key path exists" do
97
+ @registry.key_exists?("HKCU\\Software\\Root\\Branch\\Flower").should == true
98
+ end
99
+
100
+ it "returns false if the key path does not exist" do
101
+ @registry.key_exists?("HKCU\\Software\\Branch\\Flower").should == false
102
+ end
103
+
104
+ it "throws an exception if the hive does not exist" do
105
+ lambda {@registry.key_exists?("JKLM\\Software\\Branch\\Flower")}.should raise_error(Chef::Exceptions::Win32RegHiveMissing)
106
+ end
107
+ end
108
+
109
+ describe "key_exists!" do
110
+ it "returns true if the key path exists" do
111
+ @registry.key_exists!("HKCU\\Software\\Root\\Branch\\Flower").should == true
112
+ end
113
+
114
+ it "throws an exception if the key path does not exist" do
115
+ lambda {@registry.key_exists!("HKCU\\Software\\Branch\\Flower")}.should raise_error(Chef::Exceptions::Win32RegKeyMissing)
116
+ end
117
+
118
+ it "throws an exception if the hive does not exist" do
119
+ lambda {@registry.key_exists!("JKLM\\Software\\Branch\\Flower")}.should raise_error(Chef::Exceptions::Win32RegHiveMissing)
120
+ end
121
+ end
122
+
123
+ describe "value_exists?" do
124
+ it "throws an exception if the hive does not exist" do
125
+ lambda {@registry.value_exists?("JKLM\\Software\\Branch\\Flower", {:name=>"Petals"})}.should raise_error(Chef::Exceptions::Win32RegHiveMissing)
126
+ end
127
+ it "throws an exception if the key does not exist" do
128
+ lambda {@registry.value_exists?("HKCU\\Software\\Branch\\Flower", {:name=>"Petals"})}.should raise_error(Chef::Exceptions::Win32RegKeyMissing)
129
+ end
130
+ it "returns true if the value exists" do
131
+ @registry.value_exists?("HKCU\\Software\\Root\\Branch\\Flower", {:name=>"Petals"}).should == true
132
+ end
133
+ it "returns false if the value does not exist" do
134
+ @registry.value_exists?("HKCU\\Software\\Root\\Branch\\Flower", {:name=>"FOOBAR"}).should == false
135
+ end
136
+ end
137
+
138
+ describe "value_exists!" do
139
+ it "throws an exception if the hive does not exist" do
140
+ lambda {@registry.value_exists!("JKLM\\Software\\Branch\\Flower", {:name=>"Petals"})}.should raise_error(Chef::Exceptions::Win32RegHiveMissing)
141
+ end
142
+ it "throws an exception if the key does not exist" do
143
+ lambda {@registry.value_exists!("HKCU\\Software\\Branch\\Flower", {:name=>"Petals"})}.should raise_error(Chef::Exceptions::Win32RegKeyMissing)
144
+ end
145
+ it "returns true if the value exists" do
146
+ @registry.value_exists!("HKCU\\Software\\Root\\Branch\\Flower", {:name=>"Petals"}).should == true
147
+ end
148
+ it "throws an exception if the value does not exist" do
149
+ lambda {@registry.value_exists!("HKCU\\Software\\Root\\Branch\\Flower", {:name=>"FOOBAR"})}.should raise_error(Chef::Exceptions::Win32RegValueMissing)
150
+ end
151
+ end
152
+
153
+ describe "data_exists?" do
154
+ it "throws an exception if the hive does not exist" do
155
+ lambda {@registry.data_exists?("JKLM\\Software\\Branch\\Flower", {:name=>"Petals", :type=>:multi_string, :data=>["Pink", "Delicate"]})}.should raise_error(Chef::Exceptions::Win32RegHiveMissing)
156
+ end
157
+ it "throws an exception if the key does not exist" do
158
+ lambda {@registry.data_exists?("HKCU\\Software\\Branch\\Flower", {:name=>"Petals", :type=>:multi_string, :data=>["Pink", "Delicate"]})}.should raise_error(Chef::Exceptions::Win32RegKeyMissing)
159
+ end
160
+ it "returns true if all the data matches" do
161
+ @registry.data_exists?("HKCU\\Software\\Root\\Branch\\Flower", {:name=>"Petals", :type=>:multi_string, :data=>["Pink", "Delicate"]}).should == true
162
+ end
163
+ it "returns false if the name does not exist" do
164
+ @registry.data_exists?("HKCU\\Software\\Root\\Branch\\Flower", {:name=>"slateP", :type=>:multi_string, :data=>["Pink", "Delicate"]}).should == false
165
+ end
166
+ it "returns false if the types do not match" do
167
+ @registry.data_exists?("HKCU\\Software\\Root\\Branch\\Flower", {:name=>"Petals", :type=>:string, :data=>"Pink"}).should == false
168
+ end
169
+ it "returns false if the data does not match" do
170
+ @registry.data_exists?("HKCU\\Software\\Root\\Branch\\Flower", {:name=>"Petals", :type=>:multi_string, :data=>["Mauve", "Delicate"]}).should == false
171
+ end
172
+ end
173
+
174
+ describe "data_exists!" do
175
+ it "throws an exception if the hive does not exist" do
176
+ lambda {@registry.data_exists!("JKLM\\Software\\Branch\\Flower", {:name=>"Petals", :type=>:multi_string, :data=>["Pink", "Delicate"]})}.should raise_error(Chef::Exceptions::Win32RegHiveMissing)
177
+ end
178
+ it "throws an exception if the key does not exist" do
179
+ lambda {@registry.data_exists!("HKCU\\Software\\Branch\\Flower", {:name=>"Petals", :type=>:multi_string, :data=>["Pink", "Delicate"]})}.should raise_error(Chef::Exceptions::Win32RegKeyMissing)
180
+ end
181
+ it "returns true if all the data matches" do
182
+ @registry.data_exists!("HKCU\\Software\\Root\\Branch\\Flower", {:name=>"Petals", :type=>:multi_string, :data=>["Pink", "Delicate"]}).should == true
183
+ end
184
+ it "throws an exception if the name does not exist" do
185
+ lambda {@registry.data_exists!("HKCU\\Software\\Root\\Branch\\Flower", {:name=>"slateP", :type=>:multi_string, :data=>["Pink", "Delicate"]})}.should raise_error(Chef::Exceptions::Win32RegDataMissing)
186
+ end
187
+ it "throws an exception if the types do not match" do
188
+ lambda {@registry.data_exists!("HKCU\\Software\\Root\\Branch\\Flower", {:name=>"Petals", :type=>:string, :data=>"Pink"})}.should raise_error(Chef::Exceptions::Win32RegDataMissing)
189
+ end
190
+ it "throws an exception if the data does not match" do
191
+ lambda {@registry.data_exists!("HKCU\\Software\\Root\\Branch\\Flower", {:name=>"Petals", :type=>:multi_string, :data=>["Mauve", "Delicate"]})}.should raise_error(Chef::Exceptions::Win32RegDataMissing)
192
+ end
193
+ end
194
+
195
+ describe "get_values" do
196
+ it "returns all values for a key if it exists" do
197
+ values = @registry.get_values("HKCU\\Software\\Root")
198
+ values.should be_an_instance_of Array
199
+ values.should == [{:name=>"RootType1", :type=>:string, :data=>"fibrous"},
200
+ {:name=>"Roots", :type=>:multi_string, :data=>["strong roots", "healthy tree"]}]
201
+ end
202
+
203
+ it "throws an exception if the key does not exist" do
204
+ lambda {@registry.get_values("HKCU\\Software\\Branch\\Flower")}.should raise_error(Chef::Exceptions::Win32RegKeyMissing)
205
+ end
206
+
207
+ it "throws an exception if the hive does not exist" do
208
+ lambda {@registry.get_values("JKLM\\Software\\Branch\\Flower")}.should raise_error(Chef::Exceptions::Win32RegHiveMissing)
209
+ end
210
+ end
211
+
212
+ describe "set_value" do
213
+ it "updates a value if the key, value exist and type matches and value different" do
214
+ @registry.set_value("HKCU\\Software\\Root\\Branch\\Flower", {:name=>"Petals", :type=>:multi_string, :data=>["Yellow", "Changed Color"]}).should == true
215
+ @registry.data_exists?("HKCU\\Software\\Root\\Branch\\Flower", {:name=>"Petals", :type=>:multi_string, :data=>["Yellow", "Changed Color"]}).should == true
216
+ end
217
+
218
+ it "updates a value if the type does match and the values are different" do
219
+ @registry.set_value("HKCU\\Software\\Root\\Branch\\Flower", {:name=>"Petals", :type=>:string, :data=>"Yellow"}).should == true
220
+ @registry.data_exists?("HKCU\\Software\\Root\\Branch\\Flower", {:name=>"Petals", :type=>:string, :data=>"Yellow"}).should == true
221
+ @registry.data_exists?("HKCU\\Software\\Root\\Branch\\Flower", {:name=>"Petals", :type=>:multi_string, :data=>["Yellow", "Changed Color"]}).should == false
222
+ end
223
+
224
+ it "creates a value if key exists and value does not" do
225
+ @registry.set_value("HKCU\\Software\\Root\\Branch\\Flower", {:name=>"Stamen", :type=>:multi_string, :data=>["Yellow", "Changed Color"]}).should == true
226
+ @registry.data_exists?("HKCU\\Software\\Root\\Branch\\Flower", {:name=>"Stamen", :type=>:multi_string, :data=>["Yellow", "Changed Color"]}).should == true
227
+ end
228
+
229
+ it "does nothing if data,type and name parameters for the value are same" do
230
+ @registry.set_value("HKCU\\Software\\Root\\Branch\\Flower", {:name=>"Stamen", :type=>:multi_string, :data=>["Yellow", "Changed Color"]}).should == false
231
+ @registry.data_exists?("HKCU\\Software\\Root\\Branch\\Flower", {:name=>"Stamen", :type=>:multi_string, :data=>["Yellow", "Changed Color"]}).should == true
232
+ end
233
+
234
+ it "throws an exception if the key does not exist" do
235
+ lambda {@registry.set_value("HKCU\\Software\\Branch\\Flower", {:name=>"Petals", :type=>:multi_string, :data=>["Yellow", "Changed Color"]})}.should raise_error(Chef::Exceptions::Win32RegKeyMissing)
236
+ end
237
+
238
+ it "throws an exception if the hive does not exist" do
239
+ lambda {@registry.set_value("JKLM\\Software\\Root\\Branch\\Flower", {:name=>"Petals", :type=>:multi_string, :data=>["Yellow", "Changed Color"]})}.should raise_error(Chef::Exceptions::Win32RegHiveMissing)
240
+ end
241
+
242
+ # we are validating that the data gets .to_i called on it when type is a :dword
243
+
244
+ it "casts an integer string given as a dword into an integer" do
245
+ @registry.set_value("HKCU\\Software\\Root\\Branch\\Flower", {:name=>"ShouldBe32767", :type=>:dword, :data=>"32767"}).should == true
246
+ @registry.data_exists?("HKCU\\Software\\Root\\Branch\\Flower", {:name=>"ShouldBe32767", :type=>:dword, :data=>32767}).should == true
247
+ end
248
+
249
+ it "casts a nonsense string given as a dword into zero" do
250
+ @registry.set_value("HKCU\\Software\\Root\\Branch\\Flower", {:name=>"ShouldBeZero", :type=>:dword, :data=>"whatdoesthisdo"}).should == true
251
+ @registry.data_exists?("HKCU\\Software\\Root\\Branch\\Flower", {:name=>"ShouldBeZero", :type=>:dword, :data=>0}).should == true
252
+ end
253
+
254
+ it "throws an exception when trying to cast an array to an int for a dword" do
255
+ lambda {@registry.set_value("HKCU\\Software\\Root\\Branch\\Flower", {:name=>"ShouldThrow", :type=>:dword, :data=>["one","two"]})}.should raise_error
256
+ end
257
+
258
+ # we are validating that the data gets .to_s called on it when type is a :string
259
+
260
+ it "stores the string representation of an array into a string if you pass it an array" do
261
+ @registry.set_value("HKCU\\Software\\Root\\Branch\\Flower", {:name=>"ShouldBePainful", :type=>:string, :data=>["one","two"]}).should == true
262
+ @registry.data_exists?("HKCU\\Software\\Root\\Branch\\Flower", {:name=>"ShouldBePainful", :type=>:string, :data=>'["one", "two"]'}).should == true
263
+ end
264
+
265
+ it "stores the string representation of a number into a string if you pass it an number" do
266
+ @registry.set_value("HKCU\\Software\\Root\\Branch\\Flower", {:name=>"ShouldBe65535", :type=>:string, :data=>65535}).should == true
267
+ @registry.data_exists?("HKCU\\Software\\Root\\Branch\\Flower", {:name=>"ShouldBe65535", :type=>:string, :data=>"65535"}).should == true
268
+ end
269
+
270
+ # we are validating that the data gets .to_a called on it when type is a :multi_string
271
+
272
+ it "throws an exception when a multi-string is passed a number" do
273
+ lambda {@registry.set_value("HKCU\\Software\\Root\\Branch\\Flower", {:name=>"ShouldThrow", :type=>:multi_string, :data=>65535})}.should raise_error
274
+ end
275
+
276
+ it "throws an exception when a multi-string is passed a string" do
277
+ lambda {@registry.set_value("HKCU\\Software\\Root\\Branch\\Flower", {:name=>"ShouldBeWat", :type=>:multi_string, :data=>"foo"})}.should raise_error
278
+ end
279
+ end
280
+
281
+ describe "create_key" do
282
+ before(:all) do
283
+ ::Win32::Registry::HKEY_CURRENT_USER.open("Software\\Root") do |reg|
284
+ begin
285
+ reg.delete_key("Trunk", true)
286
+ rescue
287
+ end
288
+ end
289
+ end
290
+
291
+ it "throws an exception if the path has missing keys but recursive set to false" do
292
+ lambda {@registry.create_key("HKCU\\Software\\Root\\Trunk\\Peck\\Woodpecker", false)}.should raise_error(Chef::Exceptions::Win32RegNoRecursive)
293
+ @registry.key_exists?("HKCU\\Software\\Root\\Trunk\\Peck\\Woodpecker").should == false
294
+ end
295
+
296
+ it "creates the key_path if the keys were missing but recursive was set to true" do
297
+ @registry.create_key("HKCU\\Software\\Root\\Trunk\\Peck\\Woodpecker", true).should == true
298
+ @registry.key_exists?("HKCU\\Software\\Root\\Trunk\\Peck\\Woodpecker").should == true
299
+ end
300
+
301
+ it "does nothing if the key already exists" do
302
+ @registry.create_key("HKCU\\Software\\Root\\Trunk\\Peck\\Woodpecker", false).should == true
303
+ @registry.key_exists?("HKCU\\Software\\Root\\Trunk\\Peck\\Woodpecker").should == true
304
+ end
305
+
306
+ it "throws an exception of the hive does not exist" do
307
+ lambda {@registry.create_key("JKLM\\Software\\Root\\Trunk\\Peck\\Woodpecker", false)}.should raise_error(Chef::Exceptions::Win32RegHiveMissing)
308
+ end
309
+ end
310
+
311
+ describe "delete_value" do
312
+ before(:all) do
313
+ ::Win32::Registry::HKEY_CURRENT_USER.create "Software\\Root\\Trunk\\Peck\\Woodpecker"
314
+ ::Win32::Registry::HKEY_CURRENT_USER.open('Software\\Root\\Trunk\\Peck\\Woodpecker', Win32::Registry::KEY_ALL_ACCESS) do |reg|
315
+ reg['Peter', Win32::Registry::REG_SZ] = 'Tiny'
316
+ end
317
+ end
318
+
319
+ it "deletes values if the value exists" do
320
+ @registry.delete_value("HKCU\\Software\\Root\\Trunk\\Peck\\Woodpecker", {:name=>"Peter", :type=>:string, :data=>"Tiny"}).should == true
321
+ @registry.value_exists?("HKCU\\Software\\Root\\Trunk\\Peck\\Woodpecker", {:name=>"Peter", :type=>:string, :data=>"Tiny"}).should == false
322
+ end
323
+
324
+ it "does nothing if value does not exist" do
325
+ @registry.delete_value("HKCU\\Software\\Root\\Trunk\\Peck\\Woodpecker", {:name=>"Peter", :type=>:string, :data=>"Tiny"}).should == true
326
+ @registry.value_exists?("HKCU\\Software\\Root\\Trunk\\Peck\\Woodpecker", {:name=>"Peter", :type=>:string, :data=>"Tiny"}).should == false
327
+ end
328
+
329
+ it "throws an exception if the key does not exist?" do
330
+ lambda {@registry.delete_value("HKCU\\Software\\Trunk\\Peck\\Woodpecker", {:name=>"Peter", :type=>:string, :data=>"Tiny"})}.should raise_error(Chef::Exceptions::Win32RegKeyMissing)
331
+ end
332
+
333
+ it "throws an exception if the hive does not exist" do
334
+ lambda {@registry.delete_value("JKLM\\Software\\Root\\Trunk\\Peck\\Woodpecker", {:name=>"Peter", :type=>:string, :data=>"Tiny"})}.should raise_error(Chef::Exceptions::Win32RegHiveMissing)
335
+ end
336
+ end
337
+
338
+ describe "delete_key" do
339
+ before (:all) do
340
+ ::Win32::Registry::HKEY_CURRENT_USER.create "Software\\Root\\Branch\\Fruit"
341
+ ::Win32::Registry::HKEY_CURRENT_USER.open('Software\\Root\\Branch\\Fruit', Win32::Registry::KEY_ALL_ACCESS) do |reg|
342
+ reg['Apple', Win32::Registry::REG_MULTI_SZ] = ["Red", "Juicy"]
343
+ end
344
+ ::Win32::Registry::HKEY_CURRENT_USER.create "Software\\Root\\Trunk\\Peck\\Woodpecker"
345
+ ::Win32::Registry::HKEY_CURRENT_USER.open('Software\\Root\\Trunk\\Peck\\Woodpecker', Win32::Registry::KEY_ALL_ACCESS) do |reg|
346
+ reg['Peter', Win32::Registry::REG_SZ] = 'Tiny'
347
+ end
348
+ end
349
+
350
+ it "deletes a key if it has no subkeys" do
351
+ @registry.delete_key("HKCU\\Software\\Root\\Branch\\Fruit", false).should == true
352
+ @registry.key_exists?("HKCU\\Software\\Root\\Branch\\Fruit").should == false
353
+ end
354
+
355
+ it "throws an exception if key to delete has subkeys and recursive is false" do
356
+ lambda { @registry.delete_key("HKCU\\Software\\Root\\Trunk", false) }.should raise_error(Chef::Exceptions::Win32RegNoRecursive)
357
+ @registry.key_exists?("HKCU\\Software\\Root\\Trunk\\Peck\\Woodpecker").should == true
358
+ end
359
+
360
+ it "deletes a key if it has subkeys and recursive true" do
361
+ @registry.delete_key("HKCU\\Software\\Root\\Trunk", true).should == true
362
+ @registry.key_exists?("HKCU\\Software\\Root\\Trunk").should == false
363
+ end
364
+
365
+ it "does nothing if the key does not exist" do
366
+ @registry.delete_key("HKCU\\Software\\Root\\Trunk", true).should == true
367
+ @registry.key_exists?("HKCU\\Software\\Root\\Trunk").should == false
368
+ end
369
+
370
+ it "throws an exception if the hive does not exist" do
371
+ lambda {@registry.delete_key("JKLM\\Software\\Root\\Branch\\Flower", false)}.should raise_error(Chef::Exceptions::Win32RegHiveMissing)
372
+ end
373
+ end
374
+
375
+ describe "has_subkeys?" do
376
+ before(:all) do
377
+ ::Win32::Registry::HKEY_CURRENT_USER.create "Software\\Root\\Trunk"
378
+ ::Win32::Registry::HKEY_CURRENT_USER.open("Software\\Root\\Trunk") do |reg|
379
+ begin
380
+ reg.delete_key("Red", true)
381
+ rescue
382
+ end
383
+ end
384
+ end
385
+
386
+ it "throws an exception if the hive was missing" do
387
+ lambda {@registry.has_subkeys?("LMNO\\Software\\Root")}.should raise_error(Chef::Exceptions::Win32RegHiveMissing)
388
+ end
389
+
390
+ it "throws an exception if the key is missing" do
391
+ lambda {@registry.has_subkeys?("HKCU\\Software\\Root\\Trunk\\Red")}.should raise_error(Chef::Exceptions::Win32RegKeyMissing)
392
+ end
393
+
394
+ it "returns true if the key has subkeys" do
395
+ @registry.has_subkeys?("HKCU\\Software\\Root").should == true
396
+ end
397
+
398
+ it "returns false if the key has no subkeys" do
399
+ ::Win32::Registry::HKEY_CURRENT_USER.create "Software\\Root\\Trunk\\Red"
400
+ @registry.has_subkeys?("HKCU\\Software\\Root\\Trunk\\Red").should == false
401
+ end
402
+ end
403
+
404
+ describe "get_subkeys" do
405
+ it "throws an exception if the key is missing" do
406
+ lambda {@registry.get_subkeys("HKCU\\Software\\Trunk\\Red")}.should raise_error(Chef::Exceptions::Win32RegKeyMissing)
407
+ end
408
+ it "throws an exception if the hive does not exist" do
409
+ lambda {@registry.get_subkeys("JKLM\\Software\\Root")}.should raise_error(Chef::Exceptions::Win32RegHiveMissing)
410
+ end
411
+ it "returns the array of subkeys for a given key" do
412
+ subkeys = @registry.get_subkeys("HKCU\\Software\\Root")
413
+ reg_subkeys = []
414
+ ::Win32::Registry::HKEY_CURRENT_USER.open("Software\\Root", Win32::Registry::KEY_ALL_ACCESS) do |reg|
415
+ reg.each_key{|name| reg_subkeys << name}
416
+ end
417
+ reg_subkeys.should == subkeys
418
+ end
419
+ end
420
+
421
+ describe "architecture" do
422
+ describe "on 32-bit" do
423
+ before(:all) do
424
+ @saved_kernel_machine = @node.automatic_attrs[:kernel][:machine]
425
+ @node.automatic_attrs[:kernel][:machine] = :i386
426
+ end
427
+
428
+ after(:all) do
429
+ @node.automatic_attrs[:kernel][:machine] = @saved_kernel_machine
430
+ end
431
+
432
+ context "registry constructor" do
433
+ it "throws an exception if requested architecture is 64bit but running on 32bit" do
434
+ lambda {Chef::Win32::Registry.new(@run_context, :x86_64)}.should raise_error(Chef::Exceptions::Win32RegArchitectureIncorrect)
435
+ end
436
+
437
+ it "can correctly set the requested architecture to 32-bit" do
438
+ @r = Chef::Win32::Registry.new(@run_context, :i386)
439
+ @r.architecture.should == :i386
440
+ @r.registry_system_architecture.should == 0x0200
441
+ end
442
+
443
+ it "can correctly set the requested architecture to :machine" do
444
+ @r = Chef::Win32::Registry.new(@run_context, :machine)
445
+ @r.architecture.should == :machine
446
+ @r.registry_system_architecture.should == 0x0200
447
+ end
448
+ end
449
+
450
+ context "architecture setter" do
451
+ it "throws an exception if requested architecture is 64bit but running on 32bit" do
452
+ lambda {@registry.architecture = :x86_64}.should raise_error(Chef::Exceptions::Win32RegArchitectureIncorrect)
453
+ end
454
+
455
+ it "sets the requested architecture to :machine if passed :machine" do
456
+ @registry.architecture = :machine
457
+ @registry.architecture.should == :machine
458
+ @registry.registry_system_architecture.should == 0x0200
459
+ end
460
+
461
+ it "sets the requested architecture to 32-bit if passed i386 as a string" do
462
+ @registry.architecture = :i386
463
+ @registry.architecture.should == :i386
464
+ @registry.registry_system_architecture.should == 0x0200
465
+ end
466
+ end
467
+ end
468
+
469
+ describe "on 64-bit" do
470
+ before(:all) do
471
+ @saved_kernel_machine = @node.automatic_attrs[:kernel][:machine]
472
+ @node.automatic_attrs[:kernel][:machine] = :x86_64
473
+ end
474
+
475
+ after(:all) do
476
+ @node.automatic_attrs[:kernel][:machine] = @saved_kernel_machine
477
+ end
478
+
479
+ context "registry constructor" do
480
+ it "can correctly set the requested architecture to 32-bit" do
481
+ @r = Chef::Win32::Registry.new(@run_context, :i386)
482
+ @r.architecture.should == :i386
483
+ @r.registry_system_architecture.should == 0x0200
484
+ end
485
+
486
+ it "can correctly set the requested architecture to 64-bit" do
487
+ @r = Chef::Win32::Registry.new(@run_context, :x86_64)
488
+ @r.architecture.should == :x86_64
489
+ @r.registry_system_architecture.should == 0x0100
490
+ end
491
+
492
+ it "can correctly set the requested architecture to :machine" do
493
+ @r = Chef::Win32::Registry.new(@run_context, :machine)
494
+ @r.architecture.should == :machine
495
+ @r.registry_system_architecture.should == 0x0100
496
+ end
497
+ end
498
+
499
+ context "architecture setter" do
500
+ it "sets the requested architecture to 64-bit if passed 64-bit" do
501
+ @registry.architecture = :x86_64
502
+ @registry.architecture.should == :x86_64
503
+ @registry.registry_system_architecture.should == 0x0100
504
+ end
505
+
506
+ it "sets the requested architecture to :machine if passed :machine" do
507
+ @registry.architecture = :machine
508
+ @registry.architecture.should == :machine
509
+ @registry.registry_system_architecture.should == 0x0100
510
+ end
511
+
512
+ it "sets the requested architecture to 32-bit if passed 32-bit" do
513
+ @registry.architecture = :i386
514
+ @registry.architecture.should == :i386
515
+ @registry.registry_system_architecture.should == 0x0200
516
+ end
517
+ end
518
+ end
519
+
520
+ describe "when running on an actual 64-bit server", :windows64_only do
521
+ before(:all) do
522
+ begin
523
+ ::Win32::Registry::HKEY_LOCAL_MACHINE.open("Software\\Root", ::Win32::Registry::KEY_ALL_ACCESS | 0x0100) do |reg|
524
+ reg.delete_key("Trunk", true)
525
+ end
526
+ rescue
527
+ end
528
+ begin
529
+ ::Win32::Registry::HKEY_LOCAL_MACHINE.open("Software\\Root", ::Win32::Registry::KEY_ALL_ACCESS | 0x0200) do |reg|
530
+ reg.delete_key("Trunk", true)
531
+ end
532
+ rescue
533
+ end
534
+ # 64-bit
535
+ ::Win32::Registry::HKEY_LOCAL_MACHINE.create("Software\\Root\\Mauve", ::Win32::Registry::KEY_ALL_ACCESS | 0x0100)
536
+ ::Win32::Registry::HKEY_LOCAL_MACHINE.open('Software\\Root\\Mauve', Win32::Registry::KEY_ALL_ACCESS | 0x0100) do |reg|
537
+ reg['Alert', Win32::Registry::REG_SZ] = 'Universal'
538
+ end
539
+ # 32-bit
540
+ ::Win32::Registry::HKEY_LOCAL_MACHINE.create("Software\\Root\\Poosh", ::Win32::Registry::KEY_ALL_ACCESS | 0x0200)
541
+ ::Win32::Registry::HKEY_LOCAL_MACHINE.open('Software\\Root\\Poosh', Win32::Registry::KEY_ALL_ACCESS | 0x0200) do |reg|
542
+ reg['Status', Win32::Registry::REG_SZ] = 'Lost'
543
+ end
544
+ end
545
+
546
+ after(:all) do
547
+ ::Win32::Registry::HKEY_LOCAL_MACHINE.open("Software\\Root", ::Win32::Registry::KEY_ALL_ACCESS | 0x0100) do |reg|
548
+ reg.delete_key("Trunk", true)
549
+ end
550
+ ::Win32::Registry::HKEY_LOCAL_MACHINE.open("Software\\Root", ::Win32::Registry::KEY_ALL_ACCESS | 0x0200) do |reg|
551
+ reg.delete_key("Trunk", true)
552
+ end
553
+ end
554
+
555
+ describe "key_exists?" do
556
+ it "does not find 64-bit keys in the 32-bit registry" do
557
+ @registry.architecture=:i386
558
+ @registry.key_exists?("HKLM\\Software\\Root\\Mauve").should == false
559
+ end
560
+ it "finds 32-bit keys in the 32-bit registry" do
561
+ @registry.architecture=:i386
562
+ @registry.key_exists?("HKLM\\Software\\Root\\Poosh").should == true
563
+ end
564
+ it "does not find 32-bit keys in the 64-bit registry" do
565
+ @registry.architecture=:x86_64
566
+ @registry.key_exists?("HKLM\\Software\\Root\\Mauve").should == true
567
+ end
568
+ it "finds 64-bit keys in the 64-bit registry" do
569
+ @registry.architecture=:x86_64
570
+ @registry.key_exists?("HKLM\\Software\\Root\\Poosh").should == false
571
+ end
572
+ end
573
+
574
+ describe "value_exists?" do
575
+ it "does not find 64-bit values in the 32-bit registry" do
576
+ @registry.architecture=:i386
577
+ lambda{@registry.value_exists?("HKLM\\Software\\Root\\Mauve", {:name=>"Alert"})}.should raise_error(Chef::Exceptions::Win32RegKeyMissing)
578
+ end
579
+ it "finds 32-bit values in the 32-bit registry" do
580
+ @registry.architecture=:i386
581
+ @registry.value_exists?("HKLM\\Software\\Root\\Poosh", {:name=>"Status"}).should == true
582
+ end
583
+ it "does not find 32-bit values in the 64-bit registry" do
584
+ @registry.architecture=:x86_64
585
+ @registry.value_exists?("HKLM\\Software\\Root\\Mauve", {:name=>"Alert"}).should == true
586
+ end
587
+ it "finds 64-bit values in the 64-bit registry" do
588
+ @registry.architecture=:x86_64
589
+ lambda{@registry.value_exists?("HKLM\\Software\\Root\\Poosh", {:name=>"Status"})}.should raise_error(Chef::Exceptions::Win32RegKeyMissing)
590
+ end
591
+ end
592
+
593
+ describe "data_exists?" do
594
+ it "does not find 64-bit keys in the 32-bit registry" do
595
+ @registry.architecture=:i386
596
+ lambda{@registry.data_exists?("HKLM\\Software\\Root\\Mauve", {:name=>"Alert", :type=>:string, :data=>"Universal"})}.should raise_error(Chef::Exceptions::Win32RegKeyMissing)
597
+ end
598
+ it "finds 32-bit keys in the 32-bit registry" do
599
+ @registry.architecture=:i386
600
+ @registry.data_exists?("HKLM\\Software\\Root\\Poosh", {:name=>"Status", :type=>:string, :data=>"Lost"}).should == true
601
+ end
602
+ it "does not find 32-bit keys in the 64-bit registry" do
603
+ @registry.architecture=:x86_64
604
+ @registry.data_exists?("HKLM\\Software\\Root\\Mauve", {:name=>"Alert", :type=>:string, :data=>"Universal"}).should == true
605
+ end
606
+ it "finds 64-bit keys in the 64-bit registry" do
607
+ @registry.architecture=:x86_64
608
+ lambda{@registry.data_exists?("HKLM\\Software\\Root\\Poosh", {:name=>"Status", :type=>:string, :data=>"Lost"})}.should raise_error(Chef::Exceptions::Win32RegKeyMissing)
609
+ end
610
+ end
611
+
612
+ describe "create_key" do
613
+ it "can create a 32-bit only registry key" do
614
+ @registry.architecture = :i386
615
+ @registry.create_key("HKLM\\Software\\Root\\Trunk\\Red", true).should == true
616
+ @registry.key_exists?("HKLM\\Software\\Root\\Trunk\\Red").should == true
617
+ @registry.architecture = :x86_64
618
+ @registry.key_exists?("HKLM\\Software\\Root\\Trunk\\Red").should == false
619
+ end
620
+
621
+ it "can create a 64-bit only registry key" do
622
+ @registry.architecture = :x86_64
623
+ @registry.create_key("HKLM\\Software\\Root\\Trunk\\Blue", true).should == true
624
+ @registry.key_exists?("HKLM\\Software\\Root\\Trunk\\Blue").should == true
625
+ @registry.architecture = :i386
626
+ @registry.key_exists?("HKLM\\Software\\Root\\Trunk\\Blue").should == false
627
+ end
628
+ end
629
+
630
+ end
631
+ end
632
+ end