-
-
Notifications
You must be signed in to change notification settings - Fork 59
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
Fix template to not add trailing space after servers,peers,pools #94
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add some tests to cover these data types?
templates/chrony.conf.epp
Outdated
@@ -1,12 +1,48 @@ | |||
# NTP servers | |||
<% $chrony::servers.each |$server| { -%> | |||
server <%= $server.flatten.join(' ') %> | |||
<% if $chrony::servers.is_a(Hash) { -%> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does that work? I thought it would be is_a?
. Isn't this more native Puppet syntax:
<% if $chrony::servers.is_a(Hash) { -%> | |
<% if $chrony::servers =~ Hash { -%> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is_a?
is a ruby function, is_a
is a function from stdlib, equivalent to =~
. I just
think the object-oriented call looks nicer, and when I se a =~
I'm thinking regexp. But
if you want to I can change it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a bit confusing to me because I'm so used to ERB templates where it would be invalid. Not sure what the best practice would be here. Given the repetition, perhaps a private function chrony::parameter_as_hash()
would be useful to simplify the template? https://puppet.com/docs/puppet/6.17/lang_iteration.html#using-iteration-to-transform-data suggests you can do this with pure Puppet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could work.
<% $chrony::servers.each |$server| { -%>
server <%= $server.flatten.filter |$value| { $value }.join(' ') %>
<% } -%>
i.e. Use flatten to take care of the permitted types, and use filter to remove the
resulting empty element before joining.
I think we should add a test |
The attempt to simplify the template by letting flatten() take care of the different allowed data-types for servers,peers, and pools, was unfortunately a bit too optimistic. The result was that join(' ') on a Array with empty element created a extra space caracter when $chrony::servers was a hash with a key but no value. This commit uses filter to remove empty elements created by flatten before join.
This is another attempt. This time the goal is to keep the template simpler and avoid the I also tried to add a test to detect this, but failed. chrony::servers:
ntp1:
- minpoll 7
ntp2: so i could test with
|
The problem is that you have a filter |
Yes, but I fail to figure out how to test, or more precise how to recreate the content in But I realize now that when I saw the resulting When I try the new test on top of this branch the test passes. So perhaps it's usable |
No, that was wrong. In the previous comment I was lazy and only added the test to the I'll think some more about this, and let you know if I find a solution. |
No, I fail no matter what I try. With a hash in the test-code with servers: And with hieradata chrony::servers:
ntp1:
- minpoll 7
ntp2:
But I don't like that at all. It feels like that filtering undef is the real solution for |
FWIW, in rspec-puppet, you can use the symbol |
This is no longer current or relevant. |
The attempt to simplify the template by letting flatten() take care of the different
allowed data-types for servers,peers, and pools, was unfortunately a bit too
optimistic. The result was that join(' ') on a Array with empty element created a extra
space caracter when $chrony::servers was a hash with a key but no value.