Skip to content

Commit a95dccd

Browse files
author
Jeff McCune
committed
Merge branch 'ticket/master/9859_add_root_home_fact_to_stdlib'
* ticket/master/9859_add_root_home_fact_to_stdlib: (#9859) Add root_home fact and tests
2 parents 05018f6 + 11c1837 commit a95dccd

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

lib/facter/root_home.rb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# A facter fact to determine the root home directory.
2+
# This varies on PE supported platforms and may be
3+
# reconfigured by the end user.
4+
5+
module Facter::Util::RootHome
6+
class << self
7+
def get_root_home
8+
root_ent = Facter::Util::Resolution.exec("getent passwd root")
9+
# The home directory is the sixth element in the passwd entry
10+
root_ent.split(":")[5]
11+
end
12+
end
13+
end
14+
15+
Facter.add(:root_home) do
16+
setcode { Facter::Util::RootHome.get_root_home }
17+
end

spec/spec_helper.rb

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
ARGV.clear
88

99
require 'puppet'
10+
require 'facter'
1011
require 'mocha'
1112
gem 'rspec', '>=2.0.0'
1213
require 'rspec/expectations'

spec/unit/facter/root_home_spec.rb

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
require 'spec_helper'
2+
require 'facter/root_home'
3+
4+
describe Facter::Util::RootHome do
5+
context "solaris" do
6+
let(:root_ent) { "root:x:0:0:Super-User:/:/sbin/sh" }
7+
let(:expected_root_home) { "/" }
8+
9+
it "should return /" do
10+
Facter::Util::Resolution.expects(:exec).with("getent passwd root").returns(root_ent)
11+
Facter::Util::RootHome.get_root_home.should == expected_root_home
12+
end
13+
end
14+
context "linux" do
15+
let(:root_ent) { "root:x:0:0:root:/root:/bin/bash" }
16+
let(:expected_root_home) { "/root" }
17+
18+
it "should return /root" do
19+
Facter::Util::Resolution.expects(:exec).with("getent passwd root").returns(root_ent)
20+
Facter::Util::RootHome.get_root_home.should == expected_root_home
21+
end
22+
end
23+
context "macosx" do
24+
let(:root_ent) { "root:*:0:0:System Administrator:/var/root:/bin/sh" }
25+
let(:expected_root_home) { "/var/root" }
26+
27+
it "should return /var/root" do
28+
Facter::Util::Resolution.expects(:exec).with("getent passwd root").returns(root_ent)
29+
Facter::Util::RootHome.get_root_home.should == expected_root_home
30+
end
31+
end
32+
context "windows" do
33+
let(:root_ent) { "FIXME TBD on Windows" }
34+
let(:expected_root_home) { "FIXME TBD on Windows" }
35+
36+
it "should return FIXME TBD on windows" do
37+
pending "FIXME: TBD on windows"
38+
Facter::Util::Resolution.expects(:exec).with("getent passwd root").returns(root_ent)
39+
Facter::Util::RootHome.get_root_home.should == expected_root_home
40+
end
41+
end
42+
end

0 commit comments

Comments
 (0)