diff --git a/lib/puppet/functions/os_version_gte.rb b/lib/puppet/functions/os_version_gte.rb index c0e0043..151c3c5 100644 --- a/lib/puppet/functions/os_version_gte.rb +++ b/lib/puppet/functions/os_version_gte.rb @@ -1,27 +1,27 @@ # frozen_string_literal: true # @summary # Checks if the OS version is at least a certain version. # > *Note:* # Only the major version is taken into account. # # @example Example usage:# # if os_version_gte('Debian', '9') { } # if os_version_gte('Ubuntu', '18.04') { } Puppet::Functions.create_function(:os_version_gte) do # @param os operating system # @param version # # @return [Boolean] `true` or `false dispatch :os_version_gte do param 'String[1]', :os param 'String[1]', :version return_type 'Boolean' end def os_version_gte(os, version) facts = closure_scope['facts'] (facts['operatingsystem'] == os && - Puppet::Util::Package.versioncmp(version, facts['operatingsystemmajrelease']) >= 0) + Puppet::Util::Package.versioncmp(facts['operatingsystemmajrelease'], version) >= 0) end end diff --git a/spec/functions/os_version_gte_spec.rb b/spec/functions/os_version_gte_spec.rb index 5de5838..aec4982 100644 --- a/spec/functions/os_version_gte_spec.rb +++ b/spec/functions/os_version_gte_spec.rb @@ -1,47 +1,50 @@ # frozen_string_literal: true require 'spec_helper' describe 'os_version_gte' do context 'on Debian 9' do let(:facts) do { operatingsystem: 'Debian', operatingsystemmajrelease: '9', } end + it { is_expected.to run.with_params('Debian', '10').and_return(false) } it { is_expected.to run.with_params('Debian', '9').and_return(true) } - it { is_expected.to run.with_params('Debian', '8').and_return(false) } - it { is_expected.to run.with_params('Debian', '8.0').and_return(false) } + it { is_expected.to run.with_params('Debian', '8').and_return(true) } + it { is_expected.to run.with_params('Debian', '8.0').and_return(true) } it { is_expected.to run.with_params('Ubuntu', '16.04').and_return(false) } it { is_expected.to run.with_params('Fedora', '29').and_return(false) } end context 'on Ubuntu 16.04' do let(:facts) do { operatingsystem: 'Ubuntu', operatingsystemmajrelease: '16.04', } end it { is_expected.to run.with_params('Debian', '9').and_return(false) } - it { is_expected.to run.with_params('Ubuntu', '16').and_return(false) } + it { is_expected.to run.with_params('Ubuntu', '16').and_return(true) } + it { is_expected.to run.with_params('Ubuntu', '14.04').and_return(true) } it { is_expected.to run.with_params('Ubuntu', '16.04').and_return(true) } - it { is_expected.to run.with_params('Ubuntu', '18.04').and_return(true) } + it { is_expected.to run.with_params('Ubuntu', '18.04').and_return(false) } + it { is_expected.to run.with_params('Ubuntu', '20.04').and_return(false) } it { is_expected.to run.with_params('Fedora', '29').and_return(false) } end context 'with invalid params' do let(:facts) do { operatingsystem: 'Ubuntu', operatingsystemmajrelease: '16.04', } end it { is_expected.to run.with_params('123', 'abc').and_return(false) } it { is_expected.to run.with_params([], 123).and_raise_error(ArgumentError) } end end