chef-attribute-validator 0.1.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.
Files changed (52) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +20 -0
  3. data/CHANGES +4 -0
  4. data/Gemfile +2 -0
  5. data/LICENSE +27 -0
  6. data/README.md +109 -0
  7. data/Rakefile +27 -0
  8. data/chef-attribute-validator.gemspec +25 -0
  9. data/lib/chef-attribute-validator.rb +52 -0
  10. data/lib/chef-attribute-validator/attribute_set.rb +90 -0
  11. data/lib/chef-attribute-validator/check.rb +56 -0
  12. data/lib/chef-attribute-validator/checks/looks_like.rb +52 -0
  13. data/lib/chef-attribute-validator/checks/max_children.rb +37 -0
  14. data/lib/chef-attribute-validator/checks/min_children.rb +37 -0
  15. data/lib/chef-attribute-validator/checks/regex.rb +31 -0
  16. data/lib/chef-attribute-validator/checks/required.rb +42 -0
  17. data/lib/chef-attribute-validator/checks/type.rb +48 -0
  18. data/lib/chef-attribute-validator/rule.rb +54 -0
  19. data/lib/chef-attribute-validator/version.rb +7 -0
  20. data/lib/chef-attribute-validator/violation.rb +17 -0
  21. data/test/fixtures/attr_set.rb +10 -0
  22. data/test/fixtures/check_child_count.rb +104 -0
  23. data/test/fixtures/check_looks_like_arg_ip.rb +6 -0
  24. data/test/fixtures/check_looks_like_arg_regex.rb +6 -0
  25. data/test/fixtures/check_looks_like_arg_url.rb +6 -0
  26. data/test/fixtures/check_looks_like_arg_your_mom.rb +6 -0
  27. data/test/fixtures/check_looks_like_ip.rb +57 -0
  28. data/test/fixtures/check_looks_like_url.rb +39 -0
  29. data/test/fixtures/check_regex_assorted.rb +42 -0
  30. data/test/fixtures/check_regex_literal.rb +5 -0
  31. data/test/fixtures/check_regex_nil.rb +5 -0
  32. data/test/fixtures/check_regex_object.rb +5 -0
  33. data/test/fixtures/check_regex_string.rb +5 -0
  34. data/test/fixtures/check_required_assorted.rb +82 -0
  35. data/test/fixtures/check_required_false.rb +5 -0
  36. data/test/fixtures/check_required_true.rb +5 -0
  37. data/test/fixtures/check_required_zero.rb +5 -0
  38. data/test/fixtures/check_type.rb +270 -0
  39. data/test/fixtures/rules_empty.rb +2 -0
  40. data/test/fixtures/rules_missing_path.rb +1 -0
  41. data/test/fixtures/rules_no_check.rb +1 -0
  42. data/test/fixtures/rules_type_and_min_children.rb +3 -0
  43. data/test/fixtures/rules_type_check.rb +2 -0
  44. data/test/unit/attr_set_spec.rb +55 -0
  45. data/test/unit/check_child_count_spec.rb +109 -0
  46. data/test/unit/check_looks_like_spec.rb +106 -0
  47. data/test/unit/check_regex_spec.rb +62 -0
  48. data/test/unit/check_required_spec.rb +106 -0
  49. data/test/unit/check_type_spec.rb +191 -0
  50. data/test/unit/rule_parse_spec.rb +93 -0
  51. data/test/unit/spec_helper.rb +18 -0
  52. metadata +170 -0
