Skip to content

Commit a2f1e46

Browse files
committed
Rubocop + fixes
- Enables rubocop - Fixes rubocop ofenses - Fixes broken `facts_to_tags` on A5, which was broken because the tests didn't run because the syntax was not correct (uncovered by rubocop) - Adds a test for `facts_to_tags` on A6/A7.
1 parent a38a497 commit a2f1e46

60 files changed

Lines changed: 4371 additions & 3277 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.rubocop.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,5 @@ Style/IfUnlessModifier:
131131
Enabled: false
132132
Style/SymbolProc:
133133
Enabled: false
134+
Style/NumericPredicate:
135+
Enabled: false

Gemfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ group :development do
2828
gem "puppet-lint", "~> 2.4.2"
2929
gem "metadata-json-lint", "~> 1.2.2"
3030
gem "puppet-syntax", "~> 2.5.0"
31-
gem "rspec-puppet", '2.6.9'
31+
gem "rspec-puppet", "~> 2.6.9"
32+
gem "rubocop", "~> 0.49.1"
33+
gem "rubocop-i18n", "~> 1.2.0"
34+
gem "rubocop-rspec", "~> 1.16.0"
3235
end
3336
end

Rakefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ require 'puppetlabs_spec_helper/rake_tasks'
22
require 'puppet-syntax/tasks/puppet-syntax'
33
require 'puppet_blacksmith/rake_tasks' if Bundler.rubygems.find_name('puppet-blacksmith').any?
44
require 'puppet-lint/tasks/puppet-lint'
5+
require 'rubocop/rake_task'
56

