From 1c6e64d275f47f269c704e00f9809ae297988325 Mon Sep 17 00:00:00 2001 From: Adrian Date: Mon, 15 Aug 2022 10:08:18 -0700 Subject: [PATCH 1/3] (SUP-3557) Add spec tests for the main class --- .fixtures.yml | 1 + manifests/init.pp | 17 ++++- spec/acceptance/install_spec.rb | 6 +- spec/classes/init_spec.rb | 125 ++++++++++++++++++++++++++++++++ 4 files changed, 142 insertions(+), 7 deletions(-) create mode 100644 spec/classes/init_spec.rb diff --git a/.fixtures.yml b/.fixtures.yml index 6b2e03a..8db6b4f 100644 --- a/.fixtures.yml +++ b/.fixtures.yml @@ -4,6 +4,7 @@ fixtures: forge_modules: archive: "puppet/archive" + yumrepo: "puppetlabs/yumrepo_core" repositories: facts: 'https://github.com/puppetlabs/puppetlabs-facts' puppet_agent: 'https://github.com/puppetlabs/puppetlabs-puppet_agent' diff --git a/manifests/init.pp b/manifests/init.pp index c0feb10..e579e14 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -15,7 +15,8 @@ # @param version # Version of InfluxDB to install. Changing this is not recommended. # @param archive_source -# URL containing an InfluxDB archive if not installing from a repository or false to disable installing from source. Defaults to version 2-2.1.1 on amd64 architechture. +# URL containing an InfluxDB archive if not installing from a repository or false to disable installing from source. +# Defaults to version 2-2.1.1 on amd64 architechture. # @param use_ssl # Whether to use http or https connections. Defaults to true (https). # @param manage_ssl @@ -122,9 +123,13 @@ group => 'root', } file { '/var/lib/influxdb': - ensure => directory, - owner => 'influxdb', - group => 'influxdb', + ensure => directory, + owner => 'influxdb', + group => 'influxdb', + require => [ + Group['influxdb'], + User['influxdb'], + ] } $default_dir = $facts['os']['family'] ? { @@ -177,6 +182,10 @@ package {'influxdb2': ensure => installed, } + + service {'influxdb': + ensure => running, + } } if $use_ssl { diff --git a/spec/acceptance/install_spec.rb b/spec/acceptance/install_spec.rb index 339e5c1..4ab9d07 100644 --- a/spec/acceptance/install_spec.rb +++ b/spec/acceptance/install_spec.rb @@ -5,14 +5,14 @@ it 'installs influxdb' do pp = <<-MANIFEST include influxdb - MANIFEST + MANIFEST idempotent_apply(pp) end # Influxdb should be listening on port 8086 by default - describe port('8086') do - it { is_expected.to be_listening } + it 'is listening on port 8086' do + expect(run_shell('ss -Htln sport = :8086').stdout).to match(%r{LISTEN}) end end end diff --git a/spec/classes/init_spec.rb b/spec/classes/init_spec.rb new file mode 100644 index 0000000..f8b52de --- /dev/null +++ b/spec/classes/init_spec.rb @@ -0,0 +1,125 @@ +require 'spec_helper' +require 'pry' + +describe 'influxdb' do + let(:facts) { { os: { family: 'RedHat' }, identity: { user: 'root' } } } + + on_supported_os.each do |os, os_facts| + context "on #{os}" do + let(:facts) { os_facts } + + it { is_expected.to compile } + end + end + + context 'when using default parameters' do + let(:params) { { host: 'localhost' } } + + it { + is_expected.to contain_class('influxdb').with( + host: 'localhost', + port: 8086, + use_ssl: true, + initial_org: 'puppetlabs', + token: nil, + token_file: '/root/.influxdb_token', + ) + + is_expected.to contain_service('influxdb').with_ensure('running') + is_expected.to contain_package('influxdb2').with_ensure('2.1.1') + + ['/etc/influxdb/cert.pem', '/etc/influxdb/ca.pem', '/etc/influxdb/key.pem'].each do |file| + is_expected.to contain_file(file) + end + + is_expected.to contain_influxdb_setup('localhost').with( + ensure: 'present', + token_file: '/root/.influxdb_token', + bucket: 'puppet_data', + org: 'puppetlabs', + username: 'admin', + password: RSpec::Puppet::Sensitive.new('puppetlabs'), + ) + + ['/etc/systemd/system/influxdb.service.d', '/etc/systemd/system/influxdb.service.d/override.conf'].each do |file| + is_expected.to contain_file(file) + end + } + end + + context 'when not using ssl' do + let(:params) { { host: 'localhost', use_ssl: false } } + + it { + is_expected.to contain_class('influxdb').with(use_ssl: false) + + is_expected.not_to contain_file('/etc/influxdb/cert.pem') + is_expected.not_to contain_file('/etc/influxdb/ca.pem') + is_expected.not_to contain_file('/etc/influxdb/key.pem') + } + end + + context 'when using a repository' do + let(:params) { { host: 'localhost' } } + + it { + is_expected.to contain_yumrepo('influxdb2').with( + ensure: 'present', + descr: 'influxdb2', + name: 'influxdb2', + baseurl: 'https://repos.influxdata.com/rhel/$releasever/$basearch/stable', + gpgkey: 'https://repos.influxdata.com/influxdb2.key https://repos.influxdata.com/influxdb.key', + enabled: '1', + gpgcheck: '1', + target: '/etc/yum.repos.d/influxdb2.repo', + ) + + is_expected.not_to contain_archive('/tmp/influxdb.tar.gz') + } + end + + context 'when using an archive source' do + let(:params) { { host: 'localhost', manage_repo: false } } + + it { + is_expected.not_to contain_yumrepo('influxdb2') + + ['/etc/influxdb', '/opt/influxdb', '/etc/influxdb/scripts'].each do |file| + is_expected.to contain_file(file).with( + ensure: 'directory', + owner: 'root', + group: 'root', + ) + end + is_expected.to contain_file('/var/lib/influxdb').with( + ensure: 'directory', + owner: 'influxdb', + group: 'influxdb', + ) + is_expected.to contain_file('/var/lib/influxdb').that_requires(['User[influxdb]', 'Group[influxdb]']) + + ['/etc/influxdb/scripts/influxd-systemd-start.sh', '/etc/systemd/system/influxdb.service'].each do |file| + is_expected.to contain_file(file) + end + + is_expected.to contain_user('influxdb') + is_expected.to contain_group('influxdb') + + is_expected.to contain_archive('/tmp/influxdb.tar.gz').with( + source: 'https://dl.influxdata.com/influxdb/releases/influxdb2-2.1.1-linux-amd64.tar.gz', + ) + is_expected.to contain_archive('/tmp/influxdb.tar.gz').that_requires( + ['File[/etc/influxdb]', 'File[/opt/influxdb]'], + ) + } + end + + context 'when not using a repo or archive source' do + let(:params) { { host: 'localhost', manage_repo: false, archive_source: false } } + + it { + is_expected.not_to contain_yumrepo('influxdb2') + is_expected.not_to contain_archive('/tmp/influxdb.tar.gz') + } + end +end From 3741d1f0d4bcfa68277e2811e211fe309c444f6d Mon Sep 17 00:00:00 2001 From: Vadym Chepkov Date: Mon, 15 Aug 2022 14:30:38 -0400 Subject: [PATCH 2/3] manage service influxdb unconditionally --- manifests/init.pp | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/manifests/init.pp b/manifests/init.pp index e579e14..41b07ff 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -106,14 +106,8 @@ package {'influxdb2': ensure => $version, require => Yumrepo[$repo_name], + before => Service['influxdb'], } - - service {'influxdb': - ensure => running, - enable => true, - require => Package['influxdb2'], - } - } # If not managing the repo, install the package from archive source elsif $archive_source { @@ -150,6 +144,7 @@ source => $archive_source, cleanup => true, require => File['/etc/influxdb', '/opt/influxdb'], + before => Service['influxdb'], } group { 'influxdb': @@ -168,19 +163,13 @@ mode => '0775', notify => Service['influxdb'], } - - service {'influxdb': - ensure => running, - enable => true, - require => File['/opt/influxdb'], - } - } # Otherwise, assume we have a source for the package else { package {'influxdb2': ensure => installed, + before => Service['influxdb'], } service {'influxdb': @@ -188,6 +177,11 @@ } } + service {'influxdb': + ensure => running, + enable => true, + } + if $use_ssl { if $manage_ssl { file {'/etc/influxdb/cert.pem': From eb459a352d2d4175836a5d4c974bb241f830b367 Mon Sep 17 00:00:00 2001 From: Adrian Date: Mon, 15 Aug 2022 11:47:32 -0700 Subject: [PATCH 3/3] Remove duplicate service declaration and pry Also remove unnecessary require --- manifests/init.pp | 18 +++++------------- spec/classes/init_spec.rb | 1 - 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/manifests/init.pp b/manifests/init.pp index 41b07ff..b16c7fe 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -117,13 +117,9 @@ group => 'root', } file { '/var/lib/influxdb': - ensure => directory, - owner => 'influxdb', - group => 'influxdb', - require => [ - Group['influxdb'], - User['influxdb'], - ] + ensure => directory, + owner => 'influxdb', + group => 'influxdb', } $default_dir = $facts['os']['family'] ? { @@ -168,12 +164,8 @@ # Otherwise, assume we have a source for the package else { package {'influxdb2': - ensure => installed, - before => Service['influxdb'], - } - - service {'influxdb': - ensure => running, + ensure => installed, + before => Service['influxdb'], } } diff --git a/spec/classes/init_spec.rb b/spec/classes/init_spec.rb index f8b52de..cb16a97 100644 --- a/spec/classes/init_spec.rb +++ b/spec/classes/init_spec.rb @@ -1,5 +1,4 @@ require 'spec_helper' -require 'pry' describe 'influxdb' do let(:facts) { { os: { family: 'RedHat' }, identity: { user: 'root' } } }