chef 12.3.0 → 12.4.0.rc.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (247) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +86 -7
  3. data/distro/common/markdown/man1/chef-shell.mkd +4 -4
  4. data/distro/common/markdown/man1/knife-bootstrap.mkd +1 -1
  5. data/distro/common/markdown/man1/knife-client.mkd +1 -1
  6. data/distro/common/markdown/man1/knife-configure.mkd +1 -1
  7. data/distro/common/markdown/man1/knife-cookbook-site.mkd +1 -1
  8. data/distro/common/markdown/man1/knife-cookbook.mkd +4 -4
  9. data/distro/common/markdown/man1/knife-data-bag.mkd +1 -1
  10. data/distro/common/markdown/man1/knife-environment.mkd +3 -3
  11. data/distro/common/markdown/man1/knife-exec.mkd +1 -1
  12. data/distro/common/markdown/man1/knife-index.mkd +1 -1
  13. data/distro/common/markdown/man1/knife-node.mkd +1 -1
  14. data/distro/common/markdown/man1/knife-role.mkd +3 -3
  15. data/distro/common/markdown/man1/knife-search.mkd +2 -2
  16. data/distro/common/markdown/man1/knife-ssh.mkd +1 -1
  17. data/distro/common/markdown/man1/knife-status.mkd +1 -1
  18. data/distro/common/markdown/man1/knife-tag.mkd +1 -1
  19. data/distro/common/markdown/man1/knife.mkd +2 -2
  20. data/distro/common/markdown/man8/chef-client.mkd +1 -2
  21. data/distro/common/markdown/man8/chef-expander.mkd +1 -2
  22. data/distro/common/markdown/man8/chef-expanderctl.mkd +1 -2
  23. data/distro/common/markdown/man8/chef-server-webui.mkd +1 -1
  24. data/distro/common/markdown/man8/chef-server.mkd +1 -2
  25. data/distro/common/markdown/man8/chef-solo.mkd +2 -2
  26. data/distro/common/markdown/man8/chef-solr.mkd +1 -1
  27. data/lib/chef/client.rb +2 -2
  28. data/lib/chef/config.rb +17 -709
  29. data/lib/chef/cookbook/metadata.rb +9 -5
  30. data/lib/chef/cookbook_loader.rb +1 -1
  31. data/lib/chef/cookbook_site_streaming_uploader.rb +2 -18
  32. data/lib/chef/dsl/definitions.rb +44 -0
  33. data/lib/chef/dsl/recipe.rb +50 -35
  34. data/lib/chef/dsl/resources.rb +28 -0
  35. data/lib/chef/event_dispatch/dispatcher.rb +2 -0
  36. data/lib/chef/event_loggers/windows_eventlog.rb +1 -11
  37. data/lib/chef/exceptions.rb +4 -0
  38. data/lib/chef/file_access_control/unix.rb +5 -0
  39. data/lib/chef/formatters/error_inspectors/api_error_formatting.rb +17 -0
  40. data/lib/chef/formatters/error_inspectors/compile_error_inspector.rb +21 -9
  41. data/lib/chef/formatters/error_inspectors/cookbook_resolve_error_inspector.rb +2 -0
  42. data/lib/chef/formatters/error_inspectors/cookbook_sync_error_inspector.rb +2 -0
  43. data/lib/chef/formatters/error_inspectors/node_load_error_inspector.rb +2 -0
  44. data/lib/chef/formatters/error_inspectors/registration_error_inspector.rb +4 -0
  45. data/lib/chef/formatters/error_inspectors/resource_failure_inspector.rb +11 -1
  46. data/lib/chef/formatters/error_inspectors/run_list_expansion_error_inspector.rb +2 -0
  47. data/lib/chef/http/authenticator.rb +3 -0
  48. data/lib/chef/http/basic_client.rb +10 -6
  49. data/lib/chef/http/json_input.rb +6 -1
  50. data/lib/chef/key.rb +271 -0
  51. data/lib/chef/knife.rb +11 -1
  52. data/lib/chef/knife/bootstrap.rb +6 -0
  53. data/lib/chef/knife/bootstrap/templates/chef-full.erb +166 -23
  54. data/lib/chef/knife/client_key_create.rb +67 -0
  55. data/lib/chef/knife/client_key_delete.rb +76 -0
  56. data/lib/chef/knife/client_key_edit.rb +80 -0
  57. data/lib/chef/knife/client_key_list.rb +69 -0
  58. data/lib/chef/knife/client_key_show.rb +76 -0
  59. data/lib/chef/knife/key_create.rb +108 -0
  60. data/lib/chef/knife/key_create_base.rb +50 -0
  61. data/lib/chef/knife/key_delete.rb +55 -0
  62. data/lib/chef/knife/key_edit.rb +114 -0
  63. data/lib/chef/knife/key_edit_base.rb +55 -0
  64. data/lib/chef/knife/key_list.rb +88 -0
  65. data/lib/chef/knife/key_list_base.rb +45 -0
  66. data/lib/chef/knife/key_show.rb +53 -0
  67. data/lib/chef/knife/ssh.rb +26 -28
  68. data/lib/chef/knife/user_key_create.rb +69 -0
  69. data/lib/chef/knife/user_key_delete.rb +76 -0
  70. data/lib/chef/knife/user_key_edit.rb +80 -0
  71. data/lib/chef/knife/user_key_list.rb +69 -0
  72. data/lib/chef/knife/user_key_show.rb +76 -0
  73. data/lib/chef/log.rb +2 -0
  74. data/lib/chef/log/syslog.rb +46 -0
  75. data/lib/chef/log/winevt.rb +99 -0
  76. data/lib/chef/mixin/provides.rb +8 -1
  77. data/lib/chef/mixin/unformatter.rb +32 -0
  78. data/lib/chef/mixin/uris.rb +33 -0
  79. data/lib/chef/mixin/wstring.rb +31 -0
  80. data/lib/chef/node.rb +21 -2
  81. data/lib/chef/platform/provider_mapping.rb +8 -4
  82. data/lib/chef/platform/query_helpers.rb +1 -5
  83. data/lib/chef/platform/service_helpers.rb +21 -21
  84. data/lib/chef/provider.rb +33 -0
  85. data/lib/chef/provider/cron/unix.rb +1 -0
  86. data/lib/chef/provider/file.rb +5 -3
  87. data/lib/chef/provider/lwrp_base.rb +76 -58
  88. data/lib/chef/provider/ohai.rb +1 -0
  89. data/lib/chef/provider/package.rb +7 -4
  90. data/lib/chef/provider/package/aix.rb +1 -0
  91. data/lib/chef/provider/package/smartos.rb +5 -5
  92. data/lib/chef/provider/package/windows.rb +90 -6
  93. data/lib/chef/provider/package/yum.rb +102 -32
  94. data/lib/chef/provider/reboot.rb +1 -0
  95. data/lib/chef/provider/registry_key.rb +2 -0
  96. data/lib/chef/provider/remote_file.rb +1 -0
  97. data/lib/chef/provider/remote_file/content.rb +5 -1
  98. data/lib/chef/provider/remote_file/fetcher.rb +22 -8
  99. data/lib/chef/provider/remote_file/network_file.rb +48 -0
  100. data/lib/chef/provider/service/aix.rb +13 -12
  101. data/lib/chef/provider_resolver.rb +87 -0
  102. data/lib/chef/providers.rb +1 -0
  103. data/lib/chef/resource.rb +67 -8
  104. data/lib/chef/resource/bash.rb +1 -0
  105. data/lib/chef/resource/bff_package.rb +1 -2
  106. data/lib/chef/resource/breakpoint.rb +1 -0
  107. data/lib/chef/resource/csh.rb +1 -0
  108. data/lib/chef/resource/deploy.rb +1 -0
  109. data/lib/chef/resource/erl_call.rb +1 -0
  110. data/lib/chef/resource/execute.rb +1 -0
  111. data/lib/chef/resource/file.rb +18 -0
  112. data/lib/chef/resource/http_request.rb +1 -0
  113. data/lib/chef/resource/ifconfig.rb +1 -2
  114. data/lib/chef/resource/log.rb +1 -2
  115. data/lib/chef/resource/lwrp_base.rb +106 -87
  116. data/lib/chef/resource/ohai.rb +1 -0
  117. data/lib/chef/resource/package.rb +1 -0
  118. data/lib/chef/resource/perl.rb +1 -0
  119. data/lib/chef/resource/portage_package.rb +1 -0
  120. data/lib/chef/resource/python.rb +1 -0
  121. data/lib/chef/resource/reboot.rb +2 -0
  122. data/lib/chef/resource/registry_key.rb +1 -0
  123. data/lib/chef/resource/remote_file.rb +1 -1
  124. data/lib/chef/resource/route.rb +1 -2
  125. data/lib/chef/resource/ruby.rb +1 -0
  126. data/lib/chef/resource/ruby_block.rb +1 -0
  127. data/lib/chef/resource/scm.rb +1 -0
  128. data/lib/chef/resource/script.rb +1 -0
  129. data/lib/chef/resource/service.rb +1 -0
  130. data/lib/chef/resource/subversion.rb +1 -0
  131. data/lib/chef/resource/whyrun_safe_ruby_block.rb +1 -0
  132. data/lib/chef/resource/windows_package.rb +24 -2
  133. data/lib/chef/resource/windows_script.rb +1 -0
  134. data/lib/chef/resource/yum_package.rb +1 -1
  135. data/lib/chef/resource_definition.rb +1 -0
  136. data/lib/chef/resource_reporter.rb +3 -10
  137. data/lib/chef/resource_resolver.rb +20 -10
  138. data/lib/chef/run_context.rb +1 -0
  139. data/lib/chef/run_list/versioned_recipe_list.rb +18 -0
  140. data/lib/chef/run_status.rb +2 -4
  141. data/lib/chef/shell.rb +1 -1
  142. data/lib/chef/util/path_helper.rb +3 -204
  143. data/lib/chef/util/windows/net_user.rb +73 -118
  144. data/lib/chef/version.rb +8 -4
  145. data/lib/chef/win32/api.rb +2 -1
  146. data/lib/chef/win32/api/installer.rb +1 -1
  147. data/lib/chef/win32/api/net.rb +115 -2
  148. data/lib/chef/win32/api/security.rb +24 -0
  149. data/lib/chef/win32/api/unicode.rb +1 -1
  150. data/lib/chef/win32/eventlog.rb +31 -0
  151. data/lib/chef/win32/net.rb +190 -0
  152. data/lib/chef/win32/security.rb +51 -2
  153. data/lib/chef/win32/security/sid.rb +17 -0
  154. data/spec/data/lwrp/providers/buck_passer.rb +18 -2
  155. data/spec/data/lwrp/providers/buck_passer_2.rb +18 -2
  156. data/spec/data/lwrp/providers/embedded_resource_accesses_providers_scope.rb +14 -2
  157. data/spec/data/lwrp_override/resources/foo.rb +5 -0
  158. data/spec/functional/knife/ssh_spec.rb +2 -2
  159. data/spec/functional/rebooter_spec.rb +1 -1
  160. data/spec/functional/resource/aixinit_service_spec.rb +1 -1
  161. data/spec/functional/resource/user/windows_spec.rb +125 -0
  162. data/spec/functional/shell_spec.rb +25 -10
  163. data/spec/functional/win32/sid_spec.rb +55 -0
  164. data/spec/integration/client/client_spec.rb +53 -29
  165. data/spec/integration/knife/deps_spec.rb +8 -14
  166. data/spec/integration/knife/upload_spec.rb +18 -0
  167. data/spec/integration/recipes/lwrp_inline_resources_spec.rb +1 -1
  168. data/spec/integration/recipes/provider_choice.rb +41 -0
  169. data/spec/integration/recipes/recipe_dsl_spec.rb +259 -0
  170. data/spec/spec_helper.rb +6 -1
  171. data/spec/support/key_helpers.rb +104 -0
  172. data/spec/support/lib/chef/resource/cat.rb +1 -0
  173. data/spec/support/lib/chef/resource/one_two_three_four.rb +2 -0
  174. data/spec/support/lib/chef/resource/zen_follower.rb +1 -0
  175. data/spec/support/lib/chef/resource/zen_master.rb +2 -0
  176. data/spec/support/mock/platform.rb +1 -1
  177. data/spec/support/pedant/Gemfile.lock +67 -0
  178. data/spec/support/shared/functional/securable_resource.rb +34 -12
  179. data/spec/support/shared/integration/integration_helper.rb +6 -5
  180. data/spec/support/shared/unit/provider/file.rb +31 -8
  181. data/spec/unit/application/client_spec.rb +2 -2
  182. data/spec/unit/client_spec.rb +21 -4
  183. data/spec/unit/cookbook/cookbook_version_loader_spec.rb +1 -1
  184. data/spec/unit/cookbook/metadata_spec.rb +15 -0
  185. data/spec/unit/cookbook/syntax_check_spec.rb +1 -1
  186. data/spec/unit/cookbook_loader_spec.rb +1 -1
  187. data/spec/unit/cookbook_site_streaming_uploader_spec.rb +0 -21
  188. data/spec/unit/data_bag_spec.rb +1 -1
  189. data/spec/unit/event_dispatch/dispatcher_spec.rb +61 -0
  190. data/spec/unit/formatters/error_inspectors/api_error_formatting_spec.rb +75 -0
  191. data/spec/unit/formatters/error_inspectors/compile_error_inspector_spec.rb +149 -112
  192. data/spec/unit/formatters/error_inspectors/resource_failure_inspector_spec.rb +7 -0
  193. data/spec/unit/guard_interpreter/resource_guard_interpreter_spec.rb +1 -1
  194. data/spec/unit/http/authenticator_spec.rb +69 -0
  195. data/spec/unit/http/basic_client_spec.rb +16 -0
  196. data/spec/unit/key_spec.rb +634 -0
  197. data/spec/unit/knife/bootstrap_spec.rb +14 -1
  198. data/spec/unit/knife/core/subcommand_loader_spec.rb +1 -1
  199. data/spec/unit/knife/core/ui_spec.rb +1 -1
  200. data/spec/unit/knife/data_bag_from_file_spec.rb +1 -1
  201. data/spec/unit/knife/environment_from_file_spec.rb +1 -1
  202. data/spec/unit/knife/key_create_spec.rb +224 -0
  203. data/spec/unit/knife/key_delete_spec.rb +135 -0
  204. data/spec/unit/knife/key_edit_spec.rb +267 -0
  205. data/spec/unit/knife/key_helper.rb +74 -0
  206. data/spec/unit/knife/key_list_spec.rb +216 -0
  207. data/spec/unit/knife/key_show_spec.rb +126 -0
  208. data/spec/unit/knife/ssh_spec.rb +23 -26
  209. data/spec/unit/knife_spec.rb +33 -1
  210. data/spec/unit/log/syslog_spec.rb +53 -0
  211. data/spec/unit/log/winevt_spec.rb +55 -0
  212. data/spec/unit/lwrp_spec.rb +105 -51
  213. data/spec/unit/mixin/path_sanity_spec.rb +2 -2
  214. data/spec/unit/mixin/template_spec.rb +2 -2
  215. data/spec/unit/mixin/unformatter_spec.rb +61 -0
  216. data/spec/unit/mixin/uris_spec.rb +45 -0
  217. data/spec/unit/platform/query_helpers_spec.rb +1 -1
  218. data/spec/unit/policy_builder/policyfile_spec.rb +7 -3
  219. data/spec/unit/provider/deploy/revision_spec.rb +1 -1
  220. data/spec/unit/provider/deploy_spec.rb +1 -1
  221. data/spec/unit/provider/directory_spec.rb +1 -1
  222. data/spec/unit/provider/execute_spec.rb +1 -1
  223. data/spec/unit/provider/package/aix_spec.rb +20 -8
  224. data/spec/unit/provider/package/smartos_spec.rb +50 -40
  225. data/spec/unit/provider/package/windows_spec.rb +104 -25
  226. data/spec/unit/provider/package/yum_spec.rb +111 -1
  227. data/spec/unit/provider/package_spec.rb +6 -0
  228. data/spec/unit/provider/remote_file/fetcher_spec.rb +20 -1
  229. data/spec/unit/provider/remote_file/network_file_spec.rb +45 -0
  230. data/spec/unit/provider/service/aix_service_spec.rb +26 -11
  231. data/spec/unit/provider/user/dscl_spec.rb +1 -1
  232. data/spec/unit/provider_spec.rb +20 -0
  233. data/spec/unit/recipe_spec.rb +1 -1
  234. data/spec/unit/resource/batch_spec.rb +1 -0
  235. data/spec/unit/resource/powershell_spec.rb +1 -0
  236. data/spec/unit/resource/remote_file_spec.rb +10 -0
  237. data/spec/unit/resource/windows_package_spec.rb +16 -2
  238. data/spec/unit/resource_spec.rb +40 -24
  239. data/spec/unit/rest_spec.rb +10 -20
  240. data/spec/unit/role_spec.rb +1 -1
  241. data/spec/unit/run_context_spec.rb +31 -0
  242. data/spec/unit/shell_spec.rb +4 -4
  243. data/tasks/external_tests.rb +29 -0
  244. data/tasks/rspec.rb +14 -1
  245. metadata +105 -28
  246. data/spec/unit/config_spec.rb +0 -544
  247. data/spec/unit/util/path_helper_spec.rb +0 -255
@@ -0,0 +1,74 @@
1
+ #
2
+ # Author:: Tyler Cloke (<tyler@chef.io>)
3
+ # Copyright:: Copyright (c) 2015 Chef Software, 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
+
21
+ shared_examples_for "a knife key command" do
22
+ let(:stderr) { StringIO.new }
23
+ let(:params) { [] }
24
+ let(:command) do
25
+ c = described_class.new([])
26
+ c.ui.config[:disable_editing] = true
27
+ allow(c.ui).to receive(:stderr).and_return(stderr)
28
+ allow(c.ui).to receive(:stdout).and_return(stderr)
29
+ allow(c).to receive(:show_usage)
30
+ c
31
+ end
32
+
33
+ context "before apply_params! is called" do
34
+ context "when apply_params! is called with invalid args" do
35
+ it "shows the usage" do
36
+ expect(command).to receive(:show_usage)
37
+ expect { command.apply_params!(params) }.to exit_with_code(1)
38
+ end
39
+
40
+ it "outputs the proper error" do
41
+ expect { command.apply_params!(params) }.to exit_with_code(1)
42
+ expect(stderr.string).to include(command.actor_missing_error)
43
+ end
44
+
45
+ it "exits 1" do
46
+ expect { command.apply_params!(params) }.to exit_with_code(1)
47
+ end
48
+ end
49
+ end # before apply_params! is called
50
+
51
+ context "after apply_params! is called with valid args" do
52
+ let(:params) { ["charmander"] }
53
+ before do
54
+ command.apply_params!(params)
55
+ end
56
+
57
+ it "properly defines the actor" do
58
+ expect(command.actor).to eq("charmander")
59
+ end
60
+ end # after apply_params! is called with valid args
61
+
62
+ context "when the command is run" do
63
+ before do
64
+ allow(command).to receive(:service_object).and_return(service_object)
65
+ allow(command).to receive(:name_args).and_return(["charmander"])
66
+ end
67
+
68
+ context "when the command is successful" do
69
+ before do
70
+ expect(service_object).to receive(:run)
71
+ end
72
+ end
73
+ end
74
+ end # a knife key command
@@ -0,0 +1,216 @@
1
+ #
2
+ # Author:: Tyler Cloke (<tyler@chef.io>)
3
+ # Copyright:: Copyright (c) 2015 Chef Software, 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/knife/user_key_list'
21
+ require 'chef/knife/client_key_list'
22
+ require 'chef/knife/key_list'
23
+ require 'chef/key'
24
+
25
+ describe "key list commands that inherit knife" do
26
+ shared_examples_for "a key list command" do
27
+ let(:stderr) { StringIO.new }
28
+ let(:params) { [] }
29
+ let(:service_object) { instance_double(Chef::Knife::KeyList) }
30
+ let(:command) do
31
+ c = described_class.new([])
32
+ c.ui.config[:disable_editing] = true
33
+ allow(c.ui).to receive(:stderr).and_return(stderr)
34
+ allow(c.ui).to receive(:stdout).and_return(stderr)
35
+ allow(c).to receive(:show_usage)
36
+ c
37
+ end
38
+
39
+ context "after apply_params! is called with valid args" do
40
+ let(:params) { ["charmander"] }
41
+ before do
42
+ command.apply_params!(params)
43
+ end
44
+
45
+ context "when the service object is called" do
46
+ it "creates a new instance of Chef::Knife::KeyList with the correct args" do
47
+ expect(Chef::Knife::KeyList).to receive(:new).
48
+ with("charmander", command.list_method, command.ui, command.config).
49
+ and_return(service_object)
50
+ command.service_object
51
+ end
52
+ end # when the service object is called
53
+ end # after apply_params! is called with valid args
54
+ end # a key list command
55
+
56
+ describe Chef::Knife::UserKeyList do
57
+ it_should_behave_like "a key list command"
58
+ # defined in key_helpers.rb
59
+ it_should_behave_like "a knife key command" do
60
+ let(:service_object) { instance_double(Chef::Knife::KeyList) }
61
+ let(:params) { ["charmander"] }
62
+ end
63
+ end
64
+
65
+ describe Chef::Knife::ClientKeyList do
66
+ it_should_behave_like "a key list command"
67
+ # defined in key_helpers.rb
68
+ it_should_behave_like "a knife key command" do
69
+ let(:service_object) { instance_double(Chef::Knife::KeyList) }
70
+ let(:params) { ["charmander"] }
71
+ end
72
+ end
73
+ end
74
+
75
+ describe Chef::Knife::KeyList do
76
+ let(:config) { Hash.new }
77
+ let(:actor) { "charmander" }
78
+ let(:ui) { instance_double("Chef::Knife::UI") }
79
+
80
+ shared_examples_for "key list run command" do
81
+ let(:key_list_object) {
82
+ described_class.new(actor, list_method, ui, config)
83
+ }
84
+
85
+ before do
86
+ allow(Chef::Key).to receive(list_method).and_return(http_response)
87
+ allow(key_list_object).to receive(:display_info)
88
+ # simply pass the string though that colorize takes in
89
+ allow(key_list_object).to receive(:colorize).with(kind_of(String)) do |input|
90
+ input
91
+ end
92
+ end
93
+
94
+ context "when only_expired and only_non_expired were both passed" do
95
+ before do
96
+ key_list_object.config[:only_expired] = true
97
+ key_list_object.config[:only_non_expired] = true
98
+ end
99
+
100
+ it "raises a Chef::Exceptions::KeyCommandInputError with the proper error message" do
101
+ expect{ key_list_object.run }.to raise_error(Chef::Exceptions::KeyCommandInputError, key_list_object.expired_and_non_expired_msg)
102
+ end
103
+ end
104
+
105
+ context "when the command is run" do
106
+ before do
107
+ key_list_object.config[:only_expired] = false
108
+ key_list_object.config[:only_non_expired] = false
109
+ key_list_object.config[:with_details] = false
110
+ end
111
+
112
+ it "calls Chef::Key with the proper list command and input" do
113
+ expect(Chef::Key).to receive(list_method).with(actor)
114
+ key_list_object.run
115
+ end
116
+
117
+ it "displays all the keys" do
118
+ expect(key_list_object).to receive(:display_info).with(/non-expired/).twice
119
+ expect(key_list_object).to receive(:display_info).with(/out-of-date/).once
120
+ key_list_object.run
121
+ end
122
+
123
+ context "when only_expired is called" do
124
+ before do
125
+ key_list_object.config[:only_expired] = true
126
+ end
127
+
128
+ it "excludes displaying non-expired keys" do
129
+ expect(key_list_object).to receive(:display_info).with(/non-expired/).exactly(0).times
130
+ key_list_object.run
131
+ end
132
+
133
+ it "displays the expired keys" do
134
+ expect(key_list_object).to receive(:display_info).with(/out-of-date/).once
135
+ key_list_object.run
136
+ end
137
+ end # when only_expired is called
138
+
139
+ context "when only_non_expired is called" do
140
+ before do
141
+ key_list_object.config[:only_non_expired] = true
142
+ end
143
+
144
+ it "excludes displaying expired keys" do
145
+ expect(key_list_object).to receive(:display_info).with(/out-of-date/).exactly(0).times
146
+ key_list_object.run
147
+ end
148
+
149
+ it "displays the non-expired keys" do
150
+ expect(key_list_object).to receive(:display_info).with(/non-expired/).twice
151
+ key_list_object.run
152
+ end
153
+ end # when only_expired is called
154
+
155
+ context "when with_details is false" do
156
+ before do
157
+ key_list_object.config[:with_details] = false
158
+ end
159
+
160
+ it "does not display the uri" do
161
+ expect(key_list_object).to receive(:display_info).with(/https/).exactly(0).times
162
+ key_list_object.run
163
+ end
164
+
165
+ it "does not display the expired status" do
166
+ expect(key_list_object).to receive(:display_info).with(/\(expired\)/).exactly(0).times
167
+ key_list_object.run
168
+ end
169
+ end # when with_details is false
170
+
171
+ context "when with_details is true" do
172
+ before do
173
+ key_list_object.config[:with_details] = true
174
+ end
175
+
176
+ it "displays the uri" do
177
+ expect(key_list_object).to receive(:display_info).with(/https/).exactly(3).times
178
+ key_list_object.run
179
+ end
180
+
181
+ it "displays the expired status" do
182
+ expect(key_list_object).to receive(:display_info).with(/\(expired\)/).once
183
+ key_list_object.run
184
+ end
185
+ end # when with_details is true
186
+
187
+ end # when the command is run
188
+
189
+ end # key list run command
190
+
191
+ context "when list_method is :list_by_user" do
192
+ it_should_behave_like "key list run command" do
193
+ let(:list_method) { :list_by_user }
194
+ let(:http_response) {
195
+ [
196
+ {"uri"=>"https://api.opscode.piab/users/charmander/keys/non-expired1", "name"=>"non-expired1", "expired"=>false},
197
+ {"uri"=>"https://api.opscode.piab/users/charmander/keys/non-expired2", "name"=>"non-expired2", "expired"=>false},
198
+ {"uri"=>"https://api.opscode.piab/users/mary/keys/out-of-date", "name"=>"out-of-date", "expired"=>true}
199
+ ]
200
+ }
201
+ end
202
+ end
203
+
204
+ context "when list_method is :list_by_client" do
205
+ it_should_behave_like "key list run command" do
206
+ let(:list_method) { :list_by_client }
207
+ let(:http_response) {
208
+ [
209
+ {"uri"=>"https://api.opscode.piab/organizations/pokemon/clients/charmander/keys/non-expired1", "name"=>"non-expired1", "expired"=>false},
210
+ {"uri"=>"https://api.opscode.piab/organizations/pokemon/clients/charmander/keys/non-expired2", "name"=>"non-expired2", "expired"=>false},
211
+ {"uri"=>"https://api.opscode.piab/organizations/pokemon/clients/mary/keys/out-of-date", "name"=>"out-of-date", "expired"=>true}
212
+ ]
213
+ }
214
+ end
215
+ end
216
+ end
@@ -0,0 +1,126 @@
1
+ #
2
+ # Author:: Tyler Cloke (<tyler@chef.io>)
3
+ # Copyright:: Copyright (c) 2015 Chef Software, 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/knife/user_key_show'
21
+ require 'chef/knife/client_key_show'
22
+ require 'chef/knife/key_show'
23
+ require 'chef/key'
24
+
25
+ describe "key show commands that inherit knife" do
26
+ shared_examples_for "a key show command" do
27
+ let(:stderr) { StringIO.new }
28
+ let(:params) { [] }
29
+ let(:service_object) { instance_double(Chef::Knife::KeyShow) }
30
+ let(:command) do
31
+ c = described_class.new([])
32
+ c.ui.config[:disable_editing] = true
33
+ allow(c.ui).to receive(:stderr).and_return(stderr)
34
+ allow(c.ui).to receive(:stdout).and_return(stderr)
35
+ allow(c).to receive(:show_usage)
36
+ c
37
+ end
38
+
39
+ context "after apply_params! is called with valid args" do
40
+ let(:params) { ["charmander", "charmander-key"] }
41
+ before do
42
+ command.apply_params!(params)
43
+ end
44
+
45
+ context "when the service object is called" do
46
+ it "creates a new instance of Chef::Knife::KeyShow with the correct args" do
47
+ expect(Chef::Knife::KeyShow).to receive(:new).
48
+ with("charmander-key", "charmander", command.load_method, command.ui).
49
+ and_return(service_object)
50
+ command.service_object
51
+ end
52
+ end # when the service object is called
53
+ end # after apply_params! is called with valid args
54
+ end # a key show command
55
+
56
+ describe Chef::Knife::UserKeyShow do
57
+ it_should_behave_like "a key show command"
58
+ # defined in key_helpers.rb
59
+ it_should_behave_like "a knife key command with a keyname as the second arg"
60
+ it_should_behave_like "a knife key command" do
61
+ let(:service_object) { instance_double(Chef::Knife::KeyShow) }
62
+ let(:params) { ["charmander", "charmander-key"] }
63
+ end
64
+ end
65
+
66
+ describe Chef::Knife::ClientKeyShow do
67
+ it_should_behave_like "a key show command"
68
+ # defined in key_helpers.rb
69
+ it_should_behave_like "a knife key command with a keyname as the second arg"
70
+ it_should_behave_like "a knife key command" do
71
+ let(:service_object) { instance_double(Chef::Knife::KeyShow) }
72
+ let(:params) { ["charmander", "charmander-key"] }
73
+ end
74
+ end
75
+ end
76
+
77
+ describe Chef::Knife::KeyShow do
78
+ let(:actor) { "charmander" }
79
+ let(:keyname) { "charmander" }
80
+ let(:ui) { instance_double("Chef::Knife::UI") }
81
+ let(:expected_hash) {
82
+ {
83
+ actor_field_name => "charmander",
84
+ "name" => "charmander-key",
85
+ "public_key" => "some-public-key",
86
+ "expiration_date" => "infinity"
87
+ }
88
+ }
89
+
90
+ shared_examples_for "key show run command" do
91
+ let(:key_show_object) {
92
+ described_class.new(keyname, actor, load_method, ui)
93
+ }
94
+
95
+ before do
96
+ allow(key_show_object).to receive(:display_output)
97
+ allow(Chef::Key).to receive(load_method).and_return(Chef::Key.from_hash(expected_hash))
98
+ end
99
+
100
+ context "when the command is run" do
101
+ it "loads the key using the proper method and args" do
102
+ expect(Chef::Key).to receive(load_method).with(actor, keyname)
103
+ key_show_object.run
104
+ end
105
+
106
+ it "displays the key" do
107
+ expect(key_show_object).to receive(:display_output)
108
+ key_show_object.run
109
+ end
110
+ end
111
+ end
112
+
113
+ context "when load_method is :load_by_user" do
114
+ it_should_behave_like "key show run command" do
115
+ let(:load_method) { :load_by_user }
116
+ let(:actor_field_name) { 'user' }
117
+ end
118
+ end
119
+
120
+ context "when load_method is :load_by_client" do
121
+ it_should_behave_like "key show run command" do
122
+ let(:load_method) { :load_by_client }
123
+ let(:actor_field_name) { 'user' }
124
+ end
125
+ end
126
+ end
@@ -28,10 +28,10 @@ describe Chef::Knife::Ssh do
28
28
  before do
