hiera 3.1.1 → 3.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -100,7 +100,6 @@ class Hiera
100
100
  #
101
101
  # - 'knockout_prefix' Set to string value to signify prefix which deletes elements from existing element. Defaults is _undef_
102
102
  # - 'sort_merged_arrays' Set to _true_ to sort all arrays that are merged together. Default is _false_
103
- # - 'unpack_arrays' Set to string value used as a deliminator to join all array values and then split them again. Default is _undef_
104
103
  # - 'merge_hash_arrays' Set to _true_ to merge hashes within arrays. Default is _false_
105
104
  #
106
105
  # @param key [String] The key to lookup
@@ -253,7 +253,9 @@ class Hiera
253
253
  segments = Util.split_key(key) { |problem| ArgumentError.new("#{problem} in key: #{key}") }
254
254
  subsegments = nil
255
255
  if segments.size > 1
256
- raise ArgumentError, "Resolution type :#{strategy} is illegal when doing segmented key lookups" unless strategy.nil? || strategy == :priority
256
+ unless strategy.nil? || strategy == :priority
257
+ raise ArgumentError, "Resolution type :#{strategy} is illegal when accessing values using dotted keys. Offending key was '#{key}'"
258
+ end
257
259
  subsegments = segments.drop(1)
258
260
  end
259
261
 
@@ -270,7 +272,7 @@ class Hiera
270
272
  value = backend.lookup_with_segments(segments, scope, order_override, resolution_type, context)
271
273
  else
272
274
  value = backend.lookup(segments[0], scope, order_override, resolution_type, context)
273
- value = qualified_lookup(subsegments, value) unless subsegments.nil?
275
+ value = qualified_lookup(subsegments, value, key) unless subsegments.nil?
274
276
  end
275
277
  found_in_backend = true
276
278
  value
@@ -305,16 +307,24 @@ class Hiera
305
307
  @backends = {}
306
308
  end
307
309
 
308
- def qualified_lookup(segments, hash)
310
+ def qualified_lookup(segments, hash, full_key = nil)
309
311
  value = hash
310
312
  segments.each do |segment|
311
313
  throw :no_such_key if value.nil?
312
314
  if segment =~ /^[0-9]+$/
313
315
  segment = segment.to_i
314
- raise Exception, "Hiera type mismatch: Got #{value.class.name} when Array was expected enable lookup using key '#{segment}'" unless value.instance_of?(Array)
316
+ unless value.instance_of?(Array)
317
+ suffix = full_key.nil? ? '' : " from key '#{full_key}'"
318
+ raise Exception,
319
+ "Hiera type mismatch: Got #{value.class.name} when Array was expected to access value using '#{segment}'#{suffix}"
320
+ end
315
321
  throw :no_such_key unless segment < value.size
316
322
  else
317
- raise Exception, "Hiera type mismatch: Got #{value.class.name} when a non Array object that responds to '[]' was expected to enable lookup using key '#{segment}'" unless value.respond_to?(:'[]') && !value.instance_of?(Array);
323
+ unless value.respond_to?(:'[]') && !(value.instance_of?(Array) || value.instance_of?(String))
324
+ suffix = full_key.nil? ? '' : " from key '#{full_key}'"
325
+ raise Exception,
326
+ "Hiera type mismatch: Got #{value.class.name} when a hash-like object was expected to access value using '#{segment}'#{suffix}"
327
+ end
318
328
  throw :no_such_key unless value.include?(segment)
319
329
  end
320
330
  value = value[segment]
@@ -98,8 +98,8 @@ class Hiera::Interpolate
98
98
 
99
99
  def scope_interpolate(data, key, scope, extra_data, context)
100
100
  segments = Hiera::Util.split_key(key) { |problem| Hiera::InterpolationInvalidValue.new("#{problem} in interpolation expression: #{data}") }
101
- catch(:no_such_key) { return Hiera::Backend.qualified_lookup(segments, scope) }
102
- catch(:no_such_key) { Hiera::Backend.qualified_lookup(segments, extra_data) }
101
+ catch(:no_such_key) { return Hiera::Backend.qualified_lookup(segments, scope, key) }
102
+ catch(:no_such_key) { Hiera::Backend.qualified_lookup(segments, extra_data, key) }
103
103
  end
