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

Fixes #24886 - fixed audit in seeds test #6058

Merged
merged 1 commit into from
Oct 2, 2018

Conversation

lzap
Copy link
Member

@lzap lzap commented Sep 11, 2018

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.

@theforeman-bot
Copy link
Member

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
Copy link
Member Author

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
Copy link
Member Author

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.

yield
ensure
klass.enable_auditing if auditing_was_enabled
end
Copy link
Member Author

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
Copy link
Member

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

@mmoll
Copy link
Contributor

mmoll commented Sep 11, 2018

tests fail reliably now :)

klass.enable_auditing
yield
ensure
klass.enable_auditing if auditing_was_enabled
Copy link
Member

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?

Copy link
Member Author

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. :-)

Copy link
Member Author

@lzap lzap Sep 18, 2018

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

Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member

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?

Copy link
Member Author

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`

@@ -35,11 +39,11 @@ def seed
seed

tables.each do |model|
refute model.unscoped.count.zero?
assert_not model.unscoped.count.zero?
Copy link
Member

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?

Copy link
Member Author

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)
Copy link
Member

Choose a reason for hiding this comment

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

nice!

@lzap
Copy link
Member Author

lzap commented Sep 18, 2018

I believe no changes are required @tbrisker however tests were failing because my previous change, fixed that.

@lzap
Copy link
Member Author

lzap commented Sep 25, 2018

Ping anyone can do quick merge here? I believe this is RTM. @ares or @timogoebel

@lzap
Copy link
Member Author

lzap commented Oct 2, 2018

Removed assert_not change and .gitignore change.

@lzap
Copy link
Member Author

lzap commented Oct 2, 2018

How about this?

@ares ares dismissed tbrisker’s stale review October 2, 2018 10:16

comments were addressed

Copy link
Member

@ares ares left a 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

@ares
Copy link
Member

ares commented Oct 2, 2018

Thanks @lzap, merging!

@ares ares merged commit bb93b26 into theforeman:develop Oct 2, 2018
@timogoebel
Copy link
Member

... what about @tbrisker and my comment about resetting the audit state?
I believe this was still pending here.

@ares
Copy link
Member

ares commented Oct 2, 2018

@timogoebel I believe it has been resolved, it was changed to disable unless was_enabled logic, which is I believe correct. If we enable it, we ensure it's disabled back unless it wasn't enabled alreasy

@timogoebel
Copy link
Member

@ares: Oh, I see it now. Sorry for the confusion. All good. Would have merged it earlier, but needed this explanation. 👼

@ares
Copy link
Member

ares commented Oct 2, 2018

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!

@lzap
Copy link
Member Author

lzap commented Oct 3, 2018

Yeah this kind of setter/getter is confusing. But it works.

@lzap lzap deleted the seed-audit-faster-24886 branch October 3, 2018 15: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.

7 participants