chef 17.4.38 → 17.7.22

Sign up to get free protection for your applications and to get access to all the features.
Files changed (138) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +5 -0
  3. data/chef.gemspec +3 -0
  4. data/lib/chef/application/base.rb +11 -1
  5. data/lib/chef/chef_fs/file_pattern.rb +1 -1
  6. data/lib/chef/chef_fs/path_utils.rb +1 -1
  7. data/lib/chef/client.rb +1 -2
  8. data/lib/chef/compliance/input.rb +115 -0
  9. data/lib/chef/compliance/input_collection.rb +139 -0
  10. data/lib/chef/compliance/profile.rb +122 -0
  11. data/lib/chef/compliance/profile_collection.rb +109 -0
  12. data/lib/chef/compliance/runner.rb +47 -5
  13. data/lib/chef/compliance/waiver.rb +115 -0
  14. data/lib/chef/compliance/waiver_collection.rb +143 -0
  15. data/lib/chef/data_collector/run_end_message.rb +1 -1
  16. data/lib/chef/dsl/compliance.rb +38 -0
  17. data/lib/chef/dsl/reader_helpers.rb +51 -0
  18. data/lib/chef/dsl/reboot_pending.rb +1 -1
  19. data/lib/chef/dsl/recipe.rb +4 -2
  20. data/lib/chef/dsl/secret.rb +2 -4
  21. data/lib/chef/dsl/universal.rb +2 -0
  22. data/lib/chef/event_dispatch/base.rb +44 -2
  23. data/lib/chef/exceptions.rb +10 -0
  24. data/lib/chef/formatters/doc.rb +46 -0
  25. data/lib/chef/http/basic_client.rb +15 -7
  26. data/lib/chef/http.rb +7 -3
  27. data/lib/chef/provider/cron.rb +4 -1
  28. data/lib/chef/provider/file.rb +2 -0
  29. data/lib/chef/provider/git.rb +1 -1
  30. data/lib/chef/provider/ifconfig/debian.rb +1 -1
  31. data/lib/chef/provider/link.rb +2 -2
  32. data/lib/chef/provider/registry_key.rb +3 -2
  33. data/lib/chef/provider/remote_file/http.rb +1 -1
  34. data/lib/chef/provider/subversion.rb +5 -5
  35. data/lib/chef/provider/template.rb +1 -1
  36. data/lib/chef/resource/archive_file.rb +17 -14
  37. data/lib/chef/resource/chef_client_scheduled_task.rb +45 -2
  38. data/lib/chef/resource/chocolatey_config.rb +14 -14
  39. data/lib/chef/resource/chocolatey_feature.rb +1 -1
  40. data/lib/chef/resource/chocolatey_source.rb +24 -2
  41. data/lib/chef/resource/directory.rb +1 -1
  42. data/lib/chef/resource/file/verification/json.rb +50 -0
  43. data/lib/chef/resource/file/verification/yaml.rb +52 -0
  44. data/lib/chef/resource/habitat_install.rb +3 -3
  45. data/lib/chef/resource/inspec_input.rb +127 -0
  46. data/lib/chef/resource/inspec_waiver.rb +184 -0
  47. data/lib/chef/resource/inspec_waiver_file_entry.rb +1 -1
  48. data/lib/chef/resource/kernel_module.rb +27 -2
  49. data/lib/chef/resource/macos_userdefaults.rb +43 -128
  50. data/lib/chef/resource/mount.rb +1 -1
  51. data/lib/chef/resource/openssl_x509_certificate.rb +1 -1
  52. data/lib/chef/resource/powershell_package_source.rb +234 -70
  53. data/lib/chef/resource/registry_key.rb +36 -48
  54. data/lib/chef/resource/remote_file.rb +98 -2
  55. data/lib/chef/resource/timezone.rb +2 -2
  56. data/lib/chef/resource/user_ulimit.rb +1 -0
  57. data/lib/chef/resource/windows_auto_run.rb +1 -1
  58. data/lib/chef/resource/windows_dfs_namespace.rb +2 -2
  59. data/lib/chef/resource/windows_printer.rb +1 -1
  60. data/lib/chef/resource/windows_uac.rb +3 -1
  61. data/lib/chef/resource/windows_update_settings.rb +3 -3
  62. data/lib/chef/resource/windows_user_privilege.rb +1 -1
  63. data/lib/chef/resource.rb +1 -1
  64. data/lib/chef/resource_reporter.rb +1 -1
  65. data/lib/chef/resources.rb +2 -0
  66. data/lib/chef/run_context/cookbook_compiler.rb +112 -28
  67. data/lib/chef/run_context.rb +31 -1
  68. data/lib/chef/secret_fetcher/akeyless_vault.rb +57 -0
  69. data/lib/chef/secret_fetcher/aws_secrets_manager.rb +1 -1
  70. data/lib/chef/secret_fetcher/azure_key_vault.rb +63 -9
  71. data/lib/chef/secret_fetcher/base.rb +1 -1
  72. data/lib/chef/secret_fetcher/hashi_vault.rb +100 -0
  73. data/lib/chef/secret_fetcher.rb +8 -3
  74. data/lib/chef/version.rb +1 -1
  75. data/lib/chef/win32/version.rb +2 -1
  76. data/spec/data/archive_file/test_archive.tar.gz +0 -0
  77. data/spec/functional/dsl/reboot_pending_spec.rb +3 -3
  78. data/spec/functional/dsl/registry_helper_spec.rb +1 -1
  79. data/spec/functional/resource/archive_file_spec.rb +87 -0
  80. data/spec/functional/resource/dsc_script_spec.rb +2 -2
  81. data/spec/functional/resource/group_spec.rb +5 -1
  82. data/spec/functional/resource/link_spec.rb +8 -0
  83. data/spec/functional/resource/macos_userdefaults_spec.rb +119 -0
  84. data/spec/functional/resource/powershell_package_source_spec.rb +5 -6
  85. data/spec/functional/resource/registry_spec.rb +81 -81
  86. data/spec/functional/win32/registry_spec.rb +8 -8
  87. data/spec/integration/compliance/compliance_spec.rb +60 -0
  88. data/spec/spec_helper.rb +3 -0
  89. data/spec/support/platform_helpers.rb +4 -0
  90. data/spec/support/ruby_installer.rb +51 -0
  91. data/spec/unit/compliance/input_spec.rb +104 -0
  92. data/spec/unit/compliance/profile_spec.rb +120 -0
  93. data/spec/unit/compliance/waiver_spec.rb +104 -0
  94. data/spec/unit/data_collector_spec.rb +24 -1
  95. data/spec/unit/dsl/reboot_pending_spec.rb +1 -1
  96. data/spec/unit/http/basic_client_spec.rb +30 -0
  97. data/spec/unit/http_spec.rb +8 -2
  98. data/spec/unit/mixin/default_paths_spec.rb +1 -1
  99. data/spec/unit/mixin/securable_spec.rb +3 -3
  100. data/spec/unit/provider/cron_spec.rb +45 -0
  101. data/spec/unit/provider/link_spec.rb +13 -7
  102. data/spec/unit/provider/package/rubygems_spec.rb +5 -5
  103. data/spec/unit/provider/package/windows_spec.rb +1 -1
  104. data/spec/unit/provider/registry_key_spec.rb +4 -4
  105. data/spec/unit/provider/remote_file/http_spec.rb +10 -0
  106. data/spec/unit/provider/service/windows_spec.rb +5 -5
  107. data/spec/unit/provider/subversion_spec.rb +4 -4
  108. data/spec/unit/provider/template_spec.rb +2 -2
  109. data/spec/unit/provider/windows_env_spec.rb +1 -1
  110. data/spec/unit/provider/zypper_repository_spec.rb +1 -1
  111. data/spec/unit/resource/archive_file_spec.rb +414 -3
  112. data/spec/unit/resource/chef_client_scheduled_task_spec.rb +69 -0
  113. data/spec/unit/resource/chocolatey_config_spec.rb +1 -1
  114. data/spec/unit/resource/chocolatey_feature_spec.rb +1 -1
  115. data/spec/unit/resource/chocolatey_source_spec.rb +1 -1
  116. data/spec/unit/resource/file/verification/json_spec.rb +72 -0
  117. data/spec/unit/resource/file/verification/yaml_spec.rb +67 -0
  118. data/spec/unit/resource/inspec_input_spec.rb +300 -0
  119. data/spec/unit/resource/inspec_waiver_spec.rb +312 -0
  120. data/spec/unit/resource/kernel_module_spec.rb +2 -1
  121. data/spec/unit/resource/macos_user_defaults_spec.rb +36 -96
  122. data/spec/unit/resource/mount_spec.rb +10 -0
  123. data/spec/unit/resource/powershell_package_source_spec.rb +63 -62
  124. data/spec/unit/resource/registry_key_spec.rb +10 -10
  125. data/spec/unit/resource/user_ulimit_spec.rb +14 -1
  126. data/spec/unit/resource/windows_auto_run_spec.rb +1 -1
  127. data/spec/unit/resource/windows_feature_powershell_spec.rb +1 -1
  128. data/spec/unit/resource/windows_firewall_rule_spec.rb +2 -2
  129. data/spec/unit/resource/windows_task_spec.rb +3 -3
  130. data/spec/unit/resource_reporter_spec.rb +2 -2
  131. data/spec/unit/resource_spec.rb +5 -0
  132. data/spec/unit/secret_fetcher/akeyless_vault_spec.rb +37 -0
  133. data/spec/unit/secret_fetcher/azure_key_vault_spec.rb +99 -20
  134. data/spec/unit/secret_fetcher/hashi_vault_spec.rb +80 -0
  135. data/spec/unit/util/backup_spec.rb +1 -1
  136. data/spec/unit/win32/registry_spec.rb +3 -3
  137. data/tasks/rspec.rb +2 -1
  138. metadata +75 -6
@@ -0,0 +1,312 @@
1
+ #
2
+ # Copyright:: Copyright (c) Chef Software Inc.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ require "spec_helper"
19
+
20
+ describe Chef::Resource::InspecWaiver do
21
+ def load_waiver(filename)
22
+ path = "/var/chef/cache/cookbooks/acme_compliance/compliance/waivers/#{filename}"
23
+ run_context.waiver_collection << Chef::Compliance::Waiver.from_yaml(events, waiver_yaml, path, "acme_compliance")
24
+ end
25
+
26
+ let(:node) { Chef::Node.new }
27
+ let(:events) { Chef::EventDispatch::Dispatcher.new }
28
+ let(:run_context) do
29
+ Chef::RunContext.new(node, {}, events).tap do |rc|
30
+ end
31
+ end
32
+ let(:collection) { double("resource collection") }
33
+ let(:waiver_yaml) do
34
+ <<~EOH
35
+ ssh-01:
36
+ expiration_date: 2033-07-31
37
+ run: false
38
+ justification: "waived, yo"
39
+ EOH
40
+ end
41
+ let(:waiver_json) do
42
+ <<~EOH
43
+ { "ssh-01": {
44
+ "expiration_date": "2033-07-31",
45
+ "run": false,
46
+ "justification": "waived, yo"
47
+ } }
48
+ EOH
49
+ end
50
+ let(:waiver_toml) do
51
+ <<~EOH
52
+ [ssh-01]
53
+ expiration_date = 2033-07-31T00:00:00.000Z
54
+ run = false
55
+ justification = "waived, yo"
56
+ EOH
57
+ end
58
+ let(:waiver_hash) do
59
+ { "ssh-01" => {
60
+ "expiration_date" => "2033-07-31",
61
+ "run" => false,
62
+ "justification" => "waived, yo",
63
+ } }
64
+ end
65
+ let(:resource) do
66
+ Chef::Resource::InspecWaiver.new("ssh-01", run_context)
67
+ end
68
+ let(:provider) { resource.provider_for_action(:add) }
69
+
70
+ before do
71
+ allow(run_context).to receive(:resource_collection).and_return(collection)
72
+ end
73
+
74
+ it "sets the default action as :add" do
75
+ expect(resource.action).to eql([:add])
76
+ end
77
+
78
+ context "with a waiver in a cookbook" do
79
+ it "enables the waiver by the name of the cookbook" do
80
+ load_waiver("default.yml")
81
+ resource.name "acme_compliance"
82
+ resource.run_action(:add)
83
+ expect(run_context.waiver_collection.first).to be_enabled
84
+ expect(resource).not_to be_updated_by_last_action
85
+ end
86
+
87
+ it "enables the waiver with a regular expression for the cookbook" do
88
+ load_waiver("default.yml")
89
+ resource.name "acme_comp.*"
90
+ resource.run_action(:add)
91
+ expect(run_context.waiver_collection.first).to be_enabled
92
+ expect(resource).not_to be_updated_by_last_action
93
+ end
94
+
95
+ it "enables the waiver with an explicit name" do
96
+ load_waiver("default.yml")
97
+ resource.name "acme_compliance::default"
98
+ resource.run_action(:add)
99
+ expect(run_context.waiver_collection.first).to be_enabled
100
+ expect(resource).not_to be_updated_by_last_action
101
+ end
102
+
103
+ it "fails when the cookbook name is wrong" do
104
+ load_waiver("default.yml")
105
+ resource.name "evil_compliance"
106
+ expect { resource.run_action(:add) }.to raise_error(StandardError)
107
+ end
108
+
109
+ it "enables the waiver when its not named default" do
110
+ load_waiver("ssh01.yml")
111
+ resource.name "acme_compliance::ssh01"
112
+ resource.run_action(:add)
113
+ expect(run_context.waiver_collection.first).to be_enabled
114
+ expect(resource).not_to be_updated_by_last_action
115
+ end
116
+
117
+ it "fails when it is not named default and you attempt to enable the default" do
118
+ load_waiver("ssh01.yml")
119
+ resource.name "acme_compliance"
120
+ expect { resource.run_action(:add) }.to raise_error(StandardError)
121
+ end
122
+
123
+ it "succeeds with a regexp that matches the cookbook name" do
124
+ load_waiver("ssh01.yml")
125
+ resource.name "acme_comp.*::ssh01"
126
+ resource.run_action(:add)
127
+ expect(run_context.waiver_collection.first).to be_enabled
128
+ expect(resource).not_to be_updated_by_last_action
129
+ end
130
+
131
+ it "succeeds with a regexp that matches the file name" do
132
+ load_waiver("ssh01.yml")
133
+ resource.name "acme_compliance::ssh.*"
134
+ resource.run_action(:add)
135
+ expect(run_context.waiver_collection.first).to be_enabled
136
+ expect(resource).not_to be_updated_by_last_action
137
+ end
138
+
139
+ it "succeeds with a regexps for both the file name and cookbook name" do
140
+ load_waiver("ssh01.yml")
141
+ resource.name "acme_comp.*::ssh.*"
142
+ resource.run_action(:add)
143
+ expect(run_context.waiver_collection.first).to be_enabled
144
+ expect(resource).not_to be_updated_by_last_action
145
+ end
146
+
147
+ it "fails with regexps that do not match" do
148
+ load_waiver("ssh01.yml")
149
+ resource.name "evil_comp.*::etcd.*"
150
+ expect { resource.run_action(:add) }.to raise_error(StandardError)
151
+ end
152
+
153
+ it "substring matches without regexps should fail when they are at the end" do
154
+ load_waiver("ssh01.yml")
155
+ resource.name "acme_complianc::ssh0"
156
+ expect { resource.run_action(:add) }.to raise_error(StandardError)
157
+ end
158
+
159
+ it "substring matches without regexps should fail when they are at the start" do
160
+ load_waiver("ssh01.yml")
161
+ resource.name "cme_compliance::sh01"
162
+ expect { resource.run_action(:add) }.to raise_error(StandardError)
163
+ end
164
+ end
165
+
166
+ context "with a waiver in a file" do
167
+ it "loads a YAML file" do
168
+ tempfile = Tempfile.new(["spec-compliance-test", ".yaml"])
169
+ tempfile.write waiver_yaml
170
+ tempfile.close
171
+ resource.name tempfile.path
172
+
173
+ resource.run_action(:add)
174
+
175
+ expect(run_context.waiver_collection.first).to be_enabled
176
+ expect(run_context.waiver_collection.size).to be 1
177
+ expect(run_context.waiver_collection.first.cookbook_name).to be nil
178
+ expect(run_context.waiver_collection.first.path).to be nil
179
+ expect(run_context.waiver_collection.first.pathname).to be nil
180
+ expect(resource).not_to be_updated_by_last_action
181
+ end
182
+
183
+ it "loads a YAML file in a source attribute" do
184
+ tempfile = Tempfile.new(["spec-compliance-test", ".yaml"])
185
+ tempfile.write waiver_yaml
186
+ tempfile.close
187
+ resource.name "my-resource-name"
188
+ resource.source tempfile.path
189
+
190
+ resource.run_action(:add)
191
+
192
+ expect(run_context.waiver_collection.first).to be_enabled
193
+ expect(run_context.waiver_collection.size).to be 1
194
+ expect(run_context.waiver_collection.first.cookbook_name).to be nil
195
+ expect(run_context.waiver_collection.first.path).to be nil
196
+ expect(run_context.waiver_collection.first.pathname).to be nil
197
+ expect(resource).not_to be_updated_by_last_action
198
+ end
199
+
200
+ it "loads a YML file" do
201
+ tempfile = Tempfile.new(["spec-compliance-test", ".yml"])
202
+ tempfile.write waiver_yaml
203
+ tempfile.close
204
+ resource.name tempfile.path
205
+
206
+ resource.run_action(:add)
207
+
208
+ expect(run_context.waiver_collection.first).to be_enabled
209
+ expect(run_context.waiver_collection.size).to be 1
210
+ expect(run_context.waiver_collection.first.cookbook_name).to be nil
211
+ expect(run_context.waiver_collection.first.path).to be nil
212
+ expect(run_context.waiver_collection.first.pathname).to be nil
213
+ expect(resource).not_to be_updated_by_last_action
214
+ end
215
+
216
+ it "loads a YML file using the source attribute" do
217
+ tempfile = Tempfile.new(["spec-compliance-test", ".yml"])
218
+ tempfile.write waiver_yaml
219
+ tempfile.close
220
+ resource.name "my-resource-name"
221
+ resource.source tempfile.path
222
+
223
+ resource.run_action(:add)
224
+
225
+ expect(run_context.waiver_collection.first).to be_enabled
226
+ expect(run_context.waiver_collection.size).to be 1
227
+ expect(run_context.waiver_collection.first.cookbook_name).to be nil
228
+ expect(run_context.waiver_collection.first.path).to be nil
229
+ expect(run_context.waiver_collection.first.pathname).to be nil
230
+ expect(resource).not_to be_updated_by_last_action
231
+ end
232
+
233
+ it "loads a JSON file" do
234
+ tempfile = Tempfile.new(["spec-compliance-test", ".json"])
235
+ tempfile.write waiver_json
236
+ tempfile.close
237
+ resource.name tempfile.path
238
+
239
+ resource.run_action(:add)
240
+
241
+ expect(run_context.waiver_collection.first).to be_enabled
242
+ expect(run_context.waiver_collection.size).to be 1
243
+ expect(run_context.waiver_collection.first.cookbook_name).to be nil
244
+ expect(run_context.waiver_collection.first.path).to be nil
245
+ expect(run_context.waiver_collection.first.pathname).to be nil
246
+ expect(resource).not_to be_updated_by_last_action
247
+ end
248
+
249
+ it "loads a JSON file using the source attribute" do
250
+ tempfile = Tempfile.new(["spec-compliance-test", ".json"])
251
+ tempfile.write waiver_json
252
+ tempfile.close
253
+ resource.name "my-resource-name"
254
+ resource.source tempfile.path
255
+
256
+ resource.run_action(:add)
257
+
258
+ expect(run_context.waiver_collection.first).to be_enabled
259
+ expect(run_context.waiver_collection.size).to be 1
260
+ expect(run_context.waiver_collection.first.cookbook_name).to be nil
261
+ expect(run_context.waiver_collection.first.path).to be nil
262
+ expect(run_context.waiver_collection.first.pathname).to be nil
263
+ expect(resource).not_to be_updated_by_last_action
264
+ end
265
+
266
+ it "loads a TOML file" do
267
+ tempfile = Tempfile.new(["spec-compliance-test", ".toml"])
268
+ tempfile.write waiver_toml
269
+ tempfile.close
270
+ resource.name tempfile.path
271
+
272
+ resource.run_action(:add)
273
+
274
+ expect(run_context.waiver_collection.first).to be_enabled
275
+ expect(run_context.waiver_collection.size).to be 1
276
+ expect(run_context.waiver_collection.first.cookbook_name).to be nil
277
+ expect(run_context.waiver_collection.first.path).to be nil
278
+ expect(run_context.waiver_collection.first.pathname).to be nil
279
+ expect(resource).not_to be_updated_by_last_action
280
+ end
281
+
282
+ it "loads a TOML file using the source attribute" do
283
+ tempfile = Tempfile.new(["spec-compliance-test", ".toml"])
284
+ tempfile.write waiver_toml
285
+ tempfile.close
286
+ resource.name "my-resource-name"
287
+ resource.source tempfile.path
288
+
289
+ resource.run_action(:add)
290
+
291
+ expect(run_context.waiver_collection.first).to be_enabled
292
+ expect(run_context.waiver_collection.size).to be 1
293
+ expect(run_context.waiver_collection.first.cookbook_name).to be nil
294
+ expect(run_context.waiver_collection.first.path).to be nil
295
+ expect(run_context.waiver_collection.first.pathname).to be nil
296
+ expect(resource).not_to be_updated_by_last_action
297
+ end
298
+
299
+ it "loads a Hash" do
300
+ resource.source waiver_hash
301
+
302
+ resource.run_action(:add)
303
+
304
+ expect(run_context.waiver_collection.first).to be_enabled
305
+ expect(run_context.waiver_collection.size).to be 1
306
+ expect(run_context.waiver_collection.first.cookbook_name).to be nil
307
+ expect(run_context.waiver_collection.first.path).to be nil
308
+ expect(run_context.waiver_collection.first.pathname).to be nil
309
+ expect(resource).not_to be_updated_by_last_action
310
+ end
311
+ end
312
+ end
@@ -32,10 +32,11 @@ describe Chef::Resource::KernelModule do
32
32
  expect(resource.modname).to eql("foo")
