chefspec 7.3.2 → 7.3.3
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/lib/chefspec/extensions/.DS_Store +0 -0
- data/lib/chefspec/extensions/chef/.DS_Store +0 -0
- data/lib/chefspec/version.rb +1 -1
- metadata +7 -666
- data/.gitignore +0 -16
- data/.travis.yml +0 -48
- data/CHANGELOG.md +0 -819
- data/CONTRIBUTING.md +0 -52
- data/Gemfile +0 -23
- data/ISSUES.md +0 -41
- data/README.md +0 -571
- data/README_old.md +0 -1291
- data/Rakefile +0 -74
- data/chefspec.gemspec +0 -31
- data/examples/apt_package/recipes/install.rb +0 -13
- data/examples/apt_package/recipes/lock.rb +0 -13
- data/examples/apt_package/recipes/purge.rb +0 -13
- data/examples/apt_package/recipes/reconfig.rb +0 -13
- data/examples/apt_package/recipes/remove.rb +0 -13
- data/examples/apt_package/recipes/unlock.rb +0 -13
- data/examples/apt_package/recipes/upgrade.rb +0 -13
- data/examples/apt_package/spec/install_spec.rb +0 -26
- data/examples/apt_package/spec/lock_spec.rb +0 -22
- data/examples/apt_package/spec/purge_spec.rb +0 -22
- data/examples/apt_package/spec/reconfig_spec.rb +0 -22
- data/examples/apt_package/spec/remove_spec.rb +0 -22
- data/examples/apt_package/spec/unlock_spec.rb +0 -22
- data/examples/apt_package/spec/upgrade_spec.rb +0 -22
- data/examples/apt_repository/recipes/add.rb +0 -8
- data/examples/apt_repository/recipes/remove.rb +0 -4
- data/examples/apt_repository/spec/add_spec.rb +0 -17
- data/examples/apt_repository/spec/remove_spec.rb +0 -12
- data/examples/apt_update/recipes/periodic.rb +0 -5
- data/examples/apt_update/recipes/update.rb +0 -3
- data/examples/apt_update/spec/periodic_spec.rb +0 -17
- data/examples/apt_update/spec/update_spec.rb +0 -12
- data/examples/attributes/attributes/default.rb +0 -1
- data/examples/attributes/recipes/default.rb +0 -4
- data/examples/attributes/spec/default_spec.rb +0 -20
- data/examples/batch/recipes/run.rb +0 -9
- data/examples/batch/spec/run_spec.rb +0 -22
- data/examples/bff_package/recipes/install.rb +0 -13
- data/examples/bff_package/recipes/purge.rb +0 -13
- data/examples/bff_package/recipes/remove.rb +0 -13
- data/examples/bff_package/recipes/upgrade.rb +0 -13
- data/examples/bff_package/spec/install_spec.rb +0 -23
- data/examples/bff_package/spec/purge_spec.rb +0 -19
- data/examples/bff_package/spec/remove_spec.rb +0 -19
- data/examples/bff_package/spec/upgrade_spec.rb +0 -19
- data/examples/cab_package/recipes/install.rb +0 -13
- data/examples/cab_package/recipes/remove.rb +0 -13
- data/examples/cab_package/spec/install_spec.rb +0 -28
- data/examples/cab_package/spec/remove_spec.rb +0 -24
- data/examples/cached/recipes/default.rb +0 -1
- data/examples/cached/spec/default_spec.rb +0 -14
- data/examples/chef_gem/recipes/install.rb +0 -18
- data/examples/chef_gem/recipes/purge.rb +0 -16
- data/examples/chef_gem/recipes/reconfig.rb +0 -16
- data/examples/chef_gem/recipes/remove.rb +0 -16
- data/examples/chef_gem/recipes/upgrade.rb +0 -16
- data/examples/chef_gem/spec/install_spec.rb +0 -23
- data/examples/chef_gem/spec/purge_spec.rb +0 -19
- data/examples/chef_gem/spec/reconfig_spec.rb +0 -19
- data/examples/chef_gem/spec/remove_spec.rb +0 -19
- data/examples/chef_gem/spec/upgrade_spec.rb +0 -19
- data/examples/chocolatey_package/recipes/install.rb +0 -6
- data/examples/chocolatey_package/recipes/remove.rb +0 -8
- data/examples/chocolatey_package/recipes/upgrade.rb +0 -8
- data/examples/chocolatey_package/spec/install_spec.rb +0 -19
- data/examples/chocolatey_package/spec/remove_spec.rb +0 -18
- data/examples/chocolatey_package/spec/upgrade_spec.rb +0 -18
- data/examples/compile_time/recipes/default.rb +0 -3
- data/examples/compile_time/spec/default_spec.rb +0 -29
- data/examples/cookbook_file/recipes/create.rb +0 -15
- data/examples/cookbook_file/recipes/create_if_missing.rb +0 -15
- data/examples/cookbook_file/recipes/delete.rb +0 -15
- data/examples/cookbook_file/recipes/touch.rb +0 -15
- data/examples/cookbook_file/spec/create_if_missing_spec.rb +0 -28
- data/examples/cookbook_file/spec/create_spec.rb +0 -32
- data/examples/cookbook_file/spec/delete_spec.rb +0 -19
- data/examples/cookbook_file/spec/touch_spec.rb +0 -19
- data/examples/core/recipes/default.rb +0 -1
- data/examples/core/spec/default_spec.rb +0 -7
- data/examples/core/spec/non_recipe_spec.rb +0 -5
- data/examples/cron/recipes/create.rb +0 -10
- data/examples/cron/recipes/delete.rb +0 -9
- data/examples/cron/spec/create_spec.rb +0 -19
- data/examples/cron/spec/delete_spec.rb +0 -15
- data/examples/custom_matcher/libraries/matcher.rb +0 -1
- data/examples/custom_matcher/providers/thing.rb +0 -2
- data/examples/custom_matcher/recipes/install.rb +0 -13
- data/examples/custom_matcher/recipes/remove.rb +0 -13
- data/examples/custom_matcher/resources/thing.rb +0 -5
- data/examples/custom_matcher/spec/install_spec.rb +0 -27
- data/examples/custom_matcher/spec/remove_spec.rb +0 -23
- data/examples/custom_resource/recipes/default.rb +0 -1
- data/examples/custom_resource/resources/custom_resource.rb +0 -9
- data/examples/custom_resource/spec/default_spec.rb +0 -18
- data/examples/custom_resource_block/resources/default.rb +0 -3
- data/examples/custom_resource_block/spec/default_spec.rb +0 -10
- data/examples/directory/recipes/create.rb +0 -18
- data/examples/directory/recipes/delete.rb +0 -14
- data/examples/directory/spec/create_spec.rb +0 -34
- data/examples/directory/spec/delete_spec.rb +0 -26
- data/examples/dnf_package/recipes/install.rb +0 -13
- data/examples/dnf_package/recipes/purge.rb +0 -13
- data/examples/dnf_package/recipes/remove.rb +0 -13
- data/examples/dnf_package/recipes/upgrade.rb +0 -13
- data/examples/dnf_package/spec/install_spec.rb +0 -28
- data/examples/dnf_package/spec/purge_spec.rb +0 -24
- data/examples/dnf_package/spec/remove_spec.rb +0 -24
- data/examples/dnf_package/spec/upgrade_spec.rb +0 -24
- data/examples/do_nothing/recipes/default.rb +0 -3
- data/examples/do_nothing/spec/default_spec.rb +0 -15
- data/examples/dpkg_package/recipes/install.rb +0 -13
- data/examples/dpkg_package/recipes/purge.rb +0 -13
- data/examples/dpkg_package/recipes/remove.rb +0 -13
- data/examples/dpkg_package/spec/install_spec.rb +0 -26
- data/examples/dpkg_package/spec/purge_spec.rb +0 -22
- data/examples/dpkg_package/spec/remove_spec.rb +0 -22
- data/examples/dsc_resource/recipes/run.rb +0 -20
- data/examples/dsc_resource/spec/run_spec.rb +0 -38
- data/examples/dsc_script/recipes/run.rb +0 -12
- data/examples/dsc_script/spec/run_spec.rb +0 -16
- data/examples/env/recipes/create.rb +0 -13
- data/examples/env/recipes/delete.rb +0 -13
- data/examples/env/recipes/modify.rb +0 -13
- data/examples/env/spec/create_spec.rb +0 -26
- data/examples/env/spec/delete_spec.rb +0 -22
- data/examples/env/spec/modify_spec.rb +0 -22
- data/examples/execute/recipes/run.rb +0 -13
- data/examples/execute/spec/run_spec.rb +0 -23
- data/examples/expect_exception/recipes/compile_error.rb +0 -1
- data/examples/expect_exception/recipes/converge_error.rb +0 -5
- data/examples/expect_exception/recipes/no_error.rb +0 -1
- data/examples/expect_exception/spec/compile_error_spec.rb +0 -10
- data/examples/expect_exception/spec/converge_error_spec.rb +0 -10
- data/examples/expect_exception/spec/no_error_spec.rb +0 -10
- data/examples/file/recipes/create.rb +0 -15
- data/examples/file/recipes/create_if_missing.rb +0 -15
- data/examples/file/recipes/delete.rb +0 -15
- data/examples/file/recipes/touch.rb +0 -15
- data/examples/file/spec/create_if_missing_spec.rb +0 -28
- data/examples/file/spec/create_spec.rb +0 -32
- data/examples/file/spec/delete_spec.rb +0 -19
- data/examples/file/spec/touch_spec.rb +0 -19
- data/examples/freebsd_package/recipes/install.rb +0 -13
- data/examples/freebsd_package/recipes/remove.rb +0 -13
- data/examples/freebsd_package/spec/install_spec.rb +0 -26
- data/examples/freebsd_package/spec/remove_spec.rb +0 -22
- data/examples/gem_package/recipes/install.rb +0 -13
- data/examples/gem_package/recipes/purge.rb +0 -13
- data/examples/gem_package/recipes/reconfig.rb +0 -13
- data/examples/gem_package/recipes/remove.rb +0 -13
- data/examples/gem_package/recipes/upgrade.rb +0 -13
- data/examples/gem_package/spec/install_spec.rb +0 -23
- data/examples/gem_package/spec/purge_spec.rb +0 -19
- data/examples/gem_package/spec/reconfig_spec.rb +0 -19
- data/examples/gem_package/spec/remove_spec.rb +0 -19
- data/examples/gem_package/spec/upgrade_spec.rb +0 -19
- data/examples/git/recipes/checkout.rb +0 -13
- data/examples/git/recipes/export.rb +0 -13
- data/examples/git/recipes/sync.rb +0 -13
- data/examples/git/spec/checkout_spec.rb +0 -19
- data/examples/git/spec/export_spec.rb +0 -19
- data/examples/git/spec/sync_spec.rb +0 -23
- data/examples/group/recipes/create.rb +0 -13
- data/examples/group/recipes/manage.rb +0 -13
- data/examples/group/recipes/modify.rb +0 -13
- data/examples/group/recipes/remove.rb +0 -13
- data/examples/group/spec/create_spec.rb +0 -23
- data/examples/group/spec/manage_spec.rb +0 -19
- data/examples/group/spec/modify_spec.rb +0 -19
- data/examples/group/spec/remove_spec.rb +0 -18
- data/examples/guards/recipes/default.rb +0 -13
- data/examples/guards/spec/default_spec.rb +0 -17
- data/examples/heavy_provider_light_resource/libraries/resource_service.rb +0 -15
- data/examples/heavy_provider_light_resource/providers/service.rb +0 -9
- data/examples/heavy_provider_light_resource/recipes/default.rb +0 -3
- data/examples/heavy_provider_light_resource/spec/provider_service_spec.rb +0 -8
- data/examples/homebrew_package/recipes/install.rb +0 -13
- data/examples/homebrew_package/recipes/purge.rb +0 -13
- data/examples/homebrew_package/recipes/remove.rb +0 -13
- data/examples/homebrew_package/recipes/upgrade.rb +0 -13
- data/examples/homebrew_package/spec/install_spec.rb +0 -23
- data/examples/homebrew_package/spec/purge_spec.rb +0 -19
- data/examples/homebrew_package/spec/remove_spec.rb +0 -19
- data/examples/homebrew_package/spec/upgrade_spec.rb +0 -19
- data/examples/http_request/recipes/delete.rb +0 -13
- data/examples/http_request/recipes/get.rb +0 -13
- data/examples/http_request/recipes/head.rb +0 -13
- data/examples/http_request/recipes/options.rb +0 -13
- data/examples/http_request/recipes/post.rb +0 -13
- data/examples/http_request/recipes/put.rb +0 -13
- data/examples/http_request/spec/delete_spec.rb +0 -19
- data/examples/http_request/spec/get_spec.rb +0 -23
- data/examples/http_request/spec/head_spec.rb +0 -19
- data/examples/http_request/spec/options_spec.rb +0 -19
- data/examples/http_request/spec/post_spec.rb +0 -19
- data/examples/http_request/spec/put_spec.rb +0 -19
- data/examples/ifconfig/recipes/add.rb +0 -9
- data/examples/ifconfig/recipes/delete.rb +0 -8
- data/examples/ifconfig/recipes/disable.rb +0 -8
- data/examples/ifconfig/recipes/enable.rb +0 -8
- data/examples/ifconfig/spec/add_spec.rb +0 -19
- data/examples/ifconfig/spec/delete_spec.rb +0 -15
- data/examples/ifconfig/spec/disable_spec.rb +0 -15
- data/examples/ifconfig/spec/enable_spec.rb +0 -15
- data/examples/include_recipe/recipes/default.rb +0 -1
- data/examples/include_recipe/recipes/not.rb +0 -0
- data/examples/include_recipe/recipes/other.rb +0 -0
- data/examples/include_recipe/spec/default_spec.rb +0 -13
- data/examples/ips_package/recipes/install.rb +0 -13
- data/examples/ips_package/recipes/remove.rb +0 -13
- data/examples/ips_package/recipes/upgrade.rb +0 -13
- data/examples/ips_package/spec/install_spec.rb +0 -26
- data/examples/ips_package/spec/remove_spec.rb +0 -22
- data/examples/ips_package/spec/upgrade_spec.rb +0 -22
- data/examples/launchd/recipes/create.rb +0 -5
- data/examples/launchd/recipes/create_if_missing.rb +0 -3
- data/examples/launchd/recipes/delete.rb +0 -3
- data/examples/launchd/recipes/disable.rb +0 -3
- data/examples/launchd/recipes/enable.rb +0 -3
- data/examples/launchd/spec/create_if_missing_spec.rb +0 -10
- data/examples/launchd/spec/create_spec.rb +0 -14
- data/examples/launchd/spec/delete_spec.rb +0 -10
- data/examples/launchd/spec/disable_spec.rb +0 -10
- data/examples/launchd/spec/enable_spec.rb +0 -10
- data/examples/library_patch/libraries/default.rb +0 -5
- data/examples/library_patch/recipes/default.rb +0 -1
- data/examples/library_patch/spec/default_spec.rb +0 -14
- data/examples/link/recipes/create.rb +0 -13
- data/examples/link/recipes/delete.rb +0 -13
- data/examples/link/recipes/link_to.rb +0 -3
- data/examples/link/spec/create_spec.rb +0 -23
- data/examples/link/spec/delete_spec.rb +0 -19
- data/examples/link/spec/link_to_spec.rb +0 -11
- data/examples/log/recipes/write.rb +0 -13
- data/examples/log/spec/write_spec.rb +0 -24
- data/examples/macports_package/recipes/install.rb +0 -13
- data/examples/macports_package/recipes/purge.rb +0 -13
- data/examples/macports_package/recipes/remove.rb +0 -13
- data/examples/macports_package/recipes/upgrade.rb +0 -13
- data/examples/macports_package/spec/install_spec.rb +0 -23
- data/examples/macports_package/spec/purge_spec.rb +0 -19
- data/examples/macports_package/spec/remove_spec.rb +0 -19
- data/examples/macports_package/spec/upgrade_spec.rb +0 -19
- data/examples/mdadm/recipes/assemble.rb +0 -13
- data/examples/mdadm/recipes/create.rb +0 -13
- data/examples/mdadm/recipes/stop.rb +0 -13
- data/examples/mdadm/spec/assemble_spec.rb +0 -19
- data/examples/mdadm/spec/create_spec.rb +0 -23
- data/examples/mdadm/spec/stop_spec.rb +0 -19
- data/examples/mount/recipes/disable.rb +0 -8
- data/examples/mount/recipes/enable.rb +0 -8
- data/examples/mount/recipes/mount.rb +0 -9
- data/examples/mount/recipes/remount.rb +0 -8
- data/examples/mount/recipes/umount.rb +0 -8
- data/examples/mount/spec/disable_spec.rb +0 -15
- data/examples/mount/spec/enable_spec.rb +0 -15
- data/examples/mount/spec/mount_spec.rb +0 -19
- data/examples/mount/spec/remount_spec.rb +0 -15
- data/examples/mount/spec/umount_spec.rb +0 -15
- data/examples/msu_package/recipes/install.rb +0 -13
- data/examples/msu_package/recipes/remove.rb +0 -13
- data/examples/msu_package/spec/install_spec.rb +0 -28
- data/examples/msu_package/spec/remove_spec.rb +0 -24
- data/examples/multiple_actions/recipes/default.rb +0 -3
- data/examples/multiple_actions/recipes/reversed.rb +0 -9
- data/examples/multiple_actions/recipes/sequential.rb +0 -9
- data/examples/multiple_actions/spec/default_spec.rb +0 -14
- data/examples/multiple_actions/spec/reversed_spec.rb +0 -17
- data/examples/multiple_actions/spec/sequential_spec.rb +0 -17
- data/examples/multiple_run_action/recipes/default.rb +0 -5
- data/examples/multiple_run_action/spec/default_spec.rb +0 -18
- data/examples/nothing_matcher/recipes/default.rb +0 -13
- data/examples/nothing_matcher/spec/default_spec.rb +0 -20
- data/examples/notifications/recipes/before.rb +0 -7
- data/examples/notifications/recipes/chained.rb +0 -12
- data/examples/notifications/recipes/default.rb +0 -7
- data/examples/notifications/recipes/delayed.rb +0 -7
- data/examples/notifications/recipes/immediately.rb +0 -7
- data/examples/notifications/spec/before_spec.rb +0 -16
- data/examples/notifications/spec/chained_spec.rb +0 -21
- data/examples/notifications/spec/default_spec.rb +0 -16
- data/examples/notifications/spec/delayed_spec.rb +0 -16
- data/examples/notifications/spec/immediately_spec.rb +0 -16
- data/examples/ohai/recipes/reload.rb +0 -13
- data/examples/ohai/spec/reload_spec.rb +0 -23
- data/examples/openbsd_package/recipes/install.rb +0 -13
- data/examples/openbsd_package/recipes/purge.rb +0 -13
- data/examples/openbsd_package/recipes/remove.rb +0 -13
- data/examples/openbsd_package/recipes/upgrade.rb +0 -13
- data/examples/openbsd_package/spec/install_spec.rb +0 -23
- data/examples/openbsd_package/spec/purge_spec.rb +0 -19
- data/examples/openbsd_package/spec/remove_spec.rb +0 -19
- data/examples/openbsd_package/spec/upgrade_spec.rb +0 -19
- data/examples/osx_profile/recipes/install.rb +0 -8
- data/examples/osx_profile/recipes/remove.rb +0 -8
- data/examples/osx_profile/spec/install_spec.rb +0 -15
- data/examples/osx_profile/spec/remove_spec.rb +0 -13
- data/examples/package/recipes/install.rb +0 -17
- data/examples/package/recipes/lock.rb +0 -17
- data/examples/package/recipes/purge.rb +0 -17
- data/examples/package/recipes/reconfig.rb +0 -17
- data/examples/package/recipes/remove.rb +0 -17
- data/examples/package/recipes/unlock.rb +0 -17
- data/examples/package/recipes/upgrade.rb +0 -17
- data/examples/package/spec/install_spec.rb +0 -27
- data/examples/package/spec/lock_spec.rb +0 -23
- data/examples/package/spec/purge_spec.rb +0 -23
- data/examples/package/spec/reconfig_spec.rb +0 -23
- data/examples/package/spec/remove_spec.rb +0 -23
- data/examples/package/spec/unlock_spec.rb +0 -23
- data/examples/package/spec/upgrade_spec.rb +0 -23
- data/examples/pacman_package/recipes/install.rb +0 -13
- data/examples/pacman_package/recipes/purge.rb +0 -13
- data/examples/pacman_package/recipes/remove.rb +0 -13
- data/examples/pacman_package/recipes/upgrade.rb +0 -13
- data/examples/pacman_package/spec/install_spec.rb +0 -26
- data/examples/pacman_package/spec/purge_spec.rb +0 -22
- data/examples/pacman_package/spec/remove_spec.rb +0 -22
- data/examples/pacman_package/spec/upgrade_spec.rb +0 -22
- data/examples/paludis_package/recipes/install.rb +0 -13
- data/examples/paludis_package/recipes/purge.rb +0 -13
- data/examples/paludis_package/recipes/remove.rb +0 -13
- data/examples/paludis_package/recipes/upgrade.rb +0 -13
- data/examples/paludis_package/spec/install_spec.rb +0 -23
- data/examples/paludis_package/spec/purge_spec.rb +0 -19
- data/examples/paludis_package/spec/remove_spec.rb +0 -19
- data/examples/paludis_package/spec/upgrade_spec.rb +0 -19
- data/examples/portage_package/recipes/install.rb +0 -13
- data/examples/portage_package/recipes/purge.rb +0 -13
- data/examples/portage_package/recipes/remove.rb +0 -13
- data/examples/portage_package/recipes/upgrade.rb +0 -13
- data/examples/portage_package/spec/install_spec.rb +0 -26
- data/examples/portage_package/spec/purge_spec.rb +0 -22
- data/examples/portage_package/spec/remove_spec.rb +0 -22
- data/examples/portage_package/spec/upgrade_spec.rb +0 -22
- data/examples/powershell_script/recipes/run.rb +0 -9
- data/examples/powershell_script/spec/run_spec.rb +0 -22
- data/examples/reboot/recipes/cancel.rb +0 -3
- data/examples/reboot/recipes/now.rb +0 -3
- data/examples/reboot/recipes/request.rb +0 -3
- data/examples/reboot/spec/cancel_spec.rb +0 -9
- data/examples/reboot/spec/now_spec.rb +0 -9
- data/examples/reboot/spec/request_spec.rb +0 -9
- data/examples/recipe_block/metadata.rb +0 -1
- data/examples/recipe_block/spec/recipe_block_spec.rb +0 -34
- data/examples/recipe_block/templates/apache2.conf.erb +0 -1
- data/examples/registry_key/recipes/create.rb +0 -13
- data/examples/registry_key/recipes/create_if_missing.rb +0 -13
- data/examples/registry_key/recipes/delete.rb +0 -13
- data/examples/registry_key/recipes/delete_key.rb +0 -13
- data/examples/registry_key/spec/create_if_missing_spec.rb +0 -22
- data/examples/registry_key/spec/create_spec.rb +0 -26
- data/examples/registry_key/spec/delete_key_spec.rb +0 -22
- data/examples/registry_key/spec/delete_spec.rb +0 -22
- data/examples/remote_directory/recipes/create.rb +0 -13
- data/examples/remote_directory/recipes/create_if_missing.rb +0 -13
- data/examples/remote_directory/recipes/delete.rb +0 -13
- data/examples/remote_directory/spec/create_if_missing_spec.rb +0 -19
- data/examples/remote_directory/spec/create_spec.rb +0 -23
- data/examples/remote_directory/spec/delete_spec.rb +0 -19
- data/examples/remote_file/recipes/create.rb +0 -18
- data/examples/remote_file/recipes/create_if_missing.rb +0 -16
- data/examples/remote_file/recipes/delete.rb +0 -16
- data/examples/remote_file/recipes/touch.rb +0 -16
- data/examples/remote_file/spec/create_if_missing_spec.rb +0 -19
- data/examples/remote_file/spec/create_spec.rb +0 -23
- data/examples/remote_file/spec/delete_spec.rb +0 -19
- data/examples/remote_file/spec/touch_spec.rb +0 -19
- data/examples/render_file/files/default/cookbook_file +0 -1
- data/examples/render_file/recipes/default.rb +0 -15
- data/examples/render_file/recipes/template_helpers.rb +0 -9
- data/examples/render_file/spec/default_spec.rb +0 -166
- data/examples/render_file/spec/template_helpers_spec.rb +0 -10
- data/examples/render_file/templates/default/_partial.erb +0 -1
- data/examples/render_file/templates/default/partial.erb +0 -1
- data/examples/render_file/templates/default/template.erb +0 -1
- data/examples/render_file/templates/default/template_with_helper.erb +0 -1
- data/examples/roles/recipes/another.rb +0 -3
- data/examples/roles/recipes/default.rb +0 -1
- data/examples/roles/roles/role.rb +0 -9
- data/examples/roles/spec/default_spec.rb +0 -21
- data/examples/route/recipes/add.rb +0 -13
- data/examples/route/recipes/delete.rb +0 -13
- data/examples/route/spec/add_spec.rb +0 -23
- data/examples/route/spec/delete_spec.rb +0 -19
- data/examples/rpm_package/recipes/install.rb +0 -13
- data/examples/rpm_package/recipes/remove.rb +0 -13
- data/examples/rpm_package/recipes/upgrade.rb +0 -13
- data/examples/rpm_package/spec/install_spec.rb +0 -26
- data/examples/rpm_package/spec/remove_spec.rb +0 -22
- data/examples/rpm_package/spec/upgrade_spec.rb +0 -22
- data/examples/ruby_block/recipes/create.rb +0 -8
- data/examples/ruby_block/recipes/run.rb +0 -9
- data/examples/ruby_block/spec/create_spec.rb +0 -13
- data/examples/ruby_block/spec/run_spec.rb +0 -18
- data/examples/runner/recipes/default.rb +0 -1
- data/examples/runner/spec/default_spec.rb +0 -11
- data/examples/script/recipes/run_bash.rb +0 -9
- data/examples/script/recipes/run_csh.rb +0 -9
- data/examples/script/recipes/run_ksh.rb +0 -9
- data/examples/script/recipes/run_perl.rb +0 -9
- data/examples/script/recipes/run_python.rb +0 -9
- data/examples/script/recipes/run_ruby.rb +0 -9
- data/examples/script/recipes/run_script.rb +0 -9
- data/examples/script/spec/run_bash_spec.rb +0 -19
- data/examples/script/spec/run_csh_spec.rb +0 -19
- data/examples/script/spec/run_ksh_spec.rb +0 -19
- data/examples/script/spec/run_perl_spec.rb +0 -19
- data/examples/script/spec/run_python_spec.rb +0 -19
- data/examples/script/spec/run_ruby_spec.rb +0 -19
- data/examples/script/spec/run_script_spec.rb +0 -19
- data/examples/server/recipes/client.rb +0 -6
- data/examples/server/recipes/data_bag.rb +0 -7
- data/examples/server/recipes/environment.rb +0 -6
- data/examples/server/recipes/node.rb +0 -6
- data/examples/server/recipes/render_with_cached.rb +0 -3
- data/examples/server/recipes/role.rb +0 -6
- data/examples/server/recipes/search.rb +0 -5
- data/examples/server/spec/client_spec.rb +0 -18
- data/examples/server/spec/data_bag_spec.rb +0 -25
- data/examples/server/spec/environment_spec.rb +0 -18
- data/examples/server/spec/exotic_port_spec.rb +0 -30
- data/examples/server/spec/node_spec.rb +0 -49
- data/examples/server/spec/render_with_cached_spec.rb +0 -15
- data/examples/server/spec/role_spec.rb +0 -18
- data/examples/server/spec/search_spec.rb +0 -61
- data/examples/service/recipes/disable.rb +0 -13
- data/examples/service/recipes/enable.rb +0 -13
- data/examples/service/recipes/reload.rb +0 -13
- data/examples/service/recipes/restart.rb +0 -13
- data/examples/service/recipes/start.rb +0 -13
- data/examples/service/recipes/stop.rb +0 -13
- data/examples/service/spec/disable_spec.rb +0 -19
- data/examples/service/spec/enable_spec.rb +0 -19
- data/examples/service/spec/reload_spec.rb +0 -19
- data/examples/service/spec/restart_spec.rb +0 -19
- data/examples/service/spec/start_spec.rb +0 -19
- data/examples/service/spec/stop_spec.rb +0 -19
- data/examples/smartos_package/recipes/install.rb +0 -13
- data/examples/smartos_package/recipes/remove.rb +0 -13
- data/examples/smartos_package/recipes/upgrade.rb +0 -13
- data/examples/smartos_package/spec/install_spec.rb +0 -26
- data/examples/smartos_package/spec/remove_spec.rb +0 -22
- data/examples/smartos_package/spec/upgrade_spec.rb +0 -22
- data/examples/solaris_package/recipes/install.rb +0 -13
- data/examples/solaris_package/recipes/remove.rb +0 -13
- data/examples/solaris_package/recipes/upgrade.rb +0 -13
- data/examples/solaris_package/spec/install_spec.rb +0 -26
- data/examples/solaris_package/spec/remove_spec.rb +0 -22
- data/examples/solaris_package/spec/upgrade_spec.rb +0 -22
- data/examples/spec_attributes/attributes/default.rb +0 -1
- data/examples/spec_attributes/recipes/default.rb +0 -5
- data/examples/spec_attributes/spec/default_spec.rb +0 -63
- data/examples/spec_platform/recipes/default.rb +0 -3
- data/examples/spec_platform/spec/default_spec.rb +0 -36
- data/examples/spec_step_into/recipes/default.rb +0 -3
- data/examples/spec_step_into/resources/one.rb +0 -3
- data/examples/spec_step_into/resources/two.rb +0 -3
- data/examples/spec_step_into/spec/default_spec.rb +0 -50
- data/examples/state_attrs/providers/lwrp.rb +0 -2
- data/examples/state_attrs/recipes/default.rb +0 -3
- data/examples/state_attrs/resources/lwrp.rb +0 -6
- data/examples/state_attrs/spec/default_spec.rb +0 -12
- data/examples/step_into/providers/lwrp.rb +0 -3
- data/examples/step_into/providers/provides.rb +0 -3
- data/examples/step_into/recipes/default.rb +0 -3
- data/examples/step_into/resources/lwrp.rb +0 -4
- data/examples/step_into/resources/provides.rb +0 -6
- data/examples/step_into/spec/default_spec.rb +0 -30
- data/examples/stub_command/recipes/default.rb +0 -5
- data/examples/stub_command/recipes/other_recipe.rb +0 -3
- data/examples/stub_command/spec/default_spec.rb +0 -28
- data/examples/stub_data_bag/recipes/default.rb +0 -5
- data/examples/stub_data_bag/spec/default_spec.rb +0 -27
- data/examples/stub_data_bag_item/recipes/default.rb +0 -5
- data/examples/stub_data_bag_item/spec/default_spec.rb +0 -27
- data/examples/stub_node/recipes/default.rb +0 -0
- data/examples/stub_node/spec/default_spec.rb +0 -17
- data/examples/stub_search/recipes/block.rb +0 -3
- data/examples/stub_search/recipes/default.rb +0 -2
- data/examples/stub_search/spec/block_spec.rb +0 -27
- data/examples/stub_search/spec/default_spec.rb +0 -27
- data/examples/stubs_for/resources/default.rb +0 -27
- data/examples/stubs_for/resources/old.rb +0 -26
- data/examples/stubs_for/spec/default_spec.rb +0 -263
- data/examples/subscribes/recipes/before.rb +0 -5
- data/examples/subscribes/recipes/chained.rb +0 -10
- data/examples/subscribes/recipes/default.rb +0 -5
- data/examples/subscribes/recipes/delayed.rb +0 -5
- data/examples/subscribes/recipes/immediately.rb +0 -5
- data/examples/subscribes/spec/before_spec.rb +0 -16
- data/examples/subscribes/spec/chained_spec.rb +0 -21
- data/examples/subscribes/spec/default_spec.rb +0 -16
- data/examples/subscribes/spec/delayed_spec.rb +0 -16
- data/examples/subscribes/spec/immediately_spec.rb +0 -16
- data/examples/subversion/recipes/checkout.rb +0 -13
- data/examples/subversion/recipes/export.rb +0 -13
- data/examples/subversion/recipes/force_export.rb +0 -13
- data/examples/subversion/recipes/sync.rb +0 -13
- data/examples/subversion/spec/checkout_spec.rb +0 -19
- data/examples/subversion/spec/export_spec.rb +0 -19
- data/examples/subversion/spec/force_export_spec.rb +0 -19
- data/examples/subversion/spec/sync_spec.rb +0 -23
- data/examples/systemd_unit/recipes/create.rb +0 -3
- data/examples/systemd_unit/recipes/delete.rb +0 -3
- data/examples/systemd_unit/recipes/disable.rb +0 -3
- data/examples/systemd_unit/recipes/enable.rb +0 -3
- data/examples/systemd_unit/recipes/mask.rb +0 -3
- data/examples/systemd_unit/recipes/reload_or_restart.rb +0 -3
- data/examples/systemd_unit/recipes/reload_or_try_restart.rb +0 -3
- data/examples/systemd_unit/recipes/restart.rb +0 -3
- data/examples/systemd_unit/recipes/start.rb +0 -3
- data/examples/systemd_unit/recipes/stop.rb +0 -3
- data/examples/systemd_unit/recipes/try_restart.rb +0 -3
- data/examples/systemd_unit/recipes/unmask.rb +0 -3
- data/examples/systemd_unit/spec/create_spec.rb +0 -10
- data/examples/systemd_unit/spec/delete_spec.rb +0 -10
- data/examples/systemd_unit/spec/disable_spec.rb +0 -10
- data/examples/systemd_unit/spec/enable_spec.rb +0 -10
- data/examples/systemd_unit/spec/mask_spec.rb +0 -10
- data/examples/systemd_unit/spec/reload_or_restart_spec.rb +0 -10
- data/examples/systemd_unit/spec/reload_or_try_restart_spec.rb +0 -10
- data/examples/systemd_unit/spec/restart_spec.rb +0 -10
- data/examples/systemd_unit/spec/start_spec.rb +0 -10
- data/examples/systemd_unit/spec/stop_spec.rb +0 -10
- data/examples/systemd_unit/spec/try_restart_spec.rb +0 -10
- data/examples/systemd_unit/spec/unmask_spec.rb +0 -10
- data/examples/template/recipes/create.rb +0 -15
- data/examples/template/recipes/create_if_missing.rb +0 -15
- data/examples/template/recipes/delete.rb +0 -15
- data/examples/template/recipes/touch.rb +0 -15
- data/examples/template/spec/create_if_missing_spec.rb +0 -28
- data/examples/template/spec/create_spec.rb +0 -32
- data/examples/template/spec/delete_spec.rb +0 -19
- data/examples/template/spec/touch_spec.rb +0 -19
- data/examples/use_inline_resources/providers/lwrp.rb +0 -7
- data/examples/use_inline_resources/recipes/default.rb +0 -1
- data/examples/use_inline_resources/resources/lwrp.rb +0 -4
- data/examples/use_inline_resources/spec/default_spec.rb +0 -18
- data/examples/user/recipes/create.rb +0 -13
- data/examples/user/recipes/lock.rb +0 -13
- data/examples/user/recipes/manage.rb +0 -13
- data/examples/user/recipes/modify.rb +0 -13
- data/examples/user/recipes/remove.rb +0 -13
- data/examples/user/recipes/unlock.rb +0 -13
- data/examples/user/spec/create_spec.rb +0 -26
- data/examples/user/spec/lock_spec.rb +0 -22
- data/examples/user/spec/manage_spec.rb +0 -22
- data/examples/user/spec/modify_spec.rb +0 -22
- data/examples/user/spec/remove_spec.rb +0 -22
- data/examples/user/spec/unlock_spec.rb +0 -22
- data/examples/windows_package/recipes/install.rb +0 -13
- data/examples/windows_package/recipes/remove.rb +0 -13
- data/examples/windows_package/spec/install_spec.rb +0 -22
- data/examples/windows_package/spec/remove_spec.rb +0 -22
- data/examples/windows_service/recipes/configure_startup.rb +0 -13
- data/examples/windows_service/recipes/disable.rb +0 -13
- data/examples/windows_service/recipes/enable.rb +0 -13
- data/examples/windows_service/recipes/reload.rb +0 -13
- data/examples/windows_service/recipes/restart.rb +0 -13
- data/examples/windows_service/recipes/start.rb +0 -13
- data/examples/windows_service/recipes/stop.rb +0 -13
- data/examples/windows_service/spec/configure_startup_spec.rb +0 -22
- data/examples/windows_service/spec/disable_spec.rb +0 -22
- data/examples/windows_service/spec/enable_spec.rb +0 -22
- data/examples/windows_service/spec/reload_spec.rb +0 -22
- data/examples/windows_service/spec/restart_spec.rb +0 -22
- data/examples/windows_service/spec/start_spec.rb +0 -22
- data/examples/windows_service/spec/stop_spec.rb +0 -22
- data/examples/yum_package/recipes/install.rb +0 -13
- data/examples/yum_package/recipes/lock.rb +0 -13
- data/examples/yum_package/recipes/purge.rb +0 -13
- data/examples/yum_package/recipes/remove.rb +0 -13
- data/examples/yum_package/recipes/unlock.rb +0 -13
- data/examples/yum_package/recipes/upgrade.rb +0 -13
- data/examples/yum_package/spec/install_spec.rb +0 -26
- data/examples/yum_package/spec/lock_spec.rb +0 -22
- data/examples/yum_package/spec/purge_spec.rb +0 -22
- data/examples/yum_package/spec/remove_spec.rb +0 -22
- data/examples/yum_package/spec/unlock_spec.rb +0 -22
- data/examples/yum_package/spec/upgrade_spec.rb +0 -22
- data/examples/yum_repository/recipes/add.rb +0 -8
- data/examples/yum_repository/recipes/create.rb +0 -8
- data/examples/yum_repository/recipes/delete.rb +0 -3
- data/examples/yum_repository/recipes/makecache.rb +0 -3
- data/examples/yum_repository/recipes/remove.rb +0 -7
- data/examples/yum_repository/spec/add_spec.rb +0 -12
- data/examples/yum_repository/spec/create_spec.rb +0 -17
- data/examples/yum_repository/spec/delete_spec.rb +0 -12
- data/examples/yum_repository/spec/makecache_spec.rb +0 -12
- data/examples/yum_repository/spec/remove_spec.rb +0 -12
- data/examples/zypper_package/recipes/install.rb +0 -13
- data/examples/zypper_package/recipes/lock.rb +0 -13
- data/examples/zypper_package/recipes/purge.rb +0 -13
- data/examples/zypper_package/recipes/remove.rb +0 -13
- data/examples/zypper_package/recipes/unlock.rb +0 -13
- data/examples/zypper_package/recipes/upgrade.rb +0 -13
- data/examples/zypper_package/spec/install_spec.rb +0 -23
- data/examples/zypper_package/spec/lock_spec.rb +0 -19
- data/examples/zypper_package/spec/purge_spec.rb +0 -19
- data/examples/zypper_package/spec/remove_spec.rb +0 -19
- data/examples/zypper_package/spec/unlock_spec.rb +0 -19
- data/examples/zypper_package/spec/upgrade_spec.rb +0 -19
- data/spec/spec_helper.rb +0 -13
- data/spec/support/hash.rb +0 -35
- data/spec/unit/cacher_spec.rb +0 -70
- data/spec/unit/coverage/filters_spec.rb +0 -60
- data/spec/unit/deprecations_spec.rb +0 -53
- data/spec/unit/errors_spec.rb +0 -57
- data/spec/unit/expect_exception_spec.rb +0 -32
- data/spec/unit/macros_spec.rb +0 -119
- data/spec/unit/matchers/do_nothing_matcher.rb +0 -5
- data/spec/unit/matchers/include_recipe_matcher_spec.rb +0 -38
- data/spec/unit/matchers/link_to_matcher_spec.rb +0 -55
- data/spec/unit/matchers/notifications_matcher_spec.rb +0 -40
- data/spec/unit/matchers/render_file_matcher_spec.rb +0 -68
- data/spec/unit/matchers/resource_matcher_spec.rb +0 -5
- data/spec/unit/matchers/state_attrs_matcher_spec.rb +0 -68
- data/spec/unit/matchers/subscribes_matcher_spec.rb +0 -65
- data/spec/unit/renderer_spec.rb +0 -69
- data/spec/unit/server_runner_spec.rb +0 -28
- data/spec/unit/solo_runner_spec.rb +0 -171
- data/spec/unit/stubs/command_registry_spec.rb +0 -27
- data/spec/unit/stubs/command_stub_spec.rb +0 -61
- data/spec/unit/stubs/data_bag_item_registry_spec.rb +0 -39
- data/spec/unit/stubs/data_bag_item_stub_spec.rb +0 -36
- data/spec/unit/stubs/data_bag_registry_spec.rb +0 -39
- data/spec/unit/stubs/data_bag_stub_spec.rb +0 -35
- data/spec/unit/stubs/registry_spec.rb +0 -29
- data/spec/unit/stubs/search_registry_spec.rb +0 -39
- data/spec/unit/stubs/search_stub_spec.rb +0 -36
- data/spec/unit/stubs/stub_spec.rb +0 -64
data/README_old.md
DELETED
|
@@ -1,1291 +0,0 @@
|
|
|
1
|
-
# ChefSpec
|
|
2
|
-
|
|
3
|
-
[](https://badge.fury.io/rb/chefspec) [](https://travis-ci.org/chefspec/chefspec)
|
|
4
|
-
|
|
5
|
-
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.
|
|
6
|
-
|
|
7
|
-
ChefSpec runs your cookbook(s) locally with Chef Solo without actually converging a node. This has two primary benefits:
|
|
8
|
-
|
|
9
|
-
- It's really fast!
|
|
10
|
-
- Your tests can vary node attributes, operating systems, and search results to assert behavior under varying conditions.
|
|
11
|
-
|
|
12
|
-
## Important Notes
|
|
13
|
-
|
|
14
|
-
- **ChefSpec requires Ruby 2.2 or later and Chef 12.14.89 or later!**
|
|
15
|
-
- **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!**
|
|
16
|
-
- **Each resource matcher is self-documented using [Yard](http://rubydoc.info/github/chefspec/chefspec) and has a corresponding test recipe in the [examples directory](https://github.com/chefspec/chefspec/tree/master/examples).**
|
|
17
|
-
|
|
18
|
-
## Notes on Compatibility with Chef Versions
|
|
19
|
-
|
|
20
|
-
**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.
|
|
21
|
-
|
|
22
|
-
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:
|
|
23
|
-
|
|
24
|
-
## Writing a Cookbook Example
|
|
25
|
-
|
|
26
|
-
If you want `knife` to automatically generate spec stubs for you, install [knife-spec](https://github.com/sethvargo/knife-spec).
|
|
27
|
-
|
|
28
|
-
Given an extremely basic Chef recipe that just installs an operating system package:
|
|
29
|
-
|
|
30
|
-
```ruby
|
|
31
|
-
package 'foo'
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
the associated ChefSpec test might look like:
|
|
35
|
-
|
|
36
|
-
```ruby
|
|
37
|
-
require 'chefspec'
|
|
38
|
-
|
|
39
|
-
describe 'example::default' do
|
|
40
|
-
let(:chef_run) { ChefSpec::SoloRunner.new(platform: 'ubuntu', version: '16.04').converge(described_recipe) }
|
|
41
|
-
|
|
42
|
-
it 'installs foo' do
|
|
43
|
-
expect(chef_run).to install_package('foo')
|
|
44
|
-
end
|
|
45
|
-
end
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
Let's step through this file to see what is happening:
|
|
49
|
-
|
|
50
|
-
1. At the top of the spec file we require the chefspec gem. This is required so that our custom matchers are loaded. In larger projects, it is common practice to create a file named "spec_helper.rb" and include ChefSpec and perform other setup tasks in that file.
|
|
51
|
-
2. The `describe` keyword is part of RSpec and indicates that everything nested beneath is describing the `example::default` recipe. The convention is to have a separate spec for each recipe in your cookbook.
|
|
52
|
-
3. The `let` block on creates the `ChefSpec:SoloRunner` with mocked Ohai data for Ubuntu 16.04 from [Fauxhai](https://github.com/chefspec/fauxhai). It then does a fake Chef run with the run_list of `example::default`. Any subsequent examples can then refer to `chef_run` in order to make assertions about the resources that were created during the mock converge.
|
|
53
|
-
4. The `described_recipe` macro is a ChefSpec helper method that infers the recipe from the `describe` block. Alternatively you could specify the recipe directly.
|
|
54
|
-
5. The `it` block is an example specifying that the `foo` package is installed. Normally you will have multiple `it` blocks per recipe, each making a single assertion.
|
|
55
|
-
|
|
56
|
-
## Configuration
|
|
57
|
-
|
|
58
|
-
ChefSpec exposes a configuration layer at the global level and at the `Runner` level. The following settings are available:
|
|
59
|
-
|
|
60
|
-
```ruby
|
|
61
|
-
RSpec.configure do |config|
|
|
62
|
-
# Specify the path for Chef Solo to find cookbooks (default: [inferred from
|
|
63
|
-
# the location of the calling spec file])
|
|
64
|
-
config.cookbook_path = '/var/cookbooks'
|
|
65
|
-
|
|
66
|
-
# Specify the path for Chef Solo to find roles (default: [ascending search])
|
|
67
|
-
config.role_path = '/var/roles'
|
|
68
|
-
|
|
69
|
-
# Specify the path for Chef Solo to find environments (default: [ascending search])
|
|
70
|
-
config.environment_path = '/var/environments'
|
|
71
|
-
|
|
72
|
-
# Specify the path for Chef Solo file cache path (default: nil)
|
|
73
|
-
config.file_cache_path = Chef::Config[:file_cache_path]
|
|
74
|
-
|
|
75
|
-
# Specify the Chef log_level (default: :warn)
|
|
76
|
-
config.log_level = :debug
|
|
77
|
-
|
|
78
|
-
# Specify the path to a local JSON file with Ohai data (default: nil)
|
|
79
|
-
config.path = 'ohai.json'
|
|
80
|
-
|
|
81
|
-
# Specify the operating platform to mock Ohai data from (default: nil)
|
|
82
|
-
config.platform = 'ubuntu'
|
|
83
|
-
|
|
84
|
-
# Specify the operating version to mock Ohai data from (default: nil)
|
|
85
|
-
config.version = '14.04'
|
|
86
|
-
end
|
|
87
|
-
```
|
|
88
|
-
|
|
89
|
-
Values specified at the initialization of a "Runner" merge and take precedence over any global settings:
|
|
90
|
-
|
|
91
|
-
```ruby
|
|
92
|
-
# Override only the operating system version (platform is still "ubuntu" from above)
|
|
93
|
-
ChefSpec::SoloRunner.new(version: '16.04')
|
|
94
|
-
|
|
95
|
-
# Use a different operating system platform and version
|
|
96
|
-
ChefSpec::SoloRunner.new(platform: 'centos', version: '7.3.1611')
|
|
97
|
-
|
|
98
|
-
# Specify a different cookbook_path
|
|
99
|
-
ChefSpec::SoloRunner.new(cookbook_path: '/var/my/other/path', role_path: '/var/my/roles')
|
|
100
|
-
|
|
101
|
-
# By default ChefSpec sets a new temporary directory for file caching in every run.
|
|
102
|
-
# This can be overridden by passing the `file_cache_path` option.
|
|
103
|
-
# Note: Resources containing `Chef::Config[:file_cache_path]` in their name or
|
|
104
|
-
# attributes, will fail unless this option is specified.
|
|
105
|
-
ChefSpec::SoloRunner.new(file_cache_path: Chef::Config[:file_cache_path])
|
|
106
|
-
|
|
107
|
-
# Add debug log output
|
|
108
|
-
ChefSpec::SoloRunner.new(log_level: :debug).converge(described_recipe)
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
**NOTE** You do not _need_ to specify a platform and version to use ChefSpec. However, some cookbooks may rely on [Ohai](http://github.com/chef/ohai) data that ChefSpec cannot not automatically generate. Specifying the `platform` and `version` keys instructs ChefSpec to load stubbed Ohai attributes from another platform using [fauxhai](https://github.com/chefspec/fauxhai). See the [PLATFORMS.md file](https://github.com/chefspec/fauxhai/blob/master/PLATFORMS.md) in the Fauxhai repo for a complete list of platforms and versions for use with ChefSpec.
|
|
112
|
-
|
|
113
|
-
### ChefZero Server
|
|
114
|
-
|
|
115
|
-
The `ServerRunner` uses a [chef-zero](https://github.com/chef/chef-zero) instance as a stand-in for a full Chef Server. The instance is created at the initiation of the ChefSpec suite and is terminated at its completion. In between each test the state of the ChefZero server is completely reset.
|
|
116
|
-
|
|
117
|
-
```ruby
|
|
118
|
-
RSpec.configure do |config|
|
|
119
|
-
# When using ChefSpec::ServerRunner, specify the data storage method (options: in_memory, on_disk; default: in_memory)
|
|
120
|
-
# If you are in a low-memory environment, setting this value to :on_disk may improve speed and/or reliability.
|
|
121
|
-
config.server_runner_data_store = :on_disk
|
|
122
|
-
|
|
123
|
-
# Whether or not to clear the cookbooks on the ChefZero instance in-between each test (default: true)
|
|
124
|
-
# For most people, not clearing the cookbooks will drastically improve test execution time. This is a
|
|
125
|
-
# good option for people who are using chefspec within the context of a single Berksfile or Policyfile.
|
|
126
|
-
config.server_runner_clear_cookbooks = false
|
|
127
|
-
end
|
|
128
|
-
```
|
|
129
|
-
|
|
130
|
-
### Berkshelf
|
|
131
|
-
|
|
132
|
-
If you are using Berkshelf, simply require `chefspec/berkshelf` in your `spec_helper` after requiring `chefspec`:
|
|
133
|
-
|
|
134
|
-
```ruby
|
|
135
|
-
# spec_helper.rb
|
|
136
|
-
require 'chefspec'
|
|
137
|
-
require 'chefspec/berkshelf'
|
|
138
|
-
```
|
|
139
|
-
|
|
140
|
-
Requiring this file will:
|
|
141
|
-
|
|
142
|
-
- Create a temporary working directory
|
|
143
|
-
- Download all the dependencies listed in your `Berksfile` into the temporary directory
|
|
144
|
-
- Set ChefSpec's `cookbook_path` to the temporary directory
|
|
145
|
-
|
|
146
|
-
You can customize the list of options passed to the installation command using the `berkshelf_options` RSpec configuration:
|
|
147
|
-
|
|
148
|
-
```ruby
|
|
149
|
-
RSpec.configuration do |config|
|
|
150
|
-
config.berkshelf_options = { only: "my-group" }
|
|
151
|
-
end
|
|
152
|
-
```
|
|
153
|
-
|
|
154
|
-
This is a Ruby hash and valid options include `berksfile`, `only` and `except`.
|
|
155
|
-
|
|
156
|
-
### Librarian
|
|
157
|
-
|
|
158
|
-
If you are using Librarian, simply require `chefspec/librarian` in your `spec_helper` after requiring `chefspec`:
|
|
159
|
-
|
|
160
|
-
```ruby
|
|
161
|
-
# spec_helper.rb
|
|
162
|
-
require 'chefspec'
|
|
163
|
-
require 'chefspec/librarian'
|
|
164
|
-
```
|
|
165
|
-
|
|
166
|
-
Requiring this file will:
|
|
167
|
-
|
|
168
|
-
- Create a temporary working directory
|
|
169
|
-
- Download all the dependencies listed in your `Cheffile` into the temporary directory
|
|
170
|
-
- Set ChefSpec's `cookbook_path` to the temporary directory
|
|
171
|
-
|
|
172
|
-
**NOTE** In order to test the cookbook in the current working directory, you have to write your `Cheffile` like this:
|
|
173
|
-
|
|
174
|
-
```ruby
|
|
175
|
-
# Cheffile
|
|
176
|
-
site 'https://supermarket.chef.io/api/v1'
|
|
177
|
-
|
|
178
|
-
cookbook 'name_of_your_cookbook', path: '.'
|
|
179
|
-
```
|
|
180
|
-
|
|
181
|
-
### Policyfile
|
|
182
|
-
|
|
183
|
-
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.
|
|
184
|
-
|
|
185
|
-
```ruby
|
|
186
|
-
# spec_helper.rb
|
|
187
|
-
require 'chefspec'
|
|
188
|
-
require 'chefspec/policyfile'
|
|
189
|
-
```
|
|
190
|
-
|
|
191
|
-
Requiring this file will:
|
|
192
|
-
|
|
193
|
-
- Create a temporary working directory
|
|
194
|
-
- Download all the dependencies listed in your `Policyfile.rb` into the temporary directory
|
|
195
|
-
- Set ChefSpec's `cookbook_path` to the temporary directory
|
|
196
|
-
|
|
197
|
-
Your `Policyfile.rb` should look something like this:
|
|
198
|
-
|
|
199
|
-
```ruby
|
|
200
|
-
name 'my-cookbook'
|
|
201
|
-
run_list 'my-cookbook::default'
|
|
202
|
-
default_source :community
|
|
203
|
-
cookbook 'my-cookbook', path: '.'
|
|
204
|
-
```
|
|
205
|
-
|
|
206
|
-
## Running Specs
|
|
207
|
-
|
|
208
|
-
ChefSpec is actually an RSpec extension, so you can run your tests using the RSpec CLI:
|
|
209
|
-
|
|
210
|
-
```bash
|
|
211
|
-
$ rspec
|
|
212
|
-
```
|
|
213
|
-
|
|
214
|
-
You can also specify a specific spec to run and various RSpec command line options:
|
|
215
|
-
|
|
216
|
-
```bash
|
|
217
|
-
$ rspec spec/unit/recipes/default_spec.rb --color
|
|
218
|
-
```
|
|
219
|
-
|
|
220
|
-
For more information on the RSpec CLI, please see the [documentation](https://relishapp.com/rspec/rspec-core/docs/command-line).
|
|
221
|
-
|
|
222
|
-
## Making Assertions
|
|
223
|
-
|
|
224
|
-
ChefSpec asserts that resource actions have been performed. In general, ChefSpec follows the following pattern:
|
|
225
|
-
|
|
226
|
-
```ruby
|
|
227
|
-
require 'chefspec'
|
|
228
|
-
|
|
229
|
-
describe 'example::default' do
|
|
230
|
-
let(:chef_run) { ChefSpec::SoloRunner.converge(described_recipe) }
|
|
231
|
-
|
|
232
|
-
it 'does something' do
|
|
233
|
-
expect(chef_run).to ACTION_RESOURCE(NAME)
|
|
234
|
-
end
|
|
235
|
-
end
|
|
236
|
-
```
|
|
237
|
-
|
|
238
|
-
where:
|
|
239
|
-
|
|
240
|
-
- _ACTION_ - the action on the resource (e.g. `install`)
|
|
241
|
-
- _RESOURCE_ - the name of the resource (e.g. `package`)
|
|
242
|
-
- _NAME_ - the name attribute for the resource (e.g. `apache2`)
|
|
243
|
-
|
|
244
|
-
**NOTE** One exception to this rule is the `create_if_missing` action on the `file` resource. In this case the assertion is actually `create_file_if_missing`. Refer to `examples/file/spec/create_if_missing_spec.rb` for some examples.
|
|
245
|
-
|
|
246
|
-
Here's a more concrete example:
|
|
247
|
-
|
|
248
|
-
```ruby
|
|
249
|
-
require 'chefspec'
|
|
250
|
-
|
|
251
|
-
describe 'example::default' do
|
|
252
|
-
let(:chef_run) { ChefSpec::SoloRunner.converge(described_recipe) }
|
|
253
|
-
|
|
254
|
-
it 'installs apache2' do
|
|
255
|
-
expect(chef_run).to install_package('apache2')
|
|
256
|
-
end
|
|
257
|
-
end
|
|
258
|
-
```
|
|
259
|
-
|
|
260
|
-
This test is asserting that the Chef run will have a _package_ resource with the name _apache2_ with an action of _install_.
|
|
261
|
-
|
|
262
|
-
To test that a resource action is performed with a specific set of attributes, you can call `with(ATTRIBUTES_HASH)` on the expectation, per the following example:
|
|
263
|
-
|
|
264
|
-
```ruby
|
|
265
|
-
require 'chefspec'
|
|
266
|
-
|
|
267
|
-
describe 'example::default' do
|
|
268
|
-
let(:chef_run) { ChefSpec::SoloRunner.converge(described_recipe) }
|
|
269
|
-
|
|
270
|
-
it 'adds the member vagrant to the docker group' do
|
|
271
|
-
expect(chef_run).to modify_group('docker').with(members: ['vagrant'])
|
|
272
|
-
end
|
|
273
|
-
end
|
|
274
|
-
```
|
|
275
|
-
|
|
276
|
-
This test is asserting that the Chef run will have a _group_ resource with the name _docker_, an action of _modify_, and an attributes hash including `{ members: ['vagrant'] }`.
|
|
277
|
-
|
|
278
|
-
ChefSpec includes matchers for all of Chef's core resources using the above schema. Each resource matcher is self-documented using [Yard](http://rubydoc.info/github/chefspec/chefspec) and has a corresponding cucumber test from the [examples directory](https://github.com/chefspec/chefspec/tree/master/examples).
|
|
279
|
-
|
|
280
|
-
Additionally, ChefSpec includes the following helpful matchers. They are also [documented in Yard](http://rubydoc.info/github/chefspec/chefspec), but they are included here because they do not follow the "general pattern".
|
|
281
|
-
|
|
282
|
-
### include_recipe
|
|
283
|
-
|
|
284
|
-
Assert that the Chef run included a recipe from another cookbook
|
|
285
|
-
|
|
286
|
-
```ruby
|
|
287
|
-
expect(chef_run).to include_recipe('other_cookbook::recipe')
|
|
288
|
-
```
|
|
289
|
-
|
|
290
|
-
Keep the resources from an included recipe from being loaded into the Chef run, but test that the recipe was included
|
|
291
|
-
|
|
292
|
-
```ruby
|
|
293
|
-
describe 'example::default' do
|
|
294
|
-
let(:chef_run) { ChefSpec::SoloRunner.converge(described_recipe) }
|
|
295
|
-
|
|
296
|
-
before do
|
|
297
|
-
allow_any_instance_of(Chef::Recipe).to receive(:include_recipe).and_call_original
|
|
298
|
-
allow_any_instance_of(Chef::Recipe).to receive(:include_recipe).with('other_cookbook::default')
|
|
299
|
-
end
|
|
300
|
-
|
|
301
|
-
it 'includes the other_cookbook' do
|
|
302
|
-
expect_any_instance_of(Chef::Recipe).to receive(:include_recipe).with('other_cookbook::default')
|
|
303
|
-
chef_run
|
|
304
|
-
end
|
|
305
|
-
end
|
|
306
|
-
```
|
|
307
|
-
|
|
308
|
-
### notify
|
|
309
|
-
|
|
310
|
-
Assert that a resource notifies another in the Chef run
|
|
311
|
-
|
|
312
|
-
```ruby
|
|
313
|
-
resource = chef_run.template('/etc/foo')
|
|
314
|
-
expect(resource).to notify('service[apache2]').to(:restart).immediately
|
|
315
|
-
```
|
|
316
|
-
|
|
317
|
-
### subscribes
|
|
318
|
-
|
|
319
|
-
Assert that a resource subscribes to another in the Chef run
|
|
320
|
-
|
|
321
|
-
```ruby
|
|
322
|
-
resource = chef_run.service('apache2')
|
|
323
|
-
expect(resource).to subscribe_to('template[/etc/foo]').on(:create).delayed
|
|
324
|
-
```
|
|
325
|
-
|
|
326
|
-
### render_file
|
|
327
|
-
|
|
328
|
-
Assert that the Chef run renders a file (with optional content); this will match `cookbook_file`, `file`, and `template` resources and can also check the resulting content
|
|
329
|
-
|
|
330
|
-
```ruby
|
|
331
|
-
expect(chef_run).to render_file('/etc/foo')
|
|
332
|
-
expect(chef_run).to render_file('/etc/foo').with_content('This is content')
|
|
333
|
-
expect(chef_run).to render_file('/etc/foo').with_content(/regex works too.+/)
|
|
334
|
-
expect(chef_run).to render_file('/etc/foo').with_content { |content|
|
|
335
|
-
# Regular RSpec matches work in here
|
|
336
|
-
expect(content).to include('any RSpec matcher')
|
|
337
|
-
}
|
|
338
|
-
```
|
|
339
|
-
|
|
340
|
-
You can use any RSpec content matcher inside of the `with_content` predicate:
|
|
341
|
-
|
|
342
|
-
```ruby
|
|
343
|
-
expect(chef_run).to render_file('/etc/foo').with_content(start_with('# First line'))
|
|
344
|
-
```
|
|
345
|
-
|
|
346
|
-
You can chain `with_content` predicates together:
|
|
347
|
-
|
|
348
|
-
```ruby
|
|
349
|
-
expect(chef_run).to render_file('/etc/foo').with_content('This').with_content('content')
|
|
350
|
-
```
|
|
351
|
-
|
|
352
|
-
Prefer using a block to chaining `with_content` predicates, e.g. replace the above with:
|
|
353
|
-
|
|
354
|
-
```ruby
|
|
355
|
-
expect(chef_run).to render_file('/etc/foo').with_content { |content |
|
|
356
|
-
expect(content).to include('This')
|
|
357
|
-
expect(content).to include('content')
|
|
358
|
-
}
|
|
359
|
-
```
|
|
360
|
-
|
|
361
|
-
#### execution phase
|
|
362
|
-
|
|
363
|
-
It is possible to assert which [Chef phase of execution](https://docs.chef.io/chef_client.html#the-chef-client-title-run) a resource is created. Given a resource that is installed at compile time using `run_action`:
|
|
364
|
-
|
|
365
|
-
```ruby
|
|
366
|
-
package('apache2').run_action(:install)
|
|
367
|
-
```
|
|
368
|
-
|
|
369
|
-
You can assert that this package is installed during runtime using the `.at_compile_time` predicate on the resource matcher:
|
|
370
|
-
|
|
371
|
-
```ruby
|
|
372
|
-
expect(chef_run).to install_package('apache2').at_compile_time
|
|
373
|
-
```
|
|
374
|
-
|
|
375
|
-
Similarly, you can assert that a resource is executed during convergence time:
|
|
376
|
-
|
|
377
|
-
```ruby
|
|
378
|
-
expect(chef_run).to install_package('apache2').at_converge_time
|
|
379
|
-
```
|
|
380
|
-
|
|
381
|
-
Since "converge time" is the default behavior for all recipes, this test might be redundant and the predicate could be dropped depending on your situation.
|
|
382
|
-
|
|
383
|
-
### do_nothing
|
|
384
|
-
|
|
385
|
-
Assert that a resource performs no action
|
|
386
|
-
|
|
387
|
-
```ruby
|
|
388
|
-
resource = chef_run.execute('install')
|
|
389
|
-
expect(resource).to do_nothing
|
|
390
|
-
```
|
|
391
|
-
|
|
392
|
-
**For more complex examples, please see the [examples directory](https://github.com/chefspec/chefspec/tree/master/examples) or the [Yard documentation](http://rubydoc.info/github/chefspec/chefspec).**
|
|
393
|
-
|
|
394
|
-
## Specifying Node Information
|
|
395
|
-
|
|
396
|
-
Node information can be set when creating the `Runner`. The initializer yields a block that gives full access to the node object:
|
|
397
|
-
|
|
398
|
-
```ruby
|
|
399
|
-
describe 'example::default' do
|
|
400
|
-
let(:chef_run) do
|
|
401
|
-
ChefSpec::SoloRunner.new do |node|
|
|
402
|
-
node.name 'test.node-1'
|
|
403
|
-
end.converge(described_recipe)
|
|
404
|
-
end
|
|
405
|
-
|
|
406
|
-
it 'has node name set' do
|
|
407
|
-
expect(chef_run.node.name).to eq('test.node-1')
|
|
408
|
-
end
|
|
409
|
-
end
|
|
410
|
-
```
|
|
411
|
-
|
|
412
|
-
### Setting Node Attributes
|
|
413
|
-
|
|
414
|
-
Node attributes can also be set inside the initializer block:
|
|
415
|
-
|
|
416
|
-
```ruby
|
|
417
|
-
describe 'example::default' do
|
|
418
|
-
let(:chef_run) do
|
|
419
|
-
ChefSpec::SoloRunner.new do |node|
|
|
420
|
-
node.normal['cookbook']['attribute'] = 'hello'
|
|
421
|
-
end.converge(described_recipe)
|
|
422
|
-
end
|
|
423
|
-
end
|
|
424
|
-
```
|
|
425
|
-
|
|
426
|
-
### Automatic attributes
|
|
427
|
-
|
|
428
|
-
ChefSpec provides mocked automatic Ohai data using [fauxhai](https://github.com/chefspec/fauxhai). To mock out `automatic` attributes, you must use the `automatic` key:
|
|
429
|
-
|
|
430
|
-
```ruby
|
|
431
|
-
describe 'example::default' do
|
|
432
|
-
let(:chef_run) do
|
|
433
|
-
ChefSpec::SoloRunner.new do |node|
|
|
434
|
-
node.automatic['memory']['total'] = '512kB'
|
|
435
|
-
end.converge(described_recipe)
|
|
436
|
-
end
|
|
437
|
-
end
|
|
438
|
-
```
|
|
439
|
-
|
|
440
|
-
The `node` that is returned is actually a [`Chef::Node`](https://docs.chef.io/nodes.html) object.
|
|
441
|
-
|
|
442
|
-
To set an attribute within a specific test, set the attribute in the `it` block and then **(re-)converge the node**:
|
|
443
|
-
|
|
444
|
-
```ruby
|
|
445
|
-
describe 'example::default' do
|
|
446
|
-
let(:chef_run) { ChefSpec::SoloRunner.new } # Notice we don't converge here
|
|
447
|
-
|
|
448
|
-
it 'performs the action' do
|
|
449
|
-
chef_run.node.normal['cookbook']['attribute'] = 'hello'
|
|
450
|
-
chef_run.converge(described_recipe) # The converge happens inside the test
|
|
451
|
-
|
|
452
|
-
expect(chef_run).to do_something
|
|
453
|
-
end
|
|
454
|
-
end
|
|
455
|
-
```
|
|
456
|
-
|
|
457
|
-
## Using a Chef Server
|
|
458
|
-
|
|
459
|
-
All the examples thus far have used the `ChefSpec::SoloRunner`, which runs ChefSpec in Chef Solo mode. ChefSpec also includes the ability to create in-memory Chef Servers. This server can be populated with fake data and used to test search, data bags, and other "server-only" features.
|
|
460
|
-
|
|
461
|
-
To use the ChefSpec server, simply replace `ChefSpec::SoloRunner` with `ChefSpec::ServerRunner`:
|
|
462
|
-
|
|
463
|
-
```diff
|
|
464
|
-
describe 'example::default' do
|
|
465
|
-
- let(:chef_run) { ChefSpec::SoloRunner.converge(described_recipe) }
|
|
466
|
-
+ let(:chef_run) { ChefSpec::ServerRunner.converge(described_recipe) }
|
|
467
|
-
end
|
|
468
|
-
```
|
|
469
|
-
|
|
470
|
-
This will automatically create a Chef Server, synchronize all the cookbooks in your `cookbook_path`, and wire all the internals of Chef together. Recipe calls to `search`, `data_bag` and `data_bag_item` will now query this ChefSpec server.
|
|
471
|
-
|
|
472
|
-
### DSL
|
|
473
|
-
|
|
474
|
-
The ChefSpec server includes a collection of helpful DSL methods for populating data into the Chef Server.
|
|
475
|
-
|
|
476
|
-
Create a client:
|
|
477
|
-
|
|
478
|
-
```ruby
|
|
479
|
-
ChefSpec::ServerRunner.new do |node, server|
|
|
480
|
-
server.create_client('my_client', { admin: true })
|
|
481
|
-
end
|
|
482
|
-
```
|
|
483
|
-
|
|
484
|
-
Create a data bag (and items):
|
|
485
|
-
|
|
486
|
-
```ruby
|
|
487
|
-
ChefSpec::ServerRunner.new do |node, server|
|
|
488
|
-
server.create_data_bag('my_data_bag', {
|
|
489
|
-
'item_1' => {
|
|
490
|
-
'password' => 'abc123'
|
|
491
|
-
},
|
|
492
|
-
'item_2' => {
|
|
493
|
-
'password' => 'def456'
|
|
494
|
-
}
|
|
495
|
-
})
|
|
496
|
-
end
|
|
497
|
-
```
|
|
498
|
-
|
|
499
|
-
Create an environment:
|
|
500
|
-
|
|
501
|
-
```ruby
|
|
502
|
-
ChefSpec::ServerRunner.new do |node, server|
|
|
503
|
-
server.create_environment('my_environment', { default_attributes: { description: '...' } })
|
|
504
|
-
end
|
|
505
|
-
```
|
|
506
|
-
|
|
507
|
-
Create a node:
|
|
508
|
-
|
|
509
|
-
```ruby
|
|
510
|
-
ChefSpec::ServerRunner.new do |node, server|
|
|
511
|
-
server.create_node('my_node', { run_list: ['...'] })
|
|
512
|
-
end
|
|
513
|
-
```
|
|
514
|
-
|
|
515
|
-
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:
|
|
516
|
-
|
|
517
|
-
```ruby
|
|
518
|
-
ChefSpec::ServerRunner.new do |node, server|
|
|
519
|
-
node.normal['attribute'] = 'value'
|
|
520
|
-
|
|
521
|
-
# At this point, the server already has a copy of the current node object due
|
|
522
|
-
# to the way Chef compiled the resources. However, that node does not have
|
|
523
|
-
# this new value. As such, you must "save" the node back to the server to
|
|
524
|
-
# persist this attribute update.
|
|
525
|
-
server.update_node(node)
|
|
526
|
-
end
|
|
527
|
-
```
|
|
528
|
-
|
|
529
|
-
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:
|
|
530
|
-
|
|
531
|
-
```ruby
|
|
532
|
-
www = stub_node(platform: 'ubuntu', version: '16.04') do |node|
|
|
533
|
-
node.normal['attribute'] = 'value'
|
|
534
|
-
end
|
|
535
|
-
|
|
536
|
-
# `www` is now a local Chef::Node object you can use in your test. To publish
|
|
537
|
-
# this node to the server, call `create_node`:
|
|
538
|
-
|
|
539
|
-
ChefSpec::ServerRunner.new do |node, server|
|
|
540
|
-
server.create_node(www)
|
|
541
|
-
end
|
|
542
|
-
```
|
|
543
|
-
|
|
544
|
-
Create a role:
|
|
545
|
-
|
|
546
|
-
```ruby
|
|
547
|
-
ChefSpec::ServerRunner.new do |node, server|
|
|
548
|
-
server.create_role('my_role', { default_attributes: {} })
|
|
549
|
-
end
|
|
550
|
-
```
|
|
551
|
-
|
|
552
|
-
**NOTE** The ChefSpec server is empty at the start of each example to avoid interdependent tests.
|
|
553
|
-
|
|
554
|
-
### Data Store
|
|
555
|
-
|
|
556
|
-
The `ServerRunner` has two options for how it will store data: `in_memory` or `on_disk`. The default value is `in_memory`. These two options have different performance implications based on your specific setup. If you are running into performance problems (slow tests, frequent hanging, etc) with one setting, try using the other.
|
|
557
|
-
|
|
558
|
-
## Stubbing
|
|
559
|
-
|
|
560
|
-
### Command
|
|
561
|
-
|
|
562
|
-
Given a recipe with shell guard:
|
|
563
|
-
|
|
564
|
-
```ruby
|
|
565
|
-
template '/tmp/foo.txt' do
|
|
566
|
-
not_if 'grep text /tmp/foo.txt'
|
|
567
|
-
end
|
|
568
|
-
```
|
|
569
|
-
|
|
570
|
-
ChefSpec will raise an error like:
|
|
571
|
-
|
|
572
|
-
```text
|
|
573
|
-
Real commands are disabled. Unregistered command: `grep text /tmp/foo.txt`
|
|
574
|
-
|
|
575
|
-
You can stub this command with:
|
|
576
|
-
|
|
577
|
-
stub_command("grep text /tmp/foo.txt").and_return(true)
|
|
578
|
-
|
|
579
|
-
============================================================
|
|
580
|
-
```
|
|
581
|
-
|
|
582
|
-
Just like the error message says, you must stub the command result. This can be done inside a `before` block or inside the `it` block, and the stubbing method accepts both a value or Ruby code. If provided a value, the result is static. If provided a Ruby block, the block is evaluated each time the search is called.
|
|
583
|
-
|
|
584
|
-
```ruby
|
|
585
|
-
describe 'example::default' do
|
|
586
|
-
let(:chef_run) { ChefSpec::SoloRunner.new }
|
|
587
|
-
|
|
588
|
-
before do
|
|
589
|
-
stub_command("grep text /tmp/foo.txt").and_return(true)
|
|
590
|
-
end
|
|
591
|
-
end
|
|
592
|
-
```
|
|
593
|
-
|
|
594
|
-
```ruby
|
|
595
|
-
describe 'example::default' do
|
|
596
|
-
let(:chef_run) { ChefSpec::SoloRunner.new }
|
|
597
|
-
|
|
598
|
-
before do
|
|
599
|
-
stub_command("grep text /tmp/foo.txt") { rand(50)%2 == 0 }
|
|
600
|
-
end
|
|
601
|
-
end
|
|
602
|
-
```
|
|
603
|
-
|
|
604
|
-
The stubbed command can also be passed as a regular expression, allowing multiple commands to be stubbed with one line.
|
|
605
|
-
|
|
606
|
-
```ruby
|
|
607
|
-
describe 'example::default' do
|
|
608
|
-
let(:chef_run) { ChefSpec::SoloRunner.new }
|
|
609
|
-
|
|
610
|
-
before do
|
|
611
|
-
stub_command(/(foo)|(bar)/).and_return(true)
|
|
612
|
-
end
|
|
613
|
-
end
|
|
614
|
-
```
|
|
615
|
-
|
|
616
|
-
### Library Helpers
|
|
617
|
-
|
|
618
|
-
Given a library helper with a `has_bacon?` method:
|
|
619
|
-
|
|
620
|
-
```ruby
|
|
621
|
-
module Demo
|
|
622
|
-
module Helper
|
|
623
|
-
|
|
624
|
-
include Chef::Mixin::ShellOut
|
|
625
|
-
|
|
626
|
-
def has_bacon?
|
|
627
|
-
cmd = shell_out!('getent passwd bacon', {:returns => [0,2]})
|
|
628
|
-
cmd.stderr.empty? && (cmd.stdout =~ /^bacon/)
|
|
629
|
-
end
|
|
630
|
-
end
|
|
631
|
-
end
|
|
632
|
-
```
|
|
633
|
-
|
|
634
|
-
Stub the output of the library helper. [Additional information](http://jtimberman.housepub.org/blog/2015/05/30/quick-tip-stubbing-library-helpers-in-chefspec/)
|
|
635
|
-
|
|
636
|
-
```ruby
|
|
637
|
-
before do
|
|
638
|
-
allow_any_instance_of(Chef::Node).to receive(:has_bacon?).and_return(true)
|
|
639
|
-
end
|
|
640
|
-
```
|
|
641
|
-
|
|
642
|
-
### Data Bag & Data Bag Item
|
|
643
|
-
|
|
644
|
-
**NOTE** This is not required if you are using a ChefSpec server.
|
|
645
|
-
|
|
646
|
-
Given a recipe that executes a `data_bag` method:
|
|
647
|
-
|
|
648
|
-
```ruby
|
|
649
|
-
data_bag('users').each do |user|
|
|
650
|
-
data_bag_item('users', user['id'])
|
|
651
|
-
end
|
|
652
|
-
```
|
|
653
|
-
|
|
654
|
-
ChefSpec will raise an error like:
|
|
655
|
-
|
|
656
|
-
```text
|
|
657
|
-
Real data_bags are disabled. Unregistered data_bag: data_bag(:users)
|
|
658
|
-
|
|
659
|
-
You can stub this data_bag with:
|
|
660
|
-
|
|
661
|
-
stub_data_bag("users").and_return([])
|
|
662
|
-
|
|
663
|
-
============================================================
|
|
664
|
-
```
|
|
665
|
-
|
|
666
|
-
Just like the error message says, you must stub the result of the `data_bag` call. This can be done inside a `before` block or inside the `it` block, and the stubbing method accepts both a value or Ruby code. If provided a value, the result is static. If provided a Ruby block, the block is evaluated each time the search is called.
|
|
667
|
-
|
|
668
|
-
```ruby
|
|
669
|
-
describe 'example::default' do
|
|
670
|
-
let(:chef_run) { ChefSpec::SoloRunner.new }
|
|
671
|
-
|
|
672
|
-
before do
|
|
673
|
-
stub_data_bag('users').and_return([])
|
|
674
|
-
end
|
|
675
|
-
end
|
|
676
|
-
```
|
|
677
|
-
|
|
678
|
-
```ruby
|
|
679
|
-
describe 'example::default' do
|
|
680
|
-
let(:chef_run) { ChefSpec::SoloRunner.new }
|
|
681
|
-
|
|
682
|
-
before do
|
|
683
|
-
stub_data_bag('users').and_return(['svargo', 'francis'])
|
|
684
|
-
|
|
685
|
-
stub_data_bag_item('users', 'svargo').and_return({ ... })
|
|
686
|
-
stub_data_bag_item('users', 'francis') { (ruby code) }
|
|
687
|
-
end
|
|
688
|
-
end
|
|
689
|
-
```
|
|
690
|
-
|
|
691
|
-
If you are using **Encrypted Data Bag Items**, you'll need to dive into the RSpec layer and stub that class method instead:
|
|
692
|
-
|
|
693
|
-
```ruby
|
|
694
|
-
describe 'example::default' do
|
|
695
|
-
before do
|
|
696
|
-
allow(Chef::EncryptedDataBagItem).to receive(:load).with('users', 'svargo').and_return(...)
|
|
697
|
-
end
|
|
698
|
-
end
|
|
699
|
-
```
|
|
700
|
-
|
|
701
|
-
### Search
|
|
702
|
-
|
|
703
|
-
**NOTE** This is not required if you are using a ChefSpec server.
|
|
704
|
-
|
|
705
|
-
Because ChefSpec is a unit-testing framework, it is recommended that all third-party API calls be mocked or stubbed. ChefSpec exposes a helpful RSpec macro for stubbing search results in your tests. If you converge a Chef recipe that implements a `search` call, ChefSpec will throw an error like:
|
|
706
|
-
|
|
707
|
-
```text
|
|
708
|
-
Real searches are disabled. Unregistered search: search(:node, 'name:hello')
|
|
709
|
-
|
|
710
|
-
You can stub this search with:
|
|
711
|
-
|
|
712
|
-
stub_search(:node, 'name:hello') { }
|
|
713
|
-
|
|
714
|
-
============================================================
|
|
715
|
-
```
|
|
716
|
-
|
|
717
|
-
Just like the error message says, you must stub the search result. This can be done inside a `before` block or inside the `it` block, and the stubbing method accepts both a value or Ruby code. If provided a value, the result is static. If provided a Ruby block, the block is evaluated each time the search is called.
|
|
718
|
-
|
|
719
|
-
```ruby
|
|
720
|
-
describe 'example::default' do
|
|
721
|
-
let(:chef_run) { ChefSpec::SoloRunner.new }
|
|
722
|
-
|
|
723
|
-
before do
|
|
724
|
-
stub_search(:node, 'name:hello').and_return([])
|
|
725
|
-
end
|
|
726
|
-
end
|
|
727
|
-
```
|
|
728
|
-
|
|
729
|
-
```ruby
|
|
730
|
-
describe 'example::default' do
|
|
731
|
-
let(:chef_run) { ChefSpec::SoloRunner.new }
|
|
732
|
-
|
|
733
|
-
before do
|
|
734
|
-
stub_search(:node, 'name:hello') { (ruby_code) }
|
|
735
|
-
end
|
|
736
|
-
end
|
|
737
|
-
```
|
|
738
|
-
|
|
739
|
-
### Ruby libraries (File, FileUtils, etc)
|
|
740
|
-
|
|
741
|
-
When stubbing core ruby libraries, users must be aware that there is no differentiation between your cookbook code that calls `File.exist?` and core chef code (e.g. the cookbook loader) that calls `File.exist?`. If you stub or setup an expectation without qualifying the arguments then you will stub that method for all core chef code as well. Also note that if you setup an expectation on a particular argument that invoking the method with any other argument will throw an unexpected argument error out of rspec, so you must setup an allowance using `.and_call_original` to avoid breaking core chef.
|
|
742
|
-
|
|
743
|
-
```ruby
|
|
744
|
-
describe 'example::default' do
|
|
745
|
-
let(:chef_run) { ChefSpec::SoloRunner.new }
|
|
746
|
-
|
|
747
|
-
before do
|
|
748
|
-
# avoid breaking all of core chef wherever it calls File.exist? with other arguments
|
|
749
|
-
allow(File).to receive(:exist?).and_call_original
|
|
750
|
-
end
|
|
751
|
-
|
|
752
|
-
it "tests something when /etc/myfile.txt does not exist" do
|
|
753
|
-
# only setup an expectation on our file
|
|
754
|
-
expect(File).to receive(:exist?).with("/etc/myfile.txt").and_return(false)
|
|
755
|
-
[ ... test that the chef resource collection is constructed correctly in this case ... ]
|
|
756
|
-
end
|
|
757
|
-
end
|
|
758
|
-
```
|
|
759
|
-
|
|
760
|
-
This is basic usage of rspec and not specific to chefspec. It applies to any class method in `File`, `Dir`, `FileUtils`, `IO` or any other ruby library. In general any time you `expect(Some::Symbol).to receive(:a_method).and_return(value)` you run the risk of breaking other code unless you isolate your mocking or expectation down to only the arguments which your code uses.
|
|
761
|
-
|
|
762
|
-
## Reporting
|
|
763
|
-
|
|
764
|
-
**NOTE: The coverage reporting feature is deprecated and will be removed in a future version. This documentation exists only for reference purposes.**
|
|
765
|
-
|
|
766
|
-
ChefSpec can generate a report of resources read over resources tested.
|
|
767
|
-
|
|
768
|
-
To generate the coverage report, add the following to your `spec_helper.rb` before you require any "Chef" code:
|
|
769
|
-
|
|
770
|
-
```ruby
|
|
771
|
-
require 'chefspec'
|
|
772
|
-
ChefSpec::Coverage.start!
|
|
773
|
-
|
|
774
|
-
# Existing spec_helper contents...
|
|
775
|
-
```
|
|
776
|
-
|
|
777
|
-
By default, that method will output helpful information to standard out:
|
|
778
|
-
|
|
779
|
-
```text
|
|
780
|
-
ChefSpec Coverage report generated...
|
|
781
|
-
|
|
782
|
-
Total Resources: 6
|
|
783
|
-
Touched Resources: 1
|
|
784
|
-
Touch Coverage: 16.67%
|
|
785
|
-
|
|
786
|
-
Untouched Resources:
|
|
787
|
-
|
|
788
|
-
package[git] bacon/recipes/default.rb:2
|
|
789
|
-
package[build-essential] bacon/recipes/default.rb:3
|
|
790
|
-
package[apache2] bacon/recipes/default.rb:4
|
|
791
|
-
package[libvirt] bacon/recipes/default.rb:5
|
|
792
|
-
package[core] bacon/recipes/default.rb:6
|
|
793
|
-
```
|
|
794
|
-
|
|
795
|
-
By default, ChefSpec will test all cookbooks that are loaded as part of the Chef Client run. If you have a cookbook with many dependencies, this may be less than desirable. To restrict coverage reporting against certain cookbooks, `ChefSpec::Coverage` yields a block:
|
|
796
|
-
|
|
797
|
-
```ruby
|
|
798
|
-
ChefSpec::Coverage.start! do
|
|
799
|
-
add_filter 'vendor/cookbooks'
|
|
800
|
-
end
|
|
801
|
-
```
|
|
802
|
-
|
|
803
|
-
The `add_filter` method accepts a variety of objects. For example:
|
|
804
|
-
|
|
805
|
-
```ruby
|
|
806
|
-
ChefSpec::Coverage.start! do
|
|
807
|
-
# Strings are interpreted as file paths, with a forward anchor
|
|
808
|
-
add_filter 'vendor/cookbooks'
|
|
809
|
-
|
|
810
|
-
# Regular expressions must be escaped, but provide a nicer API for negative
|
|
811
|
-
# back tracking
|
|
812
|
-
add_filter /cookbooks\/(?!omnibus)/
|
|
813
|
-
|
|
814
|
-
# Custom block filters yield a {Chef::Resource} object - if the block
|
|
815
|
-
# evaluates to true, it will be filtered
|
|
816
|
-
add_filter do |resource|
|
|
817
|
-
# Bob's cookbook's are completely untested! Ignore them until he gets his
|
|
818
|
-
# shit together.
|
|
819
|
-
resource.source_file =~ /cookbooks\/bob-(.+)/
|
|
820
|
-
end
|
|
821
|
-
end
|
|
822
|
-
```
|
|
823
|
-
|
|
824
|
-
For more complex scenarios, you can create a custom `Filter` object that inherits from `ChefSpec::Coverage::Filter` and implements the `matches?` method.
|
|
825
|
-
|
|
826
|
-
```ruby
|
|
827
|
-
class CustomFilter < ChefSpec::Coverage::Filter
|
|
828
|
-
def initialize(arg1, arg2, █)
|
|
829
|
-
# Create a custom initialization method, do some magic, etc.
|
|
830
|
-
end
|
|
831
|
-
|
|
832
|
-
def matches?(resource)
|
|
833
|
-
# Custom matching logic in here - anything that evaluates to "true" will be
|
|
834
|
-
# filtered.
|
|
835
|
-
end
|
|
836
|
-
end
|
|
837
|
-
|
|
838
|
-
ChefSpec::Coverage.start! do
|
|
839
|
-
add_filter CustomFilter.new('foo', :bar)
|
|
840
|
-
end
|
|
841
|
-
```
|
|
842
|
-
|
|
843
|
-
If you are using ChefSpec's Berkshelf plugin, a filter is automatically created for you. If you would like to ignore that filter, you can `clear` all the filters before defining your own:
|
|
844
|
-
|
|
845
|
-
```ruby
|
|
846
|
-
ChefSpec::Coverage.start! do
|
|
847
|
-
filters.clear
|
|
848
|
-
|
|
849
|
-
# Add your custom filters now
|
|
850
|
-
end
|
|
851
|
-
```
|
|
852
|
-
|
|
853
|
-
If you would like a different output format for the Coverage.report! output, you can specify one of the three built-in templates, or supply your own by calling the set_template in the `ChefSpec::Coverage` block:
|
|
854
|
-
|
|
855
|
-
```ruby
|
|
856
|
-
ChefSpec::Coverage.start! do
|
|
857
|
-
set_template 'json.erb'
|
|
858
|
-
end
|
|
859
|
-
```
|
|
860
|
-
|
|
861
|
-
Provided templates are human.erb_(default)_, table.erb and json.erb, to supply a custom template, specify a relative(to run directory) or absolute path.
|
|
862
|
-
|
|
863
|
-
```ruby
|
|
864
|
-
ChefSpec::Coverage.start! do
|
|
865
|
-
set_template '/opt/custom/templates/verbose.erb'
|
|
866
|
-
end
|
|
867
|
-
```
|
|
868
|
-
|
|
869
|
-
If you would like to add alternative reporting for the Coverage.report! ouput, you can supply your own by calling add_output in the `ChefSepc::Coverage` block: Note the reportOutput has the following items in it: total, touched, coverage and collections of untouched_resources and all_resources
|
|
870
|
-
|
|
871
|
-
```ruby
|
|
872
|
-
ChefSpec::Coverage.start! do
|
|
873
|
-
add_output do |reportOutput|
|
|
874
|
-
File.open( "coverage.json","w" ) do |f|
|
|
875
|
-
f.puts(reportOutput[:total])
|
|
876
|
-
f.puts(reportOutput[:touched])
|
|
877
|
-
f.puts(reportOutput[:coverage])
|
|
878
|
-
f.puts(reportOutput[:untouched_resources])
|
|
879
|
-
f.puts(reportOutput[:all_resources])
|
|
880
|
-
end
|
|
881
|
-
end
|
|
882
|
-
end
|
|
883
|
-
```
|
|
884
|
-
|
|
885
|
-
Note the above example outputs the raw data without applying formatting.
|
|
886
|
-
|
|
887
|
-
## Mocking Out Environments
|
|
888
|
-
|
|
889
|
-
### ServerRunner
|
|
890
|
-
|
|
891
|
-
```ruby
|
|
892
|
-
ChefSpec::ServerRunner.new do |node, server|
|
|
893
|
-
# Create the environment
|
|
894
|
-
server.create_environment('staging', { default_attributes: { cookbook_attr: 'value' } })
|
|
895
|
-
|
|
896
|
-
# Assign the environment to the node
|
|
897
|
-
node.chef_environment = 'staging'
|
|
898
|
-
end
|
|
899
|
-
```
|
|
900
|
-
|
|
901
|
-
### SoloRunner
|
|
902
|
-
|
|
903
|
-
If you want to mock out `node.chef_environment`, you'll need to use RSpec mocks/stubs twice:
|
|
904
|
-
|
|
905
|
-
```ruby
|
|
906
|
-
let(:chef_run) do
|
|
907
|
-
ChefSpec::SoloRunner.new do |node|
|
|
908
|
-
# Create a new environment (you could also use a different :let block or :before block)
|
|
909
|
-
env = Chef::Environment.new
|
|
910
|
-
env.name 'staging'
|
|
911
|
-
|
|
912
|
-
# Stub the node to return this environment
|
|
913
|
-
allow(node).to receive(:chef_environment).and_return(env.name)
|
|
914
|
-
|
|
915
|
-
# Stub any calls to Environment.load to return this environment
|
|
916
|
-
allow(Chef::Environment).to receive(:load).and_return(env)
|
|
917
|
-
end.converge('cookbook::recipe')
|
|
918
|
-
end
|
|
919
|
-
```
|
|
920
|
-
|
|
921
|
-
**There is probably a better/easier way to do this. If you have a better solution, please open an issue or Pull Request so we can make this less painful :)**
|
|
922
|
-
|
|
923
|
-
## Testing LWRPs
|
|
924
|
-
|
|
925
|
-
**WARNING** Cookbooks with dashes (hyphens) are difficult to test with ChefSpec because of how Chef classifies objects. We recommend naming cookbooks with underscores (`_`) instead of dashes (`-`).
|
|
926
|
-
|
|
927
|
-
ChefSpec overrides all providers to take no action (otherwise it would actually converge your system). This means that the steps inside your LWRP are not actually executed. If an LWRP performs actions, those actions are never executed or added to the resource collection.
|
|
928
|
-
|
|
929
|
-
In order to run the actions exposed by your LWRP, you have to explicitly tell the `Runner` to step into it:
|
|
930
|
-
|
|
931
|
-
```ruby
|
|
932
|
-
require 'chefspec'
|
|
933
|
-
|
|
934
|
-
describe 'foo::default' do
|
|
935
|
-
let(:chef_run) do
|
|
936
|
-
ChefSpec::SoloRunner.new(step_into: ['my_lwrp']).converge('foo::default')
|
|
937
|
-
end
|
|
938
|
-
|
|
939
|
-
it 'installs the foo package through my_lwrp' do
|
|
940
|
-
expect(chef_run).to install_package('foo')
|
|
941
|
-
end
|
|
942
|
-
end
|
|
943
|
-
```
|
|
944
|
-
|
|
945
|
-
**NOTE:** If your cookbook exposes LWRPs, it is highly recommended you also create a `libraries/matchers.rb` file as outlined below in the "Packaging Custom Matchers" section. **You should never `step_into` an LWRP unless you are testing it. Never `step_into` an LWRP from another cookbook!**
|
|
946
|
-
|
|
947
|
-
## Automatic Matchers
|
|
948
|
-
|
|
949
|
-
As of ChefSpec 7.1.0 there are "custom" matchers generated for all internal core-chef resources, along with any LWRPs/HWRPs/Custom Resources that are user-defined in cookbooks.
|
|
950
|
-
|
|
951
|
-
The matchers follow the standard custom of `<action>_<resource_name>` with the exception of the `create_if_missing` action which _also_ gets a `create_<resource_name>_if_missing` matcher.
|
|
952
|
-
|
|
953
|
-
Matchers should be wired up for the `resource_name` of the resource along with all define `provides` lines synonyms and any `action` methods or `allowed_actions`.
|
|
954
|
-
|
|
955
|
-
There should be little reason to package custom matchers in cookbooks any more, but the approach below still works if there are special matchers which cookbooks wish to expose which do not follow the automatically generated pattern.
|
|
956
|
-
|
|
957
|
-
## Packaging Custom Matchers
|
|
958
|
-
|
|
959
|
-
ChefSpec exposes the ability for cookbook authors to package custom matchers inside a cookbook so that other developers may take advantage of them in testing. This is done by creating a special library file in the cookbook named `matchers.rb`:
|
|
960
|
-
|
|
961
|
-
```ruby
|
|
962
|
-
# cookbook/libraries/matchers.rb
|
|
963
|
-
|
|
964
|
-
if defined?(ChefSpec)
|
|
965
|
-
def my_custom_matcher(resource_name)
|
|
966
|
-
ChefSpec::Matchers::ResourceMatcher.new(resource, action, resource_name)
|
|
967
|
-
end
|
|
968
|
-
end
|
|
969
|
-
```
|
|
970
|
-
|
|
971
|
-
1. The entire contents of this file must be wrapped with the conditional clause checking if `ChefSpec` is defined.
|
|
972
|
-
2. Each matcher is actually a top-level method. The above example corresponds to the following RSpec test:
|
|
973
|
-
|
|
974
|
-
```ruby
|
|
975
|
-
expect(chef_run).to my_custom_matcher('...')
|
|
976
|
-
```
|
|
977
|
-
|
|
978
|
-
3. `ChefSpec::Matchers::ResourceMatcher` accepts three parameters:
|
|
979
|
-
|
|
980
|
-
1. The name of the resource to find in the resource collection (i.e. the name of the LWRP).
|
|
981
|
-
2. The action that resource should receive.
|
|
982
|
-
3. The value of the name attribute of the resource to find. (This is typically proxied as the value from the matcher definition.)
|
|
983
|
-
|
|
984
|
-
ChefSpec's built-in `ResourceMatcher` _should_ satisfy most common use cases for packaging a custom matcher with your LWRPs. However, if your cookbook is extending Chef core or is outside of the scope of a traditional "resource", you may need to create a custom matcher. For more information on custom matchers in RSpec, please [watch the Railscast on Custom Matchers](http://railscasts.com/episodes/157-rspec-matchers-macros) or look at some of the other custom matchers in ChefSpec's source code.
|
|
985
|
-
|
|
986
|
-
### Example
|
|
987
|
-
|
|
988
|
-
Suppose I have a cookbook named "motd" with a resource/provider "message".
|
|
989
|
-
|
|
990
|
-
```ruby
|
|
991
|
-
# motd/resources/message.rb
|
|
992
|
-
actions :write
|
|
993
|
-
default_action :write
|
|
994
|
-
|
|
995
|
-
attribute :message, name_attribute: true
|
|
996
|
-
```
|
|
997
|
-
|
|
998
|
-
```ruby
|
|
999
|
-
# motd/providers/message.rb
|
|
1000
|
-
action :write do
|
|
1001
|
-
# ...
|
|
1002
|
-
end
|
|
1003
|
-
```
|
|
1004
|
-
|
|
1005
|
-
Chef will dynamically build the `motd_message` LWRP at runtime that can be used in the recipe DSL:
|
|
1006
|
-
|
|
1007
|
-
```ruby
|
|
1008
|
-
motd_message 'my message'
|
|
1009
|
-
```
|
|
1010
|
-
|
|
1011
|
-
You can package a custom ChefSpec matcher with the motd cookbook by including the following code in `libraries/matchers.rb`:
|
|
1012
|
-
|
|
1013
|
-
```ruby
|
|
1014
|
-
# motd/libraries/matchers.rb
|
|
1015
|
-
if defined?(ChefSpec)
|
|
1016
|
-
def write_motd_message(message)
|
|
1017
|
-
ChefSpec::Matchers::ResourceMatcher.new(:motd_message, :write, message)
|
|
1018
|
-
end
|
|
1019
|
-
end
|
|
1020
|
-
```
|
|
1021
|
-
|
|
1022
|
-
Other developers can write RSpec tests against your LWRP in their cookbooks:
|
|
1023
|
-
|
|
1024
|
-
```ruby
|
|
1025
|
-
expect(chef_run).to write_motd_message('my message')
|
|
1026
|
-
```
|
|
1027
|
-
|
|
1028
|
-
**Don't forget to include documentation in your cookbook's README noting the custom matcher and its API!**
|
|
1029
|
-
|
|
1030
|
-
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:
|
|
1031
|
-
|
|
1032
|
-
```ruby
|
|
1033
|
-
# motd/resources/message.rb
|
|
1034
|
-
actions :write
|
|
1035
|
-
default_action :write
|
|
1036
|
-
|
|
1037
|
-
provides :foobar
|
|
1038
|
-
|
|
1039
|
-
attribute :message, name_attribute: true
|
|
1040
|
-
```
|
|
1041
|
-
|
|
1042
|
-
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:
|
|
1043
|
-
|
|
1044
|
-
```ruby
|
|
1045
|
-
# motd/libraries/matchers.rb
|
|
1046
|
-
if defined?(ChefSpec)
|
|
1047
|
-
def write_foobar(message)
|
|
1048
|
-
ChefSpec::Matchers::ResourceMatcher.new(:motd_message, :write, message)
|
|
1049
|
-
end
|
|
1050
|
-
end
|
|
1051
|
-
```
|
|
1052
|
-
|
|
1053
|
-
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.
|
|
1054
|
-
|
|
1055
|
-
## Writing Custom Matchers
|
|
1056
|
-
|
|
1057
|
-
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:
|
|
1058
|
-
|
|
1059
|
-
```ruby
|
|
1060
|
-
# spec/support/matchers.rb
|
|
1061
|
-
def my_custom_matcher(resource_name)
|
|
1062
|
-
ChefSpec::Matchers::ResourceMatcher.new(:resource, :action, resource_name)
|
|
1063
|
-
end
|
|
1064
|
-
```
|
|
1065
|
-
|
|
1066
|
-
Then require this file in your `spec_helper.rb` so the matcher can be used:
|
|
1067
|
-
|
|
1068
|
-
```ruby
|
|
1069
|
-
require_relative 'support/matchers'
|
|
1070
|
-
```
|
|
1071
|
-
|
|
1072
|
-
Please use this as a _temporary_ solution. Consider sending a Pull Request to the LWRP author(s) packaging the custom resource matchers (see previous section).
|
|
1073
|
-
|
|
1074
|
-
## Matchers for looking up custom resources
|
|
1075
|
-
|
|
1076
|
-
ChefSpec also provides a helper method to define a method on the Chef runner for locating a resource in the collection. This is helpful while asserting against custom resource notifications.
|
|
1077
|
-
|
|
1078
|
-
```ruby
|
|
1079
|
-
# matchers.rb
|
|
1080
|
-
ChefSpec.define_matcher :my_custom_resource
|
|
1081
|
-
```
|
|
1082
|
-
|
|
1083
|
-
And then in your spec suite, you can obtain the custom resource for assertions:
|
|
1084
|
-
|
|
1085
|
-
```ruby
|
|
1086
|
-
let(:chef_run) { ChefSpec::SoloRunner.converge('...') }
|
|
1087
|
-
|
|
1088
|
-
it 'notifies the thing' do
|
|
1089
|
-
custom = chef_run.my_custom_resource('name')
|
|
1090
|
-
expect(custom).to notify('service[apache2]').to(:restart).immediately
|
|
1091
|
-
end
|
|
1092
|
-
```
|
|
1093
|
-
|
|
1094
|
-
You can use this functionality to bundle lookup matchers with cookbooks, or to provide your own when the upstream cookbook doesn't include it.
|
|
1095
|
-
|
|
1096
|
-
## Expecting Exceptions
|
|
1097
|
-
|
|
1098
|
-
In Chef 11, custom formatters were introduced and ChefSpec uses a custom formatter to suppress Chef Client output. In the event of a convergence failure, ChefSpec will output the error message from the run to help you debug:
|
|
1099
|
-
|
|
1100
|
-
```text
|
|
1101
|
-
================================================================================
|
|
1102
|
-
Recipe Compile Error in apt_package/recipes/install.rb
|
|
1103
|
-
================================================================================
|
|
1104
|
-
|
|
1105
|
-
RuntimeError
|
|
1106
|
-
------------
|
|
1107
|
-
RuntimeError
|
|
1108
|
-
|
|
1109
|
-
Cookbook Trace:
|
|
1110
|
-
---------------
|
|
1111
|
-
.../apt_package/recipes/install.rb:1:in `from_file'
|
|
1112
|
-
.../apt_package/spec/install_spec.rb:4:in `block (2 levels) in <top (required)>'
|
|
1113
|
-
.../apt_package/spec/install_spec.rb:7:in `block (2 levels) in <top (required)>'
|
|
1114
|
-
|
|
1115
|
-
Relevant File Content:
|
|
1116
|
-
----------------------
|
|
1117
|
-
.../apt_package/recipes/install.rb:
|
|
1118
|
-
|
|
1119
|
-
1>> raise RuntimeError
|
|
1120
|
-
2:
|
|
1121
|
-
3: apt_package 'default_action'
|
|
1122
|
-
```
|
|
1123
|
-
|
|
1124
|
-
This output is automatically silenced when using RSpec's `raise_error` matcher:
|
|
1125
|
-
|
|
1126
|
-
```ruby
|
|
1127
|
-
let(:chef_run) { ChefSpec::SoloRunner.converge('cookbook::recipe') }
|
|
1128
|
-
|
|
1129
|
-
it 'raises an error' do
|
|
1130
|
-
expect {
|
|
1131
|
-
chef_run
|
|
1132
|
-
}.to raise_error
|
|
1133
|
-
end
|
|
1134
|
-
```
|
|
1135
|
-
|
|
1136
|
-
You can also assert that a particular error was raised. If the error matches the given type, the output is suppressed. If not, the test fails and the entire stack trace is presented.
|
|
1137
|
-
|
|
1138
|
-
```ruby
|
|
1139
|
-
let(:chef_run) { ChefSpec::SoloRunner.converge('cookbook::recipe') }
|
|
1140
|
-
|
|
1141
|
-
it 'raises an error' do
|
|
1142
|
-
expect {
|
|
1143
|
-
chef_run
|
|
1144
|
-
}.to raise_error(RuntimeError)
|
|
1145
|
-
end
|
|
1146
|
-
```
|
|
1147
|
-
|
|
1148
|
-
## Testing Multiple Recipes
|
|
1149
|
-
|
|
1150
|
-
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":
|
|
1151
|
-
|
|
1152
|
-
```ruby
|
|
1153
|
-
# cookbooks/sandwich/recipes/bacon.rb
|
|
1154
|
-
package 'bacon'
|
|
1155
|
-
|
|
1156
|
-
# cookbooks/sandwich/recipes/lettuce.rb
|
|
1157
|
-
package 'lettuce'
|
|
1158
|
-
|
|
1159
|
-
# cookbooks/sandwich/recipes/tomato.rb
|
|
1160
|
-
package 'tomato'
|
|
1161
|
-
```
|
|
1162
|
-
|
|
1163
|
-
```ruby
|
|
1164
|
-
let(:chef_run) { ChefSpec::SoloRunner.converge('sandwich::bacon', 'sandwich::lettuce', 'sandwich::tomato') }
|
|
1165
|
-
```
|
|
1166
|
-
|
|
1167
|
-
```ruby
|
|
1168
|
-
expect(chef_run).to install_package('bacon')
|
|
1169
|
-
expect(chef_run).to install_package('lettuce')
|
|
1170
|
-
expect(chef_run).to install_package('tomato')
|
|
1171
|
-
```
|
|
1172
|
-
|
|
1173
|
-
## Testing Roles
|
|
1174
|
-
|
|
1175
|
-
Roles can also be used in a single `ChefSpec::SoloRunner` instance. Given a cookbook "bacon" with a default recipe:
|
|
1176
|
-
|
|
1177
|
-
```ruby
|
|
1178
|
-
# cookbooks/bacon/recipes/default.rb
|
|
1179
|
-
package 'foo'
|
|
1180
|
-
```
|
|
1181
|
-
|
|
1182
|
-
and a default attributes file:
|
|
1183
|
-
|
|
1184
|
-
```ruby
|
|
1185
|
-
# cookbooks/bacon/attributes/default.rb
|
|
1186
|
-
default['bacon']['temperature'] = 200
|
|
1187
|
-
```
|
|
1188
|
-
|
|
1189
|
-
and a role "breakfast":
|
|
1190
|
-
|
|
1191
|
-
```ruby
|
|
1192
|
-
# roles/breakfast.rb
|
|
1193
|
-
default_attributes(
|
|
1194
|
-
'bacon' => {
|
|
1195
|
-
'temperature' => 150 # NOTE: This is different from the default value
|
|
1196
|
-
}
|
|
1197
|
-
)
|
|
1198
|
-
run_list([
|
|
1199
|
-
'recipe[bacon::default]'
|
|
1200
|
-
])
|
|
1201
|
-
```
|
|
1202
|
-
|
|
1203
|
-
You can test that the role is appropriately applied by telling the `ChefSpec::SoloRunner` to converge on the _role_ instead of a recipe:
|
|
1204
|
-
|
|
1205
|
-
```ruby
|
|
1206
|
-
let(:chef_run) { ChefSpec::SoloRunner.converge('role[breakfast]') }
|
|
1207
|
-
```
|
|
1208
|
-
|
|
1209
|
-
Assert that the run_list is properly expanded:
|
|
1210
|
-
|
|
1211
|
-
```ruby
|
|
1212
|
-
expect(chef_run).to include_recipe('bacon::default')
|
|
1213
|
-
```
|
|
1214
|
-
|
|
1215
|
-
Assert that the correct attribute is used:
|
|
1216
|
-
|
|
1217
|
-
```ruby
|
|
1218
|
-
expect(chef_run.node['bacon']['temperature']).to eq(150)
|
|
1219
|
-
```
|
|
1220
|
-
|
|
1221
|
-
**NOTE** If your roles live somewhere outside of the expected path, you must set `RSpec.config.role_path` to point to the directory containing your roles **before** invoking the `#converge` method!
|
|
1222
|
-
|
|
1223
|
-
```ruby
|
|
1224
|
-
RSpec.configure do |config|
|
|
1225
|
-
config.role_path = '/var/my/roles' # global setting
|
|
1226
|
-
end
|
|
1227
|
-
|
|
1228
|
-
# - OR -
|
|
1229
|
-
|
|
1230
|
-
ChefSpec::SoloRunner.new(role_path: '/var/my/roles') # local setting
|
|
1231
|
-
```
|
|
1232
|
-
|
|
1233
|
-
## Faster Specs
|
|
1234
|
-
|
|
1235
|
-
ChefSpec aims to provide the easiest and simplest path for new users to write RSpec examples for Chef cookbooks. In doing so, it makes some sacrifices in terms of speed and agility of execution. In other words, ChefSpec favors "speed to develop" over "speed to execute". Many of these decisions are directly related to the way Chef dynamically loads resources at runtime.
|
|
1236
|
-
|
|
1237
|
-
If you understand how RSpec works and would like to see some significant speed improvements in your specs, you can use the `ChefSpec::Cacher` module inspired by [Juri Timošin](https://github.com/DracoAter). Simply convert all your `let` blocks to `cached`:
|
|
1238
|
-
|
|
1239
|
-
```ruby
|
|
1240
|
-
# before
|
|
1241
|
-
let(:chef_run) { ChefSpec::SoloRunner.new }
|
|
1242
|
-
|
|
1243
|
-
# after
|
|
1244
|
-
cached(:chef_run) { ChefSpec::SoloRunner.new }
|
|
1245
|
-
```
|
|
1246
|
-
|
|
1247
|
-
Everything else should work the same. Be advised, as the method name suggests, this will cache the results of your Chef Client Run for the **entire RSpec example**. This makes stubbing more of a challenge, since the node is already converged. For more information, please see [Juri Timošin's blog post on faster specs](http://dracoater.blogspot.com/2013/12/testing-chef-cookbooks-part-25-speeding.html) as well as the discussion in [#275](https://github.com/chefspec/chefspec/issues/275).
|
|
1248
|
-
|
|
1249
|
-
## Media & Third-party Tutorials
|
|
1250
|
-
|
|
1251
|
-
- [CustomInk's Testing Chef Cookbooks](http://technology.customink.com/blog/2012/08/03/testing-chef-cookbooks/)
|
|
1252
|
-
- [Jake Vanderdray's Practical ChefSpec](http://files.meetup.com/1780846/ChefSpec.pdf)
|
|
1253
|
-
- [Jim Hopp's excellent Test Driven Development for Chef Practitioners](http://www.youtube.com/watch?v=o2e0aZUAVGw)
|
|
1254
|
-
- [Joshua Timberman's Starting ChefSpec Examples](http://jtimberman.housepub.org/blog/2013/05/09/starting-chefspec-example/)
|
|
1255
|
-
- [Juri Timošin's post on faster specs](http://dracoater.blogspot.com/2013/12/testing-chef-cookbooks-part-25-speeding.html)
|
|
1256
|
-
- [Seth Vargo's Chef recipe code coverage](https://sethvargo.com/chef-recipe-code-coverage/)
|
|
1257
|
-
- [Seth Vargo's TDDing tmux talk](http://www.confreaks.com/videos/2364-mwrc2013-tdding-tmux)
|
|
1258
|
-
- [Stephen Nelson Smith's Test-Driven Infrastructure with Chef](http://shop.oreilly.com/product/0636920030973.do)
|
|
1259
|
-
|
|
1260
|
-
## Development
|
|
1261
|
-
|
|
1262
|
-
1. Fork the repository from GitHub.
|
|
1263
|
-
2. Clone your fork to your local machine:
|
|
1264
|
-
|
|
1265
|
-
```
|
|
1266
|
-
$ git clone git@github.com:USER/chefspec.git
|
|
1267
|
-
```
|
|
1268
|
-
|
|
1269
|
-
3. Create a git branch
|
|
1270
|
-
|
|
1271
|
-
```
|
|
1272
|
-
$ git checkout -b my_bug_fix
|
|
1273
|
-
```
|
|
1274
|
-
|
|
1275
|
-
4. **Write tests**
|
|
1276
|
-
|
|
1277
|
-
5. Make your changes/patches/fixes, committing appropriately
|
|
1278
|
-
|
|
1279
|
-
6. Run the tests: `bundle exec rake`
|
|
1280
|
-
|
|
1281
|
-
7. Push your changes to GitHub
|
|
1282
|
-
|
|
1283
|
-
8. Open a Pull Request
|
|
1284
|
-
|
|
1285
|
-
ChefSpec is on [Travis CI][travis] which tests against multiple Chef and Ruby versions.
|
|
1286
|
-
|
|
1287
|
-
If you are contributing, please see the [Contributing Guidelines](https://github.com/chefspec/chefspec/blob/master/CONTRIBUTING.md) for more information.
|
|
1288
|
-
|
|
1289
|
-
## License
|
|
1290
|
-
|
|
1291
|
-
MIT - see the accompanying [LICENSE](https://github.com/chefspec/chefspec/blob/master/LICENSE) file for details.
|