Skip to content

Commit

Permalink
(PUP-1952) Fix hiera and lookup specs
Browse files Browse the repository at this point in the history
Instead of trying to reload these files on every test from a different temporary
directory, which was not working due to caching in jruby, just load once from a
fixture.
  • Loading branch information
pcarlisle committed Jun 5, 2018
1 parent 30e1902 commit 3c16b21
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 104 deletions.
@@ -0,0 +1,22 @@
class Hiera::Backend::Hieraspec_backend
def initialize(cache = nil)
Hiera.debug('Custom_backend starting')
end

def lookup(key, scope, order_override, resolution_type, context)
case key
when 'datasources'
Hiera::Backend.datasources(scope, order_override) { |source| source }
when 'resolution_type'
if resolution_type == :hash
{ key => resolution_type.to_s }
elsif resolution_type == :array
[ key, resolution_type.to_s ]
else
"resolution_type=#{resolution_type}"
end
else
throw :no_such_key
end
end
end
@@ -0,0 +1,18 @@
class Hiera::Backend::Custom_backend
def lookup(key, scope, order_override, resolution_type, context)
case key
when 'hash_c'
{ 'hash_ca' => { 'cad' => 'value hash_c.hash_ca.cad (from global custom)' }}
when 'hash'
{ 'array' => [ 'x5,x6' ] }
when 'array'
[ 'x5,x6' ]
when 'datasources'
Hiera::Backend.datasources(scope, order_override) { |source| source }
when 'dotted.key'
'custom backend received request for dotted.key value'
else
throw :no_such_key
end
end
end
@@ -0,0 +1,7 @@
class Hiera::Backend::Other_backend
def lookup(key, scope, order_override, resolution_type, context)
value = Hiera::Config[:other][key.to_sym]
throw :no_such_key if value.nil?
value
end
end
62 changes: 14 additions & 48 deletions spec/unit/functions/hiera_spec.rb
Expand Up @@ -12,47 +12,14 @@
---
:backends:
- yaml
- custom
- hieraspec
:yaml:
:datadir: #{global_dir}/hieradata
:hierarchy:
- first
- second
YAML

let(:ruby_stuff_files) do
{
'hiera' => {
'backend' => {
'custom_backend.rb' => <<-RUBY.unindent
class Hiera::Backend::Custom_backend
def initialize(cache = nil)
Hiera.debug('Custom_backend starting')
end
def lookup(key, scope, order_override, resolution_type, context)
case key
when 'datasources'
Hiera::Backend.datasources(scope, order_override) { |source| source }
when 'resolution_type'
if resolution_type == :hash
{ key => resolution_type.to_s }
elsif resolution_type == :array
[ key, resolution_type.to_s ]
else
"resolution_type=\#{resolution_type}"
end
else
throw :no_such_key
end
end
end
RUBY
}
}
}
end

let(:hieradata_files) do
{
'first.yaml' => <<-YAML.unindent,
Expand Down Expand Up @@ -143,7 +110,6 @@ class mod::baz {}
let(:global_files) do
{
'hiera.yaml' => hiera_yaml,
'ruby_stuff' => ruby_stuff_files,
'hieradata' => hieradata_files,
'environments' => environment_files
}
Expand All @@ -158,23 +124,24 @@ class mod::baz {}
let(:compiler) { Puppet::Parser::Compiler.new(node) }
let(:the_func) { Puppet.lookup(:loaders).puppet_system_loader.load(:function, 'hiera') }

before(:all) do
$LOAD_PATH.unshift(File.join(my_fixture_dir))
end

after(:all) do
Hiera::Backend.send(:remove_const, :Hieraspec_backend) if Hiera::Backend.const_defined?(:Hieraspec_backend)
$LOAD_PATH.shift
end

before(:each) do
Puppet.settings[:codedir] = global_dir
Puppet.settings[:hiera_config] = File.join(global_dir, 'hiera.yaml')
end

around(:each) do |example|
# Faking the load path to enable 'require' to load from 'ruby_stuff'. It removes the need for a static fixture
# for the custom backend
dir_contained_in(global_dir, global_files)
$LOAD_PATH.unshift(File.join(global_dir, 'ruby_stuff'))
begin
Puppet.override(:environments => environments, :current_environment => env) do
example.run
end
ensure
Hiera::Backend.send(:remove_const, :Custom_backend) if Hiera::Backend.const_defined?(:Custom_backend)
$LOAD_PATH.shift
Puppet.override(:environments => environments, :current_environment => env) do
example.run
end
end

Expand Down Expand Up @@ -406,7 +373,7 @@ class testing () {
---
:backends:
- yaml
- custom
- hieraspec
:yaml:
:datadir: #{global_dir}/hieradata
:hierarchy:
Expand Down Expand Up @@ -455,8 +422,7 @@ class testing () {
- x3
- x4
YAML
},
'ruby_stuff' => ruby_stuff_files
}
}
end

