puppet-lint-wmf_styleguide-check 1.1.1 → 1.1.3

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
  SHA256:
3
- metadata.gz: 899c8c3526bd03b42700fe80c845fc590fc60876ef10410e1216fcab6e4ac7e5
4
- data.tar.gz: 4ed16c3cff59cfa2c72b6dca1af26210daca111ff92e984c999087acd5c44b62
3
+ metadata.gz: d19787901dc3b130c947494d15441a83a3554456f2bb999f7612667d2c01556d
4
+ data.tar.gz: 331e919394e9c138cc432ea935d8fa7b3f632b987a27f84bae5858371a76c493
5
5
  SHA512:
6
- metadata.gz: b550d1bf27ecffec3b069cf131367232a0b65dc8da3c6296054aa6d1c634b655bd291cece4bf6c71fbb4a2e41939f6455c86a8a594e36d1fe486178f24eec3b8
7
- data.tar.gz: d0641b8fd0fe54e5430a1fab4f5e0b40d24d97243ceed0d8195d9faeaa2c57700b91597af050c962b959aed2c0fba6edb4600badc66286d91440df31a57e9110
6
+ metadata.gz: ed879dd47323e5098ba8f8ea7301ff917ffe249209df05fbe1d95194a3409cdcf8c3218fb2473c47dffde5aa46fa663e16bfc5e71187421f3cbceb0a2417ec11
7
+ data.tar.gz: f6552ff1c0da1f1cdad84f76a3316d78674eab5490b0a5c6a16ec734125769c90a7372b7b452cb953af207f11b2406ab510b3544dafdd4fbc5f62bd4f0d22ddb
@@ -190,7 +190,7 @@ class PuppetLint
190
190
  end
191
191
 
192
192
  def node_def?
193
- [:SSTRING, :STRING, :NAME, :REGEX].include?(@type)
193
+ [:SSTRING, :STRING, :NAME, :REGEX, :DEFAULT].include?(@type)
194
194
  end
195
195
 
196
196
  def role_keyword?
@@ -280,7 +280,6 @@ end
280
280
 
281
281
  def lookup_not_in_params(klass)
282
282
  # Checks if a lookup call is not in a parameter declaration. Used to check profiles
283
-
284
283
  # Any lookup call that is not inside a parameter declaration is a violation
285
284
  tokens = klass.lookup_calls.reject do |token|
286
285
  maybe_param = token.prev_code_token.prev_code_token
@@ -494,16 +493,61 @@ PuppetLint.new_check(:wmf_styleguide) do
494
493
  next unless in_node_def
495
494
  case token.type
496
495
  when :REGEX
497
- if !token.value.start_with?('^') || !token.value.end_with?('$')
496
+ unless token.value.start_with?('^')
497
+ msg = {
498
+ message: "wmf-style: node regex must match the start of the hostname with '^' got: #{token.value}",
499
+ line: token.line,
500
+ column: token.column,
501
+ token: token,
502
+ problem: 'node_regex'
503
+ }
504
+ notify :error, msg
505
+ end
506
+ if token.value.end_with?('$')
498
507
  msg = {
499
- message: "wmf-style: regex node matching must match the whole string (^...$), got: #{token.value}",
508
+ message: "wmf-style: node regex must not match the end of the hostname with '$' got: #{token.value}",
500
509
  line: token.line,
501
- column: token.column
510
+ column: token.column,
511
+ token: token,
512
+ problem: 'node_regex'
513
+ }
514
+ notify :error, msg
515
+ end
516
+ ['.wmnet', '.org'].each do |tld|
517
+ next unless token.value.index(tld)
518
+ msg = {
519
+ message: "wmf-style: node regex must not contain the '#{tld}' tld got: #{token.value}",
520
+ line: token.line,
521
+ column: token.column,
522
+ token: token,
523
+ problem: 'node_regex'
524
+ }
525
+ notify :error, msg
526
+ end
527
+ if !token.value.match(/\\?\.wikimedia\\?\.org/) && !token.value.end_with?('\.')
528
+ msg = {
529
+ message: "wmf-style: datacenter node regex must match subdomain end with '\.': #{token.value}",
530
+ line: token.line,
531
+ column: token.column,
532
+ token: token,
533
+ problem: 'node_regex'
502
534
  }
503
535
  notify :error, msg
504
536
  end
505
537
  when :LBRACE
