From 0b685823ad65816040d239a62d3222130911f5ac Mon Sep 17 00:00:00 2001 From: Tomas Barton Date: Thu, 25 Aug 2022 10:25:13 +0200 Subject: [PATCH] Remove duplicate code --- spec/classes/telegraf_spec.rb | 56 ++++++++++++++++------------------- 1 file changed, 26 insertions(+), 30 deletions(-) diff --git a/spec/classes/telegraf_spec.rb b/spec/classes/telegraf_spec.rb index 148ae82..d69e395 100644 --- a/spec/classes/telegraf_spec.rb +++ b/spec/classes/telegraf_spec.rb @@ -9,6 +9,20 @@ facts end + let(:config_dir) do + case facts[:osfamily] + when 'Darwin' + '/usr/local/etc/telegraf' + when 'FreeBSD' + '/usr/local/etc' + when 'windows' + 'C:/Program Files/telegraf' + else + '/etc/telegraf' + end + end + let(:main_config) { "#{config_dir}/telegraf.conf" } + context 'default include' it { is_expected.to compile.with_all_deps } it { is_expected.to contain_class('telegraf::config') } @@ -143,6 +157,7 @@ repo_location: 'https://repos.influxdata.com/' ) end + it do is_expected.to contain_class('telegraf').without( %w[ @@ -154,18 +169,9 @@ end end - config_dir = case facts[:kernel] - when 'windows' - 'C:/Program Files/telegraf' - when 'Darwin' - '/usr/local/etc/telegraf' - else - '/etc/telegraf' - end - main_config = "#{config_dir}/telegraf.conf" - it { is_expected.to contain_telegraf__processor('rename_processor') } it { is_expected.to contain_file(main_config).with_ensure('file') } + it { is_expected.to contain_file("#{config_dir}/telegraf.d"). with_purge(false) @@ -173,14 +179,14 @@ it { is_expected.to contain_file("#{config_dir}/telegraf.d/rename_processor.conf").\ - with_content(<<-STRING -[[processors.rename]] -namepass = ["diskio"] -order = 1 -[processors.rename.replace] -dest = "bar" -tag = "foo" - STRING + with_content(<<~STRING + [[processors.rename]] + namepass = ["diskio"] + order = 1 + [processors.rename.replace] + dest = "bar" + tag = "foo" + STRING ) } @@ -305,22 +311,12 @@ when 'windows' is_expected.to contain_package('telegraf').with(ensure: 'absent') end - dir = case facts[:osfamily] - when 'Darwin' - '/usr/local/etc/telegraf' - when 'FreeBSD' - '/usr/local/etc' - when 'windows' - 'C:/Program Files/telegraf' - else - '/etc/telegraf' - end - is_expected.to contain_file("#{dir}/telegraf.conf").with( + is_expected.to contain_file(main_config).with( ensure: 'absent' ) - is_expected.to contain_file("#{dir}/telegraf.d").with( + is_expected.to contain_file("#{config_dir}/telegraf.d").with( ensure: 'absent', force: true )