foreman_puppet 1.0.5 → 2.0.0.alpha.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/concerns/foreman_puppet/extensions/api_hostgroups_controller.rb +14 -16
  3. data/app/controllers/concerns/foreman_puppet/{api/import_puppetclasses_common_controller.rb → extensions/api_smart_proxies_controller.rb} +9 -3
  4. data/app/controllers/concerns/foreman_puppet/extensions/api_template_combinations_controller.rb +23 -1
  5. data/app/controllers/concerns/foreman_puppet/extensions/api_v2_hosts_controller.rb +15 -17
  6. data/app/controllers/concerns/foreman_puppet/extensions/hosts_controller_extensions.rb +0 -1
  7. data/app/controllers/concerns/foreman_puppet/extensions/parameters_host.rb +32 -41
  8. data/app/controllers/concerns/foreman_puppet/extensions/parameters_hostgroup.rb +26 -41
  9. data/app/controllers/foreman_puppet/api/v2/environments_controller.rb +0 -1
  10. data/app/controllers/foreman_puppet/puppetclasses_controller.rb +1 -1
  11. data/app/helpers/foreman_puppet/hosts_and_hostgroups_helper.rb +0 -5
  12. data/app/helpers/foreman_puppet/hosts_helper.rb +6 -10
  13. data/app/models/concerns/foreman_puppet/extensions/host.rb +2 -16
  14. data/app/models/concerns/foreman_puppet/extensions/hostgroup.rb +2 -7
  15. data/app/models/concerns/foreman_puppet/extensions/provisioning_template.rb +7 -12
  16. data/app/models/concerns/foreman_puppet/extensions/template_combination.rb +5 -10
  17. data/app/models/foreman_puppet/host_puppet_facet.rb +1 -1
  18. data/app/models/foreman_puppet/hostgroup_puppet_facet.rb +0 -2
  19. data/app/services/foreman_puppet/puppet_class_importer.rb +0 -6
  20. data/app/views/foreman_puppet/api/v2/host_puppet_facets/host_list.json.rabl +3 -0
  21. data/app/views/foreman_puppet/api/v2/hostgroup_puppet_facets/base.json.rabl +2 -0
  22. data/app/views/foreman_puppet/api/v2/hostgroup_puppet_facets/hostgroup_list.json.rabl +3 -0
  23. data/config/api_routes.rb +1 -9
  24. data/config/initializers/api_reroute.rb +73 -5
  25. data/db/migrate/20101121140000_add_environment_to_template_combinations.foreman_puppet.rb +1 -1
  26. data/db/migrate/20200803113803_migrate_environment_to_puppet_facet.foreman_puppet.rb +2 -0
  27. data/db/migrate/20200803113903_migrate_host_type_in_host_config_groups.foreman_puppet.rb +20 -36
  28. data/db/migrate/20210924103241_remove_katello_id_from_environments.foreman_puppet.rb +5 -0
  29. data/db/migrate_foreman/20090722141107_create_environments.rb +2 -2
  30. data/db/migrate_foreman/20090802062223_create_puppetclasses.rb +28 -0
  31. data/db/migrate_foreman/20090905150132_create_hostgroups_puppetclasses.rb +12 -0
  32. data/db/migrate_foreman/20110412103238_remove_unused_fields_from_puppet_classes.rb +9 -0
  33. data/db/migrate_foreman/20110712070522_create_host_class.rb +11 -0
  34. data/db/migrate_foreman/20120824142048_add_some_indexes.rb +14 -0
  35. data/db/migrate_foreman/20121018152459_create_hostgroup_classes.rb +11 -0
  36. data/db/migrate_foreman/20161205142618_delete_orphaned_smart_class_parameters.rb +1 -1
  37. data/db/migrate_foreman/20180816134832_cast_lookup_key_values.rb +1 -31
  38. data/db/migrate_foreman/20180831115634_add_uniqueness_to_puppetclass_name.rb +24 -0
  39. data/db/migrate_foreman/20181023112532_add_environment_puppetclass_id.rb +1 -1
  40. data/lib/foreman_puppet/engine.rb +4 -36
  41. data/lib/foreman_puppet/register.rb +16 -47
  42. data/lib/foreman_puppet/version.rb +1 -1
  43. data/lib/foreman_puppet.rb +1 -6
  44. data/locale/en/LC_MESSAGES/foreman_puppet.mo +0 -0
  45. data/locale/en/foreman_puppet.edit.po +162 -317
  46. data/locale/en/foreman_puppet.po +62 -8
  47. data/locale/foreman_puppet.pot +215 -133
  48. data/locale/gemspec.rb +1 -1
  49. data/package.json +8 -11
  50. data/test/controllers/foreman_puppet/api/v2/environments_controller_test.rb +0 -231
  51. data/test/controllers/foreman_puppet/api/v2/smart_proxies_controller_test.rb +255 -0
  52. data/test/controllers/foreman_puppet/environments_controller_test.rb +2 -2
  53. data/test/controllers/foreman_puppet/hosts_controller_test.rb +0 -1
  54. data/test/factories/foreman_puppet_factories.rb +0 -2
  55. data/test/helpers/foreman_puppet/hosts_and_hostgroups_helper_test.rb +31 -70
  56. data/test/integration/foreman_puppet/host_js_test.rb +0 -1
  57. data/test/integration/foreman_puppet/puppetclass_js_test.rb +1 -1
  58. data/test/models/foreman_puppet/host_puppet_facet_test.rb +1 -7
  59. data/test/models/foreman_puppet/host_test.rb +26 -0
  60. data/test/models/foreman_puppet/user_test.rb +1 -5
  61. data/test/unit/foreman_puppet/puppet_class_importer_test.rb +1 -1
  62. metadata +22 -13
  63. data/db/migrate/20211112130803_cleanup_environment_from_core_tables.foreman_puppet.rb +0 -10
  64. data/locale/cs_CZ/foreman_puppet.edit.po +0 -1085
  65. data/locale/cs_CZ/foreman_puppet.po.time_stamp +0 -0
