Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for stepdown of minor versions #53

Merged
merged 6 commits into from
Jul 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ end

### Testing a function

As with testing manifests, types, or providers, `on_supported_os` iteration simplifies your function unit tests.
As with testing manifests, types, or providers, `on_supported_os` iteration simplifies your function unit tests.

**Specifying each operating system**:

Expand Down Expand Up @@ -275,7 +275,7 @@ describe 'myfunction' do
end
```

### Adding custom fact values
### Adding custom fact values

By adding custom fact values, you can:

Expand Down Expand Up @@ -352,11 +352,10 @@ In addition to the static fact values shown in the previous examples, you can cr
To do this, pass a lambda as the value for the custom fact. The lambda is passed the same values for operating system name and fact values that your tests are provided by `on_supported_os`.

```ruby
add_custom_fact :root_home, lambda { |os,facts| "/tmp/#{facts['hostname']" }
add_custom_fact :root_home, lambda { |os,facts| "/tmp/#{facts['hostname']}" }
```

Running your tests
------------------
## Running your tests

For most cases, there is no change to how you run your tests. Running `rake spec` will run all the tests against the facts for all the supported operating systems.

Expand All @@ -365,3 +364,5 @@ If you want to run the tests against the facts for specific operating systems, y
```bash
SPEC_FACTS_OS='ubuntu-14' rake spec
```

When no facts are available for the specific facter/operating system combination, the library will fall back to facts from earlier versions of the requested operating system, to allow testing to continue when new versions of facter are released. Set `SPEC_FACTS_STRICT=yes` to instead trigger a failure.
35 changes: 35 additions & 0 deletions lib/rspec-puppet-facts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ def on_supported_os(opts = {})
end

facter_version_filter = RspecPuppetFacts.facter_version_to_filter(facterversion)
db = FacterDB.get_facts({ :facterversion => facter_version_filter })

version = facterversion
while db.empty? && version !~ /\d+\.0\.\d+/
version = RspecPuppetFacts.down_facter_version(version)
facter_version_filter = RspecPuppetFacts.facter_version_to_filter(version)
db = FacterDB.get_facts({ :facterversion => facter_version_filter})
end


filter = []
opts[:supported_os].map do |os_sup|
Expand Down Expand Up @@ -79,6 +88,14 @@ def on_supported_os(opts = {})
return {}
end

unless version == facterversion
if RspecPuppetFacts.spec_facts_strict?
raise ArgumentError, "No facts were found in the FacterDB for Facter v#{facterversion}, aborting"
else
RspecPuppetFacts.warning "No facts were found in the FacterDB for Facter v#{facterversion}, using v#{version} instead"
end
end

os_facts_hash = {}
received_facts.map do |facts|
# Fix facter bug
Expand Down Expand Up @@ -161,6 +178,13 @@ def self.spec_facts_os_filter
ENV['SPEC_FACTS_OS']
end

# If SPEC_FACTS_STRICT is set to `yes`, RspecPuppetFacts will error on missing FacterDB entries, instead of warning & skipping the tests, or using an older facter version.
# @return [Boolean]
# @api private
def self.spec_facts_strict?
ENV['SPEC_FACTS_STRICT'] == 'yes'
end

# These facts are common for all OS'es and will be
# added to the facts retrieved from the FacterDB
# @api private
Expand Down Expand Up @@ -238,4 +262,15 @@ def self.facter_version_to_filter(version)
major, minor = version.split('.')
"/\\A#{major}\\.#{minor}\\./"
end
# Subtracts from the minor version passed and returns a string representing
# the next minor version down
# @return [String] next version below
# @param version [String] the Facter version
# @param minor_subtractor [int] the value which to subtract by
# @api private
def self.down_facter_version(version, minor_subtractor = 1)
major, minor, z = version.split('.')
minor = (minor.to_i - minor_subtractor).to_s
"#{major}.#{minor}.#{z}"
end
end
37 changes: 36 additions & 1 deletion spec/rspec_puppet_facts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,9 @@
end

context 'Without a custom facterversion in the options hash' do
before(:each) do
allow(Facter).to receive(:version).and_return('2.4.5')
end
subject do
on_supported_os(
supported_os: [
Expand All @@ -381,13 +384,45 @@
end

it 'returns facts from the loaded facter version' do
is_expected.to match(
'centos-7-x86_64' => include(
facterversion: /\A2\.4\./
)
)
end
end

context 'With a version that is above the current gem' do
before(:each) do
allow(Facter).to receive(:version).and_return('2.4.5')
end

subject do
on_supported_os(
supported_os: [
{ 'operatingsystem' => 'CentOS', 'operatingsystemrelease' => %w[7] }
],
facterversion: "2.5"
)
end

it 'returns facts from a facter version matching future and below' do
major, minor = Facter.version.split('.')
is_expected.to match(
'centos-7-x86_64' => include(
facterversion: /\A#{major}\.#{minor}\./
facterversion: /\A2\.4\./
)
)
end

context 'With SPEC_FACTS_STRICT set to `yes`' do
before(:each) do
allow(RspecPuppetFacts).to receive(:spec_facts_strict?).and_return(true)
end
it 'errors' do
expect { subject }.to raise_error ArgumentError, /No facts were found in the FacterDB.*aborting/
end
end
end

context 'With a custom facterversion (3.1) in the options hash' do
Expand Down