puppet-lint-wmf_styleguide-check 1.0.0 → 1.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 827734026504a15ef6e6c2e3003499a467f3a222
4
- data.tar.gz: ba471ad1f7f634ca080cc5c0a1d93e9877343420
3
+ metadata.gz: 9c6f25a5336a67bced26bf736edb5f6281848cf3
4
+ data.tar.gz: 2a94fc2862a3784a34c94423d30339860bd90268
5
5
  SHA512:
6
- metadata.gz: 4460a00a245dbfad6e9105287f76479e7182ade5a47bf6f12a0804983a55cbaee7db026c6cd765e31279f211c8d12a09f4576042c56d6ffdc4f0bafadd268fd4
7
- data.tar.gz: 6877ce5a41f58aa4055fd01b0cdbb151cdf0620fd5336c419e4e1c6c7dcbd16a70c7adea13ca0c992f8b080ba3f5284d8edb39be11c29b6621dcd2ed6bd37a51
6
+ metadata.gz: 4d97adb3bec7f26f5d8c4d1ff141c001ab65d96ca7e399ccc9c7f958bed52efe982fa0daf06a3b8b9f05204152675d6f736ffe46bd49c5e16dc8c35f0f1bb826
7
+ data.tar.gz: c9390bc0b776df8b3c578a5654cb0b597dce8a3287406a8097a01cdcb05fbbd50ba9a6afc143fcd7052c079c45174ee477e0313586b5259a6f7f7ba6ea6c50af
@@ -1,4 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Class to manage puppet resources.
4
+ # See how we extend PuppetLint::Lexer::Token below to understand how we filter
5
+ # tokens within a parsed resource.
2
6
  class PuppetResource
3
7
  attr_accessor :profile_module, :role_module
4
8
 
@@ -49,63 +53,79 @@ class PuppetResource
49
53
  # rubocop:enable Metrics/CyclomaticComplexity, Metrics/MethodLength
50
54
 
51
55
  def params
56
+ # Lazy-load and return all the parameters of the resource
52
57
  @params || parse_params
53
58
  end
54
59
 
55
60
  def profile_module
61
+ # Return the name of the module where profiles are located
56
62
  @profile_module || 'profile'
57
63
  end
58
64
 
59
65
  def role_module
66
+ # Return the name of the module where roles are located
60
67
  @role_module || 'role'
61
68
  end
62
69
 
63
70
  def class?
71
+ # True if this is a class,
64
72
  @resource[:type] == :CLASS
65
73
  end
66
74
 
67
75
  def name
76
+ # Extract a normalized resource name (without the :: prefix if present)
68
77
  @resource[:name_token].value.gsub(/^::/, '')
69
78
  end
70
79
 
71
80
  def path
81
+ # Path of the resource
72
82
  @resource[:path]
73
83
  end
74
84
 
75
85
  def filename
76
- puts @resource
86
+ # File name of the resource
77
87
  @resource[:filename]
78
88
  end
79
89
 
80
90
  def module_name
91
+ # Module containing this resource
81
92
  name.split('::')[0]
82
93
  end
83
94
 
84
95
  def profile?
96
+ # True if the resource is in the profile module
85
97
  class? && (module_name == profile_module)
86
98
  end
87
99
 
88
100
  def role?
101
+ # True if the resource is in the role module
89
102
  class? && (module_name == role_module)
90
103
  end
91
104
 
92
105
  def hiera_calls
106
+ # Returns an array of all the tokens referencing calls to hiera
93
107
  @resource[:tokens].select(&:hiera?)
94
108
  end
95
109
 
96
110
  def included_classes
111
+ # Returns an array of all the classes included (with require/include)
97
112
  @resource[:tokens].map(&:included_class).compact
98
113
  end
99
114
 
100
115
  def declared_classes
116
+ # Returns an array of all the declared classes
101
117
  @resource[:tokens].map(&:declared_class).compact
102
118
  end
103
119
 
104
120
  def declared_resources
121
+ # Returns an array of all the declared classes
105
122
  @resource[:tokens].select(&:declared_type?)
106
123
  end
107
124
 
108
125
  def resource?(name)
126
+ # Arguments:
127
+ # name (string) Name of the resource we want to search
128
+ # Returns an array of all the defines of the specified resource
109
129
  @resource[:tokens].select { |t| t.declared_type? && t.value.gsub(/^::/, '') == name }
110
130
  end
111
131
  end
@@ -116,18 +136,22 @@ class PuppetLint
116
136
  class Token
117
137
  # Extend the basic token with utility functions
118
138
  def function?
139
+ # A function is something that has a name and is followed by a left parens
119
140
  @type == :NAME && @next_code_token.type == :LPAREN
120
141
  end
121
142
 
122
143
  def hiera?
