Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(FACT-2694) Add linux openvz fact #1970

Merged
merged 1 commit into from Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions lib/facter/facts/linux/hypervisors/openvz.rb
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module Facts
module Linux
module Hypervisors
class Openvz
FACT_NAME = 'hypervisors.openvz'

def call_the_resolver
fact_value = check_openvz
Facter::ResolvedFact.new(FACT_NAME, fact_value)
end

def check_openvz
openvz = Facter::Resolvers::OpenVz.resolve(:vm)
return unless openvz

id = Facter::Resolvers::OpenVz.resolve(:id)

{ id: id.to_i, host: openvz == 'openvzhn' }
end
end
end
end
end
2 changes: 2 additions & 0 deletions lib/facter/resolvers/open_vz.rb
Expand Up @@ -34,6 +34,8 @@ def read_proc_status

next unless /^envID:/ =~ parts[0]

@fact_list[:id] = parts[1]

return 'openvzhn' if parts[1] == '0'

return 'openvzve'
Expand Down
50 changes: 50 additions & 0 deletions spec/facter/facts/linux/hypervisors/openvz_spec.rb
@@ -0,0 +1,50 @@
# frozen_string_literal: true

describe Facts::Linux::Hypervisors::Openvz do
describe '#call_the_resolver' do
subject(:fact) { Facts::Linux::Hypervisors::Openvz.new }

before do
allow(Facter::Resolvers::OpenVz).to \
receive(:resolve).with(:vm).and_return(ovz)
end

context 'when resolver returns nil' do
let(:ovz) { nil }

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

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

context 'when resolver returns openvz host' do
before { allow(Facter::Resolvers::OpenVz).to receive(:resolve).with(:id).and_return('0') }

let(:ovz) { 'openvzhn' }
let(:value) { { 'id' => 0, 'host' => true } }

it 'returns openvz info' do
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \
have_attributes(name: 'hypervisors.openvz', value: value)
end
end

context 'when resolver returns openvz' do
before { allow(Facter::Resolvers::OpenVz).to receive(:resolve).with(:id).and_return('101') }

let(:ovz) { 'openvze' }
let(:value) { { 'id' => 101, 'host' => false } }

it 'returns openvz info' do
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \
have_attributes(name: 'hypervisors.openvz', value: value)
end
end
end
end
30 changes: 27 additions & 3 deletions spec/facter/resolvers/open_vz_spec.rb
Expand Up @@ -19,9 +19,13 @@
context 'when /proc/vz dir is empty' do
let(:vz_dir_entries) { ['.', '..'] }

it 'returns nil' do
it 'returns nil for vm' do
expect(openvz_resolver.resolve(:vm)).to be_nil
end

it 'returns nil for container id' do
expect(openvz_resolver.resolve(:id)).to be_nil
end
end

context 'when /proc/vz dir is not empty' do
Expand All @@ -34,16 +38,36 @@
context 'when /proc/self/status is nil' do
let(:status_file) { nil }

it 'returns nil' do
it 'returns nil for vm' do
expect(openvz_resolver.resolve(:vm)).to be_nil
end

it 'returns nil for container id' do
expect(openvz_resolver.resolve(:id)).to be_nil
end
end

context 'when /proc/self/status is readable and openvz host' do
let(:status_file) { load_fixture('proc_self_status_host').readlines }

it 'returns openvzhn' do
expect(openvz_resolver.resolve(:vm)).to eql('openvzhn')
end

it 'returns container id' do
expect(openvz_resolver.resolve(:id)).to eql('0')
end
end

context 'when /proc/self/status is readable' do
let(:status_file) { load_fixture('proc_self_status').readlines }

it 'returns openvzhn' do
expect(openvz_resolver.resolve(:vm)).to eql('openvzhn')
expect(openvz_resolver.resolve(:vm)).to eql('openvzve')
end

it 'returns container id' do
expect(openvz_resolver.resolve(:id)).to eql('101')
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/proc_self_status
Expand Up @@ -13,6 +13,6 @@ Groups: 4 20 24 25 27 29 30 44 46 108 114 1000
NStgid: 14437
NSpid: 14437
NSpgid: 14437
envID: 0
envID: 101
NSsid: 14122
VxID: 0
18 changes: 18 additions & 0 deletions spec/fixtures/proc_self_status_host
@@ -0,0 +1,18 @@
Name: cat
Umask: 0002
State: R (running)
Tgid: 14437
Ngid: 0
Pid: 14437
PPid: 14122
TracerPid: 0
Uid: 1000 1000 1000 1000
Gid: 1000 1000 1000 1000
FDSize: 256
Groups: 4 20 24 25 27 29 30 44 46 108 114 1000
NStgid: 14437
NSpid: 14437
NSpgid: 14437
envID: 0
NSsid: 14122
VxID: 0