33
33
  end
34
34
 
35
- it "supports :create and :flush actions" do
35
+ it "supports various actions" do
36
36
  expect { resource.action :install }.not_to raise_error
37
37
  expect { resource.action :uninstall }.not_to raise_error
38
38
  expect { resource.action :blacklist }.not_to raise_error
39
+ expect { resource.action :enable }.not_to raise_error
39
40
  expect { resource.action :disable }.not_to raise_error
40
41
  expect { resource.action :load }.not_to raise_error
41
42
  expect { resource.action :unload }.not_to raise_error
@@ -17,120 +17,60 @@
17
17
 
18
18
  require "spec_helper"
19
19
 
20
- describe Chef::Resource::MacosUserDefaults do
21
-
22
- let(:resource) { Chef::Resource::MacosUserDefaults.new("foo") }
23
- let(:provider) { resource.provider_for_action(:write) }
24
-
25
- it "has a resource name of :macos_userdefaults" do
26
- expect(resource.resource_name).to eq(:macos_userdefaults)
27
- end
28
-
29
- it "the domain property defaults to NSGlobalDomain" do
30
- expect(resource.domain).to eq("NSGlobalDomain")
31
- end
32
-
33
- it "the value property coerces keys in hashes to strings so we can compare them with plist data" do
34
- resource.value "User": "/Library/Managed Installs/way_fake.log"
35
- expect(resource.value).to eq({ "User" => "/Library/Managed Installs/way_fake.log" })
36
- end
37
-
38
- it "the host property defaults to nil" do
39
- expect(resource.host).to be_nil
40
- end
41
-
42
- it "the sudo property defaults to false" do
43
- expect(resource.sudo).to be false
44
- end
45
-
46
- it "sets the default action as :write" do
47
- expect(resource.action).to eq([:write])
48
- end
49
-
50
- it "supports :write action" do
51
- expect { resource.action :write }.not_to raise_error
52
- end
53
-
54
- describe "#defaults_export_cmd" do
55
- it "exports NSGlobalDomain if no domain is set" do
56
- expect(provider.defaults_export_cmd(resource)).to eq(["/usr/bin/defaults", "export", "NSGlobalDomain", "-"])
20
+ describe Chef::Resource::MacosUserDefaults, :macos_only, requires_root: true do
21
+ let(:test_value) { "fakest_key_value" }
22
+ let(:test_key) { "fakest_key" }
23
+ let(:node) { Chef::Node.new }
24
+ let(:events) { Chef::EventDispatch::Dispatcher.new }
25
+ let(:run_context) { Chef::RunContext.new(node, {}, events) }
26
+ let(:resource) {
27
+ Chef::Resource::MacosUserDefaults.new("foo", run_context).tap do |r|
28
+ r.value test_value
29
+ r.key test_key
57
30
  end
31
+ }
58
32
 
