Skip to content
This repository has been archived by the owner on Jun 19, 2020. It is now read-only.

Commit

Permalink
Merge 2481a6a into a9d516e
Browse files Browse the repository at this point in the history
  • Loading branch information
oanatmaria authored Mar 26, 2020
2 parents a9d516e + 2481a6a commit 4ae57c0
Show file tree
Hide file tree
Showing 14 changed files with 583 additions and 107 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,6 @@ RSpec/ExampleLength:

RSpec/DescribedClass:
EnforcedStyle: explicit

RSpec/NestedGroups:
Max: 5
379 changes: 365 additions & 14 deletions .rubocop_todo.yml

Large diffs are not rendered by default.

25 changes: 17 additions & 8 deletions lib/facts/debian/os/distro/release.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,29 @@ class Release
ALIASES = %w[lsbdistrelease lsbmajdistrelease lsbminordistrelease].freeze

def call_the_resolver
fact_value = Facter::Resolvers::LsbRelease.resolve(:release)
fact_value = determine_release_for_os

return Facter::ResolvedFact.new(FACT_NAME, nil) unless fact_value

versions = fact_value.split('.')
release = {
'full' => fact_value,
'major' => versions[0],
'minor' => versions[1]
}
release = { 'full' => fact_value, 'major' => versions[0], 'minor' => versions[1].gsub(/^0([1-9])/, '\1') }

[Facter::ResolvedFact.new(FACT_NAME, release),
Facter::ResolvedFact.new(ALIASES[0], fact_value, :legacy),
Facter::ResolvedFact.new(ALIASES[1], versions[0], :legacy),
Facter::ResolvedFact.new(ALIASES[2], versions[1], :legacy)]
Facter::ResolvedFact.new(ALIASES[1], release['major'], :legacy),
Facter::ResolvedFact.new(ALIASES[2], release['minor'], :legacy)]
end

private

def determine_release_for_os
os_name = Facter::Resolvers::OsRelease.resolve(:name)

if os_name =~ /Debian/
Facter::Resolvers::DebianVersion.resolve(:version)
else
Facter::Resolvers::OsRelease.resolve(:version_id)
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/facts/debian/os/name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Name
ALIASES = 'operatingsystem'

def call_the_resolver
fact_value = Facter::Resolvers::LsbRelease.resolve(:distributor_id)
fact_value = Facter::Resolvers::OsRelease.resolve(:name)

[Facter::ResolvedFact.new(FACT_NAME, fact_value), Facter::ResolvedFact.new(ALIASES, fact_value, :legacy)]
end
Expand Down
16 changes: 14 additions & 2 deletions lib/facts/debian/os/release.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,33 @@ class Release
ALIASES = %w[operatingsystemmajrelease operatingsystemrelease].freeze

def call_the_resolver
fact_value = Facter::Resolvers::LsbRelease.resolve(:release)
fact_value = determine_release_for_os

return Facter::ResolvedFact.new(FACT_NAME, nil) unless fact_value

versions = fact_value.split('.')
release = {
'full' => fact_value,
'major' => versions[0],
'minor' => versions[1]
'minor' => versions[1].gsub(/^0([1-9])/, '\1')
}

[Facter::ResolvedFact.new(FACT_NAME, release),
Facter::ResolvedFact.new(ALIASES.first, versions[0], :legacy),
Facter::ResolvedFact.new(ALIASES.last, fact_value, :legacy)]
end

private

def determine_release_for_os
os_name = Facter::Resolvers::OsRelease.resolve(:name)

if os_name =~ /Debian/
Facter::Resolvers::DebianVersion.resolve(:version)
else
Facter::Resolvers::OsRelease.resolve(:version_id)
end
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ def post_resolve(fact_name)
end

def read_debian_version(fact_name)
output, _status = Open3.capture2('cat /etc/debian_version')
full_version = output.delete("\n")
versions = full_version.split('.')
return unless File.readable?('/etc/debian_version')

@fact_list[:full] = full_version
@fact_list[:major] = versions[0]
@fact_list[:minor] = versions[1].gsub(/^0([1-9])/, '\1')
verion = File.read('/etc/debian_version')

@fact_list[:version] = verion.strip

@fact_list[fact_name]
end
Expand Down
97 changes: 74 additions & 23 deletions spec/facter/facts/debian/os/distro/release_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,89 @@
describe '#call_the_resolver' do
subject(:fact) { Facts::Debian::Os::Distro::Release.new }

let(:value) { '9.0' }
let(:release) { { 'full' => '9.0', 'major' => '9', 'minor' => '0' } }
context 'when os is Ubuntu' do
before do
allow(Facter::Resolvers::OsRelease).to receive(:resolve).with(:name).and_return(name)
allow(Facter::Resolvers::OsRelease).to receive(:resolve).with(:version_id).and_return(value)
end

