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

Remove cfme repos from MiqDatabase model #11304

Merged
merged 7 commits into from
Sep 16, 2016
Merged
17 changes: 14 additions & 3 deletions app/models/miq_database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class MiqDatabase < ApplicationRecord
encrypt_column :csrf_secret_token
encrypt_column :session_secret_token

validates_presence_of :session_secret_token, :csrf_secret_token, :update_repo_name
validates_presence_of :session_secret_token, :csrf_secret_token

default_values REGISTRATION_DEFAULT_VALUES

Expand All @@ -32,11 +32,22 @@ def self.registration_default_values
end

def self.registration_default_value_for_update_repo_name
"cf-me-5.6-for-rhel-7-rpms rhel-server-rhscl-7-rpms"
Vmdb::Settings.template_settings.product.update_repo_names.to_a.join(" ")
end

def update_repo_names
update_repo_name.split
Settings.product.update_repo_names.to_a
end

def update_repo_name
update_repo_names.join(" ")
end

def update_repo_name=(repos)
return unless repos
hash = {:product => {:update_repo_names => repos.split}}
Vmdb::Settings.save!(MiqRegion.my_region, hash)
Settings.reload!
Copy link
Member

Choose a reason for hiding this comment

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

IF the save! is not doing this we need an alternate method that propogates the changes to other servers.

Copy link
Member Author

Choose a reason for hiding this comment

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

It doesn't look like it is right now. Should I just add a reload! at the end of the Vmdb::Settings.save! method?

Copy link
Member Author

Choose a reason for hiding this comment

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

Can we maybe make this change in a follow up so that we can get the migrations in or do you think it is necessary for this PR?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, as discussed, follow this up, For now, this will work for a single appliance.

end

def self.seed
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class MoveRepoDataFromDatabaseToSettings < ActiveRecord::Migration[5.0]
class MiqRegion < ActiveRecord::Base; end
class MiqDatabase < ActiveRecord::Base; end
class SettingsChange < ActiveRecord::Base
serialize :value
end

SETTING_KEY = "/product/update_repo_names".freeze

def up
db = MiqDatabase.first
return unless db && my_region

say_with_time("Moving repo information from miq_databases to Settings") do
repos = db.update_repo_name.split
SettingsChange.create!(settings_hash.merge(:value => repos))
end
end

def down
return unless my_region
change = SettingsChange.where(settings_hash).first
return unless change

say_with_time("Moving repo information from Settings to miq_databases") do
db = MiqDatabase.first
db.update_attributes!(:update_repo_name => change.value.join(" ")) if db
change.delete
end
end

def my_region
MiqRegion.find_by_region(ApplicationRecord.my_region_number)
end

def settings_hash
{
:resource_type => "MiqRegion",
:resource_id => my_region.id,
:key => SETTING_KEY
}
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RemoveUpdateRepoNameFromMiqDatabases < ActiveRecord::Migration[5.0]
def change
remove_column :miq_databases, :update_repo_name, :string
end
end
1 change: 0 additions & 1 deletion db/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4888,7 +4888,6 @@ miq_databases:
- postgres_update_available
- session_secret_token
- csrf_secret_token
- update_repo_name
- registration_organization_display_name
miq_dialogs:
- id
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require_migration

describe MoveRepoDataFromDatabaseToSettings do
let(:region_stub) { migration_stub(:MiqRegion) }
let(:database_stub) { migration_stub(:MiqDatabase) }
let(:settings_stub) { migration_stub(:SettingsChange) }

let(:region_number) { ApplicationRecord.my_region_number }
let(:region) { region_stub.find_by_region(region_number) }
let(:repo_string) { "my-repo my-other-repo" }
let(:repo_list) { %w(my-repo my-other-repo) }

before do
region_id = ApplicationRecord.region_to_range(region_number).first
region_stub.create(:id => region_id, :region => region_number)
end

migration_context :up do
it "moves the data from miq_databases to the settings" do
database_attrs = {
:session_secret_token => SecureRandom.hex(64),
:csrf_secret_token => SecureRandom.hex(64),
:update_repo_name => repo_string
}
database_stub.create!(database_attrs)

migrate

setting_change = settings_stub.where(
:key => described_class::SETTING_KEY,
:resource_id => region.id,
:resource_type => MiqRegion
).first
expect(setting_change.value).to eq(repo_list)
end
end

migration_context :down do
it "moves the data from the settings to miq_databases" do
database_attrs = {
:session_secret_token => SecureRandom.hex(64),
:csrf_secret_token => SecureRandom.hex(64),
:update_repo_name => nil
}
db = database_stub.create!(database_attrs)
settings_stub.create!(
:key => described_class::SETTING_KEY,
:value => repo_list,
:resource_id => region.id,
:resource_type => MiqRegion
)

migrate

db.reload
expect(db.update_repo_name).to eq(repo_string)
end
end
end
60 changes: 54 additions & 6 deletions spec/models/miq_database_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,56 @@
describe MiqDatabase do
let(:db) { described_class.seed }

let!(:region) do
FactoryGirl.create(:miq_region, :region => ApplicationRecord.my_region_number)
end

context ".registration_default_value_for_update_repo_name" do
it "returns an empty array if the setting is not set" do
expect(described_class.registration_default_value_for_update_repo_name).to eq("")
end

it "returns the template repos as a string when the setting is set" do
stub_template_settings(:product => {:update_repo_names => %w(repo-1 repo-2)})
stub_settings(:product => {:update_repo_names => %w(repo-3 repo-4)})

expect(described_class.registration_default_value_for_update_repo_name).to eq("repo-1 repo-2")
end
end

context "#update_repo_names" do
it "returns an empty array by default" do
expect(db.update_repo_names).to eq([])
end

it "returns the repos as an array when the setting is set" do
repo_names = %w(repo-1 repo-2)
stub_settings(:product => {:update_repo_names => repo_names})

expect(db.update_repo_names).to eq(repo_names)
end
end

context "#update_repo_name" do
it "returns an empty string by default" do
expect(db.update_repo_name).to eq("")
end

it "returns the repos as a string when the setting is set" do
repo_names = %w(repo-1 repo-2)
stub_settings(:product => {:update_repo_names => repo_names})

expect(db.update_repo_name).to eq("repo-1 repo-2")
end
end

context "#update_repo_name=" do
it "sets the update repo names in the setting hash" do
db.update_repo_name = "repo-1 repo-2"
expect(Vmdb::Settings.for_resource(region).product.update_repo_names).to eq(%w(repo-1 repo-2))
end
end

context ".seed" do
include_examples ".seed called multiple times"

Expand All @@ -7,7 +59,6 @@
db = MiqDatabase.seed
expect(db.csrf_secret_token_encrypted).to be_encrypted
expect(db.session_secret_token_encrypted).to be_encrypted
expect(db.update_repo_name).to eq("cf-me-5.6-for-rhel-7-rpms rhel-server-rhscl-7-rpms")
expect(db.registration_type).to eq("sm_hosted")
expect(db.registration_server).to eq("subscription.rhn.redhat.com")
end
Expand All @@ -16,21 +67,18 @@
it "will seed nil values" do
FactoryGirl.build(:miq_database,
:csrf_secret_token => nil,
:session_secret_token => nil,
:update_repo_name => nil
:session_secret_token => nil
).save(:validate => false)

db = MiqDatabase.seed
expect(db.csrf_secret_token_encrypted).to be_encrypted
expect(db.session_secret_token_encrypted).to be_encrypted
expect(db.update_repo_name).to eq("cf-me-5.6-for-rhel-7-rpms rhel-server-rhscl-7-rpms")
end

it "will not change existing values" do
FactoryGirl.create(:miq_database,
:csrf_secret_token => "abc",
:session_secret_token => "def",
:update_repo_name => "ghi"
:session_secret_token => "def"
)
csrf, session, update_repo = MiqDatabase.all.collect { |db| [db.csrf_secret_token, db.session_secret_token, db.update_repo_name] }.first

Expand Down
14 changes: 10 additions & 4 deletions spec/models/miq_server/update_management_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
describe MiqServer do
before do
MiqDatabase.seed
_, @server, = EvmSpecHelper.create_guid_miq_server_zone
end

let(:database) { MiqDatabase.first }
let!(:database) do
FactoryGirl.create(:miq_region, :region => ApplicationRecord.my_region_number)
db = MiqDatabase.seed
db.update_repo_name = "repo-1 repo-2"
db
end

let(:reg_system) { LinuxAdmin::RegistrationSystem }
let(:yum) { LinuxAdmin::Yum }

Expand Down Expand Up @@ -161,7 +166,7 @@
expect(reg_system).to receive(:enable_repo).twice

@server.enable_repos
expect(@server.upgrade_message).to eq("enabling rhel-server-rhscl-7-rpms")
expect(@server.upgrade_message).to eq("enabling repo-2")
end

it "#check_updates" do
Expand All @@ -171,7 +176,7 @@

@server.check_updates

expect(database.cfme_version_available).to eq("3.1")
expect(database.reload.cfme_version_available).to eq("3.1")
end

context "#apply_updates" do
Expand Down Expand Up @@ -217,6 +222,7 @@
context "#cfme_available_update" do
before do
@server.update_attribute(:version, "1.2.3.4")
MiqServer.my_server_clear_cache
end

it "with nil version available" do
Expand Down
5 changes: 5 additions & 0 deletions spec/support/settings_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ def stub_settings(hash)
allow(Vmdb::Settings).to receive(:for_resource) { settings }
end

def stub_template_settings(hash)
settings = Config::Options.new.merge!(hash)
allow(Vmdb::Settings).to receive(:template_settings) { settings }
end

def stub_local_settings(my_server)
stub_const("Settings", Vmdb::Settings.for_resource(my_server))
end
Expand Down