Skip to content

Commit

Permalink
Update Rubocop config, fix offenses
Browse files Browse the repository at this point in the history
  • Loading branch information
n-rodriguez committed Aug 29, 2024
1 parent 6b9c158 commit b096652
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 93 deletions.
33 changes: 29 additions & 4 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ AllCops:
TargetRubyVersion: 3.0
Exclude:
- bin/*
- spec/**/*

Gemspec/RequireMFA:
Enabled: false
Expand All @@ -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
24 changes: 13 additions & 11 deletions spec/icinga2/api/downtime_spec.rb
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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
Expand Down
33 changes: 17 additions & 16 deletions spec/icinga2/api/host_spec.rb
Original file line number Diff line number Diff line change
@@ -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
22 changes: 12 additions & 10 deletions spec/icinga2/api/hosts_spec.rb
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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'
Expand All @@ -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
Expand Down
44 changes: 23 additions & 21 deletions spec/icinga2/api/resource_spec.rb
Original file line number Diff line number Diff line change
@@ -1,70 +1,72 @@
# 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
end

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
Expand Down
Loading

0 comments on commit b096652

Please sign in to comment.