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

Raise minimum version to Puppet 7.11 #68

Closed
wants to merge 4 commits into from
Closed
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
106 changes: 21 additions & 85 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,15 @@

gem install rspec-puppet

> Note for ruby 1.8 users: while rspec-puppet itself supports ruby 1.8, you'll
> need to pin rspec itself to `~> 3.1.0`, as later rspec versions do not work
> on old rubies anymore.

## Starting out with a new module

When you start out on a new module, create a metadata.json file for your module and then run `rspec-puppet-init` to create the necessary files to configure rspec-puppet for your module's tests.


## Configure manifests for Puppet 4

With Puppet 3, the manifest is set to `$manifestdir/site.pp`. However Puppet 4 defaults to an empty value. In order to test manifests you will need to set appropriate settings.

Puppet configuration reference for `manifest` can be found online:

* Puppet 3: https://docs.puppet.com/puppet/3.8/reference/configuration.html#manifest
* Puppet 4: https://docs.puppet.com/puppet/4.8/reference/configuration.html#manifest

Configuration is typically done in a `spec/spec_helper.rb` file which each of your spec will require. Example code:
```ruby
# /spec
base_dir = File.dirname(File.expand_path(__FILE__))

RSpec.configure do |c|
c.module_path = File.join(base_dir, 'fixtures', 'modules')
c.manifest_dir = File.join(base_dir, 'fixtures', 'manifests')
c.manifest = File.join(base_dir, 'fixtures', 'manifests', 'site.pp')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We utilize the manifest setting when testing our control repository to ensure anything in the environment site.pp is included during tests as shown in the spec/spec_helper.rb fragment below. Is there a better way to accomplish this?

RSpec.configure do |c|
  c.manifest = File.join(File.dirname(File.dirname(__FILE__)), 'manifests', 'site.pp')
end

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I dropped it is that the manifest setting is gone. At least in https://www.puppet.com/docs/puppet/7/configuration.html I can no longer find it. I suspect environments now determine this via environment.conf. But feedback is welcome. I may have missed something.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure I ran into a situation myself where I needed to the manifest, so I think you're right that this should remain.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be the case that Puppet doesn't use this setting directly anymore, but it is used by rspec-puppet internally to prepend additional Puppet code to test:

[site_pp_str, pre_cond, test_manifest(type, manifest_opts), post_cond].compact.join("\n")

def site_pp_str
return '' unless (path = adapter.manifest)
if File.file?(path)
File.read(path)
elsif File.directory?(path)
# Read and concatenate all .pp files.
Dir[File.join(path, '*.pp')].sort.map do |f|
File.read(f)
end.join("\n")
else
''
end
end

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I found this in our Infra repo where I use it to test our environment, rather than individual modules: https://github.com/theforeman/foreman-infra/blob/9d05130a5ada4fd734898b6e14b93bd292c92717/puppet/spec/spec_helper.rb#L29

c.environmentpath = File.join(Dir.pwd, 'spec')
c.module_path = File.join(__dir__, 'fixtures', 'modules')
c.environmentpath = __dir__

# Coverage generation
c.after(:suite) do
Expand All @@ -65,125 +46,83 @@ RSpec.configure do |c|
end
```

#### manifest\_dir
Type | Default | Puppet Version(s)
------ | -------- | -----------------
String | Required | 2.x, 3.x

The path to the directory containing your basic manifests like `site.pp`.

#### module\_path
Type | Default | Puppet Version(s)
------ | -------- | ------------------
String | Required | 2.x, 3.x, 4.x, 5.x
String | Required | any

The path to the directory containing your Puppet modules.

#### default\_facts
Type | Default | Puppet Version(s)
---- | ------- | ------------------
Hash | `{}` | 2.x, 3.x, 4.x, 5.x
Hash | `{}` | any

A hash of default facts that should be used for all the tests.

#### hiera\_config
Type | Default | Puppet Version(s)
------ | ------------- | -----------------
String | `"/dev/null"` | 3.x, 4.x, 5.x
String | `"/dev/null"` | any

The path to your `hiera.yaml` file (if used).

#### default\_node\_params
Type | Default | Puppet Version(s)
---- | ------- | -----------------
Hash | `{}` | 4.x, 5.x
Hash | `{}` | any

A hash of default node parameters that should be used for all the tests.

#### default\_trusted\_facts
Type | Default | Puppet Version(s)
---- | ------- | -----------------
Hash | `{}` | 4.x, 5.x
Hash | `{}` | any

A hash of default trusted facts that should be used for all the tests
(available in the manifests as the `$trusted` hash). In order to use this, the
`trusted_node_data` setting must be set to `true`.
(available in the manifests as the `$trusted` hash).

#### trusted\_node\_data
Type | Default | Puppet Version(s)
------- | ------- | -----------------
Boolean | `false` | >=3.4, 4.x, 5.x
Boolean | `false` | any

Configures rspec-puppet to use the `$trusted` hash when compiling the
catalogues.

#### trusted\_server\_facts
Type | Default | Puppet Version(s)
------- | ------- | -----------------
Boolean | `false` | >=4.3, 5.x
Boolean | `false` | any

Configures rspec-puppet to use the `$server_facts` hash when compiling the
catalogues.