123
- function? && @value == 'hiera'
144
+ # A function call specifically calling hiera
145
+ function? && ['hiera', 'hiera_array', 'hiera_hash', 'lookup'].include?(@value)
124
146
  end
125
147
 
126
148
  def class_include?
149
+ # Object is either "include" or "require", and the next token is not a =>
127
150
  @type == :NAME && ['include', 'require'].include?(@value) && @next_code_token.type != :FARROW
128
151
  end
129
152
 
130
153
  def included_class
154
+ # Fetch the token describing the included class
131
155
  return unless class_include?
132
156
  return @next_code_token.next_code_token if @next_code_token.type == :LPAREN
133
157
  @next_code_token
@@ -141,6 +165,7 @@ class PuppetLint
141
165
  end
142
166
 
143
167
  def declared_type?
168
+ # The token is a name and the next token is a {, while the previous one is not "class"
144
169
  @type == :NAME && @next_code_token.type == :LBRACE && @prev_code_token.type != :CLASS
145
170
  end
146
171
 
@@ -149,6 +174,7 @@ class PuppetLint
149
174
  end
150
175
 
151
176
  def role_keyword?
177
+ # This is a function with name "role"
152
178
  @type == :NAME && @value = 'role' && @next_code_token.type == :LPAREN
153
179
  end
154
180
  end
@@ -198,6 +224,7 @@ def check_define(define)
198
224
  end
199
225
 
200
226
  def hiera(klass)
227
+ # Searches for hiera calls inside classes and defines.
201
228
  hiera_errors(klass.hiera_calls, klass)
202
229
  end
203
230
 
@@ -216,6 +243,9 @@ def params_without_hiera_defaults(klass)
216
243
  end
217
244
 
218
245
  def hiera_not_in_params(klass)
246
+ # Checks if a hiera call is not in a parameter declaration. Used to check profiles
247
+
248
+ # Any hiera call that is not inside a parameter declaration is a violation
219
249
  tokens = klass.hiera_calls.reject do |token|
220
250
  maybe_param = token.prev_code_token.prev_code_token
221
251
  klass.params.keys.include?(maybe_param.value)
@@ -224,7 +254,9 @@ def hiera_not_in_params(klass)
224
254
  end
225
255
 
226
256
  def hiera_errors(tokens, klass)
257
+ # Helper for printing hiera errors nicely
227
258
  tokens.each do |token|
259
+ # hiera ( 'some::label' )
228
260
  value = token.next_code_token.next_code_token.value
