chef / chef-vault-testfixtures

provides an RSpec shared context for testing Chef cookbooks that use chef-vault
Apache License 2.0
7 stars 10 forks source link

Undefined Method chef_vault_item #27

Closed MichaelStankiewicz closed 6 years ago

MichaelStankiewicz commented 6 years ago

I can't seem to get this thing working properly, so I must be missing something. I've followed the instructions on the readme file with no luck. Below is my configuration. When I execute the rspec test chef exec rspec, I'm getting the undefined method error as reported in the subject of this issue.

spec_helper.rb

require 'chefspec'
require 'chefspec/berkshelf'
require 'pry'
require 'chef-vault'
require 'chef-vault/test_fixtures'

JSON Files: cookbook/test/integration/data_bags/credentials/foo.json

Contents:

{
    "username": "user",
    "password": "password"
}

recipe.rb

# Initialize cookbook dependencies
include_recipe 'chef-vault'

# Initialize Chef Vault data
vault_cred = chef_vault_item(:credentials, 'foo')

dsc_resource '[File]Copy_RepositoryManager_Provider' do
  resource :File
  property :Ensure, 'Present'
  property :DestinationPath, 'c:\\somepath'
  property :SourcePath, 'c:\\somepath'
  property :Type, 'Directory'
  property :Checksum, 'modifiedDate'
  property :MatchSource, true
  property :Recurse, true
  property :PsDscRunAsCredential, ps_credential(vault_cred['username'], vault_cred['password'])  
end

default_spec.rb

require 'spec_helper'

describe 'cookbook::default' do  
  include ChefVault::TestFixtures.rspec_shared_context
  context 'On Windows 2012 R2' do    
    let(:chef_run) do
      ChefSpec::SoloRunner.new(platform: 'windows', version: '2012R2').converge(described_recipe)
    end

    vault_cred = chef_vault_item(:credentials, 'foo')

    it 'should converge' do
      expect(chef_run).to include_recipe(described_recipe)
    end

    it 'runs dsc_resource with the File resource' do
      expect(chef_run).to run_dsc_resource('[File]Copy_RepositoryManager_Provider').with(
        resource: :File,
          properties: {
            Ensure: 'Present',
            DestinationPath: 'c:\\somepath',
            SourcePath: 'c:\\somepath',
            Type: 'Directory',
            Checksum: 'modifiedDate',
            MatchSource: true,
            Recurse: true,
            PsDscRunAsCredential: ps_credential(vault_cred['username'], vault_cred['password']),
          }
      )
    end
  end
end
MichaelStankiewicz commented 6 years ago

I got this working now. It turns out that my mistake was to include the chef_vault_item method inside my spec file. I believe I misunderstood the principal workings of this solution and at the same time misread the implementation details. This issue can be closed.

Sorry for the false alarm. :)