67
exclude_paths = [
78
"pkg/**/*",
@@ -19,5 +20,6 @@ task :test => [
1920
'check:symlinks',
2021
'check:git_ignore',
2122
'check:dot_underscore',
23+
'rubocop',
2224
'spec',
2325
]
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
require 'yaml'
22

33
module Puppet::Parser::Functions
4-
newfunction(:to_instances_yaml, :type => :rvalue) do |args|
5-
init_config = args[0]
6-
instances = args[1]
7-
logs = args[2]
8-
default_values = {
9-
'init_config' => init_config.is_a?(String) ? nil: init_config,
10-
'instances' => instances
11-
}
12-
if !logs.nil? && !logs.empty? then
13-
default_values.merge!({'logs' => logs})
14-
end
15-
YAML::dump(default_values)
4+
newfunction(:to_instances_yaml, type: :rvalue) do |args|
5+
init_config = args[0]
6+
instances = args[1]
7+
logs = args[2]
8+
default_values = {
9+
'init_config' => init_config.is_a?(String) ? nil : init_config,
10+
'instances' => instances,
11+
}
12+
if !logs.nil? && !logs.empty?
13+
default_values['logs'] = logs
1614
end
15+
YAML.dump(default_values)
16+
end
1717
end

lib/puppet/reports/datadog_reports.rb

Lines changed: 45 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33

44
begin
55
require 'dogapi'
6-
rescue LoadError => e
7-
Puppet.info "You need the `dogapi` gem to use the Datadog report (run puppet with puppet_run_reports on your master)"
6+
rescue LoadError
7+
Puppet.info 'You need the `dogapi` gem to use the Datadog report (run puppet with puppet_run_reports on your master)'
88
end
99

1010
Puppet::Reports.register_report(:datadog_reports) do
11-
12-
configfile = "/etc/datadog-agent/datadog-reports.yaml"
11+
configfile = '/etc/datadog-agent/datadog-reports.yaml'
1312
raise(Puppet::ParseError, "Datadog report config file #{configfile} not readable") unless File.readable?(configfile)
1413
config = YAML.load_file(configfile)
1514
API_KEY = config[:datadog_api_key]
@@ -22,7 +21,7 @@
2221
rescue
2322
raise(Puppet::ParseError, "Invalid hostname_extraction_regex #{HOSTNAME_REGEX}")
2423
end
25-
unless HOSTNAME_EXTRACTION_REGEX.named_captures.has_key? "hostname"
24+
unless HOSTNAME_EXTRACTION_REGEX.named_captures.key? 'hostname'
2625
raise(Puppet::ParseError, "hostname_extraction_regex doesn't include a match group named 'hostname': #{HOSTNAME_REGEX}")
2726
end
2827
else
@@ -34,25 +33,22 @@
3433
DESC
3534

3635
def pluralize(number, noun)
37-
begin
38-
if number == 0 then
39-
"no #{noun}"
40-
elsif number < 1 then
41-
"less than 1 #{noun}"
42-
elsif number == 1 then
43-
"1 #{noun}"
44-
else
45-
"#{number.round} #{noun}s"
46-
end
47-
rescue
48-
"#{number} #{noun}(s)"
36+
if number == 0
37+
"no #{noun}"
38+
elsif number < 1
39+
"less than 1 #{noun}"
40+
elsif number == 1
41+
"1 #{noun}"
42+
else
43+
"#{number.round} #{noun}s"
4944
end
45+
rescue
46+
"#{number} #{noun}(s)"
5047
end
5148

52-
5349
def process
54-
@summary = self.summary
55-
@msg_host = self.host
50+
@summary = summary
51+
@msg_host = host
5652
unless HOSTNAME_EXTRACTION_REGEX.nil?
5753
m = @msg_host.match(HOSTNAME_EXTRACTION_REGEX)
5854
if !m.nil? && !m[:hostname].nil?
@@ -65,23 +61,23 @@ def process
6561
event_priority = 'low'
6662
event_data = ''
6763

68-
if defined?(self.status)
64+
if defined?(status)
6965
# for puppet log format 2 and above
70-
@status = self.status
66+
@status = status
7167
if @status == 'failed'
7268
event_title = "Puppet failed on #{@msg_host}"
73-
alert_type = "error"
74-
event_priority = "normal"
69+
alert_type = 'error'
70+
event_priority = 'normal'
7571
elsif @status == 'changed'
7672
event_title = "Puppet changed resources on #{@msg_host}"
77-
alert_type = "success"
78-
event_priority = "normal"
79-
elsif @status == "unchanged"
73+
alert_type = 'success'
74+
event_priority = 'normal'
75+
elsif @status == 'unchanged'
8076
event_title = "Puppet ran on, and left #{@msg_host} unchanged"
81-
alert_type = "success"
77+
alert_type = 'success'
8278
else
8379
event_title = "Puppet ran on #{@msg_host}"
84-
alert_type = "success"
80+
alert_type = 'success'
8581
end
8682

8783
else
@@ -90,49 +86,48 @@ def process
9086
end
9187

9288
# Extract statuses
93-
total_resource_count = self.resource_statuses.length
94-
changed_resources = self.resource_statuses.values.find_all {|s| s.changed }
95-
failed_resources = self.resource_statuses.values.find_all {|s| s.failed }
89+
total_resource_count = resource_statuses.length
90+
changed_resources = resource_statuses.values.select { |s| s.changed }
91+
failed_resources = resource_statuses.values.select { |s| s.failed }
9692

9793
# Little insert if we know the config
98-
config_version_blurb = if defined?(self.configuration_version) then "applied version #{self.configuration_version} and" else "" end
94+
config_version_blurb = defined?(configuration_version) ? "applied version #{configuration_version} and" : ''
9995

10096
event_data << "Puppet #{config_version_blurb} changed #{pluralize(changed_resources.length, 'resource')} out of #{total_resource_count}."
10197

10298
# List changed resources
103-
if changed_resources.length > 0
99+
unless changed_resources.empty?
104100
event_data << "\nThe resources that changed are:\n@@@\n"
105-
changed_resources.each {|s| event_data << "#{s.title} in #{s.file}:#{s.line}\n" }
101+
changed_resources.each { |s| event_data << "#{s.title} in #{s.file}:#{s.line}\n" }
106102
event_data << "\n@@@\n"
107103
end
108104

109105
# List failed resources
110-
if failed_resources.length > 0
106+
unless failed_resources.empty?
111107
event_data << "\nThe resources that failed are:\n@@@\n"
112-
failed_resources.each {|s| event_data << "#{s.title} in #{s.file}:#{s.line}\n" }
108+
failed_resources.each { |s| event_data << "#{s.title} in #{s.file}:#{s.line}\n" }
113109
event_data << "\n@@@\n"
114110
end
115111

116112
Puppet.debug "Sending metrics for #{@msg_host} to Datadog"
117113
@dog = Dogapi::Client.new(API_KEY, nil, nil, nil, nil, nil, API_URL)
118114
@dog.batch_metrics do
119-
self.metrics.each { |metric,data|
120-
data.values.each { |val|
121-
name = "puppet.#{val[1].gsub(/ /, '_')}.#{metric}".downcase
115+
metrics.each do |metric, data|
116+
data.values.each do |val|
117+
name = "puppet.#{val[1].tr(' ', '_')}.#{metric}".downcase
122118
value = val[2]
123-
@dog.emit_point("#{name}", value, :host => "#{@msg_host}")
124-
}
125-
}
119+
@dog.emit_point(name.to_s, value, host: @msg_host.to_s)
120+
end
121+
end
126122
end
127123

128124
Puppet.debug "Sending events for #{@msg_host} to Datadog"
129125
@dog.emit_event(Dogapi::Event.new(event_data,
130-
:msg_title => event_title,
131-
:event_type => 'config_management.run',
132-
:event_object => @msg_host,
133-
:alert_type => alert_type,
134-
:priority => event_priority,
135-
:source_type_name => 'puppet'
136-
), :host => @msg_host)
126+
msg_title: event_title,
127+
event_type: 'config_management.run',
128+
event_object: @msg_host,
129+
alert_type: alert_type,
130+
priority: event_priority,
131+
source_type_name: 'puppet'), host: @msg_host)
137132
end
138133
end

