-
Notifications
You must be signed in to change notification settings - Fork 992
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
Fixes #24886 - fixed audit in seeds test #6058
Conversation
Issues: #24886 |
db/seeds.rb
Outdated
@@ -13,7 +13,7 @@ def format_errors(model = nil) | |||
end | |||
|
|||
# now we load all seed files | |||
foreman_seeds = Dir.glob(Rails.root + 'db/seeds.d/*.rb') | |||
foreman_seeds = Dir.glob(Rails.root + 'db/seeds.d/*.rb').sort |
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.
Oh this was left over. Removing.
tmpl = ProvisioningTemplate.unscoped.find_by_name('Kickstart default') | ||
tmpl.name = 'test' | ||
tmpl.save! | ||
end |
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 is the key thing that fixes random failures - ensures that auditing is enabled during this test.
7564084
to
d641d40
Compare
yield | ||
ensure | ||
klass.enable_auditing if auditing_was_enabled | ||
end |
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.
I don't think this is useful outside of testing context, so not putting this into model classes.
seed_files.each do |file| | ||
load File.expand_path("../../../db/seeds.d/#{file}", __dir__) | ||
end | ||
end |
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 also be written like this, saving a few lines and DRY it up.
seed_files = ['seeds.rb'] if seed_files.empty?
seed_files.each do |file|
load File.expand_path("../../../db/seeds.d/#{file}", __dir__)
end
d641d40
to
8a9ddcb
Compare
tests fail reliably now :) |
test/test_helper.rb
Outdated
klass.enable_auditing | ||
yield | ||
ensure | ||
klass.enable_auditing if auditing_was_enabled |
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.
shouldn't this be klass.auditing_enabled = auditing_was_enabled
to ensure it is reverted to the same state as before?
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 was a copy paste from audited gem, sure I can do that. :-)
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.
Oh the method is enable_auditing
and not enable_auditing=
actually.
def without_auditing
auditing_was_enabled = auditing_enabled
disable_auditing
yield
ensure
enable_auditing if auditing_was_enabled
end
def disable_auditing
self.auditing_enabled = false
end
def enable_auditing
self.auditing_enabled = true
end
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.
I think @tbrisker has a valid point here, do we want to revert this to the original state?
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.
Again, this method was taken from audited gem, there is no enable_auditing=
method available. Please elaborate what's wrong.
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.
I am wondering if klass.disable_auditing unless auditing_was_enabled
is missing.
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.
I think this is still not resolved, original value is not being restored in ensure, I think this would be necessary "auditing_was_enabled ? klass.enable_auditing : klass.disable_auditing"
in ensure block
perhaps link audited code you copied here?
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.
NOW I get it! This code was from audited gem, but they had the opposite with_auditing". Looks like I only need this then:
klass.disable_auditing unless auditing_was_enabled`
test/unit/tasks/seeds_test.rb
Outdated
@@ -35,11 +39,11 @@ def seed | |||
seed | |||
|
|||
tables.each do |model| | |||
refute model.unscoped.count.zero? | |||
assert_not model.unscoped.count.zero? |
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.
iiuc, the difference between refute
and assert_not
is that refute expects false
while assert_not
also accepts nil
. Is there a condition in which this and other assertions you changed return nil
instead of false
?
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.
Good point, I haven't found any.
@@ -14,10 +15,13 @@ class SeedsTest < ActiveSupport::TestCase | |||
Foreman.stubs(:in_rake?).returns(true) | |||
end | |||
|
|||
def seed | |||
def seed(*seed_files) |
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.
nice!
8a9ddcb
to
e5d45b7
Compare
I believe no changes are required @tbrisker however tests were failing because my previous change, fixed that. |
Ping anyone can do quick merge here? I believe this is RTM. @ares or @timogoebel |
e5d45b7
to
1ba2c9b
Compare
Removed |
How about this? |
1ba2c9b
to
ec9b110
Compare
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.
Tests failres are unrelated, all comments were IMHO addressed, looks good to me too
Thanks @lzap, merging! |
... what about @tbrisker and my comment about resetting the audit state? |
@timogoebel I believe it has been resolved, it was changed to |
@ares: Oh, I see it now. Sorry for the confusion. All good. Would have merged it earlier, but needed this explanation. 👼 |
No worries, we talked with lzap this morning on IRV and the final version was pushed just few hours ago :-) thanks for keeping eyes on what goes in! |
Yeah this kind of setter/getter is confusing. But it works. |
For some reason, audit was disabled randomly for all model classes. This
has to do with how Audited gem is loaded by bundler perhaps, not sure.
This patch enables it explicitly.
It also makes seeds test faster by only seeding minimum files required
for individual tests.