Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ChefSpec for registry_data_exists guard #1021

Open
MAN98 opened this issue Mar 17, 2022 · 2 comments
Open

ChefSpec for registry_data_exists guard #1021

MAN98 opened this issue Mar 17, 2022 · 2 comments

Comments

@MAN98
Copy link

MAN98 commented Mar 17, 2022

Hi - How do you write the ChefSpec test when you use the registry_data_exists method as a guard? I don't see any example on how to test or stub that method. When I run ChefSpec it always failing with the following error message:
Chef::Exceptions::Win32RegKeyMissing:
Registry key HKLM\SOFTWARE\Regkey does not exist

Here's how I use that method:
registry_key 'Set Data value to Auto' do
key 'HKLM\SOFTWARE\RegKey'
values [{ name: 'MaintenanceGroup', type: :string, data: 'Auto' }]
recursive true
action :create
only_if { registry_data_exists?('HKLM\SOFTWARE\RegKey', { name: 'MaintenanceGroup', type: :string, data: '' }, :machine) }
end

@MAN98
Copy link
Author

MAN98 commented Mar 29, 2022

I am looking at something that was suggested here: #514. Do I simply replace registry_value_exists? with registry_data_exists?

@MAN98
Copy link
Author

MAN98 commented Mar 29, 2022

I got passed the error message:
Chef::Exceptions::Win32RegKeyMissing:
Registry key HKLM\SOFTWARE\Regkey does not exist

by adding the registry_key_exists? guard in the registry_key resource and mock it in the corresponding ChefSpec test:

in my recipe:

registry_key 'Set Data value to Auto' do
key 'HKLM\SOFTWARE\RegKey'
values [{ name: 'MaintenanceGroup', type: :string, data: 'Auto' }]
recursive true
action :create
only_if { registry_key_exists?('HKLM\SOFTWARE\RegKey', :machine) }
only_if { registry_data_exists?('HKLM\SOFTWARE\RegKey', { name: 'MaintenanceGroup', type: :string, data: '' }, :machine) }
end

in my spec file:

before(:all) do
      RSpec::Mocks.with_temporary_scope do
        allow_any_instance_of(Chef::Resource)
          .to receive(:registry_key_exists?)
          .with('HKLM\SOFTWARE\RegKey', :machine)
          .and_return(true)

        allow_any_instance_of(Chef::Provider)
          .to receive(:registry_key_exists?)
          .with('HKLM\SOFTWARE\RegKey', :machine)
          .and_return(true)
      end
    end

But I am not sure if that's really how it should be done or if I still need to mock the registry_data_exists? guard. Still looking for some guidance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant