foreman_discovery 11.0.0 → 12.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. checksums.yaml +5 -5
  2. data/app/controllers/api/v2/discovered_hosts_controller.rb +1 -2
  3. data/app/controllers/concerns/foreman/controller/discovered_extensions.rb +1 -1
  4. data/app/controllers/discovered_hosts_controller.rb +21 -11
  5. data/app/controllers/foreman_discovery/concerns/hosts_controller_extensions.rb +1 -1
  6. data/app/helpers/discovered_hosts_helper.rb +9 -11
  7. data/app/models/discovery_rule.rb +3 -0
  8. data/app/models/host/discovered.rb +59 -19
  9. data/app/models/setting/discovered.rb +1 -0
  10. data/app/services/foreman_discovery/fact_parser.rb +6 -2
  11. data/app/services/foreman_discovery/ui_notifications/new_host.rb +3 -1
  12. data/app/views/dashboard/_discovery_widget_host_list.html.erb +1 -1
  13. data/app/views/discovered_hosts/_discovered_hosts_list.html.erb +2 -3
  14. data/app/views/discovered_hosts/_selected_hosts.html.erb +1 -1
  15. data/app/views/discovered_hosts/index.html.erb +0 -2
  16. data/app/views/discovered_hosts/multiple_auto_provision.html.erb +4 -0
  17. data/app/views/discovered_hosts/multiple_reboot.html.erb +4 -0
  18. data/app/views/discovered_hosts/show.html.erb +27 -15
  19. data/app/views/discovery_rules/_form.html.erb +9 -3
  20. data/app/views/discovery_rules/edit.html.erb +1 -1
  21. data/app/views/discovery_rules/index.html.erb +2 -2
  22. data/app/views/foreman_discovery/debian_kexec.erb +1 -1
  23. data/app/views/foreman_discovery/redhat_kexec.erb +1 -1
  24. data/config/as_deprecation_whitelist.yaml +395 -0
  25. data/config/routes.rb +7 -5
  26. data/db/migrate/20171222120314_add_constraints_on_discovery_rules_hostgroups.rb +10 -0
  27. data/db/migrate/20180412124505_add_priority_score_to_discovery_rules.rb +24 -0
  28. data/extra/discover-host +7 -5
  29. data/extra/discovery/simple-bond.json +167 -0
  30. data/lib/foreman_discovery/engine.rb +9 -3
  31. data/lib/foreman_discovery/version.rb +1 -1
  32. data/locale/ca/LC_MESSAGES/foreman_discovery.mo +0 -0
  33. data/locale/ca/foreman_discovery.edit.po +148 -221
  34. data/locale/ca/foreman_discovery.po +25 -19
  35. data/locale/de/LC_MESSAGES/foreman_discovery.mo +0 -0
  36. data/locale/de/foreman_discovery.edit.po +147 -221
  37. data/locale/de/foreman_discovery.po +26 -20
  38. data/locale/en/foreman_discovery.edit.po +106 -98
  39. data/locale/en/foreman_discovery.po +24 -18
  40. data/locale/en_GB/LC_MESSAGES/foreman_discovery.mo +0 -0
  41. data/locale/en_GB/foreman_discovery.edit.po +149 -222
  42. data/locale/en_GB/foreman_discovery.po +26 -20
  43. data/locale/es/LC_MESSAGES/foreman_discovery.mo +0 -0
  44. data/locale/es/foreman_discovery.edit.po +149 -222
  45. data/locale/es/foreman_discovery.po +26 -20
  46. data/locale/foreman_discovery.pot +115 -103
  47. data/locale/fr/LC_MESSAGES/foreman_discovery.mo +0 -0
  48. data/locale/fr/foreman_discovery.edit.po +147 -221
  49. data/locale/fr/foreman_discovery.po +26 -20
  50. data/locale/gl/foreman_discovery.edit.po +147 -220
  51. data/locale/gl/foreman_discovery.po +24 -18
  52. data/locale/it/LC_MESSAGES/foreman_discovery.mo +0 -0
  53. data/locale/it/foreman_discovery.edit.po +148 -221
  54. data/locale/it/foreman_discovery.po +25 -19
  55. data/locale/ja/LC_MESSAGES/foreman_discovery.mo +0 -0
  56. data/locale/ja/foreman_discovery.edit.po +149 -222
  57. data/locale/ja/foreman_discovery.po +26 -20
  58. data/locale/ko/LC_MESSAGES/foreman_discovery.mo +0 -0
  59. data/locale/ko/foreman_discovery.edit.po +149 -221
  60. data/locale/ko/foreman_discovery.po +26 -20
  61. data/locale/pt_BR/LC_MESSAGES/foreman_discovery.mo +0 -0
  62. data/locale/pt_BR/foreman_discovery.edit.po +149 -222
  63. data/locale/pt_BR/foreman_discovery.po +26 -20
  64. data/locale/ru/LC_MESSAGES/foreman_discovery.mo +0 -0
  65. data/locale/ru/foreman_discovery.edit.po +152 -223
  66. data/locale/ru/foreman_discovery.po +26 -20
  67. data/locale/sv_SE/LC_MESSAGES/foreman_discovery.mo +0 -0
  68. data/locale/sv_SE/foreman_discovery.edit.po +147 -220
  69. data/locale/sv_SE/foreman_discovery.po +24 -18
  70. data/locale/zh_CN/LC_MESSAGES/foreman_discovery.mo +0 -0
  71. data/locale/zh_CN/foreman_discovery.edit.po +155 -224
  72. data/locale/zh_CN/foreman_discovery.po +26 -20
  73. data/locale/zh_TW/LC_MESSAGES/foreman_discovery.mo +0 -0
  74. data/locale/zh_TW/foreman_discovery.edit.po +151 -222
  75. data/locale/zh_TW/foreman_discovery.po +26 -20
  76. data/test/functional/api/v2/discovered_hosts_controller_test.rb +2 -0
  77. data/test/functional/api/v2/discovery_rules_controller_test.rb +96 -9
  78. data/test/functional/api/v2/settings_controller_test.rb +22 -0
  79. data/test/functional/discovered_hosts_controller_test.rb +20 -21
  80. data/test/functional/discovery_rules_controller_test.rb +5 -4
  81. data/test/integration/discovered_hosts_test.rb +20 -10
  82. data/test/models/setting_test.rb +11 -0
  83. data/test/test_helper_discovery.rb +1 -0
  84. data/test/unit/discovered_extensions_test.rb +2 -13
  85. data/test/unit/discovery_rule_test.rb +87 -1
  86. data/test/unit/fact_parser_test.rb +2 -2
  87. data/test/unit/host_discovered_test.rb +60 -28
  88. data/test/unit/ui_notifications/destroy_host_test.rb +14 -4
  89. data/test/unit/ui_notifications/new_host_test.rb +8 -1
  90. metadata +22 -4
