Skip to content

Commit 657cd71

Browse files
committed
Determine root_home without shelling out
This uses Ruby's Etc library to determine the root home. This avoids shelling out and writing multiple implementations.
1 parent 0ec25be commit 657cd71

File tree

2 files changed

+15
-99
lines changed

2 files changed

+15
-99
lines changed

lib/facter/root_home.rb

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,11 @@
11
# frozen_string_literal: true
22

3-
# root_home.rb
4-
module Facter::Util::RootHome
5-
# @summary
6-
# A facter fact to determine the root home directory.
7-
# This varies on PE supported platforms and may be
8-
# reconfigured by the end user.
9-
class << self
10-
# determines the root home directory
11-
def returnt_root_home
12-
root_ent = Facter::Core::Execution.execute('getent passwd root')
13-
# The home directory is the sixth element in the passwd entry
14-
# If the platform doesn't have getent, root_ent will be nil and we should
15-
# return it straight away.
16-
root_ent && root_ent.split(':')[5]
17-
end
18-
end
19-
end
20-
21-
Facter.add(:root_home) do
22-
setcode { Facter::Util::RootHome.returnt_root_home }
23-
end
24-
25-
Facter.add(:root_home) do
26-
confine kernel: :darwin
27-
setcode do
28-
str = Facter::Core::Execution.execute('dscacheutil -q user -a name root')
29-
hash = {}
30-
str.split("\n").each do |pair|
31-
key, value = pair.split(%r{:})
32-
hash[key] = value
33-
end
34-
hash['dir'].strip
35-
end
36-
end
37-
383
Facter.add(:root_home) do
39-
confine kernel: :aix
40-
root_home = nil
414
setcode do
42-
str = Facter::Core::Execution.execute('lsuser -c -a home root')
43-
str&.split("\n")&.each do |line|
44-
next if %r{^#}.match?(line)
45-
root_home = line.split(%r{:})[1]
46-
end
47-
root_home
5+
require 'etc'
6+
rescue LoadError
7+
# Unavailable on platforms like Windows
8+
else
9+
Etc.getpwnam('root').dir
4810
end
4911
end

spec/unit/facter/root_home_spec.rb

Lines changed: 10 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,66 +2,20 @@
22

33
require 'spec_helper'
44
require 'facter/root_home'
5-
describe 'Root Home Specs' do
6-
describe Facter::Util::RootHome do
7-
context 'when solaris' do
8-
let(:root_ent) { 'root:x:0:0:Super-User:/:/sbin/sh' }
9-
let(:expected_root_home) { '/' }
105

11-
it 'returns /' do
12-
expect(Facter::Core::Execution).to receive(:execute).with('getent passwd root').and_return(root_ent)
13-
expect(described_class.returnt_root_home).to eq(expected_root_home)
14-
end
15-
end
16-
context 'when linux' do
17-
let(:root_ent) { 'root:x:0:0:root:/root:/bin/bash' }
18-
let(:expected_root_home) { '/root' }
6+
describe 'root_home', type: :fact do
7+
subject { Facter.fact(:root_home) }
198

20-
it 'returns /root' do
21-
expect(Facter::Core::Execution).to receive(:execute).with('getent passwd root').and_return(root_ent)
22-
expect(described_class.returnt_root_home).to eq(expected_root_home)
23-
end
24-
end
25-
context 'when windows' do
26-
it 'is nil on windows' do
27-
expect(Facter::Core::Execution).to receive(:execute).with('getent passwd root').and_return(nil)
28-
expect(described_class.returnt_root_home).to be_nil
29-
end
30-
end
31-
end
32-
33-
describe 'root_home', type: :fact do
34-
before(:each) { Facter.clear }
35-
after(:each) { Facter.clear }
36-
37-
context 'when macosx' do
38-
before(:each) do
39-
allow(Facter.fact(:kernel)).to receive(:value).and_return('Darwin')
40-
allow(Facter.fact(:osfamily)).to receive(:value).and_return('Darwin')
41-
end
42-
let(:expected_root_home) { '/var/root' }
9+
before(:each) { Facter.clear }
10+
after(:each) { Facter.clear }
4311

44-
sample_dscacheutil = File.read(fixtures('dscacheutil', 'root'))
45-
46-
it 'returns /var/root' do
47-
allow(Facter::Core::Execution).to receive(:execute).with('dscacheutil -q user -a name root').and_return(sample_dscacheutil)
48-
expect(Facter.fact(:root_home).value).to eq(expected_root_home)
49-
end
50-
end
51-
52-
context 'when aix' do
53-
before(:each) do
54-
allow(Facter.fact(:kernel)).to receive(:value).and_return('AIX')
55-
allow(Facter.fact(:osfamily)).to receive(:value).and_return('AIX')
56-
end
57-
let(:expected_root_home) { '/root' }
12+
context 'when Windows', if: Facter.value(:kernel) == 'Windows' do
13+
it { expect(subject.value).to be(nil) }
14+
end
5815

59-
sample_lsuser = File.read(fixtures('lsuser', 'root'))
16+
context 'when non-Windows', if: Facter.value(:kernel) != 'Windows' do
17+
let(:expected) { Facter.value(:kernel) == 'Darwin' ? '/var/root' : '/root' }
6018

61-
it 'returns /root' do
62-
allow(Facter::Core::Execution).to receive(:execute).with('lsuser -c -a home root').and_return(sample_lsuser)
63-
expect(Facter.fact(:root_home).value).to eq(expected_root_home)
64-
end
65-
end
19+
it { expect(subject.value).to eq(expected) }
6620
end
6721
end

0 commit comments

Comments
 (0)