104
104
  private :scope_interpolate
105
105
 
@@ -7,7 +7,7 @@
7
7
 
8
8
 
9
9
  class Hiera
10
- VERSION = "3.1.1"
10
+ VERSION = "3.1.2"
11
11
 
12
12
  ##
13
13
  # version is a public API method intended to always provide a fast and
@@ -682,6 +682,34 @@ class Hiera
682
682
  expect(Backend.lookup('key.33', 'dflt', {}, nil, nil)).to eq('dflt')
683
683
  end
684
684
 
685
+ it 'will fail when dotted key access is made using a numeric index and value is not array' do
686
+ Config.load({:yaml => {:datadir => '/tmp'}})
687
+ Config.load_backends
688
+ Backend::Yaml_backend.any_instance.expects(:lookup).with('key', {}, nil, nil, instance_of(Hash)).returns(
689
+ {'one' => 'value 1', 'two' => 'value 2'})
690
+ expect {Backend.lookup('key.33', 'dflt', {}, nil, nil)}.to raise_error(Exception,
691
+ /Got Hash when Array was expected to access value using '33' from key 'key.33'/)
692
+ end
693
+
694
+ it 'will fail when dotted key access is made using a string and value is not hash' do
695
+ Config.load({:yaml => {:datadir => '/tmp'}})
696
+ Config.load_backends
697
+ Backend::Yaml_backend.any_instance.expects(:lookup).with('key', {}, nil, nil, instance_of(Hash)).returns(
698
+ ['value 1', 'value 2'])
699
+ expect {Backend.lookup('key.one', 'dflt', {}, nil, nil)}.to raise_error(Exception,
700
+ /Got Array when a hash-like object was expected to access value using 'one' from key 'key.one'/)
701
+ end
702
+
703
+ it 'will fail when dotted key access is made using resolution type :hash' do
704
+ expect {Backend.lookup('key.one', 'dflt', {}, nil, :hash)}.to raise_error(Exception,
705
+ /Resolution type :hash is illegal when accessing values using dotted keys. Offending key was 'key.one'/)
706
+ end
707
+
708
+ it 'will fail when dotted key access is made using resolution type :array' do
709
+ expect {Backend.lookup('key.one', 'dflt', {}, nil, :array)}.to raise_error(Exception,
710
+ /Resolution type :array is illegal when accessing values using dotted keys. Offending key was 'key.one'/)
711
+ end
712
+
685
713
  it 'can use qualified key in interpolation to lookup value in hash' do
686
714
  Config.load({:yaml => {:datadir => '/tmp'}})
687
715
  Config.load_backends
@@ -38,3 +38,6 @@ a.f.hiera: 'a dot f: %{hiera("''a.d''")}'
38
38
  x.1: '(hiera) x dot 1'
39
39
  x.2.scope: "x dot 2: %{'x.1'}"
40
40
  x.2.hiera: 'x dot 2: %{hiera("''x.1''")}'
41
+
42
+ key: subkey
43
+ ipl_key: '- %{hiera("key.subkey")} -'
@@ -163,6 +163,14 @@ describe "Hiera" do
163
163
  it 'should not find a subkey when the dotted key is quoted with method hiera' do
164
164
  expect(hiera.lookup('"a.f.hiera"', nil, {'a' => { 'f' => '(scope) a dot f is a hash entry'}})).to eq('a dot f: ')
165
165
  end
166
+
167
+ it 'should not find a subkey that is matched within a string' do
168
+ expect{ hiera.lookup('ipl_key', nil, {}) }.to raise_error(/Got String when a hash-like object was expected to access value using 'subkey' from key 'key.subkey'/)
169
+ end
170
+
171
+ it 'should not find a subkey that is matched within a string' do
172
+ expect{ hiera.lookup('key.subkey', nil, {}) }.to raise_error(/Got String when a hash-like object was expected to access value using 'subkey' from key 'key.subkey'/)
173
+ end
166
174
  end
167
175
 
