foreman_default_hostgroup 4.0.1 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eea2c1504b131e0856539a55695b98550c20114169a60dd36e300a8745e748d9
4
- data.tar.gz: 1f7098318bbb2dc3bab72be79766c80f787922a1ba13542a74d2f8729285c591
3
+ metadata.gz: 7312b3c6f0f24f26aa86fe8930f3c05bb41f844c8b1e968e24a96d980233a5be
4
+ data.tar.gz: 88c9aec69371906eed99689c20729b10e9a0e09a1cf506aa35b6b6fdfd972582
5
5
  SHA512:
6
- metadata.gz: 1091a967380e92d8653b7b80085919371dd03d2538380c950bad23e54a02a890de8c481ab409aceaaa052f539e14c5994c43c52bf99a6896b4961c1083d1246f
7
- data.tar.gz: 8a35fbe829648e2f5e92517dfa519e0287d95604db70c1460e1aac0c1f0df4cdce6460e762b46eafa2ed8d480677f1b54b4e5566840b5877d1997aa9d227afed
6
+ metadata.gz: 126d928a10e32954e5a9e3661c2243b85668f3373da902deace6ce2e1f24b63c90190e5e9b8fe8ceba221bb7e3920a4c29191bd56ad228416b028d6ce52de577
7
+ data.tar.gz: 8b1d8acdf52bf758b689965cd3817bd6f00c05d7043e045f78c4d1f1f527ff4a504fd197acc23c2212213e0ed19a7cf6e73563ece0f0631a8d9ea7ef62be8663
data/README.md CHANGED
@@ -17,6 +17,8 @@ See Foreman's [plugin installation documentation](https://theforeman.org/plugins
17
17
  | 1.5 | 2.0.1 |
18
18
  | 1.6 - 1.11 | 3.0.0 |
19
19
  | >= 1.12 | 4.0.0 |
20
+ | >= 1.16 | 4.0.1 |
21
+ | >= 1.16 | 5.0.0 |
20
22
 
21
23
  ## Usage
22
24
 
@@ -1,34 +1,47 @@
1
1
  module DefaultHostgroupBaseHostPatch
2
2
  extend ActiveSupport::Concern
3
3
 
4
- included do
5
- alias_method_chain :import_facts, :match_hostgroup
4
+ module ManagedOverrides
5
+ def import_facts(facts, source_proxy = nil, without = false)
6
+ super(facts, source_proxy)
7
+ end
6
8
  end
7
9
 
8
- def import_facts_with_match_hostgroup(facts)
9
- # Load the facts anyway, hook onto the end of it
10
- result = import_facts_without_match_hostgroup(facts)
10
+ module Overrides
11
+ def import_facts(facts, source_proxy = nil, without_alias = false)
12
+ # Load the facts anyway, hook onto the end of it
13
+ result = super(facts, source_proxy)
11
14
 
12
- return result unless settings_exist?
15
+ # Module#prepend removes the import_facts_without_match_hostgroup method, so use
16
+ # a flag to return here if needed
17
+ return result if without_alias
13
18
 
14
- Rails.logger.debug 'DefaultHostgroupMatch: performing Hostgroup match'
19
+ # Check settings are created
20
+ return result unless settings_exist?
15
21
 
16
- return result unless host_new_or_forced?
17
- return result unless host_has_no_hostgroup_or_forced?
22
+ Rails.logger.debug 'DefaultHostgroupMatch: performing Hostgroup match'
18
23
 
19
- facts_map = SETTINGS[:default_hostgroup][:facts_map]
20
- new_hostgroup = find_match(facts_map)
24
+ return result unless host_new_or_forced?
25
+ return result unless host_has_no_hostgroup_or_forced?
21
26
 
22
- return result unless new_hostgroup
27
+ facts_map = SETTINGS[:default_hostgroup][:facts_map]
28
+ new_hostgroup = find_match(facts_map)
23
29
 