@@ -0,0 +1,11 @@
1
+ require 'test_helper'
2
+
3
+ class SettingTest < ActiveSupport::TestCase
4
+ test "should update hostname prefix with multiple valid values" do
5
+ setting = FactoryBot.create(:setting, :name => 'discovery_prefix', :value => 'mac', :category => 'Setting::Discovered')
6
+ RFauxFactory.gen_strings(exclude: [:numeric, :alphanumeric]).values.each do |value|
7
+ setting.value = value
8
+ assert setting.valid?, "Can't update discovery_prefix setting with valid value #{value}"
9
+ end
10
+ end
11
+ end
@@ -66,6 +66,7 @@ def set_default_settings
66
66
  FactoryBot.create(:setting, :name => 'discovery_pxegrub_lock_template', :value => 'pxegrub_discovery', :category => 'Setting::Discovered')
67
67
  FactoryBot.create(:setting, :name => 'discovery_pxegrub2_lock_template', :value => 'pxegrub2_discovery', :category => 'Setting::Discovered')
68
68
  FactoryBot.create(:setting, :name => 'discovery_always_rebuild_dns', :value => true, :category => 'Setting::Discovered')
69
+ FactoryBot.create(:setting, :name => 'discovery_error_on_existing', :value => false, :category => 'Setting::Discovered')
69
70
  end