59
- it "exports a provided domain" do
60
- resource.domain "com.tim"
61
- expect(provider.defaults_export_cmd(resource)).to eq(["/usr/bin/defaults", "export", "com.tim", "-"])
33
+ context "has a default value" do
34
+ it ":macos_userdefaults for resource name" do
35
+ expect(resource.resource_name).to eq(:macos_userdefaults)
62
36
  end
63
37
 
64
- it "sets -currentHost if host is 'current'" do
65
- resource.host "current"
66
- expect(provider.defaults_export_cmd(resource)).to eq(["/usr/bin/defaults", "-currentHost", "export", "NSGlobalDomain", "-"])
38
+ it "NSGlobalDomain for the domain property" do
39
+ expect(resource.domain).to eq("NSGlobalDomain")
67
40
  end
68
41
 
69
- it "sets -host 'tim-laptop if host is 'tim-laptop'" do
70
- resource.host "tim-laptop"
71
- expect(provider.defaults_export_cmd(resource)).to eq(["/usr/bin/defaults", "-host", "tim-laptop", "export", "NSGlobalDomain", "-"])
72
- end
73
- end
74
-
75
- describe "#defaults_modify_cmd" do
76
- # avoid needing to set these required values over and over. We'll overwrite them where necessary
77
- before do
78
- resource.key = "foo"
79
- resource.value = "bar"
80
- end
81
-
82
- it "writes to NSGlobalDomain if domain isn't specified" do
83
- expect(provider.defaults_modify_cmd).to eq(["/usr/bin/defaults", "write", "NSGlobalDomain", "foo", "-string", "bar"])
84
- end
85
-
86
- it "uses the domain property if set" do
87
- resource.domain = "MyCustomDomain"
88
- expect(provider.defaults_modify_cmd).to eq(["/usr/bin/defaults", "write", "MyCustomDomain", "foo", "-string", "bar"])
42
+ it "nil for the host property" do
43
+ expect(resource.host).to be_nil
89
44
  end