before do
allow(Facter::Resolvers::LsbRelease).to receive(:resolve).with(:release).and_return(value)
end
let(:name) { 'Ubuntu' }

it 'calls Facter::Resolvers::LsbRelease' do
fact.call_the_resolver
expect(Facter::Resolvers::LsbRelease).to have_received(:resolve).with(:release)
end
context 'when version_id is retrieved successful' do
let(:value) { '18.04' }
let(:value_final) { { 'full' => '18.04', 'major' => '18', 'minor' => '4' } }

it 'calls Facter::Resolvers::OsRelease with :name' do
fact.call_the_resolver
expect(Facter::Resolvers::OsRelease).to have_received(:resolve).with(:name)
end

it 'calls Facter::Resolvers::OsRelease with :version_id' do
fact.call_the_resolver
expect(Facter::Resolvers::OsRelease).to have_received(:resolve).with(:version_id)
end

it 'returns release fact' do
expect(fact.call_the_resolver).to be_an_instance_of(Array).and \
contain_exactly(an_object_having_attributes(name: 'os.distro.release', value: value_final),
an_object_having_attributes(name: 'lsbdistrelease', value: value, type: :legacy),
an_object_having_attributes(name: 'lsbmajdistrelease',
value: value_final['major'], type: :legacy),
an_object_having_attributes(name: 'lsbminordistrelease',
value: value_final['minor'], type: :legacy))
end
end

it 'returns release fact' do
expect(fact.call_the_resolver).to be_an_instance_of(Array).and \
contain_exactly(an_object_having_attributes(name: 'os.distro.release', value: release),
an_object_having_attributes(name: 'lsbdistrelease', value: value, type: :legacy),
an_object_having_attributes(name: 'lsbmajdistrelease',
value: release['major'], type: :legacy),
an_object_having_attributes(name: 'lsbminordistrelease',
value: release['minor'], type: :legacy))
context 'when version_id could not be retrieve' do
let(:value) { nil }

it 'returns release fact as nil' do
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \
have_attributes(name: 'os.distro.release', value: value)
end
end
end

context 'when lsb_release is not installed' do
let(:value) { nil }
context 'when os is Debian' do
let(:name) { 'Debian' }

before do
allow(Facter::Resolvers::LsbRelease).to receive(:resolve).with(:release).and_return(value)
allow(Facter::Resolvers::OsRelease).to receive(:resolve).with(:name).and_return(name)
allow(Facter::Resolvers::DebianVersion).to receive(:resolve).with(:version).and_return(value)
end

context 'when version_id is retrieved successful' do
let(:value) { '10.02' }
let(:value_final) { { 'full' => '10.02', 'major' => '10', 'minor' => '2' } }

it 'calls Facter::Resolvers::OsRelease with :name' do
fact.call_the_resolver
expect(Facter::Resolvers::OsRelease).to have_received(:resolve).with(:name)
end

it 'calls Facter::Resolvers::DebianVersion' do
fact.call_the_resolver
expect(Facter::Resolvers::DebianVersion).to have_received(:resolve).with(:version)
end

it 'returns release fact' do
expect(fact.call_the_resolver).to be_an_instance_of(Array).and \
contain_exactly(an_object_having_attributes(name: 'os.distro.release', value: value_final),
an_object_having_attributes(name: 'lsbdistrelease', value: value, type: :legacy),
an_object_having_attributes(name: 'lsbmajdistrelease',
value: value_final['major'], type: :legacy),
an_object_having_attributes(name: 'lsbminordistrelease',
value: value_final['minor'], type: :legacy))
end
end

it 'returns release fact' do
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact)
.and have_attributes(name: 'os.distro.release', value: value)
context 'when version_id could not be retrieve' do
let(:value) { nil }

it 'returns release fact as nil' do
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \
have_attributes(name: 'os.distro.release', value: value)
end
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/facter/facts/debian/os/name_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
let(:value) { 'Debian' }

before do
allow(Facter::Resolvers::LsbRelease).to receive(:resolve).with(:distributor_id).and_return(value)
allow(Facter::Resolvers::OsRelease).to receive(:resolve).with(:name).and_return(value)
end

it 'calls Facter::Resolvers::LsbRelease' do
it 'calls Facter::Resolvers::OsRelease' do
fact.call_the_resolver
expect(Facter::Resolvers::LsbRelease).to have_received(:resolve).with(:distributor_id)
expect(Facter::Resolvers::OsRelease).to have_received(:resolve).with(:name)
end

it 'returns operating system name fact' do
Expand Down
94 changes: 73 additions & 21 deletions spec/facter/facts/debian/os/release_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,87 @@
describe '#call_the_resolver' do
subject(:fact) { Facts::Debian::Os::Release.new }

