chef-attribute-validator 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MzE4YmM5ZjAyNGVjM2MyYTY5M2Q2OGFmMWE5YmM3YmRiNWFmNWNlZQ==
4
+ ZDJjZjdhYTFkYWNmMGU0ZTg1MDg4MzdlMWE5Nzg0ZGEwYzhlOGYxNQ==
5
5
  data.tar.gz: !binary |-
6
- Y2FlNGQ5NTYyZWMzY2UzY2JjOTZmZmMwYzY3MzFiZGNiNjU2N2U2NA==
6
+ MmM1OTA0N2RiN2E1MjE3NmQxNTg1NDc5ZjRhN2E1OGJmMDdmNzY4MQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZjM2NDdkOTBiMzQ3MDU5MmNkOGE1OGJlYzliZTY4ZWQ2NjZmNzcwODc0YmVl
10
- OWEzZDdhYWYzOGExMmJiMDAwMTQ1OWYxNzg3MDYyZWE5ZmI4NjI4NDYzYWFj
11
- ZTc3ODYzNTI0YTYxNTljNzdkOWNlN2RkNTIyZjI3MThiOWUzY2U=
9
+ NGMxOTE3M2Y3NTk3ZDU0NmJiY2I2YTdlZGIyYjY2MzBiMmUzNzk3OWQ3ZDhj
10
+ Yjc0NTA3MDZjN2M0NjAxYzVkMzM4MjEwMGI4NDRkYjFmYThjNGI0NzY2MTNi
11
+ NjA4YjQ3ZWY5MWQyYmVkNGNkZWE3MDZjYzNjODk3OTBmOTQ3ZDI=
12
12
  data.tar.gz: !binary |-
13
- ZDdkYjUyOWM2OTIyYTcwNDZlYjA4OTQ2MDk0MDViOTIzMTExNDViZmRiOTVl
14
- NzYyOGY0ZTIwOWZlOTI4OGQ0MzAzMjFkZWM5MmRmMmNjNTM3MmUyZjBmOWY0
15
- MzhlZTNhODY3ODM1NzFmNjBlODNjN2VmODQ3ZTQ3ZDk3YmVkZTQ=
13
+ MDJlMjQ0YTZhM2JlYzE1YWRjMGJlZTY2NjE2NWMyNTdiZTBiYjRiNmJmNTQ2
14
+ NWM4NTQ4ZTlhYjMyNWFiOWJjNGVlZDU3MzBhZjI4ZmNiZjI2N2FjMWFmZWJl
15
+ N2Q2OWE0NWI4YWFjZDMwNmI3OGE0OTU1ZmE0OTdlNDc2YzIyZmI=
data/CHANGES CHANGED
@@ -1,3 +1,7 @@
1
+ 2013-11-15 Clinton Wolfe 0.4.0
2
+ Add 'present' check
3
+ Reformat list of checks in README
4
+
1
5
  2013-11-15 Clinton Wolfe 0.3.2
2
6
  umm wait regex should not violate if given a non-string - use type check if you want that
3
7
 
data/README.md CHANGED
@@ -50,14 +50,42 @@ 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
- type - Checks type of value. One of 'string', 'number', 'boolean', 'hash', 'array'.
54
- min_children - Integer. Fails for all but Hash and Array. For Hash and Array, minimum number of elements to be considered valid.
55
- max_children - Integer. Fails for all but Hash and Array. For Hash and Array, maximum number of elements to be considered valid.
56
- regex - Regexp. Applies given regex to the value. Ignored for Hash and Array. See looks_like for a selection of canned regexen.
57
- required - Boolean. If true, fails if the path matches zero attributes, or the value is nil, or the value is the empty string, or if the value is an empty array or empty hash. No-op if false (use present => false to enforce absence).
58
- looks_like - String, one of 'url', 'ip'. Applies canned regexes (or more sophisticated matchers).
59
- enum - Array, a set of possible values.
60
- proc - a Proc, which should take two args - a string rule name, and a Chef::Attribute::Validator::AttributeSet. You can treat the attribute set as a hash - its each() methods will yield path, value pairs. The proc should return a possibly empty array of Chef::Attribute::Validator::Violations.
53
+ #### type
54
+
55
+ Checks type of value. One of 'string', 'number', 'boolean', 'hash', 'array'.
56
+
57
+ #### min_children
58
+
59
+ Integer. Fails for all but Hash and Array. For Hash and Array, minimum number of elements to be considered valid.
60
+
61
+ #### max_children
62
+
63
+ Integer. Fails for all but Hash and Array. For Hash and Array, maximum number of elements to be considered valid.
64
+
65
+ #### present
66
+
67
+ Boolean. If true, fails if the path matches zero attributes. If false, fails if the path matches nonzero attributes. Does not consider nilness, only existence of attribute key(s). See also required.
68
+
69
+ #### regex
70
+
71
+ Regexp. Applies given regex to the value. Ignored for Hash and Array. See looks_like for a selection of canned regexen.
72
+
73
+ #### required
74
+
75
+ Boolean. If true, fails if the path matches zero attributes, or the value is nil, or the value is the empty string, or if the value is an empty array or empty hash. No-op if false (use present => false to enforce absence).
76
+
77
+ #### looks_like
78
+
79
+ String, one of 'url', 'ip'. Applies canned regexes (or more sophisticated matchers, like contructing objects from the stdlib).
80
+
81
+ #### enum
82
+
83
+ Array, a set of possible values.
84
+
85
+ #### proc
86
+
87
+ A Proc, which will be evaluated to determine the validity. The proc should take two args - a string rule name, and a Chef::Attribute::Validator::AttributeSet. You can treat the attribute set as a hash - its each() methods will yield path, value pairs.
88
+ The proc should return a possibly empty array of Chef::Attribute::Validator::Violations.
61
89
 
