Skip to content

Commit 86b0aac

Browse files
committed
Merge pull request #40 from stbenjam/bugs2
Fix discovery and an inheritance issue
2 parents a95f795 + 4424e3c commit 86b0aac

File tree

5 files changed

+30
-22
lines changed

5 files changed

+30
-22
lines changed

app/controllers/foreman_salt/minions_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def load_ajax_vars
6969
@minion = Host::Base.authorized(:view_hosts, Host).find_by_id(params[:host_id])
7070
if @minion
7171
unless @minion.is_a?(Host::Managed)
72-
@minion = @host.becomes(Host::Managed)
72+
@minion = @minion.becomes(Host::Managed)
7373
@minion.type = 'Host::Managed'
7474
end
7575
@minion.attributes = params[:host]

app/models/foreman_salt/concerns/host_managed_extensions.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@ def params_with_salt_proxy
3333
end
3434

3535
def salt_modules_for_enc
36-
return [] unless self.salt_environment
37-
38-
hostgroup_modules = self.hostgroup ? self.hostgroup.all_salt_modules : []
39-
self.salt_environment.salt_modules.where(:id => self.salt_modules + hostgroup_modules).pluck(:name)
36+
return [] unless salt_environment
37+
modules = salt_modules + (hostgroup ? hostgroup.all_salt_modules : [])
38+
ForemanSalt::SaltModule.in_environment(salt_environment).where(:id => modules).pluck(:name).uniq
4039
end
4140

4241
def salt_master

app/models/foreman_salt/concerns/hostgroup_extensions.rb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,15 @@ module HostgroupExtensions
1616
end
1717

1818
def all_salt_modules
19-
if ancestry.present?
20-
(self.salt_modules + self.inherited_salt_modules).uniq
21-
else
22-
self.salt_modules
23-
end
19+
ForemanSalt::SaltModule.in_environment(salt_environment).where(:id => salt_module_ids + inherited_salt_module_ids)
2420
end
2521

2622
def inherited_salt_modules
2723
ForemanSalt::SaltModule.where(:id => inherited_salt_module_ids)
2824
end
2925

3026
def inherited_salt_module_ids
31-
if ancestry.present?
32-
self.class.sort_by_ancestry(ancestors.reject { |ancestor| ancestor.salt_module_ids.empty? }).map(&:salt_module_ids).inject(&:+).uniq
33-
else
34-
[]
35-
end
27+
ancestors.map(&:salt_module_ids).flatten.uniq
3628
end
3729

3830
def salt_proxy

test/factories/foreman_salt_factories.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,25 @@
44
end
55

66
factory :salt_environment, :class => 'ForemanSalt::SaltEnvironment' do
7-
sequence(:name) { |n| "module#{n}" }
7+
sequence(:name) { |n| "environment#{n}" }
88
end
99
end
1010

1111
FactoryGirl.modify do
1212
factory :host do
1313
trait :with_salt_proxy do
14-
salt_proxy { FactoryGirl.create :smart_proxy, :with_salt_feature }
14+
salt_proxy { FactoryGirl.build :smart_proxy, :with_salt_feature }
1515
end
1616
end
1717

1818
factory :hostgroup do
1919
trait :with_salt_proxy do
20-
salt_proxy { FactoryGirl.create :smart_proxy, :with_salt_feature }
20+
salt_proxy { FactoryGirl.build :smart_proxy, :with_salt_feature }
2121
end
2222

2323
trait :with_salt_modules do
24-
salt_modules { FactoryGirl.create_list :salt_module, 10 }
24+
salt_environment { FactoryGirl.build :salt_environment }
25+
salt_modules { FactoryGirl.create_list :salt_module, 10, :salt_environments => [self.salt_environment] }
2526
end
2627
end
2728

test/unit/hostgroup_extensions_test.rb

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,32 @@ class HostgroupExtensionsTest < ActiveSupport::TestCase
2525
end
2626

2727
test 'child and parent salt modules are combined' do
28+
environment = FactoryGirl.create :salt_environment
29+
parent = FactoryGirl.create :hostgroup, :with_salt_modules, :salt_environment => environment
30+
child = FactoryGirl.create :hostgroup, :with_salt_modules, :salt_environment => environment, :parent => parent
31+
32+
total = parent.salt_modules.count + child.salt_modules.count
33+
assert_equal total, child.all_salt_modules.count
34+
end
35+
36+
test 'child doesnt get modules from outside its environment' do
2837
parent = FactoryGirl.create :hostgroup, :with_salt_modules
2938
child = FactoryGirl.create :hostgroup, :with_salt_modules, :parent => parent
30-
assert_equal 10, (child.salt_modules - parent.salt_modules).length
39+
assert_equal child.salt_modules.count, child.all_salt_modules.count
3140
end
3241

33-
test 'second child inherits from parent' do
42+
test 'inheritance when only parent has modules' do
3443
parent = FactoryGirl.create :hostgroup, :with_salt_modules
3544
child_one = FactoryGirl.create :hostgroup, :parent => parent
3645
child_two = FactoryGirl.create :hostgroup, :parent => child_one
37-
assert_equal [], parent.all_salt_modules - child_two.all_salt_modules
46+
assert_blank parent.all_salt_modules - child_two.all_salt_modules
47+
end
48+
49+
test 'inheritance when no parents have modules' do
50+
parent = FactoryGirl.create :hostgroup
51+
child_one = FactoryGirl.create :hostgroup, :parent => parent
52+
child_two = FactoryGirl.create :hostgroup, :with_salt_modules, :parent => child_one
53+
assert child_two.all_salt_modules.any?
3854
end
3955
end
4056
end

0 commit comments

Comments
 (0)