24
- self.hostgroup = new_hostgroup
25
- if Setting[:force_host_environment] == true
26
- self.environment = new_hostgroup.environment
30
+ return result unless new_hostgroup
31
+
32
+ self.hostgroup = new_hostgroup
33
+ if Setting[:force_host_environment] == true
34
+ self.environment = new_hostgroup.environment
35
+ end
36
+ save(validate: false)
37
+ Rails.logger.info "DefaultHostgroupMatch: #{hostname} added to #{new_hostgroup}"
38
+
39
+ result
27
40
  end
28
- save(validate: false)
29
- Rails.logger.info "DefaultHostgroupMatch: #{hostname} added to #{new_hostgroup}"
41
+ end
30
42
 
31
- result
43
+ included do
44
+ prepend Overrides
32
45
  end
33
46
 
34
47
  def find_match(facts_map)
@@ -1,12 +1,11 @@
1
1
  require 'default_hostgroup_base_host_patch'
2
2
 
3
3
  module ForemanDefaultHostgroup
4
- # Inherit from the Rails module of the parent app (Foreman), not
5
- # the plugin. Thus, inherits from ::Rails::Engine and not from
6
- # Rails::Engine
7
4
  class Engine < ::Rails::Engine
8
5
  engine_name 'foreman_default_hostgroup'
9
6
 
7
+ config.autoload_paths += Dir["#{config.root}/app/models"]
8
+
10
9
  initializer 'foreman_default_hostgroup.load_default_settings',
11
10
  before: :load_config_initializers do