229
261
  msg = {
230
262
  message: "wmf-style: Found hiera call in #{klass.type} '#{klass.name}' for '#{value}'",
@@ -236,6 +268,9 @@ def hiera_errors(tokens, klass)
236
268
  end
237
269
 
238
270
  def profile_illegal_include(klass)
271
+ # Check if a profile includes any class that's not allowed there.
272
+ # Allowed are: any other profile, or a class from the passwords module,
273
+ # plus a couple parameter classes
239
274
  modules_include_ok = ['profile', 'passwords']
240
275
  classes_include_ok = ['lvs::configuration', 'network::constants']
241
276
  klass.included_classes.each do |token|
@@ -253,6 +288,7 @@ def profile_illegal_include(klass)
253
288
  end
254
289
 
255
290
  def class_illegal_include(klass)
291
+ # A class should only include classes from the same module.
256
292
  modules_include_ok = [klass.module_name]
257
293
  klass.included_classes.each do |token|
258
294
  class_name = token.value.gsub(/^::/, '')
@@ -268,6 +304,7 @@ def class_illegal_include(klass)
268
304
  end
269
305
 
270
306
  def include_not_profile(klass)
307
+ # Checks that a role only includes other roles, profiles and/or the special class "standard"
271
308
  modules_include_ok = ['role', 'profile', 'standard']
272
309
  klass.included_classes.each do |token|
273
310
  class_name = token.value.gsub(/^::/, '')
@@ -312,6 +349,7 @@ def check_no_system_role(klass)
312
349
  end
313
350
 
314
351
  def check_system_role(klass)
352
+ # Check that a role does indeed declare system::role
315
353
  return if klass.resource?('system::role').length == 1
316
354
  msg = {
317
355
  message: "wmf-style: role '#{klass.name}' should declare system::role once",
@@ -322,6 +360,7 @@ def check_system_role(klass)
322
360
  end
323
361
 
324
362
  def check_no_defines(klass)
363
+ # In a role, check if there is any define apart from one system::role call
325
364
  return if klass.declared_resources == klass.resource?('system::role')
326
365
  msg = {
327
366
  message: "wmf-style: role '#{klass.name}' should not include defines",
@@ -331,6 +370,21 @@ def check_no_defines(klass)
331
370
  notify :error, msg
332
371
  end
333
372
 
373
+ def check_deprecations(resource)
374
+ # Check the resource for declarations of deprecated defines
375
+ deprecated_defines = ['base::service_unit']
376
+ deprecated_defines.each do |deprecated|
377
+ resource.resource?(deprecated).each do |token|
378
+ msg = {
379
+ message: "wmf-style: '#{resource.name}' should not include the deprecated define '#{token.value}'",
380
+ line: token.line,
381
+ column: token.column
382
+ }
383
+ notify :error, msg
384
+ end
385
+ end
386
+ end
387
+
334
388
  # rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/AbcSize, Metrics/CyclomaticComplexity
335
389
  def check_node(node)
336
390
  title = node[:title_tokens].map(&:value).join(', ')
@@ -355,7 +409,7 @@ def check_node(node)
355
409
  line: token.line,
356
410
  column: token.column
357
411
  }
358
- elsif token.declared_type?
412
+ elsif token.declared_type? && token.value != 'interface::add_ip6_mapped'
359
413
  msg = {
360
414
  message: "wmf-style: node '#{title}' declares #{token.value}",
361
415
  line: token.line,
@@ -420,6 +474,7 @@ PuppetLint.new_check(:wmf_styleguide) do
420
474
  else
421
475
  check_class klass
422
476
  end
477
+ check_deprecations klass
423
478
  end
424
479
  end
425
480
 
@@ -428,6 +483,7 @@ PuppetLint.new_check(:wmf_styleguide) do
428
483
  defined_type_indexes.each do |df|
429
484
  define = PuppetResource.new(df)
430
485
  check_define define
486
+ check_deprecations define
431
487
  end
432
488
  node_indexes.each do |node|
433
489
  check_node node
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  class_ok = <<-EOF
@@ -21,7 +23,10 @@ class foo($t=hiera('foo::title')) {
21
23
  EOF
22
24
 
23
25
  profile_ok = <<-EOF
24
- class profile::foobar ($test=hiera('profile::foobar::test')) {
26
+ class profile::foobar (
27
+ $test=hiera('profile::foobar::test'),
28
+ $foo=hiera_array('profile::foobar::foo')
29
+ ) {
25
30
  require ::profile::foo
26
31
  include ::passwords::redis
27
32
  class { '::bar': }
@@ -88,6 +93,12 @@ node 'fixme' {
88
93
  }
89
94
  EOF
90
95
 
96
+ deprecation_ko = <<-EOF
97
+ define test() {
98
+ base::service_unit{ 'test2': }
99
+ }
100
+ EOF
101
+
91
102
  describe 'wmf_styleguide' do
92
103
  context 'class correctly written' do
93
104
  let(:code) { class_ok }
@@ -177,4 +188,11 @@ describe 'wmf_styleguide' do
177
188
  expect(problems).to contain_error("wmf-style: node 'fixme' declares interface::mapped")
178
189
  end
179
190
  end
191
+
192
+ context 'defined type with deprecations' do
193
+ let(:code) { deprecation_ko }
194
+ it 'should not' do
195
+ expect(problems).to contain_error("wmf-style: 'test' should not include the deprecated define 'base::service_unit'")
196
+ end
197
+ end
180
198
  end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'puppet-lint'
2
4
 
3
5
  PuppetLint::Plugins.load_spec_helper
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppet-lint-wmf_styleguide-check
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Giuseppe Lavagetto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-24 00:00:00.000000000 Z
11
+ date: 2018-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: puppet-lint
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 1.3.0
33
+ version: '1.3'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 1.3.0
40
+ version: '1.3'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -84,14 +84,14 @@ dependencies:
84
84
  name: rake
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ">="
87
+ - - "~>"
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ">="
94
+ - - "~>"
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
@@ -118,6 +118,7 @@ description: |2
118
118
  * Check for system::role calls outside of roles
119
119
  * Check for cross-module class inclusion
120
120
  * Check for the use of the include keyword in profiles
121
+ * Check for wmf-deprecated resources usage
121
122
  email: lavagetto@gmail.com
122
123
  executables: []
123
124
  extensions: []
@@ -128,7 +129,7 @@ files:
128
129
  - lib/puppet-lint/plugins/check_wmf_styleguide.rb
129
130
  - spec/puppet-lint/plugins/check_wmf_styleguide_check_spec.rb
130
131
  - spec/spec_helper.rb
131
- homepage: https://github.com/wikimedia/operations-puppet-lint-wmf_styleguide-check
132
+ homepage: https://github.com/lavagetto/puppet-lint-wmf_styleguide-check
132
133
  licenses:
133
134
  - GPL-3.0
134
135
  metadata: {}
@@ -148,7 +149,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
148
149
  version: '0'
149
150
  requirements: []
150
151
  rubyforge_project:
151
- rubygems_version: 2.5.2.1
152
+ rubygems_version: 2.5.2
152
153
  signing_key:
153
154
  specification_version: 4
154
155
  summary: A puppet-lint plugin to check code adheres to the WMF coding guidelines