Integration resource converges only when something happened.
MODIFY
.kitchen.yml
.kitchen.yml
@@ -18,6 +18,8 @@ platforms:
chef_versions = %w(
12.7.2
13.11.3
+ 14
+ 15
)
test_platforms.product(chef_versions).each do |platform_version, chef_version|
@@ -34,6 +36,8 @@ windows_platform = 'windows-2012r2'
windows_chef_versions = %w(
12.7.2
13.11.3
+ 14
+ 15
)
windows_chef_versions.each do |chef_version|
@@ -563,3 +567,12 @@ suites:
ssl_verify: true
tags: ['_default']
timeout: 20
+
+# Integrations test of the datadog_integration resource
+- name: dd_integration_resource
+ run_list:
+ - recipe[datadog::dd-agent]
+ - recipe[test::dd_integration_resource]
+ attributes:
+ datadog:
+ <<: *DATADOG

MODIFY
Berksfile
Berksfile
@@ -4,4 +4,5 @@ metadata
group :integration do
cookbook 'sudo'
+ cookbook 'test', path: './test/cookbooks/test' # Integration test
end

MODIFY
resources/integration.rb
resources/integration.rb
# The datadog_resource must be used on a system where the datadog-agent has
# already been setup.
-default_action :install
+require 'chef/mixin/shell_out'
-property :name, String, name_attribute: true
+default_action :install
+property :property_name, String, name_property: true
property :version, String, required: true
action :install do
@@ -16,11 +17,24 @@ action :install do
return
end
- Chef::Log.debug("Getting integration #{new_resource.name}")
+ Chef::Log.debug("Getting integration #{new_resource.property_name}")
execute 'integration install' do
- command "\"#{agent_exe_filepath}\" integration install #{new_resource.name}==#{new_resource.version}"
+ command "\"#{agent_exe_filepath}\" integration install #{new_resource.property_name}==#{new_resource.version}"
user 'dd-agent' unless node['platform_family'] == 'windows'
+
+ not_if {
+ output = shell_out("#{agent_exe_filepath} integration show #{new_resource.property_name}").stdout
+ if output.match(/Installed version/) then
+ property_name = output.match(/Package (.+):$/)[1].strip
+ version = output.match(/Installed version: (.+)$/)[1].strip
+ # Same version already installed? Do not reinstall.
+ property_name == new_resource.property_name and version == new_resource.version
+ else
+ # Nothing installed yet, we want to install/
+ false
+ end
+ }
end
end
@@ -30,11 +44,16 @@ action :remove do
return
end
- Chef::Log.debug("Removing integration #{new_resource.name}")
+ Chef::Log.debug("Removing integration #{new_resource.property_name}")
execute 'integration remove' do
- command "\"#{agent_exe_filepath}\" integration remove #{new_resource.name}"
+ command "\"#{agent_exe_filepath}\" integration remove #{new_resource.property_name}"
user 'dd-agent' unless node['platform_family'] == 'windows'
+
+ only_if {
+ output = shell_out("#{agent_exe_filepath} integration show #{new_resource.property_name}").stdout
+ output.match(/Installed version/)
+ }
end
end

ADD
test/integration/dd_integration_resource/serverspec/Gemfile
+../../helpers/serverspec/Gemfile
\ No newline at end of file

ADD
test/integration/dd_integration_resource/serverspec/resource_integration_spec.rb
+# Encoding: utf-8
+
+require 'serverspec'
+
+set :backend, :exec
+set :path, '/sbin:/usr/local/sbin:$PATH'
+
+describe service('datadog-agent') do
+ it { should be_running }
+end
+
+# Checks that it's installed properly and with the good version
+describe command('/opt/datadog-agent/bin/agent/agent integration show datadog-aerospike') do
+ its(:stdout) { should match /Package datadog-aerospike:\nInstalled version:/ }
+end

Integration resource converges only when something happened.