manifests/tag5.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
if is_array($value){
1111
$tags = prefix($value, "${tag_name}:")
12-
datadog_agent::tag{$tags: }
12+
datadog_agent::tag5{$tags: }
1313
} else {
1414
if $value {
1515
concat::fragment{ "datadog tag ${tag_name}:${value}":

spec/classes/datadog_agent_integrations_activemq_xml_spec.rb

Lines changed: 57 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,61 @@
44
context 'supported agents' do
55
ALL_SUPPORTED_AGENTS.each do |agent_major_version|
66
let(:pre_condition) { "class {'::datadog_agent': agent_major_version => #{agent_major_version}}" }
7+
78
if agent_major_version == 5
8-
let(:conf_file) { "/etc/dd-agent/conf.d/activemq_xml.yaml" }
9+
let(:conf_file) { '/etc/dd-agent/conf.d/activemq_xml.yaml' }
910
else
1011
let(:conf_file) { "#{CONF_DIR}/activemq_xml.d/conf.yaml" }
1112
end
1213

1314
context 'with default parameters' do
14-
it { should compile }
15+
it { is_expected.to compile }
1516
end
1617

1718
context 'with password set' do
18-
let(:params) {{
19-
username: 'foo',
20-
password: 'abc123',
21-
}}
19+
let(:params) do
20+
{
21+
username: 'foo',
22+
password: 'abc123',
23+
}
24+
end
2225

23-
it { should compile.with_all_deps }
24-
it { should contain_file(conf_file).with(
25-
owner: DD_USER,
26-
group: DD_GROUP,
27-
mode: PERMISSIONS_PROTECTED_FILE,
28-
)}
29-
it { should contain_file(conf_file).that_requires("Package[#{PACKAGE_NAME}]") }
30-
it { should contain_file(conf_file).that_notifies("Service[#{SERVICE_NAME}]") }
31-
it { should contain_file(conf_file).with_content(/username: foo/) }
32-
it { should contain_file(conf_file).with_content(/password: abc123/) }
26+
it { is_expected.to compile.with_all_deps }
27+
it {
28+
is_expected.to contain_file(conf_file).with(
29+
owner: DD_USER,
30+
group: DD_GROUP,
31+
mode: PERMISSIONS_PROTECTED_FILE,
32+
)
33+
}
34+
it { is_expected.to contain_file(conf_file).that_requires("Package[#{PACKAGE_NAME}]") }
35+
it { is_expected.to contain_file(conf_file).that_notifies("Service[#{SERVICE_NAME}]") }
36+
it { is_expected.to contain_file(conf_file).with_content(%r{username: foo}) }
37+
it { is_expected.to contain_file(conf_file).with_content(%r{password: abc123}) }
3338

3439
context 'with default parameters' do
3540
it {
36-
should contain_file(conf_file)
37-
.with_content(%r{http://localhost:8161})
38-
.with_content(%r{supress_errors: false})
39-
.without_content(%r{detailed_queues:})
40-
.without_content(%r{detailed_topics:})
41-
.without_content(%r{detailed_subscribers:})
41+
is_expected.to contain_file(conf_file)
42+
.with_content(%r{http://localhost:8161})
43+
.with_content(%r{supress_errors: false})
44+
.without_content(%r{detailed_queues:})
45+
.without_content(%r{detailed_topics:})
46+
.without_content(%r{detailed_subscribers:})
4247
}
4348
end
4449

4550
context 'with extra detailed parameters' do
46-
let(:params) {{
47-
supress_errors: true,
48-
detailed_queues: %w(queue1 queue2),
49-
detailed_topics: %w(topic1 topic2),
50-
detailed_subscribers: %w(subscriber1 subscriber2),
51-
}}
51+
let(:params) do
52+
{
53+
supress_errors: true,
54+
detailed_queues: ['queue1', 'queue2'],
55+
detailed_topics: ['topic1', 'topic2'],
56+
detailed_subscribers: ['subscriber1', 'subscriber2'],
57+
}
58+
end
59+
5260
it {
53-
should contain_file(conf_file)
61+
is_expected.to contain_file(conf_file)
5462
.with_content(%r{http://localhost:8161})
5563
.with_content(%r{supress_errors: true})
5664
.with_content(%r{detailed_queues:.*\s+- queue1\s+- queue2})
@@ -60,28 +68,31 @@
6068
end
6169

6270
context 'with instances set' do
63-
let(:params) {{
64-
instances: [
71+
let(:params) do
72+
{
73+
instances: [
6574
{
66-
'url' => 'http://localhost:8161',
67-
'username' => 'joe',
68-
'password' => 'hunter1',
69-
'detailed_queues' => %w(queue1 queue2),
70-
'detailed_topics' => %w(topic1 topic2),
71-
'detailed_subscribers' => %w(subscriber1 subscriber2),
75+
'url' => 'http://localhost:8161',
76+
'username' => 'joe',
77+
'password' => 'hunter1',
78+
'detailed_queues' => ['queue1', 'queue2'],
79+
'detailed_topics' => ['topic1', 'topic2'],
80+
'detailed_subscribers' => ['subscriber1', 'subscriber2'],
7281
},
7382
{
74-
'url' => 'http://remotehost:8162',
75-
'username' => 'moe',
76-
'password' => 'hunter2',
77-
'detailed_queues' => %w(queue3 queue4),
78-
'detailed_topics' => %w(topic3 topic4),
79-
'detailed_subscribers' => %w(subscriber3 subscriber4),
83+
'url' => 'http://remotehost:8162',
84+
'username' => 'moe',
85+
'password' => 'hunter2',
86+
'detailed_queues' => ['queue3', 'queue4'],
87+
'detailed_topics' => ['topic3', 'topic4'],
88+
'detailed_subscribers' => ['subscriber3', 'subscriber4'],
8089
},
81-
],
82-
}}
90+
],
91+
}
92+
end
93+
8394
it {
84-
should contain_file(conf_file)
95+
is_expected.to contain_file(conf_file)
8596
.with_content(%r{url: http://localhost:8161})
8697
.without_content(%r{supress_errors:})
8798
.with_content(%r{username: joe})

0 commit comments

Comments
 (0)