Context:
I have a puppet template that addresses the count
sub-component of the processors
fact when it is rendered. That fact exists on all of my clients.
The use case for the fact is in a template line that performs decimal math on it, e.g.: MyConfigVar=<%= 0.9 * @processors['count'] %>
in some .erb
file.
I want to:
- Get my templated code deployed to production hosts.
- Write robust unit tests for my templating so I can be sure it will render properly, given various reasonable values for the fact.
What I've Tried:
First, I tried with sigils: <%= 0.9 * @processors[:count] %>
. If I mocked, with rspec-puppet, something like facts = { :processors => { :count => 10 } }
, my tests all passed. Manifest application didn't work; it had a "can't multiply nil
" error. Sigils apparently are out.
Then, I tried with stringy keys: <%= 0.9 * @processors['count'] %>
. My tests with the sigil (facts = { :processors => { :count => 10 } }
) weren't picked up, but my value was properly found and multiplied with facts = { :processors => { 'count' => 10 } }
. All tests then passed. However, manifest application failed with a Can't coerce String into Int
failure.
Then, I tried with stringy values. The template still read <%= 0.9 * @processors['count'].to_i %>
, and I tested both string and integer values, e.g.
let(:facts) { :processors => { 'count' => '10' } }
# tests with string value
let(:facts) { :processors => { 'count' => 10 } }
# tests with integer value
The tests all passed, but manifest application rendered 0.0
for the value of the fact.
Questions:
Two main questions:
- How can I get inline decimal math working with this (or any) fact, reliably?
- How can I reliably unit-test, using
rspec-puppet
or similar, mocking values in facter with production-representative types?