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

Use facterdb_string_keys configuration option for custom facts #157

Merged

Conversation

jordanbreen28
Copy link
Contributor

@jordanbreen28 jordanbreen28 commented Jul 19, 2023

When passing custom facts, it would result in a duplicate fact key if those facts were already present, for example:

 puppetversion: "7.25.0",
 facterversion: "4.4.1"

would result in a facts hash like below being returned

:facterversion=>"4.2.11",
:puppetversion=>"7.25.0",
...
"puppetversion"=>"7.25.0",
"facterversion"=>"4.4.1",

Now, when run, the logic will update the element already present in the hash, as opposed to duplicating the sym key as a string.

:puppetversion =>"7.25.0",
:facterversion =>"4.4.1",

Fixes #142

@codecov
Copy link

codecov bot commented Jul 19, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (6ac4fba) 94.93% compared to head (ab01ecc) 95.23%.
Report is 8 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #157      +/-   ##
==========================================
+ Coverage   94.93%   95.23%   +0.30%     
==========================================
  Files           2        2              
  Lines         158      168      +10     
==========================================
+ Hits          150      160      +10     
  Misses          8        8              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jordanbreen28 jordanbreen28 marked this pull request as draft July 19, 2023 12:28
Copy link
Member

@ekohl ekohl left a comment

Choose a reason for hiding this comment

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

Deep merging by default feels wrong, because then you can't replace facts entirely anymore.

You're also ignoring RSpec.configuration.facterdb_string_keys exists. We should really do a breaking change at some point to and flip it to true. That does mean everyone needs to change their specs. I've also thought about ActiveSupport::HashWithIndifferentAccess but that's a dependency I don't want to pull in.

lib/rspec-puppet-facts.rb Outdated Show resolved Hide resolved
@jordanbreen28
Copy link
Contributor Author

jordanbreen28 commented Jul 19, 2023

Deep merging by default feels wrong, because then you can't replace facts entirely anymore.

You're also ignoring RSpec.configuration.facterdb_string_keys exists. We should really do a breaking change at some point to and flip it to true. That does mean everyone needs to change their specs. I've also thought about ActiveSupport::HashWithIndifferentAccess but that's a dependency I don't want to pull in.

Agreed, seems like bit of an anti-pattern in hindsight to have this deep_merge all the time..
I'll get back to the drawing board and update this as soon as I can.

@jordanbreen28 jordanbreen28 reopened this Jul 19, 2023
@jordanbreen28 jordanbreen28 force-pushed the bug-fix_merging_custom_facts branch 2 times, most recently from 1b0e86b to 0121a50 Compare July 20, 2023 15:07
@jordanbreen28 jordanbreen28 marked this pull request as ready for review July 21, 2023 07:28
@jordanbreen28 jordanbreen28 changed the title (bug) - Prevent duplicate fact keys (FEAT) - Prevent duplicate fact keys & Add merge_facts option Sep 19, 2023
Copy link
Member

@ekohl ekohl left a comment

Choose a reason for hiding this comment

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

There is also the weird bit where top level facts are symbolized but all other levels aren't (and thus strings). Right now I can see someone trying to add a custom (structured) fact to override built in facts and use symbols for the second level. So

add_custom_fact 'identity', { user: "test_user" }, merge_facts: true

The result would be:

{
  "gid"=>0,
  "group"=>"root",
  "privileged"=>true,
  "uid"=>0,
  "user"=>"root",
  :user => "test_user"
}

I tried to look at a setting for this, but I don't think there is one. It happens here: https://github.com/voxpupuli/facterdb/blob/4d3f5e846de545dad89ad5e4196024eedbcaeec2/lib/facterdb.rb#L146

lib/rspec-puppet-facts.rb Outdated Show resolved Hide resolved
@jordanbreen28
Copy link
Contributor Author

There is also the weird bit where top level facts are symbolized but all other levels aren't (and thus strings). Right now I can see someone trying to add a custom (structured) fact to override built in facts and use symbols for the second level. So

add_custom_fact 'identity', { user: "test_user" }, merge_facts: true