62
90
  ## Referencing Attributes
63
91
 
@@ -76,20 +104,17 @@ Open a github issue at https://github.com/clintoncwolfe/chef-attribute-validator
76
104
 
77
105
  ### Roadmap
78
106
 
107
+ #### Need examples in README
108
+
79
109
  #### Some wildcard syntax not yet supported
80
110
 
81
111
  Possibly eventually support for **, [<charclass>], or {<alternatives>}.
82
112
 
83
- #### Companion cookbook
84
-
85
- Simple cookbook named 'attribute-validator' that loads the gem and provides recipes for compile-time and convergence-time violation checking.
86
-
87
113
  #### Planned checks:
88
114
 
89
115
  looks_like/hostname
90
116
  looks_like/email
91
117
  name_regex - Regexp. Applies given regex to the last element in the attribute path ('basename', if you will)
92
- present - Boolean. If true, fails if the path matches zero attributes. If false, fails if the path matches nonzero attributes. Does not consider nilness, only existence of attribute key(s). See also required.
93
118
 
94
119
  ### Bugs and Defects
95
120
 
@@ -0,0 +1,34 @@
1
+ class Chef
2
+ class Attribute
3
+ class Validator
4
+ class Check
5
+ class Present < Check
6
+
7
+ register_check('present', Present)
8
+
9
+ def validate_check_arg
10
+ unless check_arg.kind_of?(TrueClass) || check_arg.kind_of?(FalseClass)
11
+ raise "Bad 'present' check argument '#{check_arg}' for rule '#{rule_name}' - expected one of true,false"
12
+ end
13
+ end
14
+
15
+ def check(attrset)
16
+ violations = []
17
+ if check_arg
18
+ # OK, presence is required - violate if attrset is empty
19
+ if attrset.empty?
20
+ violations.push Chef::Attribute::Validator::Violation.new(rule_name, path_spec, "No attributes exist for path '#{path_spec}', but this rule says its presence is required.")
21
+ end
22
+ else
23
+ # Presence is taboo. Violate on each extant attr
24
+ attrset.each do |path, value|
25
+ violations.push Chef::Attribute::Validator::Violation.new(rule_name, path, "This rule bans the presense of this attribute, but it exists (regardless of value).")
26
+ end
27
+ end
28
+ violations
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -1,7 +1,7 @@
1
1
  class Chef
2
2
  class Attribute
3
3
  class Validator
4
- VERSION = "0.3.2"
4
+ VERSION = "0.4.0"
5
5
  end
6
6
  end
7
7
  end