168
176
  context 'when bad interpolation expressions are encountered' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hiera
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.1
4
+ version: 3.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-03-23 00:00:00.000000000 Z
12
+ date: 2016-04-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json_pure
@@ -35,52 +35,52 @@ extensions: []
35
35
  extra_rdoc_files: []
36
36
  files:
37
37
  - bin/hiera
38
- - lib/hiera/backend.rb
39
- - lib/hiera/backend/json_backend.rb
40
- - lib/hiera/backend/yaml_backend.rb
41
- - lib/hiera/config.rb
42
- - lib/hiera/console_logger.rb
38
+ - lib/hiera/version.rb
39
+ - lib/hiera/noop_logger.rb
43
40
  - lib/hiera/error.rb
44
41
  - lib/hiera/fallback_logger.rb
42
+ - lib/hiera/recursive_guard.rb
43
+ - lib/hiera/backend/yaml_backend.rb
44
+ - lib/hiera/backend/json_backend.rb
45
+ - lib/hiera/config.rb
45
46
  - lib/hiera/filecache.rb
46
47
  - lib/hiera/interpolate.rb
47
- - lib/hiera/noop_logger.rb
48
+ - lib/hiera/console_logger.rb
48
49
  - lib/hiera/puppet_logger.rb
49
- - lib/hiera/recursive_guard.rb
50
50
  - lib/hiera/util.rb
51
- - lib/hiera/version.rb
51
+ - lib/hiera/backend.rb
52
52
  - lib/hiera.rb
53
53
  - COPYING
54
54
  - README.md
55
55
  - LICENSE
56
- - spec/spec_helper.rb
57
- - spec/unit/backend/json_backend_spec.rb
58
- - spec/unit/backend/yaml_backend_spec.rb
59
- - spec/unit/backend_spec.rb
60
- - spec/unit/config_spec.rb
61
- - spec/unit/console_logger_spec.rb
62
- - spec/unit/fallback_logger_spec.rb
63
- - spec/unit/filecache_spec.rb
64
- - spec/unit/fixtures/interpolate/config/hiera.yaml
56
+ - spec/unit/interpolate_spec.rb
57
+ - spec/unit/fixtures/override/config/hiera.yaml
58
+ - spec/unit/fixtures/override/data/alternate.yaml
59
+ - spec/unit/fixtures/override/data/common.yaml
65
60
  - spec/unit/fixtures/interpolate/config/hiera_iplm_hiera.yaml
66
61
  - spec/unit/fixtures/interpolate/config/hiera_iplm_hiera_bad.yaml
67
- - spec/unit/fixtures/interpolate/data/bad_interpolation.yaml
62
+ - spec/unit/fixtures/interpolate/config/hiera.yaml
63
+ - spec/unit/fixtures/interpolate/data/niltest.yaml
68
64
  - spec/unit/fixtures/interpolate/data/complex.yaml
69
- - spec/unit/fixtures/interpolate/data/dotted_keys.yaml
70
- - spec/unit/fixtures/interpolate/data/empty_interpolation.yaml
71
65
  - spec/unit/fixtures/interpolate/data/frontend.json
72
- - spec/unit/fixtures/interpolate/data/niltest.yaml
73
- - spec/unit/fixtures/interpolate/data/recursive.yaml
74
66
  - spec/unit/fixtures/interpolate/data/role.json
67
+ - spec/unit/fixtures/interpolate/data/bad_interpolation.yaml
68
+ - spec/unit/fixtures/interpolate/data/dotted_keys.yaml
75
69
  - spec/unit/fixtures/interpolate/data/weird_keys.yaml
76
- - spec/unit/fixtures/override/config/hiera.yaml
77
- - spec/unit/fixtures/override/data/alternate.yaml
78
- - spec/unit/fixtures/override/data/common.yaml
79
- - spec/unit/hiera_spec.rb
80
- - spec/unit/interpolate_spec.rb
70
+ - spec/unit/fixtures/interpolate/data/empty_interpolation.yaml
71
+ - spec/unit/fixtures/interpolate/data/recursive.yaml
72
+ - spec/unit/backend_spec.rb
73
+ - spec/unit/filecache_spec.rb
74
+ - spec/unit/config_spec.rb
75
+ - spec/unit/backend/yaml_backend_spec.rb
76
+ - spec/unit/backend/json_backend_spec.rb
81
77
  - spec/unit/puppet_logger_spec.rb
