puppet-lint-roles_and_profiles-check 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 21d70ef748ba82a8680ed21f0afb1dbed66ab250
4
+ data.tar.gz: 53ca9b19a35fb6329b75a44fbe31280184bd78e9
5
+ SHA512:
6
+ metadata.gz: 2677efc5e828266cb8230f5aee9fcd15e141aa8f6a19fac3d827ded2c3d98d07e7808bac0fbee6973806796d44e9eedc96a04f39439934fdd324fd1660c405e0
7
+ data.tar.gz: 26ca3d361e718a279d1d996095a54c296154943ea46e12d6f93ef0c18264b960e74b3586a8430cf8b23dca3910048e2ae7d421d24e1351ef7bcc6eb3b877bf01
@@ -0,0 +1,49 @@
1
+ # puppet-lint roles and profiles check
2
+
3
+ Adds a new puppet-lint plugin to verify that your code matches Roles & Profiles
4
+ paradigm.
5
+ This plugin assumes that you role classes starts with ̀`roles`and your
6
+ profiles classes starts with `profiles`.
7
+
8
+ ## Installation
9
+
10
+ To use this plugin, add the following line to the Gemfile in your Puppet code
11
+ base and run `bundle install`.
12
+
13
+ ```ruby
14
+ gem 'puppet-lint-roles_and_profiles-check'
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ This plugin provides a new checks to `puppet-lint`.
20
+
21
+ ### node_definition
22
+
23
+ **--fix support: No**
24
+
25
+ This check will raise a warning is your node definition does not contain only a role declaration.
26
+
27
+ ```
28
+ WARNING: expected only one role declaration
29
+ ```
30
+
31
+ ### roles_class_params
32
+
33
+ **--fix support: No**
34
+
35
+ This check will raise a warning for any parameter in your role definition.
36
+
37
+ ```
38
+ WARNING: expected no class parameters
39
+ ```
40
+
41
+ ### roles_resource_declaration
42
+
43
+ **--fix-support: No**
44
+
45
+ This check will raise a warning for any resource declaration in you role defintion that is not a profile class.
46
+
47
+ ```
48
+ WARNING: expected no resource declaration
49
+ ```
@@ -0,0 +1,64 @@
1
+ PuppetLint.new_check(:node_definition) do
2
+
3
+ def node_indexes
4
+ @node_indexes ||= PuppetLint::Data.definition_indexes(:NODE)
5
+ end
6
+
7
+ def check
8
+ node_indexes.each do |node|
9
+ role_already_declared = false
10
+ resource_indexes.select { |r| r[:start] > node[:start] and r[:end] < node[:end] }.each do |resource|
11
+ if resource[:type].type != :CLASS or !resource[:type].next_code_token.next_code_token.value.start_with?('roles') or role_already_declared == true
12
+ notify :warning, {
13
+ :message => 'expected only one role declaration',
14
+ :line => resource[:type].line,
15
+ :column => resource[:type].column,
16
+ :token => resource,
17
+ }
18
+ end
19
+ role_already_declared = true if resource[:type].type == :CLASS
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ PuppetLint.new_check(:roles_class_params) do
26
+ def check
27
+ class_indexes.select {|c| c[:name_token].value.start_with?('roles')}.each do |klass|
28
+ unless klass[:param_tokens].nil?
29
+ klass[:param_tokens].select {|t|t.type == :VARIABLE }.each do |token|
30
+ notify :warning, {
31
+ :message => 'expected no class parameters',
32
+ :line => token.line,
33
+ :column => token.column,
34
+ }
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ PuppetLint.new_check(:roles_resource_declaration) do
42
+ def check
43
+ class_indexes.select {|c| c[:name_token].value.start_with?('role')}.each do |klass|
44
+ resource_indexes.select { |r| r[:start] > klass[:start] and r[:end] < klass[:end] }.each do |resource|
45
+ if resource[:type].type != :CLASS or !resource[:type].next_code_token.next_code_token.value.start_with?('profiles')
46
+ notify :warning, {
47
+ :message => 'expected no resource declaration',
48
+ :line => resource[:type].line,
49
+ :column => resource[:type].column,
50
+ }
51
+ end
52
+ end
53
+ tokens[klass[:start]..klass[:end]].select { |t| t.value == 'include' }.each do |token|
54
+ if !token.next_code_token.value.start_with?('profiles')
55
+ notify :warning, {
56
+ :message => 'expected no resource declaration',
57
+ :line => token.line,
58
+ :column => token.column,
59
+ }
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,115 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'node_definition' do
4
+ let(:msg) { 'expected only one role declaration' }
5
+
6
+ context 'with no node definition file' do
7
+ let(:code) do
8
+ <<-EOS
9
+ class foo {
10
+ }
11
+ EOS
12
+ end
13
+
14
+ it 'should not detect any problems' do
15
+ expect(problems).to have(0).problems
16
+ end
17
+ end
18
+
19
+ context 'with an empty node definition file' do
20
+ let(:code) do
21
+ <<-EOS
22
+ node 'foo' {
23
+ }
24
+ class foo {
25
+ }
26
+ EOS
27
+ end
28
+
29
+ it 'should not detect any problems' do
30
+ expect(problems).to have(0).problems
31
+ end
32
+ end
33
+
34
+ context 'with a node definition file that declares only a role' do
35
+ let(:code) do
36
+ <<-EOS
37
+ node 'foo' {
38
+ class { 'roles::bar': }
39
+ }
40
+ class roles::bar {
41
+ }
42
+ EOS
43
+ end
44
+
45
+ it 'should not detect any problems' do
46
+ expect(problems).to have(0).problems
47
+ end
48
+ end
49
+
50
+ context 'with a node definition file that declares a class that is not a role' do
51
+ let(:code) do
52
+ <<-EOS
53
+ node 'foo' {
54
+ class { 'profiles::bar': }
55
+ }
56
+ class profiles::bar {
57
+ }
58
+ EOS
59
+ end
60
+
61
+ it 'should detect a single problem' do
62
+ expect(problems).to have(1).problem
63
+ end
64
+
65
+ it 'should create a warning' do
66
+ expect(problems).to contain_warning(msg).on_line(2).in_column(3)
67
+ end
68
+ end
69
+
70
+ context 'with a node definition file that declares two roles' do
71
+ let(:code) do
72
+ <<-EOS
73
+ node 'foo' {
74
+ class { 'roles::bar': }
75
+ class { 'roles::baz': }
76
+ }
77
+ class roles::bar {
78
+ }
79
+ class roles::baz {
80
+ }
81
+ EOS
82
+ end
83
+
84
+ it 'should detect a single problem' do
85
+ expect(problems).to have(1).problem
86
+ end
87
+
88
+ it 'should create a warning' do
89
+ expect(problems).to contain_warning(msg).on_line(3).in_column(3)
90
+ end
91
+ end
92
+
93
+ context 'with a node definition file that declares a resource' do
94
+ let(:code) do
95
+ <<-EOS
96
+ node 'foo' {
97
+ class { 'roles::bar': }
98
+ foo { 'bar': }
99
+ }
100
+ class roles::bar {
101
+ }
102
+ foo {
103
+ }
104
+ EOS
105
+ end
106
+
107
+ it 'should detect a single problem' do
108
+ expect(problems).to have(1).problem
109
+ end
110
+
111
+ it 'should create a warning' do
112
+ expect(problems).to contain_warning(msg).on_line(3).in_column(3)
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,91 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'roles_class_params' do
4
+ let(:msg) { 'expected no class parameters' }
5
+
6
+ context 'when class is not a role' do
7
+ context 'with parameters' do
8
+ let(:code) do
9
+ <<-EOS
10
+ class foo(
11
+ $bar = 'bar',
12
+ ) {
13
+ }
14
+ EOS
15
+ end
16
+
17
+ it 'should not detect any problems' do
18
+ expect(problems).to have(0).problems
19
+ end
20
+ end
21
+ end
22
+
23
+ context 'when class is a role' do
24
+ context 'with no parameters' do
25
+ let(:code) do
26
+ <<-EOS
27
+ class roles::foo {
28
+ }
29
+ EOS
30
+ end
31
+
32
+ it 'should not detect any problems' do
33
+ expect(problems).to have(0).problems
34
+ end
35
+ end
36
+
37
+ context 'with empty parenthesis' do
38
+ let(:code) do
39
+ <<-EOS
40
+ class roles::foo(
41
+ ) {
42
+ }
43
+ EOS
44
+ end
45
+
46
+ it 'should not detect any problems' do
47
+ expect(problems).to have(0).problems
48
+ end
49
+ end
50
+
51
+ context 'with one parameter' do
52
+ let(:code) do
53
+ <<-EOS
54
+ class roles::foo(
55
+ $bar = 'bar',
56
+ ) {
57
+ }
58
+ EOS
59
+ end
60
+
61
+ it 'should detect a single problem' do
62
+ expect(problems).to have(1).problem
63
+ end
64
+
65
+ it 'should create a warning' do
66
+ expect(problems).to contain_warning(msg).on_line(2).in_column(3)
67
+ end
68
+ end
69
+
70
+ context 'with two parameters' do
71
+ let(:code) do
72
+ <<-EOS
73
+ class roles::foo(
74
+ $bar = 'bar',
75
+ $baz = 'baz',
76
+ ) {
77
+ }
78
+ EOS
79
+ end
80
+
81
+ it 'should detect two problems' do
82
+ expect(problems).to have(2).problem
83
+ end
84
+
85
+ it 'should create a warning' do
86
+ expect(problems).to contain_warning(msg).on_line(2).in_column(3)
87
+ expect(problems).to contain_warning(msg).on_line(3).in_column(3)
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,142 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'roles_resource_declaration' do
4
+ let(:msg) { 'expected no resource declaration' }
5
+
6
+ context 'when class is not a role' do
7
+ context 'with parameters' do
8
+ let(:code) do
9
+ <<-EOS
10
+ class foo(
11
+ $bar = 'bar',
12
+ ) {
13
+ baz { 'baz': }
14
+ }
15
+ EOS
16
+ end
17
+
18
+ it 'should not detect any problems' do
19
+ expect(problems).to have(0).problems
20
+ end
21
+ end
22
+ end
23
+
24
+ context 'when class is a role' do
25
+ context 'with no tokens' do
26
+ let(:code) do
27
+ <<-EOS
28
+ class roles::foo {
29
+ }
30
+ EOS
31
+ end
32
+
33
+ it 'should not detect any problems' do
34
+ expect(problems).to have(0).problems
35
+ end
36
+ end
37
+
38
+ context 'with profile declaration' do
39
+ let(:code) do
40
+ <<-EOS
41
+ class roles::foo {
42
+ class { 'profiles::bar': }
43
+ }
44
+ EOS
45
+ end
46
+
47
+ it 'should not detect any problems' do
48
+ expect(problems).to have(0).problems
49
+ end
50
+ end
51
+
52
+ context 'with any class declaration' do
53
+ let(:code) do
54
+ <<-EOS
55
+ class roles::foo {
56
+ class { 'bar': }
57
+ }
58
+ EOS
59
+ end
60
+
61
+ it 'should not detect any problems' do
62
+ expect(problems).to have(1).problems
63
+ end
64
+
65
+ it 'should create a warning' do
66
+ expect(problems).to contain_warning(msg).on_line(2).in_column(3)
67
+ end
68
+ end
69
+
70
+ context 'with profile inclusion' do
71
+ let(:code) do
72
+ <<-EOS
73
+ class roles::foo {
74
+ include profiles::bar
75
+ }
76
+ EOS
77
+ end
78
+
79
+ it 'should not detect any problems' do
80
+ expect(problems).to have(0).problems
81
+ end
82
+ end
83
+
84
+ context 'with any class inclusion' do
85
+ let(:code) do
86
+ <<-EOS
87
+ class roles::foo {
88
+ include bar
89
+ }
90
+ EOS
91
+ end
92
+
93
+ it 'should not detect any problems' do
94
+ expect(problems).to have(1).problems
95
+ end
96
+
97
+ it 'should create a warning' do
98
+ expect(problems).to contain_warning(msg).on_line(2).in_column(3)
99
+ end
100
+ end
101
+
102
+ context 'with one resource' do
103
+ let(:code) do
104
+ <<-EOS
105
+ class roles::foo {
106
+ include profiles::bar
107
+ bar { 'bar': }
108
+ }
109
+ EOS
110
+ end
111
+
112
+ it 'should detect a single problem' do
113
+ expect(problems).to have(1).problem
114
+ end
115
+
116
+ it 'should create a warning' do
117
+ expect(problems).to contain_warning(msg).on_line(3).in_column(3)
118
+ end
119
+ end
120
+
121
+ context 'with two resources' do
122
+ let(:code) do
123
+ <<-EOS
124
+ class roles::foo {
125
+ include profiles::bar
126
+ bar { 'bar': }
127
+ quux { 'quux': }
128
+ }
129
+ EOS
130
+ end
131
+
132
+ it 'should detect two problem' do
133
+ expect(problems).to have(2).problem
134
+ end
135
+
136
+ it 'should create a warning' do
137
+ expect(problems).to contain_warning(msg).on_line(3).in_column(3)
138
+ expect(problems).to contain_warning(msg).on_line(4).in_column(3)
139
+ end
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,3 @@
1
+ require 'puppet-lint'
2
+
3
+ PuppetLint::Plugins.load_spec_helper
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: puppet-lint-roles_and_profiles-check
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mickaël Canévet
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: puppet-lint
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-its
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-collection_matchers
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: |2
84
+ A puppet-lint plugin to check that:
85
+ - a node definition declares only a role,
86
+ - a role class does not have any param and only declares profiles,
87
+ - a profiles class can declare anything but a role.
88
+ email: mickael.canevet@gmail.com
89
+ executables: []
90
+ extensions: []
91
+ extra_rdoc_files: []
92
+ files:
93
+ - README.md
94
+ - lib/puppet-lint/plugins/check_roles_and_profiles.rb
95
+ - spec/puppet-lint/plugins/check_node_definition_spec.rb
96
+ - spec/puppet-lint/plugins/check_roles_class_params_spec.rb
97
+ - spec/puppet-lint/plugins/check_roles_resource_declaration_spec.rb
98
+ - spec/spec_helper.rb
99
+ homepage: https://github.com/mcanevet/puppet-lint-roles_and_profiles-check
100
+ licenses:
101
+ - MIT
102
+ metadata: {}
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 2.2.2
120
+ signing_key:
121
+ specification_version: 4
122
+ summary: A puppet-lint plugin to check some Roles&Profiles bests practices.
123
+ test_files:
124
+ - spec/puppet-lint/plugins/check_roles_class_params_spec.rb
125
+ - spec/puppet-lint/plugins/check_node_definition_spec.rb
126
+ - spec/puppet-lint/plugins/check_roles_resource_declaration_spec.rb
127
+ - spec/spec_helper.rb