chef-attribute-validator 0.6.1 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +8 -8
- data/CHANGES +3 -0
- data/README.md +12 -3
- data/lib/chef-attribute-validator/checks/child_keys.rb +46 -0
- data/lib/chef-attribute-validator/version.rb +1 -1
- data/test/fixtures/check_child_keys_arg_array.rb +6 -0
- data/test/fixtures/check_child_keys_arg_literal_regex.rb +6 -0
- data/test/fixtures/check_child_keys_arg_nil.rb +6 -0
- data/test/fixtures/check_child_keys_arg_regexp_object.rb +6 -0
- data/test/fixtures/check_child_keys_arg_string.rb +6 -0
- data/test/fixtures/check_child_keys_assorted.rb +90 -0
- data/test/unit/check_child_keys_spec.rb +109 -0
- metadata +16 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NzA5OGRmZWY5NTA2YmRiMjkyNTFjOTY1ZDY2MzUzZjA0M2E1YzI5Mg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZTkwOGY2NDJhZmEwNTljMGZkM2VhMjE5ZTM3NzJhOWQ0NzcyMTNhMw==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZDIyYzhmODk5Mzg2YTg0ZDgxZWRkYTgxZjE3NzllZTAzYWE2NmUyYmJiODQ4
|
10
|
+
ZTZlZGUxMmRmZDI0YTZjMmE5NjFlOWI2Mzk4ODcyNWMwNTU5NGRkODQ0YWZh
|
11
|
+
YTUyOWRiYzg5MDdiMzU5MWRhNGYxYjY5N2Y2YTk4OTljMDdlOWI=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZGVkZmM2YzhlZDYyYmRmNmIzYjgzZmYyMWU3ZjA5MjY4ZmQwMmQ2NWM3Nzk3
|
14
|
+
MTIwNTE2NjhlNzEzODdjNzExYzQxNTRhZTY3NWU3MTA0OWNkZTNiOGVlZjJj
|
15
|
+
M2VlNzQxNjAyYWQxOGMyZWYwMzgyOTY2ODIwMmMyYTZhN2VjMzc=
|
data/CHANGES
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
2014-05-21 Clinton Wolfe 0.7.0
|
2
|
+
Added 'child_keys' check type
|
3
|
+
|
1
4
|
2014-05-20 Clinton Wolfe 0.6.1
|
2
5
|
Fixed issue #6, inconsistent behavior for 'enum' and 'type' checks when a attr has a nil value
|
3
6
|
Fixed issue #5, pathfinder breaking when given a path containing a stringified integer and no wildcards
|
data/README.md
CHANGED
@@ -50,7 +50,16 @@ Each rule gets a unique name. Each entry is hash structure with the following k
|
|
50
50
|
The remaining entries describe criteria to enforce on the value of the attribute(s)
|
51
51
|
referenced by 'path'. You may list zero or more.
|
52
52
|
|
53
|
-
Note that nil is permitted as a value by
|
53
|
+
Note that nil is permitted as a value by most checks, except the following:
|
54
|
+
|
55
|
+
'required' check (which performs nilness checks)
|
56
|
+
'proc' check (which implements arbitrary user behavior)
|
57
|
+
'min_children', 'max_children' (which are restricted to operating on Hashes and Arrays)
|
58
|
+
'child_keys' (which is restricted to operating on Hashes)
|
59
|
+
|
60
|
+
#### child_keys
|
61
|
+
|
62
|
+
Checks the immediate keys of the given path (which must be a Hash). Check argument may be an Array of Strings, in which case each key that is present must be in the list of valid keys, or else a Regexp, which will be matched against each key.
|
54
63
|
|
55
64
|
#### type
|
56
65
|
|
@@ -78,7 +87,7 @@ Boolean. If true, fails if the path matches zero attributes, or the value is ni
|
|
78
87
|
|
79
88
|
#### looks_like
|
80
89
|
|
81
|
-
String, one of 'email', 'guid', 'ip', 'url'. Applies canned regexes (or more sophisticated matchers, like constructing objects from the stdlib). Details:
|
90
|
+
String, one of 'email', 'guid', 'hostname', 'ip', 'url'. Applies canned regexes (or more sophisticated matchers, like constructing objects from the stdlib). Details:
|
82
91
|
|
83
92
|
##### email
|
84
93
|
|
@@ -134,7 +143,7 @@ Possibly eventually support for [<charclass>], or {<alternatives>}.
|
|
134
143
|
|
135
144
|
#### Planned checks:
|
136
145
|
|
137
|
-
|
146
|
+
Perhaps a hostname:port looks_like?
|
138
147
|
|
139
148
|
### Bugs and Defects
|
140
149
|
|
@@ -0,0 +1,46 @@
|
|
1
|
+
class Chef
|
2
|
+
class Attribute
|
3
|
+
class Validator
|
4
|
+
class Check
|
5
|
+
class ChildKeys < Check
|
6
|
+
|
7
|
+
register_check('child_keys', ChildKeys)
|
8
|
+
|
9
|
+
def validate_check_arg
|
10
|
+
if check_arg.kind_of?(Array)
|
11
|
+
unless check_arg.all? { |e| e.kind_of?(String) }
|
12
|
+
raise "Bad 'child_keys' check argument '#{check_arg}' for rule '#{rule_name}' - expected a Regexp or an Array of Strings."
|
13
|
+
end
|
14
|
+
elsif ! check_arg.kind_of?(Regexp)
|
15
|
+
raise "Bad 'child_keys' check argument '#{check_arg}' for rule '#{rule_name}' - expected a Regexp or an Array of Strings."
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def check(attrset)
|
20
|
+
violations = []
|
21
|
+
attrset.each do |path, value|
|
22
|
+
# Must be hashlike
|
23
|
+
if [ Mash, Hash ].any? { |k| value.kind_of?(k) }
|
24
|
+
value.keys.each do |key|
|
25
|
+
if check_arg.kind_of?(Array)
|
26
|
+
unless check_arg.include?(key)
|
27
|
+
violations.push Chef::Attribute::Validator::Violation.new(rule_name, path + '/' + key, "Child Key #{key} must be one of #{check_arg.join(', ')}.")
|
28
|
+
end
|
29
|
+
elsif check_arg.kind_of?(Regexp)
|
30
|
+
unless check_arg.match(key)
|
31
|
+
violations.push Chef::Attribute::Validator::Violation.new(rule_name, path + '/' + key, "Child Key #{key} does not match regex '#{check_arg}'")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
else
|
36
|
+
violations.push Chef::Attribute::Validator::Violation.new(rule_name, path, "Attribute's value is '#{value}', but must be a Hash-like thing to use child_keys")
|
37
|
+
end
|
38
|
+
end
|
39
|
+
violations
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
default['nil'] = nil
|
2
|
+
default['scalar'] = 'thing'
|
3
|
+
default['array'] = [ 1, 2, 3 ]
|
4
|
+
default['empty-hash'] = {}
|
5
|
+
default['three']['foo'] = 1
|
6
|
+
default['three']['oofoo'] = 2
|
7
|
+
default['three']['foof'] = 3
|
8
|
+
default['two']['foo'] = 1
|
9
|
+
default['two']['goof'] = 1
|
10
|
+
|
11
|
+
default['foo']['foo'] = 1
|
12
|
+
default['bar']['bar'] = 1
|
13
|
+
default['barfoo']['bar'] = 1
|
14
|
+
default['barfoo']['foo'] = 1
|
15
|
+
default['foobarbaz']['foo'] = 1
|
16
|
+
default['foobarbaz']['bar'] = 1
|
17
|
+
default['foobarbaz']['baz'] = 1
|
18
|
+
default['baz']['baz'] = 1
|
19
|
+
|
20
|
+
rules = default['attribute-validator']['rules']
|
21
|
+
rules['re-foo-nil'] = {
|
22
|
+
'path' => '/nil',
|
23
|
+
'child_keys' => /foo/,
|
24
|
+
}
|
25
|
+
rules['re-foo-scalar'] = {
|
26
|
+
'path' => '/scalar',
|
27
|
+
'child_keys' => /foo/,
|
28
|
+
}
|
29
|
+
rules['re-foo-array'] = {
|
30
|
+
'path' => '/array',
|
31
|
+
'child_keys' => /foo/,
|
32
|
+
}
|
33
|
+
rules['re-foo-empty-hash'] = {
|
34
|
+
'path' => '/empty-hash',
|
35
|
+
'child_keys' => /foo/,
|
36
|
+
}
|
37
|
+
rules['re-foo-three'] = {
|
38
|
+
'path' => '/three',
|
39
|
+
'child_keys' => /foo/,
|
40
|
+
}
|
41
|
+
rules['re-foo-two'] = {
|
42
|
+
'path' => '/two',
|
43
|
+
'child_keys' => /foo/,
|
44
|
+
}
|
45
|
+
|
46
|
+
|
47
|
+
|
48
|
+
rules['ar-foo-nil'] = {
|
49
|
+
'path' => '/nil',
|
50
|
+
'child_keys' => ['foo', 'bar'],
|
51
|
+
}
|
52
|
+
rules['ar-foo-scalar'] = {
|
53
|
+
'path' => '/scalar',
|
54
|
+
'child_keys' => ['foo', 'bar'],
|
55
|
+
}
|
56
|
+
rules['ar-foo-array'] = {
|
57
|
+
'path' => '/array',
|
58
|
+
'child_keys' => ['foo', 'bar'],
|
59
|
+
}
|
60
|
+
rules['ar-foo-empty-hash'] = {
|
61
|
+
'path' => '/empty-hash',
|
62
|
+
'child_keys' => ['foo', 'bar'],
|
63
|
+
}
|
64
|
+
rules['ar-foo-foo'] = {
|
65
|
+
'path' => '/foo',
|
66
|
+
'child_keys' => ['foo', 'bar'],
|
67
|
+
}
|
68
|
+
rules['ar-foo-bar'] = {
|
69
|
+
'path' => '/bar',
|
70
|
+
'child_keys' => ['foo', 'bar'],
|
71
|
+
}
|
72
|
+
rules['ar-foo-barfoo'] = {
|
73
|
+
'path' => '/barfoo',
|
74
|
+
'child_keys' => ['foo', 'bar'],
|
75
|
+
}
|
76
|
+
rules['ar-foo-foobarbaz'] = {
|
77
|
+
'path' => '/foobarbaz',
|
78
|
+
'child_keys' => ['foo', 'bar'],
|
79
|
+
}
|
80
|
+
rules['ar-foo-baz'] = {
|
81
|
+
'path' => '/baz',
|
82
|
+
'child_keys' => ['foo', 'bar'],
|
83
|
+
}
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require_relative './spec_helper'
|
2
|
+
|
3
|
+
# Verify that the 'child_keys' check works correctly
|
4
|
+
|
5
|
+
describe "'child_keys' check" do
|
6
|
+
|
7
|
+
describe "check registry" do
|
8
|
+
it "should be present in the Check registry" do
|
9
|
+
expect(Chef::Attribute::Validator::Check.list_check_types).to include('child_keys')
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "check_arg checks" do
|
14
|
+
it "should accept a // literal regex" do
|
15
|
+
node = CAVHelper.load_fixture_attributes('check_child_keys_arg_literal_regex')
|
16
|
+
expect { Chef::Attribute::Validator.new(node) }.not_to raise_error
|
17
|
+
end
|
18
|
+
it "should accept a Regexp object" do
|
19
|
+
node = CAVHelper.load_fixture_attributes('check_child_keys_arg_regexp_object')
|
20
|
+
expect { Chef::Attribute::Validator.new(node) }.not_to raise_error
|
21
|
+
end
|
22
|
+
it "should accept an Array of Strings" do
|
23
|
+
node = CAVHelper.load_fixture_attributes('check_child_keys_arg_array')
|
24
|
+
expect { Chef::Attribute::Validator.new(node) }.not_to raise_error
|
25
|
+
end
|
26
|
+
it "should reject a string" do
|
27
|
+
node = CAVHelper.load_fixture_attributes('check_child_keys_arg_string')
|
28
|
+
expect { Chef::Attribute::Validator.new(node) }.to raise_error
|
29
|
+
end
|
30
|
+
it "should reject a nil regex" do
|
31
|
+
node = CAVHelper.load_fixture_attributes('check_child_keys_arg_nil')
|
32
|
+
expect { Chef::Attribute::Validator.new(node) }.to raise_error
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "assorted regexes" do
|
37
|
+
let(:node) { CAVHelper.load_fixture_attributes('check_child_keys_assorted') }
|
38
|
+
let(:av) { Chef::Attribute::Validator.new(node) }
|
39
|
+
|
40
|
+
# Must be a hash to have keys - empty hash OK
|
41
|
+
it "/foo/ should violate on nil" do
|
42
|
+
expect(av.validate_rule('re-foo-nil')).not_to be_empty
|
43
|
+
end
|
44
|
+
|
45
|
+
it "/foo/ should violate on a scalar" do
|
46
|
+
expect(av.validate_rule('re-foo-scalar')).not_to be_empty
|
47
|
+
end
|
48
|
+
|
49
|
+
it "/foo/ should violate on an array" do
|
50
|
+
expect(av.validate_rule('re-foo-array')).not_to be_empty
|
51
|
+
end
|
52
|
+
|
53
|
+
it "/foo/ should not violate on empty hash" do
|
54
|
+
expect(av.validate_rule('re-foo-empty-hash')).to be_empty
|
55
|
+
end
|
56
|
+
|
57
|
+
it "/foo/ should not violate on foo, oofoo, foof" do
|
58
|
+
expect(av.validate_rule('re-foo-three')).to be_empty
|
59
|
+
end
|
60
|
+
|
61
|
+
it "/foo/ should violate on foo, goof" do
|
62
|
+
expect(av.validate_rule('re-foo-two')).not_to be_empty
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe "assorted arrays" do
|
67
|
+
let(:node) { CAVHelper.load_fixture_attributes('check_child_keys_assorted') }
|
68
|
+
let(:av) { Chef::Attribute::Validator.new(node) }
|
69
|
+
|
70
|
+
# Must be a hash to have keys - empty hash OK
|
71
|
+
it "['foo', 'bar'] should violate on nil" do
|
72
|
+
expect(av.validate_rule('ar-foo-nil')).not_to be_empty
|
73
|
+
end
|
74
|
+
|
75
|
+
it "['foo', 'bar'] should violate on a scalar" do
|
76
|
+
expect(av.validate_rule('ar-foo-scalar')).not_to be_empty
|
77
|
+
end
|
78
|
+
|
79
|
+
it "['foo', 'bar'] should violate on an array" do
|
80
|
+
expect(av.validate_rule('ar-foo-array')).not_to be_empty
|
81
|
+
end
|
82
|
+
|
83
|
+
it "['foo', 'bar'] should not violate on empty hash" do
|
84
|
+
expect(av.validate_rule('ar-foo-empty-hash')).to be_empty
|
85
|
+
end
|
86
|
+
|
87
|
+
it "['foo', 'bar'] should not violate on foo" do
|
88
|
+
expect(av.validate_rule('ar-foo-foo')).to be_empty
|
89
|
+
end
|
90
|
+
|
91
|
+
it "['foo', 'bar'] should not violate on bar" do
|
92
|
+
expect(av.validate_rule('ar-foo-bar')).to be_empty
|
93
|
+
end
|
94
|
+
|
95
|
+
it "['foo', 'bar'] should not violate on bar, foo" do
|
96
|
+
expect(av.validate_rule('ar-foo-barfoo')).to be_empty
|
97
|
+
end
|
98
|
+
|
99
|
+
it "['foo', 'bar'] should violate on foo, bar, baz" do
|
100
|
+
expect(av.validate_rule('ar-foo-foobarbaz')).not_to be_empty
|
101
|
+
end
|
102
|
+
|
103
|
+
it "['foo', 'bar'] should violate on baz" do
|
104
|
+
expect(av.validate_rule('ar-foo-baz')).not_to be_empty
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chef-attribute-validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Clinton Wolfe
|
@@ -74,6 +74,7 @@ files:
|
|
74
74
|
- lib/chef-attribute-validator.rb
|
75
75
|
- lib/chef-attribute-validator/attribute_set.rb
|
76
76
|
- lib/chef-attribute-validator/check.rb
|
77
|
+
- lib/chef-attribute-validator/checks/child_keys.rb
|
77
78
|
- lib/chef-attribute-validator/checks/enum.rb
|
78
79
|
- lib/chef-attribute-validator/checks/looks_like.rb
|
79
80
|
- lib/chef-attribute-validator/checks/max_children.rb
|
@@ -91,6 +92,12 @@ files:
|
|
91
92
|
- lib/chef-attribute-validator/wildcard_expander/no_wildcards.rb
|
92
93
|
- test/fixtures/attr_set.rb
|
93
94
|
- test/fixtures/check_child_count.rb
|
95
|
+
- test/fixtures/check_child_keys_arg_array.rb
|
96
|
+
- test/fixtures/check_child_keys_arg_literal_regex.rb
|
97
|
+
- test/fixtures/check_child_keys_arg_nil.rb
|
98
|
+
- test/fixtures/check_child_keys_arg_regexp_object.rb
|
99
|
+
- test/fixtures/check_child_keys_arg_string.rb
|
100
|
+
- test/fixtures/check_child_keys_assorted.rb
|
94
101
|
- test/fixtures/check_enum_array_empty.rb
|
95
102
|
- test/fixtures/check_enum_array_mixed.rb
|
96
103
|
- test/fixtures/check_enum_array_strings.rb
|
@@ -143,6 +150,7 @@ files:
|
|
143
150
|
- test/unit/attr_set_spec.rb
|
144
151
|
- test/unit/attr_set_spec_wildcard.rb
|
145
152
|
- test/unit/check_child_count_spec.rb
|
153
|
+
- test/unit/check_child_keys_spec.rb
|
146
154
|
- test/unit/check_enum_spec.rb
|
147
155
|
- test/unit/check_looks_like_spec.rb
|
148
156
|
- test/unit/check_present_spec.rb
|
@@ -180,6 +188,12 @@ summary: A Rubygem implementing a rule engine for validating Chef node attribute
|
|
180
188
|
test_files:
|
181
189
|
- test/fixtures/attr_set.rb
|
182
190
|
- test/fixtures/check_child_count.rb
|
191
|
+
- test/fixtures/check_child_keys_arg_array.rb
|
192
|
+
- test/fixtures/check_child_keys_arg_literal_regex.rb
|
193
|
+
- test/fixtures/check_child_keys_arg_nil.rb
|
194
|
+
- test/fixtures/check_child_keys_arg_regexp_object.rb
|
195
|
+
- test/fixtures/check_child_keys_arg_string.rb
|
196
|
+
- test/fixtures/check_child_keys_assorted.rb
|
183
197
|
- test/fixtures/check_enum_array_empty.rb
|
184
198
|
- test/fixtures/check_enum_array_mixed.rb
|
185
199
|
- test/fixtures/check_enum_array_strings.rb
|
@@ -232,6 +246,7 @@ test_files:
|
|
232
246
|
- test/unit/attr_set_spec.rb
|
233
247
|
- test/unit/attr_set_spec_wildcard.rb
|
234
248
|
- test/unit/check_child_count_spec.rb
|
249
|
+
- test/unit/check_child_keys_spec.rb
|
235
250
|
- test/unit/check_enum_spec.rb
|
236
251
|
- test/unit/check_looks_like_spec.rb
|
237
252
|
- test/unit/check_present_spec.rb
|