90
45
 
91
- it "sets host specific values using host property" do
92
- resource.host = "tims_laptop"
93
- expect(provider.defaults_modify_cmd).to eq(["/usr/bin/defaults", "-host", "tims_laptop", "write", "NSGlobalDomain", "foo", "-string", "bar"])
46
+ it "nil for the user property" do
47
+ expect(resource.user).to be_nil
94
48
  end
95
49
 
96
- it "if host is set to :current it passes CurrentHost" do
97
- resource.host = :current
98
- expect(provider.defaults_modify_cmd).to eq(["/usr/bin/defaults", "-currentHost", "write", "NSGlobalDomain", "foo", "-string", "bar"])
99
- end
100
-
101
- it "raises ArgumentError if bool is specified, but the value can't be made into a bool" do
102
- resource.type "bool"
103
- expect { provider.defaults_modify_cmd }.to raise_error(ArgumentError)
104
- end
105
-
106
- it "autodetects array type and passes individual values" do
107
- resource.value = %w{one two three}
108
- expect(provider.defaults_modify_cmd).to eq(["/usr/bin/defaults", "write", "NSGlobalDomain", "foo", "-array", "one", "two", "three"])
109
- end
110
-
111
- it "autodetects string type and passes a single value" do
112
- resource.value = "one"
113
- expect(provider.defaults_modify_cmd).to eq(["/usr/bin/defaults", "write", "NSGlobalDomain", "foo", "-string", "one"])
50
+ it ":write for resource action" do
51
+ expect(resource.action).to eq([:write])
114
52
  end
