Skip to content

Commit

Permalink
Add support for custom data bags path
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Plantier <plantier@esker.fr>
  • Loading branch information
Plantier committed Apr 2, 2019
1 parent 59241a1 commit 303ae4e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ The recipe that the example tests:
The helper will call `ChefVault::Item.load`, which will be stubbed using
the data bag from the test/integration/data_bags directory.

To set a custom folder from where to load the data bags, you can
use the :custom_data_bags_path parameter when calling rspec_shared_context:

RSpec.describe 'my_cookbook::default' do
include ChefVault::TestFixtures.rspec_shared_context(custom_data_bags_path: "path/to/data_bags")

## VAULT PROBING

Some recipes and helpers attempt to determine if a data bag is a vault
Expand Down
17 changes: 13 additions & 4 deletions lib/chef-vault/test_fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,35 @@ class << self
# created a shared RSpec context that stubs calls to ChefVault::Item.load
# @param stub_encrypted_data [Boolean] whether to also stub calls to
# Chef::DataBagItem.load
# @param custom_data_bags_path [String] path to a custom data bags folder
# @return [Module] a shared context to include in your example groups
def rspec_shared_context(stub_encrypted_data = false)
def rspec_shared_context(stub_encrypted_data = false,
custom_data_bags_path: nil)
@context ||= begin
Module.new do
extend RSpec::Core::SharedContext

before { find_vaults(stub_encrypted_data) }
before do
unless custom_data_bags_path.nil?
custom_data_bags_path = Pathname.new(custom_data_bags_path)
end
find_vaults(stub_encrypted_data, custom_data_bags_path)
end

private

# finds all the directories in test/integration/data_bags, stubbing
# each as a vault
# @param stub_encrypted_data [Boolean] whether to also stub calls to
# Chef::DataBagItem.load
# @param custom_data_bags_path [Pathname] path to a custom data bags
# folder
# return [void]
# @api private
def find_vaults(stub_encrypted_data)
def find_vaults(stub_encrypted_data, custom_data_bags_path)
dbdir = Pathname.new("test") + "integration" + "data_bags"
smokedir = Pathname.new("test") + "smoke" + "default" + "data_bags"
[ dbdir, smokedir ].each do |dir|
[ dbdir, smokedir, custom_data_bags_path ].compact.each do |dir|
next unless dir.directory?
dir.each_child do |vault|
next unless vault.directory?
Expand Down
2 changes: 1 addition & 1 deletion lib/chef-vault/test_fixtures_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ class ChefVault
# dynamic RSpec contexts for cookbooks that use chef-vault
class TestFixtures
# the version of the gem
VERSION = "3.0.1"
VERSION = "3.1.0"
end
end
14 changes: 14 additions & 0 deletions spec/lib/chef-vault/test_fixtures_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,17 @@
end
end
end

RSpec.describe ChefVault::TestFixtures do
describe "with a custom data bags path" do
ChefVault::TestFixtures.clear_context
include ChefVault::TestFixtures.rspec_shared_context(
custom_data_bags_path: 'test/custom/data_bags'
)

it "should load data bags from a custom path" do
bar = ChefVault::Item.load("foobar", "foo")["bar"]
expect(bar).to eq(42)
end
end
end
3 changes: 3 additions & 0 deletions test/custom/data_bags/foobar/foo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"bar": 42
}

0 comments on commit 303ae4e

Please sign in to comment.