foreman_discovery 3.0.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/app/controllers/api/v2/discovered_hosts_controller.rb +31 -11
  4. data/app/controllers/api/v2/discovery_rules_controller.rb +8 -16
  5. data/app/controllers/concerns/foreman/controller/discovered_extensions.rb +47 -2
  6. data/app/controllers/discovered_hosts_controller.rb +30 -55
  7. data/app/controllers/discovery_rules_controller.rb +2 -4
  8. data/app/helpers/discovered_hosts_helper.rb +27 -5
  9. data/app/lib/facter_utils.rb +18 -0
  10. data/app/lib/puppet_fact_parser_extensions.rb +18 -32
  11. data/app/models/discovery_rule.rb +33 -2
  12. data/app/models/host/discovered.rb +47 -61
  13. data/app/models/host/managed_extensions.rb +12 -3
  14. data/app/models/setting/discovered.rb +18 -6
  15. data/app/services/foreman_discovery/host_converter.rb +22 -0
  16. data/app/views/api/v2/discovered_hosts/main.json.rabl +1 -1
  17. data/app/views/api/v2/discovery_rules/main.json.rabl +6 -1
  18. data/app/views/api/v2/discovery_rules/show.json.rabl +4 -0
  19. data/app/views/dashboard/_discovery_widget.html.erb +6 -4
  20. data/app/views/dashboard/_discovery_widget_host.html.erb +4 -0
  21. data/app/views/dashboard/_discovery_widget_host_list.html.erb +2 -3
  22. data/app/views/discovered_hosts/_discovered_host.html.erb +3 -5
  23. data/app/views/discovered_hosts/_discovered_hosts_list.html.erb +7 -7
  24. data/app/views/discovered_hosts/index.html.erb +1 -0
  25. data/app/views/discovery_rules/_form.html.erb +51 -32
  26. data/config/routes.rb +4 -4
  27. data/db/migrate/20150310153859_remove_discovery_attribute_sets_from_managed_hosts.rb +8 -0
  28. data/db/migrate/20150331132115_remove_old_permissions.rb +16 -0
  29. data/db/migrate/20150505111345_remove_leftover_tokens.rb +13 -0
  30. data/db/migrate/20150512150432_remove_old_discovery_reader_permissions.rb +10 -0
  31. data/db/migrate/20150714144500_review_discovery_permissions.rb +17 -0
  32. data/lib/foreman_discovery/engine.rb +46 -16
  33. data/lib/foreman_discovery/version.rb +1 -1
  34. data/locale/de/LC_MESSAGES/foreman_discovery.mo +0 -0
  35. data/locale/de/foreman_discovery.po +74 -72
  36. data/locale/de/foreman_discovery.pox +40 -0
  37. data/locale/en_GB/LC_MESSAGES/foreman_discovery.mo +0 -0
  38. data/locale/en_GB/foreman_discovery.po +90 -88
  39. data/locale/en_GB/foreman_discovery.pox +0 -0
  40. data/locale/es/LC_MESSAGES/foreman_discovery.mo +0 -0
  41. data/locale/es/foreman_discovery.po +3 -3
  42. data/locale/es/foreman_discovery.pox +41 -0
  43. data/locale/fr/LC_MESSAGES/foreman_discovery.mo +0 -0
  44. data/locale/fr/foreman_discovery.po +3 -3
  45. data/locale/fr/foreman_discovery.pox +69 -0
  46. data/locale/gl/LC_MESSAGES/foreman_discovery.mo +0 -0
  47. data/locale/gl/foreman_discovery.po +3 -3
  48. data/locale/gl/foreman_discovery.pox +21 -0
  49. data/locale/it/LC_MESSAGES/foreman_discovery.mo +0 -0
  50. data/locale/it/foreman_discovery.po +3 -3
  51. data/locale/it/foreman_discovery.pox +0 -0
  52. data/locale/ja/LC_MESSAGES/foreman_discovery.mo +0 -0
  53. data/locale/ja/foreman_discovery.po +67 -66
  54. data/locale/ja/foreman_discovery.pox +29 -0
  55. data/locale/ko/LC_MESSAGES/foreman_discovery.mo +0 -0
  56. data/locale/ko/foreman_discovery.po +3 -3
  57. data/locale/ko/foreman_discovery.pox +189 -0
  58. data/locale/messages.mo +0 -0
  59. data/locale/pt_BR/LC_MESSAGES/foreman_discovery.mo +0 -0
  60. data/locale/pt_BR/foreman_discovery.po +7 -7
  61. data/locale/pt_BR/foreman_discovery.pox +0 -0
  62. data/locale/ru/LC_MESSAGES/foreman_discovery.mo +0 -0
  63. data/locale/ru/foreman_discovery.po +98 -96
  64. data/locale/ru/foreman_discovery.pox +0 -0
  65. data/locale/sv_SE/LC_MESSAGES/foreman_discovery.mo +0 -0
  66. data/locale/sv_SE/foreman_discovery.po +3 -3
  67. data/locale/zh_CN/LC_MESSAGES/foreman_discovery.mo +0 -0
  68. data/locale/zh_CN/foreman_discovery.po +3 -3
  69. data/locale/zh_CN/foreman_discovery.pox +33 -0
  70. data/locale/zh_TW/LC_MESSAGES/foreman_discovery.mo +0 -0
  71. data/locale/zh_TW/foreman_discovery.po +3 -3
  72. data/locale/zh_TW/foreman_discovery.pox +23 -0
  73. data/test/functional/api/v2/discovered_hosts_controller_test.rb +45 -7
  74. data/test/functional/discovered_hosts_controller_test.rb +52 -11
  75. data/test/unit/discovered_extensions_test.rb +40 -17
  76. data/test/unit/discovery_rule_test.rb +59 -0
  77. data/test/unit/host_discovered_test.rb +69 -6
  78. data/test/unit/puppet_fact_parser_extensions_test.rb +55 -40
  79. metadata +103 -81
  80. data/app/services/host_converter.rb +0 -30
@@ -65,7 +65,9 @@ class Api::V2::DiscoveredHostsControllerTest < ActionController::TestCase
65
65
  def test_auto_provision_success_via_upload
66
66
  disable_orchestration
67
67
  facts = @facts.merge({"somefact" => "abc"})
68
- FactoryGirl.create(:discovery_rule, :priority => 1, :name => 'rule', :search => "facts.somefact = abc", :hostgroup => hostgroups(:common))
68
+ FactoryGirl.create(:discovery_rule, :priority => 1, :name => 'rule', :search => "facts.somefact = abc",
69
+ :hostgroup => FactoryGirl.create(:hostgroup, :with_os, :with_rootpass), :organizations => [Organization.first],
70
+ :locations => [Location.first])
69
71
  post :facts, { :facts => facts }
70
72
  assert_response :success
71
73
  assert_equal "Auto-discovered and provisioned via rule 'rule'", Host.first.comment
@@ -75,8 +77,35 @@ class Api::V2::DiscoveredHostsControllerTest < ActionController::TestCase
75
77
  disable_orchestration
76
78
  facts = @facts.merge({"somefact" => "abc"})
77
79
  host = Host::Discovered.import_host_and_facts(facts).first
78
- FactoryGirl.create(:discovery_rule, :priority => 1, :search => "facts.somefact = abc", :hostgroup => hostgroups(:common))
80
+ FactoryGirl.create(:discovery_rule, :priority => 1, :search => "facts.somefact = abc",
81
+ :hostgroup => FactoryGirl.create(:hostgroup, :with_os, :with_rootpass),
82
+ :organizations => [host.organization], :locations => [host.location])
79
83
  post :auto_provision, { :id => host.id }
84
+ assert_match host.name, @response.body
85
+ assert_response :success
86
+ end
87
+
88
+ def test_auto_provision_with_wrong_org_or_loc_fail
89
+ disable_orchestration
90
+ facts = @facts.merge({"somefact" => "abc"})
91
+ host = Host::Discovered.import_host_and_facts(facts).first
92
+ FactoryGirl.create(:discovery_rule, :priority => 1, :search => "facts.somefact = abc",
93
+ :hostgroup => FactoryGirl.create(:hostgroup, :with_os, :with_rootpass))
94
+ post :auto_provision, { :id => host.id }
95
+ show_response = ActiveSupport::JSON.decode(@response.body)
96
+ assert_equal "No rule found for host #{host.name}", show_response["error"]["message"]
97
+ end
98
+
99
+ def test_auto_provision_with_wrong_disabled_org_sucess
100
+ disable_orchestration
101
+ SETTINGS[:organizations_enabled] = false
102
+ facts = @facts.merge({"somefact" => "abc"})
103
+ host = Host::Discovered.import_host_and_facts(facts).first
104
+ FactoryGirl.create(:discovery_rule, :priority => 1, :search => "facts.somefact = abc",
105
+ :hostgroup => FactoryGirl.create(:hostgroup, :with_os, :with_rootpass),
106
+ :locations => [host.location])
107
+ post :auto_provision, { :id => host.id }
108
+ assert_match host.name, @response.body
80
109
  assert_response :success
81
110
  end
82
111
 
@@ -84,30 +113,38 @@ class Api::V2::DiscoveredHostsControllerTest < ActionController::TestCase
84
113
  disable_orchestration
85
114
  facts = @facts.merge({"somefact" => "abc"})
86
115
  host = Host::Discovered.import_host_and_facts(facts).first
87
- FactoryGirl.create(:discovery_rule, :priority => 1, :search => "facts.somefact = abc", :hostgroup => hostgroups(:common))
116
+ FactoryGirl.create(:discovery_rule, :priority => 1, :search => "facts.somefact = abc",
117
+ :hostgroup => FactoryGirl.create(:hostgroup, :with_os, :with_rootpass),
118
+ :organizations => [host.organization], :locations => [host.location])
88
119
  post :auto_provision, { :id => host.id }
89
120
  assert_response :success
90
121
  # test deletion of a managed host
91
122
  switch_controller(::Api::V2::HostsController) do
92
123
  delete :destroy, { :id => host.id }
124
+ assert_match host.name, @response.body
93
125
  assert_response :success
94
126
  end
95
127
  end
96
128
 
97
- def test_auto_provision_no_rule_success
129
+ def test_auto_provision_no_rule_error
98
130
  disable_orchestration