12
11
  require_dependency File.expand_path(
@@ -16,20 +15,18 @@ module ForemanDefaultHostgroup
16
15
  initializer 'foreman_default_hostgroup.register_plugin',
17
16
  before: :finisher_hook do
18
17
  Foreman::Plugin.register :foreman_default_hostgroup do
19
- requires_foreman '>= 1.12'
18
+ requires_foreman '>= 1.17'
20
19
  end
21
20
  end
22
21
 
23
22
  config.to_prepare do
24
23
  begin
25
24
  ::Host::Base.send(:include, DefaultHostgroupBaseHostPatch)
25
+ ::Host::Managed.send(:prepend, DefaultHostgroupBaseHostPatch::ManagedOverrides)
26
26
  rescue => e
27
27
  Rails.logger.warn "ForemanDefaultHostgroup: skipping engine hook (#{e})"
28
28
  end
29
29
  end
30
30
 
31
- rake_tasks do
32
- load 'default_hostgroup.rake'
33
- end
34
31
  end
35
32
  end
@@ -1,3 +1,3 @@
1
1
  module ForemanDefaultHostgroup
2
- VERSION = '4.0.1'.freeze
2
+ VERSION = '5.0.0'.freeze
3
3
  end
@@ -1,11 +1,13 @@
1
+ require 'rake/testtask'
1
2
  # Tests
2
3
  namespace :test do
3
4
  desc "Test DefaultHostgroup plugin"
4
5
  Rake::TestTask.new(:foreman_default_hostgroup) do |t|
5
- test_dir = File.join(File.dirname(__FILE__), '..', 'test')
6
- t.libs << ["test",test_dir]
6
+ test_dir = File.join(File.dirname(__FILE__), '../..', 'test')
7
+ t.libs << ['test', test_dir]
7
8
  t.pattern = "#{test_dir}/**/*_test.rb"
8
9
  t.verbose = true
10
+ t.warning = false
9
11
  end
10
12
  end
11
13
 
@@ -1,6 +1,6 @@
1
1
  # This calls the main test_helper in Foreman-core
2
2
  require 'test_helper'
3
3
 
4
- # Add plugin to FactoryGirl's paths
5
- FactoryGirl.definition_file_paths << File.join(File.dirname(__FILE__), 'factories')
6
- FactoryGirl.reload
4
+ # Add plugin to FactoryBot's paths
5
+ FactoryBot.definition_file_paths << File.join(File.dirname(__FILE__), 'factories')
6
+ FactoryBot.reload
@@ -2,32 +2,36 @@ require 'test_plugin_helper'
2
2
 
3
3
  # Tests for the plugin
4
4
  class DefaultHostgroupTest < ActiveSupport::TestCase
5
+ include FactImporterIsolation
6
+
7
+ allow_transactions_for_any_importer
8
+
5
9
  setup do
6
10
  disable_orchestration
7
- User.current = User.find_by_login 'admin'
11
+ set_admin
8
12
  setup_hostgroup_matchers
9
13
  setup_host_and_facts
10
14
  end
11
15
 
12
16
  def setup_hostgroup_matchers
13
17
  # The settings.yml fixture in Core wipes out the Setting table,
14
- # so we use FactoryGirl to re-create it
15
- FactoryGirl.create(:setting,
16
- name: 'force_hostgroup_match',
17
- category: 'Setting::DefaultHostgroup')
18
- FactoryGirl.create(:setting,
19
- name: 'force_hostgroup_match_only_new',
20
- category: 'Setting::DefaultHostgroup')
21
- FactoryGirl.create(:setting,
22
- name: 'force_host_environment',
23
- category: 'Setting::DefaultHostgroup')
18
+ # so we use FactoryBot to re-create it
19
+ FactoryBot.create(:setting,
20
+ name: 'force_hostgroup_match',
21
+ category: 'Setting::DefaultHostgroup')
22
+ FactoryBot.create(:setting,
23
+ name: 'force_hostgroup_match_only_new',
24
+ category: 'Setting::DefaultHostgroup')
25
+ FactoryBot.create(:setting,
26
+ name: 'force_host_environment',
27
+ category: 'Setting::DefaultHostgroup')
24
28
  # Set the defaults
25
29
  Setting[:force_hostgroup_match] = false
26
30
  Setting[:force_hostgroup_match_only_new] = true
27
31
  Setting[:force_host_environment] = true
28
32
 
29
33
  # Mimic plugin config fron config file
30
- FactoryGirl.create(:hostgroup, :with_environment, name: 'Test Default')
34
+ FactoryBot.create(:hostgroup, :with_environment, name: 'Test Default')
31
35
  SETTINGS[:default_hostgroup] = {}
32
36
  SETTINGS[:default_hostgroup][:facts_map] = {
33
37
  'Test Default' => { 'hostname' => '.*' }
@@ -48,7 +52,7 @@ class DefaultHostgroupTest < ActiveSupport::TestCase
48
52
  end
49
53
 
50
54
  test 'matched host not updated if host already has a hostgroup' do
51
- hostgroup = FactoryGirl.create(:hostgroup)
55
+ hostgroup = FactoryBot.create(:hostgroup)
52
56
  @host.hostgroup = hostgroup
53
57
  @host.save(validate: false)
54
58
 
@@ -67,14 +71,14 @@ class DefaultHostgroupTest < ActiveSupport::TestCase
67
71
 
68
72
  context 'force host environment setting' do
69
73
  test 'environment is updated if enabled' do
70
- h = FactoryGirl.create(:host, :with_environment, created_at: Time.current)
74
+ h = FactoryBot.create(:host, :with_environment, created_at: Time.current)
71
75
  h.import_facts(@facts)
72
76
  assert_equal Hostgroup.find_by_name('Test Default').environment, h.environment
73
77
  end
74
78
 
75
79
  test 'environment not updated if disabled' do
76
80
  Setting[:force_host_environment] = false
77
- h = FactoryGirl.create(:host, :with_environment, created_at: Time.current)
81
+ h = FactoryBot.create(:host, :with_environment, created_at: Time.current)
78
82
  h.import_facts(@facts)
79
83
  refute_equal Hostgroup.find_by_name('Test Default').environment, h.environment
80
84
  end
@@ -84,7 +88,7 @@ class DefaultHostgroupTest < ActiveSupport::TestCase
84
88
  # takes a config map, returns a group or false
85
89
  test 'match a single hostgroup' do
86
90
  facts_map = SETTINGS[:default_hostgroup][:facts_map]
87
- assert @host.import_facts_without_match_hostgroup(@facts)
91
+ assert @host.import_facts(@facts, nil, true)
88
92
  assert_equal Hostgroup.find_by_name('Test Default'), @host.find_match(facts_map)
89
93
  end
90
94
 
@@ -92,7 +96,7 @@ class DefaultHostgroupTest < ActiveSupport::TestCase
92
96
  facts_map = SETTINGS[:default_hostgroup][:facts_map] = {
93
97
  'Test Default' => { 'hostname' => 'nosuchhost' }
94
98
  }
95
- assert @host.import_facts_without_match_hostgroup(@facts)
99
+ assert @host.import_facts(@facts, nil, true)
96
100
  refute @host.find_match(facts_map)
97
101
  end
98
102
 
@@ -101,7 +105,7 @@ class DefaultHostgroupTest < ActiveSupport::TestCase
101
105
  'Test Default' => { 'hostname' => '.*' },
102
106
  'Some Other Group' => { 'hostname' => '/\.lan$/' }
103
107
  }
104
- assert @host.import_facts_without_match_hostgroup(@facts)
108
+ assert @host.import_facts(@facts, nil, true)
105
109
  assert_equal Hostgroup.find_by_name('Test Default'), @host.find_match(facts_map)
106
110
  end
107
111
 
@@ -110,7 +114,7 @@ class DefaultHostgroupTest < ActiveSupport::TestCase
110
114
  'Some Other Group' => { 'hostname' => '.*' },
111
115
  'Test Default' => { 'hostname' => '/\.lan$/' }
112
116
  }
