puppet-lint-roles-profiles 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8b945872a2eb66807e4b1d9ecb5f96ad20dc03ce
4
+ data.tar.gz: e5c4b11b9c66a5b85581a3bc7ae48780670d3823
5
+ SHA512:
6
+ metadata.gz: 9e40e268d84f92c85cca3ed00402e02c4a1a6c594e975908e44bcbbaa77406c3bd647c5b2a8afd5b53b8dd59a5d2010c87b8839877ddce048282634976a6ce08
7
+ data.tar.gz: df7b1bdbad9ba0810c5027b7c058c5dbf8e6e135a42cb77a244d0c3aef421459a3ee0fe4e4c6ef20660b9e221491edcb317276de79a7ac3e8667582b40369fb9
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Tim Sharpe
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,77 @@
1
+ # puppet-lint-roles-profiles
2
+
3
+ This plugin validates that the code base adheres to the roles and profiles
4
+ workflow[1].
5
+
6
+ ## Installation
7
+
8
+ To use this plugin, add the following line to the Gemfile in your Puppet code
9
+ base and run `bundle install`.
10
+
11
+ ```ruby
12
+ gem 'puppet-lint-roles-profiles'
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ### roles_include_profiles
18
+
19
+ **--fix-support: No**
20
+
21
+ Will raise a warning if a roles does anything other than `include` `profile`s.
22
+ Resource like class definitions will also raise an error.
23
+
24
+ ```
25
+ WARNING: roles must only include profiles
26
+ ```
27
+
28
+ What you did:
29
+
30
+ ```puppet
31
+ class role::foo {
32
+ class { 'ssh': }
33
+ ```
34
+
35
+ or:
36
+
37
+ ```puppet
38
+ class role::foo {
39
+ class { 'profile::ssh': }
40
+ }
41
+ ```
42
+
43
+ What you should have done:
44
+
45
+ ```
46
+ class role::foo {
47
+ include profile::ssh
48
+ }
49
+ ```
50
+
51
+ ### roles_with_params
52
+
53
+ **--fix-support: No**
54
+
55
+ Will raise a warning when a role has class parameters
56
+
57
+ ```
58
+ Warning: roles must not have any parameters
59
+ ```
60
+
61
+ ### roles_inherits_roles
62
+
63
+ **--fix-support: No**
64
+
65
+ Will raise a warning when a role inherits something other than a role
66
+
67
+ ### nodes_include_one_role
68
+
69
+ **--fix-support: No**
70
+
71
+ Will raise a warning when a node definition contains more than one role
72
+ `include` (except comments).
73
+
74
+ ## References
75
+
76
+ [1] https://puppet.com/docs/pe/2017.2/r_n_p_full_example.html
77
+
@@ -0,0 +1,86 @@
1
+ PuppetLint.new_check(:roles_with_params) do
2
+ def check
3
+ class_indexes.select { |c| c[:name_token].value =~ /^roles?(::|$)/ }.each do |c|
4
+ next if c[:param_tokens].nil?
5
+ # breaks when roles have params that have variables as defaults
6
+ c[:param_tokens].select { |t| t.type == :VARIABLE }.each do |t|
7
+ notify :warning, {
8
+ :message => 'roles must not have parameters',
9
+ :line => t.line,
10
+ :column => t.column,
11
+ }
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ PuppetLint.new_check(:roles_include_profiles) do
18
+ def check
19
+ class_indexes.select { |c| c[:name_token].value =~ /^roles?(::|$)/ }.each do |c|
20
+ # As only `include profile` is allowed any resource is bad
21
+ resource_indexes.select { |r| r[:start] >= c[:start] and r[:end] <= c[:end] }.each do |r|
22
+ notify :warning, {
23
+ :message => "Roles must only `include profiles`",
24
+ :line => r[:type].line,
25
+ :column => r[:type].column,
26
+ }
27
+ end
28
+ # each include must be followed with a profile
29
+ tokens[c[:start]..c[:end]].select { |t| t.value == 'include' }.each do |t|
30
+ unless t.next_code_token.value =~ /^(::)?profiles?(::|$)/
31
+ notify :warning, {
32
+ :message => "Roles must only `include profiles`",
33
+ :line => t.line,
34
+ :column => t.column
35
+ }
36
+ end
37
+ end
38
+ # Conditional logic should be avoided
39
+ tokens[c[:start]..c[:end]].select { |t| [ :IF, :ELSE, :ELSIF, :UNLESS, :CASE].include?(t.type) }.each do |t|
40
+ notify :warning, {
41
+ :message => "Roles must only `include profiles`",
42
+ :line => t.line,
43
+ :column => t.column
44
+ }
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ PuppetLint.new_check(:roles_inherits_roles) do
51
+ def check
52
+ class_indexes.select { |c| c[:name_token].value =~ /^roles?(::|$)/ }.each do |c|
53
+ if c[:inherited_token] and ! (c[:inherited_token].value =~ /^(::)?roles?(::|$)/)
54
+ notify :warning, {
55
+ :message => "Roles must only inherit other roles",
56
+ :line => c[:inherited_token].line,
57
+ :column => c[:inherited_token].column
58
+ }
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ PuppetLint.new_check(:nodes_include_one_role) do
65
+ def check
66
+ node_indexes.each do |n|
67
+ tokens = n[:tokens].select { |t|
68
+ ![ :NEWLINE, :WHITESPACE, :INDENT, :COMMENT, :MLCOMMENT, :SLASH_COMMENT].include?(t.type)
69
+ }
70
+ ibs = tokens.find_index { |t| t.type == :LBRACE }
71
+ # as we have filtered out all whitespace and only allow one role include per node def
72
+ # the only option for the token serie is: [ '{', 'include', 'role', '}' ] and everything
73
+ # else is an error
74
+ unless (tokens[ibs].type == :LBRACE and
75
+ tokens[ibs+1].value == 'include' and
76
+ tokens[ibs+2].type == :NAME and tokens[ibs+2].value =~ /^(::)?roles?(::|$)/ and
77
+ tokens[ibs+3].type == :RBRACE)
78
+ notify :warning, {
79
+ :message => "Nodes must only include roles",
80
+ :line => tokens[ibs+1].line,
81
+ :column => tokens[ibs+1].column,
82
+ }
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'nodes_include_one_role' do
4
+ let(:msg) { 'nodes must only include roles' }
5
+ context 'node with role' do
6
+ let(:code) { 'node /some.machine/ { include role::ssh }' }
7
+ it 'should not be a problem' do
8
+ expect(problems).to have(0).problem
9
+ end
10
+ end
11
+ context 'node with profile' do
12
+ let(:code) { 'node /some.machine/ { include profile::ssh }' }
13
+ it 'should be a problem' do
14
+ expect(problems).to have(1).problem
15
+ end
16
+ end
17
+ context "node with multiple roles" do
18
+ let(:code) do
19
+ <<-EOL
20
+ node 'somemachine' {
21
+ include role::ssh
22
+ include role::ssh2
23
+ }
24
+ EOL
25
+ end
26
+ it 'should be a problem' do
27
+ expect(problems).to have(1).problem
28
+ end
29
+ end
30
+ context "node with multiple inline comments" do
31
+ let(:code) do
32
+ <<-EOL
33
+ node 'somemachine' {
34
+ # good descriptions
35
+ # go here
36
+ include role::ssh
37
+ # and here
38
+ # more
39
+ }
40
+ EOL
41
+ end
42
+ it 'should not be a problem' do
43
+ expect(problems).to have(0).problem
44
+ end
45
+ end
46
+ context "node with comments and multiple includes" do
47
+ let(:code) do
48
+ <<-EOL
49
+ # comments
50
+ node 'somemachine' {
51
+ # good descriptions
52
+ # go here
53
+ include role::ssh
54
+ # and here
55
+ include profile::ssh
56
+ # more
57
+ }
58
+ EOL
59
+ end
60
+ it 'should be a problem' do
61
+ expect(problems).to have(1).problem
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,86 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'roles_include_profiles' do
4
+ let(:msg) { 'roles must only `include profiles`' }
5
+ [ 'role', 'roles' ].each do | rt |
6
+ context "#{rt} without body" do
7
+ let(:code) { "class #{rt}() {}" }
8
+ it 'should not be a problem' do
9
+ expect(problems).to have(0).problem
10
+ end
11
+ end
12
+ context "nested #{rt} without body" do
13
+ let(:code) { "class #{rt}::example() {}" }
14
+ it 'should not be a problem' do
15
+ expect(problems).to have(0).problem
16
+ end
17
+ end
18
+ context "#{rt} including a profile" do
19
+ let(:code) { "class #{rt}() { include profile::example }" }
20
+ it 'should not be a problem' do
21
+ expect(problems).to have(0).problem
22
+ end
23
+ end
24
+ context "#{rt} including a topscope profile" do
25
+ let(:code) { "class #{rt}() { include ::profile::example }" }
26
+ it 'should not be a problem' do
27
+ expect(problems).to have(0).problem
28
+ end
29
+ end
30
+ context "#{rt} resource like profile" do
31
+ let(:code) { "class #{rt}() { class { 'profile::example': } }" }
32
+ it 'should be a problem' do
33
+ expect(problems).to have(1).problem
34
+ end
35
+ end
36
+ context "#{rt} with profiles mixed include and resource-like" do
37
+ let(:code) do
38
+ <<-EOL
39
+ class role::test {
40
+ include profile::abc
41
+ class { 'profile::def': }
42
+ include profile::xyz
43
+ }
44
+ EOL
45
+ end
46
+ it 'should be a problem' do
47
+ expect(problems).to have(1).problem
48
+ end
49
+ end
50
+ context "#{rt} with non profiles mixed include and resource-like" do
51
+ let(:code) do
52
+ <<-EOL
53
+ class role::test {
54
+ include profile::ssh
55
+ include ssh::abc
56
+ class { 'mysql::def': }
57
+ include profile::xyz
58
+ # include xyz
59
+ }
60
+ EOL
61
+ end
62
+ it 'should be a problem' do
63
+ expect(problems).to have(2).problem
64
+ end
65
+ end
66
+ context "#{rt} with conditional logic" do
67
+ let(:code) do
68
+ <<-EOL
69
+ class role::test {
70
+ if $x == 1 {
71
+ include profile::ssh
72
+ } else {
73
+ include ssh::abc
74
+ }
75
+ class { 'mysql::def': }
76
+ include profile::xyz
77
+ # include xyz
78
+ }
79
+ EOL
80
+ end
81
+ it 'should be a problem' do
82
+ expect(problems).to have(4).problem
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'roles_inherits_roles' do
4
+ let(:msg) { 'roles must only `include profiles`' }
5
+ [ 'role', 'roles' ].each do | rt |
6
+ context "#{rt} without inherit" do
7
+ let(:code) { "class #{rt}() {}" }
8
+ it 'should not be a problem' do
9
+ expect(problems).to have(0).problem
10
+ end
11
+ end
12
+ [
13
+ [ "role", 0 ],
14
+ [ "roles", 0 ],
15
+ [ "profile", 1 ],
16
+ [ "ssh", 1 ],
17
+ [ "::role", 0 ],
18
+ [ "::roles", 0 ],
19
+ [ "::role::ssh", 0 ],
20
+ [ "rolex", 1 ],
21
+ ].each do | data |
22
+ context "#{rt} with inherit #{data[0]}" do
23
+ let(:code) { "class #{rt} inherits #{data[0]}{}" }
24
+ it { expect(problems).to have(data[1]).problem }
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'roles_with_params' do
4
+ let(:msg) { 'roles must not have parameters' }
5
+ [ 'role', 'roles' ].each do | rt |
6
+ context "#{rt} without body" do
7
+ let(:code) { "class #{rt}() {}" }
8
+ it 'should not be a problem' do
9
+ expect(problems).to have(0).problem
10
+ end
11
+ end
12
+ context "#{rt} with attr" do
13
+ let(:code) { "class #{rt}($er) {}" }
14
+ it 'should be a problem' do
15
+ expect(problems).to have(1).problem
16
+ expect(problems).to contain_warning(msg).on_line(1)
17
+ end
18
+ end
19
+ context "#{rt} without body" do
20
+ let(:code) { "class #{rt}($er, $er2) {}" }
21
+ it 'should be a problem' do
22
+ expect(problems).to have(2).problem
23
+ end
24
+ end
25
+ context "#{rt} with typed and default attrs" do
26
+ let(:code) do
27
+ <<-EOL
28
+ class role::test (
29
+ $attr,
30
+ String $attr2,
31
+ Optional[Variant[Integer,String]] $attr3 = 'test',
32
+ ) {
33
+ }
34
+ EOL
35
+ end
36
+ it 'should be a problem' do
37
+ expect(problems).to have(3).problem
38
+ expect(problems).to contain_warning(msg).on_line(2)
39
+ expect(problems).to contain_warning(msg).on_line(3)
40
+ expect(problems).to contain_warning(msg).on_line(4)
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ require 'puppet-lint'
2
+
3
+ PuppetLint::Plugins.load_spec_helper
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: puppet-lint-roles-profiles
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Hostnet
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-03-07 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.1'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '3.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '1.1'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '3.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: rspec
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '3.0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '3.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec-its
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rspec-collection_matchers
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '1.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '1.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rake
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ description: |2
90
+ This puppet-lint extension validates that:
91
+ - node definitions only `include` a single role
92
+ - roles only `include` profiles and have no class parameters
93
+ - roles only `inherit` other roles
94
+ email: opensource@hostnet.nl
95
+ executables: []
96
+ extensions: []
97
+ extra_rdoc_files: []
98
+ files:
99
+ - LICENSE
100
+ - README.md
101
+ - lib/puppet-lint/plugins/check_roles_profiles.rb
102
+ - spec/puppet-lint/plugins/check_nodes_include_one_role_spec.rb
103
+ - spec/puppet-lint/plugins/check_roles_include_profiles_spec.rb
104
+ - spec/puppet-lint/plugins/check_roles_inherits_roles_spec.rb
105
+ - spec/puppet-lint/plugins/check_roles_with_params_spec.rb
106
+ - spec/spec_helper.rb
107
+ homepage: https://github.com/hostnet/puppet-lint-roles-profiles
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.2.5
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: puppet-lint check to validate that the codebase adheres to the roles and
131
+ profiles workflow
132
+ test_files:
133
+ - spec/puppet-lint/plugins/check_nodes_include_one_role_spec.rb
134
+ - spec/puppet-lint/plugins/check_roles_include_profiles_spec.rb
135
+ - spec/puppet-lint/plugins/check_roles_inherits_roles_spec.rb
136
+ - spec/puppet-lint/plugins/check_roles_with_params_spec.rb
137
+ - spec/spec_helper.rb