99
131
  facts = @facts.merge({"somefact" => "abc"})
100
132
  host = Host::Discovered.import_host_and_facts(facts).first
101
133
  post :auto_provision, { :id => host.id }
102
- assert_response :success
134
+ assert_response :not_found
135
+ show_response = ActiveSupport::JSON.decode(@response.body)
136
+ assert_equal "No rule found for host #{host.name}", show_response["error"]["message"]
103
137
  end
104
138
 
105
139
  def test_auto_provision_all_success
106
140
  disable_orchestration
107
141
  facts = @facts.merge({"somefact" => "abc"})
108
- Host::Discovered.import_host_and_facts(facts).first
109
- FactoryGirl.create(:discovery_rule, :priority => 1, :search => "facts.somefact = abc", :hostgroup => hostgroups(:common))
142
+ host = Host::Discovered.import_host_and_facts(facts).first
143
+ FactoryGirl.create(:discovery_rule, :priority => 1, :search => "facts.somefact = abc",
144
+ :hostgroup => FactoryGirl.create(:hostgroup, :with_os, :with_rootpass), :organizations => [host.organization],
145
+ :locations => [host.location])
110
146
  post :auto_provision_all, {}
147
+ assert_equal '{}', @response.body
111
148
  assert_response :success
112
149
  end
113
150
 
@@ -116,6 +153,7 @@ class Api::V2::DiscoveredHostsControllerTest < ActionController::TestCase
116
153
  facts = @facts.merge({"somefact" => "abc"})
117
154
  Host::Discovered.import_host_and_facts(facts).first
118
155
  post :auto_provision_all, {}
156
+ assert_equal '{}', @response.body
119
157
  assert_response :success
120
158
  end
121
159
 
@@ -6,12 +6,17 @@ class DiscoveredHostsControllerTest < ActionController::TestCase
6
6
  setup do
7
7
  @request.env['HTTP_REFERER'] = '/discovery_rules'
8
8
  @facts = {
9
- "interfaces" => "lo,eth0",
10
- "ipaddress" => "192.168.100.42",
11
- "ipaddress_eth0" => "192.168.100.42",
12
- "macaddress_eth0" => "AA:BB:CC:DD:EE:FF",
13
- "discovery_bootif" => "AA:BB:CC:DD:EE:FF",
9
+ "interfaces" => "lo,eth0",
10
+ "ipaddress" => "192.168.100.42",
11
+ "ipaddress_eth0" => "192.168.100.42",
12
+ "macaddress_eth0" => "AA:BB:CC:DD:EE:FF",
13
+ "discovery_bootif" => "AA:BB:CC:DD:EE:FF",
14
+ "physicalprocessorcount" => "42",
14
15
  }
16
+ FactoryGirl.create(:setting,
17
+ :name => 'discovery_reboot',
18
+ :value => true,
19
+ :category => 'Setting::Discovered')
15
20
  end
16
21
 
17
22
  def test_index
@@ -31,7 +36,7 @@ class DiscoveredHostsControllerTest < ActionController::TestCase
31
36
  assert_response :success
32
37
  end
33
38
 
34
- def test_edit_form
39
+ def test_edit_form_elements
35
40
  host = Host::Discovered.import_host_and_facts(@facts).first
36
41
  get :edit, {:id => host.id}, set_session_user
37
42
  assert_select "select" do |elements|
@@ -42,9 +47,12 @@ class DiscoveredHostsControllerTest < ActionController::TestCase
42
47
  end
43
48
  end
44
49
 
45
- def test_index_json
46
- get :index, {:format => "json"}, set_session_user
47
- assert_response :success
50
+ def test_edit_form_attributes
51
+ host = Host::Discovered.import_host_and_facts(@facts).first
52
+ Host.transaction do
53
+ get :edit, {:id => host.id}, set_session_user
54
+ end
55
+ assert_not_nil host.cpu_count
48
56
  end
49
57
 
50
58
  def test_add_entry_to_nav_menu
@@ -80,10 +88,10 @@ class DiscoveredHostsControllerTest < ActionController::TestCase
80
88
  host = FactoryGirl.create(:host,
81
89
  :ip => '1.2.3.4',
82
90
  :type => "Host::Discovered")
83
- ::ProxyAPI::BMC.any_instance.expects(:power).raises("request must fail")
91
+ ::ProxyAPI::BMC.any_instance.expects(:power).raises("request failed")
84
92
  post "reboot", { :id => host.id }, set_session_user
85
93
  assert_redirected_to discovered_hosts_url
86
- assert_equal "Failed to reboot host #{host.name} with error request must fail", flash[:error]
94
+ assert_match(/ERF50-6734/, flash[:error])
87
95
  end
88
96
 
89
97
  def test_auto_provision_success
@@ -125,6 +133,39 @@ class DiscoveredHostsControllerTest < ActionController::TestCase
125
133
  assert_nil flash[:error]
126
134
  end
127
135
 