113
- assert @host.import_facts_without_match_hostgroup(@facts)
117
+ assert @host.import_facts(@facts, nil, true)
114
118
  assert_equal Hostgroup.find_by_name('Test Default'), @host.find_match(facts_map)
115
119
  end
116
120
  end
@@ -119,31 +123,31 @@ class DefaultHostgroupTest < ActiveSupport::TestCase
119
123
  # passing a hash of (group_name, regex) pairs
120
124
  test 'full regex matches' do
121
125
  regex = { 'hostname' => '^sinn1636.lan$' }
122
- assert @host.import_facts_without_match_hostgroup(@facts)
126
+ assert @host.import_facts(@facts, nil, true)
123
127
  assert @host.group_matches?(regex)
124
128
  end
125
129
 
126
130
  test 'partial regex matches' do
127
131
  regex = { 'hostname' => '.lan$' }
128
- assert @host.import_facts_without_match_hostgroup(@facts)
132
+ assert @host.import_facts(@facts, nil, true)
129
133
  assert @host.group_matches?(regex)
130
134
  end
131
135
 
132
136
  test 'regex slashes are stripped' do
133
137
  regex = { 'hostname' => '/\.lan$/' }
134
- assert @host.import_facts_without_match_hostgroup(@facts)
138
+ assert @host.import_facts(@facts, nil, true)
135
139
  assert @host.group_matches?(regex)
136
140
  end
137
141
 
138
142
  test 'invalid keys are ignored' do
139
143
  regex = { 'nosuchfact' => '.*' }
140
- assert @host.import_facts_without_match_hostgroup(@facts)
144
+ assert @host.import_facts(@facts, nil, true)
141
145
  refute @host.group_matches?(regex)
142
146
  end
143
147
 
144
148
  test 'unmatched values are ignored' do
145
149
  regex = { 'hostname' => 'nosuchname' }
146
- assert @host.import_facts_without_match_hostgroup(@facts)
150
+ assert @host.import_facts(@facts, nil, true)
147
151
  refute @host.group_matches?(regex)
148
152
  end
149
153
 
@@ -153,66 +157,66 @@ class DefaultHostgroupTest < ActiveSupport::TestCase
153
157
  'osfamily' => 'nosuchos',
154
158
  'hostname' => '.lan$'
155
159
  }
156
- assert @host.import_facts_without_match_hostgroup(@facts)
160
+ assert @host.import_facts(@facts, nil, true)
157
161
  assert @host.group_matches?(regex)
158
162
  end
159
163
  end
160
164
 
161
165
  context 'settings_exist?' do
162
166
  test 'true when Settings exist' do
163
- h = FactoryGirl.create(:host)
167
+ h = FactoryBot.create(:host)
164
168
  assert h.settings_exist?
165
169
  end
166
170
 
167
171
  test 'false when Settings are missing' do
168
172
  SETTINGS[:default_hostgroup] = {}
169
- h = FactoryGirl.create(:host)
173
+ h = FactoryBot.create(:host)
170
174
  refute h.settings_exist?