data/locale/gemspec.rb CHANGED
@@ -1,2 +1,2 @@
1
1
  # Matches foreman_puppet_enc.gemspec
2
- _('Allow assigning Puppet environmets and classes to the Foreman Hosts.')
2
+ _('Allow assigning Puppet environments and classes to the Foreman Hosts.')
data/package.json CHANGED
@@ -21,25 +21,22 @@
21
21
  "url": "http://projects.theforeman.org/projects/foreman_puppet/issues"
22
22
  },
23
23
  "peerDependencies": {
24
- "@theforeman/vendor": ">= 6.0.0"
24
+ "@theforeman/vendor": "^8.15.0"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@babel/core": "^7.7.0",
28
28
  "@sheerun/mutationobserver-shim": "^0.3.3",
29
- "@theforeman/builder": "^6.0.0",
30
- "@theforeman/eslint-plugin-foreman": "^6.0.0",
31
- "@theforeman/find-foreman": "^4.8.0",
32
- "@theforeman/stories": "^7.0.0",
33
- "@theforeman/test": "^8.0.0",
34
- "@theforeman/vendor-dev": "^6.0.0",
29
+ "@theforeman/builder": "^8.15.0",
30
+ "@theforeman/eslint-plugin-foreman": "^8.15.0",
31
+ "@theforeman/find-foreman": "^8.15.0",
32
+ "@theforeman/stories": "^8.15.0",
33
+ "@theforeman/test": "^8.15.0",
34
+ "@theforeman/vendor-dev": "^8.15.0",
35
35
  "babel-eslint": "^10.0.3",
36
36
  "eslint": "^6.7.2",
37
+ "jed": "^1.1.1",
37
38
  "prettier": "^1.19.1",
38
39
  "stylelint-config-standard": "^18.0.0",
39
40
  "stylelint": "^9.3.0"
40
- },
41
- "dependencies": {
42
- "jed": "^1.1.1",
43
- "react-intl": "^2.8.0"
44
41
  }
45
42
  }
@@ -111,237 +111,6 @@ module ForemanPuppet
111
111
  end
