require 'spec_helper'

describe 'collectd::plugin::dcpmm', type: :class do
  on_supported_os(baseline_os_hash).each do |os, facts|
    context "on #{os} " do
      let :facts do
        facts
      end

      options = os_specific_options(facts)

      context ':ensure => present, default params' do
        content = <<EOS
# Generated by Puppet
<LoadPlugin dcpmm>
  Globals false
</LoadPlugin>

<Plugin dcpmm>
  Interval 10.0
  CollectHealth false
  CollectPerfMetrics true
  EnableDispatchAll false
</Plugin>

EOS

        it "Will create #{options[:plugin_conf_dir]}/10-dcpmm.conf" do
          is_expected.to compile.with_all_deps
          is_expected.to contain_file('dcpmm.load').with(
            ensure: 'present',
            path: "#{options[:plugin_conf_dir]}/10-dcpmm.conf",
            content: content
          )
        end
      end

      context ':ensure => absent' do
        let :params do
          { ensure: 'absent' }
        end

        it "Will not create #{options[:plugin_conf_dir]}/10-dcpmm.conf" do
          is_expected.to compile.with_all_deps
          is_expected.to contain_file('dcpmm.load').with(
            ensure: 'absent',
            path: "#{options[:plugin_conf_dir]}/10-dcpmm.conf"
          )
        end
      end

      context ':ensure => present and :interval => 3.14' do
        let :params do
          { interval: 3.14 }
        end

        it "Will create #{options[:plugin_conf_dir]}/10-dcpmm.conf" do
          is_expected.to compile.with_all_deps
          is_expected.to contain_file('dcpmm.load').with(
            ensure: 'present',
            path: "#{options[:plugin_conf_dir]}/10-dcpmm.conf",
            content: %r{Interval 3.14}m
          )
        end
      end

      context ':ensure => present, :collect_health => true and :collect_perf_metrics => false' do
        let :params do
          { collect_health: true,
            collect_perf_metrics: false }
        end

        it "Will create #{options[:plugin_conf_dir]}/10-dcpmm.conf" do
          is_expected.to compile.with_all_deps
          is_expected.to contain_file('dcpmm.load').with(
            ensure: 'present',
            path: "#{options[:plugin_conf_dir]}/10-dcpmm.conf",
            content: %r{CollectHealth true}m
          )
        end
      end

      context ':ensure => present and :collect_perf_metrics => false' do
        let :params do
          { collect_perf_metrics: false }
        end

        it "Will create #{options[:plugin_conf_dir]}/10-dcpmm.conf" do
          is_expected.to compile.with_all_deps
          is_expected.to contain_file('dcpmm.load').with(
            ensure: 'present',
            path: "#{options[:plugin_conf_dir]}/10-dcpmm.conf",
            content: %r{CollectPerfMetrics false}m
          )
        end
      end

      context ':ensure => present, :collect_health => false and :collect_perf_metrics => true' do
        let :params do
          { collect_health: true,
            collect_perf_metrics: true }
        end

        it 'Will raise error' do
          is_expected.to compile.and_raise_error(%r{collect_health and collect_perf_metrics cannot be true at the same time.})
        end
      end

      context ':ensure => present and :enable_dispatch_all => true' do
        let :params do
          { enable_dispatch_all: true }
        end

        it 'Will raise error' do
          is_expected.to compile.and_raise_error(%r{enable_dispatch_all is unused at the moment and must always be false.})
        end
      end
    end
  end
end
