foreman_discovery 5.0.0 → 5.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/api/v2/discovered_hosts_controller.rb +41 -51
  3. data/app/controllers/concerns/foreman/controller/discovered_extensions.rb +20 -23
  4. data/app/controllers/discovered_hosts_controller.rb +50 -46
  5. data/app/helpers/concerns/foreman_discovery/settings_helper_extensions.rb +34 -0
  6. data/app/helpers/discovery_rules_helper.rb +5 -6
  7. data/app/mailers/discovered_mailer.rb +19 -0
  8. data/app/models/discovery_rule.rb +3 -3
  9. data/app/models/host/discovered.rb +33 -11
  10. data/app/models/host/managed_extensions.rb +10 -0
  11. data/app/models/hostgroup_extensions.rb +1 -1
  12. data/app/models/setting/discovered.rb +17 -11
  13. data/app/services/foreman_discovery/fact_parser.rb +16 -0
  14. data/app/services/foreman_discovery/host_converter.rb +0 -4
  15. data/app/services/foreman_discovery/node_api/power.rb +3 -3
  16. data/app/views/discovered_hosts/_discovered_host.html.erb +4 -2
  17. data/app/views/discovered_hosts/_discovered_hosts_list.html.erb +1 -1
  18. data/app/views/discovered_mailer/_discovered_host.html.erb +10 -0
  19. data/app/views/discovered_mailer/discovered_summary.html.erb +35 -0
  20. data/app/views/discovered_mailer/discovered_summary.text.erb +24 -0
  21. data/config/routes.rb +3 -3
  22. data/db/seeds.d/70_discovery_mail_notification.rb +9 -0
  23. data/lib/foreman_discovery/engine.rb +12 -9
  24. data/lib/foreman_discovery/version.rb +1 -1
  25. data/locale/ca/LC_MESSAGES/foreman_discovery.mo +0 -0
  26. data/locale/ca/foreman_discovery.edit.po +780 -0
  27. data/locale/ca/foreman_discovery.po +358 -261
  28. data/locale/de/LC_MESSAGES/foreman_discovery.mo +0 -0
  29. data/locale/de/foreman_discovery.edit.po +774 -0
  30. data/locale/de/foreman_discovery.po +418 -311
  31. data/locale/en_GB/LC_MESSAGES/foreman_discovery.mo +0 -0
  32. data/locale/en_GB/foreman_discovery.edit.po +779 -0
  33. data/locale/en_GB/foreman_discovery.po +423 -313
  34. data/locale/es/LC_MESSAGES/foreman_discovery.mo +0 -0
  35. data/locale/es/foreman_discovery.edit.po +781 -0
  36. data/locale/es/foreman_discovery.po +425 -313
  37. data/locale/foreman_discovery.pot +496 -159
  38. data/locale/fr/LC_MESSAGES/foreman_discovery.mo +0 -0
  39. data/locale/fr/foreman_discovery.edit.po +778 -0
  40. data/locale/fr/foreman_discovery.po +458 -322
  41. data/locale/gl/LC_MESSAGES/foreman_discovery.mo +0 -0
  42. data/locale/gl/foreman_discovery.edit.po +757 -0
  43. data/locale/gl/foreman_discovery.po +306 -215
  44. data/locale/it/LC_MESSAGES/foreman_discovery.mo +0 -0
  45. data/locale/it/foreman_discovery.edit.po +771 -0
  46. data/locale/it/foreman_discovery.po +411 -301
  47. data/locale/ja/LC_MESSAGES/foreman_discovery.mo +0 -0
  48. data/locale/ja/foreman_discovery.edit.po +770 -0
  49. data/locale/ja/foreman_discovery.po +395 -300
  50. data/locale/ko/LC_MESSAGES/foreman_discovery.mo +0 -0
  51. data/locale/ko/foreman_discovery.edit.po +768 -0
  52. data/locale/ko/foreman_discovery.po +393 -299
  53. data/locale/pt_BR/LC_MESSAGES/foreman_discovery.mo +0 -0
  54. data/locale/pt_BR/foreman_discovery.edit.po +780 -0
  55. data/locale/pt_BR/foreman_discovery.po +438 -309
  56. data/locale/ru/LC_MESSAGES/foreman_discovery.mo +0 -0
  57. data/locale/ru/foreman_discovery.edit.po +775 -0
  58. data/locale/ru/foreman_discovery.po +413 -308
  59. data/locale/sv_SE/LC_MESSAGES/foreman_discovery.mo +0 -0
  60. data/locale/sv_SE/foreman_discovery.edit.po +766 -0
  61. data/locale/sv_SE/foreman_discovery.po +321 -232
  62. data/locale/zh_CN/LC_MESSAGES/foreman_discovery.mo +0 -0
  63. data/locale/zh_CN/foreman_discovery.edit.po +769 -0
  64. data/locale/zh_CN/foreman_discovery.po +393 -302
  65. data/locale/zh_TW/LC_MESSAGES/foreman_discovery.mo +0 -0
  66. data/locale/zh_TW/foreman_discovery.edit.po +769 -0
  67. data/locale/zh_TW/foreman_discovery.po +395 -302
  68. data/test/functional/api/v2/discovered_hosts_controller_test.rb +8 -0
  69. data/test/functional/api/v2/discovery_rules_controller_test.rb +9 -0
  70. data/test/functional/discovered_hosts_controller_test.rb +26 -17
  71. data/test/functional/discovery_rules_controller_test.rb +9 -8
  72. data/test/test_helper.rb +0 -4
  73. data/test/test_helper_discovery.rb +24 -0
  74. data/test/unit/discovered_extensions_test.rb +31 -0
  75. data/test/unit/discovered_mailer_test.rb +44 -0
  76. data/test/unit/discovery_attribute_set_test.rb +12 -0
  77. data/test/unit/discovery_rule_test.rb +20 -0
  78. data/test/unit/{puppet_fact_parser_extensions_test.rb → fact_parser_test.rb} +3 -16
  79. data/test/unit/host_discovered_test.rb +56 -11
  80. metadata +29 -5
  81. data/app/lib/puppet_fact_parser_extensions.rb +0 -29
@@ -29,6 +29,14 @@ class Api::V2::DiscoveredHostsControllerTest < ActionController::TestCase
29
29
  :name => 'discovery_reboot',
30
30
  :value => true,
31
31
  :category => 'Setting::Discovered')
32
+ FactoryGirl.create(:setting,
33
+ :name => 'discovery_hostname',
34
+ :value => 'discovery_bootif',
35
+ :category => 'Setting::Discovered')
36
+ FactoryGirl.create(:setting,
37
+ :name => 'discovery_prefix',
38
+ :value => 'mac',
39
+ :category => 'Setting::Discovered')
32
40
  ::ForemanDiscovery::NodeAPI::PowerService.any_instance.stubs(:reboot).returns(true)
33
41
  end
34
42
 
@@ -4,6 +4,15 @@ class Api::V2::DiscoveryRulesControllerTest < ActionController::TestCase
4
4
 
5
5
  setup do
6
6
  User.current = User.find_by_login "admin"
7
+
8
+ FactoryGirl.create(:setting,
9
+ :name => 'discovery_hostname',
10
+ :value => 'discovery_bootif',
11
+ :category => 'Setting::Discovered')
12
+ FactoryGirl.create(:setting,
13
+ :name => 'discovery_prefix',
14
+ :value => 'mac',
15
+ :category => 'Setting::Discovered')
7
16
  end
8
17
 
9
18
  test "should get index" do
@@ -1,4 +1,5 @@
1
1
  require 'test_plugin_helper'
2
+ require 'test_helper_discovery'
2
3
 
3
4
  class DiscoveredHostsControllerTest < ActionController::TestCase
4
5
  setup :initialize_host
@@ -18,11 +19,19 @@ class DiscoveredHostsControllerTest < ActionController::TestCase
18
19
  :name => 'discovery_reboot',
19
20
  :value => true,
20
21
  :category => 'Setting::Discovered')
22
+ FactoryGirl.create(:setting,
23
+ :name => 'discovery_hostname',
24
+ :value => 'discovery_bootif',
25
+ :category => 'Setting::Discovered')
26
+ FactoryGirl.create(:setting,
27
+ :name => 'discovery_prefix',
28
+ :value => 'mac',
29
+ :category => 'Setting::Discovered')
21
30
  ::ForemanDiscovery::NodeAPI::PowerService.any_instance.stubs(:reboot).returns(true)
22
31
  end
23
32
 
24
33
  def test_index
25
- get :index, {}, set_session_user
34
+ get :index, {}, set_session_user_default_reader
26
35
  assert_response :success
27
36
  end
28
37
 
@@ -33,14 +42,14 @@ class DiscoveredHostsControllerTest < ActionController::TestCase
33
42
  :category => 'Setting::Discovered')
34
43
  facts = @facts.merge({"bios_vendor" => "QEMU"})
35
44
  Host::Discovered.import_host_and_facts(facts).first
36
- get :index, {}, set_session_user
45
+ get :index, {}, set_session_user_default_reader
37
46
  assert_select "td", /QEMU/
38
47
  assert_response :success
39
48
  end
40
49
 
41
50
  def test_edit_form_elements
42
51
  host = Host::Discovered.import_host_and_facts(@facts).first
43
- get :edit, {:id => host.id}, set_session_user
52
+ get :edit, {:id => host.id}, set_session_user_default_manager
44
53
  assert_select "select" do |elements|
45
54
  elements.each do |element|
46
55
  assert_match(/^host_/, element.attributes['id'])
@@ -51,9 +60,7 @@ class DiscoveredHostsControllerTest < ActionController::TestCase
51
60
 
52
61
  def test_edit_form_attributes
53
62
  host = Host::Discovered.import_host_and_facts(@facts).first
54
- Host.transaction do
55
- get :edit, {:id => host.id}, set_session_user
56
- end
63
+ get :edit, {:id => host.id}, set_session_user_default_reader
57
64
  assert_not_nil host.cpu_count
58
65
  end
59
66
 
@@ -67,7 +74,7 @@ class DiscoveredHostsControllerTest < ActionController::TestCase
67
74
  @request.env["HTTP_REFERER"] = discovered_hosts_url
68
75
  host = Host::Discovered.import_host_and_facts(@facts).first
69
76
  ::ForemanDiscovery::NodeAPI::PowerService.any_instance.expects(:reboot).returns(true)
70
- post "reboot", { :id => host.id }, set_session_user
77
+ post "reboot", { :id => host.id }, set_session_user_default_manager
71
78
  assert_redirected_to discovered_hosts_url
72
79
  assert_nil flash[:error]
73
80
  assert_equal "Rebooting host #{host.name}", flash[:notice]
@@ -80,7 +87,9 @@ class DiscoveredHostsControllerTest < ActionController::TestCase
80
87
  Host::Discovered::any_instance.stubs(:proxied?).returns(false)
81
88
  Host::Discovered::any_instance.stubs(:proxy_url).returns("http://1.2.3.4:8443")
82
89
  ::ForemanDiscovery::NodeAPI::PowerLegacyDirectService.any_instance.expects(:reboot).returns(true)
83
- post "reboot", { :id => host.id }, set_session_user
90
+ ActiveSupport::Deprecation.silence do
91
+ post "reboot", { :id => host.id }, set_session_user
92
+ end
84
93
  assert_redirected_to discovered_hosts_url
85
94
  assert_nil flash[:error]
86
95
  assert_equal "Rebooting host #{host.name}", flash[:notice]
@@ -90,7 +99,7 @@ class DiscoveredHostsControllerTest < ActionController::TestCase
90
99
  @request.env["HTTP_REFERER"] = discovered_hosts_url
91
100
  host = Host::Discovered.import_host_and_facts(@facts).first
92
101
  ::ForemanDiscovery::NodeAPI::PowerService.any_instance.expects(:reboot).returns(false)
93
- post "reboot", { :id => host.id }, set_session_user
102
+ post "reboot", { :id => host.id }, set_session_user_default_manager
94
103
  assert_redirected_to discovered_hosts_url
95
104
  assert_equal "Failed to reboot host #{host.name}", flash[:error]
96
105
  end
@@ -99,7 +108,7 @@ class DiscoveredHostsControllerTest < ActionController::TestCase
99
108
  @request.env["HTTP_REFERER"] = discovered_hosts_url
100
109
  host = Host::Discovered.import_host_and_facts(@facts).first
101
110
  ::ForemanDiscovery::NodeAPI::PowerService.any_instance.expects(:reboot).raises("request failed")
102
- post "reboot", { :id => host.id }, set_session_user
111
+ post "reboot", { :id => host.id }, set_session_user_default_manager
103
112
  assert_redirected_to discovered_hosts_url
104
113
  assert_match(/ERF50-4973/, flash[:error])
105
114
  end
@@ -109,7 +118,7 @@ class DiscoveredHostsControllerTest < ActionController::TestCase
109
118
  facts = @facts.merge({"somefact" => "abc"})
110
119
  host = Host::Discovered.import_host_and_facts(facts).first
111
120
  FactoryGirl.create(:discovery_rule, :priority => 1, :search => "facts.somefact = abc", :hostgroup => hostgroups(:common))
112
- post :auto_provision, { :id => host.id }, set_session_user
121
+ post :auto_provision, { :id => host.id }, set_session_user_default_manager
113
122
  assert_response :redirect
114
123
  assert_nil flash[:error]
115
124
  end
@@ -118,7 +127,7 @@ class DiscoveredHostsControllerTest < ActionController::TestCase
118
127
  disable_orchestration
119
128
  facts = @facts.merge({"somefact" => "abc"})
120
129
  host = Host::Discovered.import_host_and_facts(facts).first
121
- post :auto_provision, { :id => host.id }, set_session_user
130
+ post :auto_provision, { :id => host.id }, set_session_user_default_manager
122
131
  assert_response :redirect
123
132
  assert_nil flash[:error]
124
133
  end
@@ -128,7 +137,7 @@ class DiscoveredHostsControllerTest < ActionController::TestCase
128
137
  facts = @facts.merge({"somefact" => "abc"})
129
138
  Host::Discovered.import_host_and_facts(facts).first
130
139
  FactoryGirl.create(:discovery_rule, :priority => 1, :search => "facts.somefact = abc", :hostgroup => hostgroups(:common))
131
- post :auto_provision_all, {}, set_session_user
140
+ post :auto_provision_all, {}, set_session_user_default_manager
132
141
  assert_response :redirect
133
142
  assert_nil flash[:error]
134
143
  end
@@ -138,7 +147,7 @@ class DiscoveredHostsControllerTest < ActionController::TestCase
138
147
  facts = @facts.merge({"somefact" => "abc"})
139
148
  Host::Discovered.import_host_and_facts(facts).first
140
149
  FactoryGirl.create(:discovery_rule, :priority => 1, :search => "facts.somefact = abc", :hostgroup => hostgroups(:common))
141
- post :auto_provision_all, {}, set_session_user
150
+ post :auto_provision_all, {}, set_session_user_default_manager
142
151
  assert_response :redirect
143
152
  assert_nil flash[:error]
144
153
  end
@@ -147,7 +156,7 @@ class DiscoveredHostsControllerTest < ActionController::TestCase
147
156
  @request.env["HTTP_REFERER"] = discovered_hosts_url
148
157
  host = Host::Discovered.import_host_and_facts(@facts).first
149
158
  ::ForemanDiscovery::NodeAPI::PowerService.any_instance.expects(:reboot).returns(true)
150
- post "reboot", { :id => host.id }, set_session_user
159
+ post "reboot", { :id => host.id }, set_session_user_default_manager
151
160
  assert_redirected_to discovered_hosts_url
152
161
  assert_equal "Rebooting host #{host.name}", flash[:notice]
153
162
  end
@@ -156,7 +165,7 @@ class DiscoveredHostsControllerTest < ActionController::TestCase
156
165
  @request.env["HTTP_REFERER"] = discovered_hosts_url
157
166
  host = Host::Discovered.import_host_and_facts(@facts).first
158
167
  ::ForemanDiscovery::NodeAPI::PowerService.any_instance.expects(:reboot).returns(false)
159
- post "reboot_all", { }, set_session_user
168
+ post "reboot_all", { }, set_session_user_default_manager
160
169
  assert_redirected_to discovered_hosts_url
161
170
  assert_equal "Errors during reboot: #{host.name}: failed to reboot", flash[:error]
162
171
  end
@@ -165,7 +174,7 @@ class DiscoveredHostsControllerTest < ActionController::TestCase
165
174
  @request.env["HTTP_REFERER"] = discovered_hosts_url
166
175
  host = Host::Discovered.import_host_and_facts(@facts).first
167
176
  ::ForemanDiscovery::NodeAPI::PowerService.any_instance.expects(:reboot).raises("request failed")
168
- post "reboot_all", { }, set_session_user
177
+ post "reboot_all", { }, set_session_user_default_manager
169
178
  assert_redirected_to discovered_hosts_url
170
179
  assert_match(/ERF50-4973/, flash[:error])
171
180
  end
@@ -1,4 +1,5 @@
1
1
  require 'test_helper'
2
+ require 'test_helper_discovery'
2
3
 
3
4
  class DiscoveryRulesControllerTest < ActionController::TestCase
4
5
  setup :initialize_host
@@ -8,14 +9,14 @@ class DiscoveryRulesControllerTest < ActionController::TestCase
8
9
  assert_tag :tag => 'a', :attributes => {:href => '/discovery_rules'}
9
10
  end
10
11
 
11
- test "should get index" do
12
- get :index, {}, set_session_user
12
+ test "reader role should get index" do
13
+ get :index, {}, set_session_user_default_reader
13
14
  assert_response :success
14
15
  assert_not_nil assigns(:discovery_rules)
15
16
  end
16
17
 
17
18
  test "should get new" do
18
- get :new, {}, set_session_user
19
+ get :new, {}, set_session_user_default_manager
19
20
  assert_response :success
20
21
  end
21
22
 
@@ -26,7 +27,7 @@ class DiscoveryRulesControllerTest < ActionController::TestCase
26
27
  :search => "bar",
27
28
  :hostgroup_id => 1,
28
29
  :hostname => "",
29
- :priority => 1}}, set_session_user
30
+ :priority => 1}}, set_session_user_default_manager
30
31
  end
31
32
 
32
33
  assert_redirected_to discovery_rules_path
@@ -34,25 +35,25 @@ class DiscoveryRulesControllerTest < ActionController::TestCase
34
35
 
35
36
  test "should get edit" do
36
37
  rule = FactoryGirl.create(:discovery_rule)
37
- get :edit, {:id => rule.to_param}, set_session_user
38
+ get :edit, {:id => rule.to_param}, set_session_user_default_manager
38
39
  assert_response :success
39
40
  end
40
41
 
41
42
  test "should update discovery rule" do
42
43
  rule = FactoryGirl.create(:discovery_rule)
43
- put :update, {:id => rule.to_param, :discovery_rule => {}}, set_session_user
44
+ put :update, {:id => rule.to_param, :discovery_rule => {}}, set_session_user_default_manager
44
45
  assert_redirected_to discovery_rules_path
45
46
  end
46
47
 
47
48
  test "should destroy discovery rule" do
48
49
  rule = FactoryGirl.create(:discovery_rule)
49
50
  assert_difference('DiscoveryRule.count', -1) do
50
- delete :destroy, {:id => rule.to_param}, set_session_user
51
+ delete :destroy, {:id => rule.to_param}, set_session_user_default_manager
51
52
  end
52
53
 
53
54
  assert_redirected_to discovery_rules_path
54
55
  end
55
-
56
+
56
57
  private
57
58
 
58
59
  def initialize_host
@@ -21,7 +21,3 @@ class ActiveSupport::TestCase
21
21
 
22
22
  set_fixture_class({ :hosts => Host::Base })
23
23
  end
24
-
25
- def set_session_user
26
- SETTINGS[:login] ? {:user => User.admin.id, :expires_at => 5.minutes.from_now} : {}
27
- end
@@ -0,0 +1,24 @@
1
+ def set_session_user_with_perms perms
2
+ perms = perms.collect{|p| Permission.find_by_name(p) || Permission.create(:name => p) }
3
+ perms.each do |p|
4
+ p.resource_type = 'Host' if p.name =~ /discovered_hosts$/
5
+ p.resource_type = 'DiscoveryRule' if p.name =~ /discovery_rules$/
6
+ p.save!
7
+ end
8
+ role = FactoryGirl.create :role
9
+ perms.each do |perm|
10
+ FactoryGirl.create(:filter, :role => role, :permissions => [perm])
11
+ end
12
+ user = FactoryGirl.create :user, :with_mail, :admin => false
13
+ user.roles << role
14
+ user.save!
15
+ SETTINGS[:login] ? {:user => user, :expires_at => 5.minutes.from_now} : {}
16
+ end
17
+
18
+ def set_session_user_default_reader
19
+ set_session_user_with_perms Foreman::Plugin.find('foreman_discovery').default_roles['Discovery Reader']
20
+ end
21
+
22
+ def set_session_user_default_manager
23
+ set_session_user_with_perms Foreman::Plugin.find('foreman_discovery').default_roles['Discovery Manager']
24
+ end
@@ -11,6 +11,15 @@ class FindDiscoveryRulesTest < ActiveSupport::TestCase
11
11
  "macaddress_eth0" => "AA:BB:CC:DD:EE:FF",
12
12
  "discovery_bootif" => "AA:BB:CC:DD:EE:FF",
13
13
  }
14
+
15
+ FactoryGirl.create(:setting,
16
+ :name => 'discovery_hostname',
17
+ :value => 'discovery_bootif',
18
+ :category => 'Setting::Discovered')
19
+ FactoryGirl.create(:setting,
20
+ :name => 'discovery_prefix',
21
+ :value => 'mac',
22
+ :category => 'Setting::Discovered')
14
23
  end
15
24
 
16
25
  test "no rule is found for empty rule set" do
@@ -89,6 +98,28 @@ class FindDiscoveryRulesTest < ActiveSupport::TestCase
89
98
  assert_equal host.discovery_rule_id, r1.id
90
99
  end
91
100
 
101
+ test "auto provisioning fails for rule without a hostgroup" do
102
+ facts = @facts.merge({"somefact" => "abc"})
103
+ host = Host::Discovered.import_host_and_facts(facts).first
104
+ r1 = FactoryGirl.create(:discovery_rule, :priority => 1, :search => "facts.somefact = abc",
105
+ :organizations => [host.organization], :locations => [host.location])
106
+ r1.hostgroup = nil
107
+ exception = assert_raises(::Foreman::Exception) do
108
+ perform_auto_provision host, r1
109
+ end
110
+ assert_match /No hostgroup associated with rule/, exception.message
111
+ end
112
+
113
+ test "existing rule revent from hostgroup deletion" do
114
+ facts = @facts.merge({"somefact" => "abc"})
115
+ host = Host::Discovered.import_host_and_facts(facts).first
116
+ r1 = FactoryGirl.create(:discovery_rule, :priority => 1, :search => "facts.somefact = abc",
117
+ :organizations => [host.organization], :locations => [host.location])
118
+ assert_raises(ActiveRecord::RecordNotDestroyed) do
119
+ r1.hostgroup.destroy!
120
+ end
121
+ end
122
+
92
123
  test "rules with incorrect syntax are skipped" do
93
124
  facts = @facts.merge({"somefact" => "abc"})
94
125
  host = Host::Discovered.import_host_and_facts(facts).first
@@ -0,0 +1,44 @@
1
+ require 'test_plugin_helper'
2
+
3
+ class DiscoveredMailerTest < ActiveSupport::TestCase
4
+ setup do
5
+ @user = User.current = users :admin
6
+
7
+ FactoryGirl.create(:mail_notification,
8
+ :name => :discovered_summary,
9
+ :description => N_('A summary of discovered hosts'),
10
+ :mailer => 'DiscoveredMailer',
11
+ :method => 'discovered_summary',
12
+ :subscription_type => 'report',
13
+ )
14
+
15
+ @user.mail_notifications << MailNotification[:discovered_summary]
16
+
17
+ ActionMailer::Base.deliveries = []
18
+ @user.user_mail_notifications.first.deliver
19
+ @email = ActionMailer::Base.deliveries.first
20
+ end
21
+
22
+ test 'discovery mailer should deliver summary' do
23
+ assert @email.to.include?("admin@someware.com")
24
+ end
25
+
26
+ test 'discovery mailer should contain body' do
27
+ refute @email.body.nil?
28
+ end
29
+
30
+ test 'discovery mailer should have a correct subject' do
31
+ refute @email.subject.empty?
32
+ assert @email.subject.include? Setting[:email_subject_prefix].first
33
+ end
34
+
35
+ test 'discovery mailer sends Foreman URL in body' do
36
+ assert @email.body.parts.first.body.raw_source.include? Setting[:foreman_url]
37
+ end
38
+
39
+ test 'discovery mailer sends both html and text emails' do
40
+ assert_equal(2, @email.body.parts.size)
41
+ refute(@email.body.parts.first.body.raw_source.start_with?('<!DOCTYPE')) # text email
42
+ assert(@email.body.parts.last.body.raw_source.start_with?('<!DOCTYPE')) # html email
43
+ end
44
+ end
@@ -1,6 +1,18 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class DiscoveryAttributeSetTest < ActiveSupport::TestCase
4
+
5
+ setup do
6
+ FactoryGirl.create(:setting,
7
+ :name => 'discovery_hostname',
8
+ :value => 'discovery_bootif',
9
+ :category => 'Setting::Discovered')
10
+ FactoryGirl.create(:setting,
11
+ :name => 'discovery_prefix',
12
+ :value => 'mac',
13
+ :category => 'Setting::Discovered')
14
+ end
15
+
4
16
  test "can search discovered hosts by cpu" do
5
17
  raw = parse_json_fixture('/facts.json')
6
18
  host = Host::Discovered.import_host_and_facts(raw['facts']).first
@@ -35,6 +35,26 @@ class DiscoveryRuleTest < ActiveSupport::TestCase
35
35
  assert_equal "can't be blank", rule.errors[:hostgroup_id].first
36
36
  end
37
37
 
38
+ test "should not be able to create a rule with priority bigger than 2^31" do
39
+ rule = DiscoveryRule.new :name => "myrule",
40
+ :search => "cpu_count > 1",
41
+ :priority => 2**31 + 1,
42
+ :hostgroup_id => @hostgroup.id,
43
+ :organization_ids => [@hg_org.id]
44
+ refute_valid rule
45
+ assert_equal "must be less than 2147483648", rule.errors[:priority].first
46
+ end
47
+
48
+ test "should not be able to create a rule with max count bigger than 2^31" do
49
+ rule = DiscoveryRule.new :name => "myrule",
50
+ :search => "cpu_count > 1",
51
+ :max_count => 2**31 + 1,
52
+ :hostgroup_id => @hostgroup.id,
53
+ :organization_ids => [@hg_org.id]
54
+ refute_valid rule
55
+ assert_equal "must be less than 2147483648", rule.errors[:max_count].first
56
+ end
57
+
38
58
  test "should enforce hostgroup organizations" do
39
59
  skip unless SETTINGS[:organizations_enabled]
40
60
  rule = DiscoveryRule.new :name => "myrule",
@@ -1,20 +1,6 @@
1
1
  require 'test_helper'
2
2
 
3
- class PuppetFactParserStub
4
- attr_reader :interfaces, :facts
5
-
6
- def initialize(interfaces, facts)
7
- @interfaces = interfaces
8
- @facts = facts
9
- end
10
-
11
- def suggested_primary_interface(_); end
12
- def parse_interfaces?; end
13
-
14
- include PuppetFactParserExtensions
15
- end
16
-
17
- class PuppetFactParserTest < ActiveSupport::TestCase
3
+ class FactParserTest < ActiveSupport::TestCase
18
4
  setup do
19
5
  interfaces = {
20
6
  'eth0' => {'macaddress' => 'aa:bb:cc:dd:ee:f1', 'ipaddress' => '192.168.1.1'},
@@ -32,7 +18,8 @@ class PuppetFactParserTest < ActiveSupport::TestCase
32
18
  :macaddress => 'aa:bb:cc:dd:ee:f2',
33
19
  :discovery_bootif => 'aa:bb:cc:dd:ee:f3'
34
20
  }.with_indifferent_access
35
- @parser = PuppetFactParserStub.new(interfaces, facts)
21
+ @parser = ::ForemanDiscovery::FactParser.new(facts)
22
+ @parser.stubs(:interfaces).returns(interfaces)
36
23
  @host = Host::Discovered.new(:name => 'dummy')
37
24
  end
38
25
 
@@ -6,6 +6,10 @@ class HostDiscoveredTest < ActiveSupport::TestCase
6
6
  :name => 'discovery_fact',
7
7
  :value => 'discovery_bootif',
8
8
  :category => 'Setting::Discovered')
