-
Summary
When I try to perform a `hiera.lookup` request in a spec, the hierarchy doesn't find the `kernel` fact. Because of that, Hiera shows this message:
Ignoring bad definition in :hierarchy: 'kernel/'
And as a result, the data I need is not found.
-
-
spec/spec_helper.rb
``` require "puppetlabs_spec_helper/module_spec_helper" require "rspec-puppet-facts" include RspecPuppetFacts
-
needed to perform lookups inside specs
require "hiera"
RSpec.configure do |c| c.default_facts = { "hostname" => `hostname -f`.strip, "ipaddress" => `hostname -i`.strip, "puppetversion" => "4.6.1", }
c.fail_fast = true if ENV['FAIL_FAST'] == "true"
-
needed to evaluate hiera lookups in the puppet code
c.hiera_config = "spec/fixtures/hiera/hiera.yaml"
if ENV['COVERAGE'] == "true" c.after(:suite) do RSpec::Puppet::Coverage.report! end end end
-
Default to "no" as our specs fail on default facts (i.e. "$::networking")
ENV['STRICT_VARIABLES'] = "no" unless ENV['STRICT_VARIABLES'] ```
-
-
spec/fixtures/hiera/hiera.yaml
``` :backends:
-
yaml
:yaml: :datadir: spec/fixtures/hiera :hierarchy:
-
"fqdn/% {fqdn}
"
-
"environment/% {environment}
"
-
"hostgroup/% {hostgroup}
"
-
"os/% {operatingsystem}/%{operatingsystemrelease}"
- "os/%{operatingsystem}
"
-
"kernel/% {kernel}
"
-
"common"
```
-
-
spec/fixtures/hiera/os/Ubuntu.yaml
``` cron_service: 'cron' ```
-
-
spec/fixtures/hiera/kernel/Linux.yaml
``` cron_service: 'crond' ```
-
-
manifests/linux.pp
``` class ta_base::linux() { $cron_service = hiera('cron_service')
service { $cron_service: enable => true, ensure => true, hasrestart => true, hasstatus => true, }
} ```
-
-
spec/classes/linux_spec.rb
``` require "spec_helper"
hiera = Hiera.new(:config => "spec/fixtures/hiera/hiera.yaml")
describe "ta_base::linux" do on_supported_os.each do |os, facts| context "on # {os}
" do let(:facts) { facts }
cron_service = hiera.lookup("cron_service", nil, facts)
it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_service(cron_service) .with( "enable" => true, "ensure" => true ) }
end end end ```
-
-
Example run
``` $ SPEC=spec/classes/linux_spec.rb FAIL_FAST=true rake spec_standalone /home/dunk/.rvm/rubies/ruby-2.3.1/bin/ruby -I/home/dunk/.rvm/gems/ruby-2.3.1@puppet-testing/gems/rspec-core-3.6.0/lib:/home/dunk/.rvm/gems/ruby-2.3.1@puppet-testing/gems/rspec-support-3.6.0/lib/home/dunk/.rvm/gems/ruby-2.3.1@puppet-testing/gems/rspec-core-3.6.0/exe/rspec spec/classes/linux_spec.rb --color DEBUG: 2017-08-23 17:38:05 -0600: Hiera YAML backend starting DEBUG: 2017-08-23 17:38:05 -0600: Looking up cron_service in YAML backend DEBUG: 2017-08-23 17:38:05 -0600: Ignoring bad definition in :hierarchy: 'fqdn/' DEBUG: 2017-08-23 17:38:05 -0600: Ignoring bad definition in :hierarchy: 'environment/' DEBUG: 2017-08-23 17:38:05 -0600: Ignoring bad definition in :hierarchy: 'hostgroup/' DEBUG: 2017-08-23 17:38:05 -0600: Ignoring bad definition in :hierarchy: 'os//' DEBUG: 2017-08-23 17:38:05 -0600: Ignoring bad definition in :hierarchy: 'os/' DEBUG: 2017-08-23 17:38:05 -0600: Ignoring bad definition in :hierarchy: 'kernel/' DEBUG: 2017-08-23 17:38:05 -0600: Looking for data source common DEBUG: 2017-08-23 17:38:05 -0600: Looking up cron_service in YAML backend DEBUG: 2017-08-23 17:38:05 -0600: Ignoring bad definition in :hierarchy: 'fqdn/' DEBUG: 2017-08-23 17:38:05 -0600: Ignoring bad definition in :hierarchy: 'environment/' DEBUG: 2017-08-23 17:38:05 -0600: Ignoring bad definition in :hierarchy: 'hostgroup/' DEBUG: 2017-08-23 17:38:05 -0600: Ignoring bad definition in :hierarchy: 'os//' DEBUG: 2017-08-23 17:38:05 -0600: Ignoring bad definition in :hierarchy: 'os/' DEBUG: 2017-08-23 17:38:05 -0600: Ignoring bad definition in :hierarchy: 'kernel/' DEBUG: 2017-08-23 17:38:05 -0600: Looking for data source common DEBUG: 2017-08-23 17:38:05 -0600: Looking up cron_service in YAML backend DEBUG: 2017-08-23 17:38:05 -0600: Ignoring bad definition in :hierarchy: 'fqdn/' DEBUG: 2017-08-23 17:38:05 -0600: Ignoring bad definition in :hierarchy: 'environment/' DEBUG: 2017-08-23 17:38:05 -0600: Ignoring bad definition in :hierarchy: 'hostgroup/' DEBUG: 2017-08-23 17:38:05 -0600: Ignoring bad definition in :hierarchy: 'os//' DEBUG: 2017-08-23 17:38:05 -0600: Ignoring bad definition in :hierarchy: 'os/' DEBUG: 2017-08-23 17:38:05 -0600: Ignoring bad definition in :hierarchy: 'kernel/' DEBUG: 2017-08-23 17:38:05 -0600: Looking for data source common
ta_base::linux on centos-6-x86_64 should compile into a catalogue without dependency cycles should contain Service[] with enable => true and ensure => true (FAILED - 1)
Failures:
1) ta_base::linux on centos-6-x86_64 should contain Service[] with enable => true and ensure => true Failure/Error: it { is_expected.to contain_service(cron_service) .with( "enable" => true, "ensure" => true ) }
ArgumentError: No title provided and "Service" is not a valid resource reference
-
/home/dunk/.rvm/gems/ruby-2.3.1@puppet-testing/gems/puppet-4.7.0/lib/puppet/resource.rb:572:in `extract_type_and_title'
-
/home/dunk/.rvm/gems/ruby-2.3.1@puppet-testing/gems/puppet-4.7.0/lib/puppet/resource.rb:557:in `type_and_title'
-
/home/dunk/.rvm/gems/ruby-2.3.1@puppet-testing/gems/puppet-4.7.0/lib/puppet/resource/catalog.rb:355:in `resource'
-
/home/dunk/.rvm/gems/ruby-2.3.1@puppet-testing/gems/rspec-puppet-2.6.8/lib/rspec-puppet/matchers/create_generic.rb:85:in `matches?'
-
./spec/classes/linux_spec.rb:17:in `block (4 levels) in <top (required)>'
Finished in 3.93 seconds (files took 4.26 seconds to load) 2 examples, 1 failure
Failed examples:
rspec ./spec/classes/linux_spec.rb[1:1:2] # ta_base::linux on centos-6-x86_64 should contain Service[] with enable => true and ensure => true
/home/dunk/.rvm/rubies/ruby-2.3.1/bin/ruby -I/home/dunk/.rvm/gems/ruby-2.3.1@puppet-testing/gems/rspec-core-3.6.0/lib:/home/dunk/.rvm/gems/ruby-2.3.1@puppet-testing/gems/rspec-support-3.6.0/lib/home/dunk/.rvm/gems/ruby-2.3.1@puppet-testing/gems/rspec-core-3.6.0/exe/rspec spec/classes/linux_spec.rb --color failed ```
From what I can see, this comes down to the `facts` hash having `kernel` as a `Symbol` instead of a `String`, because it properly loads `spec/fixtures/hiera/kernel/Linux.yaml` when I do this:
```
-
manifest
facts['kernel'] = "Linux" cron_service = hiera.lookup("cron_service", nil, facts)
-
log output from running test
DEBUG: 2017-08-23 17:39:14 -0600: Looking up cron_service in YAML backend DEBUG: 2017-08-23 17:39:14 -0600: Ignoring bad definition in :hierarchy: 'fqdn/' DEBUG: 2017-08-23 17:39:14 -0600: Ignoring bad definition in :hierarchy: 'environment/' DEBUG: 2017-08-23 17:39:14 -0600: Ignoring bad definition in :hierarchy: 'hostgroup/' DEBUG: 2017-08-23 17:39:14 -0600: Ignoring bad definition in :hierarchy: 'os//' DEBUG: 2017-08-23 17:39:14 -0600: Ignoring bad definition in :hierarchy: 'os/' DEBUG: 2017-08-23 17:39:14 -0600: Looking for data source kernel/Linux DEBUG: 2017-08-23 17:39:14 -0600: Found cron_service in kernel/Linux ```
-
The last 2 lines being the key difference:
Looking for data source kernel/Linux Found cron_service in kernel/Linux
So....
|