Skip to content

Commit d1706fe

Browse files
ekohleimlav
authored andcommitted
Add a function to compare the OS version (#972)
* Add a function to compare the OS version This function aims to reduce the boiler plate that a lot of modules have to compare versions: if $facts['operatingsystem'] == 'Ubuntu' && versioncmp(facts['operatingsystemmajrelease'], '16.04') >= 0 { Can now be reduced to: if os_version_gte('Ubuntu', '16.04') { * Readme Updated * Fix typo in README * Readme format update
1 parent 34502b6 commit d1706fe

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -1997,6 +1997,20 @@ function in Puppet for the many available type conversions.
19971997
19981998
*Type*: rvalue.
19991999
2000+
#### `os_version_gte`
2001+
2002+
Checks to see if the OS version is at least a certain version. Note that only the major version is taken into account.
2003+
2004+
Example usage:
2005+
```
2006+
if os_version_gte('Debian', '9') { }
2007+
if os_version_gte('Ubuntu', '18.04') { }
2008+
```
2009+
2010+
Returns:
2011+
- Boolean(0) # When OS is below the given version.
2012+
- Boolean(1) # When OS is equal to or greater than the given version.
2013+
20002014
#### `parsejson`
20012015
20022016
Converts a string of JSON into the correct Puppet structure (as a hash, array, string, integer, or a combination of such).
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Checks if the OS version is at least a certain version. Note that only the
2+
# major version is taken into account.
3+
#
4+
# Example usage:
5+
#
6+
# if os_version_gte('Debian', '9') { }
7+
# if os_version_gte('Ubuntu', '18.04') { }
8+
Puppet::Functions.create_function(:os_version_gte) do
9+
dispatch :os_version_gte do
10+
param 'String[1]', :os
11+
param 'String[1]', :version
12+
return_type 'Boolean'
13+
end
14+
15+
def os_version_gte(os, version)
16+
facts = closure_scope['facts']
17+
(facts['operatingsystem'] == os &&
18+
Puppet::Util::Package.versioncmp(version, facts['operatingsystemmajrelease']) >= 0)
19+
end
20+
end

spec/functions/os_version_gte_spec.rb

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require 'spec_helper'
2+
3+
describe 'os_version_gte' do
4+
context 'on Debian 9' do
5+
let(:facts) do
6+
{
7+
:operatingsystem => 'Debian',
8+
:operatingsystemmajrelease => '9',
9+
}
10+
end
11+
12+
it { is_expected.to run.with_params('Debian', '9').and_return(true) }
13+
it { is_expected.to run.with_params('Debian', '8').and_return(false) }
14+
it { is_expected.to run.with_params('Debian', '8.0').and_return(false) }
15+
it { is_expected.to run.with_params('Ubuntu', '16.04').and_return(false) }
16+
it { is_expected.to run.with_params('Fedora', '29').and_return(false) }
17+
end
18+
19+
context 'on Ubuntu 16.04' do
20+
let(:facts) do
21+
{
22+
:operatingsystem => 'Ubuntu',
23+
:operatingsystemmajrelease => '16.04',
24+
}
25+
end
26+
27+
it { is_expected.to run.with_params('Debian', '9').and_return(false) }
28+
it { is_expected.to run.with_params('Ubuntu', '16').and_return(false) }
29+
it { is_expected.to run.with_params('Ubuntu', '16.04').and_return(true) }
30+
it { is_expected.to run.with_params('Ubuntu', '18.04').and_return(true) }
31+
it { is_expected.to run.with_params('Fedora', '29').and_return(false) }
32+
end
33+
end

0 commit comments

Comments
 (0)