@@ -0,0 +1,88 @@
1
+
2
+ default['yep']['what'] = 'missed me'
3
+ default['nil'] = nil
4
+
5
+ default['empty']['string'] = ""
6
+ default['empty']['array'] = []
7
+ default['empty']['hash'] = {}
8
+
9
+ default['present']['nil'] = nil
10
+ default['present']['string'] = 'foo'
11
+ default['present']['zero'] = 0
12
+ default['present']['false'] = false
13
+ default['present']['array'] = [ 'a' ]
14
+ default['present']['hash'] = { 'a' => 'b' }
15
+
16
+
17
+
18
+ rules = default['attribute-validator']['rules']
19
+ rules['missing-shallow'] = {
20
+ 'path' => '/nope',
21
+ 'present' => true,
22
+ }
23
+
24
+ rules['missing-deep'] = {
25
+ 'path' => '/yep/nope',
26
+ 'present' => true,
27
+ }
28
+
29
+ rules['value-nil'] = {
30
+ 'path' => '/nil',
31
+ 'present' => true,
32
+ }
33
+
34
+ rules['value-empty-string'] = {
35
+ 'path' => '/empty/string',
36
+ 'present' => true,
37
+ }
38
+
39
+ rules['value-empty-array'] = {
40
+ 'path' => '/empty/array',
41
+ 'present' => true,
42
+ }
43
+
44
+ rules['value-empty-hash'] = {
45
+ 'path' => '/empty/hash',
46
+ 'present' => true,
47
+ }
48
+
49
+ rules['value-present-string'] = {
50
+ 'path' => '/present/string',
51
+ 'present' => true,
52
+ }
53
+
54
+ rules['value-present-false'] = {
55
+ 'path' => '/present/false',
56
+ 'present' => true,
57
+ }
58
+
59
+ rules['value-present-zero'] = {
60
+ 'path' => '/present/zero',
61
+ 'present' => true,
62
+ }
63
+
64
+ rules['value-present-array'] = {
65
+ 'path' => '/present/array',
66
+ 'present' => true,
67
+ }
68
+
69
+ rules['value-present-hash'] = {
70
+ 'path' => '/present/hash',
71
+ 'present' => true,
72
+ }
73
+
74
+ rules['spurious-present-string'] = {
75
+ 'path' => '/present/string',
76
+ 'present' => false,
77
+ }
78
+
79
+ rules['spurious-present-nil'] = {
80
+ 'path' => '/present/nil',
81
+ 'present' => false,
82
+ }
83
+
84
+ rules['spurious-missing'] = {
85
+ 'path' => '/nope',
86
+ 'present' => false,
87
+ }
88
+
@@ -0,0 +1,5 @@
1
+ rules = default['attribute-validator']['rules']
2
+ rules['check-present_false'] = {
3
+ 'path' => '/feh',
4
+ 'present' => false,
5
+ }
@@ -0,0 +1,5 @@
1
+ rules = default['attribute-validator']['rules']
2
+ rules['check-present_true'] = {
3
+ 'path' => '/feh',
4
+ 'present' => true,
5
+ }
@@ -0,0 +1,5 @@
1
+ rules = default['attribute-validator']['rules']
2
+ rules['check-present_zero'] = {
3
+ 'path' => '/feh',
4
+ 'present' => 0,
5
+ }
@@ -0,0 +1,110 @@
1
+ require_relative './spec_helper'
2
+
3
+ # Verify that the 'present' check works correctly
4
+
5
+ describe "'present' 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('present')
10
+ end
11
+ end
12
+
13
+ describe "check_arg checks" do
14
+ it "should accept true" do
15
+ node = CAVHelper.load_fixture_attributes('check_present_true')
16
+ expect { Chef::Attribute::Validator.new(node) }.not_to raise_error
17
+ end
18
+ it "should accept false" do
19
+ node = CAVHelper.load_fixture_attributes('check_present_false')
20
+ expect { Chef::Attribute::Validator.new(node) }.not_to raise_error
21
+ end
22
+ it "should reject 0" do
23
+ node = CAVHelper.load_fixture_attributes('check_present_zero')
24
+ expect { Chef::Attribute::Validator.new(node) }.to raise_error
25
+ end
26
+ end
27
+
28
+ context "when present is true, but the attribute is missing" do
29
+ let(:node) { CAVHelper.load_fixture_attributes('check_present_assorted') }
30
+ let(:av) { Chef::Attribute::Validator.new(node) }
31
+
32
+ it "should violate when the attribute is missing, shallow" do
33
+ expect(av.validate_rule('missing-shallow')).not_to be_empty
34
+ end
35
+
36
+ it "should violate when the attribute is missing, deep" do
37
+ expect(av.validate_rule('missing-deep')).not_to be_empty
38
+ end
39
+ end
40
+
41
+ context "when present is true, and the value is missing" do
42
+ let(:node) { CAVHelper.load_fixture_attributes('check_present_assorted') }
43
+ let(:av) { Chef::Attribute::Validator.new(node) }
44
+
45
+ it "should not violate when the value is nil" do
46
+ expect(av.validate_rule('value-nil')).to be_empty
47
+ end
48
+
49
+ it "should not violate when the value is an empty string" do
50
+ expect(av.validate_rule('value-empty-string')).to be_empty
51
+ end
52
+
53
+ it "should not violate when the value is an empty array" do
54
+ expect(av.validate_rule('value-empty-array')).to be_empty
55
+ end
56
+
57
+ it "should not violate when the value is an empty hash" do
58
+ expect(av.validate_rule('value-empty-hash')).to be_empty
59
+ end
60
+ end
61
+
62
+ context "when present is true, and a value is present" do
63
+ let(:node) { CAVHelper.load_fixture_attributes('check_present_assorted') }
64
+ let(:av) { Chef::Attribute::Validator.new(node) }
65
+
66
+ it "should not violate when the value is a string " do
67
+ expect(av.validate_rule('value-present-string')).to be_empty
68
+ end
69
+
70
+ it "should not violate when the value is false " do
71
+ expect(av.validate_rule('value-present-false')).to be_empty
72
+ end
73
+
74
+ it "should not violate when the value is zero " do
75
+ expect(av.validate_rule('value-present-zero')).to be_empty
76
+ end
77
+
78
+ it "should not violate when the value is a non-empty array " do
79
+ expect(av.validate_rule('value-present-array')).to be_empty
80
+ end
81
+
82
+ it "should not violate when the value is a non-empty hash" do
83
+ expect(av.validate_rule('value-present-hash')).to be_empty
84
+ end
85
+ end
86
+
87
+ context "when present is false, but the value is present" do
88
+ let(:node) { CAVHelper.load_fixture_attributes('check_present_assorted') }
89
+ let(:av) { Chef::Attribute::Validator.new(node) }
90
+
91
+ it "should violate when the value is a string" do
92
+ expect(av.validate_rule('spurious-present-string')).not_to be_empty
93
+ end
94
+
95
+ it "should violate when the value is nil" do
96
+ expect(av.validate_rule('spurious-present-nil')).not_to be_empty
97
+ end
98
+ end
99
+
100
+ context "when present is false, but the attribute is missing" do
101
+ let(:node) { CAVHelper.load_fixture_attributes('check_present_assorted') }
102
+ let(:av) { Chef::Attribute::Validator.new(node) }
103
+
104
+ it "should not violate when the attribute is missing" do
105
+ expect(av.validate_rule('spurious-missing')).to be_empty
106
+ end
107
+ end
108
+
109
+
110
+ 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.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Clinton Wolfe
@@ -78,6 +78,7 @@ files:
78
78
  - lib/chef-attribute-validator/checks/looks_like.rb
