Skip to content

Commit

Permalink
(CAT-1742) Update hiera README
Browse files Browse the repository at this point in the history
Prior to this commit, the README.md file was not up to date with the
latest information on hiera usage.

This commit aims to update the README.md to ensure that users have
access to the correct information, based on our documentation.
  • Loading branch information
LukasAud committed Mar 22, 2024
1 parent 6888827 commit f223efd
Showing 1 changed file with 32 additions and 41 deletions.
73 changes: 32 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -888,66 +888,57 @@ end

## Hiera integration

### Configuration

Set the hiera config symbol properly in your spec files:
At some point, you might want to make use of Hiera to bring in custom parameters for your class tests. In this section, we will
provide you with basic guidance to setup Hiera implementation within rspec testing. For more information on Hiera, you should
check our official [documentation](https://www.puppet.com/docs/puppet/8/hiera.html).

```ruby
let(:hiera_config) { 'spec/fixtures/hiera/hiera.yaml' }
hiera = Hiera.new(:config => 'spec/fixtures/hiera/hiera.yaml')
```
### Configuration

Create your spec hiera files
The first step is to create the general hiera configuration file. Since we want this to be exclusive for testing, we recommend creating
it inside your spec folder. Something along the lines of `spec/fixtures/hiera/hiera.yaml`. It should look something like this:

spec/fixtures/hiera/hiera.yaml
```ruby
```yaml
---
:backends:
- yaml
:hierarchy:
- test
:yaml:
:datadir: 'spec/fixtures/hiera'
version: 5
defaults: # Used for any hierarchy level that omits these keys.
datadir: data # This path is relative to hiera.yaml's directory.
data_hash: yaml_data # Use the built-in YAML backend.

hierarchy:
- name: 'rspec'
path: 'rspec-data.yaml'
```
spec/fixtures/hiera/test.yaml
```ruby
---
ntpserver: ['ntp1.domain.com','ntpXX.domain.com']
user:
oneuser:
shell: '/bin/bash'
twouser:
shell: '/sbin/nologin'
It is often recommended to use dummy data during testing to avoid real values from being entangled. In order to create
these values, we will need a new file containing this data exclusively, normally existing within a subfolder called `data`, ending up with
`spec/fixtures/hiera/data/rspec-data.yaml`. Here is an example of its contents:

```yaml
ntpserver: 'ntp1.domain.com'
user: 'guest'
password: 'unsafepassword1'
```

### Use hiera in your tests
Finally, we make the target class spec file load the Hiera config, at which point we will be able to freely access it:

```ruby
ntpserver = hiera.lookup('ntpserver', nil, nil)
let(:params) { 'ntpserver' => ntpserver }
let(:hiera_config) { 'spec/fixtures/hiera/hiera.yaml' }
```
#### Enabling lookup()

### Enabling hiera lookups
If you just want to fetch values from hiera (e.g. because
you're testing code that uses explicit hiera lookups) just specify
the path to the hiera config in your `spec_helper.rb`
There will also be situations in which you will simply want to fetch values from hiera across one or many testing files(e.g. because you're testing code
that uses explicit hiera lookups). In this case, just specify the path to the hiera config in your `spec_helper.rb`:

```ruby
RSpec.configure do |c|
c.hiera_config = 'spec/fixtures/hiera/hiera.yaml'
end
```

spec/fixtures/hiera/hiera.yaml
```yaml
---
:backends:
- yaml
:yaml:
:datadir: spec/fixtures/hieradata
:hierarchy:
- common
Once that is set, you should be able to start using the lookup function

```ruby
ntpserver = lookup('ntpserver')
```

**Please note:** In-module hiera data depends on having a correct metadata.json file. It is
Expand Down

0 comments on commit f223efd

Please sign in to comment.