506
- title_tokens = tokens[start + 1..(i - 1)].select(&:node_def?) if braces_level.zero?
538
+ if braces_level.zero?
539
+ title_tokens = tokens[start + 1..(i - 1)].select(&:node_def?)
540
+ unless title_tokens[0].type == :REGEX || title_tokens[0].type == :DEFAULT
541
+ msg = {
542
+ message: "wmf-style: node definition must use a regex, got: #{title_tokens[0].value}",
543
+ line: token.line,
544
+ column: token.column,
545
+ token: token,
546
+ problem: 'node_regex'
547
+ }
548
+ notify :error, msg
549
+ end
550
+ end
507
551
  braces_level += 1
508
552
  when :RBRACE
509
553
  braces_level -= 1
@@ -547,4 +591,20 @@ PuppetLint.new_check(:wmf_styleguide) do
547
591
  check_node node
548
592
  end
549
593
  end
594
+
595
+ # rubocop:disable Metrics/AbcSize
596
+ def fix_node_regex(problem)
597
+ problem[:token].value.insert(0, '^') unless problem[:token].value.start_with?('^')
598
+ problem[:token].value.chop! if problem[:token].value.end_with?('$')
599
+ ['wmnet', 'org'].each do |tld|
600
+ problem[:token].value.slice!(/#{tld}/) if problem[:token].value =~ /\\?\.#{tld}/
601
+ end
602
+ end
603
+ # rubocop:enable Metrics/AbcSize
604
+
605
+ def fix(problem)
606
+ raise PuppetLint::NoFix unless problem[:problem] == 'node_regex'
607
+ method = "fix_#{problem[:problem]}"
608
+ send(method, problem) if respond_to? method
609
+ end
550
610
  end
@@ -88,7 +88,19 @@ define foo::fixme ($a=hiera('something')) {
88
88
  EOF
89
89
 
90
90
  node_ok = <<-EOF
91
- node /^test1.*\\.example\\.com$/ {
91
+ node /^test1.*\\.eqiad\\./ {
92
+ role(spare::system)
93
+ }
94
+ EOF
95
+
96
+ node_ok_wikimedia = <<-EOF
97
+ node /^test1.*\\.wikimedia\\./ {
98
+ role(spare::system)
99
+ }
100
+ EOF
101
+
102
+ node_default = <<-EOF
103
+ node default {
92
104
  role(spare::system)
93
105
  }
94
106
  EOF
@@ -106,20 +118,38 @@ node 'fixme' {
106
118
  }
107
119
  EOF
108
120
 
121
+ node_regex_with_wmf_tld = <<-EOF
122
+ node /^test1.*\\.eqiad\\.wmnet/ {
123
+ role(spare::system)
124
+ }
125
+ EOF
126
+
127
+ node_regex_with_org_tld = <<-EOF
128
+ node /^test1.*\\.wikimedia\\.org/ {
129
+ role(spare::system)
130
+ }
131
+ EOF
132
+
109
133
  node_regex_missing_start = <<-EOF
110
- node /test1.*\\.example\\.com$/ {
134
+ node /test1.*\\.eqiad\\./ {
135
+ role(spare::system)
136
+ }
137
+ EOF
138
+
139
+ node_regex_with_end = <<-EOF
140
+ node /^test1.*\\.eqiad\\.wmnet$/ {
111
141
  role(spare::system)
112
142
  }
113
143
  EOF
114
144
 
115
- node_regex_missing_end = <<-EOF
116
- node /^test1.*\\.example\\.com/ {
145
+ node_regex_fixed = <<-EOF
146
+ node /^test1.*\\.eqiad\\./ {
117
147
  role(spare::system)
118
148
  }
119
149
  EOF
120
150
 
121
- node_regex_missing_both = <<-EOF
122
- node /test1.*\\.example\\.com/ {
151
+ node_regex_fixed_wmf_tld = <<-EOF
152
+ node /^test1.*\\.eqiad\\./ {
123
153
  role(spare::system)
124
154
  }
125
155
  EOF
@@ -236,6 +266,21 @@ describe 'wmf_styleguide' do
236
266
  expect(problems).to have(0).problems
237
267
  end
238
268
  end
269
+
270
+ context 'node wikimedia.org with no errors' do
271
+ let(:code) { node_ok_wikimedia }
272
+ it 'should not detect any problems' do
273
+ expect(problems).to have(0).problems
274
+ end
275
+ end
276
+
277
+ context 'node default with no errors' do
278
+ let(:code) { node_default }
279
+ it 'should not detect any problems' do
280
+ expect(problems).to have(0).problems
281
+ end
282
+ end
283
+
239
284
  context 'node with violations' do
240
285
  let(:code) { node_ko }
241
286
  it 'should not include classes directly' do
@@ -256,24 +301,36 @@ describe 'wmf_styleguide' do
256
301
  it 'should not call hiera_hash' do
257
302
  expect(problems).to contain_error("wmf-style: node 'fixme' calls legacy hiera_hash function")
258
303
  end
304
+ it 'should use a regex' do
305
+ expect(problems).to contain_error('wmf-style: node definition must use a regex, got: fixme')
306
+ end
307
+ end
308
+
309
+ context 'node regex with wmnet tld violation' do
310
+ let(:code) { node_regex_with_wmf_tld }
311
+ it 'should not contain the wmnet tld' do
312
+ expect(problems).to contain_error("wmf-style: node regex must not contain the '.wmnet' tld got: ^test1.*\\.eqiad\\.wmnet")
313
+ end
314
+ end
315
+
316
+ context 'node regex with org tld violation' do
317
+ let(:code) { node_regex_with_org_tld }
318
+ it 'should not contain the wmnet tld' do
319
+ expect(problems).to contain_error("wmf-style: node regex must not contain the '.org' tld got: ^test1.*\\.wikimedia\\.org")
320
+ end
259
321
  end
260
322
 
261
323
  context 'node regex with start violation' do
262
324
  let(:code) { node_regex_missing_start }
263
325
  it 'should start the regex with ^' do
264
- expect(problems).to contain_error('wmf-style: regex node matching must match the whole string (^...$), got: test1.*\\.example\\.com$')
326
+ expect(problems).to contain_error("wmf-style: node regex must match the start of the hostname with '^' got: test1.*\\.eqiad\\.")
265
327
  end
266
328
  end
329
+
267
330
  context 'node regex with end violation' do
268
- let(:code) { node_regex_missing_end }
269
- it 'should end the regex with $' do
270
- expect(problems).to contain_error('wmf-style: regex node matching must match the whole string (^...$), got: ^test1.*\\.example\\.com')
271
- end
272
- end
273
- context 'node regex with start and end violations' do
274
- let(:code) { node_regex_missing_both }
275
- it 'should start the regex with ^ and end it with $' do
276
- expect(problems).to contain_error('wmf-style: regex node matching must match the whole string (^...$), got: test1.*\\.example\\.com')
331
+ let(:code) { node_regex_with_end }
332
+ it 'should not end the regex with $' do
333
+ expect(problems).to contain_error("wmf-style: node regex must not match the end of the hostname with '$' got: ^test1.*\\.eqiad\\.wmnet$")
277
334
  end
278
335
  end
279
336
 
@@ -283,4 +340,27 @@ describe 'wmf_styleguide' do
283
340
  expect(problems).to contain_error("wmf-style: 'test' should not include the deprecated define 'base::service_unit'")
284
341
  end
285
342
  end
343
+
344
+ context 'with fix enabled' do
345
+ before do
346
+ PuppetLint.configuration.fix = true
347
+ end
348
+
349
+ after do
350
+ PuppetLint.configuration.fix = false
351
+ end
352
+
353
+ context 'node regex with start violation' do
354
+ let(:code) { node_regex_missing_start }
355
+ it { expect(manifest).to eq(node_regex_fixed) }
356
+ end
357
+ context 'node regex with end violation' do
358
+ let(:code) { node_regex_with_end }
359
+ it { expect(manifest).to eq(node_regex_fixed) }
360
+ end
361
+ context 'node regex with tld violation' do
362
+ let(:code) { node_regex_with_wmf_tld }
363
+ it { expect(manifest).to eq(node_regex_fixed_wmf_tld) }
364
+ end
365
+ end
286
366
  end
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.1.1
4
+ version: 1.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Giuseppe Lavagetto
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-13 00:00:00.000000000 Z
11
+ date: 2023-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: puppet-lint
@@ -148,7 +148,7 @@ homepage: https://github.com/lavagetto/puppet-lint-wmf_styleguide-check
148
148
  licenses:
149
149
  - GPL-3.0
150
150
  metadata: {}
151
- post_install_message:
151
+ post_install_message:
152
152
  rdoc_options: []
153
153
  require_paths:
154
154
  - lib
@@ -163,8 +163,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
163
163
  - !ruby/object:Gem::Version
164
164
  version: '0'
165
165
  requirements: []
166
- rubygems_version: 3.2.5
167
- signing_key:
166
+ rubygems_version: 3.3.15
167
+ signing_key:
168
168
  specification_version: 4
169
169
  summary: A puppet-lint plugin to check code adheres to the WMF coding guidelines
170
170
  test_files: