This repository has been archived by the owner on Jun 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Oana Tanasoiu
committed
May 25, 2020
1 parent
f6a9a6c
commit 910762e
Showing
5 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# frozen_string_literal: true | ||
|
||
module Facter | ||
module Resolvers | ||
class OpenVz < BaseResolver | ||
# build | ||
|
||
@semaphore = Mutex.new | ||
@fact_list ||= {} | ||
|
||
class << self | ||
private | ||
|
||
def post_resolve(fact_name) | ||
@fact_list.fetch(fact_name) { check_proc_vz(fact_name) } | ||
end | ||
|
||
def check_proc_vz(fact_name) | ||
if !Dir.exist?('/proc/vz') || !File.executable?('/proc/lve/list') || Dir.entries('/proc/vz').count.equal?(2) | ||
return | ||
end | ||
|
||
@fact_list[:vm] = read_proc_status | ||
@fact_list[fact_name] | ||
end | ||
|
||
def read_proc_status | ||
proc_status_content = Facter::Util::FileHelper.safe_readlines('/proc/self/status', nil) | ||
return unless proc_status_content | ||
|
||
proc_status_content.each do |line| | ||
parts = line.split("\s") | ||
next unless parts.size.equal?(2) | ||
|
||
next unless /^envID:/.match?(parts[0]) | ||
|
||
return 'openvzhn' if parts[1] == '0' | ||
|
||
return 'openvzve' | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Facter::Resolvers::OpenVz do | ||
subject(:openvz_resolver) { Facter::Resolvers::OpenVz } | ||
|
||
let(:log_spy) { instance_spy(Facter::Log) } | ||
let(:vz_dir) { true } | ||
let(:list_executable) { true } | ||
|
||
after do | ||
openvz_resolver.invalidate_cache | ||
end | ||
|
||
before do | ||
openvz_resolver.instance_variable_set(:@log, log_spy) | ||
allow(Dir).to receive(:exist?).with('/proc/vz').and_return(vz_dir) | ||
allow(File).to receive(:executable?).with('/proc/lve/list').and_return(list_executable) | ||
allow(Dir).to receive(:entries).with('/proc/vz').and_return(vz_dir_entries) | ||
end | ||
|
||
context 'when /proc/vz dir is empty' do | ||
let(:vz_dir_entries) { ['.', '..'] } | ||
|
||
it 'returns nil' do | ||
expect(openvz_resolver.resolve(:vm)).to be_nil | ||
end | ||
end | ||
|
||
context 'when /proc/vz dir is not empty' do | ||
let(:vz_dir_entries) { ['.', '..', 'some_file.txt'] } | ||
|
||
before do | ||
allow(Facter::Util::FileHelper).to receive(:safe_readlines).with('/proc/self/status', nil).and_return(status_file) | ||
end | ||
|
||
context 'when /proc/self/status is nil' do | ||
let(:status_file) { nil } | ||
|
||
it 'returns nil' do | ||
expect(openvz_resolver.resolve(:vm)).to be_nil | ||
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') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters