Skip to content

Commit 9693c04

Browse files
author
Jeff McCune
committed
Revert "Revert "Merge branch 'haus-add_pe_facts_to_stdlib' into 2.4.x""
This reverts commit d6d23b4. This backwards-compatible additional functionality is targeted at the next minor release. There are already backwards-incompatible changes in the master branch so we need to establish a new minor branch.
1 parent d6d23b4 commit 9693c04

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

lib/facter/pe_version.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Fact: is_pe, pe_version, pe_major_version, pe_minor_version, pe_patch_version
2+
#
3+
# Purpose: Return various facts about the PE state of the system
4+
#
5+
# Resolution: Uses a regex match against puppetversion to determine whether the
6+
# machine has Puppet Enterprise installed, and what version (overall, major,
7+
# minor, patch) is installed.
8+
#
9+
# Caveats:
10+
#
11+
Facter.add("pe_version") do
12+
setcode do
13+
pe_ver = Facter.value("puppetversion").match(/Puppet Enterprise (\d+\.\d+\.\d+)/)
14+
pe_ver[1] if pe_ver
15+
end
16+
end
17+
18+
Facter.add("is_pe") do
19+
setcode do
20+
if Facter.value(:pe_version).to_s.empty? then
21+
false
22+
else
23+
true
24+
end
25+
end
26+
end
27+
28+
Facter.add("pe_major_version") do
29+
confine :is_pe => true
30+
setcode do
31+
if pe_version = Facter.value(:pe_version)
32+
pe_version.to_s.split('.')[0]
33+
end
34+
end
35+
end
36+
37+
Facter.add("pe_minor_version") do
38+
confine :is_pe => true
39+
setcode do
40+
if pe_version = Facter.value(:pe_version)
41+
pe_version.to_s.split('.')[1]
42+
end
43+
end
44+
end
45+
46+
Facter.add("pe_patch_version") do
47+
confine :is_pe => true
48+
setcode do
49+
if pe_version = Facter.value(:pe_version)
50+
pe_version.to_s.split('.')[2]
51+
end
52+
end
53+
end

spec/spec_helper.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,17 @@
1212

1313
require 'puppetlabs_spec_helper/module_spec_helper'
1414

15+
RSpec.configure do |config|
16+
# FIXME REVISIT - We may want to delegate to Facter like we do in
17+
# Puppet::PuppetSpecInitializer.initialize_via_testhelper(config) because
18+
# this behavior is a duplication of the spec_helper in Facter.
19+
config.before :each do
20+
# Ensure that we don't accidentally cache facts and environment between
21+
# test cases. This requires each example group to explicitly load the
22+
# facts being exercised with something like
23+
# Facter.collection.loader.load(:ipaddress)
24+
Facter::Util::Loader.any_instance.stubs(:load_all)
25+
Facter.clear
26+
Facter.clear_messages
27+
end
28+
end
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env rspec
2+
3+
require 'spec_helper'
4+
5+
describe "PE Version specs" do
6+
before :each do
7+
Facter.collection.loader.load(:pe_version)
8+
end
9+
10+
context "If PE is installed" do
11+
%w{ 2.6.1 2.10.300 }.each do |version|
12+
puppetversion = "2.7.19 (Puppet Enterprise #{version})"
13+
context "puppetversion => #{puppetversion}" do
14+
before :each do
15+
Facter.fact(:puppetversion).stubs(:value).returns(puppetversion)
16+
end
17+
18+
(major,minor,patch) = version.split(".")
19+
20+
it "Should return true" do
21+
Facter.fact(:is_pe).value.should == true
22+
end
23+
24+
it "Should have a version of #{version}" do
25+
Facter.fact(:pe_version).value.should == version
26+
end
27+
28+
it "Should have a major version of #{major}" do
29+
Facter.fact(:pe_major_version).value.should == major
30+
end
31+
32+
it "Should have a minor version of #{minor}" do
33+
Facter.fact(:pe_minor_version).value.should == minor
34+
end
35+
36+
it "Should have a patch version of #{patch}" do
37+
Facter.fact(:pe_patch_version).value.should == patch
38+
end
39+
end
40+
end
41+
end
42+
43+
context "When PE is not installed" do
44+
before :each do
45+
Facter.fact(:puppetversion).stubs(:value).returns("2.7.19")
46+
end
47+
48+
it "is_pe is false" do
49+
Facter.fact(:is_pe).value.should == false
50+
end
51+
52+
it "pe_version is nil" do
53+
Facter.fact(:pe_version).value.should be_nil
54+
end
55+
56+
it "pe_major_version is nil" do
57+
Facter.fact(:pe_major_version).value.should be_nil
58+
end
59+
60+
it "pe_minor_version is nil" do
61+
Facter.fact(:pe_minor_version).value.should be_nil
62+
end
63+
64+
it "Should have a patch version" do
65+
Facter.fact(:pe_patch_version).value.should be_nil
66+
end
67+
end
68+
end

0 commit comments

Comments
 (0)