Skip to content

Commit 3bf6eb6

Browse files
author
Oana Tanasoiu
committed
(FACT-2694) Add linux openvz fact
1 parent 353bf71 commit 3bf6eb6

6 files changed

Lines changed: 123 additions & 4 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
module Facts
4+
module Linux
5+
module Hypervisors
6+
class Openvz
7+
FACT_NAME = 'hypervisors.openvz'
8+
9+
def call_the_resolver
10+
fact_value = check_openvz
11+
Facter::ResolvedFact.new(FACT_NAME, fact_value)
12+
end
13+
14+
def check_openvz
15+
openvz = Facter::Resolvers::OpenVz.resolve(:vm)
16+
return unless openvz
17+
18+
id = Facter::Resolvers::OpenVz.resolve(:id)
19+
20+
{ id: id.to_i, host: openvz == 'openvzhn' }
21+
end
22+
end
23+
end
24+
end
25+
end

lib/facter/resolvers/open_vz.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ def read_proc_status
3434

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

37+
@fact_list[:id] = parts[1]
38+
3739
return 'openvzhn' if parts[1] == '0'
3840

3941
return 'openvzve'
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# frozen_string_literal: true
2+
3+
describe Facts::Linux::Hypervisors::Openvz do
4+
describe '#call_the_resolver' do
5+
subject(:fact) { Facts::Linux::Hypervisors::Openvz.new }
6+
7+
before do
8+
allow(Facter::Resolvers::OpenVz).to \
9+
receive(:resolve).with(:vm).and_return(ovz)
10+
end
11+
12+
context 'when resolver returns nil' do
13+
let(:ovz) { nil }
14+
15+
it 'calls Facter::Resolvers::OpenVz' do
16+
fact.call_the_resolver
17+
expect(Facter::Resolvers::OpenVz).to have_received(:resolve).with(:vm)
18+
end
19+
20+
it 'returns virtual fact as nil' do
21+
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \
22+
have_attributes(name: 'hypervisors.openvz', value: nil)
23+
end
24+
end
25+
26+
context 'when resolver returns openvz host' do
27+
before { allow(Facter::Resolvers::OpenVz).to receive(:resolve).with(:id).and_return('0') }
28+
29+
let(:ovz) { 'openvzhn' }
30+
let(:value) { { 'id' => 0, 'host' => true } }
31+
32+
it 'returns openvz info' do
33+
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \
34+
have_attributes(name: 'hypervisors.openvz', value: value)
35+
end
36+
end
37+
38+
context 'when resolver returns openvz' do
39+
before { allow(Facter::Resolvers::OpenVz).to receive(:resolve).with(:id).and_return('101') }
40+
41+
let(:ovz) { 'openvze' }
42+
let(:value) { { 'id' => 101, 'host' => false } }
43+
44+
it 'returns openvz info' do
45+
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \
46+
have_attributes(name: 'hypervisors.openvz', value: value)
47+
end
48+
end
49+
end
50+
end

spec/facter/resolvers/open_vz_spec.rb

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@
1919
context 'when /proc/vz dir is empty' do
2020
let(:vz_dir_entries) { ['.', '..'] }
2121

22-
it 'returns nil' do
22+
it 'returns nil for vm' do
2323
expect(openvz_resolver.resolve(:vm)).to be_nil
2424
end
25+
26+
it 'returns nil for container id' do
27+
expect(openvz_resolver.resolve(:id)).to be_nil
28+
end
2529
end
2630

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

37-
it 'returns nil' do
41+
it 'returns nil for vm' do
3842
expect(openvz_resolver.resolve(:vm)).to be_nil
3943
end
44+
45+
it 'returns nil for container id' do
46+
expect(openvz_resolver.resolve(:id)).to be_nil
47+
end
48+
end
49+
50+
context 'when /proc/self/status is readable and openvz host' do
51+
let(:status_file) { load_fixture('proc_self_status_host').readlines }
52+
53+
it 'returns openvzhn' do
54+
expect(openvz_resolver.resolve(:vm)).to eql('openvzhn')
55+
end
56+
57+
it 'returns container id' do
58+
expect(openvz_resolver.resolve(:id)).to eql('0')
59+
end
4060
end
4161

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

4565
it 'returns openvzhn' do
46-
expect(openvz_resolver.resolve(:vm)).to eql('openvzhn')
66+
expect(openvz_resolver.resolve(:vm)).to eql('openvzve')
67+
end
68+
69+
it 'returns container id' do
70+
expect(openvz_resolver.resolve(:id)).to eql('101')
4771
end
4872
end
4973
end

spec/fixtures/proc_self_status

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ Groups: 4 20 24 25 27 29 30 44 46 108 114 1000
1313
NStgid: 14437
1414
NSpid: 14437
1515
NSpgid: 14437
16-
envID: 0
16+
envID: 101
1717
NSsid: 14122
1818
VxID: 0
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Name: cat
2+
Umask: 0002
3+
State: R (running)
4+
Tgid: 14437
5+
Ngid: 0
6+
Pid: 14437
7+
PPid: 14122
8+
TracerPid: 0
9+
Uid: 1000 1000 1000 1000
10+
Gid: 1000 1000 1000 1000
11+
FDSize: 256
12+
Groups: 4 20 24 25 27 29 30 44 46 108 114 1000
13+
NStgid: 14437
14+
NSpid: 14437
15+
NSpgid: 14437
16+
envID: 0
17+
NSsid: 14122
18+
VxID: 0

0 commit comments

Comments
 (0)