Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate the process hashes for the process integration #177

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions lib/puppet/parser/functions/validate_processes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Function: validate_processes
#
# A function to validate processes for the datadog process integration.
# It validates that the input is an array of process hashes, and that each key
# has the correct data type.
#
# This currently has to be done in a custom function because this module
# supports versions of Puppet < 4.0. This could be done with Puppet iterators,
# so if support for < 4.0 is ever dropped, this should be moved into the
# manifests.
#
# Parameters:
# processes:
# An array of process hashes
#
#
module Puppet::Parser::Functions
newfunction(:validate_processes) do |args|
processes = args[0]

function_validate_array([processes])

processes.each do |process|
function_validate_string([process['name']])
function_validate_array([process['search_string']])
function_validate_bool([process['exact_match']])
end
end
end
2 changes: 1 addition & 1 deletion manifests/integrations/process.pp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
$processes = [],
) inherits datadog_agent::params {

validate_array( $processes )
validate_processes( $processes )

file { "${datadog_agent::params::conf_dir}/process.yaml":
ensure => file,
Expand Down
40 changes: 40 additions & 0 deletions spec/functions/validate_processes_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'spec_helper'

RSpec.describe 'validate_processes' do
context 'with valid input' do
it 'is expected to validate all fields and do nothing' do
params = [
{
'name' => 'my_process',
'search_string' => [
'my_process',
],
'exact_match' => false,
}
]

is_expected.to_not run.with_params(params).and_raise_error(Puppet::ParseError)
end
end

context 'with invalid input' do
it 'is expected to validate all fields and raise Puppet::ParseError' do
params = [
{
'name' => 'my_process',
'search_string' => [
'my_process',
],
'exact_match' => false,
},
{
'name' => 'my_other_process',
'search_string' => 'my_other_process',
'exact_match' => false,
}
]

is_expected.to run.with_params(params).and_raise_error(Puppet::ParseError)
end
end
end