@@ -0,0 +1,191 @@
1
+ require_relative './spec_helper'
2
+
3
+ # Verify that the 'type' check works correctly
4
+
5
+
6
+ describe "'type' check" do
7
+
8
+ let(:node) { CAVHelper.load_fixture_attributes('check_type') }
9
+ let(:av) { Chef::Attribute::Validator.new(node) }
10
+
11
+ describe "check registry" do
12
+ it "should be present in the Check registry" do
13
+ expect(Chef::Attribute::Validator::Check.list_check_types).to include('type')
14
+ end
15
+ end
16
+
17
+ describe "string" do
18
+ it "should not violate on an empty string" do
19
+ expect(av.validate_rule('string-empty')).to be_empty
20
+ end
21
+
22
+ it "should not violate on foo" do
23
+ expect(av.validate_rule('string-foo')).to be_empty
24
+ end
25
+
26
+ it "should violate on a number" do
27
+ expect(av.validate_rule('string-number')).not_to be_empty
28
+ end
29
+
30
+ it "should violate on nil" do
31
+ expect(av.validate_rule('string-nil')).not_to be_empty
32
+ end
33
+
34
+ it "should violate on array" do
35
+ expect(av.validate_rule('string-array')).not_to be_empty
36
+ end
37
+
38
+ it "should violate on hash" do
39
+ expect(av.validate_rule('string-hash')).not_to be_empty
40
+ end
41
+ end
42
+
43
+ describe "number" do
44
+ it "should not violate on zero" do
45
+ expect(av.validate_rule('number-zero')).to be_empty
46
+ end
47
+
48
+ it "should not violate on 42" do
49
+ expect(av.validate_rule('number-42')).to be_empty
50
+ end
51
+
52
+ it "should not violate on 98.6" do
53
+ expect(av.validate_rule('number-98point6')).to be_empty
54
+ end
55
+
56
+ it "should violate on a number as a string" do
57
+ expect(av.validate_rule('number-as-string')).not_to be_empty
58
+ end
59
+
60
+ it "should violate on nil" do
61
+ expect(av.validate_rule('number-nil')).not_to be_empty
62
+ end
63
+
64
+ it "should violate on array" do
65
+ expect(av.validate_rule('number-array')).not_to be_empty
66
+ end
67
+
68
+ it "should violate on hash" do
69
+ expect(av.validate_rule('number-hash')).not_to be_empty
70
+ end
71
+ end
72
+
73
+ describe "boolean" do
74
+ it "should not violate on false" do
75
+ expect(av.validate_rule('boolean-false')).to be_empty
76
+ end
77
+
78
+ it "should not violate on true" do
79
+ expect(av.validate_rule('boolean-true')).to be_empty
80
+ end
81
+
82
+ it "should violate on a boolean true as a string" do
83
+ expect(av.validate_rule('boolean-true-as-string')).not_to be_empty
84
+ end
85
+
86
+ it "should violate on a boolean false as a string" do
87
+ expect(av.validate_rule('boolean-false-as-string')).not_to be_empty
88
+ end
89
+
90
+ it "should violate on zero" do
91
+ expect(av.validate_rule('boolean-zero')).not_to be_empty
92
+ end
93
+
94
+ it "should violate on one" do
95
+ expect(av.validate_rule('boolean-one')).not_to be_empty
96
+ end
97
+
98
+ it "should violate on empty string" do
99
+ expect(av.validate_rule('boolean-empty')).not_to be_empty
100
+ end
101
+
102
+ it "should violate on nil" do
103
+ expect(av.validate_rule('boolean-nil')).not_to be_empty
104
+ end
105
+
106
+ it "should violate on array" do
107
+ expect(av.validate_rule('boolean-array')).not_to be_empty
108
+ end
109
+
110
+ it "should violate on hash" do
111
+ expect(av.validate_rule('boolean-hash')).not_to be_empty
112
+ end
113
+ end
114
+
115
+ describe "array" do
116
+ it "should not violate on empty array" do
117
+ expect(av.validate_rule('array-empty')).to be_empty
118
+ end
119
+
120
+ it "should not violate on literal array" do
121
+ expect(av.validate_rule('array-literal')).to be_empty
122
+ end
123
+
124
+ it "should not violate on incrementally built simple array" do
125
+ expect(av.validate_rule('array-incremental-simple')).to be_empty
126
+ end
127
+
128
+ it "should not violate on incrementally built indexed array" do
129
+ expect(av.validate_rule('array-incremental-indexed')).to be_empty
130
+ end
131
+
132
+ it "should not violate on incrementally built deep array" do
133
+ expect(av.validate_rule('array-incremental-deep')).to be_empty
134
+ end
135
+
136
+ it "should violate on a array as a string" do
137
+ expect(av.validate_rule('array-as-string')).not_to be_empty
138
+ end
139
+
140
+ it "should violate on zero" do
141
+ expect(av.validate_rule('array-zero')).not_to be_empty
142
+ end
143
+
144
+ it "should violate on nil" do
145
+ expect(av.validate_rule('array-nil')).not_to be_empty
146
+ end
147
+
148
+ it "should violate on hash" do
149
+ expect(av.validate_rule('array-hash')).not_to be_empty
150
+ end
151
+ end
152
+
153
+ describe "hash" do
154
+ it "should not violate on empty hash" do
155
+ expect(av.validate_rule('hash-empty')).to be_empty
156
+ end
157
+
158
+ it "should not violate on literal hash" do
159
+ expect(av.validate_rule('hash-literal')).to be_empty
160
+ end
161
+
162
+ it "should not violate on incrementally built simple hash" do
163
+ expect(av.validate_rule('hash-incremental-simple')).to be_empty
164
+ end
165
+
166
+ it "should not violate on incrementally built indexed hash" do
167
+ expect(av.validate_rule('hash-incremental-indexed')).to be_empty
168
+ end
169
+
170
+ it "should not violate on incrementally built deep hash" do
171
+ expect(av.validate_rule('hash-incremental-deep')).to be_empty
172
+ end
173
+
174
+ it "should violate on a hash as a string" do
175
+ expect(av.validate_rule('hash-as-string')).not_to be_empty
176
+ end
177
+
178
+ it "should violate on zero" do
179
+ expect(av.validate_rule('hash-zero')).not_to be_empty
180
+ end
181
+
182
+ it "should violate on nil" do
183
+ expect(av.validate_rule('hash-nil')).not_to be_empty
184
+ end
185
+
186
+ it "should violate on array" do
187
+ expect(av.validate_rule('hash-array')).not_to be_empty
188
+ end
189
+ end
190
+
191
+ end
@@ -0,0 +1,93 @@
1
+ require_relative './spec_helper'
2
+
3
+ # Verify that we can read rules from a node's attributes
4
+
5
+ # Given a node
6
+ # I should be able to create a Chef::Attribute::Validator
7
+ # and it should have the rules I expect
8
+
9
+ shared_examples "constructor" do
10
+ it "should be able to create a Validator" do
11
+ expect { Chef::Attribute::Validator.new(node) }.not_to raise_error
12
+ end
13
+ end
14
+
15
+ describe "no rules" do
16
+ let(:node) { CAVHelper.load_fixture_attributes('rules_empty') }
17
+ include_examples "constructor"
18
+
19
+ it "should have no rules" do
20
+ expect(Chef::Attribute::Validator.new(node).rules).to be_empty
21
+ end
22
+ end
23
+
24
+ describe "one rule with no checks" do
25
+ let(:node) { CAVHelper.load_fixture_attributes('rules_no_check') }
26
+ let(:av) { Chef::Attribute::Validator.new(node) }
27
+ include_examples "constructor"
28
+
29
+ it "should have one rule" do
30
+ expect(av.rules.size).to eq(1)
31
+ end
32
+
33
+ it "should have one rule with no checks" do
34
+ expect(av.rules['test-no-check-rule'].check_count).to eq(0)
35
+ end
36
+
37
+ end
38
+
39
+ describe "one rule with a missing path" do
40
+ let(:node) { CAVHelper.load_fixture_attributes('rules_missing_path') }
41
+
42
+ it "should fail during the constructor" do
43
+ expect { Chef::Attribute::Validator.new(node) }.to raise_error
44
+ end
45
+
46
+ end
47
+
48
+
49
+ describe "one rule with a type check" do
50
+ let(:node) { CAVHelper.load_fixture_attributes('rules_type_check') }
51
+ let(:av) { Chef::Attribute::Validator.new(node) }
52
+ include_examples "constructor"
53
+
54
+ it "should have one rule" do
55
+ expect(av.rules.size).to eq(1)
56
+ end
57
+
58
+ it "should have one rule with one check" do
59
+ expect(av.rules['test-type-rule'].check_count).to eq(1)
60
+ end
61
+
62
+ it "should have one rule with a type check check" do
63
+ expect(av.rules['test-type-rule'].has_check?('type')).to be_true
64
+ end
65
+
66
+ end
67
+
68
+ describe "one rule with a type check and a min_children check" do
69
+ let(:node) { CAVHelper.load_fixture_attributes('rules_type_and_min_children') }
70
+ let(:av) { Chef::Attribute::Validator.new(node) }
71
+ include_examples "constructor"
72
+
73
+ it "should have one rule" do
74
+ expect(av.rules.size).to eq(1)
75
+ end
76
+
77
+ it "should have one rule with two checks" do
78
+ expect(av.rules['test-type-and-min-children-rule'].check_count).to eq(2)
79
+ end
80
+
81
+ it "should have one rule with a type check check" do
82
+ expect(av.rules['test-type-and-min-children-rule'].has_check?('type')).to be_true
83
+ end
84
+
85
+ it "should have one rule with a min_children check" do
86
+ expect(av.rules['test-type-and-min-children-rule'].has_check?('min_children')).to be_true
87
+ end
88
+
89
+ end
90
+
91
+
92
+
93
+
@@ -0,0 +1,18 @@
1
+
2
+ require 'chef/node'
3
+
4
+ require_relative '../../lib/chef-attribute-validator'
5
+
6
+ module CAVHelper
7
+ def load_fixture_attributes(filename, node = nil)
8
+ node ||= Chef::Node.new
9
+ filepath = File.join(File.dirname(__FILE__), '..', 'fixtures', filename)
10
+ filepath = filepath.gsub(/\.rb$/, '') + '.rb'
11
+ node.from_file(filepath)
12
+ node
13
+ end
14
+
15
+ module_function :load_fixture_attributes
16
+
17
+ end
18
+
metadata ADDED
@@ -0,0 +1,170 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chef-attribute-validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Clinton Wolfe
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: chef
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 11.6.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 11.6.0
55
+ description: Define, enforce, and handle violations of validation rules for Chef node
56
+ attributes. This gem provides the validation engine, and can be used outside of
57
+ a convergence run; a cookbook (attribute-validator) is availabel to perform validation
58
+ during a chef run, at compile or converge time.
59
+ email:
60
+ - clinton@omniti.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - .gitignore
66
+ - CHANGES
67
+ - Gemfile
68
+ - LICENSE
69
+ - README.md
70
+ - Rakefile
71
+ - chef-attribute-validator.gemspec
72
+ - lib/chef-attribute-validator.rb
73
+ - lib/chef-attribute-validator/attribute_set.rb
74
+ - lib/chef-attribute-validator/check.rb
75
+ - lib/chef-attribute-validator/checks/looks_like.rb
76
+ - lib/chef-attribute-validator/checks/max_children.rb
77
+ - lib/chef-attribute-validator/checks/min_children.rb
78
+ - lib/chef-attribute-validator/checks/regex.rb
79
+ - lib/chef-attribute-validator/checks/required.rb
80
+ - lib/chef-attribute-validator/checks/type.rb
81
+ - lib/chef-attribute-validator/rule.rb
82
+ - lib/chef-attribute-validator/version.rb
83
+ - lib/chef-attribute-validator/violation.rb
84
+ - test/fixtures/attr_set.rb
85
+ - test/fixtures/check_child_count.rb
86
+ - test/fixtures/check_looks_like_arg_ip.rb
87
+ - test/fixtures/check_looks_like_arg_regex.rb
88
+ - test/fixtures/check_looks_like_arg_url.rb
89
+ - test/fixtures/check_looks_like_arg_your_mom.rb
90
+ - test/fixtures/check_looks_like_ip.rb
91
+ - test/fixtures/check_looks_like_url.rb
92
+ - test/fixtures/check_regex_assorted.rb
93
+ - test/fixtures/check_regex_literal.rb
94
+ - test/fixtures/check_regex_nil.rb
95
+ - test/fixtures/check_regex_object.rb
96
+ - test/fixtures/check_regex_string.rb
97
+ - test/fixtures/check_required_assorted.rb
98
+ - test/fixtures/check_required_false.rb
99
+ - test/fixtures/check_required_true.rb
100
+ - test/fixtures/check_required_zero.rb
101
+ - test/fixtures/check_type.rb
102
+ - test/fixtures/rules_empty.rb
103
+ - test/fixtures/rules_missing_path.rb
104
+ - test/fixtures/rules_no_check.rb
105
+ - test/fixtures/rules_type_and_min_children.rb
106
+ - test/fixtures/rules_type_check.rb
107
+ - test/unit/attr_set_spec.rb
108
+ - test/unit/check_child_count_spec.rb
109
+ - test/unit/check_looks_like_spec.rb
110
+ - test/unit/check_regex_spec.rb
111
+ - test/unit/check_required_spec.rb
112
+ - test/unit/check_type_spec.rb
113
+ - test/unit/rule_parse_spec.rb
114
+ - test/unit/spec_helper.rb
115
+ homepage: https://github.com/clintoncwolfe/chef-attribute-validator
116
+ licenses:
117
+ - BSD (3-clause)
118
+ metadata: {}
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ! '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ! '>='
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 2.1.5
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: A Rubygem implementing a rule engine for validating Chef node attributes.
139
+ test_files:
140
+ - test/fixtures/attr_set.rb
141
+ - test/fixtures/check_child_count.rb
142
+ - test/fixtures/check_looks_like_arg_ip.rb
143
+ - test/fixtures/check_looks_like_arg_regex.rb
144
+ - test/fixtures/check_looks_like_arg_url.rb
145
+ - test/fixtures/check_looks_like_arg_your_mom.rb
146
+ - test/fixtures/check_looks_like_ip.rb
147
+ - test/fixtures/check_looks_like_url.rb
148
+ - test/fixtures/check_regex_assorted.rb
149
+ - test/fixtures/check_regex_literal.rb
150
+ - test/fixtures/check_regex_nil.rb
151
+ - test/fixtures/check_regex_object.rb
152
+ - test/fixtures/check_regex_string.rb
153
+ - test/fixtures/check_required_assorted.rb
154
+ - test/fixtures/check_required_false.rb
155
+ - test/fixtures/check_required_true.rb
156
+ - test/fixtures/check_required_zero.rb
157
+ - test/fixtures/check_type.rb
158
+ - test/fixtures/rules_empty.rb
159
+ - test/fixtures/rules_missing_path.rb
160
+ - test/fixtures/rules_no_check.rb
161
+ - test/fixtures/rules_type_and_min_children.rb
162
+ - test/fixtures/rules_type_check.rb
163
+ - test/unit/attr_set_spec.rb
164
+ - test/unit/check_child_count_spec.rb
165
+ - test/unit/check_looks_like_spec.rb
166
+ - test/unit/check_regex_spec.rb
167
+ - test/unit/check_required_spec.rb
168
+ - test/unit/check_type_spec.rb
169
+ - test/unit/rule_parse_spec.rb
170
+ - test/unit/spec_helper.rb