diff --git a/.rubocop.yml b/.rubocop.yml index 8acea7a..f6799cd 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -9,7 +9,6 @@ AllCops: TargetRubyVersion: 3.0 Exclude: - bin/* - - spec/**/* Gemspec/RequireMFA: Enabled: false @@ -24,15 +23,41 @@ Style/Documentation: Style/RedundantBegin: Enabled: false +Style/BlockDelimiters: + AllowedPatterns: ['expect'] + ########## # LAYOUT # ########## -Layout/EmptyLinesAroundModuleBody: +Layout/LineLength: + Max: 125 + +Layout/EmptyLines: + Enabled: false + +Layout/EmptyLineBetweenDefs: Enabled: false Layout/EmptyLinesAroundClassBody: Enabled: false -Layout/LineLength: - Max: 125 +Layout/EmptyLinesAroundBlockBody: + Enabled: false + +Layout/EmptyLinesAroundModuleBody: + Enabled: false + +Layout/HashAlignment: + EnforcedColonStyle: table + EnforcedHashRocketStyle: table + +######### +# RSPEC # +######### + +RSpec/MultipleExpectations: + Max: 3 + +RSpec/ExampleLength: + Max: 8 diff --git a/spec/icinga2/api/downtime_spec.rb b/spec/icinga2/api/downtime_spec.rb index f3e11ba..fe059a4 100644 --- a/spec/icinga2/api/downtime_spec.rb +++ b/spec/icinga2/api/downtime_spec.rb @@ -1,10 +1,10 @@ +# frozen_string_literal: true + require 'spec_helper' RSpec.describe Icinga2::API::Downtime do - let(:client) { Icinga2::API::Client.new('https://icinga2.example.net:5665', icinga_credentials) } - - subject do + subject(:downtime) do create_downtime('foo.example.net', 'dockerd|daemon') client.hosts .find('foo.example.net') @@ -14,34 +14,36 @@ .first end + let(:client) { Icinga2::API::Client.new('https://icinga2.example.net:5665', icinga_credentials) } + describe '#api_client' do - it 'should return the previous defined client' do + it 'returns the previous defined client' do VCR.use_cassette('single_host_with_downtimes', record: :new_episodes) do - expect(subject.api_client).to eq client + expect(downtime.api_client).to eq client end end end describe '#to_s' do - it 'should return Icinga2 downtime name' do + it 'returns Icinga2 downtime name' do VCR.use_cassette('single_host_with_downtimes', record: :new_episodes) do - expect(subject.to_s).to include('foo.example.net!dockerd|daemon') + expect(downtime.to_s).to include('foo.example.net!dockerd|daemon') end end end describe '#full_name' do - it 'should return Icinga2 downtime full_name' do + it 'returns Icinga2 downtime full_name' do VCR.use_cassette('single_host_with_downtimes', record: :new_episodes) do - expect(subject.full_name).to include('foo.example.net!dockerd|daemon') + expect(downtime.full_name).to include('foo.example.net!dockerd|daemon') end end end describe '#cancel' do - it 'should cancel Icinga2 downtime' do + it 'cancels Icinga2 downtime' do VCR.use_cassette('single_host_cancel_downtimes', record: :new_episodes) do - expect(subject.cancel).to be_a(Hash) + expect(downtime.cancel).to be_a(Hash) end end end diff --git a/spec/icinga2/api/host_spec.rb b/spec/icinga2/api/host_spec.rb index 9e64d21..3f6666a 100644 --- a/spec/icinga2/api/host_spec.rb +++ b/spec/icinga2/api/host_spec.rb @@ -1,52 +1,53 @@ +# frozen_string_literal: true + require 'spec_helper' RSpec.describe Icinga2::API::Host do - let(:client) { Icinga2::API::Client.new('https://icinga2.example.net:5665', icinga_credentials) } + subject(:host) { client.hosts.find('foo.example.net') } - subject { client.hosts.find('foo.example.net') } + let(:client) { Icinga2::API::Client.new('https://icinga2.example.net:5665', icinga_credentials) } describe '#api_client' do - it 'should return the previous defined client' do + it 'returns the previous defined client' do VCR.use_cassette('single_host') do - expect(subject.api_client).to eq client + expect(host.api_client).to eq client end end end describe '#to_s' do - it 'should return Icinga2 host name' do + it 'returns Icinga2 host name' do VCR.use_cassette('single_host') do - expect(subject.to_s).to eq 'foo.example.net' + expect(host.to_s).to eq 'foo.example.net' end end end describe '#to_h' do - it 'should return host attributes as a hash' do + it 'returns host attributes as a hash' do VCR.use_cassette('single_host') do - expect(subject.to_h).to be_a(Hash) - expect(subject.to_h[:name]).to eq 'foo.example.net' + expect(host.to_h).to be_a(Hash) + expect(host.to_h[:name]).to eq 'foo.example.net' end end end describe '#name' do - it 'should return Icinga2 host name' do + it 'returns Icinga2 host name' do VCR.use_cassette('single_host') do - expect(subject.to_s).to eq 'foo.example.net' + expect(host.name).to eq 'foo.example.net' end end end describe '#services' do - it 'should return Icinga2 host services' do + it 'returns Icinga2 host services' do VCR.use_cassette('single_host_with_services') do - expect(subject.services).to be_a(Icinga2::API::Services) - expect(subject.services.host).to eq subject - expect(subject.services.api_client).to eq subject.api_client + expect(host.services).to be_a(Icinga2::API::Services) + expect(host.services.host).to eq host + expect(host.services.api_client).to eq host.api_client end end end - end diff --git a/spec/icinga2/api/hosts_spec.rb b/spec/icinga2/api/hosts_spec.rb index d3e19c0..96aad1a 100644 --- a/spec/icinga2/api/hosts_spec.rb +++ b/spec/icinga2/api/hosts_spec.rb @@ -1,23 +1,25 @@ +# frozen_string_literal: true + require 'spec_helper' RSpec.describe Icinga2::API::Hosts do - let(:client) { Icinga2::API::Client.new('https://icinga2.example.net:5665', icinga_credentials) } + subject(:all_hosts) { client.hosts } - subject { client.hosts } + let(:client) { Icinga2::API::Client.new('https://icinga2.example.net:5665', icinga_credentials) } describe '#api_client' do - it 'should return the previous defined client' do - expect(subject.api_client).to eq client + it 'returns the previous defined client' do + expect(all_hosts.api_client).to eq client end end describe '#all' do - it 'should return all Icinga2 hosts' do + it 'returns all Icinga2 hosts' do VCR.use_cassette('all_hosts') do # Store the request result to not trigger it # multiple times - hosts = subject.all + hosts = all_hosts.all host1 = hosts.first host2 = hosts.last @@ -30,11 +32,11 @@ describe '#find' do context 'when Icinga2 host exist' do - it 'should return host object' do + it 'returns host object' do VCR.use_cassette('find_existing_host') do # Store the request result to not trigger it # multiple times - host = subject.find('foo.example.net') + host = all_hosts.find('foo.example.net') expect(host).to be_a(Icinga2::API::Host) expect(host.to_s).to eq 'foo.example.net' @@ -44,11 +46,11 @@ end context 'when Icinga2 host dont exist' do - it 'should return nil' do + it 'returns nil' do VCR.use_cassette('find_null_host') do # Store the request result to not trigger it # multiple times - host = subject.find('baz.example.net') + host = all_hosts.find('baz.example.net') expect(host).to be_nil end end diff --git a/spec/icinga2/api/resource_spec.rb b/spec/icinga2/api/resource_spec.rb index 4cf3183..9465f70 100644 --- a/spec/icinga2/api/resource_spec.rb +++ b/spec/icinga2/api/resource_spec.rb @@ -1,55 +1,57 @@ +# frozen_string_literal: true + require 'spec_helper' RSpec.describe Icinga2::API::Resource do - let(:client) { Icinga2::API::Client.new('https://icinga2.example.net:5665', icinga_credentials) } + subject(:resource) { described_class.new(api_client: client.api, foo: 'bar', bar: 'foo') } - subject { described_class.new(api_client: client.api, foo: 'bar', bar: 'foo') } + let(:client) { Icinga2::API::Client.new('https://icinga2.example.net:5665', icinga_credentials) } describe '#to_yaml_properties' do - it 'should return the list of attributes to render in the YAML dump' do - expect(subject.to_yaml_properties).to eq [:@attributes] + it 'returns the list of attributes to render in the YAML dump' do + expect(resource.to_yaml_properties).to eq [:@attributes] end end describe '#to_hash' do context 'when no option is passed' do - it 'should return a hash of stored attributes' do - expect(subject.to_hash).to eq({ foo: 'bar', bar: 'foo' }) + it 'returns a hash of stored attributes' do + expect(resource.to_hash).to eq({ foo: 'bar', bar: 'foo' }) end end context 'when except option is passed' do - it 'should return a hash of stored attributes' do - expect(subject.to_hash(except: ['foo'])).to eq({ bar: 'foo' }) + it 'returns a hash of stored attributes' do + expect(resource.to_hash(except: ['foo'])).to eq({ bar: 'foo' }) end end context 'when only option is passed' do - it 'should return a hash of stored attributes' do - expect(subject.to_hash(only: ['foo'])).to eq({ foo: 'bar' }) + it 'returns a hash of stored attributes' do + expect(resource.to_hash(only: ['foo'])).to eq({ foo: 'bar' }) end end end describe '#dynamic_method' do - it 'should return stored attributes' do - expect(subject.foo).to eq 'bar' - expect(subject.bar).to eq 'foo' + it 'returns stored attributes' do + expect(resource.foo).to eq 'bar' + expect(resource.bar).to eq 'foo' end end describe '#method_missing' do context 'when method exist' do - it 'should return the value' do - expect(subject.foo).to eq 'bar' + it 'returns the value' do + expect(resource.foo).to eq 'bar' end end context 'when method dont exist' do - it 'should call super (and raise error)' do + it 'calls super (and raise error)' do expect { - subject.baz + resource.baz }.to raise_error(NoMethodError) end end @@ -57,14 +59,14 @@ describe '#respond_to_missing?' do context 'when method exist' do - it 'should return true' do - expect(subject.respond_to?(:foo)).to be true + it 'returns true' do + expect(resource.respond_to?(:foo)).to be true end end context 'when method dont exist' do - it 'should return false' do - expect(subject.respond_to?(:baz)).to be false + it 'returns false' do + expect(resource.respond_to?(:baz)).to be false end end end diff --git a/spec/icinga2/api/service_spec.rb b/spec/icinga2/api/service_spec.rb index 2c3567d..225ecde 100644 --- a/spec/icinga2/api/service_spec.rb +++ b/spec/icinga2/api/service_spec.rb @@ -1,49 +1,51 @@ +# frozen_string_literal: true + require 'spec_helper' RSpec.describe Icinga2::API::Service do - let(:client) { Icinga2::API::Client.new('https://icinga2.example.net:5665', icinga_credentials) } + subject(:service) { client.hosts.find('foo.example.net').services.find('dockerd|daemon') } - subject { client.hosts.find('foo.example.net').services.find('dockerd|daemon') } + let(:client) { Icinga2::API::Client.new('https://icinga2.example.net:5665', icinga_credentials) } describe '#api_client' do - it 'should return the previous defined client' do + it 'returns the previous defined client' do VCR.use_cassette('single_host_with_services', record: :new_episodes) do - expect(subject.api_client).to eq client + expect(service.api_client).to eq client end end end describe '#to_s' do - it 'should return Icinga2 service name' do + it 'returns Icinga2 service name' do VCR.use_cassette('single_host_with_services', record: :new_episodes) do - expect(subject.to_s).to eq 'foo.example.net!dockerd|daemon' + expect(service.to_s).to eq 'foo.example.net!dockerd|daemon' end end end describe '#to_h' do - it 'should return service attributes as a hash' do + it 'returns service attributes as a hash' do VCR.use_cassette('single_host_with_services', record: :new_episodes) do - expect(subject.to_h).to be_a(Hash) - expect(subject.to_h[:name]).to eq 'dockerd|daemon' + expect(service.to_h).to be_a(Hash) + expect(service.to_h[:name]).to eq 'dockerd|daemon' end end end describe '#full_name' do - it 'should return Icinga2 service full_name' do + it 'returns Icinga2 service full_name' do VCR.use_cassette('single_host_with_services', record: :new_episodes) do - expect(subject.full_name).to eq 'foo.example.net!dockerd|daemon' + expect(service.full_name).to eq 'foo.example.net!dockerd|daemon' end end end describe '#downtimes' do context 'when Icinga2 no downtimes exist' do - it 'should return empty array' do + it 'returns empty array' do VCR.use_cassette('service_without_downtimes') do - downtimes = subject.downtimes + downtimes = service.downtimes expect(downtimes).to be_a(Array) expect(downtimes.size).to eq 0 end @@ -51,10 +53,10 @@ end context 'when Icinga2 downtimes exist' do - it 'should return all Icinga2 downtimes for the service' do + it 'returns all Icinga2 downtimes for the service' do VCR.use_cassette('service_with_downtimes', record: :new_episodes) do create_downtime('foo.example.net', 'dockerd|daemon') - downtimes = subject.downtimes + downtimes = service.downtimes expect(downtimes.size).to eq 1 expect(downtimes.first.to_s).to include('foo.example.net!dockerd|daemon') @@ -62,5 +64,4 @@ end end end - end diff --git a/spec/icinga2/api/services_spec.rb b/spec/icinga2/api/services_spec.rb index 64d42e9..49b2234 100644 --- a/spec/icinga2/api/services_spec.rb +++ b/spec/icinga2/api/services_spec.rb @@ -1,15 +1,17 @@ +# frozen_string_literal: true + require 'spec_helper' RSpec.describe Icinga2::API::Services do - let(:client) { Icinga2::API::Client.new('https://icinga2.example.net:5665', icinga_credentials) } + subject(:all_services) { client.hosts.find('foo.example.net').services } - subject { client.hosts.find('foo.example.net').services } + let(:client) { Icinga2::API::Client.new('https://icinga2.example.net:5665', icinga_credentials) } describe '#all' do - it 'should return all Icinga2 services for the host' do + it 'returns all Icinga2 services for the host' do VCR.use_cassette('single_host_with_services', record: :new_episodes) do - services = subject.all + services = all_services.all expect(services.size).to eq 2 expect(services.first.to_s).to eq 'foo.example.net!dockerd|daemon' end @@ -18,9 +20,9 @@ describe '#find' do context 'when Icinga2 service host exist' do - it 'should return service object' do + it 'returns service object' do VCR.use_cassette('single_host_with_services', record: :new_episodes) do - service = subject.find('dockerd|daemon') + service = all_services.find('dockerd|daemon') expect(service).to be_a(Icinga2::API::Service) expect(service.to_s).to eq 'foo.example.net!dockerd|daemon' end @@ -28,9 +30,9 @@ end context 'when Icinga2 service host dont exist' do - it 'should return nil' do + it 'returns nil' do VCR.use_cassette('single_host_with_services', record: :new_episodes) do - expect(subject.find('foo')).to be_nil + expect(all_services.find('foo')).to be_nil end end end @@ -38,9 +40,9 @@ describe '#downtimes' do context 'when Icinga2 no downtimes exist' do - it 'should return empty array' do + it 'returns empty array' do VCR.use_cassette('single_host_without_downtimes') do - downtimes = subject.downtimes + downtimes = all_services.downtimes expect(downtimes).to be_a(Array) expect(downtimes.size).to eq 0 @@ -49,17 +51,15 @@ end context 'when Icinga2 downtimes exist' do - it 'should return all Icinga2 services downtimes for the host' do + it 'returns all Icinga2 services downtimes for the host' do VCR.use_cassette('single_host_with_downtimes', record: :new_episodes) do create_downtime('foo.example.net', 'dockerd|daemon') - downtimes = subject.downtimes + downtimes = all_services.downtimes expect(downtimes.size).to eq 1 expect(downtimes.first.to_s).to include('foo.example.net!dockerd|daemon') end end end - end - end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d6a1c0f..6bc3872 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'simplecov' require 'rspec' require 'vcr' @@ -31,7 +33,7 @@ def icinga_credentials { version: 'v1', username: 'root', password: ENV.fetch('ICINGA_API_PASSWORD', 'root'), verify_ssl: false } end -def create_downtime(host, service) +def create_downtime(host, service) # rubocop:disable Metrics/MethodLength duration = 300 start_time = DateTime.now end_time = start_time + duration