136
+ def test_reboot_all_success
137
+ @request.env["HTTP_REFERER"] = discovered_hosts_url
138
+ host = FactoryGirl.create(:host,
139
+ :ip => '1.2.3.4',
140
+ :type => "Host::Discovered")
141
+ ::ProxyAPI::BMC.any_instance.stubs(:power).returns(true)
142
+ post "reboot", { :id => host.id }, set_session_user
143
+ assert_redirected_to discovered_hosts_url
144
+ assert_equal "Rebooting host #{host.name}", flash[:notice]
145
+ end
146
+
147
+ def test_reboot_all_failure
148
+ @request.env["HTTP_REFERER"] = discovered_hosts_url
149
+ host = FactoryGirl.create(:host,
150
+ :ip => '1.2.3.4',
151
+ :type => "Host::Discovered")
152
+ ::ProxyAPI::BMC.any_instance.stubs(:power).returns(false)
153
+ post "reboot_all", { }, set_session_user
154
+ assert_redirected_to discovered_hosts_url
155
+ assert_equal "Errors during reboot: #{host.name}: failed to reboot", flash[:error]
156
+ end
157
+
158
+ def test_reboot_all_error
159
+ @request.env["HTTP_REFERER"] = discovered_hosts_url
160
+ FactoryGirl.create(:host,
161
+ :ip => '1.2.3.4',
162
+ :type => "Host::Discovered")
163
+ ::ProxyAPI::BMC.any_instance.expects(:power).raises("request must fail")
164
+ post "reboot_all", { }, set_session_user
165
+ assert_redirected_to discovered_hosts_url
166
+ assert_match(/ERF50-6734/, flash[:error])
167
+ end
168
+
128
169
  private
129
170
 
130
171
  def initialize_host
@@ -41,31 +41,38 @@ class FindDiscoveryRulesTest < ActiveSupport::TestCase
41
41
  test "rule out of one is found for a discovered host" do
42
42
  facts = @facts.merge({"somefact" => "abc"})
43
43
  host = Host::Discovered.import_host_and_facts(facts).first
44
- r1 = FactoryGirl.create(:discovery_rule, :priority => 1, :search => "facts.somefact = abc")
44
+ r1 = FactoryGirl.create(:discovery_rule, :priority => 1, :search => "facts.somefact = abc",
45
+ :organizations => [host.organization], :locations => [host.location])
45
46
  assert_equal find_discovery_rule(host), r1
46
47
  end
47
48
 
48
49
  test "first rule out of two is found for a discovered host" do
49
50
  facts = @facts.merge({"somefact" => "abc"})
50
51
  host = Host::Discovered.import_host_and_facts(facts).first
51
- r1 = FactoryGirl.create(:discovery_rule, :priority => 1, :search => "facts.somefact = abc")
52
- FactoryGirl.create(:discovery_rule, :priority => 2, :search => "facts.somefact = abc")
52
+ r1 = FactoryGirl.create(:discovery_rule, :priority => 1, :search => "facts.somefact = abc",
53
+ :organizations => [host.organization], :locations => [host.location])
54
+ FactoryGirl.create(:discovery_rule, :priority => 2, :search => "facts.somefact = abc",
55
+ :organizations => [host.organization], :locations => [host.location])
53
56
  assert_equal find_discovery_rule(host), r1
54
57
  end
55
58
 
56
59
  test "second rule out of two is found for a discovered host" do
57
60
  facts = @facts.merge({"somefact" => "abc"})
58
61
  host = Host::Discovered.import_host_and_facts(facts).first
59
- FactoryGirl.create(:discovery_rule, :priority => 1, :search => "facts.somefact = x")
60
- r2 = FactoryGirl.create(:discovery_rule, :priority => 2, :search => "facts.somefact = abc")
62
+ FactoryGirl.create(:discovery_rule, :priority => 1, :search => "facts.somefact = x",
63
+ :organizations => [host.organization], :locations => [host.location])
64
+ r2 = FactoryGirl.create(:discovery_rule, :priority => 2, :search => "facts.somefact = abc",
65
+ :organizations => [host.organization], :locations => [host.location])
61
66
  assert_equal find_discovery_rule(host), r2
62
67
  end
63
68
 
64
69
  test "drained rule does not match for a discovered host" do
65
70
  facts = @facts.merge({"somefact" => "abc"})
66
71
  host = Host::Discovered.import_host_and_facts(facts).first
67
- r1 = FactoryGirl.create(:discovery_rule, :priority => 1, :search => "facts.somefact = abc", :max_count => 1)
68
- r2 = FactoryGirl.create(:discovery_rule, :priority => 2, :search => "facts.somefact = abc")
72
+ r1 = FactoryGirl.create(:discovery_rule, :priority => 1, :search => "facts.somefact = abc", :max_count => 1,
73
+ :organizations => [host.organization], :locations => [host.location])
74
+ r2 = FactoryGirl.create(:discovery_rule, :priority => 2, :search => "facts.somefact = abc",
75
+ :organizations => [host.organization], :locations => [host.location])
69
76
  FactoryGirl.create(:host, :discovery_rule => r1)
70
77
  assert_equal find_discovery_rule(host), r2
71
78
  end
@@ -73,7 +80,8 @@ class FindDiscoveryRulesTest < ActiveSupport::TestCase
73
80
  test "discovery rule is associated after auto provisioning" do
74
81
  facts = @facts.merge({"somefact" => "abc"})
75
82
  host = Host::Discovered.import_host_and_facts(facts).first
