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

allow MiqDialog to be seeded from plugins... #14653

Merged
merged 4 commits into from
Apr 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions app/models/miq_dialog.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
class MiqDialog < ApplicationRecord
validates_presence_of :name, :description
validates_uniqueness_of :name, :scope => :dialog_type, :case_sensitive => false

DIALOG_DIR = Rails.root.join("product/dialogs/miq_dialogs")
validates :name, :description, :presence => true
validates :name, :uniqueness => { :scope => :dialog_type, :case_sensitive => false }

DIALOG_TYPES = [
[_("VM Provision"), "MiqProvisionWorkflow"],
[_("Configured System Provision"), "MiqProvisionConfiguredSystemWorkflow"],
[_("Host Provision"), "MiqHostProvisionWorkflow"],
[_("VM Migrate"), "VmMigrateWorkflow"],
]
].freeze

serialize :content

def self.seed
sync_from_dir
sync_from_dir(Rails.root.join('product', 'dialogs', 'miq_dialogs'))
sync_from_plugins
end

def self.sync_from_dir(root)
Dir.glob(root.join("*.{yaml,yml}")).each { |f| sync_from_file(f, root) }
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'm refactoring seeding of dialog.rb as well and noticed this loads .yaml and .yml files. So should we do here too

Copy link
Member

Choose a reason for hiding this comment

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

👍

end

def self.sync_from_dir
Dir.glob(File.join(DIALOG_DIR, "*.yaml")).each { |f| sync_from_file(f) }
def self.sync_from_plugins
Vmdb::Plugins.instance.registered_provider_plugins.each do |plugin|
sync_from_dir(plugin.root.join('content', 'miq_dialogs'))
end
end

def self.sync_from_file(filename)
def self.sync_from_file(filename, root)
item = YAML.load_file(filename)

item[:filename] = filename.sub(DIALOG_DIR.to_path + "/", "")
item[:filename] = filename.sub("#{root}/", "")
item[:file_mtime] = File.mtime(filename).utc
item[:default] = true

Expand Down
27 changes: 27 additions & 0 deletions spec/models/miq_dialog_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
describe MiqDialog do
context '.seed' do
it 'seeds from the core with correct metadata' do
root = Rails.root.join('product', 'dialogs', 'miq_dialogs')
allow(described_class).to receive(:find_by)
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 expect(described_class).to receive(:find_by).once.with(...) would be much simpler than allow and expect().to have_received().

Copy link
Member Author

Choose a reason for hiding this comment

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

right, when I was trying that, rspec complained about the other calls - now it works 🤷‍♂️
thanks for letting me re-visit that

Copy link
Member Author

Choose a reason for hiding this comment

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

here it is still, I need to stub find_by with allow(described_class).to receive(:find_by) otherwise I get this:
same is true for :sync_from_dir below.

Failures:

  1) MiqDialog.seed seeds from the core with correct metadata
     Failure/Error: rec = find_by(:name => item[:name], :filename => item[:filename])

       #<MiqDialog(id: integer, name: string, description: string, dialog_type: string, content: text, default: boolean, filename: string, file_mtime: datetime, created_at: datetime, updated_at: datetime, href_slug: string, region_number: integer, region_description: string) (class)> received :find_by with unexpected arguments
         expected: ({:name=>"miq_host_provision_dialogs", :filename=>"miq_host_provision_dialogs.yaml"})
              got: ({:name=>"miq_provision_amazon_dialogs_template", :filename=>"miq_provision_amazon_dialogs_template.yaml"})
       Diff:
       @@ -1,3 +1,3 @@
       -[{:name=>"miq_host_provision_dialogs",
       -  :filename=>"miq_host_provision_dialogs.yaml"}]
       +[{:name=>"miq_provision_amazon_dialogs_template",
       +  :filename=>"miq_provision_amazon_dialogs_template.yaml"}]

     # ./app/models/miq_dialog.rb:36:in `sync_from_file'
     # ./app/models/miq_dialog.rb:20:in `block in sync_from_dir'
     # ./app/models/miq_dialog.rb:20:in `each'
     # ./app/models/miq_dialog.rb:20:in `sync_from_dir'
     # ./app/models/miq_dialog.rb:15:in `seed'
     # ./spec/models/miq_dialog_spec.rb:11:in `block (3 levels) in <top (required)>'
     # -e:1:in `<main>'

Copy link
Member

Choose a reason for hiding this comment

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

I see 👍


expect(described_class).to receive(:sync_from_file).at_least(:once).with(/^#{root}/, root).and_call_original
expect(described_class).to receive(:find_by).once.with(
:name => "miq_host_provision_dialogs", :filename => "miq_host_provision_dialogs.yaml"
)

described_class.seed
end

it 'seed from plugins' do
mock_engine = double(:root => Pathname.new('/some/root'))
allow(described_class).to receive(:sync_from_dir)

expect(Vmdb::Plugins.instance).to receive(:registered_provider_plugins).and_return([mock_engine])
expect(described_class).to receive(:sync_from_dir).once.with(
mock_engine.root.join('content', 'miq_dialogs')
)

described_class.seed
end
end
end