79
79
  - lib/chef-attribute-validator/checks/max_children.rb
80
80
  - lib/chef-attribute-validator/checks/min_children.rb
81
+ - lib/chef-attribute-validator/checks/present.rb
81
82
  - lib/chef-attribute-validator/checks/proc.rb
82
83
  - lib/chef-attribute-validator/checks/regex.rb
83
84
  - lib/chef-attribute-validator/checks/required.rb
@@ -102,6 +103,10 @@ files:
102
103
  - test/fixtures/check_looks_like_arg_your_mom.rb
103
104
  - test/fixtures/check_looks_like_ip.rb
104
105
  - test/fixtures/check_looks_like_url.rb
106
+ - test/fixtures/check_present_assorted.rb
107
+ - test/fixtures/check_present_false.rb
108
+ - test/fixtures/check_present_true.rb
109
+ - test/fixtures/check_present_zero.rb
105
110
  - test/fixtures/check_proc_arity0.rb
106
111
  - test/fixtures/check_proc_arity1.rb
107
112
  - test/fixtures/check_proc_arity2.rb
@@ -131,6 +136,7 @@ files:
131
136
  - test/unit/check_child_count_spec.rb
132
137
  - test/unit/check_enum_spec.rb
133
138
  - test/unit/check_looks_like_spec.rb
139
+ - test/unit/check_present_spec.rb
134
140
  - test/unit/check_proc_spec.rb
135
141
  - test/unit/check_regex_spec.rb
136
142
  - test/unit/check_required_spec.rb
@@ -177,6 +183,10 @@ test_files:
177
183
  - test/fixtures/check_looks_like_arg_your_mom.rb
178
184
  - test/fixtures/check_looks_like_ip.rb
179
185
  - test/fixtures/check_looks_like_url.rb
186
+ - test/fixtures/check_present_assorted.rb
187
+ - test/fixtures/check_present_false.rb
188
+ - test/fixtures/check_present_true.rb
189
+ - test/fixtures/check_present_zero.rb
180
190
  - test/fixtures/check_proc_arity0.rb
181
191
  - test/fixtures/check_proc_arity1.rb
182
192
  - test/fixtures/check_proc_arity2.rb
@@ -206,6 +216,7 @@ test_files:
206
216
  - test/unit/check_child_count_spec.rb
207
217
  - test/unit/check_enum_spec.rb
208
218
  - test/unit/check_looks_like_spec.rb
219
+ - test/unit/check_present_spec.rb
209
220
  - test/unit/check_proc_spec.rb
210
221
  - test/unit/check_regex_spec.rb
211
222
  - test/unit/check_required_spec.rb