76
- r1 = FactoryGirl.create(:discovery_rule, :priority => 1, :search => "facts.somefact = abc")
83
+ r1 = FactoryGirl.create(:discovery_rule, :priority => 1, :search => "facts.somefact = abc",
84
+ :organizations => [host.organization], :locations => [host.location])
77
85
  perform_auto_provision host, r1
78
86
  assert_equal host.primary_interface.managed, true
79
87
  assert_equal host.build, true
@@ -84,15 +92,18 @@ class FindDiscoveryRulesTest < ActiveSupport::TestCase
84
92
  test "rules with incorrect syntax are skipped" do
85
93
  facts = @facts.merge({"somefact" => "abc"})
86
94
  host = Host::Discovered.import_host_and_facts(facts).first
87
- FactoryGirl.create(:discovery_rule, :priority => 1, :search => '=!^$#@?x')
88
- r2 = FactoryGirl.create(:discovery_rule, :priority => 2, :search => "facts.somefact = abc")
95
+ FactoryGirl.create(:discovery_rule, :priority => 1, :search => '=!^$#@?x',
96
+ :organizations => [host.organization], :locations => [host.location])
97
+ r2 = FactoryGirl.create(:discovery_rule, :priority => 2, :search => "facts.somefact = abc",
98
+ :organizations => [host.organization], :locations => [host.location])
89
99
  assert_equal find_discovery_rule(host), r2
90
100
  end
91
101
 
92
102
  test "hostname is copied after auto provisioning" do
93
103
  facts = @facts.merge({"somefact" => "abc"})
94
104
  host = Host::Discovered.import_host_and_facts(facts).first
95
- r1 = FactoryGirl.create(:discovery_rule, :priority => 1, :search => "facts.somefact = abc")
105
+ r1 = FactoryGirl.create(:discovery_rule, :priority => 1, :search => "facts.somefact = abc",
106
+ :organizations => [host.organization], :locations => [host.location])
96
107
  perform_auto_provision host, r1
97
108
  assert_equal host.name, "macaabbccddeeff"
98
109
  end
@@ -121,7 +132,9 @@ class FindDiscoveryRulesTest < ActiveSupport::TestCase
121
132
  host = Host::Discovered.import_host_and_facts(@facts).first
122
133
  r1 = FactoryGirl.create(:discovery_rule,
123
134
  :search => "facts.somefact = abc",
124
- :hostname => '<%= "" %>')
135
+ :hostname => '<%= "" %>',
136
+ :organizations => [host.organization],
137
+ :locations => [host.location])
125
138
  perform_auto_provision host, r1
126
139
  assert_equal "macaabbccddeeff", host.name
127
140
  end
@@ -130,7 +143,9 @@ class FindDiscoveryRulesTest < ActiveSupport::TestCase
130
143
  host = Host::Discovered.import_host_and_facts(@facts).first
131
144
  r1 = FactoryGirl.create(:discovery_rule,
132
145
  :search => "facts.somefact = abc",
133
- :hostname => 'x<%= 1+1 %>')
146
+ :hostname => 'x<%= 1+1 %>',
147
+ :organizations => [host.organization],
148
+ :locations => [host.location])
134
149
  perform_auto_provision host, r1
135
150
  assert_equal "x2", host.name
136
151
  end
@@ -139,7 +154,9 @@ class FindDiscoveryRulesTest < ActiveSupport::TestCase
139
154
  host = Host::Discovered.import_host_and_facts(@facts).first
140
155
  r1 = FactoryGirl.create(:discovery_rule,
141
156
  :search => "facts.somefact = abc",
142
- :hostname => 'x<%= rand(4) %>')
157
+ :hostname => 'x<%= rand(4) %>',
158
+ :organizations => [host.organization],
159
+ :locations => [host.location])
143
160
  perform_auto_provision host, r1
144
161
  assert_match(/x[0123]/, host.name)
145
162
  end
@@ -148,7 +165,9 @@ class FindDiscoveryRulesTest < ActiveSupport::TestCase
148
165
  host = Host::Discovered.import_host_and_facts(@facts).first
149
166
  r1 = FactoryGirl.create(:discovery_rule,
150
167
  :search => "facts.somefact = abc",
151
- :hostname => 'x<%= @host.name %>')
168
+ :hostname => 'x<%= @host.name %>',
169
+ :organizations => [host.organization],
170
+ :locations => [host.location])
152
171
  perform_auto_provision host, r1
153
172
  assert_equal "xmacaabbccddeeff", host.name
154
173
  end
@@ -157,7 +176,9 @@ class FindDiscoveryRulesTest < ActiveSupport::TestCase
157
176
  host = Host::Discovered.import_host_and_facts(@facts).first
158
177
  r1 = FactoryGirl.create(:discovery_rule,
159
178
  :search => "facts.somefact = abc",
160
- :hostname => 'x<%= @host.ip.gsub(".","-") %>')
179
+ :hostname => 'x<%= @host.ip.gsub(".","-") %>',
180
+ :organizations => [host.organization],
181
+ :locations => [host.location])
161
182
  perform_auto_provision host, r1
162
183
  assert_equal "x192-168-100-42", host.name
163
184
  end
