chefspec 4.2.0 → 4.7.0
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.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +39 -16
- data/CHANGELOG.md +136 -72
- data/README.md +187 -13
- data/chefspec.gemspec +5 -5
- data/examples/batch/spec/run_spec.rb +5 -0
- data/examples/chef_gem/recipes/install.rb +8 -3
- data/examples/chef_gem/recipes/purge.rb +6 -3
- data/examples/chef_gem/recipes/reconfig.rb +6 -3
- data/examples/chef_gem/recipes/remove.rb +6 -3
- data/examples/chef_gem/recipes/upgrade.rb +6 -3
- data/examples/chocolatey_package/recipes/install.rb +6 -0
- data/examples/chocolatey_package/recipes/remove.rb +8 -0
- data/examples/chocolatey_package/recipes/upgrade.rb +8 -0
- data/examples/chocolatey_package/spec/install_spec.rb +21 -0
- data/examples/chocolatey_package/spec/remove_spec.rb +20 -0
- data/examples/chocolatey_package/spec/upgrade_spec.rb +20 -0
- data/examples/directory/recipes/create.rb +4 -0
- data/examples/directory/spec/create_spec.rb +4 -0
- data/examples/dpkg_package/spec/install_spec.rb +5 -0
- data/examples/dpkg_package/spec/purge_spec.rb +5 -0
- data/examples/dpkg_package/spec/remove_spec.rb +5 -0
- data/examples/dsc_resource/recipes/default.rb +20 -0
- data/examples/dsc_resource/spec/default_spec.rb +40 -0
- data/examples/env/spec/create_spec.rb +5 -0
- data/examples/env/spec/delete_spec.rb +5 -0
- data/examples/env/spec/modify_spec.rb +5 -0
- data/examples/ips_package/spec/install_spec.rb +5 -0
- data/examples/ips_package/spec/remove_spec.rb +5 -0
- data/examples/ips_package/spec/upgrade_spec.rb +5 -0
- data/examples/package/recipes/install.rb +4 -0
- data/examples/package/recipes/purge.rb +4 -0
- data/examples/package/recipes/reconfig.rb +4 -0
- data/examples/package/recipes/remove.rb +4 -0
- data/examples/package/recipes/upgrade.rb +4 -0
- data/examples/package/spec/install_spec.rb +4 -0
- data/examples/package/spec/purge_spec.rb +4 -0
- data/examples/package/spec/reconfig_spec.rb +4 -0
- data/examples/package/spec/remove_spec.rb +4 -0
- data/examples/package/spec/upgrade_spec.rb +4 -0
- data/examples/pacman_package/spec/install_spec.rb +5 -0
- data/examples/pacman_package/spec/purge_spec.rb +5 -0
- data/examples/pacman_package/spec/remove_spec.rb +5 -0
- data/examples/pacman_package/spec/upgrade_spec.rb +5 -0
- data/examples/powershell_script/spec/run_spec.rb +5 -0
- data/examples/reboot/recipes/cancel.rb +3 -0
- data/examples/reboot/recipes/now.rb +3 -0
- data/examples/reboot/recipes/request.rb +3 -0
- data/examples/reboot/spec/cancel_spec.rb +10 -0
- data/examples/reboot/spec/now_spec.rb +10 -0
- data/examples/reboot/spec/request_spec.rb +10 -0
- data/examples/render_file/spec/default_spec.rb +40 -0
- data/examples/rpm_package/spec/install_spec.rb +5 -0
- data/examples/rpm_package/spec/remove_spec.rb +5 -0
- data/examples/rpm_package/spec/upgrade_spec.rb +5 -0
- data/examples/server/spec/node_spec.rb +1 -1
- data/examples/server/spec/search_spec.rb +4 -0
- data/examples/step_into/providers/provides.rb +3 -0
- data/examples/step_into/recipes/default.rb +2 -0
- data/examples/step_into/resources/lwrp.rb +1 -1
- data/examples/step_into/resources/provides.rb +6 -0
- data/examples/step_into/spec/default_spec.rb +10 -2
- data/examples/windows_package/recipes/install.rb +13 -0
- data/examples/windows_package/recipes/remove.rb +13 -0
- data/examples/windows_package/spec/install_spec.rb +24 -0
- data/examples/windows_package/spec/remove_spec.rb +24 -0
- data/examples/windows_service/recipes/configure_startup.rb +13 -0
- data/examples/windows_service/recipes/disable.rb +13 -0
- data/examples/windows_service/recipes/enable.rb +13 -0
- data/examples/windows_service/recipes/reload.rb +13 -0
- data/examples/windows_service/recipes/restart.rb +13 -0
- data/examples/windows_service/recipes/start.rb +13 -0
- data/examples/windows_service/recipes/stop.rb +13 -0
- data/examples/windows_service/spec/configure_startup_spec.rb +24 -0
- data/examples/windows_service/spec/disable_spec.rb +24 -0
- data/examples/windows_service/spec/enable_spec.rb +24 -0
- data/examples/windows_service/spec/reload_spec.rb +24 -0
- data/examples/windows_service/spec/restart_spec.rb +24 -0
- data/examples/windows_service/spec/start_spec.rb +24 -0
- data/examples/windows_service/spec/stop_spec.rb +24 -0
- data/features/chocolatey_package.feature +29 -0
- data/features/dsc_resource.feature +18 -0
- data/features/reboot.feature +19 -0
- data/features/step_definitions/background_steps.rb +4 -1
- data/features/step_into.feature +7 -0
- data/features/support/env.rb +5 -4
- data/features/windows_package.feature +24 -0
- data/features/windows_service.feature +23 -0
- data/gemfiles/chefspec.gemfile +9 -0
- data/lib/chefspec/api/chocolatey_package.rb +107 -0
- data/lib/chefspec/api/directory.rb +10 -10
- data/lib/chefspec/api/dsc_resource.rb +49 -0
- data/lib/chefspec/api/group.rb +5 -5
- data/lib/chefspec/api/link.rb +10 -10
- data/lib/chefspec/api/mdadm.rb +5 -5
- data/lib/chefspec/api/reboot.rb +18 -0
- data/lib/chefspec/api/remote_directory.rb +10 -10
- data/lib/chefspec/api/route.rb +5 -5
- data/lib/chefspec/api/user.rb +5 -5
- data/lib/chefspec/api/windows_package.rb +86 -0
- data/lib/chefspec/api/windows_service.rb +286 -0
- data/lib/chefspec/api.rb +5 -0
- data/lib/chefspec/berkshelf.rb +7 -1
- data/lib/chefspec/coverage/filters.rb +2 -1
- data/lib/chefspec/coverage.rb +1 -1
- data/lib/chefspec/deprecations.rb +6 -2
- data/lib/chefspec/errors.rb +4 -0
- data/lib/chefspec/extensions/chef/lwrp_base.rb +23 -0
- data/lib/chefspec/file_cache_path_proxy.rb +15 -0
- data/lib/chefspec/matchers/render_file_matcher.rb +20 -2
- data/lib/chefspec/matchers/resource_matcher.rb +9 -2
- data/lib/chefspec/mixins/normalize.rb +7 -1
- data/lib/chefspec/policyfile.rb +62 -0
- data/lib/chefspec/renderer.rb +2 -4
- data/lib/chefspec/rspec.rb +3 -0
- data/lib/chefspec/server_methods.rb +1 -0
- data/lib/chefspec/server_runner.rb +9 -0
- data/lib/chefspec/solo_runner.rb +13 -3
- data/lib/chefspec/version.rb +1 -1
- data/lib/chefspec.rb +1 -1
- data/spec/unit/coverage/filters_spec.rb +60 -0
- data/spec/unit/deprecations_spec.rb +3 -1
- data/spec/unit/macros_spec.rb +1 -1
- data/spec/unit/matchers/render_file_matcher_spec.rb +11 -0
- data/spec/unit/renderer_spec.rb +20 -2
- data/spec/unit/server_runner_spec.rb +28 -0
- data/spec/unit/solo_runner_spec.rb +3 -1
- data/templates/errors/invalid_berkshelf_options.erb +4 -0
- data/templates/errors/may_need_to_specify_platform.erb +25 -0
- metadata +72 -18
- data/gemfiles/chef-11.14.0.gemfile +0 -5
- data/gemfiles/chef-11.16.0.gemfile +0 -5
- data/gemfiles/chef-12.0.0.gemfile +0 -5
- data/gemfiles/chef-master.gemfile +0 -5
data/README.md
CHANGED
|
@@ -2,13 +2,9 @@ ChefSpec
|
|
|
2
2
|
========
|
|
3
3
|
[][gem]
|
|
4
4
|
[][travis]
|
|
5
|
-
[][gemnasium]
|
|
6
|
-
[][codeclimate]
|
|
7
5
|
|
|
8
6
|
[gem]: https://rubygems.org/gems/chefspec
|
|
9
7
|
[travis]: http://travis-ci.org/sethvargo/chefspec
|
|
10
|
-
[gemnasium]: https://gemnasium.com/sethvargo/chefspec
|
|
11
|
-
[codeclimate]: https://codeclimate.com/github/sethvargo/chefspec
|
|
12
8
|
|
|
13
9
|
ChefSpec is a unit testing framework for testing Chef cookbooks. ChefSpec makes it easy to write examples and get fast feedback on cookbook changes without the need for virtual machines or cloud servers.
|
|
14
10
|
|
|
@@ -32,7 +28,16 @@ Important Notes
|
|
|
32
28
|
- **ChefSpec 3.0+ requires Ruby 1.9 or higher!**
|
|
33
29
|
- **This documentation corresponds to the master branch, which may be unreleased. Please check the README of the latest git tag or the gem's source for your version's documentation!**
|
|
34
30
|
- **Each resource matcher is self-documented using [Yard](http://rubydoc.info/github/sethvargo/chefspec) and has a corresponding aruba test from the [examples directory](https://github.com/sethvargo/chefspec/tree/master/examples).**
|
|
35
|
-
- **ChefSpec aims to maintain
|
|
31
|
+
- **ChefSpec aims to maintain compatibility with the two most recent minor versions of Chef.** If you are running an older version of Chef it may work, or you will need to run an older version of ChefSpec.
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
Notes on Compatibility with Chef Versions
|
|
35
|
+
-----------------------------------------
|
|
36
|
+
As a general rule, if it is tested in the Travis CI matrix, it is a supported version. The section below details any specific versions that are _not_ supported and why:
|
|
37
|
+
|
|
38
|
+
- Chef 12 prior to Chef 12.0.2 is not supported due to the lack of a declared resource type. This was fixed in [Chef 12.0.2](https://github.com/chef/chef/blob/12.0.2/lib/chef/resource.rb#L422-428).
|
|
39
|
+
|
|
40
|
+
Additionally, if you look at a cucumber feature and see a tag like `@not_chef_x_y_z`, that means that particular functionality is not supported on those versions of Chef.
|
|
36
41
|
|
|
37
42
|
|
|
38
43
|
Writing a Cookbook Example
|
|
@@ -84,6 +89,9 @@ RSpec.configure do |config|
|
|
|
84
89
|
# Specify the path for Chef Solo to find environments (default: [ascending search])
|
|
85
90
|
config.environment_path = '/var/environments'
|
|
86
91
|
|
|
92
|
+
# Specify the path for Chef Solo file cache path (default: nil)
|
|
93
|
+
config.file_cache_path = '/var/chef/cache'
|
|
94
|
+
|
|
87
95
|
# Specify the Chef log_level (default: :warn)
|
|
88
96
|
config.log_level = :debug
|
|
89
97
|
|
|
@@ -137,6 +145,16 @@ Requiring this file will:
|
|
|
137
145
|
- Download all the dependencies listed in your `Berksfile` into the temporary directory
|
|
138
146
|
- Set ChefSpec's `cookbook_path` to the temporary directory
|
|
139
147
|
|
|
148
|
+
You can customize the list of options passed to the installation command using the `berkshelf_options` RSpec configuration:
|
|
149
|
+
|
|
150
|
+
```ruby
|
|
151
|
+
RSpec.configuration do |config|
|
|
152
|
+
config.berkshelf_options = { only: "my-group" }
|
|
153
|
+
end
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
This is a Ruby hash and valid options include `only` and `except`.
|
|
157
|
+
|
|
140
158
|
### Librarian
|
|
141
159
|
|
|
142
160
|
If you are using Librarian, simply require `chefspec/librarian` in your `spec_helper` after requiring `chefspec`:
|
|
@@ -158,11 +176,34 @@ have to write your `Cheffile` like this:
|
|
|
158
176
|
|
|
159
177
|
```ruby
|
|
160
178
|
# Cheffile
|
|
161
|
-
site '
|
|
179
|
+
site 'https://supermarket.chef.io/api/v1'
|
|
162
180
|
|
|
163
181
|
cookbook 'name_of_your_cookbook', path: '.'
|
|
164
182
|
```
|
|
165
183
|
|
|
184
|
+
### Policyfile
|
|
185
|
+
If you are using Chef Policies with ChefDK, simply require `chefspec/policyfile` in your `spec_helper`, and ensure you are using the `ChefSpec::ServerRunner` - Chef Solo does not support the exported repository format because the cookbook names use the unique version identifier.
|
|
186
|
+
|
|
187
|
+
```ruby
|
|
188
|
+
# spec_helper.rb
|
|
189
|
+
require 'chefspec'
|
|
190
|
+
require 'chefspec/policyfile'
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
Requiring this file will:
|
|
194
|
+
|
|
195
|
+
- Create a temporary working directory
|
|
196
|
+
- Download all the dependencies listed in your `Policyfile.rb` into the temporary directory
|
|
197
|
+
- Set ChefSpec's `cookbook_path` to the temporary directory
|
|
198
|
+
|
|
199
|
+
Your `Policyfile.rb` should look something like this:
|
|
200
|
+
|
|
201
|
+
```ruby
|
|
202
|
+
name 'my-cookbook'
|
|
203
|
+
run_list 'my-cookbook::default'
|
|
204
|
+
default_source :community
|
|
205
|
+
cookbook 'my-cookbook', path: '.'
|
|
206
|
+
```
|
|
166
207
|
|
|
167
208
|
Running Specs
|
|
168
209
|
-------------
|
|
@@ -213,7 +254,7 @@ require 'chefspec'
|
|
|
213
254
|
describe 'example::default' do
|
|
214
255
|
let(:chef_run) { ChefSpec::SoloRunner.converge(described_recipe) }
|
|
215
256
|
|
|
216
|
-
it '
|
|
257
|
+
it 'installs apache2' do
|
|
217
258
|
expect(chef_run).to install_package('apache2')
|
|
218
259
|
end
|
|
219
260
|
end
|
|
@@ -229,7 +270,7 @@ require 'chefspec'
|
|
|
229
270
|
describe 'example::default' do
|
|
230
271
|
let(:chef_run) { ChefSpec::SoloRunner.converge(described_recipe) }
|
|
231
272
|
|
|
232
|
-
it '
|
|
273
|
+
it 'adds the member vagrant to the docker group' do
|
|
233
274
|
expect(chef_run).to modify_group('docker').with(members: ['vagrant'])
|
|
234
275
|
end
|
|
235
276
|
end
|
|
@@ -248,6 +289,24 @@ Assert that the Chef run included a recipe from another cookbook
|
|
|
248
289
|
expect(chef_run).to include_recipe('other_cookbook::recipe')
|
|
249
290
|
```
|
|
250
291
|
|
|
292
|
+
Keep the resources from an included recipe from being loaded into the Chef run, but test that the recipe was included
|
|
293
|
+
|
|
294
|
+
```ruby
|
|
295
|
+
describe 'example::default' do
|
|
296
|
+
let(:chef_run) { ChefSpec::SoloRunner.converge(described_recipe) }
|
|
297
|
+
|
|
298
|
+
before do
|
|
299
|
+
allow_any_instance_of(Chef::Recipe).to receive(:include_recipe).and_call_original
|
|
300
|
+
allow_any_instance_of(Chef::Recipe).to receive(:include_recipe).with('other_cookbook::default')
|
|
301
|
+
end
|
|
302
|
+
|
|
303
|
+
it 'includes the other_cookbook' do
|
|
304
|
+
expect_any_instance_of(Chef::Recipe).to receive(:include_recipe).with('other_cookbook::default')
|
|
305
|
+
chef_run
|
|
306
|
+
end
|
|
307
|
+
end
|
|
308
|
+
```
|
|
309
|
+
|
|
251
310
|
##### notify
|
|
252
311
|
Assert that a resource notifies another in the Chef run
|
|
253
312
|
|
|
@@ -271,6 +330,10 @@ Assert that the Chef run renders a file (with optional content); this will match
|
|
|
271
330
|
expect(chef_run).to render_file('/etc/foo')
|
|
272
331
|
expect(chef_run).to render_file('/etc/foo').with_content('This is content')
|
|
273
332
|
expect(chef_run).to render_file('/etc/foo').with_content(/regex works too.+/)
|
|
333
|
+
expect(chef_run).to render_file('/etc/foo').with_content { |content|
|
|
334
|
+
# Regular RSpec matches work in here
|
|
335
|
+
expect(content).to include('any RSpec matcher')
|
|
336
|
+
}
|
|
274
337
|
```
|
|
275
338
|
|
|
276
339
|
You can use any RSpec content matcher inside of the `with_content` predicate:
|
|
@@ -399,7 +462,7 @@ Create an environment:
|
|
|
399
462
|
|
|
400
463
|
```ruby
|
|
401
464
|
ChefSpec::ServerRunner.new do |node, server|
|
|
402
|
-
server.create_environment('my_environment', { description: '...' })
|
|
465
|
+
server.create_environment('my_environment', { default_attributes: { description: '...' } })
|
|
403
466
|
end
|
|
404
467
|
```
|
|
405
468
|
|
|
@@ -411,7 +474,19 @@ ChefSpec::ServerRunner.new do |node, server|
|
|
|
411
474
|
end
|
|
412
475
|
```
|
|
413
476
|
|
|
414
|
-
Note: the current "node" is always uploaded to the server.
|
|
477
|
+
Note: the current "node" is always uploaded to the server. However, due to the way the Chef Client compiles cookbooks, you must update the current node on the server if any attributes are changed:
|
|
478
|
+
|
|
479
|
+
```ruby
|
|
480
|
+
ChefSpec::ServerRunner.new do |node, server|
|
|
481
|
+
node.set['attribute'] = 'value'
|
|
482
|
+
|
|
483
|
+
# At this point, the server already has a copy of the current node object due
|
|
484
|
+
# to the way Chef compiled the resources. However, that node does not have
|
|
485
|
+
# this new value. As such, you must "save" the node back to the server to
|
|
486
|
+
# persist this attribute update.
|
|
487
|
+
server.update_node(node)
|
|
488
|
+
end
|
|
489
|
+
```
|
|
415
490
|
|
|
416
491
|
You may also use the `stub_node` macro, which will create a new `Chef::Node` object and accepts the same parameters as the Chef Runner and a Fauxhai object:
|
|
417
492
|
|
|
@@ -484,6 +559,43 @@ describe 'example::default' do
|
|
|
484
559
|
end
|
|
485
560
|
```
|
|
486
561
|
|
|
562
|
+
The stubbed command can also be passed as a regular expression, allowing multiple commands to be stubbed with one line.
|
|
563
|
+
|
|
564
|
+
```ruby
|
|
565
|
+
describe 'example::default' do
|
|
566
|
+
let(:chef_run) { ChefSpec::SoloRunner.new }
|
|
567
|
+
|
|
568
|
+
before do
|
|
569
|
+
stub_command(/(foo)|(bar)/).and_return(true)
|
|
570
|
+
end
|
|
571
|
+
end
|
|
572
|
+
```
|
|
573
|
+
|
|
574
|
+
### Library Helpers
|
|
575
|
+
|
|
576
|
+
Given a library helper with a `has_bacon?` method:
|
|
577
|
+
|
|
578
|
+
```ruby
|
|
579
|
+
module Demo
|
|
580
|
+
module Helper
|
|
581
|
+
|
|
582
|
+
include Chef::Mixin::ShellOut
|
|
583
|
+
|
|
584
|
+
def has_bacon?
|
|
585
|
+
cmd = shell_out!('getent passwd bacon', {:returns => [0,2]})
|
|
586
|
+
cmd.stderr.empty? && (cmd.stdout =~ /^bacon/)
|
|
587
|
+
end
|
|
588
|
+
end
|
|
589
|
+
end
|
|
590
|
+
```
|
|
591
|
+
|
|
592
|
+
Stub the output of the library helper. [Additional information](http://jtimberman.housepub.org/blog/2015/05/30/quick-tip-stubbing-library-helpers-in-chefspec/)
|
|
593
|
+
```ruby
|
|
594
|
+
before do
|
|
595
|
+
allow_any_instance_of(Chef::Node).to receive(:has_bacon?).and_return(true)
|
|
596
|
+
end
|
|
597
|
+
```
|
|
598
|
+
|
|
487
599
|
### Data Bag & Data Bag Item
|
|
488
600
|
**NOTE** This is not required if you are using a ChefSpec server.
|
|
489
601
|
|
|
@@ -672,6 +784,18 @@ end
|
|
|
672
784
|
|
|
673
785
|
Mocking Out Environments
|
|
674
786
|
------------------------
|
|
787
|
+
### ServerRunner
|
|
788
|
+
```ruby
|
|
789
|
+
ChefSpec::ServerRunner.new do |node, server|
|
|
790
|
+
# Create the environment
|
|
791
|
+
server.create_environment('staging', { default_attributes: { cookbook_attr: 'value' } })
|
|
792
|
+
|
|
793
|
+
# Assign the environment to the node
|
|
794
|
+
node.chef_environment = 'staging'
|
|
795
|
+
end
|
|
796
|
+
```
|
|
797
|
+
|
|
798
|
+
### SoloRunner
|
|
675
799
|
If you want to mock out `node.chef_environment`, you'll need to use RSpec mocks/stubs twice:
|
|
676
800
|
|
|
677
801
|
```ruby
|
|
@@ -786,12 +910,37 @@ Other developers can write RSpec tests against your LWRP in their cookbooks:
|
|
|
786
910
|
expect(chef_run).to write_motd_message('my message')
|
|
787
911
|
```
|
|
788
912
|
|
|
789
|
-
**Don't forget to include documentation in your cookbook's README noting the custom matcher and
|
|
913
|
+
**Don't forget to include documentation in your cookbook's README noting the custom matcher and its API!**
|
|
914
|
+
|
|
915
|
+
As a caveat, if your custom LWRP uses a custom `provides` value as shown below (Chef 12+), you will need to package slightly different custom matchers:
|
|
916
|
+
|
|
917
|
+
```ruby
|
|
918
|
+
# motd/resources/message.rb
|
|
919
|
+
actions :write
|
|
920
|
+
default_action :write
|
|
921
|
+
|
|
922
|
+
provides :foobar
|
|
923
|
+
|
|
924
|
+
attribute :message, name_attribute: true
|
|
925
|
+
```
|
|
926
|
+
|
|
927
|
+
With a custom `provides` declaration, the resource is still inserted into the resource collection with its generic name; `provides` is just sugar for use in the recipe. As such, you will also need to introduce sugar into your custom matchers:
|
|
928
|
+
|
|
929
|
+
```ruby
|
|
930
|
+
# motd/libraries/matchers.rb
|
|
931
|
+
if defined?(ChefSpec)
|
|
932
|
+
def write_foobar(message)
|
|
933
|
+
ChefSpec::Matchers::ResourceMatcher.new(:motd_message, :write, message)
|
|
934
|
+
end
|
|
935
|
+
end
|
|
936
|
+
```
|
|
937
|
+
|
|
938
|
+
Notice that we have changed the name of the method to match the "foobar" action, but the resource matcher definition remains unchanged. When the Chef run executes, the resource will be inserted into the collection as `motd_message`, even though it was given a custom provides.
|
|
790
939
|
|
|
791
940
|
|
|
792
941
|
Writing Custom Matchers
|
|
793
942
|
-----------------------
|
|
794
|
-
If you are testing a cookbook that does not package
|
|
943
|
+
If you are testing a cookbook that does not package its LWRP matchers, you can create your own following the same pattern as the "Packaging Custom Matchers" section. Simply, create a file at `spec/support/matchers.rb` and add your resource matchers:
|
|
795
944
|
|
|
796
945
|
```ruby
|
|
797
946
|
# spec/support/matchers.rb
|
|
@@ -879,9 +1028,34 @@ it 'raises an error' do
|
|
|
879
1028
|
end
|
|
880
1029
|
```
|
|
881
1030
|
|
|
1031
|
+
Testing Multiple Recipes
|
|
1032
|
+
-------------
|
|
1033
|
+
Even though ChefSpec is cookbook-centric, you can still converge multiple recipes in a single `ChefSpec::SoloRunner` instance. Given a cookbook "sandwich" with recipes "bacon", "lettuce" and "tomato":
|
|
1034
|
+
|
|
1035
|
+
```ruby
|
|
1036
|
+
# cookbooks/sandwich/recipes/bacon.rb
|
|
1037
|
+
package 'bacon'
|
|
1038
|
+
|
|
1039
|
+
# cookbooks/sandwich/recipes/lettuce.rb
|
|
1040
|
+
package 'lettuce'
|
|
1041
|
+
|
|
1042
|
+
# cookbooks/sandwich/recipes/tomato.rb
|
|
1043
|
+
package 'tomato'
|
|
1044
|
+
```
|
|
1045
|
+
|
|
1046
|
+
```ruby
|
|
1047
|
+
let(:chef_run) { ChefSpec::SoloRunner.converge('sandwich::bacon', 'sandwich::lettuce', 'sandwich::tomato') }
|
|
1048
|
+
```
|
|
1049
|
+
|
|
1050
|
+
```ruby
|
|
1051
|
+
expect(chef_run).to install_package('bacon')
|
|
1052
|
+
expect(chef_run).to install_package('lettuce')
|
|
1053
|
+
expect(chef_run).to install_package('tomato')
|
|
1054
|
+
```
|
|
1055
|
+
|
|
882
1056
|
Testing Roles
|
|
883
1057
|
-------------
|
|
884
|
-
|
|
1058
|
+
Roles can also be used in a single `ChefSpec::SoloRunner` instance. Given a cookbook "bacon" with a default recipe:
|
|
885
1059
|
|
|
886
1060
|
```ruby
|
|
887
1061
|
# cookbooks/bacon/recipes/default.rb
|
data/chefspec.gemspec
CHANGED
|
@@ -14,7 +14,7 @@ Gem::Specification.new do |s|
|
|
|
14
14
|
'ChefSpec makes it easy to write examples and get fast ' \
|
|
15
15
|
'feedback on cookbook changes without the need for ' \
|
|
16
16
|
'virtual machines or cloud servers.'
|
|
17
|
-
s.homepage = '
|
|
17
|
+
s.homepage = 'https://sethvargo.github.io/chefspec/'
|
|
18
18
|
s.license = 'MIT'
|
|
19
19
|
|
|
20
20
|
# Packaging
|
|
@@ -27,14 +27,14 @@ Gem::Specification.new do |s|
|
|
|
27
27
|
s.required_ruby_version = '>= 1.9'
|
|
28
28
|
|
|
29
29
|
s.add_dependency 'chef', '>= 11.14'
|
|
30
|
-
s.add_dependency 'fauxhai', '~> 2
|
|
30
|
+
s.add_dependency 'fauxhai', '~> 3.2'
|
|
31
31
|
s.add_dependency 'rspec', '~> 3.0'
|
|
32
32
|
|
|
33
33
|
# Development Dependencies
|
|
34
|
-
s.add_development_dependency 'rake'
|
|
35
|
-
s.add_development_dependency 'redcarpet', '~> 3.
|
|
34
|
+
s.add_development_dependency 'rake', '< 12.0'
|
|
35
|
+
s.add_development_dependency 'redcarpet', '~> 3.3'
|
|
36
36
|
s.add_development_dependency 'yard', '~> 0.8'
|
|
37
37
|
|
|
38
38
|
# Testing Dependencies
|
|
39
|
-
s.add_development_dependency 'aruba', '~> 0.
|
|
39
|
+
s.add_development_dependency 'aruba', '~> 0.8'
|
|
40
40
|
end
|
|
@@ -1,13 +1,18 @@
|
|
|
1
|
-
chef_gem 'default_action'
|
|
1
|
+
chef_gem 'default_action' do
|
|
2
|
+
compile_time false if respond_to?(:compile_time)
|
|
3
|
+
end
|
|
2
4
|
|
|
3
5
|
chef_gem 'explicit_action' do
|
|
4
|
-
action
|
|
6
|
+
action :install
|
|
7
|
+
compile_time false if respond_to?(:compile_time)
|
|
5
8
|
end
|
|
6
9
|
|
|
7
10
|
chef_gem 'with_attributes' do
|
|
8
|
-
version
|
|
11
|
+
version '1.0.0'
|
|
12
|
+
compile_time false if respond_to?(:compile_time)
|
|
9
13
|
end
|
|
10
14
|
|
|
11
15
|
chef_gem 'specifying the identity attribute' do
|
|
12
16
|
package_name 'identity_attribute'
|
|
17
|
+
compile_time false if respond_to?(:compile_time)
|
|
13
18
|
end
|
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
chef_gem 'explicit_action' do
|
|
2
|
-
action
|
|
2
|
+
action :purge
|
|
3
|
+
compile_time false if respond_to?(:compile_time)
|
|
3
4
|
end
|
|
4
5
|
|
|
5
6
|
chef_gem 'with_attributes' do
|
|
6
|
-
version
|
|
7
|
-
action
|
|
7
|
+
version '1.0.0'
|
|
8
|
+
action :purge
|
|
9
|
+
compile_time false if respond_to?(:compile_time)
|
|
8
10
|
end
|
|
9
11
|
|
|
10
12
|
chef_gem 'specifying the identity attribute' do
|
|
11
13
|
package_name 'identity_attribute'
|
|
12
14
|
action :purge
|
|
15
|
+
compile_time false if respond_to?(:compile_time)
|
|
13
16
|
end
|
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
chef_gem 'explicit_action' do
|
|
2
|
-
action
|
|
2
|
+
action :reconfig
|
|
3
|
+
compile_time false if respond_to?(:compile_time)
|
|
3
4
|
end
|
|
4
5
|
|
|
5
6
|
chef_gem 'with_attributes' do
|
|
6
|
-
version
|
|
7
|
-
action
|
|
7
|
+
version '1.0.0'
|
|
8
|
+
action :reconfig
|
|
9
|
+
compile_time false if respond_to?(:compile_time)
|
|
8
10
|
end
|
|
9
11
|
|
|
10
12
|
chef_gem 'specifying the identity attribute' do
|
|
11
13
|
package_name 'identity_attribute'
|
|
12
14
|
action :reconfig
|
|
15
|
+
compile_time false if respond_to?(:compile_time)
|
|
13
16
|
end
|
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
chef_gem 'explicit_action' do
|
|
2
|
-
action
|
|
2
|
+
action :remove
|
|
3
|
+
compile_time false if respond_to?(:compile_time)
|
|
3
4
|
end
|
|
4
5
|
|
|
5
6
|
chef_gem 'with_attributes' do
|
|
6
|
-
version
|
|
7
|
-
action
|
|
7
|
+
version '1.0.0'
|
|
8
|
+
action :remove
|
|
9
|
+
compile_time false if respond_to?(:compile_time)
|
|
8
10
|
end
|
|
9
11
|
|
|
10
12
|
chef_gem 'specifying the identity attribute' do
|
|
11
13
|
package_name 'identity_attribute'
|
|
12
14
|
action :remove
|
|
15
|
+
compile_time false if respond_to?(:compile_time)
|
|
13
16
|
end
|
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
chef_gem 'explicit_action' do
|
|
2
|
-
action
|
|
2
|
+
action :upgrade
|
|
3
|
+
compile_time false if respond_to?(:compile_time)
|
|
3
4
|
end
|
|
4
5
|
|
|
5
6
|
chef_gem 'with_attributes' do
|
|
6
|
-
version
|
|
7
|
-
action
|
|
7
|
+
version '1.0.0'
|
|
8
|
+
action :upgrade
|
|
9
|
+
compile_time false if respond_to?(:compile_time)
|
|
8
10
|
end
|
|
9
11
|
|
|
10
12
|
chef_gem 'specifying the identity attribute' do
|
|
11
13
|
package_name 'identity_attribute'
|
|
12
14
|
action :upgrade
|
|
15
|
+
compile_time false if respond_to?(:compile_time)
|
|
13
16
|
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
require 'chefspec'
|
|
2
|
+
|
|
3
|
+
RSpec.configure do |config|
|
|
4
|
+
config.platform = 'windows'
|
|
5
|
+
config.version = '2012R2'
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
describe 'chocolatey_package::install' do
|
|
9
|
+
let(:chef_run) { ChefSpec::SoloRunner.converge(described_recipe) }
|
|
10
|
+
|
|
11
|
+
it 'installs a package' do
|
|
12
|
+
expect(chef_run).to install_chocolatey_package('7zip')
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
it 'installs a specific version of a package with options' do
|
|
16
|
+
expect(chef_run).to install_chocolatey_package('git').with(
|
|
17
|
+
version: %w(2.7.1),
|
|
18
|
+
options: '--params /GitAndUnixToolsOnPath'
|
|
19
|
+
)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
require 'chefspec'
|
|
2
|
+
|
|
3
|
+
RSpec.configure do |config|
|
|
4
|
+
config.platform = 'windows'
|
|
5
|
+
config.version = '2012R2'
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
describe 'chocolatey_package::remove' do
|
|
9
|
+
let(:chef_run) { ChefSpec::SoloRunner.converge(described_recipe) }
|
|
10
|
+
|
|
11
|
+
it 'removes a package' do
|
|
12
|
+
expect(chef_run).to remove_chocolatey_package('7zip')
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
it 'removes a specific version of a package with options' do
|
|
16
|
+
expect(chef_run).to remove_chocolatey_package('git').with(
|
|
17
|
+
version: %w(2.7.1)
|
|
18
|
+
)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
require 'chefspec'
|
|
2
|
+
|
|
3
|
+
RSpec.configure do |config|
|
|
4
|
+
config.platform = 'windows'
|
|
5
|
+
config.version = '2012R2'
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
describe 'chocolatey_package::upgrade' do
|
|
9
|
+
let(:chef_run) { ChefSpec::SoloRunner.converge(described_recipe) }
|
|
10
|
+
|
|
11
|
+
it 'upgrades a package' do
|
|
12
|
+
expect(chef_run).to upgrade_chocolatey_package('7zip')
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
it 'upgrades a specific version of a package with options' do
|
|
16
|
+
expect(chef_run).to upgrade_chocolatey_package('git').with(
|
|
17
|
+
version: %w(2.7.1)
|
|
18
|
+
)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -24,6 +24,10 @@ describe 'directory::create' do
|
|
|
24
24
|
)
|
|
25
25
|
end
|
|
26
26
|
|
|
27
|
+
it 'creates a directory with windows rights' do
|
|
28
|
+
expect(chef_run).to create_directory('c:\temp\with_windows_rights').with(rights: [{:permissions=>:read_execute, :principals=>"Users", :applies_to_children=>true}])
|
|
29
|
+
end
|
|
30
|
+
|
|
27
31
|
it 'creates a directory when specifying the identity attribute' do
|
|
28
32
|
expect(chef_run).to create_directory('/tmp/identity_attribute')
|
|
29
33
|
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
dsc_resource 'archive resource' do
|
|
2
|
+
resource :archive
|
|
3
|
+
property :ensure, 'present'
|
|
4
|
+
property :path, 'C:\Users\Public\Documents\example.zip'
|
|
5
|
+
property :destination, 'C:\Users\Public\Documents\ExtractionPath'
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
dsc_resource 'group resource' do
|
|
9
|
+
resource :group
|
|
10
|
+
property :groupname, 'demo1'
|
|
11
|
+
property :ensure, 'present'
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
dsc_resource 'user resource' do
|
|
15
|
+
resource :user
|
|
16
|
+
property :username, 'Foobar1'
|
|
17
|
+
property :fullname, 'Foobar1'
|
|
18
|
+
property :password, 'P@assword!'
|
|
19
|
+
property :ensure, 'present'
|
|
20
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
require 'chefspec'
|
|
2
|
+
|
|
3
|
+
RSpec.configure do |config|
|
|
4
|
+
config.platform = 'windows'
|
|
5
|
+
config.version = '2012R2'
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
describe 'dsc_resource::default' do
|
|
9
|
+
let(:chef_run) { ChefSpec::SoloRunner.converge(described_recipe) }
|
|
10
|
+
|
|
11
|
+
it 'runs dsc_resource with the archive resource' do
|
|
12
|
+
expect(chef_run).to run_dsc_resource('archive resource').with(
|
|
13
|
+
resource: :archive,
|
|
14
|
+
properties: {
|
|
15
|
+
ensure: 'present',
|
|
16
|
+
path: 'C:\Users\Public\Documents\example.zip',
|
|
17
|
+
destination: 'C:\Users\Public\Documents\ExtractionPath'
|
|
18
|
+
})
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it 'runs dsc_resource with the group resource' do
|
|
22
|
+
expect(chef_run).to run_dsc_resource('group resource').with(
|
|
23
|
+
resource: :group,
|
|
24
|
+
properties: {
|
|
25
|
+
groupname: 'demo1',
|
|
26
|
+
ensure: 'present'
|
|
27
|
+
})
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it 'runs dsc_resource with the user resource' do
|
|
31
|
+
expect(chef_run).to run_dsc_resource('user resource').with(
|
|
32
|
+
resource: :user,
|
|
33
|
+
properties: {
|
|
34
|
+
username: 'Foobar1',
|
|
35
|
+
fullname: 'Foobar1',
|
|
36
|
+
password: 'P@assword!',
|
|
37
|
+
ensure: 'present'
|
|
38
|
+
})
|
|
39
|
+
end
|
|
40
|
+
end
|