Expand Down
69 changes: 13 additions & 56 deletions spec/unit/functions/lookup_spec.rb
Expand Up @@ -4,6 +4,7 @@
require 'puppet_spec/files'
require 'puppet/pops'
require 'deep_merge/core'
require 'hiera'

describe "The lookup function" do
include PuppetSpec::Compiler
Expand Down Expand Up @@ -37,8 +38,6 @@
}
end

let(:ruby_dir_files) { {} }

let(:logs) { [] }
let(:scope_additions ) { {} }
let(:notices) { logs.select { |log| log.level == :notice }.map { |log| log.message } }
Expand All @@ -57,11 +56,6 @@
code_dir
end

let(:populated_ruby_dir) do
dir_contained_in(ruby_dir, ruby_dir_files)
ruby_dir
end

let(:env_dir) do
d = File.join(populated_code_dir, 'environments')
Dir.mkdir(d)
Expand Down Expand Up @@ -1555,42 +1549,16 @@ def uri_test_func(options, context)
}
end

let(:ruby_dir_files) do
{
'hiera' => {
'backend' => {
'custom_backend.rb' => <<-RUBY.unindent,
class Hiera::Backend::Custom_backend
def lookup(key, scope, order_override, resolution_type, context)
case key
when 'hash_c'
{ 'hash_ca' => { 'cad' => 'value hash_c.hash_ca.cad (from global custom)' }}
when 'hash'
{ 'array' => [ 'x5,x6' ] }
when 'array'
[ 'x5,x6' ]
when 'datasources'
Hiera::Backend.datasources(scope, order_override) { |source| source }
when 'dotted.key'
'custom backend received request for dotted.key value'
else
throw :no_such_key
end
end
end
RUBY
'other_backend.rb' => <<-RUBY.unindent,
class Hiera::Backend::Other_backend
def lookup(key, scope, order_override, resolution_type, context)
value = Hiera::Config[:other][key.to_sym]
throw :no_such_key if value.nil?
value
end
end
RUBY
}
}
}
before(:all) do
$LOAD_PATH.unshift(my_fixture_dir)
end

after(:all) do
if Kernel.const_defined?(:Hiera) && Hiera.const_defined?(:Backend)
Hiera::Backend.send(:remove_const, :Custom_backend) if Hiera::Backend.const_defined?(:Custom_backend)
Hiera::Backend.send(:remove_const, :Other_backend) if Hiera::Backend.const_defined?(:Other_backend)
end
$LOAD_PATH.shift
end

before(:each) do
Expand All @@ -1600,19 +1568,8 @@ def lookup(key, scope, order_override, resolution_type, context)
end

around(:each) do |example|
# Faking the load path to enable 'require' to load from 'ruby_stuff'. It removes the need for a static fixture
# for the custom backend
$LOAD_PATH.unshift(populated_ruby_dir)
begin
Puppet.override(:environments => environments, :current_environment => env) do
example.run
end
ensure
if Kernel.const_defined?(:Hiera) && Hiera.const_defined?(:Backend)
Hiera::Backend.send(:remove_const, :Custom_backend) if Hiera::Backend.const_defined?(:Custom_backend)
Hiera::Backend.send(:remove_const, :Other_backend) if Hiera::Backend.const_defined?(:Other_backend)
end
$LOAD_PATH.shift
Puppet.override(:environments => environments, :current_environment => env) do
example.run
end
end

Expand Down

0 comments on commit 3c16b21

Please sign in to comment.