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

Rewrite fqdn_rand_string() as a Puppet 4.x function #1343

Merged
merged 2 commits into from
May 4, 2023
Merged

Conversation

smortex
Copy link
Collaborator

@smortex smortex commented Apr 27, 2023

The 3.x function rely on is_integer() which is deprecated. Rewrite it using the more modern puppet 4.x function to rely on data types for better parameters validation.

  • Rework the tests to check actual values;
  • Rewrite fqdn_rand_string() as a Puppet 4.x function.

@smortex smortex force-pushed the fqdn_rand_string branch 2 times, most recently from ea6fb65 to 0353cae Compare April 27, 2023 23:50
@alexjfisher

This comment was marked as off-topic.

@smortex

This comment was marked as off-topic.

@smortex smortex marked this pull request as ready for review April 28, 2023 00:16
it { is_expected.to run.with_params(1, 1).and_raise_error(ArgumentError, %r{parameter 'charset' expects a String value, got Integer}) }
it { is_expected.to run.with_params(1, []).and_raise_error(ArgumentError, %r{parameter 'charset' expects a String value, got Array}) }
it { is_expected.to run.with_params(1, {}).and_raise_error(ArgumentError, %r{parameter 'charset' expects a String value, got Hash}) }
it { is_expected.to run.with_params('100').and_raise_error(ArgumentError, %r{parameter 'length' expects an Integer value, got String}) }
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

'100' was allowed by is_integer() and was removed bellow.

it { is_expected.to run.with_params(1, []).and_raise_error(ArgumentError, %r{parameter 'charset' expects a String value, got Array}) }
it { is_expected.to run.with_params(1, {}).and_raise_error(ArgumentError, %r{parameter 'charset' expects a String value, got Hash}) }
it { is_expected.to run.with_params('100').and_raise_error(ArgumentError, %r{parameter 'length' expects an Integer value, got String}) }
it { is_expected.to run.with_params(100, nil).and_raise_error(ArgumentError, %r{parameter 'charset' expects a String value, got Undef}) }
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

nil was accepted with 3.x function and is rejected by 4.x function. It was remove bellow.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I might create a PR to revert this though. fqdn_rand_string(20, undef, 'MY_SEED') was a perfectly acceptable way of calling the function IMO.

# optional seed for repeatable randomness.
#
# Optionally, you can specify a character set for the function (defaults to alphanumeric).
Puppet::Functions.create_function(:fqdn_rand_string) do
Copy link
Collaborator

Choose a reason for hiding this comment

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

ditto re namespacing

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, it is the plan 😁 I am currently playing three-cushion billiards:

  1. Adjust non-deprecated functions so that they do not rely on deprecated functions; <--- we are here
  2. Remove deprecated functions (since they have been deprecated for a long time);
  3. "Break things" and namespace functions. <--- what you are talking about

The above is pretty clean in my head but I guess I should dump this somewhere where we can track progress, see what is coming next and so on. I am really not good at this, and any suggestion is welcome!

Copy link
Collaborator

Choose a reason for hiding this comment

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

ack, feel free to close all comments related to this and we can deal with the general issue in #1346

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Great! I subscribed to the issue. Thanks!

Rather that testing that the function return the same or different
values, test the actual returned value so that we are sure the behavior
does not change unexpectedly.
The 3.x function rely on is_integer() which is deprecated.  Rewrite it
using the more modern puppet 4.x function to rely on data types for
better parameters validation.
@binford2k binford2k merged commit 3378b3a into main May 4, 2023
@binford2k binford2k deleted the fqdn_rand_string branch May 4, 2023 19:11
# fqdn_rand_string(10, '', 'custom seed')
dispatch :fqdn_rand_string do
param 'Integer[1]', :length
optional_param 'String', :charset
Copy link
Collaborator

Choose a reason for hiding this comment

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

Was it necessary to make this a breaking change? I have code that called the function like this fqdn_rand_string(20, undef, 'MY_SEED')

Copy link
Collaborator

Choose a reason for hiding this comment

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

Fixed in #1367

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants