forked from theforeman/foreman_discovery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
discovered_hosts_controller_test.rb
345 lines (312 loc) · 13.1 KB
/
discovered_hosts_controller_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
require 'test_plugin_helper'
class DiscoveredHostsControllerTest < ActionController::TestCase
include FactImporterIsolation
allow_transactions_for_any_importer
setup :initialize_host
setup do
assert discovered_notification_blueprint
@request.env['HTTP_REFERER'] = '/discovery_rules'
FactoryBot.create(:subnet, :network => "192.168.100.1", :mask => "255.255.255.0", :locations => [location_one], :organizations => [organization_one])
@facts = {
"interfaces" => "lo,eth0",
"ipaddress" => "192.168.100.42",
"ipaddress_eth0" => "192.168.100.42",
"macaddress_eth0" => "AA:BB:CC:DD:EE:FF",
"discovery_bootif" => "AA:BB:CC:DD:EE:FF",
"physicalprocessorcount" => "42",
"discovery_version" => "3.0.0",
}
set_default_settings
::ForemanDiscovery::NodeAPI::PowerService.any_instance.stubs(:reboot).returns(true)
end
def test_index
get :index, params: {}, session: set_session_user_default_reader
assert_response :success
end
def test_index_with_custom_column
FactoryBot.create(:setting,
:name => 'discovery_fact_column',
:value => "bios_vendor",
:category => 'Setting::Discovered')
facts = @facts.merge({"bios_vendor" => "QEMU"})
discover_host_from_facts(facts)
get :index, params: {}, session: set_session_user_default_reader
assert_select "td", /QEMU/
assert_response :success
end
def test_show_multiple_actions
host = discover_host_from_facts(@facts)
[:multiple_destroy, :select_multiple_organization, :select_multiple_location].each do |action|
process action, method: :post, params: {:host_ids => [host.id]}, session: set_session_user, xhr: true
assert_response :success
assert response.body =~ /#{host.name}/
end
end
def test_show_page_categories
host = discover_host_from_facts(@facts)
get :show, params: {:id => host.id}, session: set_session_user_default_reader
assert_select "#category-highlights" do
assert_select "#fact-ipaddress" do
assert_select "td", /192.168.100.42/
end
end
assert_response :success
end
def test_edit_form_elements
host = discover_host_from_facts(@facts)
get :edit, params: {:id => host.id}, session: set_session_user_default_manager
assert_select "select" do |elements|
elements.each do |element|
assert_match(/^host_/, element.attributes['id'])
assert_match(/^host\[/, element.attributes['name'])
end
end
end
def test_edit_form_attributes
host = discover_host_from_facts(@facts)
get :edit, params: {:id => host.id}, session: set_session_user_default_reader
assert_not_nil host.cpu_count
end
def test_edit_form_build_mode_enabled
host = discover_host_from_facts(@facts)
get :edit, params: {:id => host.id}, session: set_session_user_default_manager
assert_response :success
assert_select 'input[type=checkbox][checked=checked]#host_build'
end
def test_edit_form_quick_submit
disable_taxonomies do
domain = FactoryBot.create(:domain)
hostgroup = FactoryBot.create(:hostgroup, :with_subnet, :with_environment, :with_rootpass, :with_os, :domain => domain)
new_ip = hostgroup.subnet.ipaddr
host = discover_host_from_facts(@facts.merge({
'ipaddress' => new_ip,
'ipaddress_eth0' => new_ip
}))
get :edit, params: {
:id => host.id,
:quick_submit => true,
:host => {
:hostgroup_id => hostgroup.id
} }, session: set_session_user_default_manager
managed_host = Host.find(host.id)
assert managed_host.build
assert_redirected_to host_url(managed_host)
assert_equal hostgroup.id, managed_host.hostgroup_id
assert_match(/Successfully/, flash[:success])
end
end
def test_edit_form_submit_parameters
host = discover_host_from_facts(@facts)
hostgroup = setup_hostgroup(host)
get :edit, params: {
:id => host.id,
:host => {
:hostgroup_id => hostgroup.id
} }, session: set_session_user_default_manager
# all inherit buttons are pressed
assert_select('button[name=is_overridden_btn]') do |e|
e.attribute("class") =~ /active/
end
# particular fields are set
assert_select '#host_hostgroup_id [selected]' do |e|
assert_equal hostgroup.id.to_s, e.first[:value]
end
assert_select '#host_architecture_id [selected]' do |e|
assert_equal hostgroup.architecture_id.to_s, e.first[:value]
end
assert_select '#host_operatingsystem_id [selected]' do |e|
assert_equal hostgroup.operatingsystem_id.to_s, e.first[:value]
end
assert_select '#host_medium_id [selected]' do |e|
assert_equal hostgroup.medium_id.to_s, e.first[:value]
end
assert_select '#host_ptable_id [selected]' do |e|
assert_equal hostgroup.ptable_id.to_s, e.first[:value]
end
assert_select '#host_interfaces_attributes_0_domain_id [selected]' do |e|
assert_equal hostgroup.domain_id.to_s, e.first[:value]
end
end
def test_update_inheritance
host = discover_host_from_facts(@facts)
hostgroup = setup_hostgroup(host)
put :update, params: {
commit: 'Update',
id: host.id,
host: {
name: 'mytest',
hostgroup_id: hostgroup.id
}
}, session: set_session_user(User.current)
# Get the managed host instance from the DB
actual = Host.find(host.id)
assert_equal hostgroup.id, actual.hostgroup_id
assert_equal hostgroup.architecture_id, actual.architecture_id
assert_equal hostgroup.operatingsystem_id, actual.operatingsystem_id
assert_equal hostgroup.medium_id, actual.medium_id
assert_equal hostgroup.ptable_id, actual.ptable_id
assert_equal hostgroup.domain_id, actual.domain_id
assert_equal hostgroup.environment_id, actual.environment_id
assert_equal hostgroup.puppet_proxy_id, actual.puppet_proxy_id
assert_equal hostgroup.puppet_ca_proxy_id, actual.puppet_ca_proxy_id
end
def test_add_entry_to_nav_menu
get :index, params: {}, session: set_session_user
assert_select "a[href=?]", "/discovered_hosts"
end
def test_reboot_success
@request.env["HTTP_REFERER"] = discovered_hosts_url
host = discover_host_from_facts(@facts)
::ForemanDiscovery::NodeAPI::PowerService.any_instance.expects(:reboot).returns(true)
post :reboot, params: { :id => host.id }, session: set_session_user_default_manager
assert_redirected_to discovered_hosts_url
assert_nil flash[:error]
assert_equal "Rebooting host #{host.name}", flash[:success]
end
def test_reboot_failure
@request.env["HTTP_REFERER"] = discovered_hosts_url
host = discover_host_from_facts(@facts)
::ForemanDiscovery::NodeAPI::PowerService.any_instance.expects(:reboot).returns(false)
post :reboot, params: { :id => host.id }, session: set_session_user_default_manager
assert_redirected_to discovered_hosts_url
assert_equal "Failed to reboot host #{host.name}", flash[:error]
end
def test_reboot_error
@request.env["HTTP_REFERER"] = discovered_hosts_url
host = discover_host_from_facts(@facts)
::ForemanDiscovery::NodeAPI::PowerService.any_instance.expects(:reboot).raises("request failed")
post :reboot, params: { :id => host.id }, session: set_session_user_default_manager
assert_redirected_to discovered_hosts_url
assert_match(/ERF50-4973/, flash[:error])
end
def test_auto_provision_success
disable_orchestration
facts = @facts.merge({"somefact" => "abc"})
host = discover_host_from_facts(facts)
hostgroup = setup_hostgroup(host)
FactoryBot.create(:discovery_rule, :priority => 1, :search => "facts.somefact = abc", :hostgroup => hostgroup, :organizations => [host.organization], :locations => [host.location])
post :auto_provision, params: { :id => host.id }, session: set_session_user(User.current)
assert_response :redirect
assert_nil flash[:error]
assert_match(/^Host macaabbccddeeff.* was provisioned/, flash[:success])
managed_host = Host.find(host.id)
assert managed_host.build
end
def test_auto_provision_no_rule_success
disable_orchestration
facts = @facts.merge({"somefact" => "abc"})
host = discover_host_from_facts(facts)
post :auto_provision, params: { :id => host.id }, session: set_session_user(User.current)
assert_response :redirect
assert_nil flash[:error]
assert_equal "No rule found for host macaabbccddeeff", flash[:success]
end
def test_auto_provision_all_success
disable_orchestration
facts = @facts.merge({"somefact" => "abc"})
host = discover_host_from_facts(facts)
hostgroup = setup_hostgroup(host)
FactoryBot.create(
:discovery_rule,
:priority => 1,
:search => "facts.somefact = abc",
:hostgroup => hostgroup,
:organizations => [host.organization], :locations => [host.location]
)
post :auto_provision_all, params: {}, session: set_session_user(User.current)
assert_response :redirect
assert_nil flash[:error]
assert_equal "Discovered hosts are provisioning now", flash[:success]
end
def test_auto_provision_all_no_rule_success
disable_orchestration
facts = @facts.merge({"somefact" => "abc"})
host = discover_host_from_facts(facts)
hostgroup = setup_hostgroup(host)
FactoryBot.create(
:discovery_rule,
:priority => 1,
:search => "facts.somefact = abc",
:hostgroup => hostgroup,
:organizations => [host.organization], :locations => [host.location]
)
post :auto_provision_all, params: {}, session: set_session_user(User.current)
assert_response :redirect
assert_nil flash[:error]
assert_equal "Discovered hosts are provisioning now", flash[:success]
end
def test_reboot_all_success
@request.env["HTTP_REFERER"] = discovered_hosts_url
host = discover_host_from_facts(@facts)
::ForemanDiscovery::NodeAPI::PowerService.any_instance.expects(:reboot).returns(true)
post :reboot_all, params: { }, session: set_session_user_default_manager
assert_redirected_to discovered_hosts_url
assert_nil flash[:error]
assert_equal "Discovered hosts are rebooting now", flash[:success]
end
def test_reboot_all_failure
@request.env["HTTP_REFERER"] = discovered_hosts_url
host = discover_host_from_facts(@facts)
::ForemanDiscovery::NodeAPI::PowerService.any_instance.expects(:reboot).returns(false)
post :reboot_all, params: { }, session: set_session_user_default_manager
assert_redirected_to discovered_hosts_url
assert_equal "Errors during reboot: #{host.name}: failed to reboot", flash[:error]
assert_nil flash[:success]
end
def test_reboot_all_error
@request.env["HTTP_REFERER"] = discovered_hosts_url
discover_host_from_facts(@facts)
::ForemanDiscovery::NodeAPI::PowerService.any_instance.expects(:reboot).raises("request failed")
post :reboot_all, params: { }, session: set_session_user_default_manager
assert_redirected_to discovered_hosts_url
assert_match(/ERF50-4973/, flash[:error])
assert_nil flash[:success]
end
def test_no_dns_rebuild_if_dns_pending
host = discover_host_from_facts(@facts)
hostgroup = prepare_hostgroup_for_dns_rebuild(host)
Nic::Managed.any_instance.expects(:rebuild_dns).never
Host::Managed.any_instance.stubs(:skip_orchestration?).returns(false)
put :update, params: {:commit => "Update", :id => host.id,
:host => {
:name => 'mytest',
:hostgroup_id => hostgroup.id,
}
}, session: set_session_user(User.current)
end
def test_dns_rebuild
host = prepare_host_for_dns_rebuild
hostgroup = prepare_hostgroup_for_dns_rebuild(host)
Nic::Managed.any_instance.expects(:rebuild_dns)
Host::Managed.any_instance.stubs(:skip_orchestration?).returns(false)
put :update, params: {:commit => "Update", :id => host.id,
:host => {
:name => 'mytest',
:hostgroup_id => hostgroup.id,
}
}, session: set_session_user(User.current)
end
def test_dns_rebuild_with_auto_provision
host = prepare_host_for_dns_rebuild
hostgroup = prepare_hostgroup_for_dns_rebuild(host)
Nic::Managed.any_instance.expects(:rebuild_dns)
Host::Managed.any_instance.stubs(:skip_orchestration?).returns(false)
FactoryBot.create(:discovery_rule, :priority => 1, :search => "name = mytest.myorchdomain.net", :hostgroup_id => hostgroup.id, :organizations => [host.organization], :locations => [host.location])
post :auto_provision, params: { :id => host.id }, session: set_session_user(User.current)
end
private
def initialize_host
User.current = users(:admin)
end
def prepare_host_for_dns_rebuild
host = discover_host_from_facts(@facts)
host.name = 'mytest.myorchdomain.net'
host.save
host
end
def prepare_hostgroup_for_dns_rebuild(host)
hostgroup = setup_hostgroup(host)
hostgroup.domain = FactoryBot.create(:domain, :name => 'myorchdomain.net', :dns => FactoryBot.create(:smart_proxy, :features => [FactoryBot.create(:feature, :dns)]))
hostgroup
end
end