9
+ FactoryGirl.create(:setting,
10
+ :name => 'discovery_hostname',
11
+ :value => 'discovery_bootif',
12
+ :category => 'Setting::Discovered')
9
13
  FactoryGirl.create(:setting,
10
14
  :name => 'discovery_prefix',
11
15
  :value => 'mac',
@@ -65,9 +69,19 @@ class HostDiscoveredTest < ActiveSupport::TestCase
65
69
  assert host.refresh_facts
66
70
  end
67
71
 
72
+ test "should create discovered host with hostname if a fact was supplied" do
73
+ raw = parse_json_fixture('/facts.json')
74
+ Setting[:discovery_hostname] = 'somefact'
75
+ facts = raw['facts'].merge({"somefact" => "somename"})
76
+ host = Host::Discovered.import_host_and_facts(facts).first
77
+ assert_equal 'macsomename', host.name
78
+ refute_equal 'e4:1f:13:cc:36:5a', host.mac
79
+ end
80
+
68
81
  test "should create discovered host with fact_name as a name if it is a valid mac" do
69
82
  raw = parse_json_fixture('/facts.json')
70
83
  Setting[:discovery_fact] = 'somefact'
84
+ Setting[:discovery_hostname] = 'somefact'
71
85
  facts = raw['facts'].merge({"somefact" => "E4:1F:13:CC:36:5A"})
72
86
  host = Host::Discovered.import_host_and_facts(facts).first
73
87
  assert_equal 'mace41f13cc365a', host.name
@@ -90,25 +104,31 @@ class HostDiscoveredTest < ActiveSupport::TestCase
90
104
  assert_equal 'teste41f13cc3658', host.name
91
105
  end
92
106
 
93
- test "should not name discovered host with prefix that starts with a number, fallback to 'mac'" do
107
+ test "should create discovered host without prefix" do
94
108
  raw = parse_json_fixture('/facts.json')
95
- Setting[:discovery_prefix] = '123'
109
+ Setting[:discovery_prefix] = ''
96
110
  host = Host::Discovered.import_host_and_facts(raw['facts']).first
97
- assert host.name.start_with?('mac')
111
+ assert_equal 'e41f13cc3658',host.name
98
112
  end
99
113
 
100
- test "should not name discovered host with prefix that starts with a special character, fallback to 'mac'" do
114
+ test "should raise when hostname fact cannot be found" do
101
115
  raw = parse_json_fixture('/facts.json')
102
- Setting[:discovery_prefix] = '^abc'
103
- host = Host::Discovered.import_host_and_facts(raw['facts']).first
104
- assert host.name.start_with?('mac')
116
+ Setting[:discovery_hostname] = 'macaddress_foo'
117
+ exception = assert_raises(::Foreman::Exception) do
118
+ Host::Discovered.import_host_and_facts(raw['facts'])
119
+ end
120
+ assert_match(/Invalid facts: hash does not contain a valid value for any of the facts in the discovery_hostname setting:/, exception.message)
105
121
  end
106
122
 
107
- test "should not name discovered host with prefix that starts with a _, fallback to 'mac'" do
123
+ test "should raise when hostname cannot be computed due to normlization and no prefix" do
108
124
  raw = parse_json_fixture('/facts.json')
109
- Setting[:discovery_prefix] = '_abc'
110
- host = Host::Discovered.import_host_and_facts(raw['facts']).first
111
- assert host.name.start_with?('mac')
125
+ raw['facts']['invalidhostnamefact'] = '...'
126
+ Setting[:discovery_hostname] = 'invalidhostnamefact'
127
+ Setting[:discovery_prefix] = ''
128
+ exception = assert_raises(::Foreman::Exception) do
129
+ Host::Discovered.import_host_and_facts(raw['facts'])
130
+ end
131
+ assert_match(/Invalid hostname: Could not normalize the hostname/, exception.message)
112
132
  end
113
133
 
114
134
  test 'discovered host can be searched in multiple taxonomies' do
@@ -148,6 +168,31 @@ class HostDiscoveredTest < ActiveSupport::TestCase
148
168
  assert Token.where(:host_id => h.id).empty?
149
169
  end
150
170
 
171
+ test "normalization of MAC into a discovered host hostname" do
172
+ assert_equal Host::Discovered.normalize_string_for_hostname("90:B1:1C:54:D5:82"),"90b11c54d582"
173
+ end
174
+
175
+ test "normalization of a string containing multiple non-alphabetical characters" do
176
+ assert_equal Host::Discovered.normalize_string_for_hostname(".-_Test::Host.name_-."),"testhostname"
177
+ end
178
+
179
+ test "normalization of a valid hostname" do
180
+ assert_equal Host::Discovered.normalize_string_for_hostname("testhostname"),"testhostname"
181
+ end
182
+
183
+ test "empty string after hostname normalization should raise an error" do
184
+ exception = assert_raises(::Foreman::Exception) do
185
+ Host::Discovered.normalize_string_for_hostname(".-_")
186
+ end
187
+ assert_match(/Invalid hostname: Could not normalize the hostname/, exception.message)
188
+ end
189
+
190
+ test "chooshing the first valid fact from array of fact names" do
191
+ facts = {"custom_hostname" => "testhostname","notmyfact" => "notusedfactvalue"}
192
+ discovery_hostname_fact_array = ['macaddress','custom_hostname','someotherfact']
193
+ assert_equal Host::Discovered.return_first_valid_fact(discovery_hostname_fact_array,facts),"testhostname"
194
+ end
195
+
151
196
  def parse_json_fixture(relative_path)
152
197
  return JSON.parse(File.read(File.expand_path(File.dirname(__FILE__) + relative_path)))
153
198
  end