#### confdir
Type | Default | Puppet Version(s)
------ | --------------- | ------------------
String | `"/etc/puppet"` | 2.x, 3.x, 4.x, 5.x
String | `"/etc/puppet"` | any

The path to the main Puppet configuration directory.

#### config
Type | Default | Puppet Version(s)
------ | ---------------------- | ------------------
String | Puppet's default value | 2.x, 3.x, 4.x, 5.x
String | Puppet's default value | any

The path to `puppet.conf`.

#### manifest
Type | Default | Puppet Version(s)
------ | ---------------------- | -----------------
String | Puppet's default value | 2.x, 3.x

The entry-point manifest for Puppet, usually `$manifest_dir/site.pp`.

#### template\_dir
Type | Default | Puppet Version(s)
------ | ------- | -----------------
String | `nil` | 2.x, 3.x

The path to the directory that Puppet should search for templates that are
stored outside of modules.

#### environmentpath
Type | Default | Puppet Version(s)
------ | ------------------------------------- | -----------------
String | `"/etc/puppetlabs/code/environments"` | 4.x, 5.x
String | `"/etc/puppetlabs/code/environments"` | any

The search path for environment directories.

#### parser
Type | Default | Puppet Version(s)
------ | ----------- | -----------------
String | `"current"` | >= 3.2

This switches between the 3.x (`current`) and 4.x (`future`) parsers.

#### ordering
Type | Default | Puppet Version(s)
------ | -------------- | -----------------
String | `"title-hash"` | >= 3.3, 4.x, 5.x

How unrelated resources should be ordered when applying a catalogue.
* `manifest` - Use the order in which the resources are declared in the
manifest.
* `title-hash` - Order the resources randomly, but in a consistent manner
across runs (the order will only change if the manifest changes).
* `random` - Order the resources randomly.

#### strict\_variables
Type | Default | Puppet Version(s)
------- | ------- | -----------------
Boolean | `false` | >= 3.5, 4.x, 5.x
Boolean | `false` | any

Makes Puppet raise an error when it tries to reference a variable that hasn't
been defined (not including variables that have been explicitly set to
Expand All @@ -192,15 +131,15 @@ been defined (not including variables that have been explicitly set to
#### stringify\_facts
Type | Default | Puppet Version(s)
------- | ------- | -----------------
Boolean | `true` | >= 3.3, 4.x, 5.x
Boolean | `true` | any

Makes rspec-puppet coerce all the fact values into strings (matching the
behaviour of older versions of Puppet).

#### enable\_pathname\_stubbing
Type | Default | Puppet Version(s)
------- | ------- | ------------------
Boolean |`false` | 2.x, 3.x, 4.x, 5.x
Boolean |`false` | any

Configures rspec-puppet to stub out `Pathname#absolute?` with it's own
implementation. This should only be enabled if you're running into an issue
Expand All @@ -210,15 +149,15 @@ functions, etc) that use `Pathname#absolute?`.
#### setup\_fixtures
Type | Default | Puppet Version(s)
------- | ------- | ------------------
Boolean | `true` | 2.x, 3.x, 4.x, 5.x
Boolean | `true` | any

Configures rspec-puppet to automatically create a link from the root of your
module to `spec/fixtures/<module name>` at the beginning of the test run.

#### derive\_node\_facts\_from\_nodename
Type | Default | Puppet Version(s)
------- | ------- | -----------------
Boolean | `true` | 2.x, 3.x, 4.x, 5.x
Boolean | `true` | any

If `true`, rspec-puppet will override the `fdqn`, `hostname`, and `domain`
facts with values that it derives from the node name (specified with
Expand All @@ -231,7 +170,7 @@ setting to `false`.
#### facter\_implementation
Type | Default | Puppet Version(s)
------- | -------- | -----------------
String | `facter` | 6.25+, 7.12+
String | `facter` | any

Configures rspec-puppet to use a specific Facter implementation for running
unit tests. If the `rspec` implementation is set and Puppet does not support
Expand Down Expand Up @@ -695,8 +634,6 @@ RSpec.configure do |c|
end
```

**NOTE** Setting top-scope variables is not supported in Puppet < 3.0.

#### Specifying extra code to load (pre-conditions)

If the manifest being tested relies on another class or variables to be set, these can be added via
Expand Down Expand Up @@ -748,7 +685,7 @@ let(:module_path) { '/path/to/your/module/dir' }

#### Specifying trusted facts

When testing with Puppet >= 4.3 the trusted facts hash will have the standard trusted fact keys
The trusted facts hash will have the standard trusted fact keys
(certname, domain, and hostname) populated based on the node name (as set with `:node`).

By default, the test environment contains no custom trusted facts (as usually obtained
Expand All @@ -773,8 +710,7 @@ end

#### Specifying trusted external data

When testing with Puppet >= 6.14, the trusted facts hash will have an additional `external`
key for trusted external data.
The trusted facts hash will have an `external` key for trusted external data.

By default, the test environment contains no trusted external data (as usually obtained from
trusted external commands and found in the `external` key). If you need to test against specific
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require 'puppet'
task :default => :test
task :spec => :test

require 'rspec-puppet/tasks/release_test' unless RUBY_VERSION.start_with?('1')
require 'rspec-puppet/tasks/release_test'

fixtures_dir = File.expand_path(File.join(__FILE__, '..', 'spec', 'fixtures', 'modules'))
fixtures = {
Expand Down
Loading