112
112
  assert_response :success
113
113
  end
114
-
115
- describe 'POST #import_puppetclasses' do
116
- # puppetmaster proxy - import_puppetclasses tests
117
-
118
- let(:proxy) { FactoryBot.create(:puppet_smart_proxy) }
119
-
120
- test 'should import new environments' do
121
- setup_import_classes
122
- as_admin do
123
- Host::Managed.update_all(environment_id: nil)
124
- Hostgroup.update_all(environment_id: nil)
125
- Puppetclass.destroy_all
126
- Environment.destroy_all
127
- end
128
- assert_difference('Environment.unscoped.count', 2) do
129
- post :import_puppetclasses, params: { id: proxy.id }, session: set_session_user
130
- end
131
- assert_response :success
132
- response = ActiveSupport::JSON.decode(@response.body)
133
- assert_equal 2, response['environments_with_new_puppetclasses']
134
- end
135
-
136
- # this is intentional: put test_attributes only for the first generated test eg: dryrun_param = {}
137
- test_attributes pid: '385efd1b-6146-47bf-babf-0127ce5955ed'
138
- [{}, { dryrun: false }, { dryrun: 'false' }].each do |dryrun_param|
139
- test 'should import new puppetclasses' do
140
- setup_import_classes
141
- as_admin do
142
- Host::Managed.update_all(environment_id: nil)
143
- Hostgroup.update_all(environment_id: nil)
144
- Puppetclass.destroy_all
145
- Environment.destroy_all
146
- assert_difference(-> { ForemanPuppet::Puppetclass.unscoped.count }, 1) do
147
- post :import_puppetclasses,
148
- params: { id: proxy.id }.merge(dryrun_param),
149
- session: set_session_user
150
- end
151
- end
152
- assert_response :success
153
- assert_match 'Successfully updated environment and puppetclasses from the on-disk puppet installation', @response.body
154
- end
155
- end
156
-
157
- test 'should not import new puppetclasses when dryrun' do
158
- setup_import_classes
159
- as_admin do
160
- Host::Managed.update_all(environment_id: nil)
161
- Hostgroup.update_all(environment_id: nil)
162
- Puppetclass.destroy_all
163
- Environment.destroy_all
164
- assert_difference(-> { ForemanPuppet::Puppetclass.unscoped.count }, 0) do
165
- post :import_puppetclasses, params: { id: proxy.id, dryrun: true }, session: set_session_user
166
- end
167
- end
168
- assert_response :success
169
- end
170
-
171
- test 'should obsolete environment' do
172
- setup_import_classes
173
- as_admin do
174
- FactoryBot.create(:environment, name: 'xyz')
175
- end
176
-
177
- post :import_puppetclasses, params: { id: proxy.id }, session: set_session_user
178
- assert_response :success
179
- as_admin do
180
- assert_nil ForemanPuppet::Environment.find_by(name: 'xyz')
181
- end
182
- end
183
-
184
- test 'should obsolete puppetclasses' do
185
- setup_import_classes
186
- as_admin do
187
- assert_difference(-> { ForemanPuppet::Environment.unscoped.find_by(name: 'env1').puppetclasses.count }, -2) do
188
- post :import_puppetclasses, params: { id: proxy.id }, session: set_session_user
189
- end
190
- end
191
- assert_response :success
192
- end
193
-
194
- test 'should update puppetclass smart class parameters' do
195
- setup_import_classes
196
- LookupKey.destroy_all
197
- assert_difference(-> { LookupKey.unscoped.count }, 1) do
198
- post :import_puppetclasses, params: { id: proxy.id }, session: set_session_user
199
- end
200
- assert_response :success
201
- end
202
-
203
- test 'no changes on import_puppetclasses' do
204
- setup_import_classes
205
- Puppetclass.find_by(name: 'b').destroy
206
- Puppetclass.find_by(name: 'c').destroy
207
- assert_difference(-> { ForemanPuppet::Environment.unscoped.count }, 0) do
208
- post :import_puppetclasses, params: { id: proxy.id }, session: set_session_user
209
- end
210
- assert_response :success
211
- response = ActiveSupport::JSON.decode(@response.body)
212
- assert_equal 'Successfully updated environment and puppetclasses from the on-disk puppet installation', response['message']
213
- end
214
-
215
- test 'should import new environment that does not exist in db' do
216
- setup_import_classes
217
- as_admin do
218
- env_name = 'env1'
219
- assert Environment.find_by(name: env_name).destroy
220
- assert_difference('Environment.unscoped.count', 1) do
221
- post :import_puppetclasses, params: { id: proxy.id, environment_id: env_name }, session: set_session_user
222
- end
223
- assert_response :success
224
- response = ActiveSupport::JSON.decode(@response.body)
225
- assert_equal env_name, response['results']['name']
226
- end
227
- end
228
-
229
- test 'should NOT delete environment if pass ?except=obsolete' do
230
- setup_import_classes
231
- as_admin do
232
- Environment.create!(name: 'xyz')
233
- end
234
- assert_difference(-> { ForemanPuppet::Environment.unscoped.count }, 0) do
235
- post :import_puppetclasses, params: { id: proxy.id, except: 'obsolete' }, session: set_session_user
236
- end
237
- assert_response :success
238
- end
239
-
240
- test 'should NOT add or update puppetclass smart class parameters if pass ?except=new,updated' do
241
- setup_import_classes
242
- LookupKey.destroy_all
243
- assert_difference(-> { LookupKey.unscoped.count }, 0) do
244
- post :import_puppetclasses, params: { id: proxy.id, except: 'new,updated' }, session: set_session_user
245
- end
246
- assert_response :success
247
- end
248
-
249
- context 'import puppetclasses' do
250
- setup do
251
- ProxyAPI::Puppet.any_instance.stubs(:environments).returns(%w[env1 env2])
252
- classes_env1 = { 'a' => Foreman::ImporterPuppetclass.new('name' => 'a') }
253
- classes_env2 = { 'b' => Foreman::ImporterPuppetclass.new('name' => 'b') }
254
- ProxyAPI::Puppet.any_instance.stubs(:classes).returns(classes_env1.merge(classes_env2))
255
- ProxyAPI::Puppet.any_instance.stubs(:classes).with('env1').returns(classes_env1)
256
- ProxyAPI::Puppet.any_instance.stubs(:classes).with('env2').returns(classes_env2)
257
- end
258
-
259
- test 'should render templates according to api version 2' do
260
- post :import_puppetclasses, params: { id: proxy.id }, session: set_session_user
261
- assert_template 'foreman_puppet/api/v2/import_puppetclasses/index'
262
- end
263
-
264
- test 'should import puppetclasses for specified environment only' do
265
- assert_difference(-> { ForemanPuppet::Puppetclass.unscoped.count }, 1) do
266
- post :import_puppetclasses, params: { id: proxy.id, environment_id: 'env1' }, session: set_session_user
267
- assert_includes Puppetclass.pluck(:name), 'a'
268
- assert_not_includes Puppetclass.pluck(:name), 'b'
269
- end
270
- assert_response :success
271
- end
272
-
273
- test 'should import puppetclasses for all environments if none specified' do
274
- assert_difference(-> { ForemanPuppet::Puppetclass.unscoped.count }, 2) do
275
- post :import_puppetclasses, params: { id: proxy.id }, session: set_session_user
276
- end
277
- assert_includes Puppetclass.pluck(:name), 'a'
278
- assert_includes Puppetclass.pluck(:name), 'b'
279
- assert_response :success
280
- end
281
-
282
- context 'ignored entvironments or classes are set' do
283
- setup do
284
- setup_import_classes
285
- end
286
-
287
- test 'should contain ignored environments' do
288
- env_name = 'env1'
289
- PuppetClassImporter.any_instance.stubs(:ignored_environments).returns([env_name])
290
-
291
- as_admin do
292
- post :import_puppetclasses, params: { id: proxy.id }, session: set_session_user
293
- assert_response :success
294
- response = ActiveSupport::JSON.decode(@response.body)
295
- assert_equal env_name, response['results'][0]['ignored_environment']
296
- end
297
- end
298
-
299
- test 'should contain ignored puppet_classes' do
300
- PuppetClassImporter.any_instance.stubs(:ignored_classes).returns([/^a$/])
301
-
302
- as_admin do
303
- post :import_puppetclasses, params: { id: proxy.id }, session: set_session_user
304
- assert_response :success
305
- response = ActiveSupport::JSON.decode(@response.body)
306
- assert_includes response['results'][0]['ignored_puppetclasses'], 'a'
307
- assert_not_includes response['results'][0]['ignored_puppetclasses'], 'c'
308
- end
309
- end
310
- end
311
- end
312
- end
313
-
314
- private
315
-
316
- def setup_import_classes
317
- as_admin do
318
- ::Host::Managed.update_all(environment_id: nil)
319
- ::Hostgroup.update_all(environment_id: nil)
320
- ForemanPuppet::HostClass.destroy_all
321
- ForemanPuppet::HostgroupClass.destroy_all
322
- ForemanPuppet::Puppetclass.destroy_all
323
- ForemanPuppet::Environment.destroy_all
324
- end
325
- orgs = [taxonomies(:organization1)]
326
- locs = [taxonomies(:location1)]
327
- # This is the database status
328
- # and should result in a db_tree of {"env1" => ["a", "b", "c"], "env2" => ["a", "b", "c"]}
329
- as_admin do
330
- %w[a b c].each { |name| ForemanPuppet::Puppetclass.create name: name }
331
- %w[env1 env2].each do |name|
332
- e = ForemanPuppet::Environment.create!(name: name, organizations: orgs, locations: locs)
333
- e.puppetclasses = ForemanPuppet::Puppetclass.all
334
- end
335
- end
336
- # This is the on-disk status
337
- # and should result in a disk_tree of {"env1" => ["a", "b", "c"],"env2" => ["a", "b", "c"]}
338
- envs = HashWithIndifferentAccess.new(env1: %w[a b c], env2: %w[a b c])
339
- pcs = [HashWithIndifferentAccess.new('a' => { 'name' => 'a', 'module' => nil, 'params' => { 'key' => 'special' } })]
340
- classes = pcs.map { |k| [k.keys.first, Foreman::ImporterPuppetclass.new(k.values.first)] }.to_h
341
- ForemanPuppet::Environment.expects(:puppetEnvs).returns(envs).at_least(0)
342
- ProxyAPI::Puppet.any_instance.stubs(:environments).returns(%w[env1 env2])
343
- ProxyAPI::Puppet.any_instance.stubs(:classes).returns(classes)
344
- end
345
114
  end