29
29
  @knife = Chef::Knife::Ssh.new
30
30
  @knife.merge_configs
31
- @knife.config[:attribute] = "fqdn"
32
31
  @node_foo = Chef::Node.new
33
32
  @node_foo.automatic_attrs[:fqdn] = "foo.example.org"
34
33
  @node_foo.automatic_attrs[:ipaddress] = "10.0.0.1"
34
+
35
35
  @node_bar = Chef::Node.new
36
36
  @node_bar.automatic_attrs[:fqdn] = "bar.example.org"
37
37
  @node_bar.automatic_attrs[:ipaddress] = "10.0.0.2"
@@ -52,15 +52,15 @@ describe Chef::Knife::Ssh do
52
52
  def self.should_return_specified_attributes
53
53
  it "returns an array of the attributes specified on the command line OR config file, if only one is set" do
54
54
  @knife.config[:attribute] = "ipaddress"
55
- @knife.config[:attribute_from_cli] = "ipaddress"
55
+ Chef::Config[:knife][:ssh_attribute] = "ipaddress" # this value will be in the config file
56
56
  configure_query([@node_foo, @node_bar])
57
57
  expect(@knife).to receive(:session_from_list).with([['10.0.0.1', nil], ['10.0.0.2', nil]])
58
58
  @knife.configure_session