82
- - spec/unit/util_spec.rb
78
+ - spec/unit/console_logger_spec.rb
83
79
  - spec/unit/version_spec.rb
80
+ - spec/unit/fallback_logger_spec.rb
81
+ - spec/unit/util_spec.rb
82
+ - spec/unit/hiera_spec.rb
83
+ - spec/spec_helper.rb
84
84
  homepage: https://github.com/puppetlabs/hiera
85
85
  licenses: []
86
86
  post_install_message:
@@ -106,31 +106,31 @@ signing_key:
106
106
  specification_version: 3
107
107
  summary: Light weight hierarchical data store
108
108
  test_files:
109
- - spec/spec_helper.rb
110
- - spec/unit/backend/json_backend_spec.rb
111
- - spec/unit/backend/yaml_backend_spec.rb
112
- - spec/unit/backend_spec.rb
113
- - spec/unit/config_spec.rb
114
- - spec/unit/console_logger_spec.rb
115
- - spec/unit/fallback_logger_spec.rb
116
- - spec/unit/filecache_spec.rb
117
- - spec/unit/fixtures/interpolate/config/hiera.yaml
109
+ - spec/unit/interpolate_spec.rb
110
+ - spec/unit/fixtures/override/config/hiera.yaml
111
+ - spec/unit/fixtures/override/data/alternate.yaml
112
+ - spec/unit/fixtures/override/data/common.yaml
118
113
  - spec/unit/fixtures/interpolate/config/hiera_iplm_hiera.yaml
119
114
  - spec/unit/fixtures/interpolate/config/hiera_iplm_hiera_bad.yaml
120
- - spec/unit/fixtures/interpolate/data/bad_interpolation.yaml
115
+ - spec/unit/fixtures/interpolate/config/hiera.yaml
116
+ - spec/unit/fixtures/interpolate/data/niltest.yaml
121
117
  - spec/unit/fixtures/interpolate/data/complex.yaml
122
- - spec/unit/fixtures/interpolate/data/dotted_keys.yaml
123
- - spec/unit/fixtures/interpolate/data/empty_interpolation.yaml
124
118
  - spec/unit/fixtures/interpolate/data/frontend.json
125
- - spec/unit/fixtures/interpolate/data/niltest.yaml
126
- - spec/unit/fixtures/interpolate/data/recursive.yaml
127
119
  - spec/unit/fixtures/interpolate/data/role.json
120
+ - spec/unit/fixtures/interpolate/data/bad_interpolation.yaml
121
+ - spec/unit/fixtures/interpolate/data/dotted_keys.yaml
128
122
  - spec/unit/fixtures/interpolate/data/weird_keys.yaml
129
- - spec/unit/fixtures/override/config/hiera.yaml
130
- - spec/unit/fixtures/override/data/alternate.yaml
131
- - spec/unit/fixtures/override/data/common.yaml
132
- - spec/unit/hiera_spec.rb
133
- - spec/unit/interpolate_spec.rb
123
+ - spec/unit/fixtures/interpolate/data/empty_interpolation.yaml
124
+ - spec/unit/fixtures/interpolate/data/recursive.yaml
125
+ - spec/unit/backend_spec.rb
126
+ - spec/unit/filecache_spec.rb
127
+ - spec/unit/config_spec.rb
128
+ - spec/unit/backend/yaml_backend_spec.rb
129
+ - spec/unit/backend/json_backend_spec.rb
134
130
  - spec/unit/puppet_logger_spec.rb
135
- - spec/unit/util_spec.rb
131
+ - spec/unit/console_logger_spec.rb
136
132
  - spec/unit/version_spec.rb
133
+ - spec/unit/fallback_logger_spec.rb
134
+ - spec/unit/util_spec.rb
135
+ - spec/unit/hiera_spec.rb
136
+ - spec/spec_helper.rb