171
175
  end
172
176
  end
173
177
 
174
178
  context 'host_new_or_forced?' do
175
179
  test 'true when host is new' do
176
- h = FactoryGirl.create(:host, created_at: Time.current)
180
+ h = FactoryBot.create(:host, created_at: Time.current)
177
181
  assert h.host_new_or_forced?
178
182
  end
179
183
 
180
184
  test 'false when host has existed for > 300s' do
181
- h = FactoryGirl.create(:host, created_at: Time.current - 1000)
185
+ h = FactoryBot.create(:host, created_at: Time.current - 1000)
182
186
  refute h.host_new_or_forced?
183
187
  end
184
188
 
185
189
  test 'false when host has a hostgroup' do
186
- h = FactoryGirl.create(:host, :with_hostgroup, created_at: Time.current)
190
+ h = FactoryBot.create(:host, :with_hostgroup, created_at: Time.current)
187
191
  refute h.host_new_or_forced?
188
192
  end
189
193
 
190
194
  test 'false when host has reports' do
191
- h = FactoryGirl.create(:host, :with_reports, created_at: Time.current)
195
+ h = FactoryBot.create(:host, :with_reports, created_at: Time.current)
192
196
  refute h.host_new_or_forced?
193
197
  end
194
198
 
195
199
  test 'true when setting is forced' do
196
200
  Setting[:force_hostgroup_match_only_new] = false
197
- h = FactoryGirl.create(:host, :with_hostgroup, created_at: Time.current)
201
+ h = FactoryBot.create(:host, :with_hostgroup, created_at: Time.current)
198
202
  assert h.host_new_or_forced?
199
203
  end
200
204
  end
201
205
 
202
206
  context 'host_has_no_hostgroup_or_forced?' do
203
207
  test 'true if host has no hostgroup' do
204
- h = FactoryGirl.create(:host)
208
+ h = FactoryBot.create(:host)
205
209
  assert h.host_has_no_hostgroup_or_forced?
206
210
  end
207
211
 
208
212
  test 'false if host has hostgroup' do
209
- h = FactoryGirl.create(:host, :with_hostgroup)
213
+ h = FactoryBot.create(:host, :with_hostgroup)
210
214
  refute h.host_has_no_hostgroup_or_forced?
211
215
  end
212
216
 
213
217
  test 'true if host has hostgroup and setting forced' do
214
218
  Setting[:force_hostgroup_match] = true
215
- h = FactoryGirl.create(:host, :with_hostgroup)
219
+ h = FactoryBot.create(:host, :with_hostgroup)
216
220
  assert h.host_has_no_hostgroup_or_forced?
217
221
  end
218
222
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman_default_hostgroup
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.1
4
+ version: 5.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Greg Sutcliffe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-14 00:00:00.000000000 Z
11
+ date: 2018-05-21 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Adds the option to specify a default hostgroup for new hosts created
14
14
  from facts/reports
@@ -23,11 +23,11 @@ files:
23
23
  - README.md
24
24
  - app/models/setting/default_hostgroup.rb
25
25
  - default_hostgroup.yaml.example
26
- - lib/default_hostgroup.rake
27
26
  - lib/default_hostgroup_base_host_patch.rb
28
27
  - lib/foreman_default_hostgroup.rb
29
28
  - lib/foreman_default_hostgroup/engine.rb
30
29
  - lib/foreman_default_hostgroup/version.rb
30
+ - lib/tasks/foreman_default_hostgroup.rake
31
31
  - test/test_plugin_helper.rb
32
32
  - test/unit/default_hostgroup_test.rb
33
33
  - test/unit/facts.json
@@ -51,11 +51,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
51
  version: '0'
52
52
  requirements: []
53
53
  rubyforge_project:
54
- rubygems_version: 2.7.3
54
+ rubygems_version: 2.7.6
55
55
  signing_key:
56
56
  specification_version: 4
57
57
  summary: Default Hostgroup Plugin for Foreman
58
58
  test_files:
59
59
  - test/test_plugin_helper.rb
60
- - test/unit/facts.json
61
60
  - test/unit/default_hostgroup_test.rb
61
+ - test/unit/facts.json