59
59
  end
60
60
 
61
61
  it "returns an array of the attributes specified on the command line even when a config value is set" do
62
- @knife.config[:attribute] = "config_file" # this value will be the config file
63
- @knife.config[:attribute_from_cli] = "ipaddress" # this is the value of the command line via #configure_attribute
62
+ Chef::Config[:knife][:ssh_attribute] = "config_file" # this value will be in the config file
63
+ @knife.config[:attribute] = "ipaddress" # this is the value of the command line via #configure_attribute
64
64
  configure_query([@node_foo, @node_bar])
65
65
  expect(@knife).to receive(:session_from_list).with([['10.0.0.1', nil], ['10.0.0.2', nil]])
66
66
  @knife.configure_session
@@ -83,7 +83,6 @@ describe Chef::Knife::Ssh do
83
83
  @node_foo.automatic_attrs[:cloud][:public_hostname] = "ec2-10-0-0-1.compute-1.amazonaws.com"
84
84
  @node_bar.automatic_attrs[:cloud][:public_hostname] = "ec2-10-0-0-2.compute-1.amazonaws.com"
85
85
  end
86
-
87
86
  it "returns an array of cloud public hostnames" do
88
87
  configure_query([@node_foo, @node_bar])
89
88
  expect(@knife).to receive(:session_from_list).with([
@@ -150,42 +149,40 @@ describe Chef::Knife::Ssh do
150
149
  end
151
150
  end
152
151
 
153
- describe "#configure_attribute" do
152
+ describe "#get_ssh_attribute" do
153
+ # Order of precedence for ssh target
154
+ # 1) command line attribute
155
+ # 2) configuration file
156
+ # 3) cloud attribute
157
+ # 4) fqdn
154
158
  before do
155
159
  Chef::Config[:knife][:ssh_attribute] = nil
156
160
  @knife.config[:attribute] = nil
161
+ @node_foo.automatic_attrs[:cloud][:public_hostname] = "ec2-10-0-0-1.compute-1.amazonaws.com"
162
+ @node_bar.automatic_attrs[:cloud][:public_hostname] = ''
157
163
  end
158
164
 
159
165
  it "should return fqdn by default" do
160
- @knife.configure_attribute
161
- expect(@knife.config[:attribute]).to eq("fqdn")
166
+ expect(@knife.get_ssh_attribute(Chef::Node.new)).to eq("fqdn")
162
167
  end
163
168
 
164
- it "should return the value set in the configuration file" do
165
- Chef::Config[:knife][:ssh_attribute] = "config_file"
166
- @knife.configure_attribute
167
- expect(@knife.config[:attribute]).to eq("config_file")
169
+ it "should return cloud.public_hostname attribute if available" do
170
+ expect(@knife.get_ssh_attribute(@node_foo)).to eq("cloud.public_hostname")
168
171
  end
169
172
 
170
- it "should return the value set on the command line" do
173
+ it "should favor to attribute_from_cli over config file and cloud" do
171
174
  @knife.config[:attribute] = "command_line"
172
- @knife.configure_attribute
173
- expect(@knife.config[:attribute]).to eq("command_line")
175
+ Chef::Config[:knife][:ssh_attribute] = "config_file"
176
+ expect( @knife.get_ssh_attribute(@node_foo)).to eq("command_line")
174
177
  end
175
178
 
176
- it "should set attribute_from_cli to the value of attribute from the command line" do
177
- @knife.config[:attribute] = "command_line"
178
- @knife.configure_attribute
179
- expect(@knife.config[:attribute]).to eq("command_line")
180
- expect(@knife.config[:attribute_from_cli]).to eq("command_line")
179
+ it "should favor config file over cloud and default" do
180
+ Chef::Config[:knife][:ssh_attribute] = "config_file"
181
+ expect( @knife.get_ssh_attribute(@node_foo)).to eq("config_file")
181
182
  end
182
183
 
183
- it "should prefer the command line over the config file for the value of attribute_from_cli" do
184
- Chef::Config[:knife][:ssh_attribute] = "config_file"
185
- @knife.config[:attribute] = "command_line"
186
- @knife.configure_attribute
187
- expect(@knife.config[:attribute]).to eq("command_line")
188
- expect(@knife.config[:attribute_from_cli]).to eq("command_line")
184
+ it "should return fqdn if cloud.hostname is empty" do
185
+ expect( @knife.get_ssh_attribute(@node_bar)).to eq("fqdn")
189
186
  end
190
187
  end
191
188