346
115
  end
347
116
  end
@@ -0,0 +1,255 @@
1
+ require 'test_puppet_helper'
2
+
3
+ module ForemanPuppet
4
+ module Api
5
+ module V2
6
+ class SmartProxiesControllerTest < ActionController::TestCase
7
+ tests ::Api::V2::SmartProxiesController
8
+
9
+ describe 'POST #import_puppetclasses' do
10
+ # puppetmaster proxy - import_puppetclasses tests
11
+
12
+ let(:proxy) { FactoryBot.create(:puppet_smart_proxy) }
13
+
14
+ test 'should import new environments' do
15
+ setup_import_classes
16
+ as_admin do
17
+ HostPuppetFacet.destroy_all
18
+ HostgroupPuppetFacet.destroy_all
19
+ Puppetclass.destroy_all
20
+ Environment.destroy_all
21
+ end
22
+ assert_difference('Environment.unscoped.count', 2) do
23
+ post :import_puppetclasses, params: { id: proxy.id }, session: set_session_user
24
+ end
25
+ assert_response :success
26
+ response = ActiveSupport::JSON.decode(@response.body)
27
+ assert_equal 2, response['environments_with_new_puppetclasses']
28
+ end
29
+
30
+ # this is intentional: put test_attributes only for the first generated test eg: dryrun_param = {}
31
+ test_attributes pid: '385efd1b-6146-47bf-babf-0127ce5955ed'
32
+ [{}, { dryrun: false }, { dryrun: 'false' }].each do |dryrun_param|
33
+ test 'should import new puppetclasses' do
34
+ setup_import_classes
35
+ as_admin do
36
+ HostPuppetFacet.destroy_all
37
+ HostgroupPuppetFacet.destroy_all
38
+ Puppetclass.destroy_all
39
+ Environment.destroy_all
40
+ assert_difference(-> { ForemanPuppet::Puppetclass.unscoped.count }, 1) do
41
+ post :import_puppetclasses,
42
+ params: { id: proxy.id }.merge(dryrun_param),
43
+ session: set_session_user
44
+ end
45
+ end
46
+ assert_response :success
47
+ assert_match 'Successfully updated environment and puppetclasses from the on-disk puppet installation', @response.body
48
+ end
49
+ end
50
+
51
+ test 'should not import new puppetclasses when dryrun' do
52
+ setup_import_classes
53
+ as_admin do
54
+ HostPuppetFacet.destroy_all
55
+ HostgroupPuppetFacet.destroy_all
56
+ Puppetclass.destroy_all
57
+ Environment.destroy_all
58
+ assert_difference(-> { ForemanPuppet::Puppetclass.unscoped.count }, 0) do
59
+ post :import_puppetclasses, params: { id: proxy.id, dryrun: true }, session: set_session_user
60
+ end
61
+ end
62
+ assert_response :success
63
+ end
64
+
65
+ test 'should obsolete environment' do
66
+ setup_import_classes
67
+ as_admin do
68
+ FactoryBot.create(:environment, name: 'xyz')
69
+ end
70
+
71
+ post :import_puppetclasses, params: { id: proxy.id }, session: set_session_user
72
+ assert_response :success
73
+ as_admin do
74
+ assert_nil ForemanPuppet::Environment.find_by(name: 'xyz')
75
+ end
76
+ end
77
+
78
+ test 'should obsolete puppetclasses' do
79
+ setup_import_classes
80
+ as_admin do
81
+ assert_difference(-> { ForemanPuppet::Environment.unscoped.find_by(name: 'env1').puppetclasses.count }, -2) do
82
+ post :import_puppetclasses, params: { id: proxy.id }, session: set_session_user
83
+ end
84
+ end
85
+ assert_response :success
86
+ end
87
+
88
+ test 'should update puppetclass smart class parameters' do
89
+ setup_import_classes
90
+ LookupKey.destroy_all
91
+ assert_difference(-> { LookupKey.unscoped.count }, 1) do
92
+ post :import_puppetclasses, params: { id: proxy.id }, session: set_session_user
93
+ end
94
+ assert_response :success
95
+ end
96
+
97
+ test 'no changes on import_puppetclasses' do
98
+ setup_import_classes
99
+ Puppetclass.find_by(name: 'b').destroy
100
+ Puppetclass.find_by(name: 'c').destroy
101
+ assert_difference(-> { ForemanPuppet::Environment.unscoped.count }, 0) do
102
+ post :import_puppetclasses, params: { id: proxy.id }, session: set_session_user
103
+ end
104
+ assert_response :success
105
+ response = ActiveSupport::JSON.decode(@response.body)
106
+ assert_equal 'Successfully updated environment and puppetclasses from the on-disk puppet installation', response['message']
107
+ end
108
+
109
+ test 'should import new environment that does not exist in db' do
110
+ setup_import_classes
111
+ as_admin do
112
+ env_name = 'env1'
113
+ assert Environment.find_by(name: env_name).destroy
114
+ assert_difference('Environment.unscoped.count', 1) do
115
+ post :import_puppetclasses, params: { id: proxy.id, environment_id: env_name }, session: set_session_user
116
+ end
117
+ assert_response :success
118
+ response = ActiveSupport::JSON.decode(@response.body)
119
+ assert_equal env_name, response['results']['name']
120
+ end
121
+ end
122
+
123
+ test 'should NOT delete environment if pass ?except=obsolete' do
124
+ setup_import_classes
125
+ as_admin do
126
+ Environment.create!(name: 'xyz')
127
+ end
128
+ assert_difference(-> { ForemanPuppet::Environment.unscoped.count }, 0) do
129
+ post :import_puppetclasses, params: { id: proxy.id, except: 'obsolete' }, session: set_session_user
130
+ end
131
+ assert_response :success
132
+ end
133
+
134
+ test 'should NOT add or update puppetclass smart class parameters if pass ?except=new,updated' do
135
+ setup_import_classes
136
+ LookupKey.destroy_all
137
+ assert_difference(-> { LookupKey.unscoped.count }, 0) do
138
+ post :import_puppetclasses, params: { id: proxy.id, except: 'new,updated' }, session: set_session_user
139
+ end
140
+ assert_response :success
141
+ end
142
+
143
+ context 'import puppetclasses' do
144
+ setup do
145
+ ProxyAPI::Puppet.any_instance.stubs(:environments).returns(%w[env1 env2])
146
+ classes_env1 = { 'a' => Foreman::ImporterPuppetclass.new('name' => 'a') }
147
+ classes_env2 = { 'b' => Foreman::ImporterPuppetclass.new('name' => 'b') }
148
+
149
+ ProxyAPI::Puppet.any_instance.stubs(:classes).with('invalid').returns({})
150
+ ProxyAPI::Puppet.any_instance.stubs(:classes).with('env1').returns(classes_env1)
151
+ ProxyAPI::Puppet.any_instance.stubs(:classes).with('env2').returns(classes_env2)
152
+ end
153
+
154
+ test 'should render templates according to api version 2' do
155
+ post :import_puppetclasses, params: { id: proxy.id }, session: set_session_user
156
+ assert_template 'foreman_puppet/api/v2/import_puppetclasses/index'
157
+ end
158
+
159
+ test 'should import puppetclasses for specified environment only' do
160
+ assert_difference(-> { ForemanPuppet::Puppetclass.unscoped.count }, 1) do
161
+ post :import_puppetclasses, params: { id: proxy.id, environment_id: 'env1' }, session: set_session_user
162
+ assert_includes Puppetclass.pluck(:name), 'a'
163
+ assert_not_includes Puppetclass.pluck(:name), 'b'
164
+ end
165
+ assert_response :success
166
+ end
167
+
168
+ test 'should render error message when invalid environment is set' do
169
+ env = 'invalid'
170
+ assert_difference(-> { ForemanPuppet::Puppetclass.unscoped.count }, 0) do
171
+ post :import_puppetclasses, params: { id: proxy.id, environment_id: env }, session: set_session_user
172
+ assert_not_includes Puppetclass.pluck(:name), 'a'
173
+ assert_not_includes Puppetclass.pluck(:name), 'b'
174
+ end
175
+ response = ActiveSupport::JSON.decode(@response.body)
176
+ assert_equal 'The requested environment cannot be found.', response['message']
177
+ assert_response :success
178
+ end
179
+
180
+ test 'should import puppetclasses for all environments if none specified' do
181
+ assert_difference(-> { ForemanPuppet::Puppetclass.unscoped.count }, 2) do
182
+ post :import_puppetclasses, params: { id: proxy.id }, session: set_session_user
183
+ end
184
+ assert_includes Puppetclass.pluck(:name), 'a'
185
+ assert_includes Puppetclass.pluck(:name), 'b'
186
+ assert_response :success
187
+ end
188
+
189
+ context 'ignored environments or classes are set' do
190
+ setup do
191
+ setup_import_classes
192
+ end
193
+
194
+ test 'should contain ignored environments' do
195
+ env_name = 'env1'
196
+ PuppetClassImporter.any_instance.stubs(:ignored_environments).returns([env_name])
197
+
198
+ as_admin do
199
+ post :import_puppetclasses, params: { id: proxy.id }, session: set_session_user
200
+ assert_response :success
201
+ response = ActiveSupport::JSON.decode(@response.body)
202
+ assert_equal env_name, response['results'][0]['ignored_environment']
203
+ end
204
+ end
205
+
206
+ test 'should contain ignored puppet_classes' do
207
+ PuppetClassImporter.any_instance.stubs(:ignored_classes).returns([/^a$/])
208
+
209
+ as_admin do
210
+ post :import_puppetclasses, params: { id: proxy.id }, session: set_session_user
211
+ assert_response :success
212
+ response = ActiveSupport::JSON.decode(@response.body)
213
+ assert_includes response['results'][0]['ignored_puppetclasses'], 'a'
214
+ assert_not_includes response['results'][0]['ignored_puppetclasses'], 'c'
215
+ end
216
+ end
217
+ end
218
+ end
219
+ end
220
+
221
+ private
222
+
223
+ def setup_import_classes
224
+ as_admin do
225
+ HostPuppetFacet.destroy_all
226
+ HostgroupPuppetFacet.destroy_all
227
+ ForemanPuppet::HostClass.destroy_all
228
+ ForemanPuppet::HostgroupClass.destroy_all
229
+ ForemanPuppet::Puppetclass.destroy_all
230
+ ForemanPuppet::Environment.destroy_all
231
+ end
232
+ orgs = [taxonomies(:organization1)]
233
+ locs = [taxonomies(:location1)]
234
+ # This is the database status
235
+ # and should result in a db_tree of {"env1" => ["a", "b", "c"], "env2" => ["a", "b", "c"]}
236
+ as_admin do
237
+ %w[a b c].each { |name| ForemanPuppet::Puppetclass.create name: name }
238
+ %w[env1 env2].each do |name|
239
+ e = ForemanPuppet::Environment.create!(name: name, organizations: orgs, locations: locs)
240
+ e.puppetclasses = ForemanPuppet::Puppetclass.all
241
+ end
242
+ end
243
+ # This is the on-disk status
244
+ # and should result in a disk_tree of {"env1" => ["a", "b", "c"],"env2" => ["a", "b", "c"]}
245
+ envs = HashWithIndifferentAccess.new(env1: %w[a b c], env2: %w[a b c])
246
+ pcs = [HashWithIndifferentAccess.new('a' => { 'name' => 'a', 'module' => nil, 'params' => { 'key' => 'special' } })]
247
+ classes = pcs.map { |k| [k.keys.first, Foreman::ImporterPuppetclass.new(k.values.first)] }.to_h
248
+ ForemanPuppet::Environment.expects(:puppetEnvs).returns(envs).at_least(0)
249
+ ProxyAPI::Puppet.any_instance.stubs(:environments).returns(%w[env1 env2])
250
+ ProxyAPI::Puppet.any_instance.stubs(:classes).returns(classes)
251
+ end
252
+ end
253
+ end
254
+ end
255
+ end
@@ -194,8 +194,8 @@ module ForemanPuppet
194
194
 