@@ -167,7 +188,9 @@ class FindDiscoveryRulesTest < ActiveSupport::TestCase
167
188
  host = Host::Discovered.import_host_and_facts(@facts).first
168
189
  r1 = FactoryGirl.create(:discovery_rule,
169
190
  :search => "facts.somefact = abc",
170
- :hostname => 'x<%= @host.facts["somefact"] %>')
191
+ :hostname => 'x<%= @host.facts["somefact"] %>',
192
+ :organizations => [host.organization],
193
+ :locations => [host.location])
171
194
  perform_auto_provision host, r1
172
195
  assert_equal "xabc", host.name
173
196
  end
@@ -0,0 +1,59 @@
1
+ require 'test_helper'
2
+
3
+ class DiscoveryRuleTest < ActiveSupport::TestCase
4
+ setup do
5
+ @hg_org = FactoryGirl.create(:organization, :name => 'hgorg')
6
+ @hg_org.save!
7
+ @hostgroup = FactoryGirl.create(:hostgroup)
8
+ @hostgroup.organizations << @hg_org
9
+ @hostgroup.save!
10
+ end
11
+
12
+ test "should be able to create a rule without entering hosts limit" do
13
+ rule = DiscoveryRule.new :name => "myrule",
14
+ :search => "cpu_count > 1",
15
+ :priority => "1",
16
+ :hostgroup_id => @hostgroup.id,
17
+ :organization_ids => [@hg_org.id]
18
+ assert_valid rule
19
+ end
20
+
21
+ test "should be able to create a rule without entering priority" do
22
+ rule = DiscoveryRule.new :name => "myrule",
23
+ :search => "cpu_count > 1",
24
+ :max_count => "1",
25
+ :hostgroup_id => @hostgroup.id,
26
+ :organization_ids => [@hg_org.id]
27
+ assert_valid rule
28
+ end
29
+
30
+ test "should not be able to create a rule without a hostgroup" do
31
+ rule = DiscoveryRule.new :name => "myrule",
32
+ :search => "cpu_count > 1",
33
+ :organization_ids => [@hg_org.id]
34
+ refute_valid rule
35
+ assert_equal "can't be blank", rule.errors[:hostgroup_id].first
36
+ end
37
+
38
+ test "should enforce hostgroup organizations" do
39
+ skip unless SETTINGS[:organizations_enabled]
40
+ rule = DiscoveryRule.new :name => "myrule",
41
+ :search => "cpu_count > 1",
42
+ :hostgroup_id => @hostgroup.id
43
+ refute_valid rule
44
+ assert_equal "Host group organization #{@hg_org.name} must also be associated to the discovery rule", rule.errors[:organizations].first
45
+ end
46
+
47
+ test "should enforce hostgroup organizations and locations" do
48
+ skip unless (SETTINGS[:organizations_enabled] && SETTINGS[:locations_enabled])
49
+ loc = FactoryGirl.create(:location, :name => "hgloc")
50
+ @hostgroup.locations << loc
51
+ @hostgroup.save!
52
+ rule = DiscoveryRule.new :name => "myrule",
53
+ :search => "cpu_count > 1",
54
+ :hostgroup_id => @hostgroup.id,
55
+ :organization_ids => [@hg_org.id]
56
+ refute_valid rule
57
+ assert_equal "Host group location #{loc.name} must also be associated to the discovery rule", rule.errors[:locations].first
58
+ end
59
+ end
@@ -2,7 +2,6 @@ require 'test_helper'
2
2
 
3
3
  class HostDiscoveredTest < ActiveSupport::TestCase
4
4
  setup do
5
- User.current = User.find_by_login "admin"
6
5
  FactoryGirl.create(:setting,
7
6
  :name => 'discovery_fact',
8
7
  :value => 'discovery_bootif',
@@ -24,12 +23,39 @@ class HostDiscoveredTest < ActiveSupport::TestCase
24
23
  assert Host::Discovered.find_by_name('mace41f13cc3658')
25
24
  end
26
25
 
26
+ test "should setup subnet" do
27
+ raw = parse_json_fixture('/facts.json')
28
+ subnet = FactoryGirl.create(:subnet, :organizations => [Organization.first], :locations => [Location.first])
29
+ Subnet.expects(:subnet_for).with('10.35.27.3').returns(subnet)
30
+ host = Host::Discovered.import_host_and_facts(raw['facts']).first
31
+ assert_equal subnet, host.primary_interface.subnet
32
+ end
33
+
34
+ test "should setup subnet with org and loc" do
35
+ org = FactoryGirl.create(:organization, :name => "subnet_org")
36
+ loc = FactoryGirl.create(:location, :name => "subnet_loc")
37
+ FactoryGirl.create(:setting,
38
+ :name => 'discovery_organization',
39
+ :value => org.name,
40
+ :category => 'Setting::Discovered')
41
+ FactoryGirl.create(:setting,
42
+ :name => 'discovery_location',
43
+ :value => loc.name,
44
+ :category => 'Setting::Discovered')
45
+ raw = parse_json_fixture('/facts.json')
46
+ subnet = FactoryGirl.create(:subnet, :organizations => [org], :locations => [loc])
47
+ Subnet.expects(:subnet_for).with('10.35.27.3').returns(subnet)
48
+ host = Host::Discovered.import_host_and_facts(raw['facts']).first
49
+ assert_equal subnet, host.primary_interface.subnet
50
+ end
51
+
27
52
  test "should raise when fact_name setting isn't present" do
28
53
  raw = parse_json_fixture('/facts.json')
29
54
  Setting[:discovery_fact] = 'macaddress_foo'
30
- assert_raises Foreman::Exception do
55
+ exception = assert_raises(::Foreman::Exception) do
31
56
  Host::Discovered.import_host_and_facts(raw['facts'])
32
57
  end
58
+ assert_match(/Expected discovery_fact '\w+' is missing/, exception.message)
33
59
  end
34
60
 
35
61
  test "should be able to refresh facts" do
@@ -51,9 +77,10 @@ class HostDiscoveredTest < ActiveSupport::TestCase
51
77
  test "should create discovered host with default name if fact_name isn't a valid mac" do
52
78
  raw = parse_json_fixture('/facts.json')
53
79
  Setting[:discovery_fact] = 'lsbdistcodename'
54
- host = Host::Discovered.import_host_and_facts(raw['facts']).first
55
- assert_equal 'mace41f13cc3658', host.name
56
- assert_equal 'e4:1f:13:cc:36:58', host.mac
80
+ exception = assert_raises(::Foreman::Exception) do
81
+ Host::Discovered.import_host_and_facts(raw['facts'])
82
+ end
83
+ assert_match(/Unable to detect primary interface using MAC/, exception.message)
57
84
  end
58
85
 
59
86
  test "should create discovered host with prefix" do
@@ -84,8 +111,44 @@ class HostDiscoveredTest < ActiveSupport::TestCase
84
111
  assert host.name.start_with?('mac')
85
112
  end
86
113
 
114
+ test 'discovered host can be searched in multiple taxonomies' do
115
+ org1 = FactoryGirl.create(:organization)
116
+ org2 = FactoryGirl.create(:organization)
117
+ org3 = FactoryGirl.create(:organization)
118
+ user = FactoryGirl.create(:user, :organizations => [org1, org2])
119
+ host1 = FactoryGirl.create(:host, :type => "Host::Discovered", :organization => org1)
120
+ host2 = FactoryGirl.create(:host, :type => "Host::Discovered", :organization => org2)
121
+ host3 = FactoryGirl.create(:host, :type => "Host::Discovered", :organization => org3)
122
+ hosts = nil
123
+
124
+ assert_nil Organization.current
125
+ as_user(user) do
126
+ hosts = Host::Discovered.all
127
+ end
128
+ assert_includes hosts, host1
129
+ assert_includes hosts, host2
130
+ refute_includes hosts, host3
131
+
132
+ as_user(:one) do
133
+ hosts = Host::Discovered.all
134
+ end
135
+ assert_includes hosts, host1
136
+ assert_includes hosts, host2
137
+ assert_includes hosts, host3
138
+ end
139
+
140
+ test "provisioning a discovered host without saving it doesn't create a token" do
141
+ Setting[:token_duration] = 30 #enable tokens so that we only test the CR
142
+ raw = parse_json_fixture('/facts.json')
143
+ Setting[:discovery_prefix] = '123'
144
+ host = Host::Discovered.import_host_and_facts(raw['facts']).first
145
+ host.save
146
+ h = ::ForemanDiscovery::HostConverter.to_managed(host)
147
+ refute_valid h
148
+ assert Token.where(:host_id => h.id).empty?
149
+ end
150
+
87
151
  def parse_json_fixture(relative_path)
88
152
  return JSON.parse(File.read(File.expand_path(File.dirname(__FILE__) + relative_path)))
89
153
  end
90
-
91
154
  end
@@ -1,63 +1,78 @@
1
1
  require 'test_helper'
2
2
 
3
- class DummyPuppetFactParser
4
- def ip
5
- '192.168.0.30'
6
- end
7
-
8
- def primary_interface
9
- 'eth1'
10
- end
3
+ class PuppetFactParserStub
4
+ attr_reader :interfaces, :facts
11
5
 
12
- def interfaces
13
- {
14
- 'eth0' => {'macaddress' => 'aa:00:00:00:00:01', 'ipaddress' => '192.168.0.2'},
15
- 'eth3' => {'ipaddress' => '192.168.2.2'},
16
- 'eth2' => {'macaddress' => 'bb:00:00:00:00:01', 'ipaddress' => '192.168.1.2'}
17
- }.with_indifferent_access
6
+ def initialize(interfaces, facts)
7
+ @interfaces = interfaces
8
+ @facts = facts
18
9
  end
19
10
 
20
- def facts
21
- {
22
- :ipaddress => '192.168.0.2', :macaddress => 'bb:00:00:00:00:01', :interfaces => 'eth0,eth1,lo',
23
- :macaddress_eth1 => 'bb:00:00:00:00:01', :ipaddress_eth1 => '192.168.1.2',
24
- :macaddress_eth0 => 'aa:00:00:00:00:01', :ipaddress_eth0 => '192.168.0.2',
25
- :macaddress_lo => '', :ipaddress_lo => '127.0.0.1',
26
- :mac_fact => 'AA:00:00:00:00:01'
27
- }.with_indifferent_access
28
- end
11
+ def suggested_primary_interface(_); end
12
+ def parse_interfaces?; end
29
13
 
30
14
  include PuppetFactParserExtensions
31
15
  end
32
16
 
33
17
  class PuppetFactParserTest < ActiveSupport::TestCase
34
18
  setup do
35
- @parser = DummyPuppetFactParser.new
19
+ interfaces = {
20
+ 'eth0' => {'macaddress' => 'aa:bb:cc:dd:ee:f1', 'ipaddress' => '192.168.1.1'},
21
+ 'eth1' => {'macaddress' => 'aa:bb:cc:dd:ee:f2', 'ipaddress' => '192.168.1.2'},
22
+ 'eth2' => {'macaddress' => 'aa:bb:cc:dd:ee:f3', 'ipaddress' => '192.168.1.3'},
23
+ }.with_indifferent_access
24
+ facts = {
25
+ :macaddress_eth0 => 'aa:bb:cc:dd:ee:f1',
26
+ :macaddress_eth1 => 'aa:bb:cc:dd:ee:f2',
27
+ :macaddress_eth2 => 'aa:bb:cc:dd:ee:f3',
28
+ :ipaddress_eth0 => '192.168.1.1',
29
+ :ipaddress_eth1 => '192.168.1.2',
30
+ :ipaddress_eth2 => '192.168.1.3',
31
+ :ipaddress => '192.168.1.2',
32
+ :macaddress => 'aa:bb:cc:dd:ee:f2',
33
+ :discovery_bootif => 'aa:bb:cc:dd:ee:f3'
34
+ }.with_indifferent_access
35
+ @parser = PuppetFactParserStub.new(interfaces, facts)
36
+ @host = Host::Discovered.new(:name => 'dummy')
36
37
  end
37
38
 
38
- test '#ip_with_discovery_fact ignores discovery part completely if discovery facts are not present' do
39
- @parser.stubs(:discovery_mac_fact_name).returns('fact_that_does_not_exist')
40
- assert_equal '192.168.0.30', @parser.ip
39
+ test "#suggested_primary_interface detects interface eth2" do
40
+ assert_equal 'eth2', @parser.suggested_primary_interface(@host).try(:first)
41
41
  end
42
42
 
43
- test '#ip_with_discovery_fact finds primary interface based on discovery facts and returns it\'s ip' do
44
- @parser.stubs(:discovery_mac_fact_name).returns('mac_fact')
45
- assert_equal '192.168.0.2', @parser.ip
43
+ test "#suggested_primary_interface detects interface eth1" do
44
+ FacterUtils.stubs(:bootif_mac).returns('aa:bb:cc:dd:ee:f2')
45
+ assert_equal 'eth1', @parser.suggested_primary_interface(@host).try(:first)
46
46
  end
47
47
 
48
- test '#primary_interface_with_discovery_fact finds primary interface based on discovery facts' do
49
- @parser.stubs(:discovery_mac_fact_name).returns('mac_fact')
50
- assert_equal 'eth0', @parser.primary_interface
48
+ test "#suggested_primary_interface detects interface with user defined bootif fact name" do
49
+ FacterUtils.stubs(:bootif_name).returns('my_primary')
50
+ @parser.facts[:my_primary] = 'aa:bb:cc:dd:ee:f1'
51
+ assert_equal 'eth0', @parser.suggested_primary_interface(@host).try(:first)
51
52
  end
52
53
 
53
- test '#primary_interface_with_discovery_fact falls back if no interface can be found by discovery mac' do
54
- @parser.facts[:mac_fact] = 'cc:00:00:00:00:01'
55
- @parser.stubs(:discovery_mac_fact_name).returns('mac_fact')
56
- assert_equal 'eth0', @parser.primary_interface
54
+ test "#suggested_primary_interface errors out when bootif fact defines unknown MAC" do
55
+ @parser.facts[:discovery_bootif] = 'aa:aa:aa:aa:aa:aa'
56
+ exception = assert_raises(::Foreman::Exception) do
57
+ @parser.suggested_primary_interface(@host)
58
+ end
59
+ assert_match(/Unable to detect primary interface using MAC/, exception.message)
57
60
  end
58
61
 
59
- test '#primary_interface_with_discovery_fact uses macaddress fact if discovery facts not available' do
60
- @parser.stubs(:discovery_mac_fact_name).returns('fact_that_does_not_exist')
61
- assert_equal 'eth1', @parser.primary_interface
62
+ test "#suggested_primary_interface errors out when bootif fact is missing" do
63
+ @parser.facts.delete(:discovery_bootif)
64
+ exception = assert_raises(::Foreman::Exception) do
65
+ @parser.suggested_primary_interface(@host)
66
+ end
67
+ assert_match(/Unable to detect primary interface using MAC/, exception.message)
62
68
  end
69
+
70
+ test "#suggested_primary_interface detects interface even when 'ignore_puppet_facts_for_provisioning' is set" do
71
+ FactoryGirl.create(:setting,
72
+ :name => 'ignore_puppet_facts_for_provisioning',
73
+ :value => true,
74
+ :category => 'Setting::Provisioning')
75
+ assert_equal 'eth2', @parser.suggested_primary_interface(@host).try(:first)
76
+ end
77
+
63
78
  end