53
+ end
115
54
 
116
- it "autodetects integer type and passes a single value" do
117
- resource.value = 1
118
- expect(provider.defaults_modify_cmd).to eq(["/usr/bin/defaults", "write", "NSGlobalDomain", "foo", "-int", 1])
55
+ context ":write" do
56
+ it "is a supported action" do
57
+ expect { resource.action :write }.not_to raise_error
119
58
  end
120
59
 
121
- it "autodetects boolean type from TrueClass value and passes a 'TRUE' string" do
122
- resource.value = true
123
- expect(provider.defaults_modify_cmd).to eq(["/usr/bin/defaults", "write", "NSGlobalDomain", "foo", "-bool", "TRUE"])
60
+ it "successfully updates the preference" do
61
+ resource.run_action(:write)
62
+ expect(resource.get_preference resource).eql? test_value
124
63
  end
64
+ end
125
65
 
126
- it "autodetects boolean type from FalseClass value and passes a 'FALSE' string" do
127
- resource.value = false
128
- expect(provider.defaults_modify_cmd).to eq(["/usr/bin/defaults", "write", "NSGlobalDomain", "foo", "-bool", "FALSE"])
66
+ context ":delete" do
67
+ it "is a supported action" do
68
+ expect { resource.action :delete }.not_to raise_error
129
69
  end