195
195
  def setup_import_classes
196
196
  as_admin do
197
- Host::Managed.all.each { |h| h.update(environment_id: nil) }
198
- Hostgroup.all.each { |h| h.update(environment_id: nil) }
197
+ HostPuppetFacet.destroy_all
198
+ HostgroupPuppetFacet.destroy_all
199
199
  Puppetclass.destroy_all
200
200
  Environment.destroy_all
201
201
  end
@@ -48,7 +48,6 @@ module ForemanPuppet
48
48
  setup { @routes = Rails.application.routes }
49
49
 
50
50
  test 'lookup value and description should be html escaped' do
51
- skip 'Needs complete migration to be done' unless ForemanPuppet.extracted_from_core?
52
51
  FactoryBot.create(:puppetclass_lookup_key,
53
52
  default_value: "<script>alert('hacked!');</script>",
54
53
  description: "<script>alert('hacked!');</script>",
@@ -105,8 +105,6 @@ FactoryBot.define do
105
105
  evaluator.parameter_count.times do
106
106
  evaluator.environments.each do |env|
107
107
  FactoryBot.create :puppetclass_lookup_key, override: false, puppetclass: pc, environment: env
108
- # for Host#managed validation, once extracted_from_core? we can remove
109
- env.reload
110
108
  end
111
109
  end
112
110
  else