before do
allow(Facter::Resolvers::LsbRelease).to receive(:resolve).with(:release).and_return(value)
end
context 'when os is Ubuntu' do
before do
allow(Facter::Resolvers::OsRelease).to receive(:resolve).with(:name).and_return(name)
allow(Facter::Resolvers::OsRelease).to receive(:resolve).with(:version_id).and_return(value)
end

let(:name) { 'Ubuntu' }

context 'when version_id is retrieved successful' do
let(:value) { '18.04' }
let(:value_final) { { 'full' => '18.04', 'major' => '18', 'minor' => '4' } }

context 'when lsb_release installed' do
let(:value) { '10.9' }
let(:value_final) { { 'full' => '10.9', 'major' => '10', 'minor' => '9' } }
it 'calls Facter::Resolvers::OsRelease with :name' do
fact.call_the_resolver
expect(Facter::Resolvers::OsRelease).to have_received(:resolve).with(:name)
end

it 'calls Facter::Resolvers::LsbRelease' do
fact.call_the_resolver
expect(Facter::Resolvers::LsbRelease).to have_received(:resolve).with(:release)
it 'calls Facter::Resolvers::OsRelease with :version_id' do
fact.call_the_resolver
expect(Facter::Resolvers::OsRelease).to have_received(:resolve).with(:version_id)
end

it 'returns release fact' do
expect(fact.call_the_resolver).to be_an_instance_of(Array).and \
contain_exactly(an_object_having_attributes(name: 'os.release', value: value_final),
an_object_having_attributes(name: 'operatingsystemmajrelease', value: value_final['major'],
type: :legacy),
an_object_having_attributes(name: 'operatingsystemrelease', value: value_final['full'],
type: :legacy))
end
end

it 'returns release fact' do
expect(fact.call_the_resolver).to be_an_instance_of(Array).and \
contain_exactly(an_object_having_attributes(name: 'os.release', value: value_final),
an_object_having_attributes(name: 'operatingsystemmajrelease', value: value_final['major'],
type: :legacy),
an_object_having_attributes(name: 'operatingsystemrelease', value: value_final['full'],
type: :legacy))
context 'when version_id could not be retrieve' do
let(:value) { nil }

it 'returns release fact as nil' do
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \
have_attributes(name: 'os.release', value: value)
end
end
end

context 'when lsb_release uninstalled' do
let(:value) { nil }
context 'when os is Debian' do
let(:name) { 'Debian' }

before do
allow(Facter::Resolvers::OsRelease).to receive(:resolve).with(:name).and_return(name)
allow(Facter::Resolvers::DebianVersion).to receive(:resolve).with(:version).and_return(value)
end

context 'when version_id is retrieved successful' do
let(:value) { '10.02' }
let(:value_final) { { 'full' => '10.02', 'major' => '10', 'minor' => '2' } }

it 'calls Facter::Resolvers::OsRelease with :name' do
fact.call_the_resolver
expect(Facter::Resolvers::OsRelease).to have_received(:resolve).with(:name)
end

it 'calls Facter::Resolvers::DebianVersion' do
fact.call_the_resolver
expect(Facter::Resolvers::DebianVersion).to have_received(:resolve).with(:version)
end

it 'returns release fact' do
expect(fact.call_the_resolver).to be_an_instance_of(Array).and \
contain_exactly(an_object_having_attributes(name: 'os.release', value: value_final),
an_object_having_attributes(name: 'operatingsystemmajrelease', value: value_final['major'],
type: :legacy),
an_object_having_attributes(name: 'operatingsystemrelease', value: value_final['full'],
type: :legacy))
end
end

context 'when version_id could not be retrieve' do
let(:value) { nil }

it 'returns release fact as nil' do
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \
have_attributes(name: 'os.release', value: value)
it 'returns release fact as nil' do
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \
have_attributes(name: 'os.release', value: value)
end
end
end
end
Expand Down
27 changes: 0 additions & 27 deletions spec/facter/resolvers/debian_version_resolver_spec.rb

This file was deleted.

14 changes: 14 additions & 0 deletions spec/facter/resolvers/debian_version_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

describe Facter::Resolvers::DebianVersion do
before do
allow(File).to receive(:readable?).with('/etc/debian_version').and_return(true)
allow(File).to receive(:read).with('/etc/debian_version').and_return("10.01\n")
end

it 'returns version' do
result = Facter::Resolvers::DebianVersion.resolve(:version)

expect(result).to eq('10.01')
end
end
2 changes: 2 additions & 0 deletions spec/facter/resolvers/os_release_resolver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
allow(File).to receive(:read)
.with('/etc/os-release')
.and_return(load_fixture('os_release').read)
end

after do
Facter::Resolvers::OsRelease.invalidate_cache
end

Expand Down
Loading

0 comments on commit 4ae57c0

Please sign in to comment.