The result would be:

{
  "gid"=>0,
  "group"=>"root",
  "privileged"=>true,
  "uid"=>0,
  "user"=>"root",
  :user => "test_user"
}

I tried to look at a setting for this, but I don't think there is one. It happens here: https://github.com/voxpupuli/facterdb/blob/4d3f5e846de545dad89ad5e4196024eedbcaeec2/lib/facterdb.rb#L146

Yeah okay that is strange.. Not ideal.

lib/rspec-puppet-facts.rb Outdated Show resolved Hide resolved
lib/rspec-puppet-facts.rb Outdated Show resolved Hide resolved
@jordanbreen28
Copy link
Contributor Author

thanks @ekohl i'll look to address your comments shortly

@jordanbreen28 jordanbreen28 force-pushed the bug-fix_merging_custom_facts branch 3 times, most recently from 0421a21 to 86ad164 Compare January 24, 2024 14:34
@jordanbreen28 jordanbreen28 force-pushed the bug-fix_merging_custom_facts branch 2 times, most recently from b6bb647 to 16bca72 Compare January 26, 2024 12:39
lib/rspec-puppet-facts.rb Outdated Show resolved Hide resolved
@jordanbreen28 jordanbreen28 force-pushed the bug-fix_merging_custom_facts branch 2 times, most recently from 27e252d to 5c54e93 Compare January 26, 2024 15:36
@jordanbreen28
Copy link
Contributor Author

Let me know if anything else is needed.

Copy link
Member

@ekohl ekohl left a comment

Choose a reason for hiding this comment

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

For the changelog I would really want the custom fact as symbol change separated into its own PR because that part backwards incompatible requiring a major version bump. It does break test suites that have conditionals on custom facts.

@jordanbreen28 jordanbreen28 changed the title (FEAT) - Prevent duplicate fact keys & Add merge_facts option (GH-142) - Prevent duplicate fact keys Jan 29, 2024
@jordanbreen28 jordanbreen28 force-pushed the bug-fix_merging_custom_facts branch 4 times, most recently from 3456d6e to 7f6dcd6 Compare January 29, 2024 20:12
@jordanbreen28
Copy link
Contributor Author

I've separated the two changes between here and #160.
Once this is merged, I'll work to get #160 in shape, hoping a rebase is all it will need but we will see..

Previously, when registering a custom fact with rspec-puppet-facts, the
fact key would be converted to a string. This lead to duplicate facts
being present in the facts hash, as default fact keys are stored as sym.

To fix this, we remove a `to_s` conversion on the key in the
register_custom_fact method, unless the rspec config stringify keys is true.
@ekohl ekohl changed the title (GH-142) - Prevent duplicate fact keys Use facterdb_string_keys configuration option for custom facts Jan 30, 2024
Copy link
Member

@ekohl ekohl left a comment

Choose a reason for hiding this comment

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

I've rephrased the PR title to better capture what it's covering. I think this is now ready to merge.

@jordanbreen28
Copy link
Contributor Author

@ekohl thanks for all your guidance with this one

@bastelfreak
Copy link
Member

Hi,
We've quite a few unreleased PRs already merged. Two are marked as breaking:

Breaking changes:

objections on how we proceed? Should we merge #157 and release it with those changes? Or do two major releases in a row?

@ekohl
Copy link
Member

ekohl commented Jan 30, 2024

I'd combine them. Possibly also di the breaking change in FacterDB of removing old stuff.

I can't find the issue now, but I'd also consider swapping the string keys value default and only deal with strings instead of symbols/strings

@jordanbreen28
Copy link
Contributor Author

@ekohl @bastelfreak 👋 can we merge this and I'll start looking at what #160 needs?

@bastelfreak bastelfreak merged commit ec05ede into voxpupuli:master Feb 13, 2024
8 checks passed
@jordanbreen28 jordanbreen28 deleted the bug-fix_merging_custom_facts branch February 13, 2024 10:40
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.

"add_custom_fact" creates String-Facts instead of Symbol-Facts
3 participants