From 6a9d6c5cd5b6d71ed526c3d147fb69d23c8f3a5b Mon Sep 17 00:00:00 2001 From: Nick Le Mouton Date: Mon, 4 Jul 2016 11:57:45 +1200 Subject: [PATCH] Adding memcache integration --- manifests/integrations/memcache.pp | 44 ++++++++++ ...atadog_agent_integrations_memcache_spec.rb | 85 +++++++++++++++++++ templates/agent-conf.d/mcache.yaml.erb | 17 ++++ 3 files changed, 146 insertions(+) create mode 100644 manifests/integrations/memcache.pp create mode 100644 spec/classes/datadog_agent_integrations_memcache_spec.rb create mode 100644 templates/agent-conf.d/mcache.yaml.erb diff --git a/manifests/integrations/memcache.pp b/manifests/integrations/memcache.pp new file mode 100644 index 00000000..93d7d722 --- /dev/null +++ b/manifests/integrations/memcache.pp @@ -0,0 +1,44 @@ +# Class: datadog_agent::integrations::memcache +# +# This class will install the necessary configuration for the memcache integration +# +# Parameters: +# $url: +# url used to connect to the memcached instance +# $port: +# $tags +# Optional array of tags +# +# Sample Usage: +# +# include 'datadog_agent::integrations::memcache' +# +# OR +# +# class { 'datadog_agent::integrations::memcache': +# url => 'localhost', +# } +# +class datadog_agent::integrations::memcache ( + $url = 'localhost', + $port = 11211, + $tags = [], + $items = false, + $slabs = false, +) inherits datadog_agent::params { + include datadog_agent + + validate_string($url) + validate_array($tags) + validate_integer($port) + + file { "${datadog_agent::params::conf_dir}/mcache.yaml": + ensure => file, + owner => $datadog_agent::params::dd_user, + group => $datadog_agent::params::dd_group, + mode => '0600', + content => template('datadog_agent/agent-conf.d/mcache.yaml.erb'), + require => Package[$datadog_agent::params::package_name], + notify => Service[$datadog_agent::params::service_name] + } +} diff --git a/spec/classes/datadog_agent_integrations_memcache_spec.rb b/spec/classes/datadog_agent_integrations_memcache_spec.rb new file mode 100644 index 00000000..6a446599 --- /dev/null +++ b/spec/classes/datadog_agent_integrations_memcache_spec.rb @@ -0,0 +1,85 @@ +require 'spec_helper' + +describe 'datadog_agent::integrations::memcache' do + let(:facts) {{ + operatingsystem: 'Ubuntu', + }} + let(:conf_dir) { '/etc/dd-agent/conf.d' } + let(:dd_user) { 'dd-agent' } + let(:dd_group) { 'root' } + let(:dd_package) { 'datadog-agent' } + let(:dd_service) { 'datadog-agent' } + let(:conf_file) { "#{conf_dir}/mcache.yaml" } + + it { should compile.with_all_deps } + it { should contain_file(conf_file).with( + owner: dd_user, + group: dd_group, + mode: '0600', + )} + it { should contain_file(conf_file).that_requires("Package[#{dd_package}]") } + it { should contain_file(conf_file).that_notifies("Service[#{dd_service}]") } + + context 'with default parameters' do + it { should contain_file(conf_file).with_content(%r{url: localhost}) } + it { should contain_file(conf_file).without_content(/tags:/) } + end + + context 'with parameters set' do + let(:params) {{ + url: 'foobar', + port: '11212', + items: 'true', + slabs: 'true', + tags: %w{foo bar baz}, + }} + it { should contain_file(conf_file).with_content(%r{url: foobar}) } + it { should contain_file(conf_file).with_content(/port: 11212/) } + it { should contain_file(conf_file).with_content(/items: true/) } + it { should contain_file(conf_file).with_content(/slabs: true/) } + end + + context 'with tags parameter single value' do + let(:params) {{ + tags: 'foo', + }} + it { should_not compile } + + skip "this is currently unimplemented behavior" do + it { should contain_file(conf_file).with_content(/tags:\s+- foo\s*?[^-]/m) } + end + end + + context 'with tags parameter array' do + let(:params) {{ + tags: %w{ foo bar baz }, + }} + it { should contain_file(conf_file).with_content(/tags:\s+- foo\s+- bar\s+- baz\s*?[^-]/m) } + end + + context 'with tags parameter empty values' do + context 'mixed in with other tags' do + let(:params) {{ + tags: [ 'foo', '', 'baz' ] + }} + + it { should contain_file(conf_file).with_content(/tags:\s+- foo\s+- baz\s*?[^-]/m) } + end + + context 'single element array of an empty string' do + let(:params) {{ + tags: [''], + }} + + skip("undefined behavior") + end + + context 'single value empty string' do + let(:params) {{ + tags: '', + }} + + skip("doubly undefined behavior") + end + end +end diff --git a/templates/agent-conf.d/mcache.yaml.erb b/templates/agent-conf.d/mcache.yaml.erb new file mode 100644 index 00000000..f2c136ed --- /dev/null +++ b/templates/agent-conf.d/mcache.yaml.erb @@ -0,0 +1,17 @@ +init_config: + +instances: + - url: <%= @url %> + port: <%= @port %> +<% if @tags and ! @tags.empty? -%> + tags: + <%- Array(@tags).each do |tag| -%> + <%- if tag != '' -%> + - <%= tag %> + <%- end -%> + <%- end -%> +<% end -%> + +options: + items: <%= @items %> + slabs: <%= @slabs %>