-
Notifications
You must be signed in to change notification settings - Fork 202
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
Using hiera data model in Puppet5 modules #551
Comments
Thanks for the report! I haven't looked into the details of how the hiera data in modules is implemented in Puppet 5, so I'm not sure if I need to make any changes yet to support it. Much ❤️ for providing an example module, I'll do a bit of experimentation tonight and let you know :) |
Well, that's fun. So, rspec-puppet is using the in module hiera data just fine. But accessing that data from in your spec is not so easy. The Hiera library can not handle the new config file (bails out if the version >= 4), so it's not as easy as using In the meantime, you should be able to get going by specifying the expected values diff --git a/spec/classes/puppet_spec.rb b/spec/classes/puppet_spec.rb
index c5b9322..0dee158 100644
--- a/spec/classes/puppet_spec.rb
+++ b/spec/classes/puppet_spec.rb
@@ -1,25 +1,36 @@
require 'spec_helper'
describe 'puppet5' do
+ package_details = {
+ 'redhat-6-x86_64' => {
+ :ensure => '5.0.0-1.el6',
+ },
+ 'redhat-7-x86_64' => {
+ :ensure => '5.0.0-1.el7',
+ },
+ 'centos-6-x86_64' => {
+ :ensure => '5.0.0-1.el6',
+ },
+ 'centos-7-x86_64' => {
+ :ensure => '5.0.0-1.el7',
+ },
+ }
+
on_supported_os.each do |os, facts|
context "on #{os}" do
let(:facts) do
facts
end
- # Initialise hiera
- let(:hiera_config) { 'hiera.yaml' }
- hiera = Hiera.new(:config => 'hiera.yaml')
it { is_expected.to compile.with_all_deps }
it { should contain_class('puppet5::oscheck') }
context "with no paramters" do
it { should contain_package('puppet-agent').with(
- 'ensure' => hiera.lookup('ensure_package', nil, nil),
- 'name' => hirea.lookup('package', nil, nil)
+ 'ensure' => package_details[os][:ensure],
) }
end
end
end
-end
\ No newline at end of file
+end |
Oh, I think I have a workaround. If I create 'spec/hiera.yaml' and specify version 4 and call it a filthy kludge... ...instead of the proper file it might stop the Hiera library from dying. |
… config sorts it out. rodjek/rspec-puppet#551
I'll use your recommended solution as my tests with older I've branched my module for testing hiera lookups with rspec_puppet Using a Hiera 3.3. config file in 1) puppet5 on centos-6-x86_64 with no paramters
Failure/Error: 'ensure' => hiera.lookup('ensure_package', nil, nil),
NoMethodError:
undefined method `lookup' for #<Hiera:0x0000000315dff8> |
Similar hiera5 error when loaded in spec tests:
Odd it says v4 even though it is v5. |
I've found that due to the nature of hiera layer 3 data sources being the end-all-be-all of data sources, that any Puppet 5 modules actually don't require a hiera fixture at all, which is quite nice, but this has caused problems when the actual data sources are exectable in a test environment (for example, eyaml data sources voxpupuli/hiera-eyaml#253) |
I am having trouble with this also, so posting here partly to get notifications, and partly to raise another issue. When doing away with params.pp, we are encouraged not to include the top level class of each module. The expectation (as I understand it) is that we pull in data from a top level module class in to the more specific classes by using global scoped variables, i.e.:
When writing a spec for module::specific, I have seem to specify module::data in node_params, which is quite tedious, as I have to do this for every class other than the top level class. Have I done something wrong here, or, is this intended, or in need of a fix of some sort? |
I'd love to avoid moving all my hiera data into my specs. Is that still the only workaround? |
You shouldn't have to move all the data into specs, it works just fine if you have the hiera.yaml file defined in the module root. You just have to test the module in isolation, instead if treating it as if you are providing it data from layer 1 or layer 2. (All data for the module must be in the module). This should not be confused with facts. In puppet 5 facts and hiera data are not the same thing. |
@LongLiveCHIEF I should have specified that in order for me to verify certain values, I must move them from hiera to the spec (akin to what was specified here: #551 (comment)). Otherwise, when I put this in my spec:
I get this error:
I've been wondering if testing specific values isn't effective (code smell? really...what's the point?), so it's not really a loss.
Thoughts? |
What's going on here, (what puppet tries to tell you but does a bad job of), is that you're calling a lookup for a piece of data outside the scope of the module, instead of a piece of data within the module. You're also using a lookup function that is deprecated. Hiera 5 is part of Puppet 5. They're no longer separate things, so you don't need to do a hiera lookup for the data in the module, nor do you need to define and load hiera in your specs. It's automatically loaded by Puppet. So:
Some examples of modules I maintain that show you how this works: |
If you want more help, join the Slack Puppet Community and check out the |
Hi @LongLiveCHIEF, Ironically I was googling this again having forgotten I'd been in this discussion, and found my comment - and though "That's exactly my problem" and realised then that it was my own words. Anyway, I've had a look at the examples you gave, and I see where I'm going wrong. I'm writing a spec file for each class (which is how PDK generates them, unless I'm using that incorrectly), rather than for the top level class which includes and/or contains the various subclasses. In your case, you only write specs for the top level class. That helps a lot, and I've got a bunch of internal refactoring to do.. but, for much the better I think. How would you test a case where there are 2 classes which can be included, one is compulsary, one optional, i.e.:
Where the some service::monitoring class requires someservice to exist and pulls parameters from it, rather than being passed them directly? Is that a reasonable case to use something like:
I could of course have a param on someservice which causes the monitoring subclass to be included, however, I'm concerned about a huge number of parameters that I'm adding to my top level class. |
Ack, just saw your next message.. joining that now. |
To satisfy Hiera lookups (as opposed to purely 'fact' based references) RSpec tests now have their own respective Hiera hierarchy defined at: 'site/profiles/fixtures/hiera.yaml'. All existing (and future) RSpec tests will now perform "Automatic Parameter Lookups" only falling back to in-class default values should the corresponding key not be found. The 'datadir' path to the RSpec test directory is relative to the 'Profiles' module's 'root' directory: 'site/profiles'. As of this commit the "data-in-module" pattern is not currently utilised and consequently dedicated RSpec specific Hiera data files are consumed. The 'ghoneycutt-ssh' community module utilises a deprecated Hiera function, 'hiera_hash', for performing a "deep" merge of an explicit key: 'ssh::keys'. This is addressed in the same manner as: c3a17e3e896ad24599d6f9274cc8f37a45844e41. Unfortunately Hiera v3 had to be adopted as the Ruby gem 'rspec-puppet' utilised by `pdk` is unable to parse Hiera v5 configuration file syntax. Related links regarding Hiera v3 & puppet-rspec limitations: * rodjek/rspec-puppet#762 * rodjek/rspec-puppet#551 * https://tickets.puppetlabs.com/browse/HI-598
To satisfy Hiera lookups (as opposed to purely 'fact' based references) RSpec tests now have their own respective Hiera hierarchy defined at: 'site/profiles/fixtures/hiera.yaml'. All existing (and future) RSpec tests will now perform "Automatic Parameter Lookups" only falling back to in-class default values should the corresponding key not be found. The 'datadir' path to the RSpec test directory is relative to the 'Profiles' module's 'root' directory: 'site/profiles'. As of this commit the "data-in-module" pattern is not currently utilised and consequently dedicated RSpec specific Hiera data files are consumed. The 'ghoneycutt-ssh' community module utilises a deprecated Hiera function, 'hiera_hash', for performing a "deep" merge of an explicit key: 'ssh::keys'. This is addressed in the same manner as: c3a17e3e896ad24599d6f9274cc8f37a45844e41. Unfortunately Hiera v3 had to be adopted as the Ruby gem 'rspec-puppet' utilised by `pdk` is unable to parse Hiera v5 configuration file syntax. Related links regarding Hiera v3 & puppet-rspec limitations: * rodjek/rspec-puppet#762 * rodjek/rspec-puppet#551 * https://tickets.puppetlabs.com/browse/HI-598
By design the SSH profile class, 'profiles::ssh', configures the OpenSSH server to accept public key authentication as the *only* mechanism for authentication. This constraint requires the user to provide either (or both) public key(s) or explicitly enable traditional password authentication. To satisfy Hiera lookups (as opposed to purely 'fact' based references) RSpec tests now have their own respective Hiera hierarchy defined at: 'site/profiles/fixtures/hiera.yaml'. All existing (and future) RSpec tests will now perform "Automatic Parameter Lookups" only falling back to in-class default values should the corresponding key not be found. The 'datadir' path to the RSpec test directory is relative to the 'Profiles' module's 'root' directory: 'site/profiles'. As of this commit the "data-in-module" pattern is not currently utilised for class default values and consequently dedicated RSpec specific Hiera data files are consumed. Unfortunately Hiera v3 had to be adopted as the Ruby gem 'rspec-puppet' utilised by `pdk` is unable to parse Hiera v5 configuration file syntax. Related links regarding Hiera v3 & puppet-rspec limitations: * rodjek/rspec-puppet#762 * rodjek/rspec-puppet#551 * https://tickets.puppetlabs.com/browse/HI-598
By design the SSH profile class, 'profiles::ssh', configures the OpenSSH server to accept public key authentication as the *only* mechanism for authentication. This constraint requires the user to provide either (or both) public key(s) or explicitly enable traditional password authentication. To satisfy Hiera lookups (as opposed to purely 'fact' based references) RSpec tests now have their own respective Hiera hierarchy defined at: 'site/profiles/fixtures/hiera.yaml'. All existing (and future) RSpec tests will now perform "Automatic Parameter Lookups" only falling back to in-class default values should the corresponding key not be found. The 'datadir' path to the RSpec test directory is relative to the 'Profiles' module's 'root' directory: 'site/profiles'. As of this commit the "data-in-module" pattern is not currently utilised for class default values and consequently dedicated RSpec specific Hiera data files are consumed. Unfortunately Hiera v3 had to be adopted as the Ruby gem 'rspec-puppet' utilised by `pdk` is unable to parse Hiera v5 configuration file syntax. Related links regarding Hiera v3 & puppet-rspec limitations: * rodjek/rspec-puppet#762 * rodjek/rspec-puppet#551 * https://tickets.puppetlabs.com/browse/HI-598
For anyone hitting this in 2024, following this article worked for me. I didn't have to change the spec helper, the only steps were to:
|
@m0dular just to clarify Puppet forked this repo and it is maintained here, im not sure if the approach to hiera documented has everything you need https://github.com/puppetlabs/rspec-puppet/?tab=readme-ov-file#hiera-integration |
Puppet5 is doing away with
params.pp
. Yay!This has been replaced with a
hiera.yaml
file in the module's root directory and adata
directory containing the Hiera datastore.How do you tell rspec-puppet to use this Hiera datastore?
I've created a new Puppet5 module here where I've tried to:
spec_helper.rb
(error log)I suspect if I got the path correct it ought to just work, in which case we just need the documentation updated for Puppet5.
The text was updated successfully, but these errors were encountered: