Skip to content

Commit

Permalink
Add check for invalid roles in server_role_names=
Browse files Browse the repository at this point in the history
  • Loading branch information
Fryguy committed Oct 26, 2020
1 parent 7ea2845 commit c85d1ac
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
7 changes: 6 additions & 1 deletion app/models/miq_server/role_management.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,12 @@ def server_role_names=(roles)
if roles.blank?
server_roles.delete_all
else
desired = (roles == "*" ? ServerRole.all_names : roles.map { |role| role.strip.downcase }.sort)
all_roles = ServerRole.all_names

desired = (roles == "*" ? all_roles : roles.map { |role| role.strip.downcase }.sort)
invalid = desired - all_roles
raise ArgumentError, _("Roles <%{names}> not defined") % {:names => invalid.join(", ")} if invalid.any?

current = server_role_names

# MiqServer#server_role_names may include database scoped roles, which are managed elsewhere,
Expand Down
2 changes: 2 additions & 0 deletions spec/models/miq_server/configuration_management_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
let(:miq_server) { EvmSpecHelper.local_miq_server.tap(&:setup_drb_variables) }

it "reloads the new changes into the settings for the resource" do
ServerRole.seed

Vmdb::Settings.save!(miq_server, :some_test_setting => 2)
expect(Settings.some_test_setting).to be_nil
expect(miq_server).to receive(:notify_workers_of_config_change)
Expand Down
8 changes: 8 additions & 0 deletions spec/models/miq_server/role_management_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
@miq_server.role = desired
expect(@miq_server.server_role_names).to eq(%w( ems_operations scheduler ))
end

it "with an invalid role name" do
expect { @miq_server.role = 'foo' }.to raise_error(ArgumentError, /not defined/)
end
end

context "server_role_names=" do
Expand Down Expand Up @@ -88,6 +92,10 @@
@miq_server.server_role_names = desired
expect(@miq_server.server_role_names).to eq(%w[ems_operations scheduler])
end

it "with an invalid role name" do
expect { @miq_server.server_role_names = ['foo'] }.to raise_error(ArgumentError, /not defined/)
end
end

it "should assign role properly when requested" do
Expand Down

0 comments on commit c85d1ac

Please sign in to comment.