70
71
 
71
72
  def setup_hostgroup(host)
@@ -48,7 +48,7 @@ class DiscoveredExtensionsTest < ActiveSupport::TestCase
48
48
  host = discover_host_from_facts(facts)
49
49
  r1 = FactoryBot.create(:discovery_rule, :priority => 1, :search => "facts.somefact = abc",
50
50
  :organizations => [host.organization], :locations => [host.location])
51
- FactoryBot.create(:discovery_rule, :priority => 1, :search => "facts.somefact = x",
51
+ FactoryBot.create(:discovery_rule, :priority => 0, :search => "facts.somefact = x",
52
52
  :organizations => [host.organization], :locations => [host.location])
53
53
  assert_equal find_discovery_rule(host), r1
54
54
  end
@@ -56,7 +56,7 @@ class DiscoveredExtensionsTest < ActiveSupport::TestCase
56
56
  test "second rule out of two is found for a discovered host" do
57
57
  facts = @facts.merge({"somefact" => "abc"})
58
58
  host = discover_host_from_facts(facts)
59
- FactoryBot.create(:discovery_rule, :priority => 1, :search => "facts.somefact = x",
59
+ FactoryBot.create(:discovery_rule, :priority => 0, :search => "facts.somefact = x",
60
60
  :organizations => [host.organization], :locations => [host.location])
61
61
  r2 = FactoryBot.create(:discovery_rule, :priority => 1, :search => "facts.somefact = abc",
62
62
  :organizations => [host.organization], :locations => [host.location])
@@ -83,17 +83,6 @@ class DiscoveredExtensionsTest < ActiveSupport::TestCase
83
83
  assert_equal find_discovery_rule(host), r2
84
84
  end
85
85
 
86
- test "older rule out of two is found for a discovered host" do
87
- facts = @facts.merge({"somefact" => "abc"})
88
- host = discover_host_from_facts(facts)
89
- r1 = FactoryBot.create(:discovery_rule, :priority => 1, :search => "facts.somefact = abc",
90
- :organizations => [host.organization], :locations => [host.location])
91
- r2 = FactoryBot.create(:discovery_rule, :priority => 1, :search => "facts.somefact = abc",
92
- :organizations => [host.organization], :locations => [host.location],
93
- :created_at => Time.now + 1.day)
94
- assert_equal find_discovery_rule(host), r1
95
- end
96
-
97
86
  test "drained rule does not match for a discovered host" do
98
87
  facts = @facts.merge({"somefact" => "abc"})
99
88
  host = discover_host_from_facts(facts)
@@ -5,6 +5,9 @@ class DiscoveryRuleTest < ActiveSupport::TestCase
5
5
  @hostgroup = FactoryBot.create(:hostgroup)
6
6
  end
7
7
 
8
+ should allow_values(*valid_name_list).for(:name)
9
+ should_not allow_values(*invalid_name_list).for(:name)
10
+
8
11
  context "#validates attribute" do
9
12
  setup do
10
13
  @huge_name = "x" * 256
@@ -52,6 +55,12 @@ class DiscoveryRuleTest < ActiveSupport::TestCase
52
55
  end
53
56
  end
54
57
 
58
+ test_attributes :pid => 'b8ae7a80-b9a8-4924-808c-482a2b4102c4'
59
+ test "should create discovery rule with name and minimum required attributes" do
60
+ rule = DiscoveryRule.new(:name => 'new_rule', :search => 'cpu_count = 1', :hostgroup_id => hostgroups(:unusual).id)
61
+ assert_valid rule
62
+ end
63
+
55
64
  test "should be able to create a rule without entering hosts limit" do
56
65
  rule = DiscoveryRule.new :name => "myrule",
57
66
  :search => "cpu_count > 1",
@@ -72,7 +81,7 @@ class DiscoveryRuleTest < ActiveSupport::TestCase
72
81
  assert_valid rule
73
82
  end
74
83
 
75
- test "should not be able to create a rule without a hostgroup" do
84
+ test "should not be able to create a rule without a hostgroup-id" do
76
85
  rule = DiscoveryRule.new :name => "myrule",
77
86
  :search => "cpu_count > 1",
78
87
  :organization_ids => [organization_one.id],
@@ -81,6 +90,30 @@ class DiscoveryRuleTest < ActiveSupport::TestCase
81
90
  assert_equal "can't be blank", rule.errors[:hostgroup_id].first
82
91
  end
83
92
 
93
+ test "should not be able to create a rule without a valid hostgroup" do
94
+ rule = DiscoveryRule.new :name => "myrule",
95
+ :search => "cpu_count > 1",
96
+ :organization_ids => [organization_one.id],
97
+ :location_ids => [location_one.id],
98
+ :hostgroup_id => 99999
99
+ refute_valid rule
100
+
101
+ assert_equal "must be present.", rule.errors[:hostgroup].first
102
+ end
103
+
104
+ test_attributes :pid => '4ec7d76a-22ba-4c3e-952c-667a6f0a5728'
105
+ test "should not create with invalid priority type" do
106
+ rule = DiscoveryRule.new(
107
+ :name => 'new_rule',
108
+ :search => 'cpu_count = 1',
109
+ :hostgroup_id => hostgroups(:unusual).id,
110
+ :priority => 'invalid priority type'
111
+ )
112
+ refute_valid rule
113
+ assert rule.errors.key?(:priority)
114
+ assert_equal "is not a number", rule.errors[:priority].first
115
+ end
116
+
84
117
  test "should not be able to create a rule with priority bigger than 2^31" do
85
118
  rule = DiscoveryRule.new :name => "myrule",
86
119
  :search => "cpu_count > 1",
@@ -92,6 +125,19 @@ class DiscoveryRuleTest < ActiveSupport::TestCase
92
125
  assert_equal "must be less than 2147483648", rule.errors[:priority].first
93
126
  end
94
127
 
128
+ test_attributes :pid => '84503d8d-86f6-49bf-ab97-eff418d3e3d0'
129
+ test "should not create with invalid max count type" do
130
+ rule = DiscoveryRule.new(
131
+ :name => 'new_rule',
132
+ :search => 'cpu_count = 1',
133
+ :hostgroup_id => hostgroups(:unusual).id,
134
+ :max_count => 'invalid max_count type'
135
+ )
136
+ refute_valid rule
137
+ assert rule.errors.key?(:max_count)
138
+ assert_equal "is not a number", rule.errors[:max_count].first
139
+ end
140
+
95
141
  test "should not be able to create a rule with max count bigger than 2^31" do
96
142
  rule = DiscoveryRule.new :name => "myrule",
97
143
  :search => "cpu_count > 1",
@@ -125,4 +171,44 @@ class DiscoveryRuleTest < ActiveSupport::TestCase
125
171
  refute_valid rule
126
172
  assert_equal "Host group location #{loc.name} must also be associated to the discovery rule", rule.errors[:locations].first
127
173
  end
174
+
175
+ context 'auditing related to discovery rule' do
176
+ test "should be able to create audit on a rule creation" do
177
+ rule = nil
178
+ assert_difference 'Audit.count' do
179
+ rule = FactoryBot.create(:discovery_rule, :with_auditing)
180
+ end
181
+ recent_audit = rule.audits.last
182
+ assert_equal 'create', recent_audit.action
183
+ end
184
+
185
+ test "should create audit when updating a discovery rule" do
186
+ rule = FactoryBot.create(:discovery_rule, :with_auditing)
187
+ assert_difference 'rule.audits.count' do
188
+ rule.enabled = false
189
+ rule.save!
190
+ end
191
+ audit = rule.audits.last
192
+ assert_equal 'update', audit.action
193
+ end
194
+
195
+ test "should create audit record when destroying a discovery rule" do
196
+ rule = FactoryBot.create(:discovery_rule, :with_auditing)
197
+ assert_difference 'Audit.count' do
198
+ rule.destroy
199
+ end
200
+ audit = rule.audits.last
201
+ assert_equal 'destroy', audit.action
202
+ end
203
+
204
+ test 'should be able to create audit record on changing hostgroup of rule' do
205
+ rule = FactoryBot.create(:discovery_rule)
206
+ hostgroup_one = FactoryBot.create(:hostgroup, :name => "test-hg")
207
+
208
+ assert_difference 'hostgroup_one.associated_audits.count' do
209
+ hostgroup_one.discovery_rules << rule
210
+ hostgroup_one.save!
211
+ end
212
+ end
213
+ end
128
214
  end
@@ -43,7 +43,7 @@ class FactParserTest < ActiveSupport::TestCase
43
43
  exception = assert_raises(::Foreman::Exception) do
44
44
  @parser.suggested_primary_interface(@host)
45
45
  end
46
- assert_match(/Unable to detect primary interface using MAC/, exception.message)
46
+ assert_match(/Unable to find primary NIC/, exception.message)
47
47
  end
48
48
 
49
49
  test "#suggested_primary_interface errors out when bootif fact is missing" do
@@ -51,7 +51,7 @@ class FactParserTest < ActiveSupport::TestCase
51
51
  exception = assert_raises(::Foreman::Exception) do
52
52
  @parser.suggested_primary_interface(@host)
53
53
  end
54
- assert_match(/Unable to detect primary interface using MAC/, exception.message)
54
+ assert_match(/Unable to find primary NIC/, exception.message)
55
55
  end
56
56
 
57
57
  test "#suggested_primary_interface detects interface even when 'ignore_puppet_facts_for_provisioning' is set" do
@@ -26,7 +26,6 @@ class HostDiscoveredTest < ActiveSupport::TestCase
26
26
  end
27
27
 
28
28
  test "should setup subnet" do
29
- raw = parse_json_fixture('/facts.json')
30
29
  subnet = FactoryBot.create(:subnet_ipv4, :name => 'Subnet99', :network => '10.35.27.0', :organizations => [organization_one], :locations => [location_one])
31
30
  Subnet.expects(:subnet_for).with('10.35.27.3').returns(subnet)
32
31
  host = discover_host_from_facts(@facts)
@@ -38,7 +37,6 @@ class HostDiscoveredTest < ActiveSupport::TestCase
38
37
  loc = FactoryBot.create(:location, :name => "subnet_loc")
39
38
  Setting['discovery_organization'] = org.name
40
39
  Setting['discovery_location'] = loc.name
41
- raw = parse_json_fixture('/facts.json')
42
40
  subnet = FactoryBot.create(:subnet_ipv4, :name => 'Subnet99', :network => '10.35.27.0', :organizations => [org], :locations => [loc])
43
41
  Subnet.expects(:subnet_for).with('10.35.27.3').returns(subnet)
44
42
  host = discover_host_from_facts(@facts)
@@ -48,7 +46,6 @@ class HostDiscoveredTest < ActiveSupport::TestCase
48
46
  test "should setup subnet with org and loc set via facts" do
49
47
  org = FactoryBot.create(:organization, :name => "subnet_org_via_fact")
50
48
  loc = FactoryBot.create(:location, :name => "subnet_loc_via_fact")
51
- raw = parse_json_fixture('/facts.json')
52
49
  @facts['foreman_organization'] = org.name
53
50
  @facts['foreman_location'] = loc.name
54
51
  subnet = FactoryBot.create(:subnet_ipv4, :name => 'Subnet99', :network => '10.35.27.0', :organizations => [org], :locations => [loc])
@@ -64,7 +61,6 @@ class HostDiscoveredTest < ActiveSupport::TestCase
64
61
  loc = FactoryBot.create(:location, :name => "subloc", :parent_id => loc_parent.id)
65
62
  Setting['discovery_organization'] = org.name
66
63
  Setting['discovery_location'] = loc.name
67
- raw = parse_json_fixture('/facts.json')
68
64
  subnet = FactoryBot.create(:subnet_ipv4, :name => 'Subnet99', :network => '10.35.27.0', :organizations => [org], :locations => [loc])
69
65
  Subnet.expects(:subnet_for).with('10.35.27.3').returns(subnet)
70
66
  host = discover_host_from_facts(@facts)
@@ -73,7 +69,6 @@ class HostDiscoveredTest < ActiveSupport::TestCase
73
69
  end
74
70
 
75
71
  test "should raise when fact_name setting isn't present" do
76
- raw = parse_json_fixture('/facts.json')
77
72
  Setting[:discovery_fact] = 'macaddress_foo'
78
73
  exception = assert_raises(::Foreman::Exception) do
79
74
  discover_host_from_facts(@facts)
@@ -83,13 +78,11 @@ class HostDiscoveredTest < ActiveSupport::TestCase
83
78
 
84
79
  test "should be able to refresh facts" do
85
80
  host = Host.create :name => "mydiscoveredhost", :ip => "1.2.3.4", :type => "Host::Discovered"
86
- raw = parse_json_fixture('/facts.json')
87
81
  ::ForemanDiscovery::NodeAPI::Inventory.any_instance.stubs(:facter).returns(@facts)
88
82
  assert host.refresh_facts
89
83
  end
90
84
 
91
85
  test "should create discovered host with hostname if a fact was supplied" do
92
- raw = parse_json_fixture('/facts.json')
93
86
  Setting[:discovery_hostname] = 'somefact'
94
87
  facts = @facts.merge({"somefact" => "somename"})
95
88
  host = discover_host_from_facts(facts)
@@ -100,7 +93,6 @@ class HostDiscoveredTest < ActiveSupport::TestCase
100
93
  test "should lock host into discovery via PXE configuration" do
101
94
  Host::Discovered.delete('mace41f13cc3658')
102
95
  Setting[:discovery_lock] = "true"
103
- raw = parse_json_fixture('/facts.json')
104
96
  subnet = FactoryBot.create(:subnet,
105
97
  :tftp,
106
98
  :network => '10.35.27.0',
@@ -124,7 +116,6 @@ class HostDiscoveredTest < ActiveSupport::TestCase
124
116
  end
125
117
 
126
118
  test "should create discovered host with fact_name as a name if it is a valid mac" do
127
- raw = parse_json_fixture('/facts.json')
128
119
  Setting[:discovery_fact] = 'somefact'
129
120
  Setting[:discovery_hostname] = 'somefact'
130
121
  facts = @facts.merge({"somefact" => "E4:1F:13:CC:36:5A"})
@@ -134,48 +125,62 @@ class HostDiscoveredTest < ActiveSupport::TestCase
134
125
  end
135
126
 
136
127
  test "should create discovered host with default name if fact_name isn't a valid mac" do
137
- raw = parse_json_fixture('/facts.json')
138
128
  Setting[:discovery_fact] = 'lsbdistcodename'
139
129
  exception = assert_raises(::Foreman::Exception) do
140
130
  discover_host_from_facts(@facts)
141
131
  end
142
- assert_match(/Unable to detect primary interface using MAC/, exception.message)
132
+ assert_match(/Unable to find primary NIC/, exception.message)
143
133
  end
144
134
 
145
135
  test "should not create discovered host when managed host exists" do
146
- FactoryBot.create(:host, :mac => 'E4:1F:13:CC:36:58')
147
- raw = parse_json_fixture('/facts.json')
148
- exception = assert_raises(::Foreman::Exception) do
149
- discover_host_from_facts(@facts)
136
+ begin
137
+ Setting[:discovery_error_on_existing] = true
138
+ FactoryBot.create(:host, :mac => 'E4:1F:13:CC:36:58')
139
+ exception = assert_raises(::Foreman::Exception) do
140
+ discover_host_from_facts(@facts)
141
+ end
142
+ assert_match(/One or more existing managed hosts found/, exception.message)
143
+ ensure
144
+ Setting[:discovery_error_on_existing] = false
150
145
  end
151
- assert_match(/Host already exists as managed/, exception.message)
146
+ end
147
+
148
+ test "should allow existing managed host discovery" do
149
+ FactoryBot.create(:host, :mac => 'E4:1F:13:CC:36:58')
150
+ host = discover_host_from_facts(@facts)
151
+ assert_equal 'mace41f13cc3658', host.name
152
+ assert_equal 'e4:1f:13:cc:36:58', host.mac
152
153
  end
153
154
 
154
155
  test "should create discovered host with prefix" do
155
- raw = parse_json_fixture('/facts.json')
156
156
  Setting[:discovery_prefix] = 'test'
157
157
  host = discover_host_from_facts(@facts)
158
158
  assert_equal 'teste41f13cc3658', host.name
159
159
  end
160
160
 
161
161
  test "should create discovered host without prefix" do
162
- raw = parse_json_fixture('/facts.json')
163
162
  Setting[:discovery_prefix] = ''
164
163
  host = discover_host_from_facts(@facts)
165
164
  assert_equal 'e41f13cc3658',host.name
166
165
  end
167
166
 
168
- test "should refresh existing discovered" do
169
- raw = parse_json_fixture('/facts.json')
170
- interface = mock()
171
- interface.stubs(:host).returns(Host.create(:name => "xyz", :type => "Host::Discovered"))
172
- ::Nic::Managed.stubs(:where).with(:mac => @facts['discovery_bootif'].downcase, :primary => true).returns([interface])
173
- host = discover_host_from_facts(@facts)
174
- assert_equal 'xyz', host.name
167
+ test "should refresh facts and NICs of an existing discovered host" do
168
+ host1 = discover_host_from_facts(@facts)
169
+ assert_equal 'mace41f13cc3658', host1.name
170
+ assert_equal 'IBM System x -[7870K4G]-', host1.facts["productname"]
171
+ assert_equal 1, Host::Discovered.where(:name => 'mace41f13cc3658').count
172
+ assert_equal '10.35.27.3', host1.ip
173
+
174
+ @facts["ipaddress_eth0"] = "1.2.3.4"
175
+ @facts["productname"] = "Dishwasher DW400"
176
+ host2 = discover_host_from_facts(@facts)
177
+ assert_equal 'mace41f13cc3658', host2.name
178
+ assert_equal 'Dishwasher DW400', host2.facts["productname"]
179
+ assert_equal '1.2.3.4', host2.ip
180
+ assert_equal 1, Host::Discovered.where(:name => 'mace41f13cc3658').count
175
181
  end
176
182
 
177
183
  test "should raise when hostname fact cannot be found" do
178
- raw = parse_json_fixture('/facts.json')
179
184
  Setting[:discovery_hostname] = 'macaddress_foo'
180
185
  exception = assert_raises(::Foreman::Exception) do
181
186
  discover_host_from_facts(@facts)
@@ -184,7 +189,6 @@ class HostDiscoveredTest < ActiveSupport::TestCase
184
189
  end
185
190
 
186
191
  test "should raise when hostname cannot be computed due to normlization and no prefix" do
187
- raw = parse_json_fixture('/facts.json')
188
192
  @facts['invalidhostnamefact'] = '...'
189
193
  Setting[:discovery_hostname] = 'invalidhostnamefact'
190
194
  Setting[:discovery_prefix] = ''
@@ -223,7 +227,6 @@ class HostDiscoveredTest < ActiveSupport::TestCase
223
227
 
224
228
  test "provisioning a discovered host without saving it doesn't create a token" do
225
229
  Setting[:token_duration] = 30 #enable tokens so that we only test the CR
226
- raw = parse_json_fixture('/facts.json')
227
230
  Setting[:discovery_prefix] = '123'
228
231
  host = discover_host_from_facts(@facts)
229
232
  host.save
@@ -325,6 +328,35 @@ class HostDiscoveredTest < ActiveSupport::TestCase
325
328
  assert_equal Host::Discovered.return_first_valid_fact(discovery_hostname_fact_array,facts),"testhostname"
326
329
  end
327
330
 
331
+ context 'notification recipients' do
332
+ setup do
333
+ @admins = User.except_hidden.where(:admin => true).pluck(:id)
334
+ @org = FactoryBot.create(:organization)
335
+ @host = FactoryBot.create(:discovered_host)
336
+ end
337
+
338
+ test 'finds admin users' do
339
+ assert_equal @admins.sort, @host.notification_recipients_ids.sort
340
+ end
341
+
342
+ test 'finds users who can create_host in the host org' do
343
+ setup_user 'create', 'hosts'
344
+ User.current.organizations << @org
345
+ recipients = [User.current.id, @admins].flatten.sort
346
+ @host.organization = @org
347
+ assert_equal recipients, @host.notification_recipients_ids.sort
348
+ end
349
+
350
+ test 'finds only admin users if organizations are disabled' do
351
+ begin
352
+ SETTINGS[:organizations_enabled] = false
353
+ assert_equal @admins.sort, @host.notification_recipients_ids.sort
354
+ ensure
355
+ SETTINGS[:organizations_enabled] = true
356
+ end
357
+ end
358
+ end
359
+
328
360
  def parse_json_fixture(relative_path)
329
361
  return JSON.parse(File.read(File.expand_path(File.dirname(__FILE__) + relative_path)))
330
362
  end
@@ -7,8 +7,9 @@ class DestroyHostNotificationTest < ActiveSupport::TestCase
7
7
  assert blueprint
8
8
  end
9
9
 
10
- test 'destorying discovered host should remove notification' do
10
+ test 'destroying discovered host should remove notification' do
11
11
  host = FactoryBot.create(:discovered_host)
12
+ ForemanDiscovery::UINotifications::NewHost.deliver!(host)
12
13
  assert_difference('blueprint.notifications.count', -1) do
13
14
  host.destroy
14
15
  end
@@ -16,11 +17,13 @@ class DestroyHostNotificationTest < ActiveSupport::TestCase
16
17
 
17
18
  test 'do not remove notification if other discovered hosts exists' do
18
19
  assert_difference('blueprint.notifications.count') do
19
- FactoryBot.create(:discovered_host)
20
+ host = FactoryBot.create(:discovered_host)
21
+ ForemanDiscovery::UINotifications::NewHost.deliver!(host)
20
22
  end
21
23
  assert_equal 1, blueprint.notifications.count
22
24
  assert_no_difference('blueprint.notifications.count') do
23
- FactoryBot.create(:discovered_host)
25
+ host = FactoryBot.create(:discovered_host)
26
+ ForemanDiscovery::UINotifications::NewHost.deliver!(host)
24
27
  end
25
28
  assert_no_difference('blueprint.notifications.count') do
26
29
  Host::Discovered.all.last.destroy
@@ -31,6 +34,7 @@ class DestroyHostNotificationTest < ActiveSupport::TestCase
31
34
 
32
35
  test 'discovered host type change should update notifications' do
33
36
  host = FactoryBot.create(:discovered_host)
37
+ ForemanDiscovery::UINotifications::NewHost.deliver!(host)
34
38
  assert_equal 1, blueprint.notifications.count
35
39
  new_host = ::ForemanDiscovery::HostConverter.to_managed(host, false, false)
36
40
  assert new_host.save!
@@ -39,10 +43,16 @@ class DestroyHostNotificationTest < ActiveSupport::TestCase
39
43
 
40
44
  test 'type change should not removing notifications for discovered hosts' do
41
45
  host1 = FactoryBot.create(:discovered_host)
42
- FactoryBot.create(:discovered_host)
46
+ ForemanDiscovery::UINotifications::NewHost.deliver!(host1)
47
+ host2 = FactoryBot.create(:discovered_host)
48
+ ForemanDiscovery::UINotifications::NewHost.deliver!(host2)
43
49
  assert_equal 1, blueprint.notifications.count
44
50
  new_host = ::ForemanDiscovery::HostConverter.to_managed(host1, false, false)
45
51
  assert new_host.save!
46
52
  assert_equal 1, blueprint.notifications.count
47
53
  end
54
+
55
+ def parse_json_fixture(relative_path)
56
+ return JSON.parse(File.read(File.expand_path(File.dirname(__FILE__) + relative_path)))
57
+ end
48
58
  end