Skip to content
New issue

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

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

Already on GitHub? # to your account

Document "on_supported_os(arg)" with modern Ruby hashes and an example. #108

Merged
merged 1 commit into from
Nov 7, 2019
Merged
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
73 changes: 71 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,27 @@ describe 'myclass::debian' do
],
}

on_supported_os(test_on).each do |os, facts|
let (:facts) { facts }
on_supported_os(test_on).each do |os, os_facts|
let (:facts) { os_facts }
it { is_expected.to compile.with_all_deps }
end
end
```
Ruby 1.9 and later:
```ruby
require 'spec_helper'

describe 'myclass::raspbian' do
test_on = {
supported_os: [
{
'operatingsystem' => 'Debian',
'operatingsystemrelease' => ['10', '9', '8'],
},
],
}
on_supported_os(test_on).each do |os, os_facts|
let(:facts) { os_facts }
it { is_expected.to compile.with_all_deps }
end
end
Expand Down Expand Up @@ -160,6 +179,56 @@ describe 'myclass' do
end
```

When using roles and profiles to manage a heterogeneous IT estate, you can test a profile that supports several OSes with many `let(:facts)` as long as each is in its own context:
```ruby
require 'spec_helper'

describe 'profiles::packagerepos' do
context 'Raspbian tests' do # We manage hundreds of desk-mounted Pis
raspbian = {
hardwaremodels: ['armv7l'],
supported_os: [
{
'operatingsystem' => 'Debian',
'operatingsystemrelease' => ['10', '9', '8'],
},
],
}
on_supported_os(raspbian).each do |os, os_facts|
let(:facts) do
os_facts
end

context "#{os} with defaults" do
it { is_expected.to compile }
# more tests ...
end
end
end
context 'Ubuntu tests' do # And also a fleet of Ubuntu desktops
ubuntu = {
supported_os: [
{
'operatingsystem' => 'Ubuntu',
'operatingsystemrelease' => ['18.04', '16.04'],
},
],
}

on_supported_os(ubuntu).each do |os, os_facts|
let(:facts) do
os_facts
end

context "#{os} with defaults" do
it { is_expected.to compile }
# more tests ...
end
end
end
end
```

### Testing a type or provider

Use `on_supported_os` in the same way for your type and provider unit tests.
Expand Down