From e370b80ce653063ff8e36a9ff5a4d408ad04e372 Mon Sep 17 00:00:00 2001 From: Juan Pablo Date: Mon, 4 Dec 2023 08:29:59 +0000 Subject: [PATCH 01/18] modified: spec/services/druid_broker_spec.rb --- spec/services/druid_broker_spec.rb | 59 +++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 18 deletions(-) diff --git a/spec/services/druid_broker_spec.rb b/spec/services/druid_broker_spec.rb index 53b1315..c7de662 100644 --- a/spec/services/druid_broker_spec.rb +++ b/spec/services/druid_broker_spec.rb @@ -6,33 +6,56 @@ packages = %w[ redborder-druid cookbook-druid druid ] +service = 'druid-broker' +port = 8084 -describe 'Checking druid-broker' do +describe "Checking packages for #{service}..." do packages.each do |package| describe package(package) do - it { should be_installed } + before do + skip("#{package} is not installed, skipping...") unless package(package).installed? + end + + it 'is expected to be installed' do + expect(package(package).installed?).to be true + end end end +end - describe service('druid-broker') do - it { should be_enabled } - it { should be_running } - end +if service_status == 'enabled' + describe "Checking #{service_status} service for #{service}..." do + describe service(service) do + it { should be_enabled } + it { should be_running } + end + + describe port(port) do + it { should be_listening } + end - describe port(8084) do - it { should be_listening } + describe 'Registered in consul' do + api_endpoint = 'http://localhost:8500/v1' + service_json = command("curl -s #{api_endpoint}/catalog/service/#{service} | jq -r '.[]'").stdout + health = command("curl -s #{api_endpoint}/health/service/#{service} | jq -r '.[].Checks[0].Status'").stdout + health = health.strip + registered = JSON.parse(service_json).key?('Address') && health == 'passing' ? true : false + it 'Should be registered and enabled' do + expect(registered).to be true + end + end end +end + +if service_status == 'disabled' + describe "Checking #{service_status} service for #{service}..." do + describe service(service) do + it { should_not be_enabled } + it { should_not be_running } + end - describe 'Registered in consul' do - service_name = 'druid-broker' - response = "curl -s http://localhost:8500/v1/catalog/service/#{service_name} | jq -r '.[].Address'" - health = "curl -s http://localhost:8500/v1/health/service/#{service_name} | jq -r '.[].Checks' | jq -r '.[].Status'" - service_health = command(health).stdout.split("\n") - ips = command(response).stdout.split("\n") - it 'Should be registered and enabled' do - expect(ips).not_to be_empty - passing_checks = service_health.to_s.chomp - expect(passing_checks).to include('passing') + describe port(port) do + it { should_not be_listening } end end end From 90048136811d7b35d5ec7a200882c90f847eccfe Mon Sep 17 00:00:00 2001 From: vimesa Date: Mon, 4 Dec 2023 09:39:04 +0000 Subject: [PATCH 02/18] Add rsyslog_spec.rb --- spec/services/rsyslog_spec.rb | 92 +++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 spec/services/rsyslog_spec.rb diff --git a/spec/services/rsyslog_spec.rb b/spec/services/rsyslog_spec.rb new file mode 100644 index 0000000..791256d --- /dev/null +++ b/spec/services/rsyslog_spec.rb @@ -0,0 +1,92 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'json' +set :os, family: 'redhat', release: '9', arch: 'x86_64' + +packages = %w[ + rsyslog cookbook-rsyslog +] + +service = 'rsyslog' +config_directory = '/etc/rsyslog.d/' +files = %w[ + 01-server.conf 02-general.conf 20-redborder.conf 99-parse_rfc5424.conf +] +port = 514 + +describe "Checking packages for #{service}..." do + packages.each do |package| + describe package(package) do + before do + skip("#{package} is not installed, skipping...") unless package(package).installed? + end + + it 'is expected to be installed' do + expect(package(package).installed?).to be true + end + end + end +end + +service_status = command("systemctl is-enabled #{service}").stdout +service_status = service_status.strip + +if service_status == 'enabled' + describe "Checking #{service_status} service for #{service}..." do + describe service(service) do + it { should be_enabled } + it { should be_running } + end + + describe file(config_directory) do + it { should exist } + it { should be_directory } + end + + files.each do |file| + describe file("#{config_directory}/#{file}") do + it { should exist } + it { should be_file } + end + end + + describe port(port) do + it { should be_listening } + end + + describe 'Registered in consul' do + api_endpoint = 'http://localhost:8500/v1' + service_json = command("curl -s #{api_endpoint}/catalog/service/#{service} | jq -r '.[]'").stdout + health = command("curl -s #{api_endpoint}/health/service/#{service} | jq -r '.[].Checks[0].Status'").stdout + health = health.strip + registered = JSON.parse(service_json).key?('Address') && health == 'passing' ? true : false + it 'Should be registered and enabled' do + expect(registered).to be true + end + end + end +end + +if service_status == 'disabled' + describe "Checking #{service_status} service for #{service}..." do + describe service(service) do + it { should_not be_enabled } + it { should_not be_running } + end + + describe file(config_directory) do + it { should_not exist } + end + + files.each do |file| + describe file("#{config_directory}/#{file}") do + it { should_not exist } + end + end + + describe port(port) do + it { should_not be_listening } + end + end +end From b63d77651d81e216b6ab72407c20e2cee8b71244 Mon Sep 17 00:00:00 2001 From: JperaltaNic Date: Mon, 4 Dec 2023 10:15:26 +0000 Subject: [PATCH 03/18] Add basic test for druid-middlemanager --- spec/services/druid_middlemanager_spec.rb | 54 +++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 spec/services/druid_middlemanager_spec.rb diff --git a/spec/services/druid_middlemanager_spec.rb b/spec/services/druid_middlemanager_spec.rb new file mode 100644 index 0000000..7bd12d7 --- /dev/null +++ b/spec/services/druid_middlemanager_spec.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +require 'spec_helper' +set :os, family: 'redhat', release: '9', arch: 'x86_64' + +packages = %w[ + redborder-druid cookbook-druid druid +] + +service = 'druid-middlemanager' +port = 8091 + +describe "Checking packages for #{service}..." do + packages.each do |package| + describe package(package) do + before do + skip("#{package} is not installed, skipping...") unless package(package).installed? + end + + it 'is expected to be installed' do + expect(package(package).installed?).to be true + end + end + end +end + +service_status = command("systemctl is-enabled #{service}").stdout +service_status = service_status.strip + +if service_status == 'enabled' + describe "Checking #{service_status} service for #{service}..." do + describe service(service) do + it { should be_enabled } + it { should be_running } + end + + describe port(port) do + it { should be_listening } + end + end +end + +if service_status == 'disabled' + describe "Checking #{service_status} service for #{service}..." do + describe service(service) do + it { should_not be_enabled } + it { should_not be_running } + end + + describe port(port) do + it { should_not be_listening } + end + end +end From 408f3a454aa43583d8be7166510bc493eedd4c54 Mon Sep 17 00:00:00 2001 From: Juan Pablo Date: Mon, 4 Dec 2023 10:20:32 +0000 Subject: [PATCH 04/18] Several changes to improve the tests --- spec/services/druid_coordinator_spec.rb | 21 +++---- spec/services/redborder_postgresql_spec.rb | 50 ++++++++++------ spec/services/zookeeper_spec.rb | 69 ++++++++++++++++------ 3 files changed, 94 insertions(+), 46 deletions(-) diff --git a/spec/services/druid_coordinator_spec.rb b/spec/services/druid_coordinator_spec.rb index c35b765..37b1bde 100644 --- a/spec/services/druid_coordinator_spec.rb +++ b/spec/services/druid_coordinator_spec.rb @@ -6,15 +6,15 @@ packages = %w[ redborder-druid cookbook-druid druid ] - -describe 'Checking druid-coordinator' do +service = 'druid-coordinator' +describe "Checking #{service}" do packages.each do |package| describe package(package) do it { should be_installed } end end - describe service('druid-coordinator') do + describe service(service) do it { should be_enabled } it { should be_running } end @@ -22,17 +22,14 @@ describe port(8084) do it { should be_listening } end - describe 'Registered in consul' do - service_name = 'druid-coordinator' - response = "curl -s http://localhost:8500/v1/catalog/service/#{service_name} | jq -r '.[].Address'" - health = "curl -s http://localhost:8500/v1/health/service/#{service_name} | jq -r '.[].Checks' | jq -r '.[].Status'" - service_health = command(health).stdout.split("\n") - ips = command(response).stdout.split("\n") + api_endpoint = 'http://localhost:8500/v1' + service_json = command("curl -s #{api_endpoint}/catalog/service/#{service} | jq -r '.[]'").stdout + health = command("curl -s #{api_endpoint}/health/service/#{service} | jq -r '.[].Checks[0].Status'").stdout + health = health.strip + registered = JSON.parse(service_json).key?('Address') && health == 'passing' ? true : false it 'Should be registered and enabled' do - expect(ips).not_to be_empty - passing_checks = service_health.to_s.chomp - expect(passing_checks).to include('passing') + expect(registered).to be true end end end diff --git a/spec/services/redborder_postgresql_spec.rb b/spec/services/redborder_postgresql_spec.rb index bdabaa0..dfcfc31 100644 --- a/spec/services/redborder_postgresql_spec.rb +++ b/spec/services/redborder_postgresql_spec.rb @@ -7,31 +7,47 @@ redborder-postgresql postgresql ] -describe 'Cheking redborder-postgresql' do +service = 'redborder-postgresql' +port = 5432 + +describe "Cheking packages for #{service}" do packages.each do |package| describe package(package) do - it { should be_installed } + before do + skip("#{package} is not installed, skipping...") unless package(package).installed? + end + it 'is expected to be installed' do + expect(package(package).installed?).to be true + end end end +end - describe service('postgresql') do - it { should be_enabled } - it { should be_running } - end +service_status = command("systemctl is-enabled #{service}").stdout +service_status = service_status.strip + +if service_status == 'enabled' + describe "Checking #{service_status} service for #{service}..." do + describe service(service) do + it { should be_enabled } + it { should be_running } + end - describe port(5432) do - it { should be_listening } + describe port(port) do + it { should be_listening } + end end +end + +if service_status == 'disabled' + describe "Checking #{service_status} service for #{service}..." do + describe service(service) do + it { should_not be_enabled } + it { should_not be_running } + end - describe 'Registered in consul' do - api_url = 'http://localhost:8500/v1' - service = 'postgresql' - service_json = command("curl -s #{api_url}/catalog/service/#{service} | jq -r '.[]'").stdout - health = command("curl -s #{api_url}/health/service/#{service} | jq -r '.[].Checks[0].Status'").stdout - health = health.strip - registered = JSON.parse(service_json).key?('Address') && health == 'passing' ? true : false - it do - expect(registered).to be true + describe port(port) do + it { should_not be_listening } end end end diff --git a/spec/services/zookeeper_spec.rb b/spec/services/zookeeper_spec.rb index 0b33ada..29ef563 100644 --- a/spec/services/zookeeper_spec.rb +++ b/spec/services/zookeeper_spec.rb @@ -3,27 +3,62 @@ require 'spec_helper' set :os, family: 'redhat', release: '9', arch: 'x86_64' -describe service('zookeeper') do - describe package('zookeeper') do - it { should be_installed } +packages = %w[ + zookeeper libzookeeper cookbook-zookeeper +] + +service = 'zookeeper' +port = 2181 + +describe "Checking packages for #{service}..." do + packages.each do |package| + describe package(package) do + before do + skip("#{package} is not installed, skipping...") unless package(package).installed? + end + + it 'is expected to be installed' do + expect(package(package).installed?).to be true + end + end end - it { should be_enabled } - it { should be_running } +end + +service_status = command("systemctl is-enabled #{service}").stdout +service_status = service_status.strip + +if service_status == 'enabled' + describe "Checking #{service_status} service for #{service}..." do + describe service(service) do + it { should be_enabled } + it { should be_running } + end + + describe port(port) do + it { should be_listening } + end - describe port(2181) do - it { should be_listening } + describe 'Registered in consul' do + api_endpoint = 'http://localhost:8500/v1' + service_json = command("curl -s #{api_endpoint}/catalog/service/#{service} | jq -r '.[]'").stdout + health = command("curl -s #{api_endpoint}/health/service/#{service} | jq -r '.[].Checks[0].Status'").stdout + health = health.strip + registered = JSON.parse(service_json).key?('Address') && health == 'passing' ? true : false + it 'Should be registered and enabled' do + expect(registered).to be true + end + end end end +if service_status == 'disabled' + describe "Checking #{service_status} service for #{service}..." do + describe service(service) do + it { should_not be_enabled } + it { should_not be_running } + end -describe 'Registered in consul' do - service_name = 'zookeeper' - response = "curl -s http://localhost:8500/v1/catalog/service/#{service_name} | jq -r '.[].Address'" - health = "curl -s http://localhost:8500/v1/health/service/#{service_name} | jq -r '.[].Checks' | jq -r '.[].Status'" - service_health = command(health).stdout.split("\n") - ips = command(response).stdout.split("\n") - it 'Should be registered and enabled' do - expect(ips).not_to be_empty - passing_checks = service_health.to_s.chomp - expect(passing_checks).to include('passing') + describe port(port) do + it { should_not be_listening } + end end end From ae7e5c6cef787278631cc4a6420c99554f297fc9 Mon Sep 17 00:00:00 2001 From: vimesa Date: Mon, 4 Dec 2023 10:27:06 +0000 Subject: [PATCH 05/18] Rubocop --- spec/services/rsyslog_spec.rb | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/spec/services/rsyslog_spec.rb b/spec/services/rsyslog_spec.rb index 791256d..3560378 100644 --- a/spec/services/rsyslog_spec.rb +++ b/spec/services/rsyslog_spec.rb @@ -10,8 +10,8 @@ service = 'rsyslog' config_directory = '/etc/rsyslog.d/' -files = %w[ - 01-server.conf 02-general.conf 20-redborder.conf 99-parse_rfc5424.conf +files = %w[ + 01-server.conf 02-general.conf 20-redborder.conf 99-parse_rfc5424.conf ] port = 514 @@ -39,15 +39,17 @@ it { should be_running } end - describe file(config_directory) do - it { should exist } - it { should be_directory } - end - - files.each do |file| - describe file("#{config_directory}/#{file}") do - it { should exist } - it { should be_file } + describe "Configuration files and directories" do + [config_directory, *files.map { |file| "#{config_directory}/#{file}" }].each do |file_path| + describe file(file_path) do + it { should exist } + + if File.directory?(file_path) + it { should be_directory } + else + it { should be_file } + end + end end end From 43f78a8ceced1714314356e59a185f50c3239d0e Mon Sep 17 00:00:00 2001 From: vimesa Date: Mon, 4 Dec 2023 10:31:15 +0000 Subject: [PATCH 06/18] Rubocop --- spec/services/rsyslog_spec.rb | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/spec/services/rsyslog_spec.rb b/spec/services/rsyslog_spec.rb index 3560378..c1431ca 100644 --- a/spec/services/rsyslog_spec.rb +++ b/spec/services/rsyslog_spec.rb @@ -39,16 +39,11 @@ it { should be_running } end - describe "Configuration files and directories" do + describe 'Configuration files and directories' do [config_directory, *files.map { |file| "#{config_directory}/#{file}" }].each do |file_path| describe file(file_path) do it { should exist } - - if File.directory?(file_path) - it { should be_directory } - else - it { should be_file } - end + it { should send(File.directory?(file_path) ? :be_directory : :be_file) } end end end From b4483ef64a2dcb3c79cb309f279527a8635adc67 Mon Sep 17 00:00:00 2001 From: vimesa Date: Mon, 4 Dec 2023 10:33:15 +0000 Subject: [PATCH 07/18] Rubocop --- spec/services/rsyslog_spec.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/services/rsyslog_spec.rb b/spec/services/rsyslog_spec.rb index c1431ca..a7953e9 100644 --- a/spec/services/rsyslog_spec.rb +++ b/spec/services/rsyslog_spec.rb @@ -75,7 +75,6 @@ describe file(config_directory) do it { should_not exist } end - files.each do |file| describe file("#{config_directory}/#{file}") do it { should_not exist } From 5e5ae8a0d970cc0526dce55fc63fe72a8fa0a4b9 Mon Sep 17 00:00:00 2001 From: vimesa Date: Mon, 4 Dec 2023 11:59:39 +0000 Subject: [PATCH 08/18] Rubocop --- spec/services/rsyslog_spec.rb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/spec/services/rsyslog_spec.rb b/spec/services/rsyslog_spec.rb index a7953e9..8dae683 100644 --- a/spec/services/rsyslog_spec.rb +++ b/spec/services/rsyslog_spec.rb @@ -72,12 +72,11 @@ it { should_not be_running } end - describe file(config_directory) do - it { should_not exist } - end - files.each do |file| - describe file("#{config_directory}/#{file}") do - it { should_not exist } + describe 'Configuration files and directories' do + [config_directory, *files.map { |file| "#{config_directory}/#{file}" }].each do |file_path| + describe file(file_path) do + it { should_not exist } + end end end From b6798043f6c4a34d05f124e8b37105e08388963b Mon Sep 17 00:00:00 2001 From: Juan Pablo Date: Mon, 4 Dec 2023 12:08:28 +0000 Subject: [PATCH 09/18] Fix error --- spec/services/druid_broker_spec.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/spec/services/druid_broker_spec.rb b/spec/services/druid_broker_spec.rb index c7de662..125c7c0 100644 --- a/spec/services/druid_broker_spec.rb +++ b/spec/services/druid_broker_spec.rb @@ -23,6 +23,9 @@ end end +service_status = command("systemctl is-enabled #{service}").stdout +service_status = service_status.strip + if service_status == 'enabled' describe "Checking #{service_status} service for #{service}..." do describe service(service) do From 327ce799a9036ef7c33f1d378157f2d66d7487fa Mon Sep 17 00:00:00 2001 From: JuanSheba Date: Mon, 4 Dec 2023 12:10:45 +0000 Subject: [PATCH 10/18] Refactor Druid Historical Serverspec tests with Consul health check --- spec/services/druid_historical_spec.rb | 75 +++++++++++++++++--------- 1 file changed, 51 insertions(+), 24 deletions(-) diff --git a/spec/services/druid_historical_spec.rb b/spec/services/druid_historical_spec.rb index 0a4ae07..c01f294 100644 --- a/spec/services/druid_historical_spec.rb +++ b/spec/services/druid_historical_spec.rb @@ -1,47 +1,74 @@ # frozen_string_literal: true require 'spec_helper' +require 'json' set :os, family: 'redhat', release: '9', arch: 'x86_64' -packages = %w[ - redborder-druid cookbook-druid druid -] +service = 'druid-historical' +port = 8083 -describe 'Checking Druid Historical Service - Basic Checks' do - packages.each do |package| - describe package(package) do - it { should be_installed } +service_status = command("systemctl is-enabled #{service}").stdout.strip + +def service_registered_and_healthy?(service) + api_endpoint = 'http://localhost:8500/v1' + service_json = command("curl -s #{api_endpoint}/catalog/service/#{service} | jq -r '.[]'").stdout + health = command("curl -s #{api_endpoint}/health/service/#{service} | jq -r '.[].Checks[0].Status'").stdout.strip + JSON.parse(service_json).any? && health == 'passing' +end + +service_status = command("systemctl is-enabled #{service}").stdout.strip + +if service_status == 'enabled' + describe 'Druid Historical Service Checks for Enabled Service' do + describe service(service) do + it { should be_enabled } + it { should be_running } end - end - describe service('druid-historical') do - it { should be_enabled } - it { should be_running } - end + describe port(port) do + it { should be_listening } + end - describe 'Configuration' do describe file('/usr/lib/druid/conf/druid/historical/jvm.config') do it { should exist } end + + describe file('/var/log/druid/historical.log') do + its(:content) { should_not match(/ERROR/) } + end + + it 'should be registered and healthy in Consul' do + expect(service_registered_and_healthy?(service)).to be true + end end end -describe 'Checking Druid Historical Service - Advanced Checks' do - describe 'System Dependencies' do - describe package('java-1.8.0-openjdk') do - it { should be_installed } +if service_status == 'disabled' + describe 'Druid Historical Service Checks for Disabled Service' do + describe service(service) do + it 'should not be enabled' do + expect(subject).not_to be_enabled + end + + it 'should not be running' do + expect(subject).not_to be_running + end end - end - describe 'Network and Connectivity' do - describe port(8083) do - it { should be_listening } + describe port(port) do + it 'should not be listening' do + expect(subject).not_to be_listening + end end end +end - describe 'Druid Historical Logs' do - describe file('/var/log/druid/historical.log') do - its(:content) { should_not match(/ERROR/) } +describe 'Checking Druid Historical Dependencies and Configuration' do + %w[redborder-druid cookbook-druid druid java-1.8.0-openjdk].each do |pkg| + describe package(pkg) do + it "#{pkg} should be installed" do + expect(subject).to be_installed + end end end end From 3b29c0ca964a70de15829f4c129d59695eac03da Mon Sep 17 00:00:00 2001 From: JuanSheba Date: Mon, 4 Dec 2023 12:17:00 +0000 Subject: [PATCH 11/18] Fix Linter --- spec/services/druid_historical_spec.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/spec/services/druid_historical_spec.rb b/spec/services/druid_historical_spec.rb index c01f294..694904c 100644 --- a/spec/services/druid_historical_spec.rb +++ b/spec/services/druid_historical_spec.rb @@ -7,8 +7,6 @@ service = 'druid-historical' port = 8083 -service_status = command("systemctl is-enabled #{service}").stdout.strip - def service_registered_and_healthy?(service) api_endpoint = 'http://localhost:8500/v1' service_json = command("curl -s #{api_endpoint}/catalog/service/#{service} | jq -r '.[]'").stdout From 00fc1c615ab8604b4a36ee41a9e28dace8d6a60b Mon Sep 17 00:00:00 2001 From: JuanSheba Date: Mon, 4 Dec 2023 12:42:08 +0000 Subject: [PATCH 12/18] Refactor Webui Serverspec tests with Consul health check --- spec/services/webui_spec.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/spec/services/webui_spec.rb b/spec/services/webui_spec.rb index 29cf909..60dcd15 100644 --- a/spec/services/webui_spec.rb +++ b/spec/services/webui_spec.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'spec_helper' +require 'json' set :os, family: 'redhat', release: '9', arch: 'x86_64' service = 'webui' # Reemplaza con el nombre real de tu servicio web @@ -25,6 +26,17 @@ expect(subject).to be_listening end end + + describe 'Registered in consul' do + api_endpoint = 'http://localhost:8500/v1' + service_json = command("curl -s #{api_endpoint}/catalog/service/#{service} | jq -r '.[]'").stdout + health = command("curl -s #{api_endpoint}/health/service/#{service} | jq -r '.[].Checks[0].Status'").stdout + health = health.strip + registered = JSON.parse(service_json).key?('Address') && health == 'passing' ? true : false + it 'Should be registered and enabled' do + expect(registered).to be true + end + end end end From 180935ccd4d8ab44d5562568c9734b1ecdcedac8 Mon Sep 17 00:00:00 2001 From: JuanSheba Date: Mon, 4 Dec 2023 13:01:38 +0000 Subject: [PATCH 13/18] Refactor Mencached Serverspec tests to check if the service is enabled --- spec/services/memcached_spec.rb | 77 ++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 29 deletions(-) diff --git a/spec/services/memcached_spec.rb b/spec/services/memcached_spec.rb index 8f6ed26..522df06 100644 --- a/spec/services/memcached_spec.rb +++ b/spec/services/memcached_spec.rb @@ -3,45 +3,64 @@ require 'spec_helper' set :os, family: 'redhat', release: '9', arch: 'x86_64' -describe 'Checking Memcached Service - Basic Checks' do - # Verifies the installation of the Memcached package. - describe package('memcached') do - it { should be_installed } - end +service = 'memcached' +service_status = command("systemctl is-enabled #{service}").stdout.strip - # Checks the status of the Memcached service. - describe service('memcached') do - it { should be_enabled } - it { should be_running } - end +if service_status == 'enabled' + describe 'Checking Memcached Service - Basic Checks' do + # Verifies the installation of the Memcached package. + describe package(service) do + it { should be_installed } + end - # Verifies the basic configuration of Memcached. - describe 'Configuration' do - describe file('/usr/lib/sysusers.d/memcached.conf') do - it { should exist } + # Checks the status of the Memcached service. + describe service(service) do + it { should be_enabled } + it { should be_running } end - end -end -describe 'Checking Memcached Service - Advanced Checks' do - # Checks network and connectivity aspects. - describe 'Network and Connectivity' do - describe port(11_211) do - it { should be_listening } + # Verifies the basic configuration of Memcached. + describe 'Configuration' do + describe file('/usr/lib/sysusers.d/memcached.conf') do + it { should exist } + end end end - # Verifies the Memcached logs. - describe 'Memcached Logs' do - describe file('/var/log/memcached.log') do - its(:content) { should_not match(/error/i) } + describe 'Checking Memcached Service - Advanced Checks' do + # Checks network and connectivity aspects. + describe 'Network and Connectivity' do + describe port(11_211) do + it { should be_listening } + end + end + + # Verifies the Memcached logs. + describe 'Memcached Logs' do + describe file('/var/log/memcached.log') do + its(:content) { should_not match(/error/i) } + end + end + + # Verifies the user running the Memcached process. + describe 'Process User' do + describe command("ps aux | grep [m]emcached | awk '{print $1}'") do + its(:stdout) { should match(/memcach\+/) } + end end end +end + +if service_status == 'disabled' + describe 'Memcached Service Checks for Disabled Service' do + describe service(service) do + it 'should not be enabled' do + expect(subject).not_to be_enabled + end - # Verifies the user running the Memcached process. - describe 'Process User' do - describe command("ps aux | grep [m]emcached | awk '{print $1}'") do - its(:stdout) { should match(/memcach\+/) } + it 'should not be running' do + expect(subject).not_to be_running + end end end end From e18f184622203a9cb13edf2a9f8420f361bd2d71 Mon Sep 17 00:00:00 2001 From: JuanSheba Date: Mon, 4 Dec 2023 13:41:19 +0000 Subject: [PATCH 14/18] Added test for check if the service is registered in consul --- spec/services/memcached_spec.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/spec/services/memcached_spec.rb b/spec/services/memcached_spec.rb index 522df06..fa5be64 100644 --- a/spec/services/memcached_spec.rb +++ b/spec/services/memcached_spec.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'spec_helper' +require 'json' set :os, family: 'redhat', release: '9', arch: 'x86_64' service = 'memcached' @@ -25,6 +26,18 @@ it { should exist } end end + + # Check if Memcached is registered and healthy in Consul + describe 'Registered in consul' do + api_endpoint = 'http://localhost:8500/v1' + service_json = command("curl -s #{api_endpoint}/catalog/service/#{service} | jq -r '.[]'").stdout + health = command("curl -s #{api_endpoint}/health/service/#{service} | jq -r '.[].Checks[0].Status'").stdout.strip + registered = JSON.parse(service_json).any? && health == 'passing' + + it 'Should be registered and healthy' do + expect(registered).to be true + end + end end describe 'Checking Memcached Service - Advanced Checks' do From b716d7e2ea9939d4195836d0c5830a083ca4aad7 Mon Sep 17 00:00:00 2001 From: vimesa Date: Mon, 4 Dec 2023 14:03:14 +0000 Subject: [PATCH 15/18] Modify name of service in consul --- spec/services/chef_spec.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/spec/services/chef_spec.rb b/spec/services/chef_spec.rb index 1faea3c..ce3c670 100644 --- a/spec/services/chef_spec.rb +++ b/spec/services/chef_spec.rb @@ -9,6 +9,7 @@ ] service = 'chef-client' +service_in_consul = 'erchef' config_file = '/etc/chef/client.rb' port = 4443 @@ -47,8 +48,8 @@ describe 'Registered in consul' do api_endpoint = 'http://localhost:8500/v1' - service_json = command("curl -s #{api_endpoint}/catalog/service/#{service} | jq -r '.[]'").stdout - health = command("curl -s #{api_endpoint}/health/service/#{service} | jq -r '.[].Checks[0].Status'").stdout + service_json = command("curl -s #{api_endpoint}/catalog/service/#{service_in_consul} | jq -r '.[]'").stdout + health = command("curl -s #{api_endpoint}/health/service/#{service_in_consul} | jq -r '.[].Checks[0].Status'").stdout health = health.strip registered = JSON.parse(service_json).key?('Address') && health == 'passing' ? true : false it 'Should be registered and enabled' do From 3161490d2ac7be87d1fc1fbc48f4cdcb2efb0917 Mon Sep 17 00:00:00 2001 From: JuanSheba Date: Mon, 4 Dec 2023 14:20:35 +0000 Subject: [PATCH 16/18] Added PostgreSQL Serverspec tests with Consul registration check --- spec/services/postgresql_spec.rb | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/spec/services/postgresql_spec.rb b/spec/services/postgresql_spec.rb index 1b9e0a9..c9381bc 100644 --- a/spec/services/postgresql_spec.rb +++ b/spec/services/postgresql_spec.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'spec_helper' +require 'json' set :os, family: 'redhat', release: '9', arch: 'x86_64' service = 'postgresql' @@ -25,14 +26,26 @@ describe port(port) do it { should be_listening } end - end - describe 'Database Connection' do - describe command('psql -U redborder -d redborder -h localhost -c "\\l"') do - databases.each do |db| - its('stdout') { should match(/#{db}/) } + # Check if PostgreSQL is registered and healthy in Consul + describe 'Registered in consul' do + api_endpoint = 'http://localhost:8500/v1' + service_json = command("curl -s #{api_endpoint}/catalog/service/#{service} | jq -r '.[]'").stdout + health = command("curl -s #{api_endpoint}/health/service/#{service} | jq -r '.[].Checks[0].Status'").stdout.strip + registered = JSON.parse(service_json).any? && health == 'passing' + + it 'Should be registered and healthy' do + expect(registered).to be true + end + end + + describe 'Database Connection' do + describe command('psql -U redborder -d redborder -h localhost -c "\\l"') do + databases.each do |db| + its('stdout') { should match(/#{db}/) } + end + its('exit_status') { should eq 0 } end - its('exit_status') { should eq 0 } end end end From aa4a94158e582e323ae189ce1da157531027fcb8 Mon Sep 17 00:00:00 2001 From: JuanSheba Date: Mon, 4 Dec 2023 14:35:25 +0000 Subject: [PATCH 17/18] Fix linter in chef_spec.rb --- spec/services/chef_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/services/chef_spec.rb b/spec/services/chef_spec.rb index ce3c670..39bd538 100644 --- a/spec/services/chef_spec.rb +++ b/spec/services/chef_spec.rb @@ -9,7 +9,7 @@ ] service = 'chef-client' -service_in_consul = 'erchef' +serv_consul = 'erchef' config_file = '/etc/chef/client.rb' port = 4443 @@ -48,7 +48,7 @@ describe 'Registered in consul' do api_endpoint = 'http://localhost:8500/v1' - service_json = command("curl -s #{api_endpoint}/catalog/service/#{service_in_consul} | jq -r '.[]'").stdout + service_json = command("curl -s #{api_endpoint}/catalog/service/#{serv_consul} | jq -r '.[]'").stdout health = command("curl -s #{api_endpoint}/health/service/#{service_in_consul} | jq -r '.[].Checks[0].Status'").stdout health = health.strip registered = JSON.parse(service_json).key?('Address') && health == 'passing' ? true : false From c09c3e095e2e62bc95e0e948816b19f4571ed752 Mon Sep 17 00:00:00 2001 From: JuanSheba Date: Mon, 4 Dec 2023 14:37:27 +0000 Subject: [PATCH 18/18] Fix linter in chef_spec.rb --- spec/services/chef_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/services/chef_spec.rb b/spec/services/chef_spec.rb index 39bd538..75152c9 100644 --- a/spec/services/chef_spec.rb +++ b/spec/services/chef_spec.rb @@ -49,7 +49,7 @@ describe 'Registered in consul' do api_endpoint = 'http://localhost:8500/v1' service_json = command("curl -s #{api_endpoint}/catalog/service/#{serv_consul} | jq -r '.[]'").stdout - health = command("curl -s #{api_endpoint}/health/service/#{service_in_consul} | jq -r '.[].Checks[0].Status'").stdout + health = command("curl -s #{api_endpoint}/health/service/#{serv_consul} | jq -r '.[].Checks[0].Status'").stdout health = health.strip registered = JSON.parse(service_json).key?('Address') && health == 'passing' ? true : false it 'Should be registered and enabled' do