130
70
 
131
- it "autodetects dict type from Hash value and flattens keys & values" do
132
- resource.value = { "foo" => "bar" }
133
- expect(provider.defaults_modify_cmd).to eq(["/usr/bin/defaults", "write", "NSGlobalDomain", "foo", "-dict", "foo", "bar"])
71
+ it "successfully deletes the preference" do
72
+ resource.run_action(:delete)
73
+ expect(resource.get_preference resource).to be_nil
134
74
  end
135
75
  end
136
76
  end
@@ -59,6 +59,16 @@ describe Chef::Resource::Mount do
59
59
  expect(resource.mount_point).to eql("//192.168.11.102/Share/backup")
60
60
  end
61
61
 
62
+ it "does not strip slash when mount_point is root directory" do
63
+ resource.mount_point "/"
64
+ expect(resource.mount_point).to eql("/")
65
+ end
66
+
67
+ it "does not strip slash when mount_point is root of network mount" do
68
+ resource.mount_point "127.0.0.1:/"
69
+ expect(resource.mount_point).to eql("127.0.0.1:/")
70
+ end
71
+
62
72
  it "raises error when mount_point property is not set" do
63
73
  expect { resource.mount_point nil }.to raise_error(Chef::Exceptions::ValidationFailed, "Property